mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock
@ 2026-11-27  3:10 Ilia Snezhkov via B4 Relay
  2026-11-27  9:30 ` Nuno Sá
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-27  3:10 UTC (permalink / raw)
  To: jic23, dlechner, nuno.sa, andy
  Cc: Andy Shevchenko, Arnav Kapoor, linux-iio, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

__fwnode_iio_channel_get() dereferences indio_dev->info->fwnode_xlate
and invokes it without holding info_exist_lock. Concurrent
iio_device_unregister() clears indio_dev->info under info_exist_lock,
allowing a NULL dereference or callback execution during provider
teardown.

Hold iio_dev_opaque->info_exist_lock with scoped_guard(mutex, ...)
matching the rest of inkern.c, and return -EPROBE_DEFER if
indio_dev->info is NULL. Since iio_device_unregister() calls
cdev_device_del() before clearing indio_dev->info, seeing a NULL info
pointer corresponds to racing right before device removal from
iio_bus_type, where !idev already returns -EPROBE_DEFER. Using
-EPROBE_DEFER also avoids colliding with the -ENODEV sentinel used by
callers to continue parent or map-list lookups and avoids the named
lookup -EINVAL fallback in __fwnode_iio_channel_get_by_name().

Fixes: acd8256723f2 ("iio: inkern: Add of_xlate function to struct iio_info")
Reviewed-by: Arnav Kapoor <kapoorarnav43@gmail.com>
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
Changes in v3:
- Place `struct iio_dev_opaque *iio_dev_opaque;` in reverse xmas tree
  order (Andy Shevchenko).
- Move `iio_dev_opaque = to_iio_dev_opaque(indio_dev);` right after
  `channel->indio_dev = indio_dev;` immediately before scoped_guard()
  (Andy Shevchenko).
- Carry Reviewed-by from Arnav Kapoor.

Changes in v2:
- Return -EPROBE_DEFER instead of -ENODEV when indio_dev->info is NULL
  so callers do not treat it as the "no channel on this node, keep
  looking" sentinel and named lookups do not overwrite it to -EINVAL
  (Arnav Kapoor).
- Drop the unrelated indio_dev->num_channels bounds check from this
  locking fix (Arnav Kapoor).
- Move the AI coding assistant note below the --- separator.

Link: https://lore.kernel.org/r/20261126060615.63790-1-iasnezhkov@gmail.com
Found and drafted with an AI coding assistant.

 drivers/iio/inkern.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 1f22275..92b6679 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -145,6 +145,7 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
 				    struct fwnode_handle *fwnode, int index)
 {
+	struct iio_dev_opaque *iio_dev_opaque;
 	struct fwnode_reference_args iiospec;
 	struct device *idev;
 	struct iio_dev *indio_dev;
 	int err;
@@ -162,10 +163,15 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
 	}
 
 	indio_dev = dev_to_iio_dev(idev);
 	channel->indio_dev = indio_dev;
-	if (indio_dev->info->fwnode_xlate)
-		index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
-	else
-		index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
+	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+	scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
+		if (!indio_dev->info)
+			index = -EPROBE_DEFER;
+		else if (indio_dev->info->fwnode_xlate)
+			index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
+		else
+			index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
+	}
 	fwnode_handle_put(iiospec.fwnode);
 	if (index < 0)
 		goto err_put;
-- 
2.54.0 (Apple Git-157)



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock
  2026-11-27  3:10 [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock Ilia Snezhkov via B4 Relay
@ 2026-11-27  9:30 ` Nuno Sá
  2026-11-27 17:22 ` Andy Shevchenko
  2026-11-28  1:44 ` Jonathan Cameron
  2 siblings, 0 replies; 4+ messages in thread
From: Nuno Sá @ 2026-11-27  9:30 UTC (permalink / raw)
  To: Ilia Snezhkov
  Cc: jic23, dlechner, andy, Andy Shevchenko, Arnav Kapoor, linux-iio,
	linux-kernel

On Fri, Nov 27, 2026 at 03:10:38AM +0000, Ilia Snezhkov wrote:
> __fwnode_iio_channel_get() dereferences indio_dev->info->fwnode_xlate
> and invokes it without holding info_exist_lock. Concurrent
> iio_device_unregister() clears indio_dev->info under info_exist_lock,
> allowing a NULL dereference or callback execution during provider
> teardown.
> 
> Hold iio_dev_opaque->info_exist_lock with scoped_guard(mutex, ...)
> matching the rest of inkern.c, and return -EPROBE_DEFER if
> indio_dev->info is NULL. Since iio_device_unregister() calls
> cdev_device_del() before clearing indio_dev->info, seeing a NULL info
> pointer corresponds to racing right before device removal from
> iio_bus_type, where !idev already returns -EPROBE_DEFER. Using
> -EPROBE_DEFER also avoids colliding with the -ENODEV sentinel used by
> callers to continue parent or map-list lookups and avoids the named
> lookup -EINVAL fallback in __fwnode_iio_channel_get_by_name().
> 
> Fixes: acd8256723f2 ("iio: inkern: Add of_xlate function to struct iio_info")
> Reviewed-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
> ---
> Changes in v3:
> - Place `struct iio_dev_opaque *iio_dev_opaque;` in reverse xmas tree
>   order (Andy Shevchenko).
> - Move `iio_dev_opaque = to_iio_dev_opaque(indio_dev);` right after
>   `channel->indio_dev = indio_dev;` immediately before scoped_guard()
>   (Andy Shevchenko).
> - Carry Reviewed-by from Arnav Kapoor.
> 
> Changes in v2:
> - Return -EPROBE_DEFER instead of -ENODEV when indio_dev->info is NULL
>   so callers do not treat it as the "no channel on this node, keep
>   looking" sentinel and named lookups do not overwrite it to -EINVAL
>   (Arnav Kapoor).
> - Drop the unrelated indio_dev->num_channels bounds check from this
>   locking fix (Arnav Kapoor).
> - Move the AI coding assistant note below the --- separator.
> 
> Link: https://lore.kernel.org/r/20261126060615.63790-1-iasnezhkov@gmail.com
> Found and drafted with an AI coding assistant.

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

> 
>  drivers/iio/inkern.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 1f22275..92b6679 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -145,6 +145,7 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
>  				    struct fwnode_handle *fwnode, int index)
>  {
> +	struct iio_dev_opaque *iio_dev_opaque;
>  	struct fwnode_reference_args iiospec;
>  	struct device *idev;
>  	struct iio_dev *indio_dev;
>  	int err;
> @@ -162,10 +163,15 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
>  	}
>  
>  	indio_dev = dev_to_iio_dev(idev);
>  	channel->indio_dev = indio_dev;
> -	if (indio_dev->info->fwnode_xlate)
> -		index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
> -	else
> -		index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
> +	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
> +	scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
> +		if (!indio_dev->info)
> +			index = -EPROBE_DEFER;
> +		else if (indio_dev->info->fwnode_xlate)
> +			index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
> +		else
> +			index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
> +	}
>  	fwnode_handle_put(iiospec.fwnode);
>  	if (index < 0)
>  		goto err_put;
> -- 
> 2.54.0 (Apple Git-157)
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock
  2026-11-27  3:10 [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock Ilia Snezhkov via B4 Relay
  2026-11-27  9:30 ` Nuno Sá
@ 2026-11-27 17:22 ` Andy Shevchenko
  2026-11-28  1:44 ` Jonathan Cameron
  2 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2026-11-27 17:22 UTC (permalink / raw)
  To: iasnezhkov
  Cc: jic23, dlechner, nuno.sa, andy, Arnav Kapoor, linux-iio, linux-kernel

On Fri, Nov 27, 2026 at 03:10:38AM +0000, Ilia Snezhkov via B4 Relay wrote:
> 
> __fwnode_iio_channel_get() dereferences indio_dev->info->fwnode_xlate
> and invokes it without holding info_exist_lock. Concurrent
> iio_device_unregister() clears indio_dev->info under info_exist_lock,
> allowing a NULL dereference or callback execution during provider
> teardown.
> 
> Hold iio_dev_opaque->info_exist_lock with scoped_guard(mutex, ...)
> matching the rest of inkern.c, and return -EPROBE_DEFER if
> indio_dev->info is NULL. Since iio_device_unregister() calls
> cdev_device_del() before clearing indio_dev->info, seeing a NULL info
> pointer corresponds to racing right before device removal from
> iio_bus_type, where !idev already returns -EPROBE_DEFER. Using
> -EPROBE_DEFER also avoids colliding with the -ENODEV sentinel used by
> callers to continue parent or map-list lookups and avoids the named
> lookup -EINVAL fallback in __fwnode_iio_channel_get_by_name().

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock
  2026-11-27  3:10 [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock Ilia Snezhkov via B4 Relay
  2026-11-27  9:30 ` Nuno Sá
  2026-11-27 17:22 ` Andy Shevchenko
@ 2026-11-28  1:44 ` Jonathan Cameron
  2 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2026-11-28  1:44 UTC (permalink / raw)
  To: Ilia Snezhkov via B4 Relay
  Cc: iasnezhkov, dlechner, nuno.sa, andy, Andy Shevchenko,
	Arnav Kapoor, linux-iio, linux-kernel

On Fri, 27 Nov 2026 03:10:38 +0000
Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org> wrote:

> From: Ilia Snezhkov <iasnezhkov@gmail.com>
> 
> __fwnode_iio_channel_get() dereferences indio_dev->info->fwnode_xlate
> and invokes it without holding info_exist_lock. Concurrent
> iio_device_unregister() clears indio_dev->info under info_exist_lock,
> allowing a NULL dereference or callback execution during provider
> teardown.
> 
> Hold iio_dev_opaque->info_exist_lock with scoped_guard(mutex, ...)
> matching the rest of inkern.c, and return -EPROBE_DEFER if
> indio_dev->info is NULL. Since iio_device_unregister() calls
> cdev_device_del() before clearing indio_dev->info, seeing a NULL info
> pointer corresponds to racing right before device removal from
> iio_bus_type, where !idev already returns -EPROBE_DEFER. Using
> -EPROBE_DEFER also avoids colliding with the -ENODEV sentinel used by
> callers to continue parent or map-list lookups and avoids the named
> lookup -EINVAL fallback in __fwnode_iio_channel_get_by_name().
> 
> Fixes: acd8256723f2 ("iio: inkern: Add of_xlate function to struct iio_info")
> Reviewed-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
Good catch.

I'm not going to rush this one in given we are getting latish in the cycle.
So queued up on the testing branch of iio.git for the next merge window.
Marked it for stable.

Thanks,

Jonathan

> ---
> Changes in v3:
> - Place `struct iio_dev_opaque *iio_dev_opaque;` in reverse xmas tree
>   order (Andy Shevchenko).
> - Move `iio_dev_opaque = to_iio_dev_opaque(indio_dev);` right after
>   `channel->indio_dev = indio_dev;` immediately before scoped_guard()
>   (Andy Shevchenko).
> - Carry Reviewed-by from Arnav Kapoor.
> 
> Changes in v2:
> - Return -EPROBE_DEFER instead of -ENODEV when indio_dev->info is NULL
>   so callers do not treat it as the "no channel on this node, keep
>   looking" sentinel and named lookups do not overwrite it to -EINVAL
>   (Arnav Kapoor).
> - Drop the unrelated indio_dev->num_channels bounds check from this
>   locking fix (Arnav Kapoor).
> - Move the AI coding assistant note below the --- separator.
> 
> Link: https://lore.kernel.org/r/20261126060615.63790-1-iasnezhkov@gmail.com
> Found and drafted with an AI coding assistant.
> 
>  drivers/iio/inkern.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 1f22275..92b6679 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -145,6 +145,7 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
>  				    struct fwnode_handle *fwnode, int index)
>  {
> +	struct iio_dev_opaque *iio_dev_opaque;
>  	struct fwnode_reference_args iiospec;
>  	struct device *idev;
>  	struct iio_dev *indio_dev;
>  	int err;
> @@ -162,10 +163,15 @@ static int __fwnode_iio_channel_get(struct iio_channel *channel,
>  	}
>  
>  	indio_dev = dev_to_iio_dev(idev);
>  	channel->indio_dev = indio_dev;
> -	if (indio_dev->info->fwnode_xlate)
> -		index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
> -	else
> -		index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
> +	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
> +	scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
> +		if (!indio_dev->info)
> +			index = -EPROBE_DEFER;
> +		else if (indio_dev->info->fwnode_xlate)
> +			index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
> +		else
> +			index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
> +	}
>  	fwnode_handle_put(iiospec.fwnode);
>  	if (index < 0)
>  		goto err_put;


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-28  1:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  3:10 [PATCH v3] iio: inkern: guard fwnode_xlate with info_exist_lock Ilia Snezhkov via B4 Relay
2026-11-27  9:30 ` Nuno Sá
2026-11-27 17:22 ` Andy Shevchenko
2026-11-28  1:44 ` Jonathan Cameron

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®