Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH v4] HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove()
@ 2026-11-23 14:55 Ayaan Mirza Baig
  2026-11-28 20:58 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Ayaan Mirza Baig @ 2026-11-23 14:55 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Srinivas Pandruvada,
	Jonathan Cameron
  Cc: Zhang Lixu, Zheng Shuhao, Arnav Kapoor, Lee Jones, Heiko Stuebner,
	linux-input, linux-iio, linux-kernel, syzbot+fb951970a334aaff00ba,
	Ayaan Mirza Baig

In sensor_hub_remove(), spin_lock_irqsave(&data->lock, flags) is held
while calling device_for_each_child(), which iterates child devices via
klist_next() and acquires the driver core's &k->k_lock.

Because &data->lock is acquired in softirq context by
sensor_hub_raw_event() while &k->k_lock is acquired elsewhere with
softirqs enabled, nesting &k->k_lock under &data->lock triggers a
SOFTIRQ-safe -> SOFTIRQ-unsafe lockdep warning and risks an ABBA
deadlock between process context and softirq context.

Move spin_lock_irqsave()/spin_unlock_irqrestore() into
sensor_hub_finalize_pending_fn() and pass data through
device_for_each_child(), so &data->lock is only held while inspecting
and completing each child's pending state after klist_next() releases
&k->k_lock.

Reported-by: syzbot+fb951970a334aaff00ba@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6afddf21.a11fe2ad.1b4426.0003.GAE@google.com/T/
Signed-off-by: Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com>
---
Changes in v4:
 - Actually pass 'data' instead of NULL to device_for_each_child() in
   sensor_hub_remove() (inadvertently omitted in v3).

Changes in v3:
 - Rebase onto hid/for-next over commit 41cb3076a221 ("HID: sensor-hub:
   Remove clients before stopping hardware").
 - Drop the NULL platform_data guard since hid/for-next already checks
   dev_is_platform() and !hsdev.

Changes in v2:
 - Attempted to pass 'data' instead of NULL to device_for_each_child().
 drivers/hid/hid-sensor-hub.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
index e9d6eb3b3831..ce9602945586 100644
--- a/drivers/hid/hid-sensor-hub.c
+++ b/drivers/hid/hid-sensor-hub.c
@@ -808,6 +808,8 @@ static int sensor_hub_probe(struct hid_device *hdev,
 static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
 {
 	struct hid_sensor_hub_device *hsdev;
+	struct sensor_hub_data *sd = data;
+	unsigned long flags;
 
 	if (!dev_is_platform(dev))
 		return 0;
@@ -816,8 +818,10 @@ static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
 	if (!hsdev)
 		return 0;
 
+	spin_lock_irqsave(&sd->lock, flags);
 	if (hsdev->pending.status)
 		complete(&hsdev->pending.ready);
+	spin_unlock_irqrestore(&sd->lock, flags);
 
 	return 0;
 }
@@ -825,14 +829,11 @@ static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
 static void sensor_hub_remove(struct hid_device *hdev)
 {
 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
-	unsigned long flags;
 
 	hid_dbg(hdev, " hardware removed\n");
 
-	spin_lock_irqsave(&data->lock, flags);
-	device_for_each_child(&hdev->dev, NULL,
+	device_for_each_child(&hdev->dev, data,
 			      sensor_hub_finalize_pending_fn);
-	spin_unlock_irqrestore(&data->lock, flags);
 
 	mfd_remove_devices(&hdev->dev);
 	/* Clients share one HID open; release it if references remain. */
-- 
2.56.1


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

* Re: [PATCH v4] HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove()
  2026-11-23 14:55 [PATCH v4] HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove() Ayaan Mirza Baig
@ 2026-11-28 20:58 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2026-11-28 20:58 UTC (permalink / raw)
  To: Ayaan Mirza Baig
  Cc: Jiri Kosina, Benjamin Tissoires, Srinivas Pandruvada, Zhang Lixu,
	Zheng Shuhao, Arnav Kapoor, Lee Jones, Heiko Stuebner,
	linux-input, linux-iio, linux-kernel, syzbot+fb951970a334aaff00ba

On Mon, 23 Nov 2026 20:25:05 +0530
Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com> wrote:

> In sensor_hub_remove(), spin_lock_irqsave(&data->lock, flags) is held
> while calling device_for_each_child(), which iterates child devices via
> klist_next() and acquires the driver core's &k->k_lock.
> 
> Because &data->lock is acquired in softirq context by
> sensor_hub_raw_event() while &k->k_lock is acquired elsewhere with
> softirqs enabled, nesting &k->k_lock under &data->lock triggers a
> SOFTIRQ-safe -> SOFTIRQ-unsafe lockdep warning and risks an ABBA
> deadlock between process context and softirq context.
> 
> Move spin_lock_irqsave()/spin_unlock_irqrestore() into
> sensor_hub_finalize_pending_fn() and pass data through
> device_for_each_child(), so &data->lock is only held while inspecting
> and completing each child's pending state after klist_next() releases
> &k->k_lock.
> 
> Reported-by: syzbot+fb951970a334aaff00ba@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/all/6afddf21.a11fe2ad.1b4426.0003.GAE@google.com/T/
> Signed-off-by: Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com>

This seems fine to me now.  However, sashiko is tripping over it and failing to
apply.  Don't resend just for that, but if you do another version make sure
that the patch includes what it is based on.   Easiest route to that
I've found is let b4 do it for you as that also generates any dependent
patch list with the appropriate IDs.

Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>


> ---
> Changes in v4:
>  - Actually pass 'data' instead of NULL to device_for_each_child() in
>    sensor_hub_remove() (inadvertently omitted in v3).
> 
> Changes in v3:
>  - Rebase onto hid/for-next over commit 41cb3076a221 ("HID: sensor-hub:
>    Remove clients before stopping hardware").
>  - Drop the NULL platform_data guard since hid/for-next already checks
>    dev_is_platform() and !hsdev.
> 
> Changes in v2:
>  - Attempted to pass 'data' instead of NULL to device_for_each_child().
>  drivers/hid/hid-sensor-hub.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
> index e9d6eb3b3831..ce9602945586 100644
> --- a/drivers/hid/hid-sensor-hub.c
> +++ b/drivers/hid/hid-sensor-hub.c
> @@ -808,6 +808,8 @@ static int sensor_hub_probe(struct hid_device *hdev,
>  static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
>  {
>  	struct hid_sensor_hub_device *hsdev;
> +	struct sensor_hub_data *sd = data;
> +	unsigned long flags;
>  
>  	if (!dev_is_platform(dev))
>  		return 0;
> @@ -816,8 +818,10 @@ static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
>  	if (!hsdev)
>  		return 0;
>  
> +	spin_lock_irqsave(&sd->lock, flags);
>  	if (hsdev->pending.status)
>  		complete(&hsdev->pending.ready);
> +	spin_unlock_irqrestore(&sd->lock, flags);
>  
>  	return 0;
>  }
> @@ -825,14 +829,11 @@ static int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
>  static void sensor_hub_remove(struct hid_device *hdev)
>  {
>  	struct sensor_hub_data *data = hid_get_drvdata(hdev);
> -	unsigned long flags;
>  
>  	hid_dbg(hdev, " hardware removed\n");
>  
> -	spin_lock_irqsave(&data->lock, flags);
> -	device_for_each_child(&hdev->dev, NULL,
> +	device_for_each_child(&hdev->dev, data,
>  			      sensor_hub_finalize_pending_fn);
> -	spin_unlock_irqrestore(&data->lock, flags);
>  
>  	mfd_remove_devices(&hdev->dev);
>  	/* Clients share one HID open; release it if references remain. */


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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 14:55 [PATCH v4] HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove() Ayaan Mirza Baig
2026-11-28 20:58 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox