xref: /openbmc/linux/net/xdp/xsk_buff_pool.c (revision 0fcda0c9)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <net/xsk_buff_pool.h>
4 #include <net/xdp_sock.h>
5 #include <net/xdp_sock_drv.h>
6 
7 #include "xsk_queue.h"
8 #include "xdp_umem.h"
9 #include "xsk.h"
10 
xp_add_xsk(struct xsk_buff_pool * pool,struct xdp_sock * xs)11 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
12 {
13 	unsigned long flags;
14 
15 	if (!xs->tx)
16 		return;
17 
18 	spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
19 	list_add_rcu(&xs->tx_list, &pool->xsk_tx_list);
20 	spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
21 }
22 
xp_del_xsk(struct xsk_buff_pool * pool,struct xdp_sock * xs)23 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
24 {
25 	unsigned long flags;
26 
27 	if (!xs->tx)
28 		return;
29 
30 	spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
31 	list_del_rcu(&xs->tx_list);
32 	spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
33 }
34 
xp_destroy(struct xsk_buff_pool * pool)35 void xp_destroy(struct xsk_buff_pool *pool)
36 {
37 	if (!pool)
38 		return;
39 
40 	kvfree(pool->tx_descs);
41 	kvfree(pool->heads);
42 	kvfree(pool);
43 }
44 
xp_alloc_tx_descs(struct xsk_buff_pool * pool,struct xdp_sock * xs)45 int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs)
46 {
47 	pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs),
48 				  GFP_KERNEL);
49 	if (!pool->tx_descs)
50 		return -ENOMEM;
51 
52 	return 0;
53 }
54 
xp_create_and_assign_umem(struct xdp_sock * xs,struct xdp_umem * umem)55 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
56 						struct xdp_umem *umem)
57 {
58 	bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
59 	struct xsk_buff_pool *pool;
60 	struct xdp_buff_xsk *xskb;
61 	u32 i, entries;
62 
63 	entries = unaligned ? umem->chunks : 0;
64 	pool = kvzalloc(struct_size(pool, free_heads, entries),	GFP_KERNEL);
65 	if (!pool)
66 		goto out;
67 
68 	pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
69 	if (!pool->heads)
70 		goto out;
71 
72 	if (xs->tx)
73 		if (xp_alloc_tx_descs(pool, xs))
74 			goto out;
75 
76 	pool->chunk_mask = ~((u64)umem->chunk_size - 1);
77 	pool->addrs_cnt = umem->size;
78 	pool->heads_cnt = umem->chunks;
79 	pool->free_heads_cnt = umem->chunks;
80 	pool->headroom = umem->headroom;
81 	pool->chunk_size = umem->chunk_size;
82 	pool->chunk_shift = ffs(umem->chunk_size) - 1;
83 	pool->unaligned = unaligned;
84 	pool->frame_len = umem->chunk_size - umem->headroom -
85 		XDP_PACKET_HEADROOM;
86 	pool->umem = umem;
87 	pool->addrs = umem->addrs;
88 	INIT_LIST_HEAD(&pool->free_list);
89 	INIT_LIST_HEAD(&pool->xskb_list);
90 	INIT_LIST_HEAD(&pool->xsk_tx_list);
91 	spin_lock_init(&pool->xsk_tx_list_lock);
92 	spin_lock_init(&pool->cq_lock);
93 	refcount_set(&pool->users, 1);
94 
95 	pool->fq = xs->fq_tmp;
96 	pool->cq = xs->cq_tmp;
97 
98 	for (i = 0; i < pool->free_heads_cnt; i++) {
99 		xskb = &pool->heads[i];
100 		xskb->pool = pool;
101 		xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
102 		INIT_LIST_HEAD(&xskb->free_list_node);
103 		INIT_LIST_HEAD(&xskb->xskb_list_node);
104 		if (pool->unaligned)
105 			pool->free_heads[i] = xskb;
106 		else
107 			xp_init_xskb_addr(xskb, pool, i * pool->chunk_size);
108 	}
109 
110 	return pool;
111 
112 out:
113 	xp_destroy(pool);
114 	return NULL;
115 }
116 
xp_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)117 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
118 {
119 	u32 i;
120 
121 	for (i = 0; i < pool->heads_cnt; i++)
122 		pool->heads[i].xdp.rxq = rxq;
123 }
124 EXPORT_SYMBOL(xp_set_rxq_info);
125 
xp_disable_drv_zc(struct xsk_buff_pool * pool)126 static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
127 {
128 	struct netdev_bpf bpf;
129 	int err;
130 
131 	ASSERT_RTNL();
132 
133 	if (pool->umem->zc) {
134 		bpf.command = XDP_SETUP_XSK_POOL;
135 		bpf.xsk.pool = NULL;
136 		bpf.xsk.queue_id = pool->queue_id;
137 
138 		err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
139 
140 		if (err)
141 			WARN(1, "Failed to disable zero-copy!\n");
142 	}
143 }
144 
145 #define NETDEV_XDP_ACT_ZC	(NETDEV_XDP_ACT_BASIC |		\
146 				 NETDEV_XDP_ACT_REDIRECT |	\
147 				 NETDEV_XDP_ACT_XSK_ZEROCOPY)
148 
xp_assign_dev(struct xsk_buff_pool * pool,struct net_device * netdev,u16 queue_id,u16 flags)149 int xp_assign_dev(struct xsk_buff_pool *pool,
150 		  struct net_device *netdev, u16 queue_id, u16 flags)
151 {
152 	bool force_zc, force_copy;
153 	struct netdev_bpf bpf;
154 	int err = 0;
155 
156 	ASSERT_RTNL();
157 
158 	force_zc = flags & XDP_ZEROCOPY;
159 	force_copy = flags & XDP_COPY;
160 
161 	if (force_zc && force_copy)
162 		return -EINVAL;
163 
164 	if (xsk_get_pool_from_qid(netdev, queue_id))
165 		return -EBUSY;
166 
167 	pool->netdev = netdev;
168 	pool->queue_id = queue_id;
169 	err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
170 	if (err)
171 		return err;
172 
173 	if (flags & XDP_USE_SG)
174 		pool->umem->flags |= XDP_UMEM_SG_FLAG;
175 
176 	if (flags & XDP_USE_NEED_WAKEUP)
177 		pool->uses_need_wakeup = true;
178 	/* Tx needs to be explicitly woken up the first time.  Also
179 	 * for supporting drivers that do not implement this
180 	 * feature. They will always have to call sendto() or poll().
181 	 */
182 	pool->cached_need_wakeup = XDP_WAKEUP_TX;
183 
184 	dev_hold(netdev);
185 
186 	if (force_copy)
187 		/* For copy-mode, we are done. */
188 		return 0;
189 
190 	if ((netdev->xdp_features & NETDEV_XDP_ACT_ZC) != NETDEV_XDP_ACT_ZC) {
191 		err = -EOPNOTSUPP;
192 		goto err_unreg_pool;
193 	}
194 
195 	if (netdev->xdp_zc_max_segs == 1 && (flags & XDP_USE_SG)) {
196 		err = -EOPNOTSUPP;
197 		goto err_unreg_pool;
198 	}
199 
200 	bpf.command = XDP_SETUP_XSK_POOL;
201 	bpf.xsk.pool = pool;
202 	bpf.xsk.queue_id = queue_id;
203 
204 	err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
205 	if (err)
206 		goto err_unreg_pool;
207 
208 	if (!pool->dma_pages) {
209 		WARN(1, "Driver did not DMA map zero-copy buffers");
210 		err = -EINVAL;
211 		goto err_unreg_xsk;
212 	}
213 	pool->umem->zc = true;
214 	return 0;
215 
216 err_unreg_xsk:
217 	xp_disable_drv_zc(pool);
218 err_unreg_pool:
219 	if (!force_zc)
220 		err = 0; /* fallback to copy mode */
221 	if (err) {
222 		xsk_clear_pool_at_qid(netdev, queue_id);
223 		dev_put(netdev);
224 	}
225 	return err;
226 }
227 
xp_assign_dev_shared(struct xsk_buff_pool * pool,struct xdp_sock * umem_xs,struct net_device * dev,u16 queue_id)228 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
229 			 struct net_device *dev, u16 queue_id)
230 {
231 	u16 flags;
232 	struct xdp_umem *umem = umem_xs->umem;
233 
234 	/* One fill and completion ring required for each queue id. */
235 	if (!pool->fq || !pool->cq)
236 		return -EINVAL;
237 
238 	flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
239 	if (umem_xs->pool->uses_need_wakeup)
240 		flags |= XDP_USE_NEED_WAKEUP;
241 
242 	return xp_assign_dev(pool, dev, queue_id, flags);
243 }
244 
xp_clear_dev(struct xsk_buff_pool * pool)245 void xp_clear_dev(struct xsk_buff_pool *pool)
246 {
247 	if (!pool->netdev)
248 		return;
249 
250 	xp_disable_drv_zc(pool);
251 	xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
252 	dev_put(pool->netdev);
253 	pool->netdev = NULL;
254 }
255 
xp_release_deferred(struct work_struct * work)256 static void xp_release_deferred(struct work_struct *work)
257 {
258 	struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
259 						  work);
260 
261 	rtnl_lock();
262 	xp_clear_dev(pool);
263 	rtnl_unlock();
264 
265 	if (pool->fq) {
266 		xskq_destroy(pool->fq);
267 		pool->fq = NULL;
268 	}
269 
270 	if (pool->cq) {
271 		xskq_destroy(pool->cq);
272 		pool->cq = NULL;
273 	}
274 
275 	xdp_put_umem(pool->umem, false);
276 	xp_destroy(pool);
277 }
278 
xp_get_pool(struct xsk_buff_pool * pool)279 void xp_get_pool(struct xsk_buff_pool *pool)
280 {
281 	refcount_inc(&pool->users);
282 }
283 
xp_put_pool(struct xsk_buff_pool * pool)284 bool xp_put_pool(struct xsk_buff_pool *pool)
285 {
286 	if (!pool)
287 		return false;
288 
289 	if (refcount_dec_and_test(&pool->users)) {
290 		INIT_WORK(&pool->work, xp_release_deferred);
291 		schedule_work(&pool->work);
292 		return true;
293 	}
294 
295 	return false;
296 }
297 
xp_find_dma_map(struct xsk_buff_pool * pool)298 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
299 {
300 	struct xsk_dma_map *dma_map;
301 
302 	list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
303 		if (dma_map->netdev == pool->netdev)
304 			return dma_map;
305 	}
306 
307 	return NULL;
308 }
309 
xp_create_dma_map(struct device * dev,struct net_device * netdev,u32 nr_pages,struct xdp_umem * umem)310 static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
311 					     u32 nr_pages, struct xdp_umem *umem)
312 {
313 	struct xsk_dma_map *dma_map;
314 
315 	dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
316 	if (!dma_map)
317 		return NULL;
318 
319 	dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
320 	if (!dma_map->dma_pages) {
321 		kfree(dma_map);
322 		return NULL;
323 	}
324 
325 	dma_map->netdev = netdev;
326 	dma_map->dev = dev;
327 	dma_map->dma_need_sync = false;
328 	dma_map->dma_pages_cnt = nr_pages;
329 	refcount_set(&dma_map->users, 1);
330 	list_add(&dma_map->list, &umem->xsk_dma_list);
331 	return dma_map;
332 }
333 
xp_destroy_dma_map(struct xsk_dma_map * dma_map)334 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
335 {
336 	list_del(&dma_map->list);
337 	kvfree(dma_map->dma_pages);
338 	kfree(dma_map);
339 }
340 
__xp_dma_unmap(struct xsk_dma_map * dma_map,unsigned long attrs)341 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
342 {
343 	dma_addr_t *dma;
344 	u32 i;
345 
346 	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
347 		dma = &dma_map->dma_pages[i];
348 		if (*dma) {
349 			*dma &= ~XSK_NEXT_PG_CONTIG_MASK;
350 			dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
351 					     DMA_BIDIRECTIONAL, attrs);
352 			*dma = 0;
353 		}
354 	}
355 
356 	xp_destroy_dma_map(dma_map);
357 }
358 
xp_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)359 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
360 {
361 	struct xsk_dma_map *dma_map;
362 
363 	if (!pool->dma_pages)
364 		return;
365 
366 	dma_map = xp_find_dma_map(pool);
367 	if (!dma_map) {
368 		WARN(1, "Could not find dma_map for device");
369 		return;
370 	}
371 
372 	if (refcount_dec_and_test(&dma_map->users))
373 		__xp_dma_unmap(dma_map, attrs);
374 
375 	kvfree(pool->dma_pages);
376 	pool->dma_pages = NULL;
377 	pool->dma_pages_cnt = 0;
378 	pool->dev = NULL;
379 }
380 EXPORT_SYMBOL(xp_dma_unmap);
381 
xp_check_dma_contiguity(struct xsk_dma_map * dma_map)382 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
383 {
384 	u32 i;
385 
386 	for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
387 		if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
388 			dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
389 		else
390 			dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
391 	}
392 }
393 
xp_init_dma_info(struct xsk_buff_pool * pool,struct xsk_dma_map * dma_map)394 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
395 {
396 	if (!pool->unaligned) {
397 		u32 i;
398 
399 		for (i = 0; i < pool->heads_cnt; i++) {
400 			struct xdp_buff_xsk *xskb = &pool->heads[i];
401 
402 			xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
403 		}
404 	}
405 
406 	pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
407 	if (!pool->dma_pages)
408 		return -ENOMEM;
409 
410 	pool->dev = dma_map->dev;
411 	pool->dma_pages_cnt = dma_map->dma_pages_cnt;
412 	pool->dma_need_sync = dma_map->dma_need_sync;
413 	memcpy(pool->dma_pages, dma_map->dma_pages,
414 	       pool->dma_pages_cnt * sizeof(*pool->dma_pages));
415 
416 	return 0;
417 }
418 
xp_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs,struct page ** pages,u32 nr_pages)419 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
420 	       unsigned long attrs, struct page **pages, u32 nr_pages)
421 {
422 	struct xsk_dma_map *dma_map;
423 	dma_addr_t dma;
424 	int err;
425 	u32 i;
426 
427 	dma_map = xp_find_dma_map(pool);
428 	if (dma_map) {
429 		err = xp_init_dma_info(pool, dma_map);
430 		if (err)
431 			return err;
432 
433 		refcount_inc(&dma_map->users);
434 		return 0;
435 	}
436 
437 	dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
438 	if (!dma_map)
439 		return -ENOMEM;
440 
441 	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
442 		dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
443 					 DMA_BIDIRECTIONAL, attrs);
444 		if (dma_mapping_error(dev, dma)) {
445 			__xp_dma_unmap(dma_map, attrs);
446 			return -ENOMEM;
447 		}
448 		if (dma_need_sync(dev, dma))
449 			dma_map->dma_need_sync = true;
450 		dma_map->dma_pages[i] = dma;
451 	}
452 
453 	if (pool->unaligned)
454 		xp_check_dma_contiguity(dma_map);
455 
456 	err = xp_init_dma_info(pool, dma_map);
457 	if (err) {
458 		__xp_dma_unmap(dma_map, attrs);
459 		return err;
460 	}
461 
462 	return 0;
463 }
464 EXPORT_SYMBOL(xp_dma_map);
465 
xp_addr_crosses_non_contig_pg(struct xsk_buff_pool * pool,u64 addr)466 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
467 					  u64 addr)
468 {
469 	return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
470 }
471 
xp_check_unaligned(struct xsk_buff_pool * pool,u64 * addr)472 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
473 {
474 	*addr = xp_unaligned_extract_addr(*addr);
475 	if (*addr >= pool->addrs_cnt ||
476 	    *addr + pool->chunk_size > pool->addrs_cnt ||
477 	    xp_addr_crosses_non_contig_pg(pool, *addr))
478 		return false;
479 	return true;
480 }
481 
xp_check_aligned(struct xsk_buff_pool * pool,u64 * addr)482 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
483 {
484 	*addr = xp_aligned_extract_addr(pool, *addr);
485 	return *addr < pool->addrs_cnt;
486 }
487 
__xp_alloc(struct xsk_buff_pool * pool)488 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
489 {
490 	struct xdp_buff_xsk *xskb;
491 	u64 addr;
492 	bool ok;
493 
494 	if (pool->free_heads_cnt == 0)
495 		return NULL;
496 
497 	for (;;) {
498 		if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
499 			pool->fq->queue_empty_descs++;
500 			return NULL;
501 		}
502 
503 		ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
504 		     xp_check_aligned(pool, &addr);
505 		if (!ok) {
506 			pool->fq->invalid_descs++;
507 			xskq_cons_release(pool->fq);
508 			continue;
509 		}
510 		break;
511 	}
512 
513 	if (pool->unaligned) {
514 		xskb = pool->free_heads[--pool->free_heads_cnt];
515 		xp_init_xskb_addr(xskb, pool, addr);
516 		if (pool->dma_pages)
517 			xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
518 	} else {
519 		xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
520 	}
521 
522 	xskq_cons_release(pool->fq);
523 	return xskb;
524 }
525 
xp_alloc(struct xsk_buff_pool * pool)526 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
527 {
528 	struct xdp_buff_xsk *xskb;
529 
530 	if (!pool->free_list_cnt) {
531 		xskb = __xp_alloc(pool);
532 		if (!xskb)
533 			return NULL;
534 	} else {
535 		pool->free_list_cnt--;
536 		xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
537 					free_list_node);
538 		list_del_init(&xskb->free_list_node);
539 	}
540 
541 	xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
542 	xskb->xdp.data_meta = xskb->xdp.data;
543 	xskb->xdp.flags = 0;
544 
545 	if (pool->dma_need_sync) {
546 		dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
547 						 pool->frame_len,
548 						 DMA_BIDIRECTIONAL);
549 	}
550 	return &xskb->xdp;
551 }
552 EXPORT_SYMBOL(xp_alloc);
553 
xp_alloc_new_from_fq(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 max)554 static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
555 {
556 	u32 i, cached_cons, nb_entries;
557 
558 	if (max > pool->free_heads_cnt)
559 		max = pool->free_heads_cnt;
560 	max = xskq_cons_nb_entries(pool->fq, max);
561 
562 	cached_cons = pool->fq->cached_cons;
563 	nb_entries = max;
564 	i = max;
565 	while (i--) {
566 		struct xdp_buff_xsk *xskb;
567 		u64 addr;
568 		bool ok;
569 
570 		__xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
571 
572 		ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
573 			xp_check_aligned(pool, &addr);
574 		if (unlikely(!ok)) {
575 			pool->fq->invalid_descs++;
576 			nb_entries--;
577 			continue;
578 		}
579 
580 		if (pool->unaligned) {
581 			xskb = pool->free_heads[--pool->free_heads_cnt];
582 			xp_init_xskb_addr(xskb, pool, addr);
583 			if (pool->dma_pages)
584 				xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
585 		} else {
586 			xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
587 		}
588 
589 		*xdp = &xskb->xdp;
590 		xdp++;
591 	}
592 
593 	xskq_cons_release_n(pool->fq, max);
594 	return nb_entries;
595 }
596 
xp_alloc_reused(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 nb_entries)597 static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
598 {
599 	struct xdp_buff_xsk *xskb;
600 	u32 i;
601 
602 	nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
603 
604 	i = nb_entries;
605 	while (i--) {
606 		xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
607 		list_del_init(&xskb->free_list_node);
608 
609 		*xdp = &xskb->xdp;
610 		xdp++;
611 	}
612 	pool->free_list_cnt -= nb_entries;
613 
614 	return nb_entries;
615 }
616 
xp_alloc_batch(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 max)617 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
618 {
619 	u32 nb_entries1 = 0, nb_entries2;
620 
621 	if (unlikely(pool->dma_need_sync)) {
622 		struct xdp_buff *buff;
623 
624 		/* Slow path */
625 		buff = xp_alloc(pool);
626 		if (buff)
627 			*xdp = buff;
628 		return !!buff;
629 	}
630 
631 	if (unlikely(pool->free_list_cnt)) {
632 		nb_entries1 = xp_alloc_reused(pool, xdp, max);
633 		if (nb_entries1 == max)
634 			return nb_entries1;
635 
636 		max -= nb_entries1;
637 		xdp += nb_entries1;
638 	}
639 
640 	nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
641 	if (!nb_entries2)
642 		pool->fq->queue_empty_descs++;
643 
644 	return nb_entries1 + nb_entries2;
645 }
646 EXPORT_SYMBOL(xp_alloc_batch);
647 
xp_can_alloc(struct xsk_buff_pool * pool,u32 count)648 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
649 {
650 	if (pool->free_list_cnt >= count)
651 		return true;
652 	return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
653 }
654 EXPORT_SYMBOL(xp_can_alloc);
655 
xp_free(struct xdp_buff_xsk * xskb)656 void xp_free(struct xdp_buff_xsk *xskb)
657 {
658 	if (!list_empty(&xskb->free_list_node))
659 		return;
660 
661 	xskb->pool->free_list_cnt++;
662 	list_add(&xskb->free_list_node, &xskb->pool->free_list);
663 }
664 EXPORT_SYMBOL(xp_free);
665 
xp_raw_get_data(struct xsk_buff_pool * pool,u64 addr)666 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
667 {
668 	addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
669 	return pool->addrs + addr;
670 }
671 EXPORT_SYMBOL(xp_raw_get_data);
672 
xp_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)673 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
674 {
675 	addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
676 	return (pool->dma_pages[addr >> PAGE_SHIFT] &
677 		~XSK_NEXT_PG_CONTIG_MASK) +
678 		(addr & ~PAGE_MASK);
679 }
680 EXPORT_SYMBOL(xp_raw_get_dma);
681 
xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk * xskb)682 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
683 {
684 	dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
685 				      xskb->pool->frame_len, DMA_BIDIRECTIONAL);
686 }
687 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
688 
xp_dma_sync_for_device_slow(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)689 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
690 				 size_t size)
691 {
692 	dma_sync_single_range_for_device(pool->dev, dma, 0,
693 					 size, DMA_BIDIRECTIONAL);
694 }
695 EXPORT_SYMBOL(xp_dma_sync_for_device_slow);
696