linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Jiri Kosina <jikos@kernel.org>
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Guenter Roeck <groeck@google.com>,
	Nicolas Boichat <drinkcat@chromium.org>,
	Guenter Roeck <groeck@chromium.org>,
	Linux USB Mailing List <linux-usb@vger.kernel.org>,
	"open list:HID CORE LAYER" <linux-input@vger.kernel.org>,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] HID: usbhid: do not sleep when opening device
Date: Tue, 2 Jun 2020 11:14:09 +0200	[thread overview]
Message-ID: <CAO-hwJLTrGs39OsxtY_r0He02Au64vg1x1_tzGjCQzyNzBCARw@mail.gmail.com> (raw)
In-Reply-To: <nycvar.YFH.7.76.2006011912150.13242@cbobk.fhfr.pm>

On Mon, Jun 1, 2020 at 7:13 PM Jiri Kosina <jikos@kernel.org> wrote:
>
> On Fri, 29 May 2020, Dmitry Torokhov 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@chromium.org>
> > > > > > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > > > > > > ---
> > > > > > >  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.
> >
> > Indeed.
> >
> > Jiri, Benjamin, do you have preference between jiffies64 and ktime_t? I
> > guess jiffies64 is a tiny bit less expensive.
>
> If I would be writing the code, I'd use ktime_t, because I personally like
> that abstraction more :) But either variant works for me.

I don't have a strong opinion on either variant. Feel free to use
whatever you like the most.

Cheers,
Benjamin

>
> Thanks!
>
> --
> Jiri Kosina
> SUSE Labs
>


      reply	other threads:[~2020-06-02  9:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-29 19:59 [PATCH] HID: usbhid: do not sleep when opening device Dmitry Torokhov
2020-05-29 20:14 ` Guenter Roeck
2020-05-29 20:33   ` Dmitry Torokhov
2020-05-29 20:44     ` Guenter Roeck
2020-05-29 23:50 ` Nicolas Boichat
2020-05-30  0:48   ` Guenter Roeck
2020-05-30  1:09     ` Dmitry Torokhov
2020-05-30  1:22       ` Guenter Roeck
2020-05-30  1:34         ` Dmitry Torokhov
2020-06-01 17:13           ` Jiri Kosina
2020-06-02  9:14             ` Benjamin Tissoires [this message]

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=CAO-hwJLTrGs39OsxtY_r0He02Au64vg1x1_tzGjCQzyNzBCARw@mail.gmail.com \
    --to=benjamin.tissoires@redhat.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=drinkcat@chromium.org \
    --cc=groeck@chromium.org \
    --cc=groeck@google.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /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).