Skip to contentSkip to conversation

xsk: support use vaddr as ring

PATCH New Xuan Zhuo 2 participants 2 messages Feb 9, 2023

Subsystems (4)
lore
Share & tools
lore

Xuan Zhuo

author
#0 lore raw

PATCH 1/1 xsk: support use vaddr as ring

When we try to start AF_XDP on some machines with long running time, due to the machine's memory fragmentation problem, there is no sufficient continuous physical memory that will cause the start failure.

After AF_XDP fails to apply for continuous physical memory, this patch tries to use vmalloc() to allocate memory to solve this problem.

---
 net/xdp/xsk.c       |  8 +++++---
 net/xdp/xsk_queue.c | 20 ++++++++++++++------
 net/xdp/xsk_queue.h |  1 +
 3 files changed, 20 insertions(+), 9 deletions(-)
net/xdp/xsk.c +5 -3
index 9f0561b67c12..33db57548ee3 100644
@@ -1296,7 +1296,6 @@ static int xsk_mmap(struct file *file, struct socket *sock,
 	struct xdp_sock *xs = xdp_sk(sock->sk);
 	struct xsk_queue *q = NULL;
 	unsigned long pfn;
-	struct page *qpg;
 
 	if (READ_ONCE(xs->state) != XSK_READY)
 		return -EBUSY;
@@ -1319,10 +1318,13 @@ static int xsk_mmap(struct file *file, struct socket *sock,
 
 	/* Matches the smp_wmb() in xsk_init_queue */
 	smp_rmb();
-	qpg = virt_to_head_page(q->ring);
-	if (size > page_size(qpg))
+
+	if (PAGE_ALIGN(q->ring_size) < size)
 		return -EINVAL;
 
+	if (is_vmalloc_addr(q->ring))
+		return remap_vmalloc_range(vma, q->ring, 0);
+
 	pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
 	return remap_pfn_range(vma, vma->vm_start, pfn,
 			       size, vma->vm_page_prot);
net/xdp/xsk_queue.c +14 -6
index 6cf9586e5027..6582d394b7e2 100644
@@ -37,14 +37,18 @@ struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
 		    __GFP_COMP  | __GFP_NORETRY;
 	size = xskq_get_ring_size(q, umem_queue);
 
+	q->ring_size = size;
 	q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
 						      get_order(size));
-	if (!q->ring) {
-		kfree(q);
-		return NULL;
-	}
+	if (q->ring)
+		return q;
+
+	q->ring = vmalloc_user(size);
+	if (q->ring)
+		return q;
 
-	return q;
+	kfree(q);
+	return NULL;
 }
 
 void xskq_destroy(struct xsk_queue *q)
@@ -52,6 +56,10 @@ void xskq_destroy(struct xsk_queue *q)
 	if (!q)
 		return;
 
-	page_frag_free(q->ring);
+	if (is_vmalloc_addr(q->ring))
+		vfree(q->ring);
+	else
+		page_frag_free(q->ring);
+
 	kfree(q);
 }
net/xdp/xsk_queue.h +1 -0
index c6fb6b763658..35922b8b92a8 100644
@@ -45,6 +45,7 @@ struct xsk_queue {
 	struct xdp_ring *ring;
 	u64 invalid_descs;
 	u64 queue_empty_descs;
+	size_t ring_size;
 };
 
 /* The structure of the shared state of the rings are a simple
Signature · 1 line
2.32.0.3.g01195cf9f
#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Xuan,

Thank you for the patch! Perhaps something to improve:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Xuan-Zhuo/xsk-support-use-vaddr-as-ring/20230209-172553
patch link:    https://lore.kernel.org/r/20230209092436.87795-1-xuanzhuo%40linux.alibaba.com
patch subject: [PATCH net-next] xsk: support use vaddr as ring
config: mips-allyesconfig (https://download.01.org/0day-ci/archive/20230209/[email protected]/config)
compiler: mips-linux-gcc (GCC) 12.1.0

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/4a7734a008a4c9739f0abcc596455e4b76b601ec
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Xuan-Zhuo/xsk-support-use-vaddr-as-ring/20230209-172553 git checkout 4a7734a008a4c9739f0abcc596455e4b76b601ec
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=mips olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash net/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]

All warnings (new ones prefixed by >>):

net/xdp/xsk_queue.c: In function 'xskq_create':
net/xdp/xsk_queue.c:46:19: error: implicit declaration of function 'vmalloc_user' [-Werror=implicit-function-declaration]

      46 |         q->ring = vmalloc_user(size);
         |                   ^~~~~~~~~~~~
>> net/xdp/xsk_queue.c:46:17: warning: assignment to 'struct xdp_ring *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
      46 |         q->ring = vmalloc_user(size);
         |                 ^
   net/xdp/xsk_queue.c: In function 'xskq_destroy':
   net/xdp/xsk_queue.c:60:17: error: implicit declaration of function 'vfree'; did you mean 'kvfree'? [-Werror=implicit-function-declaration]
      60 |                 vfree(q->ring);
         |                 ^~~~~
         |                 kvfree

cc1: some warnings being treated as errors

vim +46 net/xdp/xsk_queue.c

    22	
    23	struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
    24	{
    25		struct xsk_queue *q;
    26		gfp_t gfp_flags;
    27		size_t size;
    28	
    29		q = kzalloc(sizeof(*q), GFP_KERNEL);
    30		if (!q)
    31			return NULL;
    32	
    33		q->nentries = nentries;
    34		q->ring_mask = nentries - 1;
    35	
    36		gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
    37			    __GFP_COMP  | __GFP_NORETRY;
    38		size = xskq_get_ring_size(q, umem_queue);
    39	
    40		q->ring_size = size;
    41		q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
    42							      get_order(size));
    43		if (q->ring)
    44			return q;
    45	
  > 46		q->ring = vmalloc_user(size);
    47		if (q->ring)
    48			return q;
    49	
    50		kfree(q);
    51		return NULL;
    52	}
    53	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests