Lines Matching refs:pool
11 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) in xp_add_xsk() argument
18 spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); in xp_add_xsk()
19 list_add_rcu(&xs->tx_list, &pool->xsk_tx_list); in xp_add_xsk()
20 spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); in xp_add_xsk()
23 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) in xp_del_xsk() argument
30 spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); in xp_del_xsk()
32 spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); in xp_del_xsk()
35 void xp_destroy(struct xsk_buff_pool *pool) in xp_destroy() argument
37 if (!pool) in xp_destroy()
40 kvfree(pool->tx_descs); in xp_destroy()
41 kvfree(pool->heads); in xp_destroy()
42 kvfree(pool); in xp_destroy()
45 int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs) in xp_alloc_tx_descs() argument
47 pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs), in xp_alloc_tx_descs()
49 if (!pool->tx_descs) in xp_alloc_tx_descs()
59 struct xsk_buff_pool *pool; in xp_create_and_assign_umem() local
64 pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL); in xp_create_and_assign_umem()
65 if (!pool) in xp_create_and_assign_umem()
68 pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL); in xp_create_and_assign_umem()
69 if (!pool->heads) in xp_create_and_assign_umem()
73 if (xp_alloc_tx_descs(pool, xs)) in xp_create_and_assign_umem()
76 pool->chunk_mask = ~((u64)umem->chunk_size - 1); in xp_create_and_assign_umem()
77 pool->addrs_cnt = umem->size; in xp_create_and_assign_umem()
78 pool->heads_cnt = umem->chunks; in xp_create_and_assign_umem()
79 pool->free_heads_cnt = umem->chunks; in xp_create_and_assign_umem()
80 pool->headroom = umem->headroom; in xp_create_and_assign_umem()
81 pool->chunk_size = umem->chunk_size; in xp_create_and_assign_umem()
82 pool->chunk_shift = ffs(umem->chunk_size) - 1; in xp_create_and_assign_umem()
83 pool->unaligned = unaligned; in xp_create_and_assign_umem()
84 pool->frame_len = umem->chunk_size - umem->headroom - in xp_create_and_assign_umem()
86 pool->umem = umem; in xp_create_and_assign_umem()
87 pool->addrs = umem->addrs; in xp_create_and_assign_umem()
88 INIT_LIST_HEAD(&pool->free_list); in xp_create_and_assign_umem()
89 INIT_LIST_HEAD(&pool->xskb_list); in xp_create_and_assign_umem()
90 INIT_LIST_HEAD(&pool->xsk_tx_list); in xp_create_and_assign_umem()
91 spin_lock_init(&pool->xsk_tx_list_lock); in xp_create_and_assign_umem()
92 spin_lock_init(&pool->cq_lock); in xp_create_and_assign_umem()
93 refcount_set(&pool->users, 1); in xp_create_and_assign_umem()
95 pool->fq = xs->fq_tmp; in xp_create_and_assign_umem()
96 pool->cq = xs->cq_tmp; in xp_create_and_assign_umem()
98 for (i = 0; i < pool->free_heads_cnt; i++) { in xp_create_and_assign_umem()
99 xskb = &pool->heads[i]; in xp_create_and_assign_umem()
100 xskb->pool = pool; in xp_create_and_assign_umem()
104 if (pool->unaligned) in xp_create_and_assign_umem()
105 pool->free_heads[i] = xskb; in xp_create_and_assign_umem()
107 xp_init_xskb_addr(xskb, pool, i * pool->chunk_size); in xp_create_and_assign_umem()
110 return pool; in xp_create_and_assign_umem()
113 xp_destroy(pool); in xp_create_and_assign_umem()
117 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) in xp_set_rxq_info() argument
121 for (i = 0; i < pool->heads_cnt; i++) in xp_set_rxq_info()
122 pool->heads[i].xdp.rxq = rxq; in xp_set_rxq_info()
126 static void xp_disable_drv_zc(struct xsk_buff_pool *pool) in xp_disable_drv_zc() argument
133 if (pool->umem->zc) { in xp_disable_drv_zc()
135 bpf.xsk.pool = NULL; in xp_disable_drv_zc()
136 bpf.xsk.queue_id = pool->queue_id; in xp_disable_drv_zc()
138 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf); in xp_disable_drv_zc()
149 int xp_assign_dev(struct xsk_buff_pool *pool, in xp_assign_dev() argument
167 pool->netdev = netdev; in xp_assign_dev()
168 pool->queue_id = queue_id; in xp_assign_dev()
169 err = xsk_reg_pool_at_qid(netdev, pool, queue_id); in xp_assign_dev()
174 pool->umem->flags |= XDP_UMEM_SG_FLAG; in xp_assign_dev()
177 pool->uses_need_wakeup = true; in xp_assign_dev()
182 pool->cached_need_wakeup = XDP_WAKEUP_TX; in xp_assign_dev()
201 bpf.xsk.pool = pool; in xp_assign_dev()
208 if (!pool->dma_pages) { in xp_assign_dev()
213 pool->umem->zc = true; in xp_assign_dev()
217 xp_disable_drv_zc(pool); in xp_assign_dev()
228 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs, in xp_assign_dev_shared() argument
235 if (!pool->fq || !pool->cq) in xp_assign_dev_shared()
239 if (umem_xs->pool->uses_need_wakeup) in xp_assign_dev_shared()
242 return xp_assign_dev(pool, dev, queue_id, flags); in xp_assign_dev_shared()
245 void xp_clear_dev(struct xsk_buff_pool *pool) in xp_clear_dev() argument
247 if (!pool->netdev) in xp_clear_dev()
250 xp_disable_drv_zc(pool); in xp_clear_dev()
251 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); in xp_clear_dev()
252 dev_put(pool->netdev); in xp_clear_dev()
253 pool->netdev = NULL; in xp_clear_dev()
258 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, in xp_release_deferred() local
262 xp_clear_dev(pool); in xp_release_deferred()
265 if (pool->fq) { in xp_release_deferred()
266 xskq_destroy(pool->fq); in xp_release_deferred()
267 pool->fq = NULL; in xp_release_deferred()
270 if (pool->cq) { in xp_release_deferred()
271 xskq_destroy(pool->cq); in xp_release_deferred()
272 pool->cq = NULL; in xp_release_deferred()
275 xdp_put_umem(pool->umem, false); in xp_release_deferred()
276 xp_destroy(pool); in xp_release_deferred()
279 void xp_get_pool(struct xsk_buff_pool *pool) in xp_get_pool() argument
281 refcount_inc(&pool->users); in xp_get_pool()
284 bool xp_put_pool(struct xsk_buff_pool *pool) in xp_put_pool() argument
286 if (!pool) in xp_put_pool()
289 if (refcount_dec_and_test(&pool->users)) { in xp_put_pool()
290 INIT_WORK(&pool->work, xp_release_deferred); in xp_put_pool()
291 schedule_work(&pool->work); in xp_put_pool()
298 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool) in xp_find_dma_map() argument
302 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) { in xp_find_dma_map()
303 if (dma_map->netdev == pool->netdev) in xp_find_dma_map()
359 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) in xp_dma_unmap() argument
363 if (!pool->dma_pages) in xp_dma_unmap()
366 dma_map = xp_find_dma_map(pool); in xp_dma_unmap()
376 kvfree(pool->dma_pages); in xp_dma_unmap()
377 pool->dma_pages = NULL; in xp_dma_unmap()
378 pool->dma_pages_cnt = 0; in xp_dma_unmap()
379 pool->dev = NULL; in xp_dma_unmap()
395 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map) in xp_init_dma_info() argument
397 if (!pool->unaligned) { in xp_init_dma_info()
400 for (i = 0; i < pool->heads_cnt; i++) { in xp_init_dma_info()
401 struct xdp_buff_xsk *xskb = &pool->heads[i]; in xp_init_dma_info()
403 xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr); in xp_init_dma_info()
407 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL); in xp_init_dma_info()
408 if (!pool->dma_pages) in xp_init_dma_info()
411 pool->dev = dma_map->dev; in xp_init_dma_info()
412 pool->dma_pages_cnt = dma_map->dma_pages_cnt; in xp_init_dma_info()
413 pool->dma_need_sync = dma_map->dma_need_sync; in xp_init_dma_info()
414 memcpy(pool->dma_pages, dma_map->dma_pages, in xp_init_dma_info()
415 pool->dma_pages_cnt * sizeof(*pool->dma_pages)); in xp_init_dma_info()
420 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, in xp_dma_map() argument
428 dma_map = xp_find_dma_map(pool); in xp_dma_map()
430 err = xp_init_dma_info(pool, dma_map); in xp_dma_map()
438 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem); in xp_dma_map()
454 if (pool->unaligned) in xp_dma_map()
457 err = xp_init_dma_info(pool, dma_map); in xp_dma_map()
467 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool, in xp_addr_crosses_non_contig_pg() argument
470 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size); in xp_addr_crosses_non_contig_pg()
473 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr) in xp_check_unaligned() argument
476 if (*addr >= pool->addrs_cnt || in xp_check_unaligned()
477 *addr + pool->chunk_size > pool->addrs_cnt || in xp_check_unaligned()
478 xp_addr_crosses_non_contig_pg(pool, *addr)) in xp_check_unaligned()
483 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr) in xp_check_aligned() argument
485 *addr = xp_aligned_extract_addr(pool, *addr); in xp_check_aligned()
486 return *addr < pool->addrs_cnt; in xp_check_aligned()
489 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool) in __xp_alloc() argument
495 if (pool->free_heads_cnt == 0) in __xp_alloc()
499 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { in __xp_alloc()
500 pool->fq->queue_empty_descs++; in __xp_alloc()
504 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : in __xp_alloc()
505 xp_check_aligned(pool, &addr); in __xp_alloc()
507 pool->fq->invalid_descs++; in __xp_alloc()
508 xskq_cons_release(pool->fq); in __xp_alloc()
514 if (pool->unaligned) { in __xp_alloc()
515 xskb = pool->free_heads[--pool->free_heads_cnt]; in __xp_alloc()
516 xp_init_xskb_addr(xskb, pool, addr); in __xp_alloc()
517 if (pool->dma_pages) in __xp_alloc()
518 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); in __xp_alloc()
520 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; in __xp_alloc()
523 xskq_cons_release(pool->fq); in __xp_alloc()
527 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) in xp_alloc() argument
531 if (!pool->free_list_cnt) { in xp_alloc()
532 xskb = __xp_alloc(pool); in xp_alloc()
536 pool->free_list_cnt--; in xp_alloc()
537 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, in xp_alloc()
546 if (pool->dma_need_sync) { in xp_alloc()
547 dma_sync_single_range_for_device(pool->dev, xskb->dma, 0, in xp_alloc()
548 pool->frame_len, in xp_alloc()
555 static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) in xp_alloc_new_from_fq() argument
559 if (max > pool->free_heads_cnt) in xp_alloc_new_from_fq()
560 max = pool->free_heads_cnt; in xp_alloc_new_from_fq()
561 max = xskq_cons_nb_entries(pool->fq, max); in xp_alloc_new_from_fq()
563 cached_cons = pool->fq->cached_cons; in xp_alloc_new_from_fq()
571 __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr); in xp_alloc_new_from_fq()
573 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : in xp_alloc_new_from_fq()
574 xp_check_aligned(pool, &addr); in xp_alloc_new_from_fq()
576 pool->fq->invalid_descs++; in xp_alloc_new_from_fq()
581 if (pool->unaligned) { in xp_alloc_new_from_fq()
582 xskb = pool->free_heads[--pool->free_heads_cnt]; in xp_alloc_new_from_fq()
583 xp_init_xskb_addr(xskb, pool, addr); in xp_alloc_new_from_fq()
584 if (pool->dma_pages) in xp_alloc_new_from_fq()
585 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); in xp_alloc_new_from_fq()
587 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; in xp_alloc_new_from_fq()
594 xskq_cons_release_n(pool->fq, max); in xp_alloc_new_from_fq()
598 static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries) in xp_alloc_reused() argument
603 nb_entries = min_t(u32, nb_entries, pool->free_list_cnt); in xp_alloc_reused()
607 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node); in xp_alloc_reused()
613 pool->free_list_cnt -= nb_entries; in xp_alloc_reused()
618 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) in xp_alloc_batch() argument
622 if (unlikely(pool->dma_need_sync)) { in xp_alloc_batch()
626 buff = xp_alloc(pool); in xp_alloc_batch()
632 if (unlikely(pool->free_list_cnt)) { in xp_alloc_batch()
633 nb_entries1 = xp_alloc_reused(pool, xdp, max); in xp_alloc_batch()
641 nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max); in xp_alloc_batch()
643 pool->fq->queue_empty_descs++; in xp_alloc_batch()
649 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count) in xp_can_alloc() argument
651 if (pool->free_list_cnt >= count) in xp_can_alloc()
653 return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt); in xp_can_alloc()
662 xskb->pool->free_list_cnt++; in xp_free()
663 list_add(&xskb->free_list_node, &xskb->pool->free_list); in xp_free()
667 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr) in xp_raw_get_data() argument
669 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; in xp_raw_get_data()
670 return pool->addrs + addr; in xp_raw_get_data()
674 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) in xp_raw_get_dma() argument
676 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; in xp_raw_get_dma()
677 return (pool->dma_pages[addr >> PAGE_SHIFT] & in xp_raw_get_dma()
685 dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0, in xp_dma_sync_for_cpu_slow()
686 xskb->pool->frame_len, DMA_BIDIRECTIONAL); in xp_dma_sync_for_cpu_slow()
690 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma, in xp_dma_sync_for_device_slow() argument
693 dma_sync_single_range_for_device(pool->dev, dma, 0, in xp_dma_sync_for_device_slow()