diff mbox series

[net-next,v2,03/14] virtio_net: optimize mergeable_xdp_prepare()

Message ID 20230418065327.72281-4-xuanzhuo@linux.alibaba.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series virtio_net: refactor xdp codes | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 18 this patch: 18
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 18 this patch: 18
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 56 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Xuan Zhuo April 18, 2023, 6:53 a.m. UTC
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>
---
 drivers/net/virtio_net.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

Comments

Jason Wang April 20, 2023, 5:59 a.m. UTC | #1
On Tue, Apr 18, 2023 at 2:53 PM Xuan Zhuo <xuanzhuo@linux.alibaba.com> wrote:
>
> 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>

Thanks


> ---
>  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 12559062ffb6..50dc64d80d3b 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -1192,6 +1192,11 @@ static void *mergeable_xdp_prepare(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.
> @@ -1200,22 +1205,15 @@ static void *mergeable_xdp_prepare(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)
> @@ -1227,14 +1225,15 @@ static void *mergeable_xdp_prepare(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
>
diff mbox series

Patch

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 12559062ffb6..50dc64d80d3b 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -1192,6 +1192,11 @@  static void *mergeable_xdp_prepare(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.
@@ -1200,22 +1205,15 @@  static void *mergeable_xdp_prepare(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)
@@ -1227,14 +1225,15 @@  static void *mergeable_xdp_prepare(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,