HID: sensor-hub: Avoid holding spinlock across child iteration
Subsystems (3)
PATCH 1/1 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.
--- drivers/hid/hid-sensor-hub.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hid/hid-sensor-hub.c +5 -4
@@ -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);
[email protected]
AI reviewThank 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…
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 <[email protected]>
Quoted 38 lines
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 holdssensor_data->lockwhile callingdevice_for_each_child(). The child iteration enters the device core and may acquire the klist lock. Sincesensor_data->lockis also acquired with interrupts disabled bysensor_hub_raw_event(), lockdep reports a possible lock-order inversion.The callback only needs
sensor_data->lockwhile checking pending.status and completing pending.ready. Passsensor_datato the callback and take the lock only around that short critical section instead of holding it acrossdevice_for_each_child().This preserves synchronization of pending.status while avoiding the lock-order dependency between
sensor_data->lockand the device core's klist lock.The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1fTesting:
- 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.shandhid-wacom.shproduced 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.
Quoted 4 lines
[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:
Quoted 5 lines
drivers/hid/hid-sensor-hub.c +0
-0
drivers/hid/hid-sensor-hub.c static int sensor_hub_probe(struct hid_device *hdev, · line 845
Quoted 10 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 *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
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 36 lines · Zheng Shuhao
syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during sensor hub removal.
sensor_hub_remove()currently holdssensor_data->lockwhile callingdevice_for_each_child(). The child iteration enters the device core and may acquire the klist lock. Sincesensor_data->lockis also acquired with interrupts disabled bysensor_hub_raw_event(), lockdep reports a possible lock-order inversion.The callback only needs
sensor_data->lockwhile checking pending.status and completing pending.ready. Passsensor_datato the callback and take the lock only around that short critical section instead of holding it acrossdevice_for_each_child().This preserves synchronization of pending.status while avoiding the lock-order dependency between
sensor_data->lockand the device core's klist lock.The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1fTesting:
- 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.shandhid-wacom.shproduced 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.
Quoted 4 lines
You cannot add a Tested-by tag with your name. This is reserved for when someone independent of you tests your patch. Looking at the syzbot report there is a reproducer, so syzbot should test it. You can make syzbot test a patch by sending the following (note, this will trigger a test):
#syz test
Signature · 2 lines
Kind regards, Joshua Crofts
syzbot
syzbotI see the command but can't find the corresponding bug. The email is sent to [email protected] address but the HASH does not correspond to any known bug. Please double check the…
Quoted 41 lines · Zheng Shuhao
Quoted 36 lines
syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during sensor hub removal.
sensor_hub_remove()currently holdssensor_data->lockwhile callingdevice_for_each_child(). The child iteration enters the device core and may acquire the klist lock. Sincesensor_data->lockis also acquired with interrupts disabled bysensor_hub_raw_event(), lockdep reports a possible lock-order inversion.The callback only needs
sensor_data->lockwhile checking pending.status and completing pending.ready. Passsensor_datato the callback and take the lock only around that short critical section instead of holding it acrossdevice_for_each_child().This preserves synchronization of pending.status while avoiding the lock-order dependency between
sensor_data->lockand the device core's klist lock.The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1fTesting:
- 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.shandhid-wacom.shproduced 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.Quoted 4 lines
Quoted 6 lines
You cannot add a Tested-by tag with your name. This is reserved for when someone independent of you tests your patch. Looking at the syzbot report there is a reproducer, so syzbot should test it. You can make syzbot test a patch by sending the following (note, this will trigger a test):
#syz test
I see the command but can't find the corresponding bug. The email is sent to [email protected] address but the HASH does not correspond to any known bug. Please double check the address.
Quoted 5 lines
Signature · 3 lines
-- Kind regards, Joshua Crofts
Quoted 37 lines · Joshua Crofts
Quoted 36 lines · Zheng Shuhao
syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during sensor hub removal.
sensor_hub_remove()currently holdssensor_data->lockwhile callingdevice_for_each_child(). The child iteration enters the device core and may acquire the klist lock. Sincesensor_data->lockis also acquired with interrupts disabled bysensor_hub_raw_event(), lockdep reports a possible lock-order inversion.The callback only needs
sensor_data->lockwhile checking pending.status and completing pending.ready. Passsensor_datato the callback and take the lock only around that short critical section instead of holding it acrossdevice_for_each_child().This preserves synchronization of pending.status while avoiding the lock-order dependency between
sensor_data->lockand the device core's klist lock.The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1fTesting:
- 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.shandhid-wacom.shproduced 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.
Quoted 8 lines
Quoted 4 lines
You cannot add a Tested-by tag with your name. This is reserved for when someone independent of you tests your patch. Looking at the syzbot report there is a reproducer, so syzbot should test it. You can make syzbot test a patch by sending the following (note, this will trigger a test):
Ah, did some digging and the syzbot email is incorrect, it should be:
Cc'd the right syzbot and triggering the test.
#syz test
Signature · 2 lines
Kind regards, Joshua Crofts
syzbot
syzbotPatch testing via #syz test is temporarily disabled. See https://github.com/google/syzkaller/issues/8122 for details.
Quoted 49 lines · Joshua Crofts
Quoted 37 lines · Zheng Shuhao
Quoted 36 lines
syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during sensor hub removal.
sensor_hub_remove()currently holdssensor_data->lockwhile callingdevice_for_each_child(). The child iteration enters the device core and may acquire the klist lock. Sincesensor_data->lockis also acquired with interrupts disabled bysensor_hub_raw_event(), lockdep reports a possible lock-order inversion.The callback only needs
sensor_data->lockwhile checking pending.status and completing pending.ready. Passsensor_datato the callback and take the lock only around that short critical section instead of holding it acrossdevice_for_each_child().This preserves synchronization of pending.status while avoiding the lock-order dependency between
sensor_data->lockand the device core's klist lock.The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1fTesting:
- 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.shandhid-wacom.shproduced 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.Quoted 8 lines
Quoted 4 lines
You cannot add a Tested-by tag with your name. This is reserved for when someone independent of you tests your patch. Looking at the syzbot report there is a reproducer, so syzbot should test it. You can make syzbot test a patch by sending the following (note, this will trigger a test):
Quoted 7 lines
Ah, did some digging and the syzbot email is incorrect, it should be:
Cc'd the right syzbot and triggering the test.
#syz test
Patch testing via #syz test is temporarily disabled. See https://github.com/google/syzkaller/issues/8122 for details.
Quoted 5 lines
Signature · 3 lines
-- Kind regards, Joshua Crofts
Srinivas Pandruvada
maintainerQuoted 19 lines · Zheng Shuhao
syzbot reports a SOFTIRQ-safe -> SOFTIRQ-unsafe lock order warning during
sensor hub removal.
sensor_hub_remove()currently holdssensor_data->lockwhile callingdevice_for_each_child(). The child iteration enters the device core and may
acquire the klist lock. Sincesensor_data->lockis also acquired with interrupts disabled bysensor_hub_raw_event(), lockdep reports a possible
lock-order inversion.The callback only needs
sensor_data->lockwhile checking pending.status and
completing pending.ready. Passsensor_datato the callback and take the lock
only around that short critical section instead of holding it acrossdevice_for_each_child().
Quoted 8 lines
This preserves synchronization of pending.status while avoiding the lock-order
dependency betweensensor_data->lockand the device core's klist lock.The syzbot report is available at:
https://syzkaller.appspot.com/bug?id=c7c429f6851638ece81338f90c5b29ac9c0dea1f
Ayaan posted a similar patch yesterday.
Thanks,
Srinivas
Quoted 69 lines
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.shandhid-wacom.shproduced 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=c7c429f6851638ece81338f90c5b29ac9c0dea1fSigned-off-by Zheng Shuhao <[email protected]>Tested-by Zheng Shuhao <[email protected]>---
drivers/hid/hid-sensor-hub.c | 9 +++++----1 file changed, 5 insertions(+), 4 deletions(-)drivers/hid/hid-sensor- renamed from drivers/hid/hid-sensor-hub.c +0
-0
hub.c
index6470a290ebfc5..62799c26e6ec3100644drivers/hid/hid-sensor-hub.c +0
-0@@ -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)
{
structhid_sensor_hub_device*hsdev =dev->platform_data;drivers/hid/hid-sensor-hub.c +2
-0+struct sensor_hub_data *sensor_data = data; +unsigned long flags;drivers/hid/hid-sensor-hub.c +1
-0+spin_lock_irqsave (&sensor_data->lock, flags);if (
hsdev->pending.status)
complete(&hsdev->pending.ready);drivers/hid/hid-sensor-hub.c +1
-0+spin_unlock_irqrestore (&sensor_data->lock, flags);
return 0;
}drivers/hid/hid-sensor-hub.c +0
-1@@ -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);drivers/hid/hid-sensor-hub.c +1
-2-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);drivers/hid/hid-sensor-hub.c +0
-1-spin_unlock_irqrestore (&data->lock, flags);
mfd_remove_devices(&hdev->dev);
mutex_destroy(&data->mutex);
Quoted 1 line
Ayaan Mirza Baig posted a similar patch yesterday.
Sorry, I missed Ayaan's earlier patch. I've refined the fix to pass the callback data and skip children without Sensor Hub platform_data, and am sending v2 separately. The #syz test request was rejected because syzbot's automated patch testing is temporarily unavailable.
Thanks,
Zheng
PATCH v2 1/1 HID: sensor-hub: Avoid holding spinlock across child iteration
In reply to Zheng Shuhaosensor_hub_remove() holds sensor_data->lock while walking the HID device's children with device_for_each_child(). The child iteration takes the device core's klist lock, while sensor_hub_raw_event() takes sensor_data->lock with interrupts disabled. This creates a lock-order inversion.
Pass sensor_data to the callback and take the lock only while examining pending.status and completing pending.ready.
The callback also visits non-Sensor Hub children. When HID battery strength support is enabled, the HID input layer may register a power-supply child without platform_data. Skip children without Sensor Hub data before dereferencing it.
Testing:
- With CONFIG_HID_BATTERY_STRENGTH=y, ran a UHID Sensor Hub/Joystick device exposing Battery Strength through 15 create/destroy cycles across three QEMU boots. The power-supply child was found each time; no Oops, BUG, or KASAN report occurred.
- In the same configuration, hid_bpf passed 45 tests with 1 skip, hidraw passed 27 tests, and hid-core, hid-apple, hid-gamepad, hid-ite, hid-keyboard, and hid-mouse passed.
- The full HID suite was not completed; it stopped during hid-multitouch.
- The #syz test request was rejected because syzbot's automated patch testing is temporarily unavailable.
---
Changes in v2:
- Pass sensor_data to the callback instead of NULL.
- Skip children without Sensor Hub platform_data; add a battery-child test.
- Add Fixes and correct the syzbot address; remove the self-added Tested-by.
drivers/hid/hid-sensor-hub.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/hid/hid-sensor-hub.c +8 -4
@@ -804,9 +804,16 @@ 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; + +if (!hsdev) +return 0 ; +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 +821,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);
Srinivas Pandruvada
maintainer+Lixu
Quoted 19 lines · Zheng Shuhao
sensor_hub_remove()holdssensor_data->lockwhile walking the HID device's
children withdevice_for_each_child(). The child iteration takes the device
core's klist lock, whilesensor_hub_raw_event()takessensor_data->lockwith interrupts disabled. This creates a lock-order inversion.Pass
sensor_datato the callback and take the lock only while examining
pending.status and completing pending.ready.The callback also visits non-Sensor Hub children. When HID battery strength
support is enabled, the HID input layer may register a power-supply child
withoutplatform_data. Skip children without Sensor Hub data before dereferencing it.
Quoted 16 lines
Testing:
- WithCONFIG_HID_BATTERY_STRENGTH=y, ran a UHID Sensor Hub/Joystick device
exposing Battery Strength through 15 create/destroy cycles across three
QEMU boots. The power-supply child was found each time; no Oops, BUG, or
KASAN report occurred.
- In the same configuration,hid_bpfpassed 45 tests with 1 skip, hidraw
passed 27 tests, and hid-core, hid-apple, hid-gamepad, hid-ite, hid-keyboard, and hid-mouse passed.
- The full HID suite was not completed; it stopped during hid- multitouch.
- The #syz test request was rejected because syzbot's automated patch testing is temporarily unavailable.
None of the above tested devices are HID SENSOR HUB related, so need some tests.
Hi Lixu,
Please give a try.
Thanks,
Srinivas
Quoted 60 lines
Fixes 8a5b38c3fd70 ("HID: hid-sensor-hub: don't use stale platform-data on remove")
Reported-by [email protected]Signed-off-by Zheng Shuhao <[email protected]>---
Changes in v2:
- Passsensor_datato the callback instead of NULL.
- Skip children without Sensor Hubplatform_data; add a battery-child test.
- Add Fixes and correct the syzbot address; remove the self-added Tested-by.drivers/hid/hid-sensor-hub.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)drivers/hid/hid-sensor- renamed from drivers/hid/hid-sensor-hub.c +0
-0
hub.c
index6470a290ebfc5..4c447f62480b5100644drivers/hid/hid-sensor-hub.c +0
-0@@ -804,9 +804,16 @@ static int sensor_hub_probe(struct hid_device *hdev,static int
sensor_hub_finalize_pending_fn(struct device *dev, void *data)
{
structhid_sensor_hub_device*hsdev =dev->platform_data;drivers/hid/hid-sensor-hub.c +5
-0+struct sensor_hub_data *sensor_data = data; +unsigned long flags; + +if (!hsdev) +return 0 ;drivers/hid/hid-sensor-hub.c +1
-0+spin_lock_irqsave (&sensor_data->lock, flags);if (
hsdev->pending.status)
complete(&hsdev->pending.ready);drivers/hid/hid-sensor-hub.c +1
-0+spin_unlock_irqrestore (&sensor_data->lock, flags);
return 0;
}drivers/hid/hid-sensor-hub.c +0
-1@@ -814,16 +821,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);drivers/hid/hid-sensor-hub.c +1
-2-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);drivers/hid/hid-sensor-hub.c +0
-1-spin_unlock_irqrestore (&data->lock, flags);
mfd_remove_devices(&hdev->dev);
mutex_destroy(&data->mutex);
Srinivas Pandruvada
maintainerQuoted 13 lines · Srinivas Pandruvada
+Lixu
Quoted 10 lines · Zheng Shuhao
sensor_hub_remove()holdssensor_data->lockwhile walking the HID device's
children withdevice_for_each_child(). The child iteration takes the
device
core's klist lock, whilesensor_hub_raw_event()takessensor_data->lockwith interrupts disabled. This creates a lock- order
inversion.
In reality this can't happen as thehid_hw_close() and hid_hw_stop() is called before, so there can't be any raw_event.
Quoted 11 lines
Quoted 11 lines
Pass
sensor_datato the callback and take the lock only while examining
pending.status and completing pending.ready.The callback also visits non-Sensor Hub children. When HID battery strength
support is enabled, the HID input layer may register a power-supply child
withoutplatform_data. Skip children without Sensor Hub data before dereferencing it.
How come this is possible? The HID sensor hub is only deleting its own children. Can you give an example here?
Thanks,
Srinivas
Quoted 91 lines
Quoted 10 lines
Testing:
- WithCONFIG_HID_BATTERY_STRENGTH=y, ran a UHID Sensor Hub/Joystick
device
exposing Battery Strength through 15 create/destroy cycles across three
QEMU boots. The power-supply child was found each time; no Oops, BUG, or
KASAN report occurred.
- In the same configuration,hid_bpfpassed 45 tests with 1 skip,Quoted 8 lines
hidraw
passed 27 tests, and hid-core, hid-apple, hid-gamepad, hid-ite, hid-keyboard, and hid-mouse passed.
- The full HID suite was not completed; it stopped during hid- multitouch.
- The #syz test request was rejected because syzbot's automated patch
testing is temporarily unavailable.None of the above tested devices are HID SENSOR HUB related, so need some tests.
Hi Lixu,Please give a try.
Thanks,
SrinivasQuoted 63 lines
Fixes 8a5b38c3fd70 ("HID: hid-sensor-hub: don't use staleplatform-
data on remove")Reported-by [email protected]Closes:
https://syzkaller.appspot.com/bug?extid=fb951970a334aaff00baSigned-off-by Zheng Shuhao <[email protected]>---
Changes in v2:
- Passsensor_datato the callback instead of NULL.
- Skip children without Sensor Hubplatform_data; add a battery- child
test.
- Add Fixes and correct the syzbot address; remove the self-added Tested-by.drivers/hid/hid-sensor-hub.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)drivers/hid/hid-sensor- renamed from drivers/hid/hid-sensor-hub.c +0
-0
hub.c
index6470a290ebfc5..4c447f62480b5100644drivers/hid/hid-sensor-hub.c +0
-0@@ -804,9 +804,16 @@ static int sensor_hub_probe(struct hid_device *hdev,static int
sensor_hub_finalize_pending_fn(struct device *dev, void *data)
{
structhid_sensor_hub_device*hsdev =dev->platform_data;drivers/hid/hid-sensor-hub.c +5
-0+struct sensor_hub_data *sensor_data = data; +unsigned long flags; + +if (!hsdev) +return 0 ;drivers/hid/hid-sensor-hub.c +1
-0+spin_lock_irqsave (&sensor_data->lock, flags);if (
hsdev->pending.status)
complete(&hsdev->pending.ready);drivers/hid/hid-sensor-hub.c +1
-0+spin_unlock_irqrestore (&sensor_data->lock, flags);
return 0;
}drivers/hid/hid-sensor-hub.c +0
-1@@ -814,16 +821,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);drivers/hid/hid-sensor-hub.c +1
-2-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);drivers/hid/hid-sensor-hub.c +0
-1-spin_unlock_irqrestore (&data->lock, flags);
mfd_remove_devices(&hdev->dev);
mutex_destroy(&data->mutex);
Quoted 2 lines
In reality this can't happen as the
hid_hw_close()andhid_hw_stop()is called before, so there can't be anyraw_event.
It might not be reachable after hid_hw_stop(). I may have misunderstood the lockdep report and overstated this as a possible deadlock. Sorry.
Quoted 2 lines
How come this is possible? The HID sensor hub is only deleting its own children. Can you give an example here?
With CONFIG_HID_BATTERY_STRENGTH=y, this UHID device creates a Sensor Hub and a Joystick battery. The HID input layer registers the battery power supply as a child of the HID device, without platform_data.
With the unpatched hid-sensor-hub module, UHID_DESTROY triggers a KASAN NULL-pointer dereference in sensor_hub_finalize_pending_fn():
KASAN: null-ptr-deref in range [0x0000000000000028-0x000000000000002f]
RIP: sensor_hub_finalize_pending_fn+0x44/0x65 [hid_sensor_hub]
Call trace: device_for_each_child -> sensor_hub_remove -> hid_device_remove
-> hid_destroy_device -> uhid_dev_destroy -> uhid_char_writeThe reproducer is below:
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uhid.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static const unsigned char sensor_battery_rdesc[] = {
0x05, 0x20, /* Usage Page (Sensors) */
0x09, 0x10, /* Usage (Sensor Hub) */
0xa1, 0x01, /* Collection (Application) */
0xc0, /* End Collection */
0x05, 0x01, /* Usage Page (Generic Desktop) */
0x09, 0x04, /* Usage (Joystick) */
0xa1, 0x01, /* Collection (Application) */
0x05, 0x06, /* Usage Page (Generic Device Controls) */
0x09, 0x20, /* Usage (Battery Strength) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x64, /* Logical Maximum (100) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x01, /* Report Count (1) */
0x81, 0x02, /* Input (Data, Variable, Absolute) */
0xc0, /* End Collection */
};static int send_event(int fd, struct uhid_event *event)
{
ssize_t written = write(fd, event, sizeof(*event));
if (written == sizeof(*event))
return 0;
if (written < 0)
perror("write /dev/uhid");
else
fprintf(stderr, "short UHID write: %zd\n", written);
return -1;
}static bool hid_battery_exists(void)
{
static const char prefix[] = "hid-0003:1234:5678.";
DIR *dir = opendir("/sys/class/power_supply");
struct dirent *entry;
bool found = false;
if (!dir)
return false;
while ((entry = readdir(dir))) {
if (!strncmp(entry->d_name, prefix, sizeof(prefix) - 1)) {
found = true;
break;
}
}
closedir(dir);
return found;
}int main(void)
{
struct uhid_event event = { 0 };
struct pollfd pfd;
int fd, attempt;
bool started = false;
bool battery_found = false;
fd = open("/dev/uhid", O_RDWR | O_CLOEXEC);
if (fd < 0) {
perror("open /dev/uhid");
return 1;
}
event.type = UHID_CREATE2;
strcpy((char *)event.u.create2.name, "sensor-hub-battery-null-test");
strcpy((char *)event.u.create2.phys, "hid-null-test");
event.u.create2.rd_size = sizeof(sensor_battery_rdesc);
event.u.create2.bus = BUS_USB;
event.u.create2.vendor = 0x1234;
event.u.create2.product = 0x5678;
memcpy(event.u.create2.rd_data, sensor_battery_rdesc,
sizeof(sensor_battery_rdesc));
if (send_event(fd, &event))
goto out_close;
pfd.fd = fd;
pfd.events = POLLIN;
if (poll(&pfd, 1, 5000) <= 0) {
fprintf(stderr, "timed out waiting for UHID_START\n");
goto out_destroy;
}
memset(&event, 0, sizeof(event));
if (read(fd, &event, sizeof(event)) != sizeof(event)) {
perror("read UHID event");
goto out_destroy;
}
started = event.type == UHID_START;
if (!started) {
fprintf(stderr, "unexpected UHID event: %u\n", event.type);
goto out_destroy;
}
for (attempt = 0; attempt < 50 && !hid_battery_exists(); attempt++)
usleep(100000);
if (!hid_battery_exists()) {
fprintf(stderr, "HID battery power-supply child was not created\n");
goto out_destroy;
}
battery_found = true;
puts("HID battery power-supply child created");out_destroy:
memset(&event, 0, sizeof(event));
event.type = UHID_DESTROY;
if (send_event(fd, &event))
goto out_close;
usleep(300000);
if (!started)
goto out_close;
puts("UHID device destroyed without userspace error");
close(fd);
return battery_found ? 0 : 1;out_close:
close(fd); return 1; }
Srinivas Pandruvada
maintainerQuoted 22 lines · Zheng Shuhao
> In reality this can't happen as the
hid_hw_close()andhid_hw_stop()Quoted 1 line
is called before, so there can't be any
raw_event.It might not be reachable after
hid_hw_stop(). I may have misunderstood the
lockdep report and overstated this as a possible deadlock. Sorry.Quoted 3 lines
How come this is possible? The HID sensor hub is only deleting its own
children. Can you give an example here?With
CONFIG_HID_BATTERY_STRENGTH=y, this UHID device creates a Sensor Hub and
a Joystick battery. The HID input layer registers the battery power supply as
a child of the HID device, withoutplatform_data.With the unpatched hid-sensor-hub module,
UHID_DESTROYtriggers a KASAN
NULL-pointer dereference insensor_hub_finalize_pending_fn():
Quoted 8 lines
KASAN: null-ptr-deref in range [0x0000000000000028- 0x000000000000002f] RIP: sensor_hub_finalize_pending_fn+0x44/0x65 [hid_sensor_hub] Call trace: device_for_each_child -> sensor_hub_remove ->
hid_device_remove
->hid_destroy_device->uhid_dev_destroy->uhid_char_write
OK, Adding to commit description will help for others.
Thanks,
Srinivas
Quoted 138 lines
The reproducer is below:
#define
_GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uhid.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>static const unsigned char sensor_battery_rdesc[] = { 0x05, 0x20, /* Usage Page (Sensors) */ 0x09, 0x10, /* Usage (Sensor Hub) */ 0xa1, 0x01, /* Collection (Application) */ 0xc0, /* End Collection */ 0x05, 0x01, /* Usage Page (Generic Desktop) */ 0x09, 0x04, /* Usage (Joystick) */ 0xa1, 0x01, /* Collection (Application) */ 0x05, 0x06, /* Usage Page (Generic Device Controls) */ 0x09, 0x20, /* Usage (Battery Strength) */ 0x15, 0x00, /* Logical Minimum (0) */ 0x25, 0x64, /* Logical Maximum (100) */ 0x75, 0x08, /* Report Size (8) */ 0x95, 0x01, /* Report Count (1) */ 0x81, 0x02, /* Input (Data, Variable, Absolute) */ 0xc0, /* End Collection */ };static int
send_event(int fd, struct uhid_event *event){ ssize_t written = write(fd, event, sizeof(*event)); if (written == sizeof(*event)) return 0; if (written < 0) perror("write /dev/uhid"); else fprintf(stderr, "short UHID write: %zd\n", written); return -1; }static bool
hid_battery_exists(void){ static const char prefix[] = "hid-0003:1234:5678."; DIR *dir = opendir("/sys/class/power_supply"); struct dirent *entry; bool found = false; if (!dir) return false; while ((entry = readdir(dir))) { if (!strncmp(entry->d_name, prefix, sizeof(prefix) - 1)) { found = true; break; } } closedir(dir); return found; }int main(void)
{ struct uhid_event event = { 0 }; struct pollfd pfd; int fd, attempt; bool started = false; bool battery_found = false; fd = open("/dev/uhid", O_RDWR | O_CLOEXEC); if (fd < 0) { perror("open /dev/uhid"); return 1; } event.type = UHID_CREATE2; strcpy((char *)event.u.create2.name, "sensor-hub-battery- null-test"); strcpy((char *)event.u.create2.phys, "hid-null-test"); event.u.create2.rd_size = sizeof(sensor_battery_rdesc); event.u.create2.bus = BUS_USB; event.u.create2.vendor = 0x1234; event.u.create2.product = 0x5678; memcpy(event.u.create2.rd_data, sensor_battery_rdesc, sizeof(sensor_battery_rdesc)); if (send_event(fd, &event)) goto out_close; pfd.fd = fd; pfd.events = POLLIN; if (poll(&pfd, 1, 5000) <= 0) { fprintf(stderr, "timed out waiting for UHID_START\n"); goto out_destroy; } memset(&event, 0, sizeof(event)); if (read(fd, &event, sizeof(event)) != sizeof(event)) { perror("read UHID event"); goto out_destroy; } started = event.type == UHID_START; if (!started) { fprintf(stderr, "unexpected UHID event: %u\n", event.type); goto out_destroy; } for (attempt = 0; attempt < 50 && !hid_battery_exists(); attempt++) usleep(100000); if (!hid_battery_exists()) { fprintf(stderr, "HID battery power-supply child was not created\n"); goto out_destroy; } battery_found = true; puts("HID battery power-supply child created");
out_destroy:memset(&event, 0, sizeof(event)); event.type = UHID_DESTROY; if (send_event(fd, &event)) goto out_close; usleep(300000); if (!started) goto out_close; puts("UHID device destroyed without userspace error"); close(fd); return battery_found ? 0 : 1;
out_close:close(fd); return 1; }
Jonathan Cameron
maintainerQuoted 32 lines · On Fri, 20 Nov 2026 09:56:41 -0800 Srinivas Pandruvada…
Quoted 22 lines · Zheng Shuhao
> In reality this can't happen as the
hid_hw_close()andhid_hw_stop()Quoted 1 line
is called before, so there can't be any
raw_event.It might not be reachable after
hid_hw_stop(). I may have misunderstood the
lockdep report and overstated this as a possible deadlock. Sorry.Quoted 3 lines
How come this is possible? The HID sensor hub is only deleting its own
children. Can you give an example here?With
CONFIG_HID_BATTERY_STRENGTH=y, this UHID device creates a Sensor Hub and
a Joystick battery. The HID input layer registers the battery power supply as
a child of the HID device, withoutplatform_data.With the unpatched hid-sensor-hub module,
UHID_DESTROYtriggers a KASAN
NULL-pointer dereference insensor_hub_finalize_pending_fn():Quoted 8 lines
KASAN: null-ptr-deref in range [0x0000000000000028- 0x000000000000002f] RIP: sensor_hub_finalize_pending_fn+0x44/0x65 [hid_sensor_hub] Call trace: device_for_each_child -> sensor_hub_remove ->
hid_device_remove
->hid_destroy_device->uhid_dev_destroy->uhid_char_write
Quoted 1 line
OK, Adding to commit description will help for others.
Process thing. Zhang Lixu, please do not send a new version in reply to an older one. I only spotted this today because I was working for patchwork. Otherwise it would be way back in my email history and probably lost.
New version, new email thread. Avoids breaking tools and keeps things in order for those reviewing patches. It seems the LLMs are following the suggested process for the git project itself. As far as I aware no part of the kernel has ever asked for this style of replying to an earlier commit and many actively oppose it because it makes a massive mess for large series.
Thanks,
Jonathan
Quoted 143 lines
Thanks,
SrinivasQuoted 138 lines
The reproducer is below:
#define
_GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uhid.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>static const unsigned char sensor_battery_rdesc[] = { 0x05, 0x20, /* Usage Page (Sensors) */ 0x09, 0x10, /* Usage (Sensor Hub) */ 0xa1, 0x01, /* Collection (Application) */ 0xc0, /* End Collection */ 0x05, 0x01, /* Usage Page (Generic Desktop) */ 0x09, 0x04, /* Usage (Joystick) */ 0xa1, 0x01, /* Collection (Application) */ 0x05, 0x06, /* Usage Page (Generic Device Controls) */ 0x09, 0x20, /* Usage (Battery Strength) */ 0x15, 0x00, /* Logical Minimum (0) */ 0x25, 0x64, /* Logical Maximum (100) */ 0x75, 0x08, /* Report Size (8) */ 0x95, 0x01, /* Report Count (1) */ 0x81, 0x02, /* Input (Data, Variable, Absolute) */ 0xc0, /* End Collection */ };static int
send_event(int fd, struct uhid_event *event){ ssize_t written = write(fd, event, sizeof(*event)); if (written == sizeof(*event)) return 0; if (written < 0) perror("write /dev/uhid"); else fprintf(stderr, "short UHID write: %zd\n", written); return -1; }static bool
hid_battery_exists(void){ static const char prefix[] = "hid-0003:1234:5678."; DIR *dir = opendir("/sys/class/power_supply"); struct dirent *entry; bool found = false; if (!dir) return false; while ((entry = readdir(dir))) { if (!strncmp(entry->d_name, prefix, sizeof(prefix) - 1)) { found = true; break; } } closedir(dir); return found; }int main(void)
{ struct uhid_event event = { 0 }; struct pollfd pfd; int fd, attempt; bool started = false; bool battery_found = false; fd = open("/dev/uhid", O_RDWR | O_CLOEXEC); if (fd < 0) { perror("open /dev/uhid"); return 1; } event.type = UHID_CREATE2; strcpy((char *)event.u.create2.name, "sensor-hub-battery- null-test"); strcpy((char *)event.u.create2.phys, "hid-null-test"); event.u.create2.rd_size = sizeof(sensor_battery_rdesc); event.u.create2.bus = BUS_USB; event.u.create2.vendor = 0x1234; event.u.create2.product = 0x5678; memcpy(event.u.create2.rd_data, sensor_battery_rdesc, sizeof(sensor_battery_rdesc)); if (send_event(fd, &event)) goto out_close; pfd.fd = fd; pfd.events = POLLIN; if (poll(&pfd, 1, 5000) <= 0) { fprintf(stderr, "timed out waiting for UHID_START\n"); goto out_destroy; } memset(&event, 0, sizeof(event)); if (read(fd, &event, sizeof(event)) != sizeof(event)) { perror("read UHID event"); goto out_destroy; } started = event.type == UHID_START; if (!started) { fprintf(stderr, "unexpected UHID event: %u\n", event.type); goto out_destroy; } for (attempt = 0; attempt < 50 && !hid_battery_exists(); attempt++) usleep(100000); if (!hid_battery_exists()) { fprintf(stderr, "HID battery power-supply child was not created\n"); goto out_destroy; } battery_found = true; puts("HID battery power-supply child created");
out_destroy:memset(&event, 0, sizeof(event)); event.type = UHID_DESTROY; if (send_event(fd, &event)) goto out_close; usleep(300000); if (!started) goto out_close; puts("UHID device destroyed without userspace error"); close(fd); return battery_found ? 0 : 1;
out_close:close(fd); return 1; }
Jonathan Cameron
maintainerQuoted 35 lines · On Sat, 21 Nov 2026 20:02:30 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 32 lines · On Fri, 20 Nov 2026 09:56:41 -0800 Srinivas Pandruvada…
Quoted 30 lines · Zheng Shuhao
> In reality this can't happen as the
hid_hw_close()andhid_hw_stop()Quoted 1 line
is called before, so there can't be any
raw_event.It might not be reachable after
hid_hw_stop(). I may have misunderstood the
lockdep report and overstated this as a possible deadlock. Sorry.Quoted 3 lines
How come this is possible? The HID sensor hub is only deleting its own
children. Can you give an example here?With
CONFIG_HID_BATTERY_STRENGTH=y, this UHID device creates a Sensor Hub and
a Joystick battery. The HID input layer registers the battery power supply as
a child of the HID device, withoutplatform_data.With the unpatched hid-sensor-hub module,
UHID_DESTROYtriggers a KASAN
NULL-pointer dereference insensor_hub_finalize_pending_fn():KASAN: null-ptr-deref in range [0x0000000000000028- 0x000000000000002f] RIP: sensor_hub_finalize_pending_fn+0x44/0x65 [hid_sensor_hub] Call trace: device_for_each_child -> sensor_hub_remove ->
hid_device_remove
->hid_destroy_device->uhid_dev_destroy->uhid_char_write
Quoted 3 lines
Quoted 1 line
OK, Adding to commit description will help for others.
Process thing. Zhang Lixu, please do not send a new version
Sorry! That was for Zheng Shuhao.
I realised a fraction of a second too late to hit the cancel button :(
Quoted 161 lines
in reply to an older one. I only spotted this today because I was working for patchwork. Otherwise it would be way back in my email history and probably lost.
New version, new email thread. Avoids breaking tools and keeps things in order for those reviewing patches. It seems the LLMs are following the suggested process for the git project itself. As far as I aware no part of the kernel has ever asked for this style of replying to an earlier commit and many actively oppose it because it makes a massive mess for large series.
Thanks,
Jonathan
Quoted 143 lines
Thanks,
SrinivasQuoted 138 lines
The reproducer is below:
#define
_GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uhid.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>static const unsigned char sensor_battery_rdesc[] = { 0x05, 0x20, /* Usage Page (Sensors) */ 0x09, 0x10, /* Usage (Sensor Hub) */ 0xa1, 0x01, /* Collection (Application) */ 0xc0, /* End Collection */ 0x05, 0x01, /* Usage Page (Generic Desktop) */ 0x09, 0x04, /* Usage (Joystick) */ 0xa1, 0x01, /* Collection (Application) */ 0x05, 0x06, /* Usage Page (Generic Device Controls) */ 0x09, 0x20, /* Usage (Battery Strength) */ 0x15, 0x00, /* Logical Minimum (0) */ 0x25, 0x64, /* Logical Maximum (100) */ 0x75, 0x08, /* Report Size (8) */ 0x95, 0x01, /* Report Count (1) */ 0x81, 0x02, /* Input (Data, Variable, Absolute) */ 0xc0, /* End Collection */ };static int
send_event(int fd, struct uhid_event *event){ ssize_t written = write(fd, event, sizeof(*event)); if (written == sizeof(*event)) return 0; if (written < 0) perror("write /dev/uhid"); else fprintf(stderr, "short UHID write: %zd\n", written); return -1; }static bool
hid_battery_exists(void){ static const char prefix[] = "hid-0003:1234:5678."; DIR *dir = opendir("/sys/class/power_supply"); struct dirent *entry; bool found = false; if (!dir) return false; while ((entry = readdir(dir))) { if (!strncmp(entry->d_name, prefix, sizeof(prefix) - 1)) { found = true; break; } } closedir(dir); return found; }int main(void)
{ struct uhid_event event = { 0 }; struct pollfd pfd; int fd, attempt; bool started = false; bool battery_found = false; fd = open("/dev/uhid", O_RDWR | O_CLOEXEC); if (fd < 0) { perror("open /dev/uhid"); return 1; } event.type = UHID_CREATE2; strcpy((char *)event.u.create2.name, "sensor-hub-battery- null-test"); strcpy((char *)event.u.create2.phys, "hid-null-test"); event.u.create2.rd_size = sizeof(sensor_battery_rdesc); event.u.create2.bus = BUS_USB; event.u.create2.vendor = 0x1234; event.u.create2.product = 0x5678; memcpy(event.u.create2.rd_data, sensor_battery_rdesc, sizeof(sensor_battery_rdesc)); if (send_event(fd, &event)) goto out_close; pfd.fd = fd; pfd.events = POLLIN; if (poll(&pfd, 1, 5000) <= 0) { fprintf(stderr, "timed out waiting for UHID_START\n"); goto out_destroy; } memset(&event, 0, sizeof(event)); if (read(fd, &event, sizeof(event)) != sizeof(event)) { perror("read UHID event"); goto out_destroy; } started = event.type == UHID_START; if (!started) { fprintf(stderr, "unexpected UHID event: %u\n", event.type); goto out_destroy; } for (attempt = 0; attempt < 50 && !hid_battery_exists(); attempt++) usleep(100000); if (!hid_battery_exists()) { fprintf(stderr, "HID battery power-supply child was not created\n"); goto out_destroy; } battery_found = true; puts("HID battery power-supply child created");
out_destroy:memset(&event, 0, sizeof(event)); event.type = UHID_DESTROY; if (send_event(fd, &event)) goto out_close; usleep(300000); if (!started) goto out_close; puts("UHID device destroyed without userspace error"); close(fd); return battery_found ? 0 : 1;
out_close:close(fd); return 1; }