Re: [PATCH 1/2] usbip: stub: reference count struct stub_device
From: Michael Grzeschik
Date: Wed Nov 18 2026 - 05:35:31 EST
On 2026-11-17 12:15 AM, Shuah Khan wrote:
On 11/11/26 04:34, Michael Grzeschik wrote:
A stub_device is freed by stub_disconnect() right after usbip_stop_eh()
reported that the event handler is done with it. The rx and tx threads
and the event handler can still use it at that point:
- The event handler clears an event bit after it ran the handler for
it. A SHUTDOWN requested while a previous shutdown is running is lost,
so threads started after that shutdown are not stopped before the
stub_device is freed.
- An event queued for the device while another one is handled leaves an
entry in the event list. Once the event bits are clear,
usbip_stop_eh() returns, the stub_device is freed, and the handler
then locks the sysfs_lock of the freed device.
- The stub_device no longer holds a reference to the usb_device, so a
thread outliving the disconnect touches a freed usb_device too.
This was seen exporting the serial port of an ST-Link from an
STM32MP1-DK2 on a Kirkwood (ARMv5) host. After repeated "endpoint 5 is
stalled" / usb_clear_halt cycles while a client opened and closed the
port, stub_rx crashed in dev_err() with the device pointer computed from
a NULL sdev->udev of freed memory:
Unable to handle kernel NULL pointer dereference at virtual address 000000b8 when read
PC is at dev_driver_string+0x0/0x38
LR is at __dev_printk+0x20/0x6c
Call trace:
dev_driver_string from __dev_printk+0x20/0x6c
__dev_printk from _dev_err+0x40/0x74
_dev_err from stub_rx_loop+0x5d8/0xd68 [usbip_host]
stub_rx_loop [usbip_host] from kthread+0x100/0x120
Reference count the stub_device instead of relying on the event handler
for its lifetime:
- the binding (probe until disconnect) holds the initial reference,
stub_disconnect() drops it instead of freeing the device
- the rx and tx thread hold one each while they run, taken before they
are woken and dropped when they end. If kthread_stop() reports that a
thread never ran, stub_shutdown_connection() drops it for the thread.
- the event handler holds one per queued event until it is completely
done with the device, through new optional get/put event handler
operations, which vhci and vudc do not set
- the stub_device holds a reference to its usb_device again, dropped
with the last reference
Threads that miss their shutdown now end when the connection goes away
instead of using freed memory.
I really need a simpler change log that explains the problem and the fix.
Sorry, that I took the sloptrain here. As you see, the AI was suggesting
some complex kref mechanism, for a obvious use after free.
The main takeway of the issue here is:
When a stub device is unbound, stub_disconnect() asks the usbip event handler
to shut the connection down and frees the device as soon as usbip_stop_eh() returns.
But usbip_stop_eh() only checks whether the device’s event flags are cleared,
while event_handler() may still have a second event for the same device queued.
The handler then picks up that leftover event and runs stub_shutdown_connection()
on the already freed device, which crashes in dev_printk().
However, after taking the sashiko suggestions into account and understanding
the over all picture, I spin another patch with hopefully less complexity.
Adding reference handling seemed like the obvious choice here, but it should
also be possible with proper locking and some checks in the usbip_stop_eh().
Sorry for this again. Please ignore this thread and take Johns version of my 2/2.
Thanks,
Michael
Attachment:
OpenPGP_signature.asc
Description: OpenPGP digital signature