Re: [PATCH v2] HID: sensor-hub: Avoid holding spinlock across child iteration

From: Srinivas Pandruvada

Date: Fri Nov 20 2026 - 10:12:13 EST


+Lixu

On Fri, 2026-11-20 at 21:50 +0800, Zheng Shuhao wrote:
> sensor_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.

None of the above tested devices are HID SENSOR HUB related, so need
some tests.
Hi Lixu,

Please give a try.

Thanks,
Srinivas

>
> Fixes: 8a5b38c3fd70 ("HID: hid-sensor-hub: don't use stale platform-
> data on remove")
> Reported-by: syzbot+fb951970a334aaff00ba@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=fb951970a334aaff00ba
> Signed-off-by: Zheng Shuhao <viennaoffh@xxxxxxxxx>
> ---
> 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(-)
>
> diff --git a/drivers/hid/hid-sensor-hub.c b/drivers/hid/hid-sensor-
> hub.c
> index 6470a290ebfc5..4c447f62480b5 100644
> --- a/drivers/hid/hid-sensor-hub.c
> +++ b/drivers/hid/hid-sensor-hub.c
> @@ -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);