* [QUESTION] usb: usbfs SETINTERFACE during a runtime reset-resume
@ 2026-11-15 16:37 Cheng Yongkang
2026-11-15 20:54 ` Alan Stern
0 siblings, 1 reply; 3+ messages in thread
From: Cheng Yongkang @ 2026-11-15 16:37 UTC (permalink / raw)
To: Alan Stern; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel
Hi Alan,
I have a question about usbfs and runtime reset-resume. I am not sure
it is a bug, so I would like to ask before sending anything.
The kerneldoc of usb_reset_and_verify_device() says:
Locking exception: This routine may also be called from within an
autoresume handler. Such usage won't conflict with other tasks
holding the device lock because these tasks should always call
usb_autopm_resume_device(), thereby preventing any unwanted
autoresume.
(usb_autopm_resume_device() no longer exists; I assume
usb_autoresume_device() is meant.)
proc_setintf() runs under the device lock but takes no PM reference.
After USBDEVFS_ALLOW_SUSPEND, the device can be runtime suspended
while the file is open. If it then resumes through reset-resume,
finish_port_resume() sets USB_STATE_CONFIGURED before
usb_reset_and_verify_device() runs its restore loop. A SETINTERFACE
issued at that moment passes checkintf() and calls usb_set_interface()
while the restore loop is still reading intf->cur_altsetting and
calling usb_set_interface() to put the interfaces back into their old
altsettings.
On v7.3, with dummy_hcd and a gadget that forces reset-resume, I saw
1097 SETINTERFACE calls complete inside the resume window over ten
rounds, all with -EPROTO. There were no KASAN or lockdep reports, and
I did not see any corrupted state afterwards. I know 7794f486ed0b says
that I/O to a suspended device fails with an error code, so -EPROTO by
itself may be expected.
My questions are:
1. Is usbfs expected to follow the assumption in that comment, so
that proc_setintf() should hold a PM reference while it changes the
altsetting?
2. Or is it up to the program to call USBDEVFS_FORBID_SUSPEND or
USBDEVFS_WAIT_FOR_RESUME first, so that this overlap with the
restore loop is acceptable?
3. If it is the first, would taking usb_autoresume_device() around
checkintf() and usb_set_interface() be the right fix? In my test
the ioctl then waited until the resume finished and returned 0,
which changes the current behavior of failing with an error.
Thanks,
Cheng Yongkang
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [QUESTION] usb: usbfs SETINTERFACE during a runtime reset-resume
2026-11-15 16:37 [QUESTION] usb: usbfs SETINTERFACE during a runtime reset-resume Cheng Yongkang
@ 2026-11-15 20:54 ` Alan Stern
2026-11-16 6:05 ` [PATCH] usb: usbfs: reject SETINTERFACE while suspend is allowed Cheng Yongkang
0 siblings, 1 reply; 3+ messages in thread
From: Alan Stern @ 2026-11-15 20:54 UTC (permalink / raw)
To: Cheng Yongkang; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel
On Sun, Nov 15, 2026 at 08:37:09AM -0800, Cheng Yongkang wrote:
> Hi Alan,
>
> I have a question about usbfs and runtime reset-resume. I am not sure
> it is a bug, so I would like to ask before sending anything.
>
> The kerneldoc of usb_reset_and_verify_device() says:
>
> Locking exception: This routine may also be called from within an
> autoresume handler. Such usage won't conflict with other tasks
> holding the device lock because these tasks should always call
> usb_autopm_resume_device(), thereby preventing any unwanted
> autoresume.
>
> (usb_autopm_resume_device() no longer exists; I assume
> usb_autoresume_device() is meant.)
Yes.
> proc_setintf() runs under the device lock but takes no PM reference.
> After USBDEVFS_ALLOW_SUSPEND, the device can be runtime suspended
> while the file is open. If it then resumes through reset-resume,
More to the point, if the resume handler in a driver bound to another
interface in the same device calls usb_reset_device().
> finish_port_resume() sets USB_STATE_CONFIGURED before
> usb_reset_and_verify_device() runs its restore loop. A SETINTERFACE
> issued at that moment passes checkintf() and calls usb_set_interface()
> while the restore loop is still reading intf->cur_altsetting and
> calling usb_set_interface() to put the interfaces back into their old
> altsettings.
>
> On v7.3, with dummy_hcd and a gadget that forces reset-resume, I saw
> 1097 SETINTERFACE calls complete inside the resume window over ten
> rounds, all with -EPROTO. There were no KASAN or lockdep reports, and
> I did not see any corrupted state afterwards. I know 7794f486ed0b says
> that I/O to a suspended device fails with an error code, so -EPROTO by
> itself may be expected.
>
> My questions are:
>
> 1. Is usbfs expected to follow the assumption in that comment, so
> that proc_setintf() should hold a PM reference while it changes the
> altsetting?
In theory, it should. On the other hand, a bunch of the other proc_*
routines in usbfs don't hold a PM reference while trying to communicate
with the device and owning the lock. (Of course, they also don't try to
change any interface altsettings.)
I suspect a better approach would be to have the routine fail
immediately if the ps->suspend_allowed flag is set. The kernel
shouldn't be obligated to resume the device on the user's behalf if the
user tries to do something at the wrong time.
The main thing that concerns me is the possibility of contradictory
usb_set_interface() calls going on concurrently. That is best avoided,
even if it doesn't end up crashing the kernel.
> 2. Or is it up to the program to call USBDEVFS_FORBID_SUSPEND or
> USBDEVFS_WAIT_FOR_RESUME first, so that this overlap with the
> restore loop is acceptable?
Well, yes -- user programs should be aware that communicating with the
device is likely to fail if it is attempted while the device may be
suspended.
> 3. If it is the first, would taking usb_autoresume_device() around
> checkintf() and usb_set_interface() be the right fix? In my test
> the ioctl then waited until the resume finished and returned 0,
> which changes the current behavior of failing with an error.
If we go that way then yes, this would be the right approach. (Except
that I don't see why the autoresume shouldn't occur until after
checkintf() has run.)
Sorry to be a little wishy-washy about this. The whole topic of how
some of the more esoteric core USB features should interact with usbfs
is not very well defined.
Alan Stern
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] usb: usbfs: reject SETINTERFACE while suspend is allowed
2026-11-15 20:54 ` Alan Stern
@ 2026-11-16 6:05 ` Cheng Yongkang
0 siblings, 0 replies; 3+ messages in thread
From: Cheng Yongkang @ 2026-11-16 6:05 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: Alan Stern, linux-usb, linux-kernel
After USBDEVFS_ALLOW_SUSPEND, a usbfs file no longer holds a runtime PM
reference and the device can be runtime suspended. When it is resumed
through reset-resume, finish_port_resume() sets USB_STATE_CONFIGURED
before usb_reset_and_verify_device() restores the altsettings. The
restore loop also runs when a driver bound to another interface calls
usb_reset_device() from its resume handler. The resume path does not
take the device lock, so a SETINTERFACE issued in that window passes
checkintf() and its usb_set_interface() runs concurrently with the
usb_set_interface() calls of the restore loop.
Fail SETINTERFACE with -EHOSTUNREACH while suspend is allowed. A
program has to call USBDEVFS_FORBID_SUSPEND or USBDEVFS_WAIT_FOR_RESUME
first, which resumes the device and keeps it from suspending until the
file allows suspend again.
Fixes: 7794f486ed0b ("usbfs: Add ioctls for runtime power management")
Suggested-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
I used -EHOSTUNREACH, the same error checkintf() returns for a device
that is not configured. Please tell me if another error is preferred.
drivers/usb/core/devio.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 101cb9425480..6463ad2b1fc5 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -1533,6 +1533,8 @@ static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
if (copy_from_user(&setintf, arg, sizeof(setintf)))
return -EFAULT;
+ if (ps->suspend_allowed)
+ return -EHOSTUNREACH;
ret = checkintf(ps, setintf.interface);
if (ret)
return ret;
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-16 6:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 16:37 [QUESTION] usb: usbfs SETINTERFACE during a runtime reset-resume Cheng Yongkang
2026-11-15 20:54 ` Alan Stern
2026-11-16 6:05 ` [PATCH] usb: usbfs: reject SETINTERFACE while suspend is allowed Cheng Yongkang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®