Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3841671pxk; Tue, 8 Sep 2020 04:08:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcMubt3sr1lQQP6+5pO5f1irI0BNI/NQcoVm0hMqunMUKsfSwoqNn3s+5aGXfNIbuiKHSa X-Received: by 2002:a17:906:5856:: with SMTP id h22mr24866490ejs.480.1599563295754; Tue, 08 Sep 2020 04:08:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599563295; cv=none; d=google.com; s=arc-20160816; b=nA2UbOq5xuX7kOkmo6y5+U/33TiJGNF7liE5pZiLSOz7dpngV0WnwClYVGuMWWm5wx kJCAHGNo/Rv3aU1w13kLSedn118KQPnnPaCa7Oy5bUni1YGWkMxJYrBjlv8Jxb06bCCH 9TLGfMOInoC0jBlin2tN6IF9ibplIa3yjpe1y56w754tEXVDNnwadj44yoU6Jy+CZFeT 11IkLd1Xn420rTw4OR+/oSAe93EbSeljHMro17g45ttkp4kEjPjCNkROzM77F4s/4plS FIRQaOVGKJX4AcCpF70oAfn0FH05OKpL2+tQQRUfLvyXxGxNJgRidgbXZ9mKcOnidx0P ASHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=fiGorfohNn7kYX236zZRBKiOgpjarYh+DoifPOMtwUY=; b=H3TtfL6jSUyRdM6JephIbWkvdc7JGxdo6oNLkGcjrX0fzFUCDG9AZzUhChdESbtp/c mINDSAsPRszNDpGDAqvhX0fmO0gMetMwSZcNOumyKhGA4wgo1YKHdiEgUXNkhXZqlHib NQ9CZ3nfgI2o3/s+zDeed5MX/scoTICqZnPPwvevQqxkTmy82HKEJOwuMQ0ylwgY2cgf BRMcITpuwNOyHJLir/+cBOiZagZZxNMk80TXqx19OBh4iND0qd3HH+llnprScVwktJSW A0U/Jyz6T6xIkMD3oHWs5BUbaHbsDMkamrJ2Kaz/pw+Nof6xCkHKcZ1uzyk2Ya7ju0fZ tKPg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b1si12268718ejb.647.2020.09.08.04.07.53; Tue, 08 Sep 2020 04:08:15 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729894AbgIHLGx (ORCPT + 99 others); Tue, 8 Sep 2020 07:06:53 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:51376 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729372AbgIHLGX (ORCPT ); Tue, 8 Sep 2020 07:06:23 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 8649E9B29CFE06F35EC3; Tue, 8 Sep 2020 19:06:12 +0800 (CST) Received: from localhost.localdomain (10.69.192.58) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.487.0; Tue, 8 Sep 2020 19:06:06 +0800 From: Yunsheng Lin To: , , , , CC: , , , , Subject: [PATCH v2 net] net: sch_generic: aviod concurrent reset and enqueue op for lockless qdisc Date: Tue, 8 Sep 2020 19:02:34 +0800 Message-ID: <1599562954-87257-1-git-send-email-linyunsheng@huawei.com> X-Mailer: git-send-email 2.8.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.69.192.58] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently there is concurrent reset and enqueue operation for the same lockless qdisc when there is no lock to synchronize the q->enqueue() in __dev_xmit_skb() with the qdisc reset operation in qdisc_deactivate() called by dev_deactivate_queue(), which may cause out-of-bounds access for priv->ring[] in hns3 driver if user has requested a smaller queue num when __dev_xmit_skb() still enqueue a skb with a larger queue_mapping after the corresponding qdisc is reset, and call hns3_nic_net_xmit() with that skb later. Reused the existing synchronize_net() in dev_deactivate_many() to make sure skb with larger queue_mapping enqueued to old qdisc(which is saved in dev_queue->qdisc_sleeping) will always be reset when dev_reset_queue() is called. Fixes: 6b3ba9146fe6 ("net: sched: allow qdiscs to handle locking") Signed-off-by: Yunsheng Lin --- ChangeLog V2: Reuse existing synchronize_net(). --- net/sched/sch_generic.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c index 265a61d..54c4172 100644 --- a/net/sched/sch_generic.c +++ b/net/sched/sch_generic.c @@ -1131,24 +1131,10 @@ EXPORT_SYMBOL(dev_activate); static void qdisc_deactivate(struct Qdisc *qdisc) { - bool nolock = qdisc->flags & TCQ_F_NOLOCK; - if (qdisc->flags & TCQ_F_BUILTIN) return; - if (test_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state)) - return; - - if (nolock) - spin_lock_bh(&qdisc->seqlock); - spin_lock_bh(qdisc_lock(qdisc)); set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state); - - qdisc_reset(qdisc); - - spin_unlock_bh(qdisc_lock(qdisc)); - if (nolock) - spin_unlock_bh(&qdisc->seqlock); } static void dev_deactivate_queue(struct net_device *dev, @@ -1165,6 +1151,30 @@ static void dev_deactivate_queue(struct net_device *dev, } } +static void dev_reset_queue(struct net_device *dev, + struct netdev_queue *dev_queue, + void *_unused) +{ + struct Qdisc *qdisc; + bool nolock; + + qdisc = dev_queue->qdisc_sleeping; + if (!qdisc) + return; + + nolock = qdisc->flags & TCQ_F_NOLOCK; + + if (nolock) + spin_lock_bh(&qdisc->seqlock); + spin_lock_bh(qdisc_lock(qdisc)); + + qdisc_reset(qdisc); + + spin_unlock_bh(qdisc_lock(qdisc)); + if (nolock) + spin_unlock_bh(&qdisc->seqlock); +} + static bool some_qdisc_is_busy(struct net_device *dev) { unsigned int i; @@ -1213,12 +1223,20 @@ void dev_deactivate_many(struct list_head *head) dev_watchdog_down(dev); } - /* Wait for outstanding qdisc-less dev_queue_xmit calls. + /* Wait for outstanding qdisc-less dev_queue_xmit calls or + * outstanding qdisc enqueuing calls. * This is avoided if all devices are in dismantle phase : * Caller will call synchronize_net() for us */ synchronize_net(); + list_for_each_entry(dev, head, close_list) { + netdev_for_each_tx_queue(dev, dev_reset_queue, NULL); + + if (dev_ingress_queue(dev)) + dev_reset_queue(dev, dev_ingress_queue(dev), NULL); + } + /* Wait for outstanding qdisc_run calls. */ list_for_each_entry(dev, head, close_list) { while (some_qdisc_is_busy(dev)) { -- 2.8.1