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->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);
95 pool->fq = xs->fq_tmp;
96 pool->cq = xs->cq_tmp;
98 for (i = 0; i < pool->free_heads_cnt; i++) {
99 xskb = &pool->heads[i];
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);
105 pool->free_heads[i] = xskb;
107 xp_init_xskb_addr(xskb, pool, i * pool->chunk_size);
117 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
121 for (i = 0; i < pool->heads_cnt; i++)
122 pool->heads[i].xdp.rxq = rxq;
124 EXPORT_SYMBOL(xp_set_rxq_info);
126 static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
128 struct netdev_bpf bpf;
133 if (pool->umem->zc) {
134 bpf.command = XDP_SETUP_XSK_POOL;
136 bpf.xsk.queue_id = pool->queue_id;
138 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
141 WARN(1, "Failed to disable zero-copy!\n");
145 #define NETDEV_XDP_ACT_ZC (NETDEV_XDP_ACT_BASIC | \
146 NETDEV_XDP_ACT_REDIRECT | \
147 NETDEV_XDP_ACT_XSK_ZEROCOPY)
149 int xp_assign_dev(struct xsk_buff_pool *pool,
150 struct net_device *netdev, u16 queue_id, u16 flags)
152 bool force_zc, force_copy;
153 struct netdev_bpf bpf;
158 force_zc = flags & XDP_ZEROCOPY;
159 force_copy = flags & XDP_COPY;
161 if (force_zc && force_copy)
164 if (xsk_get_pool_from_qid(netdev, queue_id))
167 pool->netdev = netdev;
168 pool->queue_id = queue_id;
169 err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
173 if (flags & XDP_USE_SG)
174 pool->umem->flags |= XDP_UMEM_SG_FLAG;
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().
182 pool->cached_need_wakeup = XDP_WAKEUP_TX;
187 /* For copy-mode, we are done. */
190 if ((netdev->xdp_features & NETDEV_XDP_ACT_ZC) != NETDEV_XDP_ACT_ZC) {
195 if (netdev->xdp_zc_max_segs == 1 && (flags & XDP_USE_SG)) {
200 bpf.command = XDP_SETUP_XSK_POOL;
202 bpf.xsk.queue_id = queue_id;
204 err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
208 if (!pool->dma_pages) {
209 WARN(1, "Driver did not DMA map zero-copy buffers");
213 pool->umem->zc = true;
217 xp_disable_drv_zc(pool);
220 err = 0; /* fallback to copy mode */
222 xsk_clear_pool_at_qid(netdev, 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)
232 struct xdp_umem *umem = umem_xs->umem;
234 /* One fill and completion ring required for each queue id. */
235 if (!pool->fq || !pool->cq)
238 flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
239 if (umem_xs->pool->uses_need_wakeup)
240 flags |= XDP_USE_NEED_WAKEUP;
242 return xp_assign_dev(pool, dev, queue_id, flags);
245 void xp_clear_dev(struct xsk_buff_pool *pool)
250 xp_disable_drv_zc(pool);
251 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
252 dev_put(pool->netdev);
256 static void xp_release_deferred(struct work_struct *work)
258 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
266 xskq_destroy(pool->fq);
271 xskq_destroy(pool->cq);
275 xdp_put_umem(pool->umem, false);
279 void xp_get_pool(struct xsk_buff_pool *pool)
281 refcount_inc(&pool->users);
284 bool xp_put_pool(struct xsk_buff_pool *pool)
289 if (refcount_dec_and_test(&pool->users)) {
290 INIT_WORK(&pool->work, xp_release_deferred);
291 schedule_work(&pool->work);
298 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
300 struct xsk_dma_map *dma_map;
302 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
303 if (dma_map->netdev == pool->netdev)
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)
313 struct xsk_dma_map *dma_map;
315 dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
319 dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
320 if (!dma_map->dma_pages) {
325 dma_map->netdev = netdev;
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);
334 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
336 list_del(&dma_map->list);
337 kvfree(dma_map->dma_pages);
341 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
346 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
347 dma = &dma_map->dma_pages[i];
349 *dma &= ~XSK_NEXT_PG_CONTIG_MASK;
350 dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
351 DMA_BIDIRECTIONAL, attrs);
356 xp_destroy_dma_map(dma_map);
359 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
361 struct xsk_dma_map *dma_map;
363 if (!pool->dma_pages)
366 dma_map = xp_find_dma_map(pool);
368 WARN(1, "Could not find dma_map for device");
372 if (!refcount_dec_and_test(&dma_map->users))
375 __xp_dma_unmap(dma_map, attrs);
376 kvfree(pool->dma_pages);
377 pool->dma_pages = NULL;
378 pool->dma_pages_cnt = 0;
381 EXPORT_SYMBOL(xp_dma_unmap);
383 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
387 for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
388 if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
389 dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
391 dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
395 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
397 if (!pool->unaligned) {
400 for (i = 0; i < pool->heads_cnt; i++) {
401 struct xdp_buff_xsk *xskb = &pool->heads[i];
403 xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
407 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
408 if (!pool->dma_pages)
411 pool->dev = dma_map->dev;
412 pool->dma_pages_cnt = dma_map->dma_pages_cnt;
413 pool->dma_need_sync = dma_map->dma_need_sync;
414 memcpy(pool->dma_pages, dma_map->dma_pages,
415 pool->dma_pages_cnt * sizeof(*pool->dma_pages));
420 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
421 unsigned long attrs, struct page **pages, u32 nr_pages)
423 struct xsk_dma_map *dma_map;
428 dma_map = xp_find_dma_map(pool);
430 err = xp_init_dma_info(pool, dma_map);
434 refcount_inc(&dma_map->users);
438 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
442 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
443 dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
444 DMA_BIDIRECTIONAL, attrs);
445 if (dma_mapping_error(dev, dma)) {
446 __xp_dma_unmap(dma_map, attrs);
449 if (dma_need_sync(dev, dma))
450 dma_map->dma_need_sync = true;
451 dma_map->dma_pages[i] = dma;
455 xp_check_dma_contiguity(dma_map);
457 err = xp_init_dma_info(pool, dma_map);
459 __xp_dma_unmap(dma_map, attrs);
465 EXPORT_SYMBOL(xp_dma_map);
467 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
470 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
473 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
475 *addr = xp_unaligned_extract_addr(*addr);
476 if (*addr >= pool->addrs_cnt ||
477 *addr + pool->chunk_size > pool->addrs_cnt ||
478 xp_addr_crosses_non_contig_pg(pool, *addr))
483 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
485 *addr = xp_aligned_extract_addr(pool, *addr);
486 return *addr < pool->addrs_cnt;
489 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
491 struct xdp_buff_xsk *xskb;
495 if (pool->free_heads_cnt == 0)
499 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
500 pool->fq->queue_empty_descs++;
504 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
505 xp_check_aligned(pool, &addr);
507 pool->fq->invalid_descs++;
508 xskq_cons_release(pool->fq);
514 if (pool->unaligned) {
515 xskb = pool->free_heads[--pool->free_heads_cnt];
516 xp_init_xskb_addr(xskb, pool, addr);
518 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
520 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
523 xskq_cons_release(pool->fq);
527 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
529 struct xdp_buff_xsk *xskb;
531 if (!pool->free_list_cnt) {
532 xskb = __xp_alloc(pool);
536 pool->free_list_cnt--;
537 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
539 list_del_init(&xskb->free_list_node);
542 xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
543 xskb->xdp.data_meta = xskb->xdp.data;
546 if (pool->dma_need_sync) {
547 dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
553 EXPORT_SYMBOL(xp_alloc);
555 static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
557 u32 i, cached_cons, nb_entries;
559 if (max > pool->free_heads_cnt)
560 max = pool->free_heads_cnt;
561 max = xskq_cons_nb_entries(pool->fq, max);
563 cached_cons = pool->fq->cached_cons;
567 struct xdp_buff_xsk *xskb;
571 __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
573 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
574 xp_check_aligned(pool, &addr);
576 pool->fq->invalid_descs++;
581 if (pool->unaligned) {
582 xskb = pool->free_heads[--pool->free_heads_cnt];
583 xp_init_xskb_addr(xskb, pool, addr);
585 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
587 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
594 xskq_cons_release_n(pool->fq, max);
598 static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
600 struct xdp_buff_xsk *xskb;
603 nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
607 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
608 list_del_init(&xskb->free_list_node);
613 pool->free_list_cnt -= nb_entries;
618 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
620 u32 nb_entries1 = 0, nb_entries2;
622 if (unlikely(pool->dma_need_sync)) {
623 struct xdp_buff *buff;
626 buff = xp_alloc(pool);
632 if (unlikely(pool->free_list_cnt)) {
633 nb_entries1 = xp_alloc_reused(pool, xdp, max);
634 if (nb_entries1 == max)
641 nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
643 pool->fq->queue_empty_descs++;
645 return nb_entries1 + nb_entries2;
647 EXPORT_SYMBOL(xp_alloc_batch);
649 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
651 if (pool->free_list_cnt >= count)
653 return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
655 EXPORT_SYMBOL(xp_can_alloc);
657 void xp_free(struct xdp_buff_xsk *xskb)
659 if (!list_empty(&xskb->free_list_node))
662 xskb->pool->free_list_cnt++;
663 list_add(&xskb->free_list_node, &xskb->pool->free_list);
665 EXPORT_SYMBOL(xp_free);
667 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
669 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
670 return pool->addrs + addr;
672 EXPORT_SYMBOL(xp_raw_get_data);
674 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
676 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
677 return (pool->dma_pages[addr >> PAGE_SHIFT] &
678 ~XSK_NEXT_PG_CONTIG_MASK) +
681 EXPORT_SYMBOL(xp_raw_get_dma);
683 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
685 dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
686 xskb->pool->frame_len, DMA_BIDIRECTIONAL);
688 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
690 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
693 dma_sync_single_range_for_device(pool->dev, dma, 0,
694 size, DMA_BIDIRECTIONAL);
696 EXPORT_SYMBOL(xp_dma_sync_for_device_slow);