Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] HID: sensor-hub: Avoid holding spinlock across child iteration
@ 2026-11-20  7:56 Zheng Shuhao
  2026-11-20  9:34 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Zheng Shuhao @ 2026-11-20  7:56 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Jonathan Cameron, Srinivas Pandruvada, Benjamin Tissoires,
	linux-input, linux-iio, linux-kernel,
	syzbot+c7c429f6851638ece81338f90c5b29ac9c0dea1f

syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during
sensor hub removal.

sensor_hub_remove() currently holds sensor_data->lock while calling
device_for_each_child(). The child iteration enters the device core and may
acquire the klist lock. Since sensor_data->lock is also acquired with
interrupts disabled by sensor_hub_raw_event(), lockdep reports a possible
lock-order inversion.

The callback only needs sensor_data->lock while checking pending.status and
completing pending.ready. Pass sensor_data to the callback and take the lock
only around that short critical section instead of holding it across
device_for_each_child().

This preserves synchronization of pending.status while avoiding the lock-order
dependency between sensor_data->lock and the device core's klist lock.

The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1f

Testing:
- Reproduced the target lockdep warning with the unmodified kernel in 3/3
  two-CPU QEMU runs.
- The warning was absent with this patch in 3/3 equivalent runs.
- Ran the HID selftests against both baseline and patched kernels:
  hid_bpf: 45 passed, 1 skipped, 0 failed
  hidraw: 27 passed, 0 failed
  hid-core, hid-apple, hid-gamepad, hid-ite, hid-keyboard, hid-mouse,
  hid-multitouch, hid-sony, and hid-tablet all passed in both runs.
- hid-usb_crash.sh and hid-wacom.sh produced TAP 1..0 / rc=2 in both
  kernels because no executable test cases were collected in this minimal
  QEMU environment.
- No KASAN report, use-after-free, Oops, BUG, panic, or general protection
  fault was observed.
- checkpatch.pl --strict --no-signoff reports 0 errors and 0 warnings.

Reported-by: syzbot+c7c429f6851638ece81338f90c5b29ac9c0dea1f@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1f
Signed-off-by: Zheng Shuhao <viennaoffh@gmail.com>
Tested-by: Zheng Shuhao <viennaoffh@gmail.com>
---
 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 6470a290ebfc5..62799c26e6ec3 100644
--- a/drivers/hid/hid-sensor-hub.c
+++ b/drivers/hid/hid-sensor-hub.c
@@ -804,9 +804,13 @@ 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 = dev->platform_data;
+	struct sensor_hub_data *sensor_data = data;
+	unsigned long flags;
 
+	spin_lock_irqsave(&sensor_data->lock, flags);
 	if (hsdev->pending.status)
 		complete(&hsdev->pending.ready);
+	spin_unlock_irqrestore(&sensor_data->lock, flags);
 
 	return 0;
 }
@@ -814,16 +818,13 @@ 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");
 	hid_hw_close(hdev);
 	hid_hw_stop(hdev);
 
-	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);
 	mutex_destroy(&data->mutex);


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

* Re: [PATCH] HID: sensor-hub: Avoid holding spinlock across child iteration
  2026-11-20  7:56 [PATCH] HID: sensor-hub: Avoid holding spinlock across child iteration Zheng Shuhao
@ 2026-11-20  9:34 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20  9:34 UTC (permalink / raw)
  To: Zheng Shuhao; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] HID: sensor-hub: missing Fixes trailer for commit 8a5b38c3fd70
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] HID: sensor-hub: NULL pointer dereference in
  sensor_hub_finalize_pending_fn() [1]
--

commit 243a16dd0439f8612b7bbc316ea2122b572ceb3b
Author: Zheng Shuhao <viennaoffh@gmail.com>

> HID: sensor-hub: Avoid holding spinlock across child iteration
>
> syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during
> sensor hub removal.
>
> sensor_hub_remove() currently holds sensor_data->lock while calling
> device_for_each_child(). The child iteration enters the device core and may
> acquire the klist lock. Since sensor_data->lock is also acquired with
> interrupts disabled by sensor_hub_raw_event(), lockdep reports a possible
> lock-order inversion.
>
> The callback only needs sensor_data->lock while checking pending.status and
> completing pending.ready. Pass sensor_data to the callback and take the lock
> only around that short critical section instead of holding it across
> device_for_each_child().
>
> This preserves synchronization of pending.status while avoiding the lock-order
> dependency between sensor_data->lock and the device core's klist lock.
>
> The syzbot report is available at:
> https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1f
>
> Testing:
> - Reproduced the target lockdep warning with the unmodified kernel in 3/3
>   two-CPU QEMU runs.
> - The warning was absent with this patch in 3/3 equivalent runs.
> - Ran the HID selftests against both baseline and patched kernels:
>   hid_bpf: 45 passed, 1 skipped, 0 failed
>   hidraw: 27 passed, 0 failed
>   hid-core, hid-apple, hid-gamepad, hid-ite, hid-keyboard, hid-mouse,
>   hid-multitouch, hid-sony, and hid-tablet all passed in both runs.
> - hid-usb_crash.sh and hid-wacom.sh produced TAP 1..0 / rc=2 in both
>   kernels because no executable test cases were collected in this minimal
>   QEMU environment.
> - No KASAN report, use-after-free, Oops, BUG, panic, or general protection
>   fault was observed.
> - checkpatch.pl --strict --no-signoff reports 0 errors and 0 warnings.
>
> Reported-by: syzbot+c7c429f6851638ece81338f90c5b29ac9c0dea1f@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1f
> Signed-off-by: Zheng Shuhao <viennaoffh@gmail.com>
> Tested-by: Zheng Shuhao <viennaoffh@gmail.com>

[Severity: Low]
This fixes the device_for_each_child() call under data->lock added in commit
8a5b38c3fd70 ("HID: hid-sensor-hub: don't use stale platform-data on
remove").  Should this include:

  Fixes: 8a5b38c3fd70 ("HID: hid-sensor-hub: don't use stale platform-data on remove")

> diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-hub.c
> index 028e70ec7bace..81015f5b82359 100644
> --- a/drivers/hid/hid-sensor-hub.c
> +++ b/drivers/hid/hid-sensor-hub.c
> @@ -836,9 +836,13 @@ 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 = dev->platform_data;
> +	struct sensor_hub_data *sensor_data = data;
> +	unsigned long flags;
>  
> +	spin_lock_irqsave(&sensor_data->lock, flags);
>  	if (hsdev->pending.status)
>  		complete(&hsdev->pending.ready);
> +	spin_unlock_irqrestore(&sensor_data->lock, flags);

[Severity: High]
This problem wasn't introduced by this patch, but can hsdev be NULL here and
cause a NULL pointer dereference [1]?

When CONFIG_HID_BATTERY_STRENGTH is enabled and a composite HID sensor hub
device also exposes a battery strength usage, hid_hw_start() ->
hidinput_setup_battery() registers a devm-managed power_supply child device
under &hdev->dev with dev->platform_data set to NULL.  On teardown,
sensor_hub_remove() runs before hid_device_remove() releases the devres
group, and hid_hw_stop() only removes input_dev children, so the
power_supply child is still attached when device_for_each_child() calls
sensor_hub_finalize_pending_fn().

[1] https://sashiko.dev/#/bug/linux-23655093-642a-447c-b416-776e47c98d55

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/1795161418.hid-sensor-hub@viennaoffh.gmail.com?part=1

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  7:56 [PATCH] HID: sensor-hub: Avoid holding spinlock across child iteration Zheng Shuhao
2026-11-20  9:34 ` sashiko-bot

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