HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove()
Subsystems (3)
PATCH v2 1/1 HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove()
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.
---
Changes in v2:
- Pass 'data' instead of NUL to device_for_each_child() so sensor_hub_finalize_pending_fn() does not dereference a NULL sd pointer.
Tested against the syzbot C reproducer on linux-next; it runs cleanly with no lockdep warnings or oopses.
drivers/hid/hid-sensor-hub.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hid/hid-sensor-hub.c +4 -3
@@ -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 *sd = data; +unsigned long flags; +spin_lock_irqsave (&sd->lock, flags);if (hsdev->pending.status)complete (&hsdev->pending.ready); +spin_unlock_irqrestore (&sd->lock, flags);return 0 ; }
@@ -846,16 +850,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 , sensor_hub_finalize_pending_fn); -spin_unlock_irqrestore (&data->lock, flags);mfd_remove_devices (&hdev->dev);mutex_destroy (&data->mutex);
Signature · 1 line
2.56.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] HID: hid-sensor-hub: NULL dereference in sensor_hub_finalize_pending_fn() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] HID: hid-sensor-hub: NULL dereference in sensor_hub_finalize_pending_fn()
--
commit 5391cb83740840042fa3d18d4f3751bd37f573af
Author: Ayaan Mirza Baig <[email protected]>
HID: hid-sensor-hub: avoid lock inversion in sensor_hub_remove()
This commit moves spin_lock_irqsave(&data->lock, flags) from sensor_hub_remove() into sensor_hub_finalize_pending_fn() to avoid holding data->lock across device_for_each_child().
Quoted 22 lines
drivers/hid/hid-sensor-hub.c +4
-0@@ -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 *sd = data; +unsigned long flags; +spin_lock_irqsave (&sd->lock, flags);if (hsdev->pending.status)complete (&hsdev->pending.ready); +spin_unlock_irqrestore (&sd->lock, flags);return 0 ; }@@ -846,16 +850,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);
drivers/hid/hid-sensor-hub.c static int sensor_hub_finalize_pending_fn(struct device *dev, void *data) · line 858
Quoted 10 lines
drivers/hid/hid-sensor-hub.c +0
-3-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 , sensor_hub_finalize_pending_fn); -spin_unlock_irqrestore (&data->lock, flags);
[Severity: High]
As reported by Arnav Kapoor on v1 [1], doesn't device_for_each_child() still pass NULL instead of data to sensor_hub_finalize_pending_fn(), causing a NULL pointer dereference at spin_lock_irqsave(&sd->lock, flags) whenever a sensor hub with child devices is removed?
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 3 lines · Ayaan Mirza Baig
Changes in v2:
- Pass 'data' instead of NUL todevice_for_each_child()sosensor_hub_finalize_pending_fn()does not dereference a NULL sd pointer.
...
drivers/hid/hid-sensor-hub.c static int sensor_hub_finalize_pending_fn(struct device *dev, void *data) · line 859
Quoted 2 lines
drivers/hid/hid-sensor-hub.c +0
-0device_for_each_child (&hdev->dev,NULL , sensor_hub_finalize_pending_fn);
This line is still unchanged context in v2, so NULL is still passed and the callback still does spin_lock_irqsave(&sd->lock, flags) on a NULL sd. The diffstat (4 insertions, 3 deletions) matches that: the only removed lines are the lock/unlock pair and the flags variable. Perhaps the change didn't make it into the commit before git format-patch?
Separately, Zheng Shuhao posted a fix for the same syzbot report:
https://lore.kernel.org/all/[email protected]/
It passes the hub data to the callback and also skips children without platform_data (e.g. the HID battery power_supply child with CONFIG_HID_BATTERY_STRENGTH=y), which would otherwise still be dereferenced in sensor_hub_finalize_pending_fn(). It might be worth agreeing between you which version goes forward.
Thanks,
Arnav
Hi Ayaan,
Hmm, I overlooked the latest discussion, which caused some overlap between our patches. Sorry for the confusion.
We can merge the fixes into a single version or find another compromise. Either way works for me.
Thanks,
Zheng