Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp990115ybm; Fri, 29 May 2020 17:51:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdV7y4VBl8H6SlHtGMx2xX9FmO5lkpwy7lZXPA83ccr855y+dl4iG4RqQRSUgoTNeSZDAn X-Received: by 2002:a50:e08c:: with SMTP id f12mr10995532edl.233.1590799886627; Fri, 29 May 2020 17:51:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590799886; cv=none; d=google.com; s=arc-20160816; b=LeF3YxVrVCBjhOiR0nKn9sOSv01nBk189MuJglIJWd2VjGNdRktbmlLbdiR5PM8xoo 4SSCHuPuY+mHBsEmKwDj5+1M5TTKXwZrJdyNYAxzMlUPbzE34NmZnoYBXW7ja5aenCrA oitQr2uJmaayKlyJwgmEKkTTrIsb8+hJTUACZ9C7lRM1ArtDZiLrCjsnKGMvpK5+4H// i6pWgkVMgZmA4pUFVFqC+xK8jxBzae0trlq6dRiaBsZa4JPAy1xqoiKgp5qkosbk2MOX XFLfQQUMPBXmZ9hA1IwZ3AmjqQpAN11R6R5ipoektkYvlaenD3yNDNa1XZK/RRwfKeQo KjNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=SnGa+sp06YGgurCULp59Dm5yJ72F3Q121M3j0Zokm2c=; b=ryg94v/qZlUqyu/W1Bcg58sCruGoxTMeOtsVNNE80XBSwcG6S4y42zHWqq1jx1V7Gh mbL7KCQrwtbxxFhgXnIRrdSM1tDPeMJCnof5R2NhyDuo6YB+hEHZ3D9hmBnWTKaeKrT4 IVadWAB9VQChcsmDEcQsCdnod/EMdeRNn7yOvelqs8/Ek20oiRVjPOeeqVg5ZYFoSWqS orKB19D0lAhsYgHoYeeyoWfosTm4q7bdG8nddTWaQg1UtY8DB9lxbhOgXF6pbU+I/mHw fpEeYowCLuT3m7g44hCWbTsI11TJzSs9c9533ybeiHokfVnObLCIX/S2wEilb2VQu32q /OWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="d/wcQy8e"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cm22si1798384edb.370.2020.05.29.17.51.03; Fri, 29 May 2020 17:51:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="d/wcQy8e"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728604AbgE3Ask (ORCPT + 99 others); Fri, 29 May 2020 20:48:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728449AbgE3Asj (ORCPT ); Fri, 29 May 2020 20:48:39 -0400 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC75CC08C5CA for ; Fri, 29 May 2020 17:48:39 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id n24so3826591ejd.0 for ; Fri, 29 May 2020 17:48:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SnGa+sp06YGgurCULp59Dm5yJ72F3Q121M3j0Zokm2c=; b=d/wcQy8elcjGFNAUc1MXBBI2fcayOPmE9V8XLfJsTJOqDasbkiEtu7xxWmrYwcBvXi oGfybuS74o+QWL/KgFLhwviOMp6+xWBLNWfAQoLE10/bbsgA6S2eVuMrdXUSijKdCp83 T5ckrCWcEaST8oZHFwjxIkK0tOW3XYgvfzdgChJOBkjjlUa58r3VNREsyNUA28TUA+hJ lrVOExkoWYhgTVW6l9LM0O8KizleAnyYv72I1mGIIvWQQ4Ki5ZdtieSpmhyOmfDZTIey 2NRgQpjjf58c5lwnLwPrkLMCbLSus1pm7PW6AAH/2PW1kq+NKcaAYZnl6R2TXFusg0+U 1chA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SnGa+sp06YGgurCULp59Dm5yJ72F3Q121M3j0Zokm2c=; b=Tizk6ZzFosd5nCQfbAv0JBisYVfH/nD0usD8CDZFiBTTo5Fnzdxd1NfqwzuLcJ6t/j SKJlemVuQgXtLfqGcQVuyjDkXhcw3sEg+SlfxrySm3ME5lDMRzQjBEzfA0r7M34AKTYZ EHRKMv4BoZe05VmuARhXe/pow4jXUKcLFQ1vecEsuMsQEpP+SS22Pm3ZM5jERo6Xjcn4 cKT0+BJAbz8ZsmPCjh4bvNPX/zePEELQzpcQuc/oniK7yQSfHL3GTi47uNu+vL+GM0IT QY5AQhtAAoQaqkfh1a0pv2ifIrVU8cghAeMT/3GLcBLiGLmzeDaQ1gsLG9NStePnqVcP 7wAQ== X-Gm-Message-State: AOAM532IWMzeNfGbo2La/dF22Sn0oTK+9gB5m/SFyauWFpoQ+WgBn4fI JzNL2xqnzbGGlD3UxTCz3HwxEv6vt286xGdZhvv+sQ== X-Received: by 2002:a17:906:dc2:: with SMTP id p2mr10002030eji.212.1590799717876; Fri, 29 May 2020 17:48:37 -0700 (PDT) MIME-Version: 1.0 References: <20200529195951.GA3767@dtor-ws> In-Reply-To: From: Guenter Roeck Date: Fri, 29 May 2020 17:48:26 -0700 Message-ID: Subject: Re: [PATCH] HID: usbhid: do not sleep when opening device To: Nicolas Boichat Cc: Dmitry Torokhov , Jiri Kosina , Benjamin Tissoires , Guenter Roeck , linux-usb@vger.kernel.org, "open list:HID CORE LAYER" , lkml Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 29, 2020 at 4:50 PM Nicolas Boichat wrote: > > On Sat, May 30, 2020 at 3:59 AM 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 > > Signed-off-by: Dmitry Torokhov > > --- > > 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. Guenter > > 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); > > - > > - clear_bit(HID_RESUME_RUNNING, &usbhid->iofl); > > return res; > > } > > > > diff --git a/drivers/hid/usbhid/usbhid.h b/drivers/hid/usbhid/usbhid.h > > index 8620408bd7af..805949671b96 100644 > > --- a/drivers/hid/usbhid/usbhid.h > > +++ b/drivers/hid/usbhid/usbhid.h > > @@ -82,6 +82,7 @@ struct usbhid_device { > > > > spinlock_t lock; /* fifo spinlock */ > > unsigned long iofl; /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */ > > + unsigned long input_start_time; /* When to start handling input, in jiffies */ > > struct timer_list io_retry; /* Retry timer */ > > unsigned long stop_retry; /* Time to give up, in jiffies */ > > unsigned int retry_delay; /* Delay length in ms */ > > -- > > 2.27.0.rc0.183.gde8f92d652-goog > > > > > > -- > > Dmitry