diff mbox series

[net-next,8/8] virtio_net: introduce receive_small_xdp()

Message ID 20230322030308.16046-9-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 fail Errors and warnings before: 23 this patch: 25
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 warning WARNING: line length of 95 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Xuan Zhuo March 22, 2023, 3:03 a.m. UTC
The purpose of this patch is to simplify the receive_small().
Separate all the logic of XDP of small into a function.

Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
 drivers/net/virtio_net.c | 165 +++++++++++++++++++++++----------------
 1 file changed, 97 insertions(+), 68 deletions(-)

Comments

kernel test robot March 23, 2023, 2:06 a.m. UTC | #1
Hi Xuan,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Xuan-Zhuo/virtio_net-mergeable-xdp-put-old-page-immediately/20230322-110445
patch link:    https://lore.kernel.org/r/20230322030308.16046-9-xuanzhuo%40linux.alibaba.com
patch subject: [PATCH net-next 8/8] virtio_net: introduce receive_small_xdp()
config: i386-randconfig-a002 (https://download.01.org/0day-ci/archive/20230323/202303230953.y0P1e1Fc-lkp@intel.com/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/6445923dc4c91e57f98b8356d0bd706e95dafaa1
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Xuan-Zhuo/virtio_net-mergeable-xdp-put-old-page-immediately/20230322-110445
        git checkout 6445923dc4c91e57f98b8356d0bd706e95dafaa1
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/net/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202303230953.y0P1e1Fc-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/net/virtio_net.c:969:6: warning: variable 'buflen' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
           if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:78:22: note: expanded from macro 'unlikely'
   # define unlikely(x)    __builtin_expect(!!(x), 0)
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:990:22: note: uninitialized use occurs here
           xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
                               ^~~~~~
   drivers/net/virtio_net.c:969:2: note: remove the 'if' if its condition is always true
           if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:958:21: note: initialize the variable 'buflen' to silence this warning
           unsigned int buflen;
                              ^
                               = 0
   drivers/net/virtio_net.c:1428:6: warning: variable 'page' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
           if (unlikely(len > truesize - room)) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:78:22: note: expanded from macro 'unlikely'
   # define unlikely(x)    __builtin_expect(!!(x), 0)
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:1519:11: note: uninitialized use occurs here
           put_page(page);
                    ^~~~
   drivers/net/virtio_net.c:1428:2: note: remove the 'if' if its condition is always false
           if (unlikely(len > truesize - room)) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:1421:19: note: initialize the variable 'page' to silence this warning
           struct page *page;
                            ^
                             = NULL
   drivers/net/virtio_net.c:1428:6: warning: variable 'num_buf' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
           if (unlikely(len > truesize - room)) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:78:22: note: expanded from macro 'unlikely'
   # define unlikely(x)    __builtin_expect(!!(x), 0)
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:1520:25: note: uninitialized use occurs here
           mergeable_buf_free(rq, num_buf, dev, stats);
                                  ^~~~~~~
   drivers/net/virtio_net.c:1428:2: note: remove the 'if' if its condition is always false
           if (unlikely(len > truesize - room)) {
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/net/virtio_net.c:1422:13: note: initialize the variable 'num_buf' to silence this warning
           int num_buf;
                      ^
                       = 0
   3 warnings generated.


vim +969 drivers/net/virtio_net.c

4941d472bf95b4 Jason Wang             2017-07-19   941  
6445923dc4c91e Xuan Zhuo              2023-03-22   942  static struct sk_buff *receive_small_xdp(struct net_device *dev,
bb91accf27335c Jason Wang             2016-12-23   943  					 struct virtnet_info *vi,
bb91accf27335c Jason Wang             2016-12-23   944  					 struct receive_queue *rq,
6445923dc4c91e Xuan Zhuo              2023-03-22   945  					 struct bpf_prog *xdp_prog,
6445923dc4c91e Xuan Zhuo              2023-03-22   946  					 void *buf,
6445923dc4c91e Xuan Zhuo              2023-03-22   947  					 void *ctx,
186b3c998c50fc Jason Wang             2017-09-19   948  					 unsigned int len,
7d9d60fd4ab696 Toshiaki Makita        2018-07-23   949  					 unsigned int *xdp_xmit,
d46eeeaf99bcfa Jason Wang             2018-07-31   950  					 struct virtnet_rq_stats *stats)
f121159d72091f Michael S. Tsirkin     2013-11-28   951  {
4941d472bf95b4 Jason Wang             2017-07-19   952  	unsigned int xdp_headroom = (unsigned long)ctx;
f6b10209b90d48 Jason Wang             2017-02-21   953  	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
f6b10209b90d48 Jason Wang             2017-02-21   954  	unsigned int headroom = vi->hdr_len + header_offset;
6445923dc4c91e Xuan Zhuo              2023-03-22   955  	struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
4941d472bf95b4 Jason Wang             2017-07-19   956  	struct page *page = virt_to_head_page(buf);
4941d472bf95b4 Jason Wang             2017-07-19   957  	struct page *xdp_page;
6445923dc4c91e Xuan Zhuo              2023-03-22   958  	unsigned int buflen;
0354e4d19cd5de John Fastabend         2017-02-02   959  	struct xdp_buff xdp;
6445923dc4c91e Xuan Zhuo              2023-03-22   960  	struct sk_buff *skb;
6445923dc4c91e Xuan Zhuo              2023-03-22   961  	unsigned int delta = 0;
6445923dc4c91e Xuan Zhuo              2023-03-22   962  	unsigned int metasize = 0;
f6b10209b90d48 Jason Wang             2017-02-21   963  	void *orig_data;
bb91accf27335c Jason Wang             2016-12-23   964  	u32 act;
bb91accf27335c Jason Wang             2016-12-23   965  
95dbe9e7b3720e Jesper Dangaard Brouer 2018-02-20   966  	if (unlikely(hdr->hdr.gso_type))
bb91accf27335c Jason Wang             2016-12-23   967  		goto err_xdp;
0354e4d19cd5de John Fastabend         2017-02-02   968  
4941d472bf95b4 Jason Wang             2017-07-19  @969  	if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
4941d472bf95b4 Jason Wang             2017-07-19   970  		int offset = buf - page_address(page) + header_offset;
4941d472bf95b4 Jason Wang             2017-07-19   971  		unsigned int tlen = len + vi->hdr_len;
981f14d42a7f16 Heng Qi                2023-01-31   972  		int num_buf = 1;
4941d472bf95b4 Jason Wang             2017-07-19   973  
4941d472bf95b4 Jason Wang             2017-07-19   974  		xdp_headroom = virtnet_get_headroom(vi);
4941d472bf95b4 Jason Wang             2017-07-19   975  		header_offset = VIRTNET_RX_PAD + xdp_headroom;
4941d472bf95b4 Jason Wang             2017-07-19   976  		headroom = vi->hdr_len + header_offset;
4941d472bf95b4 Jason Wang             2017-07-19   977  		buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
4941d472bf95b4 Jason Wang             2017-07-19   978  			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4941d472bf95b4 Jason Wang             2017-07-19   979  		xdp_page = xdp_linearize_page(rq, &num_buf, page,
4941d472bf95b4 Jason Wang             2017-07-19   980  					      offset, header_offset,
4941d472bf95b4 Jason Wang             2017-07-19   981  					      &tlen);
4941d472bf95b4 Jason Wang             2017-07-19   982  		if (!xdp_page)
4941d472bf95b4 Jason Wang             2017-07-19   983  			goto err_xdp;
4941d472bf95b4 Jason Wang             2017-07-19   984  
4941d472bf95b4 Jason Wang             2017-07-19   985  		buf = page_address(xdp_page);
4941d472bf95b4 Jason Wang             2017-07-19   986  		put_page(page);
4941d472bf95b4 Jason Wang             2017-07-19   987  		page = xdp_page;
4941d472bf95b4 Jason Wang             2017-07-19   988  	}
4941d472bf95b4 Jason Wang             2017-07-19   989  
43b5169d8355cc Lorenzo Bianconi       2020-12-22   990  	xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
be9df4aff65f18 Lorenzo Bianconi       2020-12-22   991  	xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
be9df4aff65f18 Lorenzo Bianconi       2020-12-22   992  			 xdp_headroom, len, true);
f6b10209b90d48 Jason Wang             2017-02-21   993  	orig_data = xdp.data;
4249e276b1ab30 Xuan Zhuo              2023-03-22   994  
4249e276b1ab30 Xuan Zhuo              2023-03-22   995  	act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
0354e4d19cd5de John Fastabend         2017-02-02   996  
bb91accf27335c Jason Wang             2016-12-23   997  	switch (act) {
4249e276b1ab30 Xuan Zhuo              2023-03-22   998  	case VIRTNET_XDP_RES_PASS:
2de2f7f40ef923 John Fastabend         2017-02-02   999  		/* Recalculate length in case bpf program changed it */
f6b10209b90d48 Jason Wang             2017-02-21  1000  		delta = orig_data - xdp.data;
6870de435b90c0 Nikita V. Shirokov     2018-04-17  1001  		len = xdp.data_end - xdp.data;
503d539a6e417b Yuya Kusakabe          2020-02-25  1002  		metasize = xdp.data - xdp.data_meta;
bb91accf27335c Jason Wang             2016-12-23  1003  		break;
4249e276b1ab30 Xuan Zhuo              2023-03-22  1004  
4249e276b1ab30 Xuan Zhuo              2023-03-22  1005  	case VIRTNET_XDP_RES_CONSUMED:
bb91accf27335c Jason Wang             2016-12-23  1006  		goto xdp_xmit;
4249e276b1ab30 Xuan Zhuo              2023-03-22  1007  
4249e276b1ab30 Xuan Zhuo              2023-03-22  1008  	case VIRTNET_XDP_RES_DROP:
bb91accf27335c Jason Wang             2016-12-23  1009  		goto err_xdp;
bb91accf27335c Jason Wang             2016-12-23  1010  	}
bb91accf27335c Jason Wang             2016-12-23  1011  
f6b10209b90d48 Jason Wang             2017-02-21  1012  	skb = build_skb(buf, buflen);
053c9e18c6f9cf Wenliang Wang          2021-12-16  1013  	if (!skb)
f6b10209b90d48 Jason Wang             2017-02-21  1014  		goto err;
6445923dc4c91e Xuan Zhuo              2023-03-22  1015  
f6b10209b90d48 Jason Wang             2017-02-21  1016  	skb_reserve(skb, headroom - delta);
6870de435b90c0 Nikita V. Shirokov     2018-04-17  1017  	skb_put(skb, len);
503d539a6e417b Yuya Kusakabe          2020-02-25  1018  	if (metasize)
503d539a6e417b Yuya Kusakabe          2020-02-25  1019  		skb_metadata_set(skb, metasize);
503d539a6e417b Yuya Kusakabe          2020-02-25  1020  
f121159d72091f Michael S. Tsirkin     2013-11-28  1021  	return skb;
bb91accf27335c Jason Wang             2016-12-23  1022  
bb91accf27335c Jason Wang             2016-12-23  1023  err_xdp:
d46eeeaf99bcfa Jason Wang             2018-07-31  1024  	stats->xdp_drops++;
053c9e18c6f9cf Wenliang Wang          2021-12-16  1025  err:
d46eeeaf99bcfa Jason Wang             2018-07-31  1026  	stats->drops++;
4941d472bf95b4 Jason Wang             2017-07-19  1027  	put_page(page);
bb91accf27335c Jason Wang             2016-12-23  1028  xdp_xmit:
bb91accf27335c Jason Wang             2016-12-23  1029  	return NULL;
f121159d72091f Michael S. Tsirkin     2013-11-28  1030  }
f121159d72091f Michael S. Tsirkin     2013-11-28  1031
diff mbox series

Patch

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 6ecb17e972e1..23b1a6fd1224 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -939,6 +939,96 @@  static struct page *xdp_linearize_page(struct receive_queue *rq,
 	return NULL;
 }
 
+static struct sk_buff *receive_small_xdp(struct net_device *dev,
+					 struct virtnet_info *vi,
+					 struct receive_queue *rq,
+					 struct bpf_prog *xdp_prog,
+					 void *buf,
+					 void *ctx,
+					 unsigned int len,
+					 unsigned int *xdp_xmit,
+					 struct virtnet_rq_stats *stats)
+{
+	unsigned int xdp_headroom = (unsigned long)ctx;
+	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
+	unsigned int headroom = vi->hdr_len + header_offset;
+	struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
+	struct page *page = virt_to_head_page(buf);
+	struct page *xdp_page;
+	unsigned int buflen;
+	struct xdp_buff xdp;
+	struct sk_buff *skb;
+	unsigned int delta = 0;
+	unsigned int metasize = 0;
+	void *orig_data;
+	u32 act;
+
+	if (unlikely(hdr->hdr.gso_type))
+		goto err_xdp;
+
+	if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
+		int offset = buf - page_address(page) + header_offset;
+		unsigned int tlen = len + vi->hdr_len;
+		int num_buf = 1;
+
+		xdp_headroom = virtnet_get_headroom(vi);
+		header_offset = VIRTNET_RX_PAD + xdp_headroom;
+		headroom = vi->hdr_len + header_offset;
+		buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
+			SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
+		xdp_page = xdp_linearize_page(rq, &num_buf, page,
+					      offset, header_offset,
+					      &tlen);
+		if (!xdp_page)
+			goto err_xdp;
+
+		buf = page_address(xdp_page);
+		put_page(page);
+		page = xdp_page;
+	}
+
+	xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
+	xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
+			 xdp_headroom, len, true);
+	orig_data = xdp.data;
+
+	act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
+
+	switch (act) {
+	case VIRTNET_XDP_RES_PASS:
+		/* Recalculate length in case bpf program changed it */
+		delta = orig_data - xdp.data;
+		len = xdp.data_end - xdp.data;
+		metasize = xdp.data - xdp.data_meta;
+		break;
+
+	case VIRTNET_XDP_RES_CONSUMED:
+		goto xdp_xmit;
+
+	case VIRTNET_XDP_RES_DROP:
+		goto err_xdp;
+	}
+
+	skb = build_skb(buf, buflen);
+	if (!skb)
+		goto err;
+
+	skb_reserve(skb, headroom - delta);
+	skb_put(skb, len);
+	if (metasize)
+		skb_metadata_set(skb, metasize);
+
+	return skb;
+
+err_xdp:
+	stats->xdp_drops++;
+err:
+	stats->drops++;
+	put_page(page);
+xdp_xmit:
+	return NULL;
+}
+
 static struct sk_buff *receive_small(struct net_device *dev,
 				     struct virtnet_info *vi,
 				     struct receive_queue *rq,
@@ -949,15 +1039,11 @@  static struct sk_buff *receive_small(struct net_device *dev,
 {
 	struct sk_buff *skb;
 	struct bpf_prog *xdp_prog;
-	unsigned int xdp_headroom = (unsigned long)ctx;
-	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
+	unsigned int header_offset = VIRTNET_RX_PAD;
 	unsigned int headroom = vi->hdr_len + header_offset;
 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 	struct page *page = virt_to_head_page(buf);
-	unsigned int delta = 0;
-	struct page *xdp_page;
-	unsigned int metasize = 0;
 
 	len -= vi->hdr_len;
 	stats->bytes += len;
@@ -977,57 +1063,9 @@  static struct sk_buff *receive_small(struct net_device *dev,
 	rcu_read_lock();
 	xdp_prog = rcu_dereference(rq->xdp_prog);
 	if (xdp_prog) {
-		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
-		struct xdp_buff xdp;
-		void *orig_data;
-		u32 act;
-
-		if (unlikely(hdr->hdr.gso_type))
-			goto err_xdp;
-
-		if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
-			int offset = buf - page_address(page) + header_offset;
-			unsigned int tlen = len + vi->hdr_len;
-			int num_buf = 1;
-
-			xdp_headroom = virtnet_get_headroom(vi);
-			header_offset = VIRTNET_RX_PAD + xdp_headroom;
-			headroom = vi->hdr_len + header_offset;
-			buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
-				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
-			xdp_page = xdp_linearize_page(rq, &num_buf, page,
-						      offset, header_offset,
-						      &tlen);
-			if (!xdp_page)
-				goto err_xdp;
-
-			buf = page_address(xdp_page);
-			put_page(page);
-			page = xdp_page;
-		}
-
-		xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
-		xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
-				 xdp_headroom, len, true);
-		orig_data = xdp.data;
-
-		act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
-
-		switch (act) {
-		case VIRTNET_XDP_RES_PASS:
-			/* Recalculate length in case bpf program changed it */
-			delta = orig_data - xdp.data;
-			len = xdp.data_end - xdp.data;
-			metasize = xdp.data - xdp.data_meta;
-			break;
-
-		case VIRTNET_XDP_RES_CONSUMED:
-			rcu_read_unlock();
-			goto xdp_xmit;
-
-		case VIRTNET_XDP_RES_DROP:
-			goto err_xdp;
-		}
+		skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf, ctx, len, xdp_xmit, stats);
+		rcu_read_unlock();
+		return skb;
 	}
 	rcu_read_unlock();
 
@@ -1035,25 +1073,16 @@  static struct sk_buff *receive_small(struct net_device *dev,
 	skb = build_skb(buf, buflen);
 	if (!skb)
 		goto err;
-	skb_reserve(skb, headroom - delta);
+	skb_reserve(skb, headroom);
 	skb_put(skb, len);
-	if (!xdp_prog) {
-		buf += header_offset;
-		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
-	} /* keep zeroed vnet hdr since XDP is loaded */
-
-	if (metasize)
-		skb_metadata_set(skb, metasize);
 
+	buf += header_offset;
+	memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
 	return skb;
 
-err_xdp:
-	rcu_read_unlock();
-	stats->xdp_drops++;
 err:
 	stats->drops++;
 	put_page(page);
-xdp_xmit:
 	return NULL;
 }