iio: closing default trigger handling leaks
Subsystems (19)
Jonathan Cameron
maintainercover iio: closing default trigger handling leaks
From: Jonathan Cameron <[email protected]>
Note this is compile tested only! I definitely want a few people to test that I didn't break everything.
This is the first part of what I think is a long term solution to the currently really fragile ordering constraints around setting default triggers. Today these are set by iio_dev->trig = iio_trigger_get(trig). The reference is only released on the iio_dev.release callback and only if we have set the mode to include one that means there might be a trigger set. That mode set only occurs when a buffer is registered. Thus trigger release only works if we have already registered a buffer.
One solution would be to reorder so that the default trigger is always set after the buffer registration (we have had various recent proposed patches to do that for individual drivers) but that is non obvious and fragile.
The solution here is easier to reason about. Decouple the setting of the default trigger from the acquisition of a reference. Thus we can acquire the reference in iio_device_register() when we know all the conditions for it's release have been achieved. So that's all we do in this part 1 series.
Part 2 will address immutable triggers and those that are effectively immutable but predate that logic. That is those that set a one to one relationship between device and trigger. That change is rather less mechanical than this one and 35 patches is really a bit big anyway even if most of them are small!
Part 3 and beyond will move to a situation where the iio_dev->trig will become opaque. That should stop anyone manipulating that directly. I have a few related cunning plans to test out before we get to that, not because the particular challenge is hard, but because they will trample on the same code.
Jonathan Cameron (35):
iio: core: add default_trigger and take reference in
iio_device_register()
media: pci: mgb4: use IIO default_trigger
iio: chemical: ens160: use default_trigger
iio: adc: ad4130: use default_trigger
iio: adc: ad4170-4: use default_trigger
iio: adc: ad7768-1: use default_trigger
iio: adc: ad7779: use default_trigger
iio: adc: ti-ads131e08: use default_trigger
iio: addac: ad74115: use default_trigger
iio: addac: ad74413r: use default_trigger
iio: temperature: tmp006: use default_trigger
iio: adc: ad4062: use default_trigger
iio: accel: bma220: use default_trigger
iio: accel: bma400: use default_trigger
iio: imu: smi330: use default_trigger
iio: light: acpi-als: use default_trigger
iio: chemical: scd30: use default_trigger
iio: accel: bma180: use default_trigger
iio: accel: kxcjk-1013: use default_trigger
iio: accel: mma8452: use default_trigger
iio: chemical: ccs811: use default_trigger
iio: pressure: mpl3115: use default_trigger
iio: common: st_sensors: use default_trigger
iio: common: hid-sensors: use default_trigger
iio: pressure: bmp280: use default_trigger
iio: light: vl6180: use default_trigger
iio: gyro: itg3200: use default_trigger
iio: imu: bmi160: use default_trigger
iio: accel: adxl355: use default_trigger
iio: gyro: adxrs290: use default_trigger
iio: accel: adxl372: use default_trigger
iio: light: si1145: use default_trigger
iio: adc: ti-tsc2046: use default_trigger
iio: accel: mxc4005: use default_trigger
iio: gyro: mpu3050: use default_trigger
drivers/iio/accel/adxl355_core.c | 19 ++++++++-------- drivers/iio/accel/adxl372.c | 2 +- drivers/iio/accel/bma180.c | 2 +- drivers/iio/accel/bma220_core.c | 2 +- drivers/iio/accel/bma400_core.c | 2 +- drivers/iio/accel/kxcjk-1013.c | 2 +- drivers/iio/accel/mma8452.c | 2 +- drivers/iio/accel/mxc4005.c | 2 +- drivers/iio/adc/ad4062.c | 2 +- drivers/iio/adc/ad4130.c | 2 +- drivers/iio/adc/ad4170-4.c | 2 +- drivers/iio/adc/ad7768-1.c | 2 +- drivers/iio/adc/ad7779.c | 20 ++++++++--------- drivers/iio/adc/ti-ads131e08.c | 2 +- drivers/iio/adc/ti-tsc2046.c | 3 +-- drivers/iio/addac/ad74115.c | 2 +- drivers/iio/addac/ad74413r.c | 2 +- drivers/iio/chemical/ccs811.c | 2 +- drivers/iio/chemical/ens160_core.c | 5 ++--- drivers/iio/chemical/scd30_core.c | 2 +- .../common/hid-sensors/hid-sensor-trigger.c | 3 ++- .../common/st_sensors/st_sensors_trigger.c | 2 +- drivers/iio/gyro/adxrs290.c | 19 ++++++++-------- drivers/iio/gyro/itg3200_buffer.c | 3 +-- drivers/iio/gyro/mpu3050-core.c | 2 +- drivers/iio/imu/bmi160/bmi160.h | 1 - drivers/iio/imu/bmi160/bmi160_core.c | 22 +++++++++---------- drivers/iio/imu/smi330/smi330_core.c | 3 +-- drivers/iio/industrialio-core.c | 15 ++++++++++--- drivers/iio/industrialio-trigger.c | 21 +++++++++++++++--- drivers/iio/light/acpi-als.c | 2 +- drivers/iio/light/si1145.c | 5 +---- drivers/iio/light/vl6180.c | 2 +- drivers/iio/pressure/bmp280-core.c | 2 +- drivers/iio/pressure/mpl3115.c | 2 +- drivers/iio/temperature/tmp006.c | 22 +++++++++---------- drivers/media/pci/mgb4/mgb4_trigger.c | 2 +- include/linux/iio/iio.h | 4 ++++ 38 files changed, 114 insertions(+), 97 deletions(-)
base-commit: 94921bc9eb214f838481faa847cda5c42e486013
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 1/35 iio: core: add default_trigger and take reference in iio_device_register()
From: Jonathan Cameron <[email protected]>
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.
--- 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(-)
drivers/iio/industrialio-core.c +12 -3
@@ -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:
drivers/iio/industrialio-trigger.c +18 -3
@@ -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)
include/linux/iio/iio.h +4 -0
@@ -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;
Signature · 1 line
2.56.1
Re: [PATCH 01/35] iio: core: add default_trigger and take reference in iio_device_register()
Quoted 25 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Some drivers choose to set a default trigger in
probe()viaindio_dev->trig=iio_trigger_get(trig). The matchingiio_trigger_put()is iniio_device_unregister_trigger_consumer(), called fromiio_dev_release()only ifindio_dev->modesincludes one of the triggered modes. Ifprobe()fails after the reference is taken but beforeINDIO_BUFFER_TRIGGEREDis set (e.g. byiio_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 iniio_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 anerror_unreg_trigger_consumerunwind path for failures that occur later in registration, and clearindio_dev->trigafter the put so the subsequent call fromiio_dev_release()does not drop it again.
Quoted 4 lines
Immutable triggers are a little more complex and left for a follow on patch.
Signed-off-by Jonathan Cameron <[email protected]>
I like the concept and reasoning here, and code looks good so a clear:
Yours,
Linus Walleij
Jonathan Cameron
maintainerPATCH 2/35 media: pci: mgb4: use IIO default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe_trigger(). The IIO core now takes the reference during iio_device_register().
Previously the reference was taken before iio_triggered_buffer_setup() set INDIO_BUFFER_TRIGGERED, so a failure in that call leaked the trigger reference as the IIO core only drops it for devices with triggered modes.
---
Ideally I'd like to take this though the IIO tree. There are load of other changes coming on top of this, ultimately making the struct iio_dev->trigger opaque to drivers. I can provide an immutable branch if that is likely to be necessary.
Note as these are just a case of a resource leak in an obscure error path I haven't been marking them as fixes and it probably isn't worth backporting them.
--- drivers/media/pci/mgb4/mgb4_trigger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/pci/mgb4/mgb4_trigger.c +1 -1
@@ -126,7 +126,7 @@ static int probe_trigger(struct iio_dev *indio_dev, int irq)if (ret) goto error_free_irq; - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig;return 0 ;
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 3/35 iio: chemical: ens160: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register().
Previously the reference was taken before devm_iio_triggered_buffer_setup() set INDIO_BUFFER_TRIGGERED, so a failure in that call leaked the trigger reference as iio_dev_release() only drops it for devices with triggered modes.
As indio_dev->trig is no longer set during probe(), pass the local trigger pointer to devm_request_irq().
---
Technically a fix but only for a leak on an error path and backporting this plus the infrastructure probably doesn't mkae sense.
--- drivers/iio/chemical/ens160_core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/iio/chemical/ens160_core.c +2 -3
@@ -309,11 +309,10 @@ static int ens160_setup_trigger(struct iio_dev *indio_dev, int irq)if (ret)return ret; - indio_dev->trig =iio_trigger_get (trig); + indio_dev->default_trigger = trig; ret =devm_request_irq (dev, irq, iio_trigger_generic_data_rdy_poll, -IRQF_NO_THREAD , indio_dev->name, - indio_dev->trig); +IRQF_NO_THREAD , indio_dev->name, trig);if (ret)return ret;
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 4/35 iio: adc: ad4130: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This makes for less fragile and unintuitive ordering rules in probe.
---
The fixes tag might seem unlikely but that patch also introduced the default trigger.
--- drivers/iio/adc/ad4130.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad4130.c +1 -1
@@ -1666,7 +1666,7 @@ static int ad4130_triggered_buffer_setup(struct iio_dev *indio_dev)if (ret)return ret; - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig; return devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev, &iio_pollfunc_store_time,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 5/35 iio: adc: ad4170-4: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up before the buffer is registered (which sets the conditions for automatic release of the reference).
---
This one is technically a fix for the leak. However I only plan to fix it as part of the the larger set and not to push for a backport.
--- drivers/iio/adc/ad4170-4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad4170-4.c +1 -1
@@ -2830,7 +2830,7 @@ static int ad4170_trigger_setup(struct iio_dev *indio_dev)if (ret)return dev_err_probe (dev, ret,"Failed to register trigger\n" ); - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 6/35 iio: adc: ad7768-1: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This avoids fragile ordering requirements in probe().
--- drivers/iio/adc/ad7768-1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad7768-1.c +1 -1
@@ -1505,7 +1505,7 @@ static int ad7768_triggered_buffer_alloc(struct iio_dev *indio_dev)if (ret)return ret; - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig; return devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev, &iio_pollfunc_store_time,
Signature · 1 line
2.56.1
Re: [PATCH 06/35] iio: adc: ad7768-1: use default_trigger
Quoted 9 lines · On Mon, 16 Nov 2026 01:47:54 +0000 Jonathan Cameron <[email protected]> wrote:
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. This avoids fragile ordering requirements inprobe().Fixes a5f8c7da3dbe ("iio: adc: Add AD7768-1 ADC basic support")
Sashiko points out the Fixes tag is probably incorrect (and I agree). Commit a5f8c7da3dbe set indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_TRIGGERED, which guaranteed trigger unregistering on driver exit (I checked and it does this by bitwise & with the INDIO_ALL_TRIGGERED_MODE). This behaviour was then removed with:
bca036de0ab2 ("iio: adc: ad778-1: do not explicity set INDIO_BUFFER_TRIGGERED mode")
Signature · 2 lines
Kind regards, Joshua Crofts
Jonathan Cameron
maintainerPATCH 7/35 iio: adc: ad7779: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This avoid fragile ordering constraints.
The copy of the trigger pointer in st->trig is only used during probe(), so replace it with a local variable.
--- drivers/iio/adc/ad7779.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/iio/adc/ad7779.c +10 -10
@@ -141,7 +141,6 @@ struct ad7779_state {struct spi_device *spi;const struct ad7779_chip_info *chip_info;struct clk *mclk; -struct iio_trigger *trig;unsigned int sampling_freq;enum ad7779_filter filter_enabled;struct iio_backend *back;
@@ -821,33 +820,34 @@ static int ad7779_setup_channels(struct iio_dev *indio_dev, const struct ad7779_static int ad7779_setup_without_backend (struct ad7779_state *st,struct iio_dev *indio_dev) { -int ret;struct device *dev = &st->spi->dev; +struct iio_trigger *trig; +int ret; indio_dev->info = &ad7779_info; indio_dev->channels = st->chip_info->channels; indio_dev->num_channels =ARRAY_SIZE (ad7779_channels); - st->trig =devm_iio_trigger_alloc (dev,"%s-dev%d" , indio_dev->name, -iio_device_id (indio_dev)); -if (!st->trig) + trig =devm_iio_trigger_alloc (dev,"%s-dev%d" , indio_dev->name, +iio_device_id (indio_dev)); +if (!trig)return -ENOMEM ; - st->trig->ops = &ad7779_trigger_ops; + trig->ops = &ad7779_trigger_ops; -iio_trigger_set_drvdata (st->trig, st); +iio_trigger_set_drvdata (trig, st); ret =devm_request_irq (dev, st->spi->irq, iio_trigger_generic_data_rdy_poll,IRQF_NO_THREAD |IRQF_NO_AUTOEN , indio_dev->name, - st->trig); + trig);if (ret)return ret; - ret =devm_iio_trigger_register (dev, st->trig); + ret =devm_iio_trigger_register (dev, trig);if (ret)return ret; - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = trig; ret = devm_iio_triggered_buffer_setup(dev, indio_dev, &iio_pollfunc_store_time,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 8/35 iio: adc: ti-ads131e08: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This avoids a fragile ordering constraint.
--- drivers/iio/adc/ti-ads131e08.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads131e08.c +1 -1
@@ -851,7 +851,7 @@ static int ads131e08_probe(struct spi_device *spi)return ret; } - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig; ret =devm_iio_triggered_buffer_setup (&spi->dev, indio_dev,NULL , &ads131e08_trigger_handler,NULL );
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 9/35 iio: addac: ad74115: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This avoid a fragile registration ordering constraint.
--- drivers/iio/addac/ad74115.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74115.c +1 -1
@@ -1813,7 +1813,7 @@ static int ad74115_setup_trigger(struct iio_dev *indio_dev)if (ret)return ret; - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 10/35 iio: addac: ad74413r: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. In this driver there are a lot of places that could previously happen.
---
This one is a fix, but given impact is a reference leak on an error path I don't propose to push for a backport of the fix.
--- drivers/iio/addac/ad74413r.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/addac/ad74413r.c +1 -1
@@ -1397,7 +1397,7 @@ static int ad74413r_probe(struct spi_device *spi) indio_dev->name = st->chip_info->name; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->info = &ad74413r_info; - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig; ret =ad74413r_reset (st);if (ret)
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 11/35 iio: temperature: tmp006: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. That leak can occur in this driver, but more generally the intent is to avoid fragile ordering constraints.
The copy of the trigger pointer in data->drdy_trig is only used during probe(), so replace it with a local variable.
---
This one is a fix, but I'm not planning to push for backport given the impact is a reference and resulting leak of data in an unlikely error path only.
--- drivers/iio/temperature/tmp006.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/iio/temperature/tmp006.c +10 -12
@@ -45,7 +45,6 @@struct tmp006_data {struct i2c_client *client;u16 config; -struct iio_trigger *drdy_trig; };static int tmp006_read_measurement (struct tmp006_data *data,u8 reg)
@@ -297,6 +296,7 @@ static int tmp006_probe(struct i2c_client *client) {struct iio_dev *indio_dev;struct tmp006_data *data; +struct iio_trigger *trig;int ret;if (!i2c_check_functionality (client->adapter,I2C_FUNC_SMBUS_WORD_DATA ))
@@ -340,25 +340,23 @@ static int tmp006_probe(struct i2c_client *client)return ret;if (client->irq >0 ) { - data->drdy_trig =devm_iio_trigger_alloc (&client->dev, -"%s-dev%d" , - indio_dev->name, -iio_device_id (indio_dev)); -if (!data->drdy_trig) + trig =devm_iio_trigger_alloc (&client->dev,"%s-dev%d" , + indio_dev->name, +iio_device_id (indio_dev)); +if (!trig)return -ENOMEM ; - data->drdy_trig->ops = &tmp006_trigger_ops; -iio_trigger_set_drvdata (data->drdy_trig, indio_dev); - ret =devm_iio_trigger_register (&client->dev, data->drdy_trig); + trig->ops = &tmp006_trigger_ops; +iio_trigger_set_drvdata (trig, indio_dev); + ret =devm_iio_trigger_register (&client->dev, trig);if (ret)return ret; - indio_dev->trig =iio_trigger_get (data->drdy_trig); + indio_dev->default_trigger = trig; ret =devm_request_irq (&client->dev, client->irq, iio_trigger_generic_data_rdy_poll, -IRQF_NO_THREAD ,"tmp006_irq" , - data->drdy_trig); +IRQF_NO_THREAD ,"tmp006_irq" , trig);if (ret <0 )return ret; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 12/35 iio: adc: ad4062: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This removes a fragile ordering constraint.
--- drivers/iio/adc/ad4062.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad4062.c +1 -1
@@ -745,7 +745,7 @@ static int ad4062_request_trigger(struct iio_dev *indio_dev)if (ret)return ret; - indio_dev->trig =iio_trigger_get (st->trigger); + indio_dev->default_trigger = st->trigger;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 13/35 iio: accel: bma220: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. Whilst this patch closes the specific bug, it also makes the ordering less fragile going forwards.
--- drivers/iio/accel/bma220_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/bma220_core.c +1 -1
@@ -539,7 +539,7 @@ int bma220_common_probe(struct device *dev, struct regmap *regmap, int irq)if (ret)return dev_err_probe (dev, ret,"iio trigger register fail\n" ); - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig; ret =devm_request_threaded_irq (dev, irq,NULL , &bma220_irq_handler,IRQF_ONESHOT , indio_dev->name, indio_dev);
Signature · 1 line
2.56.1
Re: [PATCH 13/35] iio: accel: bma220: use default_trigger
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. Whilst this patch closes the specific bug, it also makes the ordering less fragile going forwards.Fixes b7e17ca10793 ("iio: accel: bma220: add interrupt trigger")Signed-off-by Jonathan Cameron <[email protected]>
Yours,
Linus Walleij
Re: [PATCH 13/35] iio: accel: bma220: use default_trigger
In reply to Jonathan CameronHello Jonathan,
Quoted 30 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. Whilst this patch closes the specific bug, it also makes the ordering less fragile going forwards.Fixes b7e17ca10793 ("iio: accel: bma220: add interrupt trigger")Signed-off-by Jonathan Cameron <[email protected]>--- drivers/iio/accel/bma220_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/accel/bma220_core.c +1
-1@@ -539,7 +539,7 @@ int bma220_common_probe(struct device *dev, struct regmap *regmap, int irq)if (ret)return dev_err_probe (dev, ret,"iio trigger register fail\n" ); - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig; ret =devm_request_threaded_irq (dev, irq,NULL , &bma220_irq_handler,IRQF_ONESHOT , indio_dev->name, indio_dev);Signature · 1 line
2.56.1
passes all my unit tests on both i2c and spi sensors, thanks for the patch.
best regards,
peter
Jonathan Cameron
maintainerPATCH 14/35 iio: accel: bma400: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. Whilst this closes a potential leak, it also makes the ordering going forwards a lot less fragile.
--- drivers/iio/accel/bma400_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/bma400_core.c +1 -1
@@ -1781,7 +1781,7 @@ int bma400_probe(struct device *dev, struct regmap *regmap, int irq,return dev_err_probe (data->dev, ret,"iio trigger register fail\n" ); - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig; ret = devm_request_threaded_irq(dev, irq, NULL, &bma400_interrupt, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Signature · 1 line
2.56.1
Re: [PATCH 14/35] iio: accel: bma400: use default_trigger
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. Whilst this closes a potential leak, it also makes the ordering going forwards a lot less fragile.Fixes ffe0ab6a9698 ("iio: accel: bma400: Add triggered buffer support")Signed-off-by Jonathan Cameron <[email protected]>
Yours,
Linus Walleij
Jonathan Cameron
maintainerPATCH 15/35 iio: imu: smi330: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This makes the ordering less fragile going forwards.
--- drivers/iio/imu/smi330/smi330_core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/imu/smi330/smi330_core.c +1 -2
@@ -888,8 +888,7 @@ int smi330_core_probe(struct device *dev, struct regmap *regmap)return dev_err_probe (dev, ret,"IIO register trigger failed\n" ); -/* Set default operation mode to data ready. */ - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig; } ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 16/35 iio: light: acpi-als: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This also makes the ordering a lot less fragile going forwards.
--- drivers/iio/light/acpi-als.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/acpi-als.c +1 -1
@@ -214,7 +214,7 @@ static int acpi_als_probe(struct platform_device *pdev)* Set hardware trigger by default to let events flow when * BIOS support notification. */ - indio_dev->trig =iio_trigger_get (als->trig); + indio_dev->default_trigger = als->trig; ret = devm_iio_triggered_buffer_setup(dev, indio_dev, iio_pollfunc_store_time,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 17/35 iio: chemical: scd30: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. As well as closing that leak, this makes the ordering a lot less fragile going forwards.
--- drivers/iio/chemical/scd30_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/chemical/scd30_core.c +1 -1
@@ -675,7 +675,7 @@ static int scd30_setup_trigger(struct iio_dev *indio_dev)if (ret)return ret; - indio_dev->trig =iio_trigger_get (trig); + indio_dev->default_trigger = trig;/* * Interrupt is enabled just before taking a fresh measurement
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 18/35 iio: accel: bma180: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. As well as closing that leak, this also makes the ordering a lot less fragile.
--- drivers/iio/accel/bma180.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/bma180.c +1 -1
@@ -998,7 +998,7 @@ static int bma180_probe(struct i2c_client *client)if (ret) goto err_trigger_free; - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig; } ret = iio_triggered_buffer_setup(indio_dev, NULL,
Signature · 1 line
2.56.1
Re: [PATCH 18/35] iio: accel: bma180: use default_trigger
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. As well as closing that leak, this also makes the ordering a lot less fragile.Fixes 0668a4e4d297 ("iio: accel: bma180: Fix indio_dev->trig assignment")Signed-off-by Jonathan Cameron <[email protected]>
Yours,
Linus Walleij
Jonathan Cameron
maintainerPATCH 19/35 iio: accel: kxcjk-1013: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. As well as closing the leak, this also makes the ordering less fragile going forwards.
--- drivers/iio/accel/kxcjk-1013.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/kxcjk-1013.c +1 -1
@@ -1487,7 +1487,7 @@ static int kxcjk1013_probe(struct i2c_client *client)if (ret) goto err_poweroff; - indio_dev->trig =iio_trigger_get (data->dready_trig); + indio_dev->default_trigger = data->dready_trig; data->motion_trig->ops = &kxcjk1013_trigger_ops;iio_trigger_set_drvdata (data->motion_trig, indio_dev);
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 20/35 iio: accel: mma8452: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. As well as closing the leak, this should make the ordering a lot less fragile going forwards.
--- drivers/iio/accel/mma8452.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/mma8452.c +1 -1
@@ -1529,7 +1529,7 @@ static int mma8452_trigger_setup(struct iio_dev *indio_dev)return ret; data->trig = trig; - indio_dev->trig =iio_trigger_get (trig); + indio_dev->default_trigger = trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 21/35 iio: chemical: ccs811: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. As well as closing the leak, this should make the ordering a lot less fragile.
--- drivers/iio/chemical/ccs811.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/chemical/ccs811.c +1 -1
@@ -502,7 +502,7 @@ static int ccs811_probe(struct i2c_client *client)if (ret) goto err_poweroff; - indio_dev->trig =iio_trigger_get (data->drdy_trig); + indio_dev->default_trigger = data->drdy_trig; } ret = iio_triggered_buffer_setup(indio_dev, NULL,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 22/35 iio: pressure: mpl3115: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. As well as closing the leak, this will make the ordering less fragile in general.
--- drivers/iio/pressure/mpl3115.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/pressure/mpl3115.c +1 -1
@@ -686,7 +686,7 @@ static int mpl3115_trigger_probe(struct mpl3115_data *data,if (ret)return ret; - indio_dev->trig =iio_trigger_get (data->drdy_trig); + indio_dev->default_trigger = data->drdy_trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 23/35 iio: common: st_sensors: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in st_sensors_allocate_trigger(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up.
All users (st_accel, st_gyro, st_magn and st_pressure) allocate the triggered buffer and trigger before calling devm_iio_device_register() and do not access indio_dev->trig before that point. The interrupt handlers are passed the trigger directly.
The ordering is such in here that their is no leak today, but in general this change will make things less fragile going forwards.
--- drivers/iio/common/st_sensors/st_sensors_trigger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/st_sensors/st_sensors_trigger.c +1 -1
@@ -219,7 +219,7 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,dev_err (parent,"failed to register iio trigger.\n" );return err; } - indio_dev->trig =iio_trigger_get (sdata->trig); + indio_dev->default_trigger = sdata->trig;return 0 ; }
Signature · 1 line
2.56.1
Re: [PATCH 23/35] iio: common: st_sensors: use default_trigger
Quoted 12 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inst_sensors_allocate_trigger(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up.All users (
st_accel,st_gyro,st_magnandst_pressure) allocate the triggered buffer and trigger before callingdevm_iio_device_register()and do not accessindio_dev->trigbefore that point. The interrupt handlers are passed the trigger directly.
Quoted 4 lines
The ordering is such in here that their is no leak today, but in general this change will make things less fragile going forwards.
Signed-off-by Jonathan Cameron <[email protected]>
Yours,
Linus Walleij
Jonathan Cameron
maintainerPATCH 24/35 iio: common: hid-sensors: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in hid_sensor_setup_trigger(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up.
Set it only once nothing else in hid_sensor_setup_trigger() can fail, so the error path does not leave it pointing at the freed trigger.
All users call devm_hid_sensor_setup_trigger() before devm_iio_device_register() and do not access indio_dev->trig.
The ordering in this driver ensures there is no leak today but this patch will make that ordering much less fragile going forwards.
--- drivers/iio/common/hid-sensors/hid-sensor-trigger.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/common/hid-sensors/hid-sensor-trigger.c +2 -1
@@ -278,12 +278,13 @@ int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, goto error_free_trig; } attrb->trigger = trig; - indio_dev->trig =iio_trigger_get (trig); ret =pm_runtime_set_active (&indio_dev->dev);if (ret) goto error_unreg_trigger; + indio_dev->default_trigger = trig; +iio_device_set_drvdata (indio_dev, attrb);INIT_WORK (&attrb->work, hid_sensor_set_power_work);
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 25/35 iio: pressure: bmp280: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This particular case does not have such a leak so this is a case of making the code less fragile going forwards.
--- drivers/iio/pressure/bmp280-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/pressure/bmp280-core.c +1 -1
@@ -1345,7 +1345,7 @@ static int __bmp280_trigger_probe(struct iio_dev *indio_dev,if (ret)return dev_err_probe (dev, ret,"iio trigger register failed.\n" ); - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig;return 0 ; }
Signature · 1 line
2.56.1
Re: [PATCH 25/35] iio: pressure: bmp280: use default_trigger
Quoted 9 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. This particular case does not have such a leak so this is a case of making the code less fragile going forwards.Signed-off-by Jonathan Cameron <[email protected]>
Yours,
Linus Walleij
Jonathan Cameron
maintainerPATCH 26/35 iio: light: vl6180: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. In this particular case the ordering does not result in a leak so the intent is to make that less fragile going forwards.
--- drivers/iio/light/vl6180.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/vl6180.c +1 -1
@@ -736,7 +736,7 @@ static int vl6180_probe(struct i2c_client *client)if (ret)return ret; - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = data->trig; }return devm_iio_device_register (&client->dev, indio_dev);
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 27/35 iio: gyro: itg3200: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. Whilst there is no leak in here today, the change will make the ordering a lot less fragile.
--- drivers/iio/gyro/itg3200_buffer.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/gyro/itg3200_buffer.c +1 -2
@@ -132,8 +132,7 @@ int itg3200_probe_trigger(struct iio_dev *indio_dev)if (ret) goto error_free_irq; -/* select default trigger */ - indio_dev->trig =iio_trigger_get (st->trig); + indio_dev->default_trigger = st->trig;return 0 ;
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 28/35 iio: imu: bmi160: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This particular driver doesn't have that leak due to the specific ordering of calls. Hence this is just making things less fragile and enabling further rework on top.
The copy of the trigger pointer in data->trig is only used during probe(), so replace it with a local variable.
--- drivers/iio/imu/bmi160/bmi160.h | 1 - drivers/iio/imu/bmi160/bmi160_core.c | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-)
drivers/iio/imu/bmi160/bmi160.h +0 -1
@@ -7,7 +7,6 @@struct bmi160_data {struct regmap *regmap; -struct iio_trigger *trig;struct regulator_bulk_data supplies[2 ];struct iio_mount_matrix orientation; };
drivers/iio/imu/bmi160/bmi160_core.c +11 -11
@@ -778,31 +778,31 @@ static const struct iio_trigger_ops bmi160_trigger_ops = {int bmi160_probe_trigger (struct iio_dev *indio_dev,int irq,u32 irq_type) {struct bmi160_data *data =iio_priv (indio_dev); +struct iio_trigger *trig;int ret; - data->trig =devm_iio_trigger_alloc (&indio_dev->dev,"%s-dev%d" , - indio_dev->name, -iio_device_id (indio_dev)); - -if (data->trig ==NULL ) + trig =devm_iio_trigger_alloc (&indio_dev->dev,"%s-dev%d" , + indio_dev->name, +iio_device_id (indio_dev)); +if (!trig)return -ENOMEM ; ret =devm_request_irq (&indio_dev->dev, irq, &iio_trigger_generic_data_rdy_poll, irq_type |IRQF_NO_THREAD , -"bmi160" , data->trig); +"bmi160" , trig);if (ret)return ret; - data->trig->dev.parent =regmap_get_device (data->regmap); - data->trig->ops = &bmi160_trigger_ops; -iio_trigger_set_drvdata (data->trig, indio_dev); + trig->dev.parent =regmap_get_device (data->regmap); + trig->ops = &bmi160_trigger_ops; +iio_trigger_set_drvdata (trig, indio_dev); - ret =devm_iio_trigger_register (&indio_dev->dev, data->trig); + ret =devm_iio_trigger_register (&indio_dev->dev, trig);if (ret)return ret; - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 29/35 iio: accel: adxl355: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger directly in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. The ordering in this particular driver is such that there is no leak, so this is just reducing fragility going forwards and enabling addition refactors on top.
The copy of the trigger pointer in data->dready_trig is only used during probe(), so replace it with a local variable.
--- drivers/iio/accel/adxl355_core.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl355_core.c +9 -10
@@ -227,7 +227,6 @@ struct adxl355_data {enum adxl355_hpf_3db hpf_3db;int calibbias[3 ];int adxl355_hpf_3db_table[7 ][2 ]; -struct iio_trigger *dready_trig; union { u8 transf_buf[3]; struct {
@@ -754,27 +753,27 @@ static const struct iio_chan_spec adxl355_channels[] = {static int adxl355_probe_trigger (struct iio_dev *indio_dev,int irq) {struct adxl355_data *data =iio_priv (indio_dev); +struct iio_trigger *trig;int ret; - data->dready_trig =devm_iio_trigger_alloc (data->dev,"%s-dev%d" , - indio_dev->name, -iio_device_id (indio_dev)); -if (!data->dready_trig) + trig =devm_iio_trigger_alloc (data->dev,"%s-dev%d" , indio_dev->name, +iio_device_id (indio_dev)); +if (!trig)return -ENOMEM ; - data->dready_trig->ops = &adxl355_trigger_ops; -iio_trigger_set_drvdata (data->dready_trig, indio_dev); + trig->ops = &adxl355_trigger_ops; +iio_trigger_set_drvdata (trig, indio_dev); ret =devm_request_irq (data->dev, irq, &iio_trigger_generic_data_rdy_poll, -IRQF_NO_THREAD ,"adxl355_irq" , data->dready_trig); +IRQF_NO_THREAD ,"adxl355_irq" , trig);if (ret)return ret; - ret =devm_iio_trigger_register (data->dev, data->dready_trig); + ret =devm_iio_trigger_register (data->dev, trig);if (ret)return dev_err_probe (data->dev, ret,"iio trigger register failed\n" ); - indio_dev->trig =iio_trigger_get (data->dready_trig); + indio_dev->default_trigger = trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 30/35 iio: gyro: adxrs290: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), relaxing ordering constraint between setting this and registration of buffers. The ordering in this driver was such that there is not a leak, so this is more about avoiding fragility and enabling further refactors on top.
The copy of the trigger pointer in st->dready_trig is only used during probe(), so replace it with a local variable.
--- drivers/iio/gyro/adxrs290.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/iio/gyro/adxrs290.c +9 -10
@@ -72,7 +72,6 @@ struct adxrs290_state {enum adxrs290_mode mode;unsigned int lpf_3db_freq_idx;unsigned int hpf_3db_freq_idx; -struct iio_trigger *dready_trig;/* Ensure correct alignment of timestamp when present */ struct {s16 channels[3 ];
@@ -566,6 +565,7 @@ static const struct iio_info adxrs290_info = {static int adxrs290_probe_trigger (struct iio_dev *indio_dev) {struct adxrs290_state *st =iio_priv (indio_dev); +struct iio_trigger *trig;int ret;if (!st->spi->irq) {
@@ -573,28 +573,27 @@ static int adxrs290_probe_trigger(struct iio_dev *indio_dev)return 0 ; } - st->dready_trig =devm_iio_trigger_alloc (&st->spi->dev,"%s-dev%d" , - indio_dev->name, -iio_device_id (indio_dev)); -if (!st->dready_trig) + trig =devm_iio_trigger_alloc (&st->spi->dev,"%s-dev%d" , indio_dev->name, +iio_device_id (indio_dev)); +if (!trig)return -ENOMEM ; - st->dready_trig->ops = &adxrs290_trigger_ops; -iio_trigger_set_drvdata (st->dready_trig, indio_dev); + trig->ops = &adxrs290_trigger_ops; +iio_trigger_set_drvdata (trig, indio_dev); ret =devm_request_irq (&st->spi->dev, st->spi->irq, &iio_trigger_generic_data_rdy_poll, -IRQF_NO_THREAD ,"adxrs290_irq" , st->dready_trig); +IRQF_NO_THREAD ,"adxrs290_irq" , trig);if (ret)return ret; - ret =devm_iio_trigger_register (&st->spi->dev, st->dready_trig); + ret =devm_iio_trigger_register (&st->spi->dev, trig);if (ret) {dev_err (&st->spi->dev,"iio trigger register failed\n" );return ret; } - indio_dev->trig =iio_trigger_get (st->dready_trig); + indio_dev->default_trigger = trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 31/35 iio: accel: adxl372: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. Note that this driver doesn't have that leak so this is about reducing fragility and enabling addition refactors on top.
Note that this driver provides two triggers, so the default is definitely not immutable.
--- drivers/iio/accel/adxl372.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/adxl372.c +1 -1
@@ -1272,7 +1272,7 @@ static int adxl372_buffer_setup(struct iio_dev *indio_dev)if (ret)return ret; - indio_dev->trig =iio_trigger_get (st->dready_trig); + indio_dev->default_trigger = st->dready_trig; return devm_request_irq(dev, st->irq, iio_trigger_generic_data_rdy_poll,
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 32/35 iio: light: si1145: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. Note this driver doesn't have this leak. This avoids fragile registration ordering constraints and enables additional refactors on top.
The copy of the trigger pointer in data->trig is only used during probe(), so drop it.
--- drivers/iio/light/si1145.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/light/si1145.c +1 -4
@@ -166,7 +166,6 @@ struct si1145_part_info {* @scan_mask: Saved scan mask to avoid duplicate set_chlist * @autonomous: If automatic measurements are active (for buffer support) * @part_info: Part information -* @trig: Pointer to iio trigger * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode * @buffer: Used to pack data read from sensor. */
@@ -178,7 +177,6 @@ struct si1145_data {const struct si1145_part_info *part_info;unsigned long scan_mask;bool autonomous; -struct iio_trigger *trig;int meas_rate;/* * Ensure timestamp will be naturally aligned if present.
@@ -1258,8 +1256,7 @@ static int si1145_probe_trigger(struct iio_dev *indio_dev)if (ret)return ret; - data->trig = trig; - indio_dev->trig =iio_trigger_get (data->trig); + indio_dev->default_trigger = trig;return 0 ; }
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 33/35 iio: adc: ti-tsc2046: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. This particular driver has ordering that means this leak doesn't occure. So this just removes a fragile ordering constraint and enabled further rework on top.
--- drivers/iio/adc/ti-tsc2046.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/adc/ti-tsc2046.c +1 -2
@@ -805,8 +805,7 @@ static int tsc2046_adc_probe(struct spi_device *spi)return ret; } -/* set default trigger */ - indio_dev->trig =iio_trigger_get (priv->trig); + indio_dev->default_trigger = priv->trig;return devm_iio_device_register (dev, indio_dev); }
Signature · 1 line
2.56.1
Re: [PATCH 33/35] iio: adc: ti-tsc2046: use default_trigger
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. This particular driver has ordering that means this leak doesn't occure. So this just removes a fragile ordering constraint and enabled further rework on top.Signed-off-by Jonathan Cameron <[email protected]>
Thank you!
Jonathan Cameron
maintainerPATCH 34/35 iio: accel: mxc4005: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. No actual leak in this case due to the particular ordering. This change relaxes that ordering constraint and makes things less fragile going forwards.
--- drivers/iio/accel/mxc4005.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/accel/mxc4005.c +1 -1
@@ -502,7 +502,7 @@ static int mxc4005_probe(struct i2c_client *client)return ret; } - indio_dev->trig =iio_trigger_get (data->dready_trig); + indio_dev->default_trigger = data->dready_trig; }return devm_iio_device_register (&client->dev, indio_dev);
Signature · 1 line
2.56.1
Jonathan Cameron
maintainerPATCH 35/35 iio: gyro: mpu3050: use default_trigger
In reply to Jonathan CameronFrom: Jonathan Cameron <[email protected]>
Set indio_dev->default_trigger rather than taking a reference to the trigger in probe(). The IIO core now takes the reference during iio_device_register(), so it is not leaked if probe() fails after the trigger is set up. Whilst the ordering is such that there is no leak in here today, this will make the ordering less fragile going forwards.
--- drivers/iio/gyro/mpu3050-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/gyro/mpu3050-core.c +1 -1
@@ -1171,7 +1171,7 @@ static int mpu3050_trigger_probe(struct iio_dev *indio_dev, int irq)if (ret) goto err_iio_trigger; - indio_dev->trig =iio_trigger_get (mpu3050->trig); + indio_dev->default_trigger = mpu3050->trig;return 0 ;
Signature · 1 line
2.56.1
Re: [PATCH 35/35] iio: gyro: mpu3050: use default_trigger
Quoted 10 lines · Jonathan Cameron
From: Jonathan Cameron <[email protected]>
Set
indio_dev->default_triggerrather than taking a reference to the trigger inprobe(). The IIO core now takes the reference duringiio_device_register(), so it is not leaked ifprobe()fails after the trigger is set up. Whilst the ordering is such that there is no leak in here today, this will make the ordering less fragile going forwards.Signed-off-by Jonathan Cameron <[email protected]>
Yours,
Linus Walleij
Quoted 35 lines · Jonathan Cameron
Note this is compile tested only! I definitely want a few people to test that I didn't break everything.
This is the first part of what I think is a long term solution to the currently really fragile ordering constraints around setting default triggers. Today these are set by
iio_dev->trig=iio_trigger_get(trig). The reference is only released on the iio_dev.release callback and only if we have set the mode to include one that means there might be a trigger set. That mode set only occurs when a buffer is registered. Thus trigger release only works if we have already registered a buffer.One solution would be to reorder so that the default trigger is always set after the buffer registration (we have had various recent proposed patches to do that for individual drivers) but that is non obvious and fragile.
The solution here is easier to reason about. Decouple the setting of the default trigger from the acquisition of a reference. Thus we can acquire the reference in
iio_device_register()when we know all the conditions for it's release have been achieved. So that's all we do in this part 1 series.Part 2 will address immutable triggers and those that are effectively immutable but predate that logic. That is those that set a one to one relationship between device and trigger. That change is rather less mechanical than this one and 35 patches is really a bit big anyway even if most of them are small!
Part 3 and beyond will move to a situation where the
iio_dev->trigwill become opaque. That should stop anyone manipulating that directly. I have a few related cunning plans to test out before we get to that, not because the particular challenge is hard, but because they will trample on the same code.
Quoted 3 lines
Jonathan Cameron (35):
iio: core: adddefault_triggerand take reference in
iio_device_register()
I have reviewed the first patch and briefly looked at the rest which seems like a semi-automatic translation in most cases.
Quoted 34 lines
media: pci: mgb4: use IIO
default_trigger
iio: chemical: ens160: usedefault_trigger
iio: adc: ad4130: usedefault_trigger
iio: adc: ad4170-4: usedefault_trigger
iio: adc: ad7768-1: usedefault_trigger
iio: adc: ad7779: usedefault_trigger
iio: adc: ti-ads131e08: usedefault_trigger
iio: addac: ad74115: usedefault_trigger
iio: addac: ad74413r: usedefault_trigger
iio: temperature: tmp006: usedefault_trigger
iio: adc: ad4062: usedefault_trigger
iio: accel: bma220: usedefault_trigger
iio: accel: bma400: usedefault_trigger
iio: imu: smi330: usedefault_trigger
iio: light: acpi-als: usedefault_trigger
iio: chemical: scd30: usedefault_trigger
iio: accel: bma180: usedefault_trigger
iio: accel: kxcjk-1013: usedefault_trigger
iio: accel: mma8452: usedefault_trigger
iio: chemical: ccs811: usedefault_trigger
iio: pressure: mpl3115: usedefault_trigger
iio: common:st_sensors: usedefault_trigger
iio: common: hid-sensors: usedefault_trigger
iio: pressure: bmp280: usedefault_trigger
iio: light: vl6180: usedefault_trigger
iio: gyro: itg3200: usedefault_trigger
iio: imu: bmi160: usedefault_trigger
iio: accel: adxl355: usedefault_trigger
iio: gyro: adxrs290: usedefault_trigger
iio: accel: adxl372: usedefault_trigger
iio: light: si1145: usedefault_trigger
iio: adc: ti-tsc2046: usedefault_trigger
iio: accel: mxc4005: usedefault_trigger
iio: gyro: mpu3050: usedefault_trigger
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 30 lines · On Mon, 16 Nov 2026 01:47:48 +0000 Jonathan Cameron <[email protected]> wrote:
From: Jonathan Cameron <[email protected]>
Note this is compile tested only! I definitely want a few people to test that I didn't break everything.
This is the first part of what I think is a long term solution to the currently really fragile ordering constraints around setting default triggers. Today these are set by
iio_dev->trig=iio_trigger_get(trig). The reference is only released on the iio_dev.release callback and only if we have set the mode to include one that means there might be a trigger set. That mode set only occurs when a buffer is registered. Thus trigger release only works if we have already registered a buffer.One solution would be to reorder so that the default trigger is always set after the buffer registration (we have had various recent proposed patches to do that for individual drivers) but that is non obvious and fragile.
The solution here is easier to reason about. Decouple the setting of the default trigger from the acquisition of a reference. Thus we can acquire the reference in
iio_device_register()when we know all the conditions for it's release have been achieved. So that's all we do in this part 1 series.Part 2 will address immutable triggers and those that are effectively immutable but predate that logic. That is those that set a one to one relationship between device and trigger. That change is rather less mechanical than this one and 35 patches is really a bit big anyway even if most of them are small!
Quoted 6 lines
Part 3 and beyond will move to a situation where the
iio_dev->trigwill become opaque. That should stop anyone manipulating that directly. I have a few related cunning plans to test out before we get to that, not because the particular challenge is hard, but because they will trample on the same code.
I went through the set and only have one nit (an incorrect fixes tag), otherwise seems fine.
Signature · 2 lines
Kind regards, Joshua Crofts