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_umem * umem,struct net_device * dev,u16 queue_id)215 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
216 			 struct net_device *dev, u16 queue_id)
217 {
218 	u16 flags;
219 
220 	/* One fill and completion ring required for each queue id. */
221 	if (!pool->fq || !pool->cq)
222 		return -EINVAL;
223 
224 	flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
225 	if (pool->uses_need_wakeup)
226 		flags |= XDP_USE_NEED_WAKEUP;
227 
228 	return xp_assign_dev(pool, dev, queue_id, flags);
229 }
230 
xp_clear_dev(struct xsk_buff_pool * pool)231 void xp_clear_dev(struct xsk_buff_pool *pool)
232 {
233 	if (!pool->netdev)
234 		return;
235 
236 	xp_disable_drv_zc(pool);
237 	xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
238 	dev_put(pool->netdev);
239 	pool->netdev = NULL;
240 }
241 
xp_release_deferred(struct work_struct * work)242 static void xp_release_deferred(struct work_struct *work)
243 {
244 	struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
245 						  work);
246 
247 	rtnl_lock();
248 	xp_clear_dev(pool);
249 	rtnl_unlock();
250 
251 	if (pool->fq) {
252 		xskq_destroy(pool->fq);
253 		pool->fq = NULL;
254 	}
255 
256 	if (pool->cq) {
257 		xskq_destroy(pool->cq);
258 		pool->cq = NULL;
259 	}
260 
261 	xdp_put_umem(pool->umem, false);
262 	xp_destroy(pool);
263 }
264 
xp_get_pool(struct xsk_buff_pool * pool)265 void xp_get_pool(struct xsk_buff_pool *pool)
266 {
267 	refcount_inc(&pool->users);
268 }
269 
xp_put_pool(struct xsk_buff_pool * pool)270 bool xp_put_pool(struct xsk_buff_pool *pool)
271 {
272 	if (!pool)
273 		return false;
274 
275 	if (refcount_dec_and_test(&pool->users)) {
276 		INIT_WORK(&pool->work, xp_release_deferred);
277 		schedule_work(&pool->work);
278 		return true;
279 	}
280 
281 	return false;
282 }
283 
xp_find_dma_map(struct xsk_buff_pool * pool)284 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
285 {
286 	struct xsk_dma_map *dma_map;
287 
288 	list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
289 		if (dma_map->netdev == pool->netdev)
290 			return dma_map;
291 	}
292 
293 	return NULL;
294 }
295 
xp_create_dma_map(struct device * dev,struct net_device * netdev,u32 nr_pages,struct xdp_umem * umem)296 static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
297 					     u32 nr_pages, struct xdp_umem *umem)
298 {
299 	struct xsk_dma_map *dma_map;
300 
301 	dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
302 	if (!dma_map)
303 		return NULL;
304 
305 	dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
306 	if (!dma_map->dma_pages) {
307 		kfree(dma_map);
308 		return NULL;
309 	}
310 
311 	dma_map->netdev = netdev;
312 	dma_map->dev = dev;
313 	dma_map->dma_need_sync = false;
314 	dma_map->dma_pages_cnt = nr_pages;
315 	refcount_set(&dma_map->users, 1);
316 	list_add(&dma_map->list, &umem->xsk_dma_list);
317 	return dma_map;
318 }
319 
xp_destroy_dma_map(struct xsk_dma_map * dma_map)320 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
321 {
322 	list_del(&dma_map->list);
323 	kvfree(dma_map->dma_pages);
324 	kfree(dma_map);
325 }
326 
__xp_dma_unmap(struct xsk_dma_map * dma_map,unsigned long attrs)327 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
328 {
329 	dma_addr_t *dma;
330 	u32 i;
331 
332 	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
333 		dma = &dma_map->dma_pages[i];
334 		if (*dma) {
335 			*dma &= ~XSK_NEXT_PG_CONTIG_MASK;
336 			dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
337 					     DMA_BIDIRECTIONAL, attrs);
338 			*dma = 0;
339 		}
340 	}
341 
342 	xp_destroy_dma_map(dma_map);
343 }
344 
xp_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)345 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
346 {
347 	struct xsk_dma_map *dma_map;
348 
349 	if (pool->dma_pages_cnt == 0)
350 		return;
351 
352 	dma_map = xp_find_dma_map(pool);
353 	if (!dma_map) {
354 		WARN(1, "Could not find dma_map for device");
355 		return;
356 	}
357 
358 	if (!refcount_dec_and_test(&dma_map->users))
359 		return;
360 
361 	__xp_dma_unmap(dma_map, attrs);
362 	kvfree(pool->dma_pages);
363 	pool->dma_pages_cnt = 0;
364 	pool->dev = NULL;
365 }
366 EXPORT_SYMBOL(xp_dma_unmap);
367 
xp_check_dma_contiguity(struct xsk_dma_map * dma_map)368 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
369 {
370 	u32 i;
371 
372 	for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
373 		if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
374 			dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
375 		else
376 			dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
377 	}
378 }
379 
xp_init_dma_info(struct xsk_buff_pool * pool,struct xsk_dma_map * dma_map)380 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
381 {
382 	if (!pool->unaligned) {
383 		u32 i;
384 
385 		for (i = 0; i < pool->heads_cnt; i++) {
386 			struct xdp_buff_xsk *xskb = &pool->heads[i];
387 
388 			xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
389 		}
390 	}
391 
392 	pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
393 	if (!pool->dma_pages)
394 		return -ENOMEM;
395 
396 	pool->dev = dma_map->dev;
397 	pool->dma_pages_cnt = dma_map->dma_pages_cnt;
398 	pool->dma_need_sync = dma_map->dma_need_sync;
399 	memcpy(pool->dma_pages, dma_map->dma_pages,
400 	       pool->dma_pages_cnt * sizeof(*pool->dma_pages));
401 
402 	return 0;
403 }
404 
xp_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs,struct page ** pages,u32 nr_pages)405 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
406 	       unsigned long attrs, struct page **pages, u32 nr_pages)
407 {
408 	struct xsk_dma_map *dma_map;
409 	dma_addr_t dma;
410 	int err;
411 	u32 i;
412 
413 	dma_map = xp_find_dma_map(pool);
414 	if (dma_map) {
415 		err = xp_init_dma_info(pool, dma_map);
416 		if (err)
417 			return err;
418 
419 		refcount_inc(&dma_map->users);
420 		return 0;
421 	}
422 
423 	dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
424 	if (!dma_map)
425 		return -ENOMEM;
426 
427 	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
428 		dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
429 					 DMA_BIDIRECTIONAL, attrs);
430 		if (dma_mapping_error(dev, dma)) {
431 			__xp_dma_unmap(dma_map, attrs);
432 			return -ENOMEM;
433 		}
434 		if (dma_need_sync(dev, dma))
435 			dma_map->dma_need_sync = true;
436 		dma_map->dma_pages[i] = dma;
437 	}
438 
439 	if (pool->unaligned)
440 		xp_check_dma_contiguity(dma_map);
441 
442 	err = xp_init_dma_info(pool, dma_map);
443 	if (err) {
444 		__xp_dma_unmap(dma_map, attrs);
445 		return err;
446 	}
447 
448 	return 0;
449 }
450 EXPORT_SYMBOL(xp_dma_map);
451 
xp_addr_crosses_non_contig_pg(struct xsk_buff_pool * pool,u64 addr)452 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
453 					  u64 addr)
454 {
455 	return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
456 }
457 
xp_check_unaligned(struct xsk_buff_pool * pool,u64 * addr)458 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
459 {
460 	*addr = xp_unaligned_extract_addr(*addr);
461 	if (*addr >= pool->addrs_cnt ||
462 	    *addr + pool->chunk_size > pool->addrs_cnt ||
463 	    xp_addr_crosses_non_contig_pg(pool, *addr))
464 		return false;
465 	return true;
466 }
467 
xp_check_aligned(struct xsk_buff_pool * pool,u64 * addr)468 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
469 {
470 	*addr = xp_aligned_extract_addr(pool, *addr);
471 	return *addr < pool->addrs_cnt;
472 }
473 
__xp_alloc(struct xsk_buff_pool * pool)474 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
475 {
476 	struct xdp_buff_xsk *xskb;
477 	u64 addr;
478 	bool ok;
479 
480 	if (pool->free_heads_cnt == 0)
481 		return NULL;
482 
483 	for (;;) {
484 		if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
485 			pool->fq->queue_empty_descs++;
486 			return NULL;
487 		}
488 
489 		ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
490 		     xp_check_aligned(pool, &addr);
491 		if (!ok) {
492 			pool->fq->invalid_descs++;
493 			xskq_cons_release(pool->fq);
494 			continue;
495 		}
496 		break;
497 	}
498 
499 	if (pool->unaligned) {
500 		xskb = pool->free_heads[--pool->free_heads_cnt];
501 		xp_init_xskb_addr(xskb, pool, addr);
502 		if (pool->dma_pages_cnt)
503 			xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
504 	} else {
505 		xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
506 	}
507 
508 	xskq_cons_release(pool->fq);
509 	return xskb;
510 }
511 
xp_alloc(struct xsk_buff_pool * pool)512 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
513 {
514 	struct xdp_buff_xsk *xskb;
515 
516 	if (!pool->free_list_cnt) {
517 		xskb = __xp_alloc(pool);
518 		if (!xskb)
519 			return NULL;
520 	} else {
521 		pool->free_list_cnt--;
522 		xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
523 					free_list_node);
524 		list_del_init(&xskb->free_list_node);
525 	}
526 
527 	xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
528 	xskb->xdp.data_meta = xskb->xdp.data;
529 
530 	if (pool->dma_need_sync) {
531 		dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
532 						 pool->frame_len,
533 						 DMA_BIDIRECTIONAL);
534 	}
535 	return &xskb->xdp;
536 }
537 EXPORT_SYMBOL(xp_alloc);
538 
xp_alloc_new_from_fq(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 max)539 static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
540 {
541 	u32 i, cached_cons, nb_entries;
542 
543 	if (max > pool->free_heads_cnt)
544 		max = pool->free_heads_cnt;
545 	max = xskq_cons_nb_entries(pool->fq, max);
546 
547 	cached_cons = pool->fq->cached_cons;
548 	nb_entries = max;
549 	i = max;
550 	while (i--) {
551 		struct xdp_buff_xsk *xskb;
552 		u64 addr;
553 		bool ok;
554 
555 		__xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
556 
557 		ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
558 			xp_check_aligned(pool, &addr);
559 		if (unlikely(!ok)) {
560 			pool->fq->invalid_descs++;
561 			nb_entries--;
562 			continue;
563 		}
564 
565 		if (pool->unaligned) {
566 			xskb = pool->free_heads[--pool->free_heads_cnt];
567 			xp_init_xskb_addr(xskb, pool, addr);
568 			if (pool->dma_pages_cnt)
569 				xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
570 		} else {
571 			xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
572 		}
573 
574 		*xdp = &xskb->xdp;
575 		xdp++;
576 	}
577 
578 	xskq_cons_release_n(pool->fq, max);
579 	return nb_entries;
580 }
581 
xp_alloc_reused(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 nb_entries)582 static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
583 {
584 	struct xdp_buff_xsk *xskb;
585 	u32 i;
586 
587 	nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
588 
589 	i = nb_entries;
590 	while (i--) {
591 		xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
592 		list_del_init(&xskb->free_list_node);
593 
594 		*xdp = &xskb->xdp;
595 		xdp++;
596 	}
597 	pool->free_list_cnt -= nb_entries;
598 
599 	return nb_entries;
600 }
601 
xp_alloc_batch(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 max)602 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
603 {
604 	u32 nb_entries1 = 0, nb_entries2;
605 
606 	if (unlikely(pool->dma_need_sync)) {
607 		struct xdp_buff *buff;
608 
609 		/* Slow path */
610 		buff = xp_alloc(pool);
611 		if (buff)
612 			*xdp = buff;
613 		return !!buff;
614 	}
615 
616 	if (unlikely(pool->free_list_cnt)) {
617 		nb_entries1 = xp_alloc_reused(pool, xdp, max);
618 		if (nb_entries1 == max)
619 			return nb_entries1;
620 
621 		max -= nb_entries1;
622 		xdp += nb_entries1;
623 	}
624 
625 	nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
626 	if (!nb_entries2)
627 		pool->fq->queue_empty_descs++;
628 
629 	return nb_entries1 + nb_entries2;
630 }
631 EXPORT_SYMBOL(xp_alloc_batch);
632 
xp_can_alloc(struct xsk_buff_pool * pool,u32 count)633 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
634 {
635 	if (pool->free_list_cnt >= count)
636 		return true;
637 	return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
638 }
639 EXPORT_SYMBOL(xp_can_alloc);
640 
xp_free(struct xdp_buff_xsk * xskb)641 void xp_free(struct xdp_buff_xsk *xskb)
642 {
643 	if (!list_empty(&xskb->free_list_node))
644 		return;
645 
646 	xskb->pool->free_list_cnt++;
647 	list_add(&xskb->free_list_node, &xskb->pool->free_list);
648 }
649 EXPORT_SYMBOL(xp_free);
650 
xp_raw_get_data(struct xsk_buff_pool * pool,u64 addr)651 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
652 {
653 	addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
654 	return pool->addrs + addr;
655 }
656 EXPORT_SYMBOL(xp_raw_get_data);
657 
xp_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)658 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
659 {
660 	addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
661 	return (pool->dma_pages[addr >> PAGE_SHIFT] &
662 		~XSK_NEXT_PG_CONTIG_MASK) +
663 		(addr & ~PAGE_MASK);
664 }
665 EXPORT_SYMBOL(xp_raw_get_dma);
666 
xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk * xskb)667 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
668 {
669 	dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
670 				      xskb->pool->frame_len, DMA_BIDIRECTIONAL);
671 }
672 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
673 
xp_dma_sync_for_device_slow(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)674 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
675 				 size_t size)
676 {
677 	dma_sync_single_range_for_device(pool->dev, dma, 0,
678 					 size, DMA_BIDIRECTIONAL);
679 }
680 EXPORT_SYMBOL(xp_dma_sync_for_device_slow);
681