* [PATCH] driver core: avoid klist_remove() on unattached knode_driver @ 2026-08-20 5:55 Nguyen Quang Le Kien 2026-08-20 6:05 ` [PATCH v2] " Nguyen Quang Le Kien 0 siblings, 1 reply; 10+ messages in thread From: Nguyen Quang Le Kien @ 2026-08-20 5:55 UTC (permalink / raw) To: gregkh, rafael, dakr Cc: driver-core, linux-usb, linux-kernel, Nguyen Quang Le Kien, syzbot+87188222c77c0dbbdb4d usb_driver_claim_interface() sets dev->driver directly and skips device_bind_driver() when the interface is not yet registered, so the device can reach teardown with dev->driver set but knode_driver never added to the driver's klist_devices. __device_release_driver() then unconditionally calls klist_remove() on the unattached node, which dereferences a NULL klist pointer in klist_put() and crashes. Guard the klist_remove() with klist_node_attached(), mirroring the existing check in bus_remove_device() for knode_bus. Reported-by: syzbot+87188222c77c0dbbdb4d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=87188222c77c0dbbdb4d Signed-off-by: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com> --- drivers/base/dd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 60c005223..14752a5e5 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 (klist_node_attached(&dev->p->knode_driver)) + klist_remove(&dev->p->knode_driver); device_pm_check_callbacks(dev); bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER); -- 2.34.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v2] driver core: avoid klist_remove() on unattached knode_driver 2026-08-20 5:55 [PATCH] driver core: avoid klist_remove() on unattached knode_driver Nguyen Quang Le Kien @ 2026-08-20 6:05 ` Nguyen Quang Le Kien 2026-08-20 6:40 ` Greg KH 0 siblings, 1 reply; 10+ messages in thread From: Nguyen Quang Le Kien @ 2026-08-20 6:05 UTC (permalink / raw) To: gregkh, rafael, dakr Cc: driver-core, linux-usb, linux-kernel, Nguyen Quang Le Kien, syzbot+87188222c77c0dbbdb4d usb_driver_claim_interface() sets dev->driver directly and skips device_bind_driver() when the interface is not yet registered, so the device can reach teardown with dev->driver set but knode_driver never added to the driver's klist_devices. __device_release_driver() then unconditionally calls klist_remove() on the unattached node, which dereferences a NULL klist pointer in klist_put() and crashes. Only remove the node if the device is actually bound, mirroring the check device_is_bound() already provides for the driver core. This matches the existing guard on knode_bus in bus_remove_device(). Reported-by: syzbot+87188222c77c0dbbdb4d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=87188222c77c0dbbdb4d Signed-off-by: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com> --- drivers/base/dd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); device_pm_check_callbacks(dev); bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER); -- 2.34.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v2] driver core: avoid klist_remove() on unattached knode_driver 2026-08-20 6:05 ` [PATCH v2] " Nguyen Quang Le Kien @ 2026-08-20 6:40 ` Greg KH 2026-08-20 6:56 ` Nguyen Quang Le Kien 2026-08-20 7:45 ` [PATCH v3] " Nguyen Quang Le Kien 0 siblings, 2 replies; 10+ messages in thread From: Greg KH @ 2026-08-20 6:40 UTC (permalink / raw) To: Nguyen Quang Le Kien, eadavis Cc: rafael, dakr, driver-core, linux-usb, linux-kernel, syzbot+87188222c77c0dbbdb4d On Thu, Aug 20, 2026 at 02:05:23PM +0800, Nguyen Quang Le Kien wrote: > usb_driver_claim_interface() sets dev->driver directly and skips > device_bind_driver() when the interface is not yet registered, so the > device can reach teardown with dev->driver set but knode_driver never > added to the driver's klist_devices. __device_release_driver() then > unconditionally calls klist_remove() on the unattached node, which > dereferences a NULL klist pointer in klist_put() and crashes. > > Only remove the node if the device is actually bound, mirroring the > check device_is_bound() already provides for the driver core. This > matches the existing guard on knode_bus in bus_remove_device(). > > Reported-by: syzbot+87188222c77c0dbbdb4d@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=87188222c77c0dbbdb4d > Signed-off-by: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com> > --- > drivers/base/dd.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) What changed from v1? And why did you send the same patch as Edward Adam Davis <eadavis@qq.com> just did: https://lore.kernel.org/r/tencent_0B76C5676E3DA13640962637444B28743709@qq.com What is suddenly causing people to care about syzbot bugs for USB? thanks, greg k-h ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2] driver core: avoid klist_remove() on unattached knode_driver 2026-08-20 6:40 ` Greg KH @ 2026-08-20 6:56 ` Nguyen Quang Le Kien 2026-08-20 7:05 ` Greg Kroah-Hartman 2026-08-20 7:45 ` [PATCH v3] " Nguyen Quang Le Kien 1 sibling, 1 reply; 10+ messages in thread From: Nguyen Quang Le Kien @ 2026-08-20 6:56 UTC (permalink / raw) To: Greg Kroah-Hartman Cc: linux-kernel, linux-usb, driver-core, Nguyen Quang Le Kien Hi Greg, v1 used klist_node_attached(&dev->p->knode_driver). v1 passed the syzbot test, but after reviewing the code again I realized klist_node_attached() isn't really the right check at that level -- it's a raw klist API. device_is_bound() is what driver core uses to ask whether a device is bound, and it also handles the NULL dev->p case. So I switched to that in v2. Nothing else changed. As for syzbot -- it's just where I find kernel bugs, and it can test my patches for me. That's all. Thanks, Kien ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v2] driver core: avoid klist_remove() on unattached knode_driver 2026-08-20 6:56 ` Nguyen Quang Le Kien @ 2026-08-20 7:05 ` Greg Kroah-Hartman 0 siblings, 0 replies; 10+ messages in thread From: Greg Kroah-Hartman @ 2026-08-20 7:05 UTC (permalink / raw) To: Nguyen Quang Le Kien; +Cc: linux-kernel, linux-usb, driver-core On Thu, Aug 20, 2026 at 02:56:06PM +0800, Nguyen Quang Le Kien wrote: > Hi Greg, Please provide email context when responding, some of us get 1000+ emails a day. > v1 used klist_node_attached(&dev->p->knode_driver). v1 passed the syzbot > test, but after reviewing the code again I realized klist_node_attached() > isn't really the right check at that level -- it's a raw klist API. Then please document this in the proper place, for v2, as required. thanks, greg k-h ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3] driver core: avoid klist_remove() on unattached knode_driver 2026-08-20 6:40 ` Greg KH 2026-08-20 6:56 ` Nguyen Quang Le Kien @ 2026-08-20 7:45 ` Nguyen Quang Le Kien 2026-08-20 8:23 ` Greg Kroah-Hartman 1 sibling, 1 reply; 10+ messages in thread From: Nguyen Quang Le Kien @ 2026-08-20 7:45 UTC (permalink / raw) To: Greg Kroah-Hartman Cc: linux-kernel, linux-usb, driver-core, syzbot+87188222c77c0dbbdb4d, Nguyen Quang Le Kien usb_driver_claim_interface() sets dev->driver directly and skips device_bind_driver() when the interface is not yet registered, so the device can reach teardown with dev->driver set but knode_driver never added to the driver's klist_devices. __device_release_driver() then unconditionally calls klist_remove() on the unattached node, which dereferences a NULL klist pointer in klist_put() and crashes. Only remove the node if the device is actually bound. Use device_is_bound() rather than klist_node_attached() directly: the latter is a raw klist API and does not NULL-check dev->p, while device_is_bound() is the standard bound-state check used throughout driver core (driver_bound(), __device_attach()). Fixes: 94e7b1c5ff20 ("[PATCH] Add a klist to struct device_driver for the devices bound to it.") Reported-by: syzbot+87188222c77c0dbbdb4d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=87188222c77c0dbbdb4d Signed-off-by: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com> --- drivers/base/dd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); device_pm_check_callbacks(dev); bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER); -- 2.34.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v3] driver core: avoid klist_remove() on unattached knode_driver 2026-08-20 7:45 ` [PATCH v3] " Nguyen Quang Le Kien @ 2026-08-20 8:23 ` Greg Kroah-Hartman [not found] ` <20260820084557.129908-1-khiemtranzo532001@gmail.com> 0 siblings, 1 reply; 10+ messages in thread From: Greg Kroah-Hartman @ 2026-08-20 8:23 UTC (permalink / raw) To: Nguyen Quang Le Kien Cc: linux-kernel, linux-usb, driver-core, syzbot+87188222c77c0dbbdb4d On Thu, Aug 20, 2026 at 03:45:38PM +0800, Nguyen Quang Le Kien wrote: > usb_driver_claim_interface() sets dev->driver directly and skips > device_bind_driver() when the interface is not yet registered, so the > device can reach teardown with dev->driver set but knode_driver never > added to the driver's klist_devices. __device_release_driver() then > unconditionally calls klist_remove() on the unattached node, which > dereferences a NULL klist pointer in klist_put() and crashes. > > Only remove the node if the device is actually bound. Use > device_is_bound() rather than klist_node_attached() directly: the > latter is a raw klist API and does not NULL-check dev->p, while > device_is_bound() is the standard bound-state check used throughout > driver core (driver_bound(), __device_attach()). > > Fixes: 94e7b1c5ff20 ("[PATCH] Add a klist to struct device_driver for the devices bound to it.") > Reported-by: syzbot+87188222c77c0dbbdb4d@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=87188222c77c0dbbdb4d > Signed-off-by: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com> > --- > drivers/base/dd.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > 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); > device_pm_check_callbacks(dev); > > bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER); > -- > 2.34.1 > > Hi, This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree. You are receiving this message because of the following common error(s) as indicated below: - This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/process/submitting-patches.rst for what needs to be done here to properly describe this. - You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this. - You have sent patches very very quickly, with no time to have others review them. Slow down, there is no rush here, and no deadlines. If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers. thanks, greg k-h's patch email bot ^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <20260820084557.129908-1-khiemtranzo532001@gmail.com>]
* 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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 0 siblings, 0 replies; 10+ 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] 10+ messages in thread
end of thread, other threads:[~2026-08-20 20:54 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-20 5:55 [PATCH] driver core: avoid klist_remove() on unattached knode_driver Nguyen Quang Le Kien
2026-08-20 6:05 ` [PATCH v2] " Nguyen Quang Le Kien
2026-08-20 6:40 ` Greg KH
2026-08-20 6:56 ` Nguyen Quang Le Kien
2026-08-20 7:05 ` Greg Kroah-Hartman
2026-08-20 7:45 ` [PATCH v3] " Nguyen Quang Le Kien
2026-08-20 8:23 ` 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox