lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Fri, 29 May 2020 13:33:24 -0700
From:   Dmitry Torokhov <dmitry.torokhov@...il.com>
To:     Guenter Roeck <linux@...ck-us.net>
Cc:     Jiri Kosina <jikos@...nel.org>,
        Benjamin Tissoires <benjamin.tissoires@...hat.com>,
        groeck@...omium.org, Nicolas Boichat <drinkcat@...omium.org>,
        linux-usb@...r.kernel.org, linux-input@...r.kernel.org,
        linux-kernel@...r.kernel.org
Subject: Re: [PATCH] HID: usbhid: do not sleep when opening device

On Fri, May 29, 2020 at 01:14:24PM -0700, Guenter Roeck wrote:
> On Fri, May 29, 2020 at 12:59:51PM -0700, 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@...omium.org>
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@...il.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)) {
> >  			hid_input_report(urb->context, HID_INPUT_REPORT,
> >  					 urb->transfer_buffer,
> >  					 urb->actual_length, 1);
> > @@ -714,17 +728,6 @@ static int usbhid_open(struct hid_device *hid)
> >  	}
> >  
> >  	usb_autopm_put_interface(usbhid->intf);
> > -
> > -	/*
> > -	 * 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.
> > -	 */
> > -	if (res == 0)
> > -		msleep(50);
> > -
> Can you just set usbhid->input_start_time here ?
> 	if (res == 0)
> 		usbhid->input_start_time = jiffies + msecs_to_jiffies(50);
> 	clear_bit(HID_RESUME_RUNNING, &usbhid->iofl);
> 
> Then you might not need the added code in hid_start_in().

That was my first version, but if hid_start_in() fails we start a timer
and try to retry the IO (and the "res" in 0 in this case). And we want
to mark the time only after we successfully submitted the interrupt URB,
that is why the code is in hid_start_in().

Thanks.

-- 
Dmitry

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ