Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp1008805ybm; Fri, 29 May 2020 18:27:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5dS3iwaSrh2+N5Mpy0ZUCnBakhhOrcKuu4uGb9iSEck6znVB2y7eVLcorPh6OZneYMatX X-Received: by 2002:a17:906:9419:: with SMTP id q25mr10624638ejx.111.1590802030026; Fri, 29 May 2020 18:27:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590802030; cv=none; d=google.com; s=arc-20160816; b=xP6qeb5Ie5BFBti0Xn6LNguFA657qx6wJdkBoIoce0JmZWYxY+T+hVkjwpsEDmSzSS s/wNW0XBZQtgibys+54hrTtNhT42SZZykfWVMSBEfxx9MqfpRpG61dhZRhXln/V42amG nE2xPn8Ck3WqDXimNtOeqzlFeW/dPwQF4f4Su1XXcJ4MdOjz1Hd/TeOfAC713oeBvNZ9 WyL0IzFU844zc6neszquvJgCQ/RhFcxtkhanY7EHfYGr79VPgZ8KeiUOOwyEZlIZeHVF ReNQuE8trzzdyEoPGV1uoWQSXV+JiiQBSF5/wjCSQdidhXTRDsGzcwy2yS+if61UYnxh Oizg== 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=zjszT2JE6xMGIXm75nxby+Q8WfWVB5aq51B6VNJEz/M=; b=FzwfwjX4f1EqqK2eujbVGHW3bgkNaXvMVoSvx9I72jcJCkGvJ/wXAjyueFQ2tS6cK4 Q4xPyf/EGkmaC46hqsfrwkJND4WwzXNyIUlK4i//+U65GQPHfnE+PqbarH7VVWZT5WtB JhKf45qwFNauvyja5TumWiULLHaaSvLUSFJCqsG7G7VJkX9+8fv7rlk6eGxlxK8n7U8e HTPutiO/QIb4FxsJc8lVJeza+HWLUaKINjxpIYvVSHW/hVM/sbjepEbfNpikQvuZEJDR fBwrmH/TErb2InVA/11TW4vPMAN4wTWUmMhBJbR6jwVYGBtmOyEPKA2uBgg4MyatgwNS c+Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=TNc28tJD; 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 dn14si4177778ejc.414.2020.05.29.18.26.45; Fri, 29 May 2020 18:27:10 -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=TNc28tJD; 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 S1728654AbgE3BXD (ORCPT + 99 others); Fri, 29 May 2020 21:23:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728293AbgE3BXC (ORCPT ); Fri, 29 May 2020 21:23:02 -0400 Received: from mail-ed1-x544.google.com (mail-ed1-x544.google.com [IPv6:2a00:1450:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E484C08C5C9 for ; Fri, 29 May 2020 18:23:02 -0700 (PDT) Received: by mail-ed1-x544.google.com with SMTP id g1so2233717edv.6 for ; Fri, 29 May 2020 18:23:02 -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=zjszT2JE6xMGIXm75nxby+Q8WfWVB5aq51B6VNJEz/M=; b=TNc28tJDwppaFCuyJp1+ty2uKeDrRPpf8C6ZqyG9itPq2QOwDnsXc+OfMFxW+JZCj3 xE7HSv22wI3UD4ZvtUs+CWyPr4PBEnN8Yd4VIyFVsDT8fcJeEYzRkHT/m2qs9/UGNYUL 9pGhZwE8B18KsdOaOni52XIoO7EMOpGQdlaSAXDDNOp8JJDsDYfKKMX7vx/18FkiRvdI 43peBEDngxdpG4b9cByUtiZT6XM1tmsN9XrYfvct0sKSBNl7waHDx/F1Rmq/cFWLEZJ/ uHQUFccP5gNwq2YUfkA3pjZVnW8lZSSYWgT6JJCtJ66cpLPYvH7Bb8FI/Ady5clHIVYK +RXg== 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=zjszT2JE6xMGIXm75nxby+Q8WfWVB5aq51B6VNJEz/M=; b=gJZ98hhwq6Pu3RYls82QPzADqHcLHTvclNNOHpx7DTd7J5Zp+m3ytkbVWdbEXXxlni ViLbIDhcuKd3ZqO2ft9Eu/mnVZ0mGMy6brogsF2TfPNLP12xIb9HAg+u50MKMlv/a072 FpL5scUAh/nwoy0VE49zVjieTRtfJjK+HgrLFk/h/uhxyqViZxJY9XZPpTKkZ6+MNNam UaJaKM8AU7VImE5ah0k/JAObG+G6d4ibtKl74j9z537xz6OVGtRP0J0ZHvfIidvcubrk AnFQJAYmpby2L7eHwgVgh15gSlJ8FA+L+mgYVYWgdIDMwogIdMbeaRooFAchy9GsyEZ4 8gIw== X-Gm-Message-State: AOAM531Zb/F/DfEhxtM7Nko5bmPl1s/9SGLvXUFUUeCuBZoByX9tzdKv GwgZDyxF19K5BW+vtwZUQBd1xNMFfOAJhOu1Fpksyw== X-Received: by 2002:a50:ed0e:: with SMTP id j14mr11400971eds.52.1590801780783; Fri, 29 May 2020 18:23:00 -0700 (PDT) MIME-Version: 1.0 References: <20200529195951.GA3767@dtor-ws> <20200530010926.GM89269@dtor-ws> In-Reply-To: <20200530010926.GM89269@dtor-ws> From: Guenter Roeck Date: Fri, 29 May 2020 18:22:49 -0700 Message-ID: Subject: Re: [PATCH] HID: usbhid: do not sleep when opening device To: Dmitry Torokhov Cc: Nicolas Boichat , 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 6:09 PM Dmitry Torokhov wrote: > > On Fri, May 29, 2020 at 05:48:26PM -0700, Guenter Roeck wrote: > > 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. > > Well, it is overflow safe, but still can not measure more than 50 days, > so if you have a device open for 50+ days there will be a 50msec gap > where it may lose events. > Or you could explicitly use 64-bit jiffies. Guenter > I guess we can switch to ktime(). A bit more expensive on 32 bits, but > in reality I do not think anyone would care. > > Thanks. > > -- > Dmitry