mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Martin Tuma <martin.tuma@digiteqautomotive.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	linux-media@vger.kernel.org,
	Gustavo Silva <gustavograzs@gmail.com>,
	Marcelo Schmitt <marcelo.schmitt@analog.com>,
	Antoniu Miclaus <antoniu.miclaus@analog.com>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	Ramona Alexandra Nechita <ramona.nechita@analog.com>,
	Tomislav Denis <tomislav.denis@avl.com>,
	Jorge Marques <jorge.marques@analog.com>,
	Petre Rodan <petre.rodan@subdimension.ro>,
	Dan Robertson <dan@dlrobertson.com>,
	Jianping Shen <Jianping.Shen@de.bosch.com>,
	Gwendal Grignou <gwendal@chromium.org>,
	Maxwell Doose <maxwell@maxwelld.cc>,
	Tomasz Duszynski <tduszyns@gmail.com>,
	Esben Haabendal <esben@geanix.com>,
	Narcisa Ana Maria Vasile <narcisaanamaria12@gmail.com>,
	Jiri Kosina <jikos@kernel.org>,
	Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>,
	Vasileios Amoiridis <vassilisamir@gmail.com>,
	Abhash Jha <abhashkumarjha123@gmail.com>,
	Puranjay Mohan <puranjay@kernel.org>,
	Nishant Malpani <nish.malpani25@gmail.com>,
	Oleksij Rempel <o.rempel@pengutronix.de>,
	Linus Walleij <linusw@kernel.org>,
	Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Subject: [PATCH 01/35] iio: core: add default_trigger and take reference in iio_device_register()
Date: Mon, 16 Nov 2026 01:47:49 +0000	[thread overview]
Message-ID: <20261116014823.3132576-2-jic23@kernel.org> (raw)
In-Reply-To: <20261116014823.3132576-1-jic23@kernel.org>

From: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>

Some drivers choose to set a default trigger in probe() via
indio_dev->trig = iio_trigger_get(trig). The matching iio_trigger_put()
is in iio_device_unregister_trigger_consumer(), called from
iio_dev_release() only if indio_dev->modes includes one of the triggered
modes. If probe() fails after the reference is taken but before
INDIO_BUFFER_TRIGGERED is set (e.g. by iio_triggered_buffer_setup()),
the reference is leaked. In devm managed flows there is no simple way
for the driver to drop the reference on error.  Whilst it is possible
to have enforce strict registration order so that the reference is
only taken once conditions for tear down are met, the requirements are
not intuitive. So move the point of reference acquire later in sequence
to a point where cleanup will always happen.

Add struct iio_dev::default_trigger. Drivers then set this without taking
a reference. The reference is instead taken in
iio_device_register_trigger_consumer(), which pairs with the existing put
under the same modes check, so they always balance.

Also check the return value of iio_device_register_trigger_consumer(),
add an error_unreg_trigger_consumer unwind path for failures that occur
later in registration, and clear indio_dev->trig after the put so the
subsequent call from iio_dev_release() does not drop it again.

Immutable triggers are a little more complex and left for a follow
on patch.

Signed-off-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
---
 drivers/iio/industrialio-core.c    | 15 ++++++++++++---
 drivers/iio/industrialio-trigger.c | 21 ++++++++++++++++++---
 include/linux/iio/iio.h            |  4 ++++
 3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index ed7aa7fd9391..6c704920d619 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -2180,8 +2180,14 @@ int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
 			"Failed to register event set\n");
 		goto error_free_sysfs;
 	}
-	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
-		iio_device_register_trigger_consumer(indio_dev);
+	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) {
+		ret = iio_device_register_trigger_consumer(indio_dev);
+		if (ret) {
+			dev_err(indio_dev->dev.parent,
+				"Failed to register trigger consumer\n");
+			goto error_unreg_eventset;
+		}
+	}
 
 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
 		indio_dev->setup_ops == NULL)
@@ -2202,10 +2208,13 @@ int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
 
 	ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
 	if (ret < 0)
-		goto error_unreg_eventset;
+		goto error_unreg_trigger_consumer;
 
 	return 0;
 
+error_unreg_trigger_consumer:
+	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
+		iio_device_unregister_trigger_consumer(indio_dev);
 error_unreg_eventset:
 	iio_device_unregister_eventset(indio_dev);
 error_free_sysfs:
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 76926fee0f84..a0149a5d0163 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -760,15 +760,30 @@ EXPORT_SYMBOL(iio_trigger_validate_own_device);
 
 int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
 {
-	return iio_device_register_sysfs_group(indio_dev,
-					       &iio_trigger_consumer_attr_group);
+	int ret;
+
+	/* Drivers must not both set a default trigger and take a reference */
+	if (indio_dev->default_trigger && WARN_ON(indio_dev->trig))
+		return -EINVAL;
+
+	ret = iio_device_register_sysfs_group(indio_dev,
+					      &iio_trigger_consumer_attr_group);
+	if (ret)
+		return ret;
+
+	if (indio_dev->default_trigger)
+		indio_dev->trig = iio_trigger_get(indio_dev->default_trigger);
+
+	return 0;
 }
 
 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
 {
 	/* Clean up an associated but not attached trigger reference */
-	if (indio_dev->trig)
+	if (indio_dev->trig) {
 		iio_trigger_put(indio_dev->trig);
+		indio_dev->trig = NULL;
+	}
 }
 
 int iio_device_suspend_triggering(struct iio_dev *indio_dev)
diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index 67718f3b9b7d..8b3fd7673293 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -632,6 +632,9 @@ struct iio_buffer_setup_ops {
  * @active_scan_mask:	[INTERN] union of all scan masks requested by buffers
  * @scan_timestamp:	[INTERN] set if any buffers have requested timestamp
  * @trig:		[INTERN] current device trigger (buffer modes)
+ * @default_trigger:	[DRIVER] optional trigger to use by default. The driver
+ *			does not hold a reference to it; the IIO core takes
+ *			one in iio_device_register().
  * @pollfunc:		[DRIVER] function run on trigger being received
  * @pollfunc_event:	[DRIVER] function run on events trigger being received
  * @channels:		[DRIVER] channel specification structure table
@@ -657,6 +660,7 @@ struct iio_dev {
 	const unsigned long		*active_scan_mask;
 	bool				__private scan_timestamp;
 	struct iio_trigger		*trig;
+	struct iio_trigger		*default_trigger;
 	struct iio_poll_func		*pollfunc;
 	struct iio_poll_func		*pollfunc_event;
 
-- 
2.56.1


  reply	other threads:[~2026-11-16  1:48 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  1:47 [PATCH 00/35] iio: closing default trigger handling leaks Jonathan Cameron
2026-11-16  1:47 ` Jonathan Cameron [this message]
2026-11-16  1:47 ` [PATCH 02/35] media: pci: mgb4: use IIO default_trigger Jonathan Cameron
2026-11-16  1:47 ` [PATCH 03/35] iio: chemical: ens160: use default_trigger Jonathan Cameron
2026-11-16  1:47 ` [PATCH 04/35] iio: adc: ad4130: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 05/35] iio: adc: ad4170-4: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 06/35] iio: adc: ad7768-1: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 07/35] iio: adc: ad7779: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 08/35] iio: adc: ti-ads131e08: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 09/35] iio: addac: ad74115: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 10/35] iio: addac: ad74413r: " Jonathan Cameron
2026-11-16  1:47 ` [PATCH 11/35] iio: temperature: tmp006: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 12/35] iio: adc: ad4062: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 13/35] iio: accel: bma220: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 14/35] iio: accel: bma400: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 15/35] iio: imu: smi330: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 16/35] iio: light: acpi-als: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 17/35] iio: chemical: scd30: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 18/35] iio: accel: bma180: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 19/35] iio: accel: kxcjk-1013: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 20/35] iio: accel: mma8452: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 21/35] iio: chemical: ccs811: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 22/35] iio: pressure: mpl3115: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 23/35] iio: common: st_sensors: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 24/35] iio: common: hid-sensors: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 25/35] iio: pressure: bmp280: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 26/35] iio: light: vl6180: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 27/35] iio: gyro: itg3200: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 28/35] iio: imu: bmi160: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 29/35] iio: accel: adxl355: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 30/35] iio: gyro: adxrs290: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 31/35] iio: accel: adxl372: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 32/35] iio: light: si1145: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 33/35] iio: adc: ti-tsc2046: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 34/35] iio: accel: mxc4005: " Jonathan Cameron
2026-11-16  1:48 ` [PATCH 35/35] iio: gyro: mpu3050: " Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116014823.3132576-2-jic23@kernel.org \
    --to=jic23@kernel.org \
    --cc=Jianping.Shen@de.bosch.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=abhashkumarjha123@gmail.com \
    --cc=andy@kernel.org \
    --cc=antoniu.miclaus@analog.com \
    --cc=dan@dlrobertson.com \
    --cc=dlechner@baylibre.com \
    --cc=esben@geanix.com \
    --cc=gustavograzs@gmail.com \
    --cc=gwendal@chromium.org \
    --cc=jikos@kernel.org \
    --cc=jonathan.cameron@oss.qualcomm.com \
    --cc=jorge.marques@analog.com \
    --cc=linusw@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=marcelo.schmitt@analog.com \
    --cc=martin.tuma@digiteqautomotive.com \
    --cc=maxwell@maxwelld.cc \
    --cc=mchehab@kernel.org \
    --cc=narcisaanamaria12@gmail.com \
    --cc=nish.malpani25@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=o.rempel@pengutronix.de \
    --cc=petre.rodan@subdimension.ro \
    --cc=puranjay@kernel.org \
    --cc=ramona.nechita@analog.com \
    --cc=srinivas.pandruvada@linux.intel.com \
    --cc=tduszyns@gmail.com \
    --cc=tomislav.denis@avl.com \
    --cc=vassilisamir@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®