Re: [PATCH v2] HID: sensor-hub: Avoid holding spinlock across child iteration
From: Zheng Shuhao
Date: Fri Nov 20 2026 - 12:15:15 EST
> In reality this can't happen as the hid_hw_close() and hid_hw_stop()
> 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.
> 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_write
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;
}