Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp820529ybm; Fri, 29 May 2020 13:02:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxix8NdxS3Ae4N3VVPO14gvt2PMrqzkp8LQQTAOvBIQYBzaOeYciT5q/bBWup7KFOMwdHU8 X-Received: by 2002:aa7:c986:: with SMTP id c6mr6236406edt.335.1590782536706; Fri, 29 May 2020 13:02:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590782536; cv=none; d=google.com; s=arc-20160816; b=tLhbtWkbD7dkznapmpItnmmj8LWPcRtuhAHmMR/9Xb/RqHoGilRGAee0iAlhTshed1 2cR2r67Uf4J1kGTGLBqghlFL1J6+0tX0WZ0dhgDt+y20b1+KU+S7hLi5ca5MiDHK/OAD eYXUN5Uj1fMgTYqgPSWbOxLVMbOnAS2i5OKbOO2PtgC2Wrt+sQV2xjNAZYKG53qwd4Ew pqPV9YfEXfdqS6u35TxIM/i1UeSwretiZtJV3+LKDXUnas8WkWOytY/wh7DTuKlyzPhp FSS/3hZieVJc4/AQtJ4t38cl4a728uZgTnHc1L0rUYry6e9ow87q9zGa8OQjk/7WfVUq TN2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=NqCh5MuSBiPzMUhleT1sZw8juhuIUtQrFL1mEW53Q4s=; b=NvidlkIkqXgWMszwvUMxvUSWyLq3+r1mAeSsgC8Z7bMS7AlKF3kw+QiMtux225ZMNt +IP4oNEmsXkieVWYUy5jSfg/bdJ+02lYAJVjamRBDgc07HY4h7IHyfiNLtTAjkCC4dV+ H2UZzHGjaaD/BxLr/E3L3XACybpTkOpAfLUjym7c8Wx01unBF1d4DYUhn72Tp97vgF4W vwTFJPeoLjS1uvL9lugC3NmwrNJknan05FDRxMfgdFUMQrZQAqYRUxGFJimPGy1hUOOb RFAICSZURnkB/1BMeP/e/2A06bjEJGpq4VGOAsB0kj+LesG+wEWhm1VBT2SCPJeuU6dJ yAJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mj5+iTyn; 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 r9si6383301eds.223.2020.05.29.13.01.53; Fri, 29 May 2020 13:02:16 -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=mj5+iTyn; 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 S1727851AbgE2T74 (ORCPT + 99 others); Fri, 29 May 2020 15:59:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726866AbgE2T74 (ORCPT ); Fri, 29 May 2020 15:59:56 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDA89C03E969; Fri, 29 May 2020 12:59:55 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id q16so1670113plr.2; Fri, 29 May 2020 12:59:55 -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:mime-version:content-disposition; bh=NqCh5MuSBiPzMUhleT1sZw8juhuIUtQrFL1mEW53Q4s=; b=mj5+iTyniqmHjsnJZCR+wxrcaPZD3SthbrpEGnjj6uw6toTDl8uRi7su0+pSi12Pxj +8XEe/lG6NE9SH5ucyxo3bK5DFKJrQN/SWzw932+ppnqcFr/KWF9I2NrCgf3FRIiONn8 dgm+6F0uW9BPOWYjRlBakZab1Az65eA//okVNAfUClpx1ixMU6tOnYUoD1FlarBULW5i 439tzmt6c3CgFO2OZ0Q14z19kqDiSnJQXNzij7fI1XSkEloVbzL6KVCm0N830EvbsPZM EkizbX0/2AsCQZ2aJMoZTdJPi/xIpF9r5eAE1orTb1wAM5WZhmt27duqxXo+J8zcNsmI YA/A== 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:mime-version :content-disposition; bh=NqCh5MuSBiPzMUhleT1sZw8juhuIUtQrFL1mEW53Q4s=; b=Baao3BHIqgocunhOfPXjWrerSbR123mNs+PyeoS4rJ8oZ4keveB4e9+FIpT0nn4QQI G5zZvu/rmfTVS5rENKtgt+A6aru8VZuaF++fqqfDtPQq1dAxO7aLCB54HKyr+8mNJPIB PhEaRvzSa7On2arQKzJUYUeFhGSCezz1LNCYQYQv6eRuRIv+PABtd2bCxc4z6UuTlKhl k1oXBf+l4h2nsjyJxIk06u+iUWubcq1yrpotJdjQruHEh66fOBjDhHYFIQNyCLHwqZTn WWvvVALR6utke/vvE3/fGz1t0SW+AL8z8DdcjMGwEoGPWLtVWhivy6OpxU5+Hwv69pYN 0uXg== X-Gm-Message-State: AOAM5306gCxNLDrh9nIa1X7RVOcJy8ZC6sYyORoqajI1tgR76PrR2XQf n5eUHMCiY7FayLUF2ExS/9Nd5iiG X-Received: by 2002:a17:902:7602:: with SMTP id k2mr9490608pll.296.1590782394849; Fri, 29 May 2020 12:59:54 -0700 (PDT) Received: from dtor-ws ([2620:15c:202:201:3c2a:73a9:c2cf:7f45]) by smtp.gmail.com with ESMTPSA id z8sm7379120pgc.80.2020.05.29.12.59.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2020 12:59:53 -0700 (PDT) Date: Fri, 29 May 2020 12:59:51 -0700 From: Dmitry Torokhov To: Jiri Kosina , Benjamin Tissoires Cc: groeck@chromium.org, Nicolas Boichat , linux-usb@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] HID: usbhid: do not sleep when opening device Message-ID: <20200529195951.GA3767@dtor-ws> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); - - 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