* Re: [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver
[not found] ` <20260820084557.129908-1-khiemtranzo532001@gmail.com>
@ 2026-08-20 16:14 ` Danilo Krummrich
2026-08-20 17:22 ` Alan Stern
0 siblings, 1 reply; 6+ messages in thread
From: Danilo Krummrich @ 2026-08-20 16:14 UTC (permalink / raw)
To: Nguyen Quang Le Kien
Cc: gregkh, rafael, driver-core, linux-kernel,
syzbot+87188222c77c0dbbdb4d, stable, linux-usb
(Cc: linux-usb)
On Thu Aug 20, 2026 at 10:45 AM CEST, Nguyen Quang Le Kien wrote:
> Fixes: 94e7b1c5ff20 ("[PATCH] Add a klist to struct device_driver for the devices bound to it.")
This is not the correct commit to reference, this commit seems fine.
> diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> index 60c005223..4154b4499 100644
> --- a/drivers/base/dd.c
> +++ b/drivers/base/dd.c
> @@ -1354,7 +1354,8 @@ static void __device_release_driver(struct device *dev, struct device *parent)
> device_unbind_cleanup(dev);
> device_links_driver_cleanup(dev);
>
> - klist_remove(&dev->p->knode_driver);
> + if (device_is_bound(dev))
> + klist_remove(&dev->p->knode_driver);
This looks like band-aid for the underlying design tension in the USB core
(which we should address instead) and does not belong in the driver core.
It's not visible from the above diff, but with this patch the control flow
becomes:
if (dev->driver) {
if (device_is_bound(dev))
klist_remove(&dev->p->knode_driver);
...
}
but dev->driver already indicates that the device is bound to dev->driver in
this context.
The reason we "need" this check regardless is that usb_driver_claim_interface()
(ab)uses dev->driver to indicate that a certain USB driver claimed, or rather
reserved, this device.
There are two cases in usb_driver_claim_interface():
(1) The device to claim is already registered with the driver core, in which
case device_bind_driver(dev) is called and dev->driver is correctly set
during the bind attempt.
(2) The device to claim was not yet registered with the driver core. This can
happen when the USB interface the driver actually binds to is registered
(and hence probed) before the additional interface the driver wants to
claim is registered. The USB core sets dev->driver independent of the bind
state to indicate it has reserved the interface.
The first case is perfectly fine, but the issue with the second case is that now
dev->driver is semantically overloaded:
The USB core treats it as "dev is reserved for dev->driver" and the driver core
treats it as "dev is currently binding or bound to dev->driver", but that's not
actually the case yet, since device_bind_driver() hasn't been called yet.
IOW, dev->driver should only be set under the device lock before calling
device_bind_driver(), and, in case of failure, should be cleared after
device_bind_driver() with the device lock still held.
Besides the reported crash, another implication of this is that all other
functions from device_release_driver() are called as well, even though
device_bind_driver() was never called before, and there is no guarantee that
this does not cause other unexpected side effects already or in the future.
I think one solution could be to add a new claimed field to struct usb_interface
to indicate that the interface is reserved for a certain driver and make
usb_device_match() reject the device if ever probed otherwise.
Another solution (but that's a bit more work) would be to separate interface
registration from interface probing in usb_set_configuration(). Of course that
needs help from the driver core as well, but it would also get us rid of the
slightly odd situation that a driver may operate a claimed device already, even
though it is not yet registered with the driver core.
I'd suggest going for a claimed field in struct usb_interface first to fix the
immediate problem and then take it from there.
That said, I wonder if there's more to think about with the
usb_driver_claim_interface() / usb_driver_release_interface() API.
After having a brief look it seems that drivers have invented various different
approaches to protect against the case where userspace could write the claimed
interface's name to:
/sys/bus/usb/drivers/<driver>/unbind
I think this could be much cleaner if the driver core would support "claimed"
devices" natively. OTH, there's only ~20 drivers across USB and PnP though, so
probably not worth.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver
2026-08-20 16:14 ` [PATCH v4] " Danilo Krummrich
@ 2026-08-20 17:22 ` Alan Stern
2026-08-20 20:54 ` Danilo Krummrich
0 siblings, 1 reply; 6+ messages in thread
From: Alan Stern @ 2026-08-20 17:22 UTC (permalink / raw)
To: Danilo Krummrich
Cc: Nguyen Quang Le Kien, gregkh, rafael, driver-core, linux-kernel,
syzbot+87188222c77c0dbbdb4d, stable, linux-usb
On Thu, Aug 20, 2026 at 06:14:37PM +0200, Danilo Krummrich wrote:
> (Cc: linux-usb)
>
> On Thu Aug 20, 2026 at 10:45 AM CEST, Nguyen Quang Le Kien wrote:
> > Fixes: 94e7b1c5ff20 ("[PATCH] Add a klist to struct device_driver for the devices bound to it.")
>
> This is not the correct commit to reference, this commit seems fine.
>
> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> > index 60c005223..4154b4499 100644
> > --- a/drivers/base/dd.c
> > +++ b/drivers/base/dd.c
> > @@ -1354,7 +1354,8 @@ static void __device_release_driver(struct device *dev, struct device *parent)
> > device_unbind_cleanup(dev);
> > device_links_driver_cleanup(dev);
> >
> > - klist_remove(&dev->p->knode_driver);
> > + if (device_is_bound(dev))
> > + klist_remove(&dev->p->knode_driver);
>
> This looks like band-aid for the underlying design tension in the USB core
> (which we should address instead) and does not belong in the driver core.
>
> It's not visible from the above diff, but with this patch the control flow
> becomes:
>
> if (dev->driver) {
> if (device_is_bound(dev))
> klist_remove(&dev->p->knode_driver);
> ...
> }
>
> but dev->driver already indicates that the device is bound to dev->driver in
> this context.
>
> The reason we "need" this check regardless is that usb_driver_claim_interface()
> (ab)uses dev->driver to indicate that a certain USB driver claimed, or rather
> reserved, this device.
>
> There are two cases in usb_driver_claim_interface():
>
> (1) The device to claim is already registered with the driver core, in which
> case device_bind_driver(dev) is called and dev->driver is correctly set
> during the bind attempt.
>
> (2) The device to claim was not yet registered with the driver core. This can
> happen when the USB interface the driver actually binds to is registered
> (and hence probed) before the additional interface the driver wants to
> claim is registered. The USB core sets dev->driver independent of the bind
> state to indicate it has reserved the interface.
>
> The first case is perfectly fine, but the issue with the second case is that now
> dev->driver is semantically overloaded:
>
> The USB core treats it as "dev is reserved for dev->driver" and the driver core
> treats it as "dev is currently binding or bound to dev->driver", but that's not
> actually the case yet, since device_bind_driver() hasn't been called yet.
>
> IOW, dev->driver should only be set under the device lock before calling
> device_bind_driver(), and, in case of failure, should be cleared after
> device_bind_driver() with the device lock still held.
>
> Besides the reported crash, another implication of this is that all other
> functions from device_release_driver() are called as well, even though
> device_bind_driver() was never called before, and there is no guarantee that
> this does not cause other unexpected side effects already or in the future.
>
> I think one solution could be to add a new claimed field to struct usb_interface
> to indicate that the interface is reserved for a certain driver and make
> usb_device_match() reject the device if ever probed otherwise.
>
> Another solution (but that's a bit more work) would be to separate interface
> registration from interface probing in usb_set_configuration(). Of course that
> needs help from the driver core as well, but it would also get us rid of the
> slightly odd situation that a driver may operate a claimed device already, even
> though it is not yet registered with the driver core.
>
> I'd suggest going for a claimed field in struct usb_interface first to fix the
> immediate problem and then take it from there.
>
> That said, I wonder if there's more to think about with the
> usb_driver_claim_interface() / usb_driver_release_interface() API.
>
> After having a brief look it seems that drivers have invented various different
> approaches to protect against the case where userspace could write the claimed
> interface's name to:
>
> /sys/bus/usb/drivers/<driver>/unbind
>
> I think this could be much cleaner if the driver core would support "claimed"
> devices" natively. OTH, there's only ~20 drivers across USB and PnP though, so
> probably not worth.
I don't object to the idea of adding a "claimed" field to usb_interface.
However, isn't it true that the driver core has always supported the
idea of a driver being associated with a device before binding? In
particular, __device_attach() specifically checks for dev->driver being
already set. If it is, the match and probe steps are skipped.
Are you saying that this code path should be removed as well?
As it is, the driver core does sort of support "claimed devices".
Namely, a device is claimed (or probing) if dev->driver is set and
device_is_bound(dev) is false.
Alan Stern
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver
2026-08-20 17:22 ` Alan Stern
@ 2026-08-20 20:54 ` Danilo Krummrich
[not found] ` <CAEe5be5OjseCMxGQP=QMd1L-muOTDguaay_-21moS1+xRNNNyw@mail.gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Danilo Krummrich @ 2026-08-20 20:54 UTC (permalink / raw)
To: Alan Stern
Cc: Nguyen Quang Le Kien, gregkh, rafael, driver-core, linux-kernel,
syzbot+87188222c77c0dbbdb4d, stable, linux-usb
On Thu Aug 20, 2026 at 7:22 PM CEST, Alan Stern wrote:
> On Thu, Aug 20, 2026 at 06:14:37PM +0200, Danilo Krummrich wrote:
>> (Cc: linux-usb)
>>
>> On Thu Aug 20, 2026 at 10:45 AM CEST, Nguyen Quang Le Kien wrote:
>> > Fixes: 94e7b1c5ff20 ("[PATCH] Add a klist to struct device_driver for the devices bound to it.")
>>
>> This is not the correct commit to reference, this commit seems fine.
>>
>> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c
>> > index 60c005223..4154b4499 100644
>> > --- a/drivers/base/dd.c
>> > +++ b/drivers/base/dd.c
>> > @@ -1354,7 +1354,8 @@ static void __device_release_driver(struct device *dev, struct device *parent)
>> > device_unbind_cleanup(dev);
>> > device_links_driver_cleanup(dev);
>> >
>> > - klist_remove(&dev->p->knode_driver);
>> > + if (device_is_bound(dev))
>> > + klist_remove(&dev->p->knode_driver);
>>
>> This looks like band-aid for the underlying design tension in the USB core
>> (which we should address instead) and does not belong in the driver core.
>>
>> It's not visible from the above diff, but with this patch the control flow
>> becomes:
>>
>> if (dev->driver) {
>> if (device_is_bound(dev))
>> klist_remove(&dev->p->knode_driver);
>> ...
>> }
>>
>> but dev->driver already indicates that the device is bound to dev->driver in
>> this context.
>>
>> The reason we "need" this check regardless is that usb_driver_claim_interface()
>> (ab)uses dev->driver to indicate that a certain USB driver claimed, or rather
>> reserved, this device.
>>
>> There are two cases in usb_driver_claim_interface():
>>
>> (1) The device to claim is already registered with the driver core, in which
>> case device_bind_driver(dev) is called and dev->driver is correctly set
>> during the bind attempt.
>>
>> (2) The device to claim was not yet registered with the driver core. This can
>> happen when the USB interface the driver actually binds to is registered
>> (and hence probed) before the additional interface the driver wants to
>> claim is registered. The USB core sets dev->driver independent of the bind
>> state to indicate it has reserved the interface.
>>
>> The first case is perfectly fine, but the issue with the second case is that now
>> dev->driver is semantically overloaded:
>>
>> The USB core treats it as "dev is reserved for dev->driver" and the driver core
>> treats it as "dev is currently binding or bound to dev->driver", but that's not
>> actually the case yet, since device_bind_driver() hasn't been called yet.
>>
>> IOW, dev->driver should only be set under the device lock before calling
>> device_bind_driver(), and, in case of failure, should be cleared after
>> device_bind_driver() with the device lock still held.
>>
>> Besides the reported crash, another implication of this is that all other
>> functions from device_release_driver() are called as well, even though
>> device_bind_driver() was never called before, and there is no guarantee that
>> this does not cause other unexpected side effects already or in the future.
>>
>> I think one solution could be to add a new claimed field to struct usb_interface
>> to indicate that the interface is reserved for a certain driver and make
>> usb_device_match() reject the device if ever probed otherwise.
>>
>> Another solution (but that's a bit more work) would be to separate interface
>> registration from interface probing in usb_set_configuration(). Of course that
>> needs help from the driver core as well, but it would also get us rid of the
>> slightly odd situation that a driver may operate a claimed device already, even
>> though it is not yet registered with the driver core.
>>
>> I'd suggest going for a claimed field in struct usb_interface first to fix the
>> immediate problem and then take it from there.
>>
>> That said, I wonder if there's more to think about with the
>> usb_driver_claim_interface() / usb_driver_release_interface() API.
>>
>> After having a brief look it seems that drivers have invented various different
>> approaches to protect against the case where userspace could write the claimed
>> interface's name to:
>>
>> /sys/bus/usb/drivers/<driver>/unbind
>>
>> I think this could be much cleaner if the driver core would support "claimed"
>> devices" natively. OTH, there's only ~20 drivers across USB and PnP though, so
>> probably not worth.
>
> I don't object to the idea of adding a "claimed" field to usb_interface.
>
> However, isn't it true that the driver core has always supported the
> idea of a driver being associated with a device before binding? In
> particular, __device_attach() specifically checks for dev->driver being
> already set. If it is, the match and probe steps are skipped.
Ah, I also wanted to add a comment about this, but forgot about it, sorry.
This was added ~25 years ago to be able to hardwire a system device to a
specific driver, which less than a year after was replaced entirely by something
else and later converted back to a bus with proper match() logic. But the
dev->driver special case in __device_attach() was never removed.
So, I guess it is fair to say it was always there, but the driver core does not
really handle dev->driver being set "randomly" outside of the bind/unbind
lifecycle in general. It is expected that it is only set when the driver is
actually bound (or binding).
One example is __device_release_driver(), if device_bind_driver() (or
really_probe()) wasn't called, we should also not land within the
if (dev->driver) conditional of __device_release_driver().
Also note that usb_driver_claim_interface() is the only callsite in the kernel
that does set dev->driver without a subsequent device_bind_driver() while
holding the device lock.
(Well, actually, after a thorough check, there's w1, which hardwires the
w1_slave_driver, where it should actually use a trivial match() callback.)
In general, matching should be done through the match() callback.
> Are you saying that this code path should be removed as well?
Yes, I think we should remove it. It's a rather error prone special case that
can easily be handled with normal match() logic.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver
@ 2026-08-24 4:41 Nguyen Quang Le Kien
0 siblings, 0 replies; 6+ messages in thread
From: Nguyen Quang Le Kien @ 2026-08-24 4:41 UTC (permalink / raw)
To: linux-usb
Cc: Nguyen Quang Le Kien, Danilo Krummrich, Alan Stern, Greg KH,
Rafael J . Wysocki, driver-core, linux-kernel,
syzbot+87188222c77c0dbbdb4d, stable
In-Reply-To: <DKU2J83B6IX6.2CG1WOB66WC39@kernel.org>
References: <DKU2J83B6IX6.2CG1WOB66WC39@kernel.org>
On Fri, Aug 21, 2026 at 03:54:44AM +0200, Danilo Krummrich wrote:
> usb_driver_claim_interface() is the only callsite in the kernel that
> does set dev->driver without a subsequent device_bind_driver().
Small correction, I think there are a few more. usb_port
(drivers/usb/core/port.c:782) sets dev->driver before device_register(),
and usb_port_driver has no ->match and no ->probe, so it depends entirely
on the __device_attach() dev->driver path to get bound. ccwgroup
(drivers/s390/cio/ccwgroup.c:385) and pata_parport
(drivers/ata/pata_parport/pata_parport.c:515) do the same. So that path
doesn't look USB-claim-specific, and if we ever want to remove it,
usb_port would need a ->match() first.
For the root cause, what about this: in usb_driver_claim_interface(),
when the interface isn't registered yet, just don't set dev->driver --
record the claim on struct usb_interface instead. Then, once the
interface gets added in usb_set_configuration(), bind it explicitly with
device_bind_driver(). That skips ->probe, which is exactly what
usb_audio's claim-now-bind-later needs, and it also attaches
knode_driver so teardown is safe. The __device_attach() path can stay
for w1/ccwgroup/pata_parport.
Does that sound reasonable?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver
@ 2026-08-24 4:46 Nguyen Quang Le Kien
0 siblings, 0 replies; 6+ messages in thread
From: Nguyen Quang Le Kien @ 2026-08-24 4:46 UTC (permalink / raw)
To: linux-usb
Cc: Nguyen Quang Le Kien, Danilo Krummrich, Alan Stern, Greg KH,
Rafael J . Wysocki, driver-core, linux-kernel,
syzbot+87188222c77c0dbbdb4d, stable
In-Reply-To: <DKU2J83B6IX6.2CG1WOB66WC39@kernel.org>
References: <DKU2J83B6IX6.2CG1WOB66WC39@kernel.org>
On Fri, Aug 21, 2026 at 03:54:44AM +0200, Danilo Krummrich wrote:
> usb_driver_claim_interface() is the only callsite in the kernel that
> does set dev->driver without a subsequent device_bind_driver().
Small correction, I think there are a few more. usb_port
(drivers/usb/core/port.c:782) sets dev->driver before device_register(),
and usb_port_driver has no ->match and no ->probe, so it depends entirely
on the __device_attach() dev->driver path to get bound. ccwgroup
(drivers/s390/cio/ccwgroup.c:385) and pata_parport
(drivers/ata/pata_parport/pata_parport.c:515) do the same. So that path
doesn't look USB-claim-specific, and if we ever want to remove it,
usb_port would need a ->match() first.
For the root cause, what about this: in usb_driver_claim_interface(),
when the interface isn't registered yet, just don't set dev->driver --
record the claim on struct usb_interface instead. Then, once the
interface gets added in usb_set_configuration(), bind it explicitly with
device_bind_driver(). That skips ->probe, which is exactly what
usb_audio's claim-now-bind-later needs, and it also attaches
knode_driver so teardown is safe. The __device_attach() path can stay
for w1/ccwgroup/pata_parport.
Does that sound reasonable?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver
[not found] ` <CAEe5be5OjseCMxGQP=QMd1L-muOTDguaay_-21moS1+xRNNNyw@mail.gmail.com>
@ 2026-08-26 21:14 ` Danilo Krummrich
0 siblings, 0 replies; 6+ messages in thread
From: Danilo Krummrich @ 2026-08-26 21:14 UTC (permalink / raw)
To: Akihiko Kai
Cc: Alan Stern, gregkh, rafael, driver-core, linux-kernel,
syzbot+87188222c77c0dbbdb4d, stable, linux-usb
On Mon Aug 24, 2026 at 6:47 AM CEST, Akihiko Kai wrote:
> On Fri, Aug 21, 2026 at 03:54:44AM +0200, Danilo Krummrich wrote:
>> usb_driver_claim_interface() is the only callsite in the kernel that
>> does set dev->driver without a subsequent device_bind_driver().
>
> Small correction, I think there are a few more. usb_port
> (drivers/usb/core/port.c:782) sets dev->driver before device_register(),
> and usb_port_driver has no ->match and no ->probe, so it depends entirely
> on the __device_attach() dev->driver path to get bound. ccwgroup
> (drivers/s390/cio/ccwgroup.c:385) and pata_parport
> (drivers/ata/pata_parport/pata_parport.c:515) do the same. So that path
> doesn't look USB-claim-specific, and if we ever want to remove it,
> usb_port would need a ->match() first.
Good catch, I think those should be fixed as well.
I had a brief look at the usb_port one and it should be exactly the same case as
w1, which I already fixed [1]. Actually, it is even simpler, usb_device_match()
can just handle the usb_port_device_type, as the USB core already does for other
device types. I can send a patch for this later on.
[1] https://lore.kernel.org/driver-core/20260820225430.1847356-1-dakr@kernel.org/
> For the root cause, what about this: in usb_driver_claim_interface(),
> when the interface isn't registered yet, just don't set dev->driver --
> record the claim on struct usb_interface instead. Then, once the
> interface gets added in usb_set_configuration(), bind it explicitly with
> device_bind_driver(). That skips ->probe, which is exactly what
> usb_audio's claim-now-bind-later needs, and it also attaches
> knode_driver so teardown is safe. The __device_attach() path can stay
> for w1/ccwgroup/pata_parport.
>
> Does that sound reasonable?
I'd prefer to get rid of the dev->driver path in __device_attach(), I think
there's no reason not to use the common match() path instead.
So, for a fix that also goes into stable trees what you propose sounds
reasonable, but eventually we should use the match() infrastructure instead.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-08-26 21:14 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-24 4:46 [PATCH v4] driver core: avoid klist_remove() on unattached knode_driver Nguyen Quang Le Kien
-- strict thread matches above, loose matches on Subject: below --
2026-08-24 4:41 Nguyen Quang Le Kien
2026-08-20 8:23 [PATCH v3] " Greg Kroah-Hartman
[not found] ` <20260820084557.129908-1-khiemtranzo532001@gmail.com>
2026-08-20 16:14 ` [PATCH v4] " Danilo Krummrich
2026-08-20 17:22 ` Alan Stern
2026-08-20 20:54 ` Danilo Krummrich
[not found] ` <CAEe5be5OjseCMxGQP=QMd1L-muOTDguaay_-21moS1+xRNNNyw@mail.gmail.com>
2026-08-26 21:14 ` Danilo Krummrich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox