usb: usbfs SETINTERFACE during a runtime reset-resume
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
Quoted 7 lines · Cheng Yongkang
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:
Quoted 8 lines
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 assumeusb_autoresume_device()is meant.)
Yes.
Quoted 3 lines
proc_setintf()runs under the device lock but takes no PM reference. AfterUSBDEVFS_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().
Quoted 14 lines
finish_port_resume()setsUSB_STATE_CONFIGUREDbeforeusb_reset_and_verify_device()runs its restore loop. A SETINTERFACE issued at that moment passescheckintf()and callsusb_set_interface()while the restore loop is still readingintf->cur_altsettingand callingusb_set_interface()to put the interfaces back into their old altsettings.On v7.3, with
dummy_hcdand 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 know7794f486ed0bsays that I/O to a suspended device fails with an error code, so-EPROTOby itself may be expected.
Quoted 5 lines
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.
Quoted 3 lines
2. Or is it up to the program to call
USBDEVFS_FORBID_SUSPENDorUSBDEVFS_WAIT_FOR_RESUMEfirst, 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.
Quoted 4 lines
3. If it is the first, would taking
usb_autoresume_device()aroundcheckintf()andusb_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
PATCH 1/1 usb: usbfs: reject SETINTERFACE while suspend is allowed
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.
---
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(+)
drivers/usb/core/devio.c +2 -0
@@ -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
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH] usb: usbfs: reject SETINTERFACE while suspend is allowed
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
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()setsUSB_STATE_CONFIGUREDbeforeusb_reset_and_verify_device()restores the altsettings. The restore loop also runs when a driver bound to another interface callsusb_reset_device()from its resume handler. The resume path does not take the device lock, so a SETINTERFACE issued in that window passescheckintf()and itsusb_set_interface()runs concurrently with theusb_set_interface()calls of the restore loop.
Quoted 8 lines
Fail SETINTERFACE with
-EHOSTUNREACHwhile suspend is allowed. A program has to callUSBDEVFS_FORBID_SUSPENDorUSBDEVFS_WAIT_FOR_RESUMEfirst, 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 <[email protected]>Signed-off-by Cheng Yongkang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH] usb: usbfs: reject SETINTERFACE while suspend is allowed
In reply to Cheng YongkangQuoted 15 lines · Cheng Yongkang
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()setsUSB_STATE_CONFIGUREDbeforeusb_reset_and_verify_device()restores the altsettings. The restore loop also runs when a driver bound to another interface callsusb_reset_device()from its resume handler. The resume path does not take the device lock, so a SETINTERFACE issued in that window passescheckintf()and itsusb_set_interface()runs concurrently with theusb_set_interface()calls of the restore loop.Fail SETINTERFACE with
-EHOSTUNREACHwhile suspend is allowed. A program has to callUSBDEVFS_FORBID_SUSPENDorUSBDEVFS_WAIT_FOR_RESUMEfirst, which resumes the device and keeps it from suspending until the file allows suspend again.
Quoted 6 lines
Fixes 7794f486ed0b ("usbfs: Add ioctls for runtime power management")Suggested-by Alan Stern <[email protected]>Signed-off-by Cheng Yongkang <[email protected]>---
I used-EHOSTUNREACH, the same errorcheckintf()returns for a device that is not configured. Please tell me if another error is preferred.
-EHOSTUNREACH is fine, as far as I know.
drivers/usb/core/devio.c static int proc_setintf(struct usb_dev_state *ps, void __user *arg) · line 1537
Quoted 13 lines
drivers/usb/core/devio.c | 2 ++ 1 file changed, 2 insertions(+)drivers/usb/core/devio.c +2
-0@@ -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 ;
How about putting this before the copy_from_user(), and adding a comment about avoiding possible races against an unlocked reset occurring during a device resume?
Alan Stern
Quoted 8 lines
drivers/usb/core/devio.c +0
-0ret =checkintf (ps, setintf.interface);if (ret)return ret;base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4Signature · 1 line
2.34.1
PATCH v2 1/1 usb: usbfs: reject SETINTERFACE while suspend is allowed
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.
---
Changes in v2:
- Move the check before copy_from_user() and add a comment (Alan).
v1: https://lore.kernel.org/linux-usb/[email protected]/
drivers/usb/core/devio.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/usb/core/devio.c +7 -0
@@ -1531,6 +1531,13 @@ static int proc_setintf(struct usb_dev_state *ps, void __user *arg)struct usbdevfs_setinterface setintf;int ret; +/* +* Avoid racing with a reset during device resume, which restores +* the altsettings without holding the device lock. The device can +* only be suspended while suspend is allowed. +*/ +if (ps->suspend_allowed) +return -EHOSTUNREACH ;if (copy_from_user (&setintf, arg,sizeof (setintf)))return -EFAULT ; ret =checkintf (ps, setintf.interface);
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2] usb: usbfs: reject SETINTERFACE while suspend is allowed
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
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()setsUSB_STATE_CONFIGUREDbeforeusb_reset_and_verify_device()restores the altsettings. The restore loop also runs when a driver bound to another interface callsusb_reset_device()from its resume handler. The resume path does not take the device lock, so a SETINTERFACE issued in that window passescheckintf()and itsusb_set_interface()runs concurrently with theusb_set_interface()calls of the restore loop.
Quoted 8 lines
Fail SETINTERFACE with
-EHOSTUNREACHwhile suspend is allowed. A program has to callUSBDEVFS_FORBID_SUSPENDorUSBDEVFS_WAIT_FOR_RESUMEfirst, 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 <[email protected]>Signed-off-by Cheng Yongkang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2] usb: usbfs: reject SETINTERFACE while suspend is allowed
In reply to Cheng YongkangQuoted 15 lines · Cheng Yongkang
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()setsUSB_STATE_CONFIGUREDbeforeusb_reset_and_verify_device()restores the altsettings. The restore loop also runs when a driver bound to another interface callsusb_reset_device()from its resume handler. The resume path does not take the device lock, so a SETINTERFACE issued in that window passescheckintf()and itsusb_set_interface()runs concurrently with theusb_set_interface()calls of the restore loop.Fail SETINTERFACE with
-EHOSTUNREACHwhile suspend is allowed. A program has to callUSBDEVFS_FORBID_SUSPENDorUSBDEVFS_WAIT_FOR_RESUMEfirst, which resumes the device and keeps it from suspending until the file allows suspend again.
Quoted 4 lines
Fixes 7794f486ed0b ("usbfs: Add ioctls for runtime power management")Suggested-by Alan Stern <[email protected]>Signed-off-by Cheng Yongkang <[email protected]>---
Quoted 30 lines
Changes in v2:
- Move the check beforecopy_from_user()and add a comment (Alan).v1: https://lore.kernel.org/linux-usb/[email protected]/
drivers/usb/core/devio.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/usb/core/devio.c +7
-0@@ -1531,6 +1531,13 @@ static int proc_setintf(struct usb_dev_state *ps, void __user *arg)struct usbdevfs_setinterface setintf;int ret; +/* +* Avoid racing with a reset during device resume, which restores +* the altsettings without holding the device lock. The device can +* only be suspended while suspend is allowed. +*/ +if (ps->suspend_allowed) +return -EHOSTUNREACH ;if (copy_from_user (&setintf, arg,sizeof (setintf)))return -EFAULT ; ret =checkintf (ps, setintf.interface);base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4Signature · 1 line
2.34.1