netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
To: netdev@vger.kernel.org
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	Xuan Zhuo <xuanzhuo@linux.alibaba.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	virtualization@lists.linux-foundation.org, bpf@vger.kernel.org
Subject: [PATCH net-next v5 03/15] virtio_net: optimize mergeable_xdp_get_buf()
Date: Mon,  8 May 2023 14:14:05 +0800	[thread overview]
Message-ID: <20230508061417.65297-4-xuanzhuo@linux.alibaba.com> (raw)
In-Reply-To: <20230508061417.65297-1-xuanzhuo@linux.alibaba.com>

The previous patch, in order to facilitate review, I do not do any
modification. This patch has made some optimization on the top.

* remove some repeated logics in this function.
* add fast check for passing without any alloc.

Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Acked-by: Jason Wang <jasowang@redhat.com>
---
 drivers/net/virtio_net.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index a75745dfe2e1..3c3602982ea2 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -1196,6 +1196,11 @@ static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
 	 */
 	*frame_sz = truesize;
 
+	if (likely(headroom >= virtnet_get_headroom(vi) &&
+		   (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
+		return page_address(*page) + offset;
+	}
+
 	/* This happens when headroom is not enough because
 	 * of the buffer was prefilled before XDP is set.
 	 * This should only happen for the first several packets.
@@ -1204,22 +1209,15 @@ static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
 	 * support it, and we don't want to bother users who are
 	 * using xdp normally.
 	 */
-	if (!xdp_prog->aux->xdp_has_frags &&
-	    (*num_buf > 1 || headroom < virtnet_get_headroom(vi))) {
+	if (!xdp_prog->aux->xdp_has_frags) {
 		/* linearize data for XDP */
 		xdp_page = xdp_linearize_page(rq, num_buf,
 					      *page, offset,
 					      VIRTIO_XDP_HEADROOM,
 					      len);
-		*frame_sz = PAGE_SIZE;
-
 		if (!xdp_page)
 			return NULL;
-		offset = VIRTIO_XDP_HEADROOM;
-
-		put_page(*page);
-		*page = xdp_page;
-	} else if (unlikely(headroom < virtnet_get_headroom(vi))) {
+	} else {
 		xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
 					  sizeof(struct skb_shared_info));
 		if (*len + xdp_room > PAGE_SIZE)
@@ -1231,14 +1229,15 @@ static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
 
 		memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
 		       page_address(*page) + offset, *len);
-		*frame_sz = PAGE_SIZE;
-		offset = VIRTIO_XDP_HEADROOM;
-
-		put_page(*page);
-		*page = xdp_page;
 	}
 
-	return page_address(*page) + offset;
+	*frame_sz = PAGE_SIZE;
+
+	put_page(*page);
+
+	*page = xdp_page;
+
+	return page_address(*page) + VIRTIO_XDP_HEADROOM;
 }
 
 static struct sk_buff *receive_mergeable(struct net_device *dev,
-- 
2.32.0.3.g01195cf9f


  parent reply	other threads:[~2023-05-08  6:14 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-08  6:14 [PATCH net-next v5 00/15] virtio_net: refactor xdp codes Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 01/15] virtio_net: mergeable xdp: put old page immediately Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 02/15] virtio_net: introduce mergeable_xdp_get_buf() Xuan Zhuo
2023-05-08  6:14 ` Xuan Zhuo [this message]
2023-05-08  6:14 ` [PATCH net-next v5 04/15] virtio_net: introduce virtnet_xdp_handler() to seprate the logic of run xdp Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 05/15] virtio_net: separate the logic of freeing xdp shinfo Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 06/15] virtio_net: separate the logic of freeing the rest mergeable buf Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 07/15] virtio_net: virtnet_build_xdp_buff_mrg() auto release xdp shinfo Xuan Zhuo
2023-05-08  6:46   ` Jason Wang
2023-05-08  6:14 ` [PATCH net-next v5 08/15] virtio_net: introduce receive_mergeable_xdp() Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 09/15] virtio_net: merge: remove skip_xdp Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 10/15] virtio_net: introduce receive_small_xdp() Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 11/15] virtio_net: small: remove the delta Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 12/15] virtio_net: small: avoid code duplication in xdp scenarios Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 13/15] virtio_net: small: remove skip_xdp Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 14/15] virtio_net: introduce receive_small_build_xdp Xuan Zhuo
2023-05-08  6:14 ` [PATCH net-next v5 15/15] virtio_net: introduce virtnet_build_skb() Xuan Zhuo
2023-05-08  6:18 ` [PATCH net-next v5 00/15] virtio_net: refactor xdp codes Michael S. Tsirkin
2023-05-10  2:50 ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230508061417.65297-4-xuanzhuo@linux.alibaba.com \
    --to=xuanzhuo@linux.alibaba.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hawk@kernel.org \
    --cc=jasowang@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).