From patchwork Fri Oct 18 10:16:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13841509 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 D3A0F20110A; Fri, 18 Oct 2024 10:16:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246620; cv=none; b=hb5Oh4v7XTKdM6fGA9lvqI0xtQSOadUkX2mRsfvOMH9ErSXucIov6GosapvrBxqGxf7CV5K7Ad4MojVC0XQsc87yrJbJRoRtgFg0XCuR1o8CiBICkfjfFfGqrKFdwCO0D1iNL5CoQw/gHgPKqFOpKoMfWaYa2tduL46or3oaGtc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246620; c=relaxed/simple; bh=0ClYzGYWuo9R4ytQT8+YomjzZyyvK/shpte3TSu50RA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sa4tC55A0veBVVNsqhmsTlB6iazHroJU+1HHvuOGS/JkptEN14KR7ARRZIKelxc6fCwO5xGopvAYh2gG3agVQaNkp5gsabBxsNdcKY5eOw+K1avrFaHBXQMlVRy3WCk1Xh3JzuqMokn1ABfn5Pvdgg2Ru+q29YPL39hOonfazmA= 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=cucaDfVn; arc=none smtp.client-ip=209.85.128.52 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="cucaDfVn" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-43163667f0eso2928705e9.0; Fri, 18 Oct 2024 03:16:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729246617; x=1729851417; 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=cucaDfVnr89esYQ39szL0yzZVD8fLI/IeTZluHSNQimqjRPv48GzaN9hfykWBFceTo qiGa3NmbWAMwT6Bw4Q3HZ4yMYEuRBEDO8zL0zSJa5BBbCzZQTjMyUbwo4LuB2ZmspcSr B9qkRPDvCq3A879BUHnhNHAcBIoMeKoIcWMGzJtj4H/gS5WBlespCyZXXDRaczMXu5a1 UgU3+sURlGWr+kimjG1pfzH1/VkP4Lm5ahK3z74n62K6FDjpZknHDJ1++NXjVqVR5Gim VZ7BIny+0BjRe3iWYjPSX4eF/7Ft2EXYv/+qkKyTClVFeghqa0Uql9YcoPKZSTAFvihm Y40w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729246617; x=1729851417; 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=fjFEvV5a2ZK2EaaiSGGphDJIKFWHmqS3erNqv93gi1cb/SL7J6S6cPNNPgAV51WB39 eZNTuvrofiusUABGGWORBGBBXCN3GU19frUzC4tcjay1IQAm8G9zYpxgWpmxAChc9XHd HndqZXOBBVTDG7ymGzX6MRRpsps1f9THlSjDUnqObgBe+Ey0bWB2djpQBBle6TXkFcOL hrkTy0w5LxDfr0FAf+e7USykMjQfFzNKqymElC4Jqs/X3Mp/YNKEY1T/felZ2DycR8rp qxRtrP+DAprR9NI1EbaaAtzj336z/4j/pbZ65yttkah+QjKa+7IxFAXPcDGgrj9yPkZ5 3isw== X-Forwarded-Encrypted: i=1; AJvYcCUqSZo/xZM8AF7klnqjoAngKMODTzhegmskm8jgmQELcosXZN0RDuQAtXXShgQtFZnx04P6/+TRdgbw+g==@vger.kernel.org, AJvYcCVqSx7JDK4WcozsR3T+hjiZ3cKgnnpmper8Rjzk0sRs/4H58+ga8m0hhSx2MOLdOAc3iI2B8C4NU1s=@vger.kernel.org, AJvYcCWP1eozMr6cb0fgxvjvs5pRBMg34iLkUTiNKYdQ7291dNJRgoXflc+SvE9EJY3YTKCyPag5codDCLWuCpo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+TqtFMKVbzFjBRG3SF0Z23dahFx8jQxvpvkH3v7F3/9+ug+XK CbkNniY+JG233kDNCOmOhZ7/76b0Y1EgWsLjjHjftqEWFhjEJIA7 X-Google-Smtp-Source: AGHT+IF+vC1TXXZ41e6GhjeXRX0z3gfl5Roxla4WW2YlYG9z9+bnzPXhoqV+JJMI803cwtmFEfPWvQ== X-Received: by 2002:a05:600c:3d9b:b0:431:5bb1:f088 with SMTP id 5b1f17b1804b1-431616a35dbmr13565805e9.29.1729246616911; Fri, 18 Oct 2024 03:16:56 -0700 (PDT) Received: from localhost (host-79-18-120-72.retail.telecomitalia.it. [79.18.120.72]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ecf027d90sm1547801f8f.8.2024.10.18.03.16.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:16:56 -0700 (PDT) From: Matteo Martelli Date: Fri, 18 Oct 2024 12:16:40 +0200 Subject: [PATCH v4 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: <20241018-iio-read-avail-release-v4-1-53c8ac618585@gmail.com> References: <20241018-iio-read-avail-release-v4-0-53c8ac618585@gmail.com> In-Reply-To: <20241018-iio-read-avail-release-v4-0-53c8ac618585@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 Fri Oct 18 10:16:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13841510 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 54445201251; Fri, 18 Oct 2024 10:17:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246622; cv=none; b=ZL4oRNMdDlmOLfevBwSwTjsvlzTRt3SHUx/VF1NBBVBSEyYUSOzmAX3DVEyL5LcVJ4aAYa6h/zTVbKiE2Q8hzv+BF+jMGWQG0YgmfmV6IWH/TvDN9+IcjO116fYcwGszlPbHmh5vMp46lenFfcRd/X3B05tTmCw3oOqCfWIcUeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246622; c=relaxed/simple; bh=skxYbsxlomcPahNBo9EqyYgAPhdvkUeYyQAIxCTJGXA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bZegG2c+1Hr5oaxKUwfRvh00iY7q/JOAv37isTyzd28K6eGu9DlCJG5G7+Is8HdEpqdsHCKoc7p4K1AEhHDN1z0flL6yZIDcPhFRNM7DLqz03BYKNetflbX62mGNBaWhsDVC3iH3bS37tRZ/a2vV7E5J9Mcu8Ma9LdryLsvGwW8= 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=Gm7/vvUu; arc=none smtp.client-ip=209.85.221.52 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="Gm7/vvUu" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-37d41894a32so1304311f8f.1; Fri, 18 Oct 2024 03:17:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729246618; x=1729851418; 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=68F+I2t0jIdksrHXOUU/sZlxHnw1vILpUVDiRtg0A9A=; b=Gm7/vvUu6oVYVugkPk9C3YXhjAK9m8qxolrGcATyChgctweZlQFvscLCMbHUr59l2l npS4BorZFtOpyQzvBnB/kdoNtLNOaRpOFyLtjSSoXAUot8815W3vhyqk1LBeg2zfjXzs 4bnJqjcObnnceJ3fzy+CM14zHMIs59UimEhpf7UTQZpFmSghclCWCfaL2pVEi/bsiiXV Y/oozq8+Q7uP3xh9qrPFhIYI7UXQyOuNS4nOmPgFqto8B4A0y0IYNTsVnqr00Sj+L1TV cfr+XSMJGLJb23qGy2sdEoY4qfAw6NKBCUMQyvc43QexFl7FAuQ5btUoij1JxpKc0AFW WOHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729246618; x=1729851418; 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=68F+I2t0jIdksrHXOUU/sZlxHnw1vILpUVDiRtg0A9A=; b=WvYKwbUP7v+X3a+Em/+4eiRrVGoI0N8weoG1tgZeAMzzxzIysNJxr6URpHbHUsGhks T9pa9ADgEpJRU9iWf/zUp12nIEYCbjIX+/H2fzdP2ZFAAhvMIXql6pVaa/vxJ1p0zmQj R/abXRQfuWZnbsAYgc1uhI4BSf5XGniOAFve0aqLpqhcwIAZOu5EoJ+MconYkoJGj5EL /mwa4jkuJlAlWOFWOtuO7dMxhk4WblYs6mOqVxDw8seP78jaZQZYao7a3igDSPaUQ78H HziQbZTWnLf/GnIg6rqXECOsfWal5KZM1KJw9fK6lrX5UdlMvjL3SohmpjDCrAOXSIRt 4EsA== X-Forwarded-Encrypted: i=1; AJvYcCU5sO58FL3Fk1Ue6MB7i6gAsYiU24kY6xviWifB3cnmh2a/cBQhX+aPlxLQ9VM5mWaSjRuE39so+sY=@vger.kernel.org, AJvYcCX0k8Ea5X6gjeFeLQFO7tymcp+d+H5A+L0USMjzSXQ998JWPjgUc6IW2qMuDzbM/IwG9wVey9YTsRmCbg==@vger.kernel.org, AJvYcCXdSVE3upVuqSylC8/5JnXmN1i+EGKBeYB1c3cFTZW4UYUrXm+tm5NDVR9QKBL5zE+jmc7xvDangqCEqjM=@vger.kernel.org X-Gm-Message-State: AOJu0YxAK6756lH+J65PtS6MhkFFs4kp/YyDXm24wftOFAaU5jR7MSDe Gb/7HvONzX9z8hz/LIIf12qq1/Hxahdw0/o9ZEowDCk17Y9oIBwL X-Google-Smtp-Source: AGHT+IGzn1OOgQhFIyFCgI1k5wdbR8ibGxMospdE8wD7XTWVSNizom9PDnz6YoodIBC1M+9Y2Kcczw== X-Received: by 2002:a05:6000:e83:b0:374:cee6:c298 with SMTP id ffacd0b85a97d-37d93e2492bmr4259631f8f.21.1729246618185; Fri, 18 Oct 2024 03:16:58 -0700 (PDT) Received: from localhost (host-79-18-120-72.retail.telecomitalia.it. [79.18.120.72]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43160e4fe8bsm19734805e9.44.2024.10.18.03.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:16:57 -0700 (PDT) From: Matteo Martelli Date: Fri, 18 Oct 2024 12:16:41 +0200 Subject: [PATCH v4 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: <20241018-iio-read-avail-release-v4-2-53c8ac618585@gmail.com> References: <20241018-iio-read-avail-release-v4-0-53c8ac618585@gmail.com> In-Reply-To: <20241018-iio-read-avail-release-v4-0-53c8ac618585@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. Signed-off-by: Matteo Martelli --- drivers/iio/afe/iio-rescale.c | 8 ++++++++ drivers/iio/dac/dpot-dac.c | 8 ++++++++ drivers/iio/inkern.c | 34 ++++++++++++++++++++++++++++------ drivers/iio/multiplexer/iio-mux.c | 8 ++++++++ drivers/power/supply/ingenic-battery.c | 4 +++- include/linux/iio/consumer.h | 4 ++-- 6 files changed, 57 insertions(+), 9 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..9af6b33eb8d178ee7b3f5d417ccfa4d0f87835bc 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -760,9 +760,29 @@ 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; } @@ -789,9 +809,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,7 +823,7 @@ 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; + const int *vals __free(kfree) = NULL; int length; int ret; @@ -857,7 +879,7 @@ 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; + const int *vals __free(kfree) = NULL; int length; int ret; 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..6b7856e69f5fb7b8b73166b9b6825f4af7b19129 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,7 +64,7 @@ static int ingenic_battery_get_property(struct power_supply *psy, */ static int ingenic_battery_set_scale(struct ingenic_battery *bat) { - const int *scale_raw; + const int *scale_raw __free(kfree) = NULL; int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret; u64 max_mV; diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index 333d1d8ccb37f387fe531577ac5e0bfc7f752cec..e3e268d2574b3e01c9412449d90d627de7efcd84 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. From patchwork Fri Oct 18 10:16:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13841511 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 8C92220126D; Fri, 18 Oct 2024 10:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246623; cv=none; b=S9yM6OJMhdC/KS2FbIjZJwAO8XnJLU+o282LtB78Z83Wbtqy3tuMkl2ZgKTIu88mnqU1xVJ/GzuhJlBw2YwmRzTk+ACOH2EnOshypnCH7jolrCw2tVJxG1f7eRUKOG9Ha42d0vWQxwmGdBlcXpTXleEgAk3JcYSZIFytes00Ag0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246623; 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=HpC33R/laa7/wP1XK/diV9RzIbK6ZUYGNccQK0FaCpgjSiURUz1XhxOj4s+iS6RGVSMm8fTxonG5mE/V4RiPzcgLePVY3tETz+416UGvP/krOgQ8KYNaTZUPcMVbMFbqqqB8vRC0A1Z0NPP3jtcPkNwVd+HjR0X7igsNz7Bf7Yw= 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=DbanhXqX; arc=none smtp.client-ip=209.85.221.54 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="DbanhXqX" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d4c482844so1370114f8f.0; Fri, 18 Oct 2024 03:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729246620; x=1729851420; 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=DbanhXqXUH3ISikTIe3B+GML8kPFd5ib9TlAyC0TLlsSRrKVCzFRdvhiEc/oqL0mzP MUC0t2TG1CFcuhrIr3VdaHbRlb6ZOTf5fu+7vPaSl75r10G/DbfS8AJPJDjf3dydebUz OaEM/bcwL9kKftf5Ge0UblGbu6h59JGqVhrD5zmBHWtdPWWANbMuh0Rs1CRo8Z1uCvJc MpCJec/2s3jhkqNekUfuLQfSnVyhonnLpx2Vq6up4yjMeP4T28qtoVIGsiZb6bzxe2dz bIHfhQrIcm8GcvWn9Zw6NB5tItHTyP1ou3baS3DxsvSkrJBaLBwNmYSXNLz/RTT4ZbAm bzSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729246620; x=1729851420; 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=EIrsAliH/O7mYGekDZ+AzmNRPPFKGIzVpHeFrtklyK8nWYFLAwgO9IQ9x1s2dqGfZH yCWoNZlX7vmE30mJJ0CDPPOx3K653WPGYvyexNoi6jD5ZXZaYMqMwEoQrg3GTiCXu8lT 1PJeiA1DTE3MYwmdCOUzoCtrM+CmFNDg3jrthEVGR7DxiI0JPa2Lvy06kQke9ZLg4PJE Zu46soSPyzhLMp+waeCoUSIXedBiWEsZq12TBIkDm7njTak/O2zkasGw80WHdCn9zy8A Pk9IRxnjNYPUM2pjma2E/7vzuhXv3y+buon8Jxg1m+GonRYRsFE/1zxWaWeFEL66GWUD 7b3w== X-Forwarded-Encrypted: i=1; AJvYcCUTFTGxGUx/YhIhmWZMTfcXzpUf/nje2qnyGuJjcfbVp95kDyPqo3JKxO9oAZc7kgvYFIDCEbCsFutTkg==@vger.kernel.org, AJvYcCVU07dkHXxCi2OkT8BQSa3E7+Rxr6685oONj5c3mNT/UuwY5BgP78jFG1tHF8oMZQ7y5bhIdPCVh1Pa1+M=@vger.kernel.org, AJvYcCWMB6mU02nk3visRYXyZUhm71dZKxKnOEPrmlu6XeUdqXU+t9Alf9r4DgdM4PfS1YtHQgNgMi9zPqs=@vger.kernel.org X-Gm-Message-State: AOJu0YxAaYmR3x+1SSWdAj4pS+GLhzo1yc+VD3vjC/ecSAKE4lmGua3W UlSsvuRRVQBfl0Yd6VXAYmeksw5oFo7vKZL8dee5Was2kel3Gym6urKQwFfajYo= X-Google-Smtp-Source: AGHT+IEWRXU4xyknVixAqH/dHVbkeIzgabgehIuumuZvx5wOOxzWL621sJYBa6pc1pakcMDimDc95A== X-Received: by 2002:adf:e9c2:0:b0:37d:493c:f7b8 with SMTP id ffacd0b85a97d-37eab72779emr1350142f8f.2.1729246619450; Fri, 18 Oct 2024 03:16:59 -0700 (PDT) Received: from localhost (host-79-18-120-72.retail.telecomitalia.it. [79.18.120.72]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ecf066600sm1522219f8f.37.2024.10.18.03.16.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:16:59 -0700 (PDT) From: Matteo Martelli Date: Fri, 18 Oct 2024 12:16:42 +0200 Subject: [PATCH v4 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: <20241018-iio-read-avail-release-v4-3-53c8ac618585@gmail.com> References: <20241018-iio-read-avail-release-v4-0-53c8ac618585@gmail.com> In-Reply-To: <20241018-iio-read-avail-release-v4-0-53c8ac618585@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 Fri Oct 18 10:16:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13841512 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 8A6D22022CE; Fri, 18 Oct 2024 10:17:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246624; cv=none; b=XQtstmv9upVLWO+3TKedl6IGclC9AHbxrCtw/MOQxd6551j9DjGPmWLzhQeyLGs3lVJ9xqGT7RzEuze68XxDnI9uFKUqUbZ32lyp6WX3KXnDOyxfIgJaY7EsH9tBjBADfX3GAwppO/BTzP2oOoC2esShLI1fQ2UEMoFEY4RT3eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246624; c=relaxed/simple; bh=lLwcWxwjDNJi83wrQWAVlHlVnJucfvYmyQRif2fP9tA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aTrB8XcUyMrM4shX26uKcoqu1CejHJoD4jOl1NK/I5jpHeT/rtX1mO/ddm9DaKSBB2athv1mULuni5+p077cm6Jf582HAuCUlWxrPn/gIXk3chdA53hKwisEmrpywWTZUQe7tk2eJI2p6+Dd7zDfuaheIGP6/+apkqVXhWUrEZ4= 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=V0et5gkD; arc=none smtp.client-ip=209.85.221.48 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="V0et5gkD" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-37d49a7207cso1359468f8f.0; Fri, 18 Oct 2024 03:17:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729246621; x=1729851421; 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=V0et5gkDbUjGDH43nGDlNmoFBpKq4Q6Z/jsn4Of8jhubqHnbSfZPw3G77D95fLouyz HTRtVPMRuNF1csLp6QELOrtZiRTnVL8TCblkbfSvNYoDDhdNrAaavAuzKBw3MjNAMFhj IIp5C74Z1KJSAMWdZ1qJMMueOgA/iaA5OtNTiHnSyIR6oFpcX6ORUkZVPBAL8blmhKds zdD3HSfCHdXtb3YT03V46Z/zn0LhGc0hUWANB0teCeL9t2Nl+m9O7qPw38y0josThqZA uKOAqhHExvd81LXnvZCqd3Yur5+dOF58/VaOtFFCVjj+m1rR4Yh+6DE7NFooRZ4yuOBW TEJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729246621; x=1729851421; 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=UvjT3bsXwWFFLwmff1tDIzOTmKHvtvrbJIebdJHParWrWbnVvyKW54IwpmSm5kQw6A X2FVXJuuphg0n7ub0IvO0b+vWMqpL9Bi030rs9AYmCDW4iTWZyMNF+W6eEqPOeckojcL QOQ5zepCkfJXLMxpdyQd/1vl+LCHudpK852Kd8aHYCqVsoDLtZWqOOswYfDqHgYllAON Xcne/IDvwmyv1LDyeKdh71nHVw3CeXAfwJu8B7P+2EpwOPjAMQVHOUsU+kwe8QTdE9JJ BwgqTbyTR1WY5Us8mLKb67ZSXRl7X9pPHT/uMgWMdqgHFJiw7Di+t16bjWom8eHR8v6G mJ9w== X-Forwarded-Encrypted: i=1; AJvYcCV/Zas7VPD2f5OOjIafr8GZuIjwtE3qgdAuB0zrk8RzRMbmQN9ACoIZEWr2nMiTBZO64s/cbGNOu55J7g==@vger.kernel.org, AJvYcCVv7MR7unSxfDAZ9L2EVp73vA5YuTVDPH06baKRZRF3B4M+wObwLEeEZVFsqdOgg+HsjUl4ZslJyPc=@vger.kernel.org, AJvYcCWnnF9ZiXeMMiCx5kdJ1U+oDjpdrUt9LijteVxpIE5COVecqmSc521XoPzQoGkKBsG6C+2vnTSbKodxqNE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz19u+rOiTg6kwdOaDEGD677GCfIKT+lL+BqR87kkKIEnzT1K3j zEuvX35M0YGMRyki6xjaxgB93c1ik7i54ak4JPXXkNpXZ9TY8JCM X-Google-Smtp-Source: AGHT+IHlbRgeDtSiwkVImTXi6eLGA0djtvQh25VEd8vaYpE2W6OyXUpc7iS8UBuQTPmhk1FJ6BJcKg== X-Received: by 2002:a5d:554a:0:b0:37d:43f1:57fd with SMTP id ffacd0b85a97d-37ea213717emr1388681f8f.6.1729246620727; Fri, 18 Oct 2024 03:17:00 -0700 (PDT) Received: from localhost (host-79-18-120-72.retail.telecomitalia.it. [79.18.120.72]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ecf089d52sm1543397f8f.61.2024.10.18.03.17.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:17:00 -0700 (PDT) From: Matteo Martelli Date: Fri, 18 Oct 2024 12:16:43 +0200 Subject: [PATCH v4 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: <20241018-iio-read-avail-release-v4-4-53c8ac618585@gmail.com> References: <20241018-iio-read-avail-release-v4-0-53c8ac618585@gmail.com> In-Reply-To: <20241018-iio-read-avail-release-v4-0-53c8ac618585@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 Fri Oct 18 10:16:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Martelli X-Patchwork-Id: 13841513 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 EE6D420262F; Fri, 18 Oct 2024 10:17:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246625; cv=none; b=GrUVR+ODjW5itMpvAY/1oVSM8yl6FR0KPiFiuKbDw4CBccfzo28g9LCcqcArknAbMqixfFP8iNHt0QeEsluPdC/P6k6DyiQ1EPCDaMEYGoZo5JezUpAl5mwUkowfbNXEP6n4wxDk3C+hdZYRbgJo7wYQpCKXtmMees0rJJyfWYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729246625; c=relaxed/simple; bh=zPhyRDG9F+zgzh0+HIEmtXBb7OQBzROgLZ7mJASXsvQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ji5U242up16IbWqr98Ru3+w63BRvhY5UEx1DWRFW1F1Sh8plnkqO6W/vIdw6/7W5RXHl2VG3r3se3+VrfDcY7NBVuvqHwENvT2KC2lFa0oTM+HT1BdUrF71l/c2NLUL6VNNpWMrDA1zNsrkJUkils3uwwzSTJolwgRtinY/qHuU= 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=KZEfnDiN; arc=none smtp.client-ip=209.85.128.41 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="KZEfnDiN" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4311c285bc9so19132635e9.3; Fri, 18 Oct 2024 03:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729246622; x=1729851422; 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=Yg5C99O5lFsD3GMjKIKiUiVjWoF4T1zlBP/PbUaocsA=; b=KZEfnDiNukXZzeCNsmYRTFp4l+1S0hhJ6MZA9fycmEXMwCOd0IckNeQojiJHTHY+4l jXgTw49k3+7dFTc5AZqCk9apqimEn5Z1uw0P2A4hiYuVEcWcFX6ogesGtj0Tx4FKYM9d 0BLHzVruluyvhWhjVnyojwwspo0qGn+ocpnSu9FzSh1TWbe99zB6h/hV/OyLvqGvmNPh WBcIorX6oTa38vcMREvKALeagQmoPEQIgH9+FZXUezhjAFpMETH8MMZe29HCg75lq8l2 vLkly61mALj0m4VNibAMgansStrONoY5nujcG3D0V/Ia5g8hsyQmJTJ5z4rhlxcJJl7o OsOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729246622; x=1729851422; 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=Yg5C99O5lFsD3GMjKIKiUiVjWoF4T1zlBP/PbUaocsA=; b=l0QNUKgLikyREu+doQ/NeE+9q656JGwZ3IRjlqOh5FYJl696ePbRBoVj2jcriEemVH MIgYb5ZSJpUOr/bOTSijO3WaNBgCWIDuOl4lsV9eN1vsPOpg5jszgDHmyLlGIKrQFIO9 SIXcgH6JpACyRbdAGZFsCK2+wLG+2PFmtuAOmp6qz5rnpZ2ZhOuOazxMaWfpbgxiULiA TTTUVA7RnsFgvCVGYtBvlDuPYVU0q1JBxZ1RitUW9DsA8ncja5sOZwa+HZVwmvkZOHeE 78MvCYzsyzeZRRWzz8OOAf28/V8/+B3dOzzHqoycycSDKY+HPYQcIWudIOc943PkimmG 0gmA== X-Forwarded-Encrypted: i=1; AJvYcCVJH6/7dbfyrXffsX62nC8cwpeuLpDoaeBiRnFGDY9DbjC9vNXNXAE6yD5dAJekMEILtvbfyP3kcsEPWuQ=@vger.kernel.org, AJvYcCXU6UTTZfbV9a47G3wYtAew97yrXWJGT7+YUdUZNZalrKeUQJJNkV7iC1vBzXMUWIeBxyRibmPzc/4=@vger.kernel.org, AJvYcCXjJ+mrn/81FbqxWlKCV3fLjV3ZEnPcdIrNcWCbI/tMe+OY5VWUdR/HM7b7p/iFOp5KjsZXoT7rWt/Uug==@vger.kernel.org X-Gm-Message-State: AOJu0YzpD+OOQzEuCF70sDmhFP4CeJExmjhgOF8NvMMlyoNW1R+7aSdy MV28v2Js7JgMg6kzzqsMmf2+WuguTaUAk6xGecn+Bf/voye2wuIjwwwBgZ0ggf0= X-Google-Smtp-Source: AGHT+IEeLVJ7h84WQ4/+u3k4S9ShFi1dHNq8ZcXPQUIGtlWymZVFe8HnRUMIjBIrqeVsPikZlvPQ7g== X-Received: by 2002:a05:600c:3d99:b0:431:588a:4498 with SMTP id 5b1f17b1804b1-4316164dd18mr16051385e9.14.1729246621980; Fri, 18 Oct 2024 03:17:01 -0700 (PDT) Received: from localhost (host-79-18-120-72.retail.telecomitalia.it. [79.18.120.72]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37ecf146e2asm1512707f8f.116.2024.10.18.03.17.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Oct 2024 03:17:01 -0700 (PDT) From: Matteo Martelli Date: Fri, 18 Oct 2024 12:16:44 +0200 Subject: [PATCH v4 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: <20241018-iio-read-avail-release-v4-5-53c8ac618585@gmail.com> References: <20241018-iio-read-avail-release-v4-0-53c8ac618585@gmail.com> In-Reply-To: <20241018-iio-read-avail-release-v4-0-53c8ac618585@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. Signed-off-by: Matteo Martelli Tested-by: Christian Eggers --- 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, };