Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp174325pxk; Wed, 2 Sep 2020 18:23:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJww51YyESkXftHSo/TvyhxFJae2iNQNaC4tVBV8CaXxuj7E2MNUV4QdIr07fgAAfVRfwUmk X-Received: by 2002:a17:906:3191:: with SMTP id 17mr832304ejy.239.1599096203706; Wed, 02 Sep 2020 18:23:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599096203; cv=none; d=google.com; s=arc-20160816; b=WVLBPkNxkoXpOvUmqBst4tWGDrgSJnJ/osww5Xo3C5d3AtNIJMdkP4PbReL2VUNrB3 sjN3CTU/dp0XpPMCYoxix66ytYb25ovjqK3ZejrTpfgSlgXkkf/cI+ZTmZOWYDfMy1d0 L6k4MaUp3FUN2TqpzOfM3Qr8Hv94KU+C+lsg62u3fygPZzyehVpiArnEHk/T2PIsj8GQ Ot63O4vWedhH0i4KIgH/deQxSUnJY23OGrLj7ku0XvVUhhx+1kmzz2Vr4m6nzbh6Ztcy yi00zPHKQLrMNCYZRF8kiWo2KHo8F/HJSaMMzzIGso9bqd2QhY0UYU+gSzf13uAUjLmH eCYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=LM1MJqnto2bQpbBRbeVcfZXhFONmld0ylSa43567s2o=; b=mq7iFrm5k002cNX1xqpCIwCPQDDX3a8xTb281RBliV+JJRyirh3pkexMBBPKrTfoex /qiG/Hu1pskZGsrtlVJzHa0jN+nHZ1f1HFTF1FN5OxvI/mOlojYB5P0954SQCIhFp4IF aY7igqB43tChLoMgXD+fIGHFGEX16NVCO0YM2tg4DdVCHsAQsYHsHmko2zfXMvWgR38F sX3BgSEtejZ0J7/XRkzG/GA79+0fALSEfgE1VhcO2AaHvJuYQDE1QUUmCQbveOnVMTxP +F8kMpXW2p+Nk1+n6kBRLLJcKVO+MXuYPhUFPSz7YdlyC6ZsvZmU9xPJhigFVoLMO5Fn Uz4g== 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 k13si629120edh.371.2020.09.02.18.23.00; Wed, 02 Sep 2020 18:23:23 -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 S1727894AbgICBVN (ORCPT + 99 others); Wed, 2 Sep 2020 21:21:13 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:10757 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726586AbgICBVN (ORCPT ); Wed, 2 Sep 2020 21:21:13 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 252F644608CD59A7BDEC; Thu, 3 Sep 2020 09:21:09 +0800 (CST) Received: from [10.74.191.121] (10.74.191.121) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.487.0; Thu, 3 Sep 2020 09:21:02 +0800 Subject: Re: [PATCH net-next] net: sch_generic: aviod concurrent reset and enqueue op for lockless qdisc To: Cong Wang CC: Jamal Hadi Salim , Jiri Pirko , "David Miller" , Jakub Kicinski , "Linux Kernel Network Developers" , LKML , References: <1598921718-79505-1-git-send-email-linyunsheng@huawei.com> <511bcb5c-b089-ab4e-4424-a83c6e718bfa@huawei.com> From: Yunsheng Lin Message-ID: Date: Thu, 3 Sep 2020 09:21:02 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.74.191.121] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/9/3 8:35, Cong Wang wrote: > On Tue, Sep 1, 2020 at 11:35 PM Yunsheng Lin wrote: >> >> On 2020/9/2 12:41, Cong Wang wrote: >>> On Tue, Sep 1, 2020 at 6:42 PM Yunsheng Lin wrote: >>>> >>>> On 2020/9/2 2:24, Cong Wang wrote: >>>>> On Mon, Aug 31, 2020 at 5:59 PM Yunsheng Lin wrote: >>>>>> >>>>>> 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. >>>>> >>>>> Can you be more specific here? Which call path requests a smaller >>>>> tx queue num? If you mean netif_set_real_num_tx_queues(), clearly >>>>> we already have a synchronize_net() there. >>>> >>>> When the netdevice is in active state, the synchronize_net() seems to >>>> do the correct work, as below: >>>> >>>> CPU 0: CPU1: >>>> __dev_queue_xmit() netif_set_real_num_tx_queues() >>>> rcu_read_lock_bh(); >>>> netdev_core_pick_tx(dev, skb, sb_dev); >>>> . >>>> . dev->real_num_tx_queues = txq; >>>> . . >>>> . . >>>> . synchronize_net(); >>>> . . >>>> q->enqueue() . >>>> . . >>>> rcu_read_unlock_bh() . >>>> qdisc_reset_all_tx_gt >>>> >>>> >>> >>> Right. >>> >>> >>>> but dev->real_num_tx_queues is not RCU-protected, maybe that is a problem >>>> too. >>>> >>>> The problem we hit is as below: >>>> In hns3_set_channels(), hns3_reset_notify(h, HNAE3_DOWN_CLIENT) is called >>>> to deactive the netdevice when user requested a smaller queue num, and >>>> txq->qdisc is already changed to noop_qdisc when calling >>>> netif_set_real_num_tx_queues(), so the synchronize_net() in the function >>>> netif_set_real_num_tx_queues() does not help here. >>> >>> How could qdisc still be running after deactivating the device? >> >> qdisc could be running during the device deactivating process. >> >> The main process of changing channel number is as below: >> >> 1. dev_deactivate() >> 2. hns3 handware related setup >> 3. netif_set_real_num_tx_queues() >> 4. netif_tx_wake_all_queues() >> 5. dev_activate() >> >> During step 1, qdisc could be running while qdisc is resetting, so >> there could be skb left in the old qdisc(which will be restored back to >> txq->qdisc during dev_activate()), as below: >> >> CPU 0: CPU1: >> __dev_queue_xmit(): dev_deactivate_many(): >> rcu_read_lock_bh(); qdisc_deactivate(qdisc); >> q = rcu_dereference_bh(txq->qdisc); . >> netdev_core_pick_tx(dev, skb, sb_dev); . >> . >> . rcu_assign_pointer(dev_queue->qdisc, qdisc_default); >> . . >> . . >> . . >> . . >> q->enqueue() . > > > Well, like I said, if the deactivated bit were tested before ->enqueue(), > there would be no packet queued after qdisc_deactivate(). Only if the deactivated bit testing is also protected by qdisc->seqlock? otherwise there is still window between setting and testing the deactivated bit. > > >> . . >> rcu_read_unlock_bh() . >> >> And During step 3, txq->qdisc is pointing to noop_qdisc, so the qdisc_reset() >> only reset the noop_qdisc, but not the actual qdisc, which is stored in >> txq->qdisc_sleeping, so the actual qdisc may still have skb. >> >> When hns3_link_status_change() call step 4 and 5, it will restore all queue's >> qdisc using txq->qdisc_sleeping and schedule all queue with net_tx_action(). >> The skb enqueued in step 1 may be dequeued and run, which cause the problem. >> >>> >>> >>>> >>>>> >>>>>> >>>>>> Avoid the above concurrent op by calling synchronize_rcu_tasks() >>>>>> after assigning new qdisc to dev_queue->qdisc and before calling >>>>>> qdisc_deactivate() to make sure skb with larger queue_mapping >>>>>> enqueued to old qdisc will always be reset when qdisc_deactivate() >>>>>> is called. >>>>> >>>>> Like Eric said, it is not nice to call such a blocking function when >>>>> we have a large number of TX queues. Possibly we just need to >>>>> add a synchronize_net() as in netif_set_real_num_tx_queues(), >>>>> if it is missing. >>>> >>>> As above, the synchronize_net() in netif_set_real_num_tx_queues() seems >>>> to work when netdevice is in active state, but does not work when in >>>> deactive. >>> >>> Please explain why deactivated device still has qdisc running? >>> >>> At least before commit 379349e9bc3b4, we always test deactivate >>> bit before enqueueing. Are you complaining about that commit? >>> That commit is indeed suspicious, at least it does not precisely revert >>> commit ba27b4cdaaa66561aaedb21 as it claims. >> >> I am not familiar with TCQ_F_CAN_BYPASS. >> From my understanding, the problem is that there is no order between >> qdisc enqueuing and qdisc reset. > > It has almost nothing to do with BYPASS, my point here is all about > __QDISC_STATE_DEACTIVATED bit, clearly commit 379349e9bc3b4 > removed this bit test for lockless qdisc, whether intentionally or accidentally. > That bit test existed prior to BYPASS test, see commit ba27b4cdaaa665. It seems there is no qdisc->seqlock or qdisc->seqlock to protect the deactivated bit testing in commit ba27b4cdaaa665 too? > > Thanks. > . >