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