Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2808735rwb; Mon, 15 Aug 2022 11:46:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR6UvljKXuf/eXTJa+gmyV6+Ondavkm1wWxLMEqsP9OvnI2Aa+wtxPYlkYmktqReD4jkXIu7 X-Received: by 2002:a05:6402:b88:b0:442:5d35:2133 with SMTP id cf8-20020a0564020b8800b004425d352133mr15561334edb.53.1660589181095; Mon, 15 Aug 2022 11:46:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660589181; cv=none; d=google.com; s=arc-20160816; b=q4iZ+4OAGgAu1dPgJdv+FKkFTjMPJCMnQJo4AcQpKT66mQ5+Eq4Wn2SlFQC7IZrmxz yggm3FqS79ZsBETpcL3QfRZH+nehMFhVxKdk0U3zpvDhNz1grkd8VFBeqCYCfn/Zik07 bJxhuNt99Oy5oNY8tBTnG6x2Jo/bORMuejG3LgBQTGgJ8T8ehEIjk/dUUmj5FbYVctH1 Ntj5mJeRUqJ8Pl1B06vqV//yGIbve/v8p8X1buK/ON+wJvlsgB2hIGoi8j08R/LYn5Xi TjOcYdRi7trVfkV5tIFENf5ei9ylJYoq0kbMxCM70MLHs648l/cMLrkoxSRixgEbKbjx 1BuA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WfiPlrQcdztHCfQYoTLCnCH+KaLvHmgVEpRioWgD0tQ=; b=F4c36/k1sjgZWJc2OD6UDCwXYgNizqnWEoBxrwIyHMJrOJvoFav8X80c96q0klvns1 L8Bnkpl/pD+mx9TYHp3tXQGZa8Xt4DFFeW63hMr+9PMMzdxmlFH7DpOq8In7VuIuoCxR oHTLf8WjLDhmfAjT2vPYC0xHyQbobb5sQ8C8plFbX+2KnwTTVW4OqKCqmZ4fB/D19soX 6mePrLQfn9i8zRs8ZQCypVxmvXURc4jPjkgEPN3zwcw25+fKF+AsJBr1wOPD4XolOEt/ bqjqTaw50DLtrzhATYxeSth/pTEYEOrdy0pu2C1T/ANspJ+abH3IGyykFQqpBIn90jdo GPUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Jw2qSFe5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw28-20020a1709066a1c00b00731417f551esi7784875ejc.283.2022.08.15.11.45.54; Mon, 15 Aug 2022 11:46:21 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Jw2qSFe5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241658AbiHOSZS (ORCPT + 99 others); Mon, 15 Aug 2022 14:25:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241164AbiHOSYo (ORCPT ); Mon, 15 Aug 2022 14:24:44 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C5F05FC2; Mon, 15 Aug 2022 11:18:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B38F6B80F99; Mon, 15 Aug 2022 18:18:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04E43C433D6; Mon, 15 Aug 2022 18:18:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660587485; bh=fhXmri8Qjp0EzDtGXhrJdIiFlwZJ7xvylvHa/3A9hao=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jw2qSFe5+3Pwiw5DLj7S2Px01dBYgJhafcI6oAb8GKwd5lQ2RPRelxv5Cf1J8PHu/ ucBnd92L5E6WXprwK/CHHXlKwkp+ocTOkULoCIHXomnd7Ol1Dwf3HMPt8IMlklOCqU RYAQVVCfVGXI0olgFlDSuBzrMRiuxOzy06wV9W4I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable , Alan Stern , Weitao Wang Subject: [PATCH 5.15 100/779] USB: HCD: Fix URB giveback issue in tasklet function Date: Mon, 15 Aug 2022 19:55:44 +0200 Message-Id: <20220815180341.576168483@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Weitao Wang commit 26c6c2f8a907c9e3a2f24990552a4d77235791e6 upstream. 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. In order to prevent the tasklet function from occupying the cpu for a long time at a time, new URBS will not be added to the local_list even though the bh->head list is not empty. But also need to ensure the left URB giveback to be processed in time, so add a member high_prio for structure giveback_urb_bh to prioritize tasklet and schelule this tasklet again if bh->head list is not empty. At the same time, we are able to prioritize tasklet through structure member high_prio. So, 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") Cc: stable Reviewed-by: Alan Stern Signed-off-by: Weitao Wang Link: https://lore.kernel.org/r/20220726074918.5114-1-WeitaoWang-oc@zhaoxin.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/core/hcd.c | 26 +++++++++++++++----------- include/linux/usb/hcd.h | 1 + 2 files changed, 16 insertions(+), 11 deletions(-) --- a/drivers/usb/core/hcd.c +++ b/drivers/usb/core/hcd.c @@ -1691,7 +1691,6 @@ static void usb_giveback_urb_bh(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,17 @@ static void usb_giveback_urb_bh(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 +1743,7 @@ static void usb_giveback_urb_bh(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 +1754,10 @@ void usb_hcd_giveback_urb(struct usb_hcd 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 +1766,7 @@ void usb_hcd_giveback_urb(struct usb_hcd if (running) ; - else if (high_prio_bh) + else if (bh->high_prio) tasklet_hi_schedule(&bh->bh); else tasklet_schedule(&bh->bh); @@ -2959,6 +2962,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 = true; init_giveback_urb_bh(&hcd->low_prio_bh); /* enable irqs just before we start the controller, --- 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;