linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mayuresh Kulkarni <mkulkarni@opensource.cirrus.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Oliver Neukum <oneukum@suse.com>,
	Greg KH <gregkh@linuxfoundation.org>,
	<patches@opensource.cirrus.com>, <linux-usb@vger.kernel.org>
Subject: Re: [PATCH] usb: core: devio: add ioctls for suspend and resume
Date: Fri, 21 Jun 2019 17:16:57 +0100	[thread overview]
Message-ID: <1561133817.11118.16.camel@opensource.cirrus.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.1906201145280.1512-100000@iolanthe.rowland.org>

On Thu, 2019-06-20 at 11:49 -0400, Alan Stern wrote:
> On Thu, 20 Jun 2019, Mayuresh Kulkarni wrote:
> 
> > 
> > On Wed, 2019-06-19 at 10:40 -0400, Alan Stern wrote:
> > 
> > > 
> > > The only solution I can think of is for the userspace program to
> > > first
> > > set the device's autosuspend delay to 0.  Then whenever the
> > > WAIT_FOR_RESUME ioctl returns -- even if it returns immediately --
> > > the
> > > program should assume the suspend is over or is not going to
> > > happen.  
> > > Then the program can run normally for a short while (10 seconds,
> > > perhaps) before making another attempt to suspend.
> > > 
> > Looks like usb_autosuspend_delay parameter is applicable to all USB
> > devices. Also, since it is a module parameter, it might be tuned on
> > a
> > particular platform (e.g.: from init.<vendor>.rc on Android).
> > So, I am not sure if it is good idea to rely on user-space to change
> > it
> > and restore it to original value when the USB device of interest is
> > detached.
> That's up to you.  There are lots of different ways to set the 
> autosuspend delay.  For example, you could create a udev rule that 
> would do it only for the devices your program handles.
> 
> > 
> > > 
> > > The only change I would make to the patch posted earlier is to
> > > have 
> > > proc_wait_for_resume() call usb_autoresume_device() and set 
> > > ps->suspend_requested = false before returning.
> > > 
> > > Mayuresh, how does that sound?
> > With the code-changes you send me (in response to the
> > patch), usb_autoresume_device() will be called when the waiter
> > in proc_wait_for_resume() will return and send some message to
> > "know"
> > why resume happened.
> > 
> > With above proposed change, proc_wait_for_resume() will return
> > with usb_autoresume_device() and suspend_requested = false. So when
> > the
> > user-space will send some message to "know" resume reason, the
> > checks in
> > ioctl() handler will be skipped.
> > 
> > (Apologies if above is obvious, but still wanted to comment so that
> > we
> > are on same page).
> > 
> > With that said, I think there would be an issue with "host-wake"
> > case as
> > below - the sequence of operations are:
> > - suspend ioctl called: assume actual bus suspend happens
> > - wait-for-resume ioctl called
> > - after a while user-space wants to send a message (due to end user
> > interaction for example)
> > 
> > Here the ioctl() will do usb_autoresume_device()
> > since suspend_requested
> > = true. This will end up calling usbfs_notify_resume() which will
> > release waiter in proc_wait_for_resume(). And due to above proposed
> > change, it will end up calling usb_autoresume_device() again.
> > 
> > As a result, suspend will not happen for next suspend ioctl call.
> Obviously the code would need to be more careful.  It would call 
> usb_autoresume_device() only if ps->suspend_requested was true.
> 
> Alan Stern
> 
> > 
> > So, looks like the original proposed change seems better here. On
> > the
> > side note, I am still in process of verifying the code changes.

Hi Alan,

With the suggested modification (of having suspend/resume of usbfs at
device level instead of interface level), looks like I am seeing a
deadlock described as below -

Pre-condition: USB device is connected but suspended before running test
program.

1. The test program calls open(/dev/bus/usb/...).
2. This ends up calling usbdev_open().
3. usbdev_open() takes the lock and calls usb_autoresume_device().
4. usb_autoresume_device() calls pm_runtime_get_sync(). Due to _sync
version, this call will return after calling the resume call-back of
driver (please correct me if wrong).
5. This ends up calling generic_resume() which
calls usbfs_notify_resume().
6. Now usbfs_notify_resume() also wants the same lock that usbdev_open()
in (3) has already taken.

My observation so far is - when the USB device is connected for first
time, Android's USB manager server is able to open the device and reads
its descriptors using usbfs. But the test is not able to. The change is
auto-suspend in between device connect and run of test program.

I am still analysing the situation here to see if pre-condition above
really makes difference or not. So please take this update with pinch of
salt. However, still I wanted send this update and get a quick review to
ensure I am not wandering in weeds.

Thanks,

  reply	other threads:[~2019-06-21 16:17 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-10 10:01 [PATCH] usb: core: devio: add ioctls for suspend and resume Mayuresh Kulkarni
2019-06-05  9:41 ` Greg KH
2019-06-05 21:02   ` Alan Stern
2019-06-13 14:00     ` Mayuresh Kulkarni
2019-06-13 20:54       ` Alan Stern
2019-06-17 11:38     ` Mayuresh Kulkarni
2019-06-17 15:55       ` Alan Stern
2019-06-18 14:00         ` Mayuresh Kulkarni
2019-06-18 15:50           ` Alan Stern
2019-06-19  9:19             ` Oliver Neukum
2019-06-19 14:40               ` Alan Stern
2019-06-19 15:12                 ` Oliver Neukum
2019-06-19 16:07                   ` Alan Stern
2019-06-20 15:11                 ` Mayuresh Kulkarni
2019-06-20 15:49                   ` Alan Stern
2019-06-21 16:16                     ` Mayuresh Kulkarni [this message]
2019-06-21 19:28                       ` Alan Stern
2019-06-24 16:02                         ` Mayuresh Kulkarni
2019-06-24 17:48                           ` Alan Stern
2019-06-25 10:41                             ` Mayuresh Kulkarni
2019-06-25 14:08                               ` Alan Stern
2019-06-26  7:42                                 ` Oliver Neukum
2019-06-26 14:35                                   ` Alan Stern
2019-06-26 14:15                                 ` Mayuresh Kulkarni
2019-06-26 15:07                                   ` Alan Stern
2019-06-27 13:20                                     ` Mayuresh Kulkarni
2019-06-27 13:52                                       ` Alan Stern
2019-07-01  9:18                                         ` Oliver Neukum
2019-07-01 14:17                                           ` Alan Stern
2019-07-02  9:21                                             ` Oliver Neukum
2019-07-02 14:29                                               ` Alan Stern
2019-07-03 14:44                         ` Mayuresh Kulkarni
2019-07-05 18:51                           ` [RFC] usbfs: Add ioctls for runtime " Alan Stern
2019-07-11  9:16                             ` Mayuresh Kulkarni
2019-07-11 14:20                               ` Alan Stern
2019-07-11 14:36                                 ` Greg KH
2019-07-25  9:10                             ` Mayuresh Kulkarni
2019-07-25  9:18                               ` Greg KH
2019-07-25 15:18                                 ` Alan Stern
2019-07-25 16:05                                   ` Greg KH
2019-06-20 14:34               ` [PATCH] usb: core: devio: add ioctls for " Mayuresh Kulkarni
2019-06-20 14:43                 ` Alan Stern
2019-06-13 13:32   ` Mayuresh Kulkarni

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=1561133817.11118.16.camel@opensource.cirrus.com \
    --to=mkulkarni@opensource.cirrus.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=oneukum@suse.com \
    --cc=patches@opensource.cirrus.com \
    --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;
as well as URLs for NNTP newsgroup(s).