Re: [PATCH] HID: usbhid: do not sleep when opening device

From: Guenter Roeck
Date: Fri May 29 2020 - 21:23:09 EST


On Fri, May 29, 2020 at 6:09 PM Dmitry Torokhov
<dmitry.torokhov@xxxxxxxxx> wrote:
>
> On Fri, May 29, 2020 at 05:48:26PM -0700, Guenter Roeck wrote:
> > On Fri, May 29, 2020 at 4:50 PM Nicolas Boichat <drinkcat@xxxxxxxxxxxx> wrote:
> > >
> > > On Sat, May 30, 2020 at 3:59 AM Dmitry Torokhov
> > > <dmitry.torokhov@xxxxxxxxx> wrote:
> > > >
> > > > usbhid tries to give the device 50 milliseconds to drain its queues
> > > > when opening the device, but does it naively by simply sleeping in open
> > > > handler, which slows down device probing (and thus may affect overall
> > > > boot time).
> > > >
> > > > However we do not need to sleep as we can instead mark a point of time
> > > > in the future when we should start processing the events.
> > > >
> > > > Reported-by: Nicolas Boichat <drinkcat@xxxxxxxxxxxx>
> > > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
> > > > ---
> > > > drivers/hid/usbhid/hid-core.c | 27 +++++++++++++++------------
> > > > drivers/hid/usbhid/usbhid.h | 1 +
> > > > 2 files changed, 16 insertions(+), 12 deletions(-)
> > > >
> > > > diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c
> > > > index c7bc9db5b192..e69992e945b2 100644
> > > > --- a/drivers/hid/usbhid/hid-core.c
> > > > +++ b/drivers/hid/usbhid/hid-core.c
> > > > @@ -95,6 +95,19 @@ static int hid_start_in(struct hid_device *hid)
> > > > set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
> > > > } else {
> > > > clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
> > > > +
> > > > + if (test_and_clear_bit(HID_RESUME_RUNNING,
> > > > + &usbhid->iofl)) {
> > > > + /*
> > > > + * In case events are generated while nobody was
> > > > + * listening, some are released when the device
> > > > + * is re-opened. Wait 50 msec for the queue to
> > > > + * empty before allowing events to go through
> > > > + * hid.
> > > > + */
> > > > + usbhid->input_start_time = jiffies +
> > > > + msecs_to_jiffies(50);
> > > > + }
> > > > }
> > > > }
> > > > spin_unlock_irqrestore(&usbhid->lock, flags);
> > > > @@ -280,7 +293,8 @@ static void hid_irq_in(struct urb *urb)
> > > > if (!test_bit(HID_OPENED, &usbhid->iofl))
> > > > break;
> > > > usbhid_mark_busy(usbhid);
> > > > - if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) {
> > > > + if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl) &&
> > > > + time_after(jiffies, usbhid->input_start_time)) {
> > >
> > > Are we worried about jiffies overflowing (32-bit@1000Hz is "only" 49.7 days...)
> > >
> >
> > time_after() is overflow-safe. That is why it is used and jiffies is
> > not compared directly.
>
> Well, it is overflow safe, but still can not measure more than 50 days,
> so if you have a device open for 50+ days there will be a 50msec gap
> where it may lose events.
>

Or you could explicitly use 64-bit jiffies.

Guenter

> I guess we can switch to ktime(). A bit more expensive on 32 bits, but
> in reality I do not think anyone would care.
>
> Thanks.
>
> --
> Dmitry