From patchwork Mon Oct 21 12:54:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13844098 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 890311EB9FC; Mon, 21 Oct 2024 12:56:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515389; cv=none; b=tShUNNuJhjJr+1SBm4jFy9Hq3vwP7J0wfvsUFEICJLFrEa+QK965Q34ddkfXe/ZCzrrIiT5C291jo8d662uuhYi5Lfy7rT75JJf9pQeV/C72rxa+z3GnuWxiHYTANyfSb98fkmKdJy/3gAI/cSDDZdNAjM+rExD9jJ5oCXWkn5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515389; c=relaxed/simple; bh=0ClYzGYWuo9R4ytQT8+YomjzZyyvK/shpte3TSu50RA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=M6XclNvg3mzALkx8dc152QSrnmdm0byEevaQOvaSMe+pmmYLHQcx4EQYjhgW+vRQfmlXJNH4WyYBCnJ5r+c5zCwW7p/Dvb5FwXx40E8hm9sA5hPPBtW55gf2JVa9Cy/yELpgHs6tgDGLS2dwRETtrJ+SxHisJ4adSADZfJB2RSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WmzM8OMK; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WmzM8OMK" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4314fa33a35so45598175e9.1; Mon, 21 Oct 2024 05:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729515386; x=1730120186; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=M4DbDXoWuKEPG/SkjaxUaXOQ8tk/bgeFy/xVB16h3Gg=; b=WmzM8OMKa4chw4UtNMIkyqRM6W+MxkBbMEre5NVw6cFckW2SfuoFH2P4nw5EuFdKFQ 4jnFjKXZx4ZCVAt7ZAx3waKavoJ1w8ZFXJ9ChGWAL1fjVI4VLwUtIpmYfHtqFhtV41MS S+crv52UN5BD10hvzFGOVqyFqwf9I+BrkvhevuhuSIM/SZPDYZ9IBA+GsSYOPiLWYK8U jzD6ZCobqfv2/MdgL6gT0rPlYOLyR26gESLtdOgA49Wd9SoC7BHG6y9r7XC4Ht3gNBA9 8wptuKzNw1V3259E0PZPM1QJxKoU4tdhbSS4BlfQlLUq7r8cBCaxxqbo0D0cS3uij6Fd X3mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729515386; x=1730120186; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M4DbDXoWuKEPG/SkjaxUaXOQ8tk/bgeFy/xVB16h3Gg=; b=HlEVEXK4dHXCok8PQburPUJSM2Q0nahtKc9SYjwkUB8TZxNJ9sKvdKzoMURqCY/gcH RBylTQMecXSNJ9AcyYq+UeZokDjVWiUbpYXNMG8yvLYaOjV8WjeTqPkneHhBo+/6SAVy o1ugrC6+ec7mOGKCT/9Dtah5akgy+44s063y4AOxVEP5mbulZk+i+6yJiwJaAiIFh7Ug Dh/+p1XBJnKnjWHal48/x9MDKIE7H00X3KgkBgrEaiOrRa+VfsMarM70uviEqoPBFMhE Ppo8fxNExbxkPkQePmMkVKv8T8J8sX6PtuNvHRVIWJ7eHuKbT1D2fSFm0iVm9FxuULXj 0w2g== X-Forwarded-Encrypted: i=1; AJvYcCVR1ai9mNHq6vwcAPWtBxTi74b8IoyxzDezrjJfGVraxfLJ61BqLSrh2oTolt3DXsXoTmuahZlwMJdnWiU=@vger.kernel.org, AJvYcCW2hT+EGuYCLdwFelExaidHMAZwaaDhwnU320GeoRMHGMoj+hv8rd/OQx7RkeJo3wn8pbdgTiZ58K0=@vger.kernel.org, AJvYcCWzmza8AXjDsGxSI90z9hRBRS8Nb9DZTZDwIeefX3LC84/jF1UnMM3U2LKI32B6FQCcf8CIdubLzOYilg==@vger.kernel.org X-Gm-Message-State: AOJu0YzPx29M6813NAXlhbbg4nr+Cf1BYzXRzo0k3nqs9kwwiUC6rma3 lWQzWB5J0YwXNlxSxHDSy5q2aEptI3+7UD190drYqpgxJOgABO3h X-Google-Smtp-Source: AGHT+IGZ8MiqwunkQyNVTGOJ9VHraenoSHSD98vz/0FpWLI2bapcbutIDFCTUd2ygH4Rgow6TNUO/w== X-Received: by 2002:a05:6000:1f0a:b0:374:c8e5:d568 with SMTP id ffacd0b85a97d-37eab6ee8abmr6798456f8f.29.1729515385527; Mon, 21 Oct 2024 05:56:25 -0700 (PDT) Received: from localhost (host-82-56-18-47.retail.telecomitalia.it. [82.56.18.47]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f570dd5sm57210805e9.3.2024.10.21.05.56.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 05:56:25 -0700 (PDT) From: Matteo Martelli Date: Mon, 21 Oct 2024 14:54:14 +0200 Subject: [PATCH v5 1/5] iio: core: add read_avail_release_resource callback to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241021-iio-read-avail-release-v5-1-b168713fab33@gmail.com> References: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> In-Reply-To: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 Some iio drivers currently share an available info buffer that might be changed while iio core prints it to sysfs. To avoid the buffer corruption, add a release callback to let iio drivers allocate a copy of the available info buffer and later free it in the release callback. Such control is kept in the driver logic so that some driver that needs a big available info buffer might also perform some check to keep the copied buffer around in case no race has occurred. Signed-off-by: Matteo Martelli --- drivers/iio/industrialio-core.c | 14 +++++++++++--- include/linux/iio/iio.h | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 6a6568d4a2cb3a3f63381d5a6f25a2881b3ba2ed..4aea9de9f15a4d70f9d02fb3d47df49eef8c8423 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -859,12 +859,20 @@ static ssize_t iio_read_channel_info_avail(struct device *dev, return ret; switch (ret) { case IIO_AVAIL_LIST: - return iio_format_avail_list(buf, vals, type, length); + ret = iio_format_avail_list(buf, vals, type, length); + break; case IIO_AVAIL_RANGE: - return iio_format_avail_range(buf, vals, type); + ret = iio_format_avail_range(buf, vals, type); + break; default: - return -EINVAL; + ret = -EINVAL; } + + if (indio_dev->info->read_avail_release_resource) + indio_dev->info->read_avail_release_resource( + indio_dev, this_attr->c, vals, this_attr->address); + + return ret; } /** diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 3a9b57187a958d6e65c699cf7814df5bac9a99e3..03bb765670a0c5f0129fc677c3a4a4cb38f4dad1 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -491,6 +491,10 @@ struct iio_info { int *length, long mask); + void (*read_avail_release_resource)(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask); + int (*write_raw)(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, From patchwork Mon Oct 21 12:54:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13844099 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE47A1F5854; Mon, 21 Oct 2024 12:56:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515391; cv=none; b=s7eBOiwNpeCXoGVFoaYq27FPKatSjHCFdzwTSezW4Q/hCoYkl1k1+UCRI8iqTAeEbDPRFU0F9neFCLDuAAN2c6edy3wWIQxWEEMoQHmwOna3ymEKe9C23UKwAsysCeyhByRsCy9YVrBmo1dckQ17SPk6Xp5E8FI6TwtfvOAF/n4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515391; c=relaxed/simple; bh=JfFB3V4BVK38Q4fAhaF1uwjuIrMsvLUeWs/YXuG+ZFE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Tkm7vaBb07czw9dMwihWQ4XmqW+gdU+lKNvBhd6W6Ss8ddvBS60Wbg8EngBrkzUibTyy7cOJ9GwMUQa72ls6nAsmJONGpv5EHMMiEBbgLAi25eAG5Gm+WquCcC5Pt6KX+Ig8ChObDwwk5UbP11vajIpYUcg3HAu6dTNZiq73jX8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kzmgTG/T; arc=none smtp.client-ip=209.85.221.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kzmgTG/T" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-37d70df0b1aso3566345f8f.3; Mon, 21 Oct 2024 05:56:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729515387; x=1730120187; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=R+7zY7pVPoAV0E1XWnpSqHgCSXjFqEo7qS0+dawRshU=; b=kzmgTG/T/CidOqKqmgNopTaCVMY44+J3i13SnnOmRTciDnD8ph09VHlDWQWSZa9sRi bmk2lCQqonzpcs7ZJdvHT/d5TTP3qdez9Rp13ymC8HtJJOdOXnValtgrysJF9aWJuTGL rF4pSuwqHqltbcawK47IFeWxDlZsIfWlwOtQPEdPOsHWJNwg5UjGeA5KMxvDdXRrbsk7 RK+bQ+Qn5b+Hi+Z6zjFxtU/kxg0iX9niQcqPeKgk6b+Fw3h2ehZwPTLMxPA2mGEKI9yh cAPVw7s2ke9p8oshWYHdxgPUpwii6RDp4l8dggsAJrZLzbEh6zHvqqG7pN771D+Vtxqk aHJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729515387; x=1730120187; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=R+7zY7pVPoAV0E1XWnpSqHgCSXjFqEo7qS0+dawRshU=; b=euoXT0SX0NHzFbnU3NFTp1YgiZf37KGFTNEozbzCkZYU1xGblhzpFLT4nDFXrRC0EV 9wBjnVnNmjbiAX8pw9IsXuunrWZJADm7JfdVKbo5PxAHyMu4nFoNlFDSsMyM62UxHXsb 9iZtBEluHflbj5FTek5ARWFqhXqMjyF7Ljan9YVwYWI9W8ONcEg23I0xj57jxQF7D39m wgDWC86FfEBkQQbx3RSVaxq2TGD+J3bx+l+ocEacFYKT80qr+71YJP8cgwYNvNxlCtfU qP/z6o36a/xXb0dK5RZ1F1etimhSWCoPD9guexBgnrs2S1cbh1vE2/cKbMS7/9hRt8wD 1aVQ== X-Forwarded-Encrypted: i=1; AJvYcCURykNm2wLqV6qPxMtiz8SB+sCNlOfJLDv7WJrZNleKJvxa8OfZXaXXI2dJ8bSvlI/2ZSkjGfaQr70=@vger.kernel.org, AJvYcCWqrGxlIdNN4IBjqZCvHv+fxoxHqUuXoaYXDcJYzzcC9JtXQ+FnEHbbkgar0xjMMBNfubPQL6LcbvTemg==@vger.kernel.org, AJvYcCXW4esQJ++5b/tpu4WRVLyF8XtnAVTODvnWSYzSlsDaXaFQxcL4ViL3StDxkIvQ6/7rBDW1dN/iuqS8MSE=@vger.kernel.org X-Gm-Message-State: AOJu0YwjbuCyP9TEpPEjhmGnYdPeWz1zR+YomfHa68sQ0zjliu1tJwF1 HNQPWSVYuG0RFbA6FLYVUU9oO9Vq4EsEY9fMauPtSEoyTh9ZJikP X-Google-Smtp-Source: AGHT+IHP7sHgIC6w6W7OP4fpp7leuM9MQnc1DMJ3jIsQEduXzpQ2ws49ONw1AkmemZt1VIkFsmoeuQ== X-Received: by 2002:a5d:60cd:0:b0:37d:52d0:a59d with SMTP id ffacd0b85a97d-37eab72732cmr7286276f8f.10.1729515386783; Mon, 21 Oct 2024 05:56:26 -0700 (PDT) Received: from localhost (host-82-56-18-47.retail.telecomitalia.it. [82.56.18.47]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f5cc401sm55615835e9.43.2024.10.21.05.56.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 05:56:26 -0700 (PDT) From: Matteo Martelli Date: Mon, 21 Oct 2024 14:54:15 +0200 Subject: [PATCH v5 2/5] iio: consumers: copy/release available info from producer to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241021-iio-read-avail-release-v5-2-b168713fab33@gmail.com> References: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> In-Reply-To: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 Consumers need to call the producer's read_avail_release_resource() callback after reading producer's available info. To avoid a race condition with the producer unregistration, change inkern iio_channel_read_avail() so that it copies the available info from the producer and immediately calls its release callback with info_exists locked. Also, modify the users of iio_read_avail_channel_raw() and iio_read_avail_channel_attribute() to free the copied available buffers after calling these functions. To let users free the copied buffer with a cleanup pattern, also add a iio_read_avail_channel_attr_retvals() consumer helper that is equivalent to iio_read_avail_channel_attribute() but stores the available values in the returned variable. Signed-off-by: Matteo Martelli --- drivers/iio/afe/iio-rescale.c | 8 +++ drivers/iio/dac/dpot-dac.c | 8 +++ drivers/iio/inkern.c | 99 ++++++++++++++++++++++++++++------ drivers/iio/multiplexer/iio-mux.c | 8 +++ drivers/power/supply/ingenic-battery.c | 22 +++++--- include/linux/iio/consumer.h | 28 +++++++++- 6 files changed, 147 insertions(+), 26 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 56e5913ab82d1c045c9ca27012008a4495502cbf..78bb86c291706748b4072a484532ad20c415ff9f 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -249,9 +249,17 @@ static int rescale_read_avail(struct iio_dev *indio_dev, } } +static void rescale_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + kfree(vals); +} + static const struct iio_info rescale_info = { .read_raw = rescale_read_raw, .read_avail = rescale_read_avail, + .read_avail_release_resource = rescale_read_avail_release_res, }; static ssize_t rescale_read_ext_info(struct iio_dev *indio_dev, diff --git a/drivers/iio/dac/dpot-dac.c b/drivers/iio/dac/dpot-dac.c index f36f10bfb6be7863a56b911b5f58671ef530c977..43d68e17fc3a5fca59fad6ccf818eeadfecdb8c1 100644 --- a/drivers/iio/dac/dpot-dac.c +++ b/drivers/iio/dac/dpot-dac.c @@ -108,6 +108,13 @@ static int dpot_dac_read_avail(struct iio_dev *indio_dev, return -EINVAL; } +static void dpot_dac_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + kfree(vals); +} + static int dpot_dac_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) @@ -125,6 +132,7 @@ static int dpot_dac_write_raw(struct iio_dev *indio_dev, static const struct iio_info dpot_dac_info = { .read_raw = dpot_dac_read_raw, .read_avail = dpot_dac_read_avail, + .read_avail_release_resource = dpot_dac_read_avail_release_res, .write_raw = dpot_dac_write_raw, }; diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 7f325b3ed08fae6674245312cf8f57bb151006c0..7b87d1c57d6fd1258d254192835aa6cb9355f859 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -760,12 +760,56 @@ static int iio_channel_read_avail(struct iio_channel *chan, if (!iio_channel_has_available(chan->channel, info)) return -EINVAL; - if (iio_info->read_avail) - return iio_info->read_avail(chan->indio_dev, chan->channel, - vals, type, length, info); + if (iio_info->read_avail) { + const int *vals_tmp; + int ret; + + ret = iio_info->read_avail(chan->indio_dev, chan->channel, + &vals_tmp, type, length, info); + if (ret < 0) + return ret; + + /* + * Copy the producer's avail buffer with lock_exists locked to + * avoid possible race with producer unregistration. + */ + *vals = kmemdup_array(vals_tmp, *length, sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + + if (iio_info->read_avail_release_resource) + iio_info->read_avail_release_resource( + chan->indio_dev, chan->channel, vals_tmp, info); + + return ret; + } return -EINVAL; } +/* + * iio_channel_read_avail_retvals() is equivalent to iio_channel_read_avail() + * but stores the pointer to the buffer of available values in the returned + * variable. Since such buffer must be freed after use, this function lets the + * user declare a cleanup local variable, e.g.: + * const int *vals = __free(kfree) = iio_channel_read_avail_retvals(...); + */ +static const int *iio_channel_read_avail_retvals(struct iio_channel *chan, + int *type, int *length, + int *avail_type, + enum iio_chan_info_enum info) +{ + const int *vals; + int ret; + + ret = iio_channel_read_avail(chan, &vals, type, length, info); + if (ret < 0) + return ERR_PTR(ret); + + *avail_type = ret; + + return vals; +} + int iio_read_avail_channel_attribute(struct iio_channel *chan, const int **vals, int *type, int *length, enum iio_chan_info_enum attribute) @@ -780,6 +824,25 @@ int iio_read_avail_channel_attribute(struct iio_channel *chan, } EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute); +const int * +iio_read_avail_channel_attr_retvals(struct iio_channel *chan, int *type, + int *length, int *avail_type, + enum iio_chan_info_enum attribute) +{ + const int *vals; + int ret; + + ret = iio_read_avail_channel_attribute(chan, &vals, type, length, + attribute); + if (ret < 0) + return ERR_PTR(ret); + + *avail_type = ret; + + return vals; +} +EXPORT_SYMBOL_GPL(iio_read_avail_channel_attr_retvals); + int iio_read_avail_channel_raw(struct iio_channel *chan, const int **vals, int *length) { @@ -789,9 +852,11 @@ int iio_read_avail_channel_raw(struct iio_channel *chan, ret = iio_read_avail_channel_attribute(chan, vals, &type, length, IIO_CHAN_INFO_RAW); - if (ret >= 0 && type != IIO_VAL_INT) + if (ret >= 0 && type != IIO_VAL_INT) { /* raw values are assumed to be IIO_VAL_INT */ + kfree(*vals); ret = -EINVAL; + } return ret; } @@ -801,15 +866,16 @@ static int iio_channel_read_max(struct iio_channel *chan, int *val, int *val2, int *type, enum iio_chan_info_enum info) { - const int *vals; int length; - int ret; + int avail_type; - ret = iio_channel_read_avail(chan, &vals, type, &length, info); - if (ret < 0) - return ret; + const int *vals __free(kfree) = + iio_channel_read_avail_retvals(chan, type, &length, + &avail_type, info); + if (IS_ERR(vals)) + return PTR_ERR(vals); - switch (ret) { + switch (avail_type) { case IIO_AVAIL_RANGE: switch (*type) { case IIO_VAL_INT: @@ -857,15 +923,16 @@ static int iio_channel_read_min(struct iio_channel *chan, int *val, int *val2, int *type, enum iio_chan_info_enum info) { - const int *vals; int length; - int ret; + int avail_type; - ret = iio_channel_read_avail(chan, &vals, type, &length, info); - if (ret < 0) - return ret; + const int *vals __free(kfree) = + iio_channel_read_avail_retvals(chan, type, &length, + &avail_type, info); + if (IS_ERR(vals)) + return PTR_ERR(vals); - switch (ret) { + switch (avail_type) { case IIO_AVAIL_RANGE: switch (*type) { case IIO_VAL_INT: diff --git a/drivers/iio/multiplexer/iio-mux.c b/drivers/iio/multiplexer/iio-mux.c index 2953403bef53bbe47a97a8ab1c475ed88d7f86d2..31345437784b01c5d6f8ea70263f4c2574388e7a 100644 --- a/drivers/iio/multiplexer/iio-mux.c +++ b/drivers/iio/multiplexer/iio-mux.c @@ -142,6 +142,13 @@ static int mux_read_avail(struct iio_dev *indio_dev, return ret; } +static void mux_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + kfree(vals); +} + static int mux_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) @@ -171,6 +178,7 @@ static int mux_write_raw(struct iio_dev *indio_dev, static const struct iio_info mux_info = { .read_raw = mux_read_raw, .read_avail = mux_read_avail, + .read_avail_release_resource = mux_read_avail_release_res, .write_raw = mux_write_raw, }; diff --git a/drivers/power/supply/ingenic-battery.c b/drivers/power/supply/ingenic-battery.c index 0a40f425c27723ccec49985b8b5e14a737b6a7eb..5d82c799ba5af3302bfcdfadb5f57adf6b12b353 100644 --- a/drivers/power/supply/ingenic-battery.c +++ b/drivers/power/supply/ingenic-battery.c @@ -6,12 +6,14 @@ * based on drivers/power/supply/jz4740-battery.c */ +#include #include #include #include #include #include #include +#include struct ingenic_battery { struct device *dev; @@ -62,8 +64,8 @@ static int ingenic_battery_get_property(struct power_supply *psy, */ static int ingenic_battery_set_scale(struct ingenic_battery *bat) { - const int *scale_raw; - int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret; + int scale_len, scale_type, scale_avail_type; + int best_idx = -1, best_mV, max_raw, i, ret; u64 max_mV; ret = iio_read_max_channel_raw(bat->channel, &max_raw); @@ -72,14 +74,18 @@ static int ingenic_battery_set_scale(struct ingenic_battery *bat) return ret; } - ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw, - &scale_type, &scale_len, - IIO_CHAN_INFO_SCALE); - if (ret < 0) { + const int *scale_raw __free(kfree) = + iio_read_avail_channel_attr_retvals(bat->channel, + &scale_type, + &scale_len, + &scale_avail_type, + IIO_CHAN_INFO_SCALE); + if (IS_ERR(scale_raw)) { dev_err(bat->dev, "Unable to read channel avail scale\n"); - return ret; + return PTR_ERR(scale_raw); } - if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2) + if (scale_avail_type != IIO_AVAIL_LIST || + scale_type != IIO_VAL_FRACTIONAL_LOG2) return -EINVAL; max_mV = bat->info->voltage_max_design_uv / 1000; diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index 333d1d8ccb37f387fe531577ac5e0bfc7f752cec..188cc64609bd1fd8e0522e69f0b65a73b9b1606d 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -316,7 +316,7 @@ int iio_read_min_channel_raw(struct iio_channel *chan, int *val); /** * iio_read_avail_channel_raw() - read available raw values from a given channel * @chan: The channel being queried. - * @vals: Available values read back. + * @vals: Available values read back. Must be freed after use. * @length: Number of entries in vals. * * Returns an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST. @@ -334,7 +334,7 @@ int iio_read_avail_channel_raw(struct iio_channel *chan, /** * iio_read_avail_channel_attribute() - read available channel attribute values * @chan: The channel being queried. - * @vals: Available values read back. + * @vals: Available values read back. Must be freed after use. * @type: Type of values read back. * @length: Number of entries in vals. * @attribute: info attribute to be read back. @@ -345,6 +345,30 @@ int iio_read_avail_channel_attribute(struct iio_channel *chan, const int **vals, int *type, int *length, enum iio_chan_info_enum attribute); + +/** + * iio_read_avail_channel_attr_retvals() - read available channel attr values + * @chan: The channel being queried. + * @type: Type of values read back. + * @length: Number of entries in vals. + * @avail_type: Available type of values read back + * (IIO_AVAIL_RANGE or IIO_AVAIL_LIST). + * @attribute: info attribute to be read back. + * + * This function is equivalent to iio_read_avail_channel_attribute() but stores + * the pointer to the buffer of available values in the returned variable. + * Since such buffer must be freed after use, this function lets the user + * declare a cleanup local variable, e.g.: + * const int *vals = __free(kfree) = iio_read_avail_channel_attr_retvals(...); + * + * Returns a pointer to negative errno on error otherwise returns the available + * values read back that must be freed after use. + */ +const int * +iio_read_avail_channel_attr_retvals(struct iio_channel *chan, int *type, + int *length, int *avail_type, + enum iio_chan_info_enum attribute); + /** * iio_get_channel_type() - get the type of a channel * @channel: The channel being queried. From patchwork Mon Oct 21 12:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13844100 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CECE31F7074; Mon, 21 Oct 2024 12:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515392; cv=none; b=LXz6pSOL4mOn4HOf5g6AZTU70EV1hacjsJPyoeXqfcrY1UxaZ8nWW+NsRgnS677OlrxeFt0weVKdnzokEmUpiZy8UTmuPTAq/N4Ox6tT/KBhK/zWltZGs9dEL8eyz9F+OwLW9kswqt1W3IzFY+Pe0yLBGLO1Y0qeV3ek3JTkqfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515392; c=relaxed/simple; bh=QdYDXg+F9crN+GcA1VSjP47ejtoSADQbLgM8+WYO3yU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eeMNP4dZvL0JgwqJaXTsNNKC+kxmgcLy7ZocBHf0hrzHUwovepWrpxaO5J40JyBZvD93hbel/dygf+qkHhhLR3J+kI4SDco8f8obnp3KelmmpLk93yTmx7j0krhmyDt34BDa2h7gFWzfih2EDIfJPZdOKhGrItP5v+mknfER/Eg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XZKhUeS9; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XZKhUeS9" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-43161c0068bso30493535e9.1; Mon, 21 Oct 2024 05:56:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729515388; x=1730120188; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=sBrQ02u0x+gOef7QkcexjhixwvYLcPbk6l/zE/g/i1o=; b=XZKhUeS9P3Y3CoBlmhHWpilI/q5A65sHVeFSmfUgBtvw5ZmiWr+NDYKc+l2XqOW62u wI/Wn4iDNy767InY3NMdvez2QHE0GaTekIkiAYUmzmsu342rL/EGJcDUk0N1Sj5tnOxd NsIvoP0bCyyL39J3v7bJEFJSAK4AABJrF472bPLX91PmdnlRvm7qH/oSrQccYrCyV6l6 0Icg9WxmWn3QdWObQpe5/ywAFMr8hTjKwLYSecUTy/r06A5LZDKYkUym5qWSo2va/wCI rny7mhQBw3cLq4oHgIrlGWhyaHA5t2zgZc8zmgSXTC3AvlLZ1cYSbPHVzdzFRuiSeE/l Yyyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729515388; x=1730120188; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sBrQ02u0x+gOef7QkcexjhixwvYLcPbk6l/zE/g/i1o=; b=SyydGuP0daHuvcvSPdlCbsOg0hnJtjjhz/nw5IY+iEEtWxCVWG3ZJNS5voBwiKgM4V kIfNCeAFFb4H3A92lXylumfeM47jxO60vBZB2Fdh3hvSeG5QRmozGC2pR2Hic6SFRwm7 ISIrRCHGH8xuEChqIZfjxreFWn5+Gkya31rLuGCkHGBL7QY6ycx1RfAFyk8sV0gR4iKK QIyh/FX6VyJ6D4TEMCj7qkaCNsS05SF3r4Br13FfULg4EqLBTQHGFuB+gr4WFSKJDktE iFfYaM2xWhKlCZmlfjPQ02Qg+meC2i/G7sMuf1abR49yjtgfeRcliaJiJiMYIhr1ktiv F77w== X-Forwarded-Encrypted: i=1; AJvYcCV0kB80zSgRMsULY+xOrb9yzle4g1AUm0vk5k1MJkg8Wse9CRYhEBEVgTpI+SIOYZao9ob8M60R0lLtAtI=@vger.kernel.org, AJvYcCWjBPSgINgQQSbe+XAQKZ4vOpQk2tlLaNjB4Iy+XYYf71Y9VrKnDM+uiBGplIc38G4gz3DR8ibXtzE=@vger.kernel.org, AJvYcCXZcjHmhLdt+41s/UZ4Vs8LqftgDROz/h6c5IQ8T5cu5zAad+mLoKCxZ/Vu2K+R2Dt215aWFd8Il4GrVQ==@vger.kernel.org X-Gm-Message-State: AOJu0Yx+9DHkGPoAKm+XzMHciwgMT9tb/kT5jDji3U/Z5Jgcrt0zjQu5 +r6mZ49Fy8jGphnpl6IC8xsJA6rD64eph6w0HtFhGwnsYPyNAMbG X-Google-Smtp-Source: AGHT+IHTdxFJmJrGfuZiT2KuSOoWNJzMyoctKdk4/SqZcCw1YvqBxoJO2n9iIQAHSDbEp9dsgXBrIw== X-Received: by 2002:a05:600c:3d9b:b0:431:5bb1:f088 with SMTP id 5b1f17b1804b1-431616a35dbmr94325905e9.29.1729515388025; Mon, 21 Oct 2024 05:56:28 -0700 (PDT) Received: from localhost (host-82-56-18-47.retail.telecomitalia.it. [82.56.18.47]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f5c2df7sm56628355e9.33.2024.10.21.05.56.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 05:56:27 -0700 (PDT) From: Matteo Martelli Date: Mon, 21 Oct 2024 14:54:16 +0200 Subject: [PATCH v5 3/5] iio: pac1921: use read_avail+release APIs instead of custom ext_info Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241021-iio-read-avail-release-v5-3-b168713fab33@gmail.com> References: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> In-Reply-To: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 The pac1921 driver was exposing the available scale info via an ad-hoc ext_info attribute instead of using the read_avail API. This to avoid a possible race condition: while the available current scales were being printed to sysfs by iio core (iio_read_channel_info_avail), the shunt resistor might have been changed concurrently. Switch to the read_avail+release APIs now that the race condition has been addressed. Signed-off-by: Matteo Martelli --- drivers/iio/adc/pac1921.c | 128 ++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 83 deletions(-) diff --git a/drivers/iio/adc/pac1921.c b/drivers/iio/adc/pac1921.c index a96fae546bc1e6d1bf3a0dbe67204c191d77a3ee..f6f8f9122a78d1b5e63d8184203eb3dae55eb560 100644 --- a/drivers/iio/adc/pac1921.c +++ b/drivers/iio/adc/pac1921.c @@ -444,11 +444,52 @@ static int pac1921_read_avail(struct iio_dev *indio_dev, *vals = pac1921_int_num_samples; *length = ARRAY_SIZE(pac1921_int_num_samples); return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_SCALE: + switch (chan->channel) { + case PAC1921_CHAN_VBUS: + *vals = (const int *)pac1921_vbus_scales; + *length = ARRAY_SIZE(pac1921_vbus_scales) * 2; + *type = IIO_VAL_INT_PLUS_NANO; + return IIO_AVAIL_LIST; + + case PAC1921_CHAN_VSENSE: + *vals = (const int *)pac1921_vsense_scales; + *length = ARRAY_SIZE(pac1921_vsense_scales) * 2; + *type = IIO_VAL_INT_PLUS_NANO; + return IIO_AVAIL_LIST; + + case PAC1921_CHAN_CURRENT: { + struct pac1921_priv *priv = iio_priv(indio_dev); + + *length = ARRAY_SIZE(priv->current_scales) * 2; + *type = IIO_VAL_INT_PLUS_NANO; + + guard(mutex)(&priv->lock); + + *vals = kmemdup_array((int *)priv->current_scales, + *length, sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + + return IIO_AVAIL_LIST; + } + default: + return -EINVAL; + } default: return -EINVAL; } } +static void pac1921_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + if (mask == IIO_CHAN_INFO_SCALE && + chan->channel == PAC1921_CHAN_CURRENT) + kfree(vals); +} + /* * Perform configuration update sequence: set the device into read state, then * write the config register and set the device back into integration state. @@ -748,6 +789,7 @@ static int pac1921_read_event_value(struct iio_dev *indio_dev, static const struct iio_info pac1921_iio = { .read_raw = pac1921_read_raw, .read_avail = pac1921_read_avail, + .read_avail_release_resource = pac1921_read_avail_release_res, .write_raw = pac1921_write_raw, .write_raw_get_fmt = pac1921_write_raw_get_fmt, .read_label = pac1921_read_label, @@ -805,88 +847,7 @@ static ssize_t pac1921_write_shunt_resistor(struct iio_dev *indio_dev, return len; } -/* - * Emit on sysfs the list of available scales contained in scales_tbl - * - * TODO:: this function can be replaced with iio_format_avail_list() if the - * latter will ever be exported. - * - * Must be called with lock held if the scales_tbl can change runtime (e.g. for - * the current scales table) - */ -static ssize_t pac1921_format_scale_avail(const int (*const scales_tbl)[2], - size_t size, char *buf) -{ - ssize_t len = 0; - - for (unsigned int i = 0; i < size; i++) { - if (i != 0) { - len += sysfs_emit_at(buf, len, " "); - if (len >= PAGE_SIZE) - return -EFBIG; - } - len += sysfs_emit_at(buf, len, "%d.%09d", scales_tbl[i][0], - scales_tbl[i][1]); - if (len >= PAGE_SIZE) - return -EFBIG; - } - - len += sysfs_emit_at(buf, len, "\n"); - return len; -} - -/* - * Read available scales for a specific channel - * - * NOTE: using extended info insted of iio.read_avail() because access to - * current scales must be locked as they depend on shunt resistor which may - * change runtime. Caller of iio.read_avail() would access the table unlocked - * instead. - */ -static ssize_t pac1921_read_scale_avail(struct iio_dev *indio_dev, - uintptr_t private, - const struct iio_chan_spec *chan, - char *buf) -{ - struct pac1921_priv *priv = iio_priv(indio_dev); - const int (*scales_tbl)[2]; - size_t size; - - switch (chan->channel) { - case PAC1921_CHAN_VBUS: - scales_tbl = pac1921_vbus_scales; - size = ARRAY_SIZE(pac1921_vbus_scales); - return pac1921_format_scale_avail(scales_tbl, size, buf); - - case PAC1921_CHAN_VSENSE: - scales_tbl = pac1921_vsense_scales; - size = ARRAY_SIZE(pac1921_vsense_scales); - return pac1921_format_scale_avail(scales_tbl, size, buf); - - case PAC1921_CHAN_CURRENT: { - guard(mutex)(&priv->lock); - scales_tbl = priv->current_scales; - size = ARRAY_SIZE(priv->current_scales); - return pac1921_format_scale_avail(scales_tbl, size, buf); - } - default: - return -EINVAL; - } -} - -#define PAC1921_EXT_INFO_SCALE_AVAIL { \ - .name = "scale_available", \ - .read = pac1921_read_scale_avail, \ - .shared = IIO_SEPARATE, \ -} - -static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = { - PAC1921_EXT_INFO_SCALE_AVAIL, - {} -}; - static const struct iio_chan_spec_ext_info pac1921_ext_info_current[] = { - PAC1921_EXT_INFO_SCALE_AVAIL, { .name = "shunt_resistor", .read = pac1921_read_shunt_resistor, @@ -910,6 +871,7 @@ static const struct iio_chan_spec pac1921_channels[] = { .type = IIO_VOLTAGE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | BIT(IIO_CHAN_INFO_SAMP_FREQ), @@ -927,12 +889,12 @@ static const struct iio_chan_spec pac1921_channels[] = { .indexed = 1, .event_spec = pac1921_overflow_event, .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), - .ext_info = pac1921_ext_info_voltage, }, { .type = IIO_VOLTAGE, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | BIT(IIO_CHAN_INFO_SAMP_FREQ), @@ -950,12 +912,12 @@ static const struct iio_chan_spec pac1921_channels[] = { .indexed = 1, .event_spec = pac1921_overflow_event, .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), - .ext_info = pac1921_ext_info_voltage, }, { .type = IIO_CURRENT, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | BIT(IIO_CHAN_INFO_SAMP_FREQ), From patchwork Mon Oct 21 12:54:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13844101 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FD831F76C1; Mon, 21 Oct 2024 12:56:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515393; cv=none; b=JurX7AXlWIa0DwzjWZPuilFj2C3WtsKoHyMky7J6J8/HdjShz9s9BEwhzQRF6WKOJmYDaqByF2s0riOkCkBuu1CPXHgUPoL5ezNqcfWDtNFRB+U3KNquVhgQcqwsWJ6DQ4WY/Uktk5M11WoIHtrJeeChbd1A36krptseWLQ51O0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515393; c=relaxed/simple; bh=lLwcWxwjDNJi83wrQWAVlHlVnJucfvYmyQRif2fP9tA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sU1VD3H4T/oBepKixuIw5Wgq1/i4AeucbLbsTbJRbGZ89Cixtm4BPG5HT7tsLUUqu2N0DDrwiTLJ1AKqynL1BtILwE6YlDpk0o4pIdvA7/5VUrTLNzsCFnCC/ayHDohfTDLQ8Kz6QGV/ciqJlgMexwZlGUHUKdbsgGQFDx3uvdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MJTI9eZu; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MJTI9eZu" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-37d4d1b48f3so3226067f8f.1; Mon, 21 Oct 2024 05:56:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729515389; x=1730120189; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=rwzowURA7LrdNAtuervNEFmMl54ysOzLP8Fuq/kRIC8=; b=MJTI9eZu+KYOVcXbr/7tPzGX42O/IO3svhEdqrEbWNlAo+tjmAsr7Bw0vGeJwx6YO3 Pl+bOfv6yPgCKcnRKwlI/jpdSuPHtIF7SJb9SnL1+fn59mL77lJHe7fai+YX0/wNWkiD 3MLHwKImIVd8n4AXwZqeLMZ2xCNs6IxG+GfKD3MXwf9/N/6CglzwSqRfObe6WzA0oumi IW61RAl8iyi8daqwFREa1Hlq0p98a2/jjluOwDLCS6Wt3ByPoteEUrc66m0/nszcS2rp WX+9jlwmrC3oSikKvSMUYJ58+fymrc6+5sAx3iw/yyNJKl3DLadxtM+/h3mPhvsXHggN BV7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729515389; x=1730120189; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rwzowURA7LrdNAtuervNEFmMl54ysOzLP8Fuq/kRIC8=; b=XJePwiY04fkVIowBHrLs+FJil4JiL8ZAFbCbXh+augXly+RiA890eEoBulUx96A3E+ 1bH1ECyzbw9OkYTG6XFRRat7Z7IfU0CoohCsdkh9vCquYVx0sJzm4xdhhs2zr+cT1/Yp 3Y8fKEhiG2sXJ7VM1jRJPq8EMZdpBjM0pQ+tU8kH+fSXrT6l+YFVgcwDrKf6d3k1p7+t MItgITxhkxM+5z/9jDD3DnovOAeHPagC+e3DqslPIL6f0jCmHc4x6z9E3QVkHg5iEo+R CdFwLpR1USTzmWemKZ14CwglzRTImVkplTpMbBCW0YN8xktRwQQx6o9VQKpd1bd5axO+ i7KA== X-Forwarded-Encrypted: i=1; AJvYcCU+WCyhsYS8ghm9SmaB/GmqiYa3SifSgyjmYlis64dSoKNvGbmBg1WGQrjP4snmEEkZtRgojroplyI=@vger.kernel.org, AJvYcCXJFcT3xEnp3yjxxaTQDpx5n3ivI3cWlZ9FU1SRNPmVmeQBA+0DWyG0KS4CwUqWUHt+mD/d4OtX0SaIH7g=@vger.kernel.org, AJvYcCXpaX/bh0fTn8bV1Twdsz1pyBM3pGifKPLtGsk1e3EDwmZEk09FM7fm9y8PhVEu5g4RAAfuA9znMTS/sg==@vger.kernel.org X-Gm-Message-State: AOJu0YyDfvvLUY6a39FZGds76Gpx2x9gQWkjk9uBYIj795+LyadHGkzC owVsiEmUkm/BilRvIC1nIYgN+OdkXFuZVfmwF/djZY85JW6mKOle X-Google-Smtp-Source: AGHT+IE3sqIbhrRKNSwUqpOTS0bsiMboQAOmfOeXcbuKaZtHZv2GyJ7SF2BjInNbWjk1RDedJ4tbbQ== X-Received: by 2002:a5d:5e11:0:b0:37c:d512:d427 with SMTP id ffacd0b85a97d-37edc481847mr4023498f8f.35.1729515389230; Mon, 21 Oct 2024 05:56:29 -0700 (PDT) Received: from localhost (host-82-56-18-47.retail.telecomitalia.it. [82.56.18.47]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f570d86sm58173265e9.8.2024.10.21.05.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 05:56:28 -0700 (PDT) From: Matteo Martelli Date: Mon, 21 Oct 2024 14:54:17 +0200 Subject: [PATCH v5 4/5] iio: ad7192: copy/release available filter frequencies to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241021-iio-read-avail-release-v5-4-b168713fab33@gmail.com> References: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> In-Reply-To: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 While available filter frequencies are being printed to sysfs by iio core (iio_read_channel_info_avail), the sampling frequency might be changed. This could cause the buffer shared with iio core to be corrupted. To prevent it, make a copy of the filter frequencies buffer and free it in the read_avail_release_resource callback. Signed-off-by: Matteo Martelli --- drivers/iio/adc/ad7192.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c index 7042ddfdfc03ee5ea58ca07fb1943feb6538175b..acf625ced0b21db8d44f77929e8a875b3c10e1b1 100644 --- a/drivers/iio/adc/ad7192.c +++ b/drivers/iio/adc/ad7192.c @@ -1056,12 +1056,19 @@ static int ad7192_read_avail(struct iio_dev *indio_dev, *length = ARRAY_SIZE(st->scale_avail) * 2; return IIO_AVAIL_LIST; - case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: - *vals = (int *)st->filter_freq_avail; + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: { *type = IIO_VAL_FRACTIONAL; *length = ARRAY_SIZE(st->filter_freq_avail) * 2; + guard(mutex)(&st->lock); + + *vals = kmemdup_array((int *)st->filter_freq_avail, *length, + sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + return IIO_AVAIL_LIST; + } case IIO_CHAN_INFO_OVERSAMPLING_RATIO: *vals = (int *)st->oversampling_ratio_avail; *type = IIO_VAL_INT; @@ -1073,6 +1080,14 @@ static int ad7192_read_avail(struct iio_dev *indio_dev, return -EINVAL; } +static void ad7192_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + if (mask == IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) + kfree(vals); +} + static int ad7192_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask) { struct ad7192_state *st = iio_priv(indio_dev); @@ -1098,6 +1113,7 @@ static const struct iio_info ad7192_info = { .write_raw = ad7192_write_raw, .write_raw_get_fmt = ad7192_write_raw_get_fmt, .read_avail = ad7192_read_avail, + .read_avail_release_resource = ad7192_read_avail_release_res, .attrs = &ad7192_attribute_group, .validate_trigger = ad_sd_validate_trigger, .update_scan_mode = ad7192_update_scan_mode, @@ -1108,6 +1124,7 @@ static const struct iio_info ad7194_info = { .write_raw = ad7192_write_raw, .write_raw_get_fmt = ad7192_write_raw_get_fmt, .read_avail = ad7192_read_avail, + .read_avail_release_resource = ad7192_read_avail_release_res, .validate_trigger = ad_sd_validate_trigger, }; @@ -1116,6 +1133,7 @@ static const struct iio_info ad7195_info = { .write_raw = ad7192_write_raw, .write_raw_get_fmt = ad7192_write_raw_get_fmt, .read_avail = ad7192_read_avail, + .read_avail_release_resource = ad7192_read_avail_release_res, .attrs = &ad7195_attribute_group, .validate_trigger = ad_sd_validate_trigger, .update_scan_mode = ad7192_update_scan_mode, From patchwork Mon Oct 21 12:54:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13844102 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67B1D1F8921; Mon, 21 Oct 2024 12:56:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515394; cv=none; b=ou0h2TMBOaIYUtfaqq7ZfoB2go2mAzGaAm1KHY8NLPsiWseRMZdRs8EojV7VFV7zItawnP3D0yn6l1c+BLkzA7J3COb9R0ZWW/JA27MP0UJe+8DZKO2hEgFc4c5NlCUvcL5Kq+bbFfTiNmBCwdfOOsSaUURrZa79dyFK6AmiYaU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729515394; c=relaxed/simple; bh=UzaA2fjWs1SIl19H07gMaVFNLQFaYamk7HcjjWTA/c8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T/s6QgFnYg1wvFZGLyRYiN2CvkoWcjumbPMvjs5ZMPisrQJye3yozTJ32hsulvfLI4umnO5sVOswrkwEntd9SyUVPQoocVsQA2yU2FiZxRbBpSw6OGGjx0vv+EjaAShnQAxAm/lw/u7HwnKLaQUkG6GPlpAOyM8Q105pDx6ijnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eM/A38b7; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eM/A38b7" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-37d5aedd177so3744696f8f.1; Mon, 21 Oct 2024 05:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729515390; x=1730120190; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NiiDyZUCEkhw54V+Qb4R9KaSMKdtWRNhFaEC6pW+G7o=; b=eM/A38b7vNsFEQz0s9vWyA8xM4evmOaWa93LyGMJMpiJ6BsI/LjJoNmOcjrQDS686U 40it7TqiT4IE3dVEn6+jtHMbdGC5kjOjckfCXWHR/YhaVIW7NHQL4jmsdY2hisj8nN8S OsLPyohtnl4pgZakxh6GaMje9mwt5YJJqIWNTBQdoQ46/HmCRw68Jj6ewQIqxfCVZ8b3 ci5TEZynyPjZEAiNb5cyjtsmwnqFmmzoRS8+vDiKT6u4SCU2Q90XHclpSL0wFFi3ilDV SsP2MLoL+3EB3LAaqUJrYeiA414bqay7dt2zMt0kS6QfZVQ8btQPziNZVMry9MZgoPtE /CqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729515390; x=1730120190; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NiiDyZUCEkhw54V+Qb4R9KaSMKdtWRNhFaEC6pW+G7o=; b=n6d3GlFJrz6a0UwAH4HYXNSo+9NE/OtSZDohqbPWDjJhR/gca9/7DiAZ84YgUbrLL8 A0BvI3FC6Rv/Qq4mh7MaSx7/lNlc/oGEcTf2wr1Nc5ffYKKOwVGXavHyA19YkuLZfeTM q3JYqjPITBdkowxQabR38XD11IrFV1/PKtSZPLoBTmJ0UUPP2lfYOzy+dd1KmtEqUqfn xLd8S4eH3xih2FtYZpA/MnyGbRUgbwD308HBzxRKVwc/cyfEJ5wYYpmaO5Mt6vNESyg4 8oNZCpI3tv5Rr8DrAPUbFxhkTzRCRkxAzgsSidNaGWWvViTz6eWvxhYh4Q61AElLP0Xw Rc3w== X-Forwarded-Encrypted: i=1; AJvYcCVsiqqks2eJFfI9skDptvRfooPdgQRobeWG4Vd8jor5/TJ1+2SuVJ9yXAexAUCfkOxspQ2U+WQt5ixrOBM=@vger.kernel.org, AJvYcCX+St39mRiaauRMQnNKgK5kAEdV7O4sNWrZNZuNfwhfCbPlWMrQv3+J3OZST/rUXhtB5raLIhFS75K0VQ==@vger.kernel.org, AJvYcCXFRjHE8ICG99RkEvYxwX15kcMF6pLSSB7A2DhD38/jBVJ2R2LBSh5D1WB7hwsXMvrvjmL0lmfE7UA=@vger.kernel.org X-Gm-Message-State: AOJu0YyyTU0Ydgz3TTsKx/DBnVQBjGP6YlPvxNyZKYM9kkI1forK4Yot kNTxJqyy8YIt861+P+Sej7p19YVoswGGVX52isZI0PE6WGIRaUoQlLKjcvfS X-Google-Smtp-Source: AGHT+IF+Pj3QHFtGd3jCzAfHaNNFO/gXUtmbtgJxh6gwBbuJjmeaOpO9u+Xq0MtpxYE2e+EQVtkFHQ== X-Received: by 2002:adf:b112:0:b0:37c:cd0d:3437 with SMTP id ffacd0b85a97d-37eab73cf70mr5825218f8f.58.1729515390426; Mon, 21 Oct 2024 05:56:30 -0700 (PDT) Received: from localhost (host-82-56-18-47.retail.telecomitalia.it. [82.56.18.47]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ee0b9cd48sm4275204f8f.111.2024.10.21.05.56.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 05:56:30 -0700 (PDT) From: Matteo Martelli Date: Mon, 21 Oct 2024 14:54:18 +0200 Subject: [PATCH v5 5/5] iio: as73211: copy/release available integration times to fix race Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241021-iio-read-avail-release-v5-5-b168713fab33@gmail.com> References: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> In-Reply-To: <20241021-iio-read-avail-release-v5-0-b168713fab33@gmail.com> To: Jonathan Cameron , Lars-Peter Clausen , Michael Hennerich , Alisa-Dariana Roman , Christian Eggers , Peter Rosin , Paul Cercueil , Sebastian Reichel Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-pm@vger.kernel.org, Matteo Martelli X-Mailer: b4 0.14.2 While available integration times are being printed to sysfs by iio core (iio_read_channel_info_avail), the sampling frequency might be changed. This could cause the buffer shared with iio core to be corrupted. To prevent it, make a copy of the integration times buffer and free it in the read_avail_release_resource callback. Tested-by: Christian Eggers Signed-off-by: Matteo Martelli --- drivers/iio/light/as73211.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/as73211.c b/drivers/iio/light/as73211.c index be0068081ebbbb37fdfb252b67a77b302ff725f6..c4c94873e6a1cc926cfb724d906b07222773c43f 100644 --- a/drivers/iio/light/as73211.c +++ b/drivers/iio/light/as73211.c @@ -108,7 +108,8 @@ struct as73211_spec_dev_data { * @creg1: Cached Configuration Register 1. * @creg2: Cached Configuration Register 2. * @creg3: Cached Configuration Register 3. - * @mutex: Keeps cached registers in sync with the device. + * @mutex: Keeps cached registers in sync with the device and protects + * int_time_avail concurrent access for updating and reading. * @completion: Completion to wait for interrupt. * @int_time_avail: Available integration times (depend on sampling frequency). * @spec_dev: device-specific configuration. @@ -493,17 +494,32 @@ static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec co *type = IIO_VAL_INT; return IIO_AVAIL_LIST; - case IIO_CHAN_INFO_INT_TIME: + case IIO_CHAN_INFO_INT_TIME: { *length = ARRAY_SIZE(data->int_time_avail); - *vals = data->int_time_avail; *type = IIO_VAL_INT_PLUS_MICRO; - return IIO_AVAIL_LIST; + guard(mutex)(&data->mutex); + + *vals = kmemdup_array(data->int_time_avail, *length, + sizeof(int), GFP_KERNEL); + if (!*vals) + return -ENOMEM; + + return IIO_AVAIL_LIST; + } default: return -EINVAL; } } +static void as73211_read_avail_release_res(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int *vals, long mask) +{ + if (mask == IIO_CHAN_INFO_INT_TIME) + kfree(vals); +} + static int _as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan __always_unused, int val, int val2, long mask) @@ -699,6 +715,7 @@ static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p) static const struct iio_info as73211_info = { .read_raw = as73211_read_raw, .read_avail = as73211_read_avail, + .read_avail_release_resource = as73211_read_avail_release_res, .write_raw = as73211_write_raw, };