The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH 2/2] USB: serial: fix driver deregistration order
Date: Fri, 21 Aug 2026 11:35:23 +0200	[thread overview]
Message-ID: <aogb2-XZXZHUprHe@hovoldconsulting.com> (raw)
In-Reply-To: <2b757e10-c39d-48e5-8240-5390053f6f99@rowland.harvard.edu>

On Thu, Aug 20, 2026 at 11:47:05AM -0400, Alan Stern wrote:
> On Thu, Aug 20, 2026 at 04:54:02PM +0200, Johan Hovold wrote:
> > USB serial driver modules register one driver for the USB bus and one or
> > more drivers for the ports on the USB serial bus.
> > 
> > When unloading a driver module, the USB driver must be deregistered
> > before the USB serial bus drivers so that I/O is stopped before
> > unbinding the ports to avoid use-after-free in completion handlers
> > accessing port data.
> > 
> > Note that the order does not matter currently in the registration error
> > path as the USB driver is not bound until after the USB serial drivers
> > have been registered.
> 
> But __usb_serial_register_drivers() does usb_register(udriver) _before_ 
> calling usb_serial_register() for the serial_drivers.  Not _after_, as 
> claimed here.

It registers the driver, but it doesn't set up the id table and
therefore will not bind to any interface until after the serial drivers
have also been registered (as an optimisation).
 
> And if the order in the registration error path does not matter, why 
> does the patch change it?

For symmetry with deregistration and for the unlikely event that there
is ever a later error path added (or if the optimisation mentioned above
is removed).

> Also, what about the failure mode described in 765e0ba62613?  Not to 
> mention that it seems odd to register a parent device driver after a 
> child device driver, rather than before.  Or to unregister them in the 
> reverse order.

There is generally no issue registering a driver for a child device
after a driver for a parent so that everything is in place when the
parent driver registers the child device.

In fact, that is sort of what is done today by not setting up the
id_table until after the serial drivers have been registered even if
things are complicated by how dynamic id support is implemented for USB
serial.

As I'm not changing the registration order I sort of skimmed over the
dynamic id issue addressed by commit 765e0ba62613 ("usb-serial: new API
for driver registration"). But I guess need to revisit that to make sure
I don't reintroduce a race during deregistration.
 
> If the point is to avoid I/O operations completing after the device 
> structure has been deallocated, why not rely on the serial-bus drivers 
> to stop all their I/O when they are unbound?

The problem is that there are two drivers involved for the USB interface
and USB serial port, respectively, both of which can do I/O and where
the parent USB driver may access the driver data of the child ports.

Tearing things down properly is a bit involved as you know (and as can
be seen from the first patch in the series). Everything is therefore
managed from the parent driver disconnect callback and individual ports
are, for example, no longer allowed to be unbound independently (cf.
commit fdb838efa31e ("USB: serial: suppress driver bind attributes")).

The only remaining corner case should be driver deregistration.

> If these questions don't make sense, remember that I haven't done 
> serious work on this subsystem for 15 years or so and blame it on that.  
> :-)

Heh. They make perfect sense. Thanks for taking a look.

Johan

  reply	other threads:[~2026-08-21  9:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-20 14:54 [PATCH 0/2] USB: serial: fix port tear down use-after-free Johan Hovold
2026-08-20 14:54 ` [PATCH 1/2] " Johan Hovold
2026-08-20 14:54 ` [PATCH 2/2] USB: serial: fix driver deregistration order Johan Hovold
2026-08-20 15:47   ` Alan Stern
2026-08-21  9:35     ` Johan Hovold [this message]
2026-08-20 15:08 ` [PATCH 0/2] USB: serial: fix port tear down use-after-free Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aogb2-XZXZHUprHe@hovoldconsulting.com \
    --to=johan@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox