Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp2517393imi; Mon, 25 Jul 2022 00:03:05 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uT+6MTKeL5O7MZSIOomxC2WHCXC2qUnfRrlwz7OoEn+U//qzBBpyefEYVP/dvAdtSjemwj X-Received: by 2002:a05:6402:1767:b0:43b:a844:14f with SMTP id da7-20020a056402176700b0043ba844014fmr11915259edb.131.1658732585304; Mon, 25 Jul 2022 00:03:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658732585; cv=none; d=google.com; s=arc-20160816; b=bFbVkuCecCPZ7kBL5R/VBTh6W91nocyYK60ll3TyTthhcmqh6NNp8m5oZuQ/dRmhEb JCNKqW+tE6t/fLAe9DxNFXLP7wR2AnMGWd6oxqnSGZhWURBCU2DnyAVWxnBjettz+FG0 9vSRLtvU3AXHgg2OMRGljyE4QAWLczJNJYEFQthZ6tFAfN/2HMYTrX+q20r0HAXSr0FQ miHVY4EOFxlMLZjvBawJUhIeVlu/hyTJ4syzS8rp4hAwRMwYDsYQiaFC03TZHtc/Zdzg Dh0LNVHyD5LuuQlsgToqNaNWigy5ls1s8N4zYoKC/5I40u5M2iYaI847X7ACnJXj8UUh 3w1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=sceCOZDaN2Uua4UzGvh1YWQNgSt4kFFCbDJNziSZjzo=; b=MdHprXLyh1yyORVwOnIMOvPMMhw+USYcrur97NfgZoHLTDN8d0SrJby1yE/UcMyhID KG5C+fF/qUf+WL91kEEGB5ByKLFBNUWs5jOweFGEOd9flm26Unt+s4eYdvEAmYuOGC8u sPRFmg1qt0VpaXMx8o7r3lBVcWacEIxamx8m4EV7wGOiE21SQoFnNCR3r/9koUXkDXQb vwCpYzmwL+0QcgUX6lmo94yy5A7MwkKJXfnPCRIgasxu+JG5RA2f44qifzef11ULqm4V LTEf0/jw2UALQY33Q3dQTPzBde71ncv+ZSc9iMRlqODNMdzRD9rA1DOHpfhb5gy26rmW bviQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sb9-20020a1709076d8900b00726f11c4c8esi14441629ejc.669.2022.07.25.00.02.41; Mon, 25 Jul 2022 00:03:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232611AbiGYGyJ (ORCPT + 99 others); Mon, 25 Jul 2022 02:54:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232177AbiGYGxY (ORCPT ); Mon, 25 Jul 2022 02:53:24 -0400 Received: from ZXSHCAS1.zhaoxin.com (ZXSHCAS1.zhaoxin.com [210.0.225.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 149511055A; Sun, 24 Jul 2022 23:53:20 -0700 (PDT) Received: from zxbjmbx1.zhaoxin.com (10.29.252.163) by ZXSHCAS1.zhaoxin.com (10.28.252.161) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.27; Mon, 25 Jul 2022 14:52:52 +0800 Received: from L440.zhaoxin.com (10.29.8.21) by zxbjmbx1.zhaoxin.com (10.29.252.163) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.27; Mon, 25 Jul 2022 14:52:52 +0800 From: Weitao Wang To: , , , , , , , , CC: , Subject: [PATCH v2] USB: HCD: Fix URB giveback issue in tasklet function Date: Mon, 25 Jul 2022 14:52:51 +0800 Message-ID: <20220725065251.832087-1-WeitaoWang-oc@zhaoxin.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.29.8.21] X-ClientProxiedBy: zxbjmbx1.zhaoxin.com (10.29.252.163) To zxbjmbx1.zhaoxin.com (10.29.252.163) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Usb core introduce the mechanism of giveback of URB in tasklet context to reduce hardware interrupt handling time. On some test situation(such as FIO with 4KB block size), when tasklet callback function called to giveback URB, interrupt handler add URB node to the bh->head list also. If check bh->head list again after finish all URB giveback of local_list, then it may introduce a "dynamic balance" between giveback URB and add URB to bh->head list. This tasklet callback function may not exit for a long time, which will cause other tasklet function calls to be delayed. Some real-time applications(such as KB and Mouse) will see noticeable lag. Fix this issue by taking new URBs giveback in next tasklet function call. Add a member high_prio for structure giveback_urb_bh and replace the local high_prio_bh variable with this structure member in usb_hcd_giveback_urb. Fixes: 94dfd7edfd5c ("USB: HCD: support giveback of URB in tasklet context") Signed-off-by: Weitao Wang --- v1->v2: - Fix compile warning by remove label "restart". - Modify the patch description info. - Change structure member from hi_priority to high_prio. drivers/usb/core/hcd.c | 25 ++++++++++++++----------- include/linux/usb/hcd.h | 1 + 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c index 06eea8848ccc..1feb9a604380 100644 --- a/drivers/usb/core/hcd.c +++ b/drivers/usb/core/hcd.c @@ -1691,7 +1691,6 @@ static void usb_giveback_urb_bh(struct tasklet_struct *t) spin_lock_irq(&bh->lock); bh->running = true; - restart: list_replace_init(&bh->head, &local_list); spin_unlock_irq(&bh->lock); @@ -1705,10 +1704,16 @@ static void usb_giveback_urb_bh(struct tasklet_struct *t) bh->completing_ep = NULL; } - /* check if there are new URBs to giveback */ + /* giveback new URBs next time to prevent this function from + * not exiting for a long time. + */ spin_lock_irq(&bh->lock); - if (!list_empty(&bh->head)) - goto restart; + if (!list_empty(&bh->head)) { + if (bh->high_prio) + tasklet_hi_schedule(&bh->bh); + else + tasklet_schedule(&bh->bh); + } bh->running = false; spin_unlock_irq(&bh->lock); } @@ -1737,7 +1742,7 @@ static void usb_giveback_urb_bh(struct tasklet_struct *t) void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) { struct giveback_urb_bh *bh; - bool running, high_prio_bh; + bool running; /* pass status to tasklet via unlinked */ if (likely(!urb->unlinked)) @@ -1748,13 +1753,10 @@ void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) return; } - if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) { + if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) bh = &hcd->high_prio_bh; - high_prio_bh = true; - } else { + else bh = &hcd->low_prio_bh; - high_prio_bh = false; - } spin_lock(&bh->lock); list_add_tail(&urb->urb_list, &bh->head); @@ -1763,7 +1765,7 @@ void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) if (running) ; - else if (high_prio_bh) + else if (bh->high_prio) tasklet_hi_schedule(&bh->bh); else tasklet_schedule(&bh->bh); @@ -2959,6 +2961,7 @@ int usb_add_hcd(struct usb_hcd *hcd, /* initialize tasklets */ init_giveback_urb_bh(&hcd->high_prio_bh); + hcd->high_prio_bh.high_prio = 1; init_giveback_urb_bh(&hcd->low_prio_bh); /* enable irqs just before we start the controller, diff --git a/include/linux/usb/hcd.h b/include/linux/usb/hcd.h index 2c1fc9212cf2..98d1921f02b1 100644 --- a/include/linux/usb/hcd.h +++ b/include/linux/usb/hcd.h @@ -66,6 +66,7 @@ struct giveback_urb_bh { bool running; + bool high_prio; spinlock_t lock; struct list_head head; struct tasklet_struct bh; -- 2.32.0