Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1083385pxk; Fri, 25 Sep 2020 05:57:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNUFfgalF2bPNAN5oNcfj+VKFLVHTxq9RYi0zSGbNfoP3P/W4Kt5nCcGil3ZtBltFmZGUq X-Received: by 2002:a17:906:ae45:: with SMTP id lf5mr2464145ejb.339.1601038647733; Fri, 25 Sep 2020 05:57:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601038647; cv=none; d=google.com; s=arc-20160816; b=qOvAc6Z1taVrXLHzfoWFfD8VQ3FAXMdY3JrmDzeDQZvLamENbObT2FhVhkyKM3JeP/ hwYrYGZXzmjFRGtCrVRlZazrH4BnY0gX8c+OnS97nf69kH9s+FBqjem58xw3FP9eZypk 6lXa6BWRGVzse38ubU6otdNqeWYLSa0fSBZCFEX+tYRgbJ7XalZ/eUvmIUS+zSdvy6lm UVa6vASkP71WWb8IN/jA8PlVs7FQ2bq5YTJVogj0OM/TCjoqokKlITS8waLreRDKRAKE eDfZwCkHmXEQUKNdL0f9R3TvQ+wcIH4EvJF7tKCjHHgz4dZpUS/hNG8FyU9WUR05MRAy 0jQQ== 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=cies6DN5k0YCYLcOsL/kL+sAXAs/oeeuyqwrCRR+mCk=; b=b1tzrkOG7YMNrrch/a7ka6QplSPBqSQWN0DkAWerjj0+DSnDhKqEBVR9UHbSKPsLal 7gRgn/sOyurUlH24LfIgzgOugcwcZW7mf15Vud8j/+DUr0n1yRBZTnbS9JOopnPIKGmI +gY43N1klDdmzy62gEOEKsOr2q7qVbbdeT0GUP4+BcgSOjxFhKTVEBghn3JETUfXPEkR Y8eVoG6bsEejtmCTbA1nScAXC9g0Kf//hdnuAMRHTwLsfHpmDsom7f7Es1Q6IPDBGnRh iHoL76udB3rjK28y+m4tAhF0CPN+Y+/w68CQ/Nz8WGiIepC1jxtbyR5tkNJ7RpMSWDik rtHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sHcS+JgD; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d17si1882314edp.500.2020.09.25.05.57.04; Fri, 25 Sep 2020 05:57:27 -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=@kernel.org header.s=default header.b=sHcS+JgD; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729276AbgIYMzL (ORCPT + 99 others); Fri, 25 Sep 2020 08:55:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:34326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728993AbgIYMzE (ORCPT ); Fri, 25 Sep 2020 08:55:04 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EBD58206DB; Fri, 25 Sep 2020 12:55:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601038503; bh=HD+6LhVe8zCaOT1+C2bNz/haLq9f9WZzd4gLeJD34Dg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sHcS+JgD6U7CmzOlDOANFkud1rRGC4Az8LSTx+8r/y8CdJeIEvCvTrs0FapHJpVdZ nPetvVA2OcrGL427SFndEMm9h6I+iG1tL0bX9CqSCX9wCyYWrXinToD7rrOcD9jVwX wpTvgFsADXylyKylzk95ddBvPYMHvidVQHDRJg8I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yunsheng Lin , "David S. Miller" Subject: [PATCH 4.19 13/37] net: sch_generic: aviod concurrent reset and enqueue op for lockless qdisc Date: Fri, 25 Sep 2020 14:48:41 +0200 Message-Id: <20200925124722.922202675@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925124720.972208530@linuxfoundation.org> References: <20200925124720.972208530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yunsheng Lin [ Upstream commit 2fb541c862c987d02dfdf28f1545016deecfa0d5 ] 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 Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sched/sch_generic.c | 49 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) --- a/net/sched/sch_generic.c +++ b/net/sched/sch_generic.c @@ -1115,27 +1115,36 @@ static void dev_deactivate_queue(struct struct netdev_queue *dev_queue, void *_qdisc_default) { - struct Qdisc *qdisc_default = _qdisc_default; - struct Qdisc *qdisc; + struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc); - qdisc = rtnl_dereference(dev_queue->qdisc); if (qdisc) { - bool nolock = qdisc->flags & TCQ_F_NOLOCK; - - if (nolock) - spin_lock_bh(&qdisc->seqlock); - spin_lock_bh(qdisc_lock(qdisc)); - if (!(qdisc->flags & TCQ_F_BUILTIN)) set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state); + } +} - rcu_assign_pointer(dev_queue->qdisc, qdisc_default); - qdisc_reset(qdisc); +static void dev_reset_queue(struct net_device *dev, + struct netdev_queue *dev_queue, + void *_unused) +{ + struct Qdisc *qdisc; + bool nolock; - spin_unlock_bh(qdisc_lock(qdisc)); - if (nolock) - spin_unlock_bh(&qdisc->seqlock); - } + 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) @@ -1196,12 +1205,20 @@ void dev_deactivate_many(struct list_hea 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))