Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2907248pxb; Tue, 24 Aug 2021 10:19:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAQu+jntMfdtKsT9geYA4a8MedsCZxQtKxIdzr5vNch+hvrt/pxgpo5yw4HbSg1TUqLnX1 X-Received: by 2002:a05:6638:22d5:: with SMTP id j21mr35968176jat.119.1629825587794; Tue, 24 Aug 2021 10:19:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629825587; cv=none; d=google.com; s=arc-20160816; b=YvKjv44cFamyTvECMRR2njKyc4ZzLWoch5uzK31pZMGMGTrQW+P8KN0S7a/MIhkK0w r2OfhuOn8MLr7Iw4YmSSB6/dblRXySiZ9NBraeo2zpnRE4B6WHHGvLnYdEcLWiuoimtH DtI7chLEiKoL6/bmT3N+QOD23THJKRA+qFBegZXNC8NKA3CmUnMLWPL1ZIN+0snq/jKE MHCKggoWp1Mh3MCJ2HxjbHlWQYN9dyVNH1pspJjyiOmowlEVJZUZEwxX/R4Ap/hz4dEC MJwqnIaEuEkFbGDe8XcHmBv99JZzRBhzylTuyPRh17Pu9EQb3wu9o8zon9Cw3tZXoqZz ndhQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=K9xnR2k/kKIQw4GAp93IC9FDtJnRkCB4RI4Z4LhFh1g=; b=EoiJXMPU5cuQS61HlSRCRMYt4OTUnLJ7yB8v9la2/iBMo8DYlRAO9x11WKpo+zdyMq brrmHQROC/B+jOIPn61kih4n5QlVkM8yDMiFrSvuAGIADqCyCIMrsHqc8IFDDInC2Iui fzL8zhDR+H2PKns4joN3CK2gFtWm9xH1L8Y4NOM8gpIxA6vHUkLNpKumeH7dHlc6/czo 15Ye8OCWVphzDibcuJGoRnjdFFa19QqFLe9Xh4ar8BQTNVG5GSdlH0N9VCJHagbmd+D/ hu6XXh9yag3KD45upl75nUuENFVgdE0wncXyb4Dh/rx0L+2VJwHUXZ66C5L9MUs+MHFw +KLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LLMVpMLh; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o13si17773816jat.13.2021.08.24.10.19.35; Tue, 24 Aug 2021 10:19:47 -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=k20201202 header.b=LLMVpMLh; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236192AbhHXRRs (ORCPT + 99 others); Tue, 24 Aug 2021 13:17:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:55606 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240404AbhHXROn (ORCPT ); Tue, 24 Aug 2021 13:14:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C4B2361A8A; Tue, 24 Aug 2021 17:01:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629824504; bh=pdfOPGlNDuzzmXhSCF6aeSrwzt4evmp+6WUXhcTbLeo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LLMVpMLh7G6lXbWESWODcUFRqyLILb0zAyWdM5T4VjkO/EbpN5Jpe0dcctFJqSxa4 K3rtb/+VJROB2FTQrITECjqAvBjsH16d9n/DPjRTnYSvuTO8VVl0Laop5rWU0c7m0Z aRHWadH6HIHO9ysAY/Gh4n2bg0sKVWMX7KqmFDXDBNHEzEeZ7nE0+xljsz/UtajU5X OVVzlTySM7UY02TVP9s/PBnwpyze5zFszYXFzEPE1nKP6n777QyBQRodfQVRWe4Qur gKptx3mMLh5zi7mMOxe3Xj6BsG23MLk7X8/h/947TZrQdzdKWeYG1ObbByYWw9K/Yn fNLfYfQPUJh5A== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Xuan Zhuo , Dust Li , Jason Wang , "David S . Miller" , Sasha Levin Subject: [PATCH 5.4 37/61] virtio-net: support XDP when not more queues Date: Tue, 24 Aug 2021 13:00:42 -0400 Message-Id: <20210824170106.710221-38-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210824170106.710221-1-sashal@kernel.org> References: <20210824170106.710221-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.4.143-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.4.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.4.143-rc1 X-KernelTest-Deadline: 2021-08-26T17:01+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xuan Zhuo [ Upstream commit 97c2c69e1926260c78c7f1c0b2c987934f1dc7a1 ] The number of queues implemented by many virtio backends is limited, especially some machines have a large number of CPUs. In this case, it is often impossible to allocate a separate queue for XDP_TX/XDP_REDIRECT, then xdp cannot be loaded to work, even xdp does not use the XDP_TX/XDP_REDIRECT. This patch allows XDP_TX/XDP_REDIRECT to run by reuse the existing SQ with __netif_tx_lock() hold when there are not enough queues. Signed-off-by: Xuan Zhuo Reviewed-by: Dust Li Acked-by: Jason Wang Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/virtio_net.c | 62 +++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 15453d6fcc23..36f8aeb113a8 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -195,6 +195,9 @@ struct virtnet_info { /* # of XDP queue pairs currently used by the driver */ u16 xdp_queue_pairs; + /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */ + bool xdp_enabled; + /* I like... big packets and I cannot lie! */ bool big_packets; @@ -485,12 +488,41 @@ static int __virtnet_xdp_xmit_one(struct virtnet_info *vi, return 0; } -static struct send_queue *virtnet_xdp_sq(struct virtnet_info *vi) -{ - unsigned int qp; - - qp = vi->curr_queue_pairs - vi->xdp_queue_pairs + smp_processor_id(); - return &vi->sq[qp]; +/* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on + * the current cpu, so it does not need to be locked. + * + * Here we use marco instead of inline functions because we have to deal with + * three issues at the same time: 1. the choice of sq. 2. judge and execute the + * lock/unlock of txq 3. make sparse happy. It is difficult for two inline + * functions to perfectly solve these three problems at the same time. + */ +#define virtnet_xdp_get_sq(vi) ({ \ + struct netdev_queue *txq; \ + typeof(vi) v = (vi); \ + unsigned int qp; \ + \ + if (v->curr_queue_pairs > nr_cpu_ids) { \ + qp = v->curr_queue_pairs - v->xdp_queue_pairs; \ + qp += smp_processor_id(); \ + txq = netdev_get_tx_queue(v->dev, qp); \ + __netif_tx_acquire(txq); \ + } else { \ + qp = smp_processor_id() % v->curr_queue_pairs; \ + txq = netdev_get_tx_queue(v->dev, qp); \ + __netif_tx_lock(txq, raw_smp_processor_id()); \ + } \ + v->sq + qp; \ +}) + +#define virtnet_xdp_put_sq(vi, q) { \ + struct netdev_queue *txq; \ + typeof(vi) v = (vi); \ + \ + txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \ + if (v->curr_queue_pairs > nr_cpu_ids) \ + __netif_tx_release(txq); \ + else \ + __netif_tx_unlock(txq); \ } static int virtnet_xdp_xmit(struct net_device *dev, @@ -516,7 +548,7 @@ static int virtnet_xdp_xmit(struct net_device *dev, if (!xdp_prog) return -ENXIO; - sq = virtnet_xdp_sq(vi); + sq = virtnet_xdp_get_sq(vi); if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) { ret = -EINVAL; @@ -564,12 +596,13 @@ out: sq->stats.kicks += kicks; u64_stats_update_end(&sq->stats.syncp); + virtnet_xdp_put_sq(vi, sq); return ret; } static unsigned int virtnet_get_headroom(struct virtnet_info *vi) { - return vi->xdp_queue_pairs ? VIRTIO_XDP_HEADROOM : 0; + return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0; } /* We copy the packet for XDP in the following cases: @@ -1458,12 +1491,13 @@ static int virtnet_poll(struct napi_struct *napi, int budget) xdp_do_flush_map(); if (xdp_xmit & VIRTIO_XDP_TX) { - sq = virtnet_xdp_sq(vi); + sq = virtnet_xdp_get_sq(vi); if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) { u64_stats_update_begin(&sq->stats.syncp); sq->stats.kicks++; u64_stats_update_end(&sq->stats.syncp); } + virtnet_xdp_put_sq(vi, sq); } return received; @@ -2480,10 +2514,9 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, /* XDP requires extra queues for XDP_TX */ if (curr_qp + xdp_qp > vi->max_queue_pairs) { - NL_SET_ERR_MSG_MOD(extack, "Too few free TX rings available"); - netdev_warn(dev, "request %i queues but max is %i\n", + netdev_warn(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n", curr_qp + xdp_qp, vi->max_queue_pairs); - return -ENOMEM; + xdp_qp = 0; } old_prog = rtnl_dereference(vi->rq[0].xdp_prog); @@ -2520,11 +2553,14 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, vi->xdp_queue_pairs = xdp_qp; if (prog) { + vi->xdp_enabled = true; for (i = 0; i < vi->max_queue_pairs; i++) { rcu_assign_pointer(vi->rq[i].xdp_prog, prog); if (i == 0 && !old_prog) virtnet_clear_guest_offloads(vi); } + } else { + vi->xdp_enabled = false; } for (i = 0; i < vi->max_queue_pairs; i++) { @@ -2609,7 +2645,7 @@ static int virtnet_set_features(struct net_device *dev, int err; if ((dev->features ^ features) & NETIF_F_LRO) { - if (vi->xdp_queue_pairs) + if (vi->xdp_enabled) return -EBUSY; if (features & NETIF_F_LRO) -- 2.30.2