Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp841556ybm; Fri, 29 May 2020 13:35:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+ElfCC5v7FviUHl/gEUsof/DonaM7VnVwP07rXlNKMelJAWhVJvaHkVGkn5MHwdlGaAJ1 X-Received: by 2002:a17:906:6b92:: with SMTP id l18mr9545621ejr.145.1590784558006; Fri, 29 May 2020 13:35:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590784557; cv=none; d=google.com; s=arc-20160816; b=zY3nPNwxM1I4juXKct7CEiwj7/knLu8LP6Hgi5IqcvBD3mF3sLMj2SC16QaAwWB2mN 2arl5f5ZTOE6H58DySgNea5Mr3SSrYbFDOQUhQtTVcjTBcD+gxdVwHaRTvIvtkgkBZRZ 5n8zgAXs82f/XRgsLJSKePOE5vDKi16DlEq+ZhrH06XOkG8ainHSMtjyz/q8OWUpU928 Wxp/g+BDuKQMPG4KvBtNLJaOwlA/jEW0s7LQMQfFb/yA9Zy5QzhggC20dum1XDpwlYXw DSCve+C+GH6uE3GJ8x/qj3vYsaKyAZDuWYOXPrg6cP6COsglRrp3bE2sEWTlXGEFlrKq CiAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=SFmuSEf0AGpMxKm8V34s/d7k2oUKvRzQXSbrrQduN4k=; b=gW3oli+cma0X3QmEfsZStewryL6pIOPhw8UfCdZh/9etEXAtoYmaaGM00np9toGRWk y/nDEQhpUNuXkEiR97n125OuyYb5si3mY9dVil5o0YiT0MPZhdQl+3QLapiY2ypufRzB E+Y8csHxW12k0WBSwRujXzVARY0BvwPyEEWBOtSmmWGhmMrls7ipplEAjjVgOdDeD25C 8+5rRlobRklrJ4LmmpdlxObQW8vkWPO9zD3+IZlrq8n9IGnk3sV4ay0Rll/jYLGlLOEB ZYRW3Z2tZJi42uiIOkLb7DfYuKGCZAL0wjEZqOI/vPnR2LYaypLUa4xIsSoUjFrdOl7E bgtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LM1dPShE; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn10si8545266ejc.89.2020.05.29.13.35.35; Fri, 29 May 2020 13:35:57 -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=@gmail.com header.s=20161025 header.b=LM1dPShE; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728252AbgE2Udc (ORCPT + 99 others); Fri, 29 May 2020 16:33:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728129AbgE2Ud1 (ORCPT ); Fri, 29 May 2020 16:33:27 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CD88C08C5CB; Fri, 29 May 2020 13:33:27 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id p30so451530pgl.11; Fri, 29 May 2020 13:33:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=SFmuSEf0AGpMxKm8V34s/d7k2oUKvRzQXSbrrQduN4k=; b=LM1dPShEli098FLDwnYhkV+0T2hp8Uwz984pm/7QF4xIByrHV/6bRnEvoAL59nx1Ek lWt+92RZtwue8pHMoXS1yN+So60GTR+wtRgKzqimp6JhUQh5KqHa3Ua6JmfMF3CBeBr5 diK6yJrOHUfwiH73ui1MfXPWaPbTfpxf/fax8XLb0eUt7cGij5ikAlfVmZ7sq1Hvci0Y aR6v3w1jLkXzvQpnO2aN++0QYGOqFRmvQhxXxewrF/mvOA5SoA6P/lkAtCjlS8ofUFEj UlEFtEezp3VpUT4O7+AWbEzELarcEbG2x2xzqWgAvuuHj4llEenZvcM23PkOV+1tIQze ucQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=SFmuSEf0AGpMxKm8V34s/d7k2oUKvRzQXSbrrQduN4k=; b=SRBKOYXItjhI9kYt5S4U126y1Ng9q849l7qPQ1EbdIpubfOjrmoHObbhfFForReJOb Zgx1CQX2gTSw6uPqq0TRb3ZTYdKNjMrpZoI46yasNddWvOfVuvmsYJII8XRBNChfYO+Q kAenGZJ/BHtIhghxX2qkzZFvsR6yj04GxGP6h8RHW5+r9faFI/Ys6NgJTK9IIy0yv77l SkMWW8nwLOWbjXWujS7s1hhf6kjEDyQ6zxe6f1uEQx9eyEEBRE8FilRENEPjGmBC7MLW GVoXNlmKBhm0wn6TO6Wkq0D6kBuqjq1B0qVOS7KW2m3FaDqscRb/mME6NsHD2YsH+hTN p4Ow== X-Gm-Message-State: AOAM532l8UGnsJInUYgwEyByw9WekEiCMrGYzgEwB25MlG8OZCjVd/S4 cif9YDcTnBSJ+YioDLCH5ZI9oNSf X-Received: by 2002:aa7:8c44:: with SMTP id e4mr10221335pfd.108.1590784406650; Fri, 29 May 2020 13:33:26 -0700 (PDT) Received: from dtor-ws ([2620:15c:202:201:3c2a:73a9:c2cf:7f45]) by smtp.gmail.com with ESMTPSA id y4sm7834796pfn.101.2020.05.29.13.33.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2020 13:33:26 -0700 (PDT) Date: Fri, 29 May 2020 13:33:24 -0700 From: Dmitry Torokhov To: Guenter Roeck Cc: Jiri Kosina , Benjamin Tissoires , groeck@chromium.org, Nicolas Boichat , linux-usb@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] HID: usbhid: do not sleep when opening device Message-ID: <20200529203324.GL89269@dtor-ws> References: <20200529195951.GA3767@dtor-ws> <20200529201424.GA180211@roeck-us.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200529201424.GA180211@roeck-us.net> 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 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 > > 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)) { > > 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