* [PATCH v4 0/3] iio: adc: at91_adc: fix trigger NULL deref and convert to devm_*
@ 2026-11-17 14:05 Abdelnasser Hussein
2026-11-17 14:05 ` [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove Abdelnasser Hussein
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Abdelnasser Hussein @ 2026-11-17 14:05 UTC (permalink / raw)
To: jic23, linux-iio
Cc: dlechner, nuno.sa, andy, nicolas.ferre, alexandre.belloni,
claudiu.beznea, giorgitchankvetadze1997, amin.gattout,
antoniu.miclaus, gregkh, mripard, linux-arm-kernel, linux-kernel,
Abdelnasser Hussein
This series fixes a NULL pointer dereference in at91_adc when external
triggers are absent from the Device Tree, and converts
the driver to devm_* APIs.
The series is split
into three patches:
1.NULL-pointer fix.
2.introduce a local 'struct device *dev' and use sizeof(*st) in probe().
3.removing manual cleanup and inlining trigger
and buffer setup helpers into probe().
Changes in v4:
- Split v3 into three patches (Joshua Crofts, Jonathan Cameron).
- Patch 1:NULL check.
- Patch 2: Separate the local 'dev' pointer introduction and sizeof(*st)
change (Jonathan Cameron).
- Patch 3:
- Remove at91_adc_allocate_trigger() and at91_adc_buffer_init(),
inline their logic in probe(), and avoid relying on idev->dev.parent
(Jonathan Cameron).
- Remove redundant dev_err() on trigger allocation -ENOMEM
(Jonathan Cameron).
- Use dev_err_probe() in at91_ts_register() and for
devm_iio_device_register() failures in probe() (Jonathan Cameron).
Changes in v3:
- Remove redundant dev_err() after devm_input_allocate_device()
(Andy Shevchenko).
- Introduce local 'struct device *dev = &pdev->dev' in probe()
(Andy Shevchenko).
- Declare the loop iterator inside the for-loop.
Changes in v2:
- Replace the simple NULL check with a full devm_* conversion,
as suggested by Jonathan Cameron.
Abdelnasser Hussein (3):
iio: adc: at91_adc: fix NULL pointer dereference on trigger remove
iio: adc: at91_adc: use local struct device pointer in probe
iio: adc: at91_adc: convert to device-managed resources
drivers/iio/adc/at91_adc.c | 229 ++++++++++---------------------------
1 file changed, 62 insertions(+), 167 deletions(-)
--
2.56.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove
2026-11-17 14:05 [PATCH v4 0/3] iio: adc: at91_adc: fix trigger NULL deref and convert to devm_* Abdelnasser Hussein
@ 2026-11-17 14:05 ` Abdelnasser Hussein
2026-11-17 16:44 ` Andy Shevchenko
2026-11-17 14:05 ` [PATCH v4 2/3] iio: adc: at91_adc: use local struct device pointer in probe Abdelnasser Hussein
2026-11-17 14:05 ` [PATCH v4 3/3] iio: adc: at91_adc: convert to device-managed resources Abdelnasser Hussein
2 siblings, 1 reply; 8+ messages in thread
From: Abdelnasser Hussein @ 2026-11-17 14:05 UTC (permalink / raw)
To: jic23, linux-iio
Cc: dlechner, nuno.sa, andy, nicolas.ferre, alexandre.belloni,
claudiu.beznea, giorgitchankvetadze1997, amin.gattout,
antoniu.miclaus, gregkh, mripard, linux-arm-kernel, linux-kernel,
Abdelnasser Hussein
When the "atmel,adc-use-external-triggers" property is not set in the
Device Tree, at91_adc_trigger_init() skips the external trigger slots
and leaves the corresponding entries in st->trig as NULL.
In the error rollback path of at91_adc_trigger_init() and in
at91_adc_trigger_remove(), iio_trigger_unregister() and
iio_trigger_free() are called on every slot without checking for NULL,
causing a NULL pointer dereference whenever an external trigger slot
was skipped, either because of a partial allocation failure or because
the external triggers are simply not used.
Skip NULL trigger slots in both teardown loops so that only triggers
that were actually allocated are unregistered and freed.
Fixes: 0e589d5fb317 ("ARM: AT91: IIO: Add AT91 ADC driver.")
Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@gmail.com>
---
drivers/iio/adc/at91_adc.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index 0ac74ad92fca..c7fc06214c05 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -660,6 +660,8 @@ static int at91_adc_trigger_init(struct iio_dev *idev)
error_trigger:
for (i--; i >= 0; i--) {
+ if (!st->trig[i])
+ continue;
iio_trigger_unregister(st->trig[i]);
iio_trigger_free(st->trig[i]);
}
@@ -673,6 +675,8 @@ static void at91_adc_trigger_remove(struct iio_dev *idev)
int i;
for (i = 0; i < st->caps->trigger_number; i++) {
+ if (!st->trig[i])
+ continue;
iio_trigger_unregister(st->trig[i]);
iio_trigger_free(st->trig[i]);
}
--
2.56.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 2/3] iio: adc: at91_adc: use local struct device pointer in probe
2026-11-17 14:05 [PATCH v4 0/3] iio: adc: at91_adc: fix trigger NULL deref and convert to devm_* Abdelnasser Hussein
2026-11-17 14:05 ` [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove Abdelnasser Hussein
@ 2026-11-17 14:05 ` Abdelnasser Hussein
2026-11-17 16:50 ` Andy Shevchenko
2026-11-17 14:05 ` [PATCH v4 3/3] iio: adc: at91_adc: convert to device-managed resources Abdelnasser Hussein
2 siblings, 1 reply; 8+ messages in thread
From: Abdelnasser Hussein @ 2026-11-17 14:05 UTC (permalink / raw)
To: jic23, linux-iio
Cc: dlechner, nuno.sa, andy, nicolas.ferre, alexandre.belloni,
claudiu.beznea, giorgitchankvetadze1997, amin.gattout,
antoniu.miclaus, gregkh, mripard, linux-arm-kernel, linux-kernel,
Abdelnasser Hussein
Introduce a local struct device pointer in at91_adc_probe() and use it
in place of &pdev->dev and &idev->dev throughout the function.As a
result, messages previously logged against the IIO device are now
printed using the platform device being probed.
switch devm_iio_device_alloc() to use sizeof(*st) instead of
spelling out the full private structure type.
Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@gmail.com>
---
drivers/iio/adc/at91_adc.c | 46 +++++++++++++++++++-------------------
1 file changed, 23 insertions(+), 23 deletions(-)
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index c7fc06214c05..0553a08b8d24 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -987,32 +987,33 @@ static void at91_ts_unregister(struct at91_adc_state *st)
static int at91_adc_probe(struct platform_device *pdev)
{
unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim;
- struct device_node *node = pdev->dev.of_node;
+ struct device *dev = &pdev->dev;
+ struct device_node *node = dev->of_node;
int ret;
struct iio_dev *idev;
struct at91_adc_state *st;
u32 reg, prop;
const char *s;
- idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
+ idev = devm_iio_device_alloc(dev, sizeof(*st));
if (!idev)
return -ENOMEM;
st = iio_priv(idev);
- st->caps = of_device_get_match_data(&pdev->dev);
+ st->caps = of_device_get_match_data(dev);
st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
if (of_property_read_u32(node, "atmel,adc-channels-used", &prop))
- return dev_err_probe(&idev->dev, -EINVAL,
+ return dev_err_probe(dev, -EINVAL,
"Missing adc-channels-used property in the DT.\n");
st->channels_mask = prop;
st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
if (of_property_read_u32(node, "atmel,adc-startup-time", &prop))
- return dev_err_probe(&idev->dev, -EINVAL,
+ return dev_err_probe(dev, -EINVAL,
"Missing adc-startup-time property in the DT.\n");
st->startup_time = prop;
@@ -1021,7 +1022,7 @@ static int at91_adc_probe(struct platform_device *pdev)
st->sample_hold_time = prop;
if (of_property_read_u32(node, "atmel,adc-vref", &prop))
- return dev_err_probe(&idev->dev, -EINVAL,
+ return dev_err_probe(dev, -EINVAL,
"Missing adc-vref property in the DT.\n");
st->vref_mv = prop;
@@ -1031,21 +1032,21 @@ static int at91_adc_probe(struct platform_device *pdev)
&& !strcmp(s, "lowres"))
st->res = st->caps->low_res_bits;
- dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
+ dev_info(dev, "Resolution used: %u bits\n", st->res);
st->registers = &st->caps->registers;
st->num_channels = st->caps->num_channels;
/* Check if touchscreen is supported. */
if (st->caps->has_ts) {
- ret = at91_adc_probe_dt_ts(node, st, &idev->dev);
+ ret = at91_adc_probe_dt_ts(node, st, dev);
if (ret)
return ret;
}
platform_set_drvdata(pdev, idev);
- idev->name = dev_name(&pdev->dev);
+ idev->name = dev_name(dev);
idev->modes = INDIO_DIRECT_MODE;
idev->info = &at91_adc_info;
@@ -1064,25 +1065,25 @@ static int at91_adc_probe(struct platform_device *pdev)
at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
if (st->caps->has_tsmr)
- ret = devm_request_irq(&pdev->dev, st->irq,
+ ret = devm_request_irq(dev, st->irq,
at91_adc_9x5_interrupt, 0,
- pdev->dev.driver->name, idev);
+ dev->driver->name, idev);
else
- ret = devm_request_irq(&pdev->dev, st->irq,
+ ret = devm_request_irq(dev, st->irq,
at91_adc_rl_interrupt, 0,
- pdev->dev.driver->name, idev);
+ dev->driver->name, idev);
if (ret)
- return dev_err_probe(&pdev->dev, ret,
+ return dev_err_probe(dev, ret,
"Failed to allocate IRQ.\n");
- st->clk = devm_clk_get_enabled(&pdev->dev, "adc_clk");
+ st->clk = devm_clk_get_enabled(dev, "adc_clk");
if (IS_ERR(st->clk))
- return dev_err_probe(&pdev->dev, PTR_ERR(st->clk),
+ return dev_err_probe(dev, PTR_ERR(st->clk),
"Could not prepare or enable the clock.\n");
- st->adc_clk = devm_clk_get_enabled(&pdev->dev, "adc_op_clk");
+ st->adc_clk = devm_clk_get_enabled(dev, "adc_op_clk");
if (IS_ERR(st->adc_clk))
- return dev_err_probe(&pdev->dev, PTR_ERR(st->adc_clk),
+ return dev_err_probe(dev, PTR_ERR(st->adc_clk),
"Could not prepare or enable the ADC clock.\n");
/*
@@ -1094,13 +1095,13 @@ static int at91_adc_probe(struct platform_device *pdev)
adc_clk = clk_get_rate(st->adc_clk);
adc_clk_khz = adc_clk / 1000;
- dev_dbg(&pdev->dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
+ dev_dbg(dev, "Master clock is set as: %d Hz, adc_clk should set as: %d Hz\n",
mstrclk, adc_clk);
prsc = (mstrclk / (2 * adc_clk)) - 1;
if (!st->startup_time)
- return dev_err_probe(&pdev->dev, -EINVAL,
+ return dev_err_probe(dev, -EINVAL,
"No startup time available.\n");
ticks = (*st->caps->calc_startup_ticks)(st->startup_time, adc_clk_khz);
@@ -1127,7 +1128,7 @@ static int at91_adc_probe(struct platform_device *pdev)
/* Setup the ADC channels available on the board */
ret = at91_adc_channel_init(idev);
if (ret < 0)
- return dev_err_probe(&pdev->dev, ret,
+ return dev_err_probe(dev, ret,
"Couldn't initialize the channels.\n");
init_waitqueue_head(&st->wq_data_avail);
@@ -1141,8 +1142,7 @@ static int at91_adc_probe(struct platform_device *pdev)
if (!st->touchscreen_type) {
ret = at91_adc_buffer_init(idev);
if (ret < 0)
- return dev_err_probe(&pdev->dev, ret,
- "Couldn't initialize the buffer.\n");
+ return dev_err_probe(dev, ret, "Couldn't initialize the buffer.\n");
ret = at91_adc_trigger_init(idev);
if (ret < 0) {
--
2.56.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 3/3] iio: adc: at91_adc: convert to device-managed resources
2026-11-17 14:05 [PATCH v4 0/3] iio: adc: at91_adc: fix trigger NULL deref and convert to devm_* Abdelnasser Hussein
2026-11-17 14:05 ` [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove Abdelnasser Hussein
2026-11-17 14:05 ` [PATCH v4 2/3] iio: adc: at91_adc: use local struct device pointer in probe Abdelnasser Hussein
@ 2026-11-17 14:05 ` Abdelnasser Hussein
2026-11-17 16:57 ` Andy Shevchenko
2 siblings, 1 reply; 8+ messages in thread
From: Abdelnasser Hussein @ 2026-11-17 14:05 UTC (permalink / raw)
To: jic23, linux-iio
Cc: dlechner, nuno.sa, andy, nicolas.ferre, alexandre.belloni,
claudiu.beznea, giorgitchankvetadze1997, amin.gattout,
antoniu.miclaus, gregkh, mripard, linux-arm-kernel, linux-kernel,
Abdelnasser Hussein
Convert the driver to device-managed APIs so that all resources are
freed automatically on probe failure and on driver detach:
- allocate and register the IIO triggers directly in at91_adc_probe()
with devm_iio_trigger_alloc() and devm_iio_trigger_register(),
dropping at91_adc_allocate_trigger(), at91_adc_trigger_init() and
at91_adc_trigger_remove() along with their manual rollback paths
- set up the triggered buffer with devm_iio_triggered_buffer_setup()
directly in at91_adc_probe(), dropping at91_adc_buffer_init() and
at91_adc_buffer_remove()
- allocate the touchscreen input device with
devm_input_allocate_device(), dropping at91_ts_unregister() and the
manual input_free_device() error path in at91_ts_register()
- register the IIO device with devm_iio_device_register()
at91_adc_probe() no longer needs any error unwinding labels and
at91_adc_remove() is removed together with the platform driver's
.remove callback.
Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@gmail.com>
---
drivers/iio/adc/at91_adc.c | 187 ++++++++-----------------------------
1 file changed, 39 insertions(+), 148 deletions(-)
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index 0553a08b8d24..e4cf5b1f231e 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -605,94 +605,6 @@ static const struct iio_trigger_ops at91_adc_trigger_ops = {
.set_trigger_state = &at91_adc_configure_trigger,
};
-static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
- const struct at91_adc_trigger *trigger)
-{
- struct iio_trigger *trig;
- int ret;
-
- trig = iio_trigger_alloc(idev->dev.parent, "%s-dev%d-%s", idev->name,
- iio_device_id(idev), trigger->name);
- if (trig == NULL)
- return NULL;
-
- iio_trigger_set_drvdata(trig, idev);
- trig->ops = &at91_adc_trigger_ops;
-
- ret = iio_trigger_register(trig);
- if (ret) {
- iio_trigger_free(trig);
- return NULL;
- }
-
- return trig;
-}
-
-static int at91_adc_trigger_init(struct iio_dev *idev)
-{
- struct at91_adc_state *st = iio_priv(idev);
- int i, ret;
-
- st->trig = devm_kcalloc(&idev->dev,
- st->caps->trigger_number, sizeof(*st->trig),
- GFP_KERNEL);
-
- if (st->trig == NULL) {
- ret = -ENOMEM;
- goto error_ret;
- }
-
- for (i = 0; i < st->caps->trigger_number; i++) {
- if (st->caps->triggers[i].is_external && !(st->use_external))
- continue;
-
- st->trig[i] = at91_adc_allocate_trigger(idev,
- st->caps->triggers + i);
- if (st->trig[i] == NULL) {
- dev_err(&idev->dev,
- "Could not allocate trigger %d\n", i);
- ret = -ENOMEM;
- goto error_trigger;
- }
- }
-
- return 0;
-
-error_trigger:
- for (i--; i >= 0; i--) {
- if (!st->trig[i])
- continue;
- iio_trigger_unregister(st->trig[i]);
- iio_trigger_free(st->trig[i]);
- }
-error_ret:
- return ret;
-}
-
-static void at91_adc_trigger_remove(struct iio_dev *idev)
-{
- struct at91_adc_state *st = iio_priv(idev);
- int i;
-
- for (i = 0; i < st->caps->trigger_number; i++) {
- if (!st->trig[i])
- continue;
- iio_trigger_unregister(st->trig[i]);
- iio_trigger_free(st->trig[i]);
- }
-}
-
-static int at91_adc_buffer_init(struct iio_dev *idev)
-{
- return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
- &at91_adc_trigger_handler, NULL);
-}
-
-static void at91_adc_buffer_remove(struct iio_dev *idev)
-{
- iio_triggered_buffer_cleanup(idev);
-}
-
static int at91_adc_read_raw(struct iio_dev *idev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
@@ -928,13 +840,10 @@ static int at91_ts_register(struct iio_dev *idev,
{
struct at91_adc_state *st = iio_priv(idev);
struct input_dev *input;
- int ret;
- input = input_allocate_device();
- if (!input) {
- dev_err(&idev->dev, "Failed to allocate TS device!\n");
+ input = devm_input_allocate_device(&pdev->dev);
+ if (!input)
return -ENOMEM;
- }
input->name = DRIVER_NAME;
input->id.bustype = BUS_HOST;
@@ -952,12 +861,9 @@ static int at91_ts_register(struct iio_dev *idev,
0, 0);
input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
} else {
- if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE) {
- dev_err(&pdev->dev,
- "This touchscreen controller only support 4 wires\n");
- ret = -EINVAL;
- goto err;
- }
+ if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE)
+ return dev_err_probe(&pdev->dev, -EINVAL,
+ "This touchscreen controller only support 4 wires\n");
input_set_abs_params(input, ABS_X, 0, (1 << MAX_RLPOS_BITS) - 1,
0, 0);
@@ -968,20 +874,7 @@ static int at91_ts_register(struct iio_dev *idev,
st->ts_input = input;
input_set_drvdata(input, st);
- ret = input_register_device(input);
- if (ret)
- goto err;
-
- return ret;
-
-err:
- input_free_device(input);
- return ret;
-}
-
-static void at91_ts_unregister(struct at91_adc_state *st)
-{
- input_unregister_device(st->ts_input);
+ return input_register_device(input);
}
static int at91_adc_probe(struct platform_device *pdev)
@@ -1140,15 +1033,39 @@ static int at91_adc_probe(struct platform_device *pdev)
* trigger for classic adc.
*/
if (!st->touchscreen_type) {
- ret = at91_adc_buffer_init(idev);
+ ret = devm_iio_triggered_buffer_setup(dev, idev,
+ &iio_pollfunc_store_time,
+ &at91_adc_trigger_handler,
+ NULL);
if (ret < 0)
return dev_err_probe(dev, ret, "Couldn't initialize the buffer.\n");
- ret = at91_adc_trigger_init(idev);
- if (ret < 0) {
- dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
- at91_adc_buffer_remove(idev);
- return ret;
+ st->trig = devm_kcalloc(dev, st->caps->trigger_number,
+ sizeof(*st->trig), GFP_KERNEL);
+ if (!st->trig)
+ return -ENOMEM;
+
+ for (int i = 0; i < st->caps->trigger_number; i++) {
+ struct iio_trigger *trig;
+
+ if (st->caps->triggers[i].is_external && !st->use_external)
+ continue;
+
+ trig = devm_iio_trigger_alloc(dev, "%s-dev%d-%s",
+ idev->name,
+ iio_device_id(idev),
+ st->caps->triggers[i].name);
+ if (!trig)
+ return -ENOMEM;
+
+ iio_trigger_set_drvdata(trig, idev);
+ trig->ops = &at91_adc_trigger_ops;
+
+ ret = devm_iio_trigger_register(dev, trig);
+ if (ret)
+ return ret;
+
+ st->trig[i] = trig;
}
} else {
ret = at91_ts_register(idev, pdev);
@@ -1158,36 +1075,11 @@ static int at91_adc_probe(struct platform_device *pdev)
at91_ts_hw_init(idev, adc_clk_khz);
}
- ret = iio_device_register(idev);
- if (ret < 0) {
- dev_err(&pdev->dev, "Couldn't register the device.\n");
- goto error_iio_device_register;
- }
+ ret = devm_iio_device_register(dev, idev);
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Couldn't register the device.\n");
return 0;
-
-error_iio_device_register:
- if (!st->touchscreen_type) {
- at91_adc_trigger_remove(idev);
- at91_adc_buffer_remove(idev);
- } else {
- at91_ts_unregister(st);
- }
- return ret;
-}
-
-static void at91_adc_remove(struct platform_device *pdev)
-{
- struct iio_dev *idev = platform_get_drvdata(pdev);
- struct at91_adc_state *st = iio_priv(idev);
-
- iio_device_unregister(idev);
- if (!st->touchscreen_type) {
- at91_adc_trigger_remove(idev);
- at91_adc_buffer_remove(idev);
- } else {
- at91_ts_unregister(st);
- }
}
static int at91_adc_suspend(struct device *dev)
@@ -1337,7 +1229,6 @@ MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
static struct platform_driver at91_adc_driver = {
.probe = at91_adc_probe,
- .remove = at91_adc_remove,
.driver = {
.name = DRIVER_NAME,
.of_match_table = at91_adc_dt_ids,
--
2.56.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove
2026-11-17 14:05 ` [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove Abdelnasser Hussein
@ 2026-11-17 16:44 ` Andy Shevchenko
2026-11-17 16:45 ` Andy Shevchenko
0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2026-11-17 16:44 UTC (permalink / raw)
To: Abdelnasser Hussein
Cc: jic23, linux-iio, dlechner, nuno.sa, andy, nicolas.ferre,
alexandre.belloni, claudiu.beznea, giorgitchankvetadze1997,
amin.gattout, antoniu.miclaus, gregkh, mripard, linux-arm-kernel,
linux-kernel
On Tue, Nov 17, 2026 at 04:05:08PM +0200, Abdelnasser Hussein wrote:
> When the "atmel,adc-use-external-triggers" property is not set in the
> Device Tree, at91_adc_trigger_init() skips the external trigger slots
> and leaves the corresponding entries in st->trig as NULL.
>
> In the error rollback path of at91_adc_trigger_init() and in
> at91_adc_trigger_remove(), iio_trigger_unregister() and
> iio_trigger_free() are called on every slot without checking for NULL,
> causing a NULL pointer dereference whenever an external trigger slot
> was skipped, either because of a partial allocation failure or because
> the external triggers are simply not used.
>
> Skip NULL trigger slots in both teardown loops so that only triggers
> that were actually allocated are unregistered and freed.
...
> for (i = 0; i < st->caps->trigger_number; i++) {
> + if (!st->trig[i])
> + continue;
> iio_trigger_unregister(st->trig[i]);
> iio_trigger_free(st->trig[i]);
> }
Yes, but no. Please, rather fix the above mentioned IIO trigger APIs to be
NULL-aware.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove
2026-11-17 16:44 ` Andy Shevchenko
@ 2026-11-17 16:45 ` Andy Shevchenko
0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-11-17 16:45 UTC (permalink / raw)
To: Abdelnasser Hussein
Cc: jic23, linux-iio, dlechner, nuno.sa, andy, nicolas.ferre,
alexandre.belloni, claudiu.beznea, giorgitchankvetadze1997,
amin.gattout, antoniu.miclaus, gregkh, mripard, linux-arm-kernel,
linux-kernel
On Tue, Nov 17, 2026 at 06:44:37PM +0200, Andy Shevchenko wrote:
> On Tue, Nov 17, 2026 at 04:05:08PM +0200, Abdelnasser Hussein wrote:
...
> > for (i = 0; i < st->caps->trigger_number; i++) {
> > + if (!st->trig[i])
> > + continue;
> > iio_trigger_unregister(st->trig[i]);
> > iio_trigger_free(st->trig[i]);
> > }
>
> Yes, but no. Please, rather fix the above mentioned IIO trigger APIs to be
> NULL-aware.
Actually the iio_trigger_free() is already NULL-aware, so make it only for
iio_trigger_unregister().
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] iio: adc: at91_adc: use local struct device pointer in probe
2026-11-17 14:05 ` [PATCH v4 2/3] iio: adc: at91_adc: use local struct device pointer in probe Abdelnasser Hussein
@ 2026-11-17 16:50 ` Andy Shevchenko
0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-11-17 16:50 UTC (permalink / raw)
To: Abdelnasser Hussein
Cc: jic23, linux-iio, dlechner, nuno.sa, andy, nicolas.ferre,
alexandre.belloni, claudiu.beznea, giorgitchankvetadze1997,
amin.gattout, antoniu.miclaus, gregkh, mripard, linux-arm-kernel,
linux-kernel
On Tue, Nov 17, 2026 at 04:05:09PM +0200, Abdelnasser Hussein wrote:
> Introduce a local struct device pointer in at91_adc_probe() and use it
> in place of &pdev->dev and &idev->dev throughout the function.As a
> result, messages previously logged against the IIO device are now
> printed using the platform device being probed.
>
> switch devm_iio_device_alloc() to use sizeof(*st) instead of
> spelling out the full private structure type.
...
> + struct device *dev = &pdev->dev;
> + struct device_node *node = dev->of_node;
Before doing this, switch to device_property_*() / fwnode_property_*() APIs
first.
...
> - st->caps = of_device_get_match_data(&pdev->dev);
> + st->caps = of_device_get_match_data(dev);
In a separate patch (perhaps in the first one in this block, convert this
to use device_get_match_data() and add an error check (for now until we able
the driver_override case to address differently.
> st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
>
> if (of_property_read_u32(node, "atmel,adc-channels-used", &prop))
> - return dev_err_probe(&idev->dev, -EINVAL,
> + return dev_err_probe(dev, -EINVAL,
> "Missing adc-channels-used property in the DT.\n");
> st->channels_mask = prop;
>
> st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
>
> if (of_property_read_u32(node, "atmel,adc-startup-time", &prop))
> - return dev_err_probe(&idev->dev, -EINVAL,
> + return dev_err_probe(dev, -EINVAL,
> "Missing adc-startup-time property in the DT.\n");
> st->startup_time = prop;
...
> if (st->caps->has_tsmr)
> - ret = devm_request_irq(&pdev->dev, st->irq,
> + ret = devm_request_irq(dev, st->irq,
> at91_adc_9x5_interrupt, 0,
> - pdev->dev.driver->name, idev);
> + dev->driver->name, idev);
> else
> - ret = devm_request_irq(&pdev->dev, st->irq,
> + ret = devm_request_irq(dev, st->irq,
> at91_adc_rl_interrupt, 0,
> - pdev->dev.driver->name, idev);
> + dev->driver->name, idev);
> if (ret)
> - return dev_err_probe(&pdev->dev, ret,
> + return dev_err_probe(dev, ret,
> "Failed to allocate IRQ.\n");
In a separate patch, before this one drop this dup message. It's printed
by the devm_request_irq().
...
> adc_clk_khz = adc_clk / 1000;
At some point better to use HZ_PER_KHZ instead of odd 1000.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 3/3] iio: adc: at91_adc: convert to device-managed resources
2026-11-17 14:05 ` [PATCH v4 3/3] iio: adc: at91_adc: convert to device-managed resources Abdelnasser Hussein
@ 2026-11-17 16:57 ` Andy Shevchenko
0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-11-17 16:57 UTC (permalink / raw)
To: Abdelnasser Hussein
Cc: jic23, linux-iio, dlechner, nuno.sa, andy, nicolas.ferre,
alexandre.belloni, claudiu.beznea, giorgitchankvetadze1997,
amin.gattout, antoniu.miclaus, gregkh, mripard, linux-arm-kernel,
linux-kernel
On Tue, Nov 17, 2026 at 04:05:10PM +0200, Abdelnasser Hussein wrote:
> Convert the driver to device-managed APIs so that all resources are
> freed automatically on probe failure and on driver detach:
>
> - allocate and register the IIO triggers directly in at91_adc_probe()
> with devm_iio_trigger_alloc() and devm_iio_trigger_register(),
> dropping at91_adc_allocate_trigger(), at91_adc_trigger_init() and
> at91_adc_trigger_remove() along with their manual rollback paths
> - set up the triggered buffer with devm_iio_triggered_buffer_setup()
> directly in at91_adc_probe(), dropping at91_adc_buffer_init() and
> at91_adc_buffer_remove()
> - allocate the touchscreen input device with
> devm_input_allocate_device(), dropping at91_ts_unregister() and the
> manual input_free_device() error path in at91_ts_register()
> - register the IIO device with devm_iio_device_register()
>
> at91_adc_probe() no longer needs any error unwinding labels and
> at91_adc_remove() is removed together with the platform driver's
> .remove callback.
...
> static int at91_ts_register(struct iio_dev *idev,
The pdev is not used in this function, which suggests to use 'struct device
*dev' as a second parameter instead.
Please, make that change before this one in a separate patch.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-17 16:57 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 14:05 [PATCH v4 0/3] iio: adc: at91_adc: fix trigger NULL deref and convert to devm_* Abdelnasser Hussein
2026-11-17 14:05 ` [PATCH v4 1/3] iio: adc: at91_adc: fix NULL pointer dereference on trigger remove Abdelnasser Hussein
2026-11-17 16:44 ` Andy Shevchenko
2026-11-17 16:45 ` Andy Shevchenko
2026-11-17 14:05 ` [PATCH v4 2/3] iio: adc: at91_adc: use local struct device pointer in probe Abdelnasser Hussein
2026-11-17 16:50 ` Andy Shevchenko
2026-11-17 14:05 ` [PATCH v4 3/3] iio: adc: at91_adc: convert to device-managed resources Abdelnasser Hussein
2026-11-17 16:57 ` Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox