Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp4490108rwb; Tue, 6 Sep 2022 08:13:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR48L1xDiu4GE8nQN8VOL0N15I0b0a5fMV2KtaoGmx5HX5Hfp4EP/SqgS2xQ7LV9xSp0iDxS X-Received: by 2002:a17:907:3e0a:b0:741:4b9b:8d08 with SMTP id hp10-20020a1709073e0a00b007414b9b8d08mr33446349ejc.113.1662477189286; Tue, 06 Sep 2022 08:13:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662477189; cv=none; d=google.com; s=arc-20160816; b=ERDJuD90jhMeSM1e+1LtnbUMxFKqvrzrQPFHB88US8CGtdfbXFPVYMzZobHLoWxdnv vvWEGf4g5RHIRckkQO6mQAYASH6N9dmnHbmEDt0M3D6toMpJj4pRTkUXuAp+XuIZpd38 3ZE1nbm1aOR62WSct+9HJSpUyCBQ4dexejATcm1ZOODfHcdCZrHu45mKOKcgXIjW3N5U yOjoLAXGWVnq7dzQZUsO6EeWveJpdmIPdaiil+z1Uw2nvEHTC0FEPmZYFDymUQDotVJc HhtOICA+4mhe9Pj6/vwMm4bMuY5Clo9NSlJSh2uI9g5nUuOOnLmntDnOCVIpnhXBcXGO 3pCg== 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=LVa8G8LRH2yyY2z4a6g/A5zQ1wBmgSacCSR+7WnIoMQ=; b=bGyYpxTwsrn8GkW39tNwr7AOg/jUq/9HE0G79liH40xV3Jpt0OwXnEBqTsQuR0Akbn 1yK+xq4okLM3klGUItag2feEZyKg2XDjvrcTZEbTMrpc9gkQ8Md5cs83wwwV8wyii+sc E54xShVUmtdJTkNbL5kOK5Q+gKAN9hgv8Xy67SJXA1cCawwx/BP1n1qbJIRoNEGbyjcw pniaZzrYipEqK0YhIM4Kt7Qg2/lH6VwZ7pMfH3wTmtpEcRkuLpxPkSROBO3N8qnacff/ Tb2uf1tPYnlTayGFm0Cxejbo2QddG4+2MhCraNaPgXCa9+JVfmgjUXNich+rFwJkIKyC /s6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=g1I9Y0GS; 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 t14-20020a170906a10e00b00741990286a5si6499694ejy.768.2022.09.06.08.12.44; Tue, 06 Sep 2022 08:13:09 -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=g1I9Y0GS; 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 S241915AbiIFOXJ (ORCPT + 99 others); Tue, 6 Sep 2022 10:23:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242072AbiIFOTa (ORCPT ); Tue, 6 Sep 2022 10:19:30 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF6518A7E8; Tue, 6 Sep 2022 06:50:41 -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 1E795B816A0; Tue, 6 Sep 2022 13:44:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67E7FC433C1; Tue, 6 Sep 2022 13:44:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1662471881; bh=NjjLy2zMmSsNxcJdVg9s6RJm2XFnN3Y8vRp0V65QGhY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g1I9Y0GSdZ0wg2yQiqXNLxMhGNqchVs3XJGERzcryVqQTEiuiWLgl61a0Bll18P5R arVfriL48u+6BL5hq6ppWFNyUqEqirEc1y47yzVaUhb+JsS9+RO8kvnM1VxuJ5XhtD BiY3fFuPmOkI/BsYSKuKyNDkSZOTel1+mJfmVdFw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wang Hai , Paolo Abeni , Sasha Levin Subject: [PATCH 5.19 040/155] net/sched: fix netdevice reference leaks in attach_default_qdiscs() Date: Tue, 6 Sep 2022 15:29:48 +0200 Message-Id: <20220906132831.109125560@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220906132829.417117002@linuxfoundation.org> References: <20220906132829.417117002@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: Wang Hai [ Upstream commit f612466ebecb12a00d9152344ddda6f6345f04dc ] In attach_default_qdiscs(), if a dev has multiple queues and queue 0 fails to attach qdisc because there is no memory in attach_one_default_qdisc(). Then dev->qdisc will be noop_qdisc by default. But the other queues may be able to successfully attach to default qdisc. In this case, the fallback to noqueue process will be triggered. If the original attached qdisc is not released and a new one is directly attached, this will cause netdevice reference leaks. The following is the bug log: veth0: default qdisc (fq_codel) fail, fallback to noqueue unregister_netdevice: waiting for veth0 to become free. Usage count = 32 leaked reference. qdisc_alloc+0x12e/0x210 qdisc_create_dflt+0x62/0x140 attach_one_default_qdisc.constprop.41+0x44/0x70 dev_activate+0x128/0x290 __dev_open+0x12a/0x190 __dev_change_flags+0x1a2/0x1f0 dev_change_flags+0x23/0x60 do_setlink+0x332/0x1150 __rtnl_newlink+0x52f/0x8e0 rtnl_newlink+0x43/0x70 rtnetlink_rcv_msg+0x140/0x3b0 netlink_rcv_skb+0x50/0x100 netlink_unicast+0x1bb/0x290 netlink_sendmsg+0x37c/0x4e0 sock_sendmsg+0x5f/0x70 ____sys_sendmsg+0x208/0x280 Fix this bug by clearing any non-noop qdiscs that may have been assigned before trying to re-attach. Fixes: bf6dba76d278 ("net: sched: fallback to qdisc noqueue if default qdisc setup fail") Signed-off-by: Wang Hai Link: https://lore.kernel.org/r/20220826090055.24424-1-wanghai38@huawei.com Signed-off-by: Paolo Abeni Signed-off-by: Sasha Levin --- net/sched/sch_generic.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c index a64c3c1541118..b3596d4bd14a2 100644 --- a/net/sched/sch_generic.c +++ b/net/sched/sch_generic.c @@ -1125,6 +1125,21 @@ struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, } EXPORT_SYMBOL(dev_graft_qdisc); +static void shutdown_scheduler_queue(struct net_device *dev, + struct netdev_queue *dev_queue, + void *_qdisc_default) +{ + struct Qdisc *qdisc = dev_queue->qdisc_sleeping; + struct Qdisc *qdisc_default = _qdisc_default; + + if (qdisc) { + rcu_assign_pointer(dev_queue->qdisc, qdisc_default); + dev_queue->qdisc_sleeping = qdisc_default; + + qdisc_put(qdisc); + } +} + static void attach_one_default_qdisc(struct net_device *dev, struct netdev_queue *dev_queue, void *_unused) @@ -1172,6 +1187,7 @@ static void attach_default_qdiscs(struct net_device *dev) if (qdisc == &noop_qdisc) { netdev_warn(dev, "default qdisc (%s) fail, fallback to %s\n", default_qdisc_ops->id, noqueue_qdisc_ops.id); + netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); dev->priv_flags |= IFF_NO_QUEUE; netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); qdisc = txq->qdisc_sleeping; @@ -1450,21 +1466,6 @@ void dev_init_scheduler(struct net_device *dev) timer_setup(&dev->watchdog_timer, dev_watchdog, 0); } -static void shutdown_scheduler_queue(struct net_device *dev, - struct netdev_queue *dev_queue, - void *_qdisc_default) -{ - struct Qdisc *qdisc = dev_queue->qdisc_sleeping; - struct Qdisc *qdisc_default = _qdisc_default; - - if (qdisc) { - rcu_assign_pointer(dev_queue->qdisc, qdisc_default); - dev_queue->qdisc_sleeping = qdisc_default; - - qdisc_put(qdisc); - } -} - void dev_shutdown(struct net_device *dev) { netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); -- 2.35.1