1 // SPDX-License-Identifier: GPL-2.0
3 #include <net/xsk_buff_pool.h>
4 #include <net/xdp_sock.h>
5 #include <net/xdp_sock_drv.h>
11 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
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);
23 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
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);
35 void xp_destroy(struct xsk_buff_pool *pool)
40 kvfree(pool->tx_descs);
45 int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs)
47 pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs),
55 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
56 struct xdp_umem *umem)
58 bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
59 struct xsk_buff_pool *pool;
60 struct xdp_buff_xsk *xskb;
63 entries = unaligned ? umem->chunks : 0;
64 pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL);
68 pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
73 if (xp_alloc_tx_descs(pool, xs))
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 -
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);
94 pool->fq = xs->fq_tmp;
95 pool->cq = xs->cq_tmp;
97 for (i = 0; i < pool->free_heads_cnt; i++) {
98 xskb = &pool->heads[i];
100 xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
101 INIT_LIST_HEAD(&xskb->free_list_node);
103 pool->free_heads[i] = xskb;
105 xp_init_xskb_addr(xskb, pool, i * pool->chunk_size);
115 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
119 for (i = 0; i < pool->heads_cnt; i++)
120 pool->heads[i].xdp.rxq = rxq;
122 EXPORT_SYMBOL(xp_set_rxq_info);
124 static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
126 struct netdev_bpf bpf;
131 if (pool->umem->zc) {
132 bpf.command = XDP_SETUP_XSK_POOL;
134 bpf.xsk.queue_id = pool->queue_id;
136 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
139 WARN(1, "Failed to disable zero-copy!\n");
143 int xp_assign_dev(struct xsk_buff_pool *pool,
144 struct net_device *netdev, u16 queue_id, u16 flags)
146 bool force_zc, force_copy;
147 struct netdev_bpf bpf;
152 force_zc = flags & XDP_ZEROCOPY;
153 force_copy = flags & XDP_COPY;
155 if (force_zc && force_copy)
158 if (xsk_get_pool_from_qid(netdev, queue_id))
161 pool->netdev = netdev;
162 pool->queue_id = queue_id;
163 err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
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().
173 pool->cached_need_wakeup = XDP_WAKEUP_TX;
178 /* For copy-mode, we are done. */
181 if (!netdev->netdev_ops->ndo_bpf ||
182 !netdev->netdev_ops->ndo_xsk_wakeup) {
187 bpf.command = XDP_SETUP_XSK_POOL;
189 bpf.xsk.queue_id = queue_id;
191 err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
195 if (!pool->dma_pages) {
196 WARN(1, "Driver did not DMA map zero-copy buffers");
200 pool->umem->zc = true;
204 xp_disable_drv_zc(pool);
207 err = 0; /* fallback to copy mode */
209 xsk_clear_pool_at_qid(netdev, 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)
219 struct xdp_umem *umem = umem_xs->umem;
221 /* One fill and completion ring required for each queue id. */
222 if (!pool->fq || !pool->cq)
225 flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
226 if (umem_xs->pool->uses_need_wakeup)
227 flags |= XDP_USE_NEED_WAKEUP;
229 return xp_assign_dev(pool, dev, queue_id, flags);
232 void xp_clear_dev(struct xsk_buff_pool *pool)
237 xp_disable_drv_zc(pool);
238 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
239 dev_put(pool->netdev);
243 static void xp_release_deferred(struct work_struct *work)
245 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
253 xskq_destroy(pool->fq);
258 xskq_destroy(pool->cq);
262 xdp_put_umem(pool->umem, false);
266 void xp_get_pool(struct xsk_buff_pool *pool)
268 refcount_inc(&pool->users);
271 bool xp_put_pool(struct xsk_buff_pool *pool)
276 if (refcount_dec_and_test(&pool->users)) {
277 INIT_WORK(&pool->work, xp_release_deferred);
278 schedule_work(&pool->work);
285 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
287 struct xsk_dma_map *dma_map;
289 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
290 if (dma_map->netdev == pool->netdev)
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)
300 struct xsk_dma_map *dma_map;
302 dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
306 dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
307 if (!dma_map->dma_pages) {
312 dma_map->netdev = netdev;
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);
321 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
323 list_del(&dma_map->list);
324 kvfree(dma_map->dma_pages);
328 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
333 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
334 dma = &dma_map->dma_pages[i];
336 *dma &= ~XSK_NEXT_PG_CONTIG_MASK;
337 dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
338 DMA_BIDIRECTIONAL, attrs);
343 xp_destroy_dma_map(dma_map);
346 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
348 struct xsk_dma_map *dma_map;
350 if (pool->dma_pages_cnt == 0)
353 dma_map = xp_find_dma_map(pool);
355 WARN(1, "Could not find dma_map for device");
359 if (!refcount_dec_and_test(&dma_map->users))
362 __xp_dma_unmap(dma_map, attrs);
363 kvfree(pool->dma_pages);
364 pool->dma_pages_cnt = 0;
367 EXPORT_SYMBOL(xp_dma_unmap);
369 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
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;
377 dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
381 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
383 if (!pool->unaligned) {
386 for (i = 0; i < pool->heads_cnt; i++) {
387 struct xdp_buff_xsk *xskb = &pool->heads[i];
389 xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
393 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
394 if (!pool->dma_pages)
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));
406 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
407 unsigned long attrs, struct page **pages, u32 nr_pages)
409 struct xsk_dma_map *dma_map;
414 dma_map = xp_find_dma_map(pool);
416 err = xp_init_dma_info(pool, dma_map);
420 refcount_inc(&dma_map->users);
424 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
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);
435 if (dma_need_sync(dev, dma))
436 dma_map->dma_need_sync = true;
437 dma_map->dma_pages[i] = dma;
441 xp_check_dma_contiguity(dma_map);
443 err = xp_init_dma_info(pool, dma_map);
445 __xp_dma_unmap(dma_map, attrs);
451 EXPORT_SYMBOL(xp_dma_map);
453 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
456 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
459 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
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))
469 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
471 *addr = xp_aligned_extract_addr(pool, *addr);
472 return *addr < pool->addrs_cnt;
475 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
477 struct xdp_buff_xsk *xskb;
481 if (pool->free_heads_cnt == 0)
485 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
486 pool->fq->queue_empty_descs++;
490 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
491 xp_check_aligned(pool, &addr);
493 pool->fq->invalid_descs++;
494 xskq_cons_release(pool->fq);
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);
506 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
509 xskq_cons_release(pool->fq);
513 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
515 struct xdp_buff_xsk *xskb;
517 if (!pool->free_list_cnt) {
518 xskb = __xp_alloc(pool);
522 pool->free_list_cnt--;
523 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
525 list_del_init(&xskb->free_list_node);
528 xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
529 xskb->xdp.data_meta = xskb->xdp.data;
531 if (pool->dma_need_sync) {
532 dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
538 EXPORT_SYMBOL(xp_alloc);
540 static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
542 u32 i, cached_cons, nb_entries;
544 if (max > pool->free_heads_cnt)
545 max = pool->free_heads_cnt;
546 max = xskq_cons_nb_entries(pool->fq, max);
548 cached_cons = pool->fq->cached_cons;
552 struct xdp_buff_xsk *xskb;
556 __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
558 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
559 xp_check_aligned(pool, &addr);
561 pool->fq->invalid_descs++;
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);
572 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
579 xskq_cons_release_n(pool->fq, max);
583 static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
585 struct xdp_buff_xsk *xskb;
588 nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
592 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
593 list_del_init(&xskb->free_list_node);
598 pool->free_list_cnt -= nb_entries;
603 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
605 u32 nb_entries1 = 0, nb_entries2;
607 if (unlikely(pool->dma_need_sync)) {
608 struct xdp_buff *buff;
611 buff = xp_alloc(pool);
617 if (unlikely(pool->free_list_cnt)) {
618 nb_entries1 = xp_alloc_reused(pool, xdp, max);
619 if (nb_entries1 == max)
626 nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
628 pool->fq->queue_empty_descs++;
630 return nb_entries1 + nb_entries2;
632 EXPORT_SYMBOL(xp_alloc_batch);
634 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
636 if (pool->free_list_cnt >= count)
638 return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
640 EXPORT_SYMBOL(xp_can_alloc);
642 void xp_free(struct xdp_buff_xsk *xskb)
644 if (!list_empty(&xskb->free_list_node))
647 xskb->pool->free_list_cnt++;
648 list_add(&xskb->free_list_node, &xskb->pool->free_list);
650 EXPORT_SYMBOL(xp_free);
652 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
654 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
655 return pool->addrs + addr;
657 EXPORT_SYMBOL(xp_raw_get_data);
659 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
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) +
666 EXPORT_SYMBOL(xp_raw_get_dma);
668 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
670 dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
671 xskb->pool->frame_len, DMA_BIDIRECTIONAL);
673 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
675 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
678 dma_sync_single_range_for_device(pool->dev, dma, 0,
679 size, DMA_BIDIRECTIONAL);
681 EXPORT_SYMBOL(xp_dma_sync_for_device_slow);