Re: [PATCH v2 1/2] usb: gadget: u_serial: Publish the console only under port_lock
From: Fabricio Akio
Date: Tue Nov 24 2026 - 22:28:45 EST
On Mon, Nov 23, 2026 at 09:55:07AM +0100, Michał Mirosław wrote:
> Note that there is `if (port->console) return` just at the start of this
> function. If there is indeed a race here, that should be taken into
> account.
gserial_set_console() and gserial_free_line() hold ports[port_num].lock
across gs_console_init() and gs_console_exit(). The initial call from
gserial_alloc_line() during configfs function creation runs before the
function's console attribute is exposed. That makes the early check safe.
The race is later in gs_console_init(). After it sets port->console
and before it takes port_lock, gserial_connect() can take port_lock,
set port->port_usb and attach a request to the console. Init then
takes port_lock, sees port->port_usb set and attaches a second
request, overwriting the first. Moving the assignment under port_lock
closes that window.