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