From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83E37C433E1 for ; Wed, 10 Jun 2020 05:28:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5776E207F9 for ; Wed, 10 Jun 2020 05:28:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NVKK8Ncy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726167AbgFJF2P (ORCPT ); Wed, 10 Jun 2020 01:28:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726046AbgFJF2N (ORCPT ); Wed, 10 Jun 2020 01:28:13 -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 13844C03E96B for ; Tue, 9 Jun 2020 22:28:13 -0700 (PDT) Received: by mail-ej1-x641.google.com with SMTP id gl26so1073751ejb.11 for ; Tue, 09 Jun 2020 22:28:13 -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=T/jnmVd/bh9loEdDEgeb3HlsdzM3UCvTcIUqLjOK8wM=; b=NVKK8Ncy1CLQa1GlK2EjqNT2z0/cxOFotcgTyS92UZqtKAixCKZm5P7LgHCa7PP2ZI flclmztcZcsY97HSfIjpavaBhH70f7SwiaDd+gawqqoyVQlxTMpEdNgc/jgzcCjWhhOW IgLUEocOdgu4TeJds3+h6ALqtlmtSFVLyCp72tDI98Jd5HlUjBP/m1ptwaW494vPhlbL 89LaiuT6ofUQDTvESFwkYMIu3CNjtgnVg8FPjvrogRE77m2XTcyS72gpTNhIcBxtoZSl fKqk4/D7LjIeJA8S65IHkkW364GmTwj8Ucz4eCMw+TqvWNyuVMQclYcWPDll7rLd/V1t /O9Q== 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=T/jnmVd/bh9loEdDEgeb3HlsdzM3UCvTcIUqLjOK8wM=; b=rUal1bV12znnro6zVdJTvkh/z78QeTOwgpzjzrn4Q30nMkIu7b/thQhaIGflRZlRJ2 LKdjqtuAm3VY6aC2E01rhV6qmWuckfTCzbvjlk2HiFzwxv0kVfk39M1sqSTF85v0mK/o 9qQmVfdQdaMnr15BLx+cQVq2EIY4cTeNo7z7ItPKeuRJ1ZhXZ6DOrN4To3hKLiTeBWA8 QhR3H01WRI0glf+xazpmS3cPexS/O4XxA984mRoNqO22tYJYXEWQFu0SmuTb2fZ/S5PD DXijlDfKzNAlubgL8/LKOs5FrIGuDhZhx50qOGHv0I1okmPuO2K4ax77zoqTo8g+n3kE scaQ== X-Gm-Message-State: AOAM532iamT85xlM1EB31Cr95tDORUrsiE+iwCFTllUi4K++SCD7fj+K NyBj3VrvaUQGYh3xknOsw2ABk0vUyNNpcxqJd4JANA== X-Google-Smtp-Source: ABdhPJz0/EqE/wsKuHGBezMRKKF5qcxIfkGPjwwqO2vlE2Lz0OqDCLBvz2CrRh6NSHlfDRU79JvbMI9q2sJ2yf+qmxg= X-Received: by 2002:a17:906:9a02:: with SMTP id ai2mr1671514ejc.97.1591766891357; Tue, 09 Jun 2020 22:28:11 -0700 (PDT) MIME-Version: 1.0 References: <20200610043824.GA171503@dtor-ws> In-Reply-To: <20200610043824.GA171503@dtor-ws> From: Guenter Roeck Date: Tue, 9 Jun 2020 22:28:00 -0700 Message-ID: Subject: Re: [PATCH v2] HID: usbhid: do not sleep when opening device To: Dmitry Torokhov Cc: Jiri Kosina , Benjamin Tissoires , Guenter Roeck , Nicolas Boichat , linux-usb@vger.kernel.org, "open list:HID CORE LAYER" , linux-kernel 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 Tue, Jun 9, 2020 at 9:38 PM Dmitry Torokhov wrote: > > usbhid tries to give the device 50 milliseconds to drain its queues when > opening the device, but dies 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 Reviewed-by: Guenter Roeck > --- > > v2: switched from using jiffies to ktime_t to make sure we won't have > issues with jiffies overflowing. > > drivers/hid/usbhid/hid-core.c | 53 +++++++++++++++++++---------------- > drivers/hid/usbhid/usbhid.h | 2 ++ > 2 files changed, 31 insertions(+), 24 deletions(-) > > diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c > index c7bc9db5b192..72c92aab2b18 100644 > --- a/drivers/hid/usbhid/hid-core.c > +++ b/drivers/hid/usbhid/hid-core.c > @@ -26,6 +26,7 @@ > #include > #include > #include > +#include > > #include > > @@ -95,6 +96,18 @@ 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_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 = > + ktime_add_ms(ktime_get_coarse(), 50); > + } > } > } > spin_unlock_irqrestore(&usbhid->lock, flags); > @@ -280,20 +293,23 @@ 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)) { > - hid_input_report(urb->context, HID_INPUT_REPORT, > - urb->transfer_buffer, > - urb->actual_length, 1); > - /* > - * autosuspend refused while keys are pressed > - * because most keyboards don't wake up when > - * a key is released > - */ > - if (hid_check_keys_pressed(hid)) > - set_bit(HID_KEYS_PRESSED, &usbhid->iofl); > - else > - clear_bit(HID_KEYS_PRESSED, &usbhid->iofl); > + if (test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) { > + if (ktime_before(ktime_get_coarse(), > + usbhid->input_start_time)) > + break; > + clear_bit(HID_RESUME_RUNNING, &usbhid->iofl); > } > + hid_input_report(urb->context, HID_INPUT_REPORT, > + urb->transfer_buffer, urb->actual_length, 1); > + /* > + * autosuspend refused while keys are pressed > + * because most keyboards don't wake up when > + * a key is released > + */ > + if (hid_check_keys_pressed(hid)) > + set_bit(HID_KEYS_PRESSED, &usbhid->iofl); > + else > + clear_bit(HID_KEYS_PRESSED, &usbhid->iofl); > break; > case -EPIPE: /* stall */ > usbhid_mark_busy(usbhid); > @@ -714,17 +730,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..0f0bcf7037f8 100644 > --- a/drivers/hid/usbhid/usbhid.h > +++ b/drivers/hid/usbhid/usbhid.h > @@ -13,6 +13,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -82,6 +83,7 @@ struct usbhid_device { > > spinlock_t lock; /* fifo spinlock */ > unsigned long iofl; /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */ > + ktime_t input_start_time; /* When to start handling input */ > 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.278.ge193c7cf3a9-goog > > > -- > Dmitry