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 pool->tx_metadata_len = umem->tx_metadata_len; in xp_create_and_assign_umem()
89 pool->tx_sw_csum = umem->flags & XDP_UMEM_TX_SW_CSUM; in xp_create_and_assign_umem()
90 INIT_LIST_HEAD(&pool->free_list); in xp_create_and_assign_umem()
91 INIT_LIST_HEAD(&pool->xskb_list); in xp_create_and_assign_umem()
92 INIT_LIST_HEAD(&pool->xsk_tx_list); in xp_create_and_assign_umem()
93 spin_lock_init(&pool->xsk_tx_list_lock); in xp_create_and_assign_umem()
94 spin_lock_init(&pool->cq_lock); in xp_create_and_assign_umem()
95 refcount_set(&pool->users, 1); in xp_create_and_assign_umem()
97 pool->fq = xs->fq_tmp; in xp_create_and_assign_umem()
98 pool->cq = xs->cq_tmp; in xp_create_and_assign_umem()
100 for (i = 0; i < pool->free_heads_cnt; i++) { in xp_create_and_assign_umem()
101 xskb = &pool->heads[i]; in xp_create_and_assign_umem()
102 xskb->pool = pool; in xp_create_and_assign_umem()
106 if (pool->unaligned) in xp_create_and_assign_umem()
107 pool->free_heads[i] = xskb; in xp_create_and_assign_umem()
109 xp_init_xskb_addr(xskb, pool, i * pool->chunk_size); in xp_create_and_assign_umem()
112 return pool; in xp_create_and_assign_umem()
115 xp_destroy(pool); in xp_create_and_assign_umem()
119 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) in xp_set_rxq_info() argument
123 for (i = 0; i < pool->heads_cnt; i++) in xp_set_rxq_info()
124 pool->heads[i].xdp.rxq = rxq; in xp_set_rxq_info()
128 void xp_fill_cb(struct xsk_buff_pool *pool, struct xsk_cb_desc *desc) in xp_fill_cb() argument
132 for (i = 0; i < pool->heads_cnt; i++) { in xp_fill_cb()
133 struct xdp_buff_xsk *xskb = &pool->heads[i]; in xp_fill_cb()
140 static void xp_disable_drv_zc(struct xsk_buff_pool *pool) in xp_disable_drv_zc() argument
147 if (pool->umem->zc) { in xp_disable_drv_zc()
149 bpf.xsk.pool = NULL; in xp_disable_drv_zc()
150 bpf.xsk.queue_id = pool->queue_id; in xp_disable_drv_zc()
152 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf); in xp_disable_drv_zc()
163 int xp_assign_dev(struct xsk_buff_pool *pool, in xp_assign_dev() argument
181 pool->netdev = netdev; in xp_assign_dev()
182 pool->queue_id = queue_id; in xp_assign_dev()
183 err = xsk_reg_pool_at_qid(netdev, pool, queue_id); in xp_assign_dev()
188 pool->umem->flags |= XDP_UMEM_SG_FLAG; in xp_assign_dev()
191 pool->uses_need_wakeup = true; in xp_assign_dev()
196 pool->cached_need_wakeup = XDP_WAKEUP_TX; in xp_assign_dev()
220 bpf.xsk.pool = pool; in xp_assign_dev()
227 if (!pool->dma_pages) { in xp_assign_dev()
232 pool->umem->zc = true; in xp_assign_dev()
236 xp_disable_drv_zc(pool); in xp_assign_dev()
247 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs, in xp_assign_dev_shared() argument
254 if (!pool->fq || !pool->cq) in xp_assign_dev_shared()
258 if (umem_xs->pool->uses_need_wakeup) in xp_assign_dev_shared()
261 return xp_assign_dev(pool, dev, queue_id, flags); in xp_assign_dev_shared()
264 void xp_clear_dev(struct xsk_buff_pool *pool) in xp_clear_dev() argument
266 if (!pool->netdev) in xp_clear_dev()
269 xp_disable_drv_zc(pool); in xp_clear_dev()
270 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); in xp_clear_dev()
271 dev_put(pool->netdev); in xp_clear_dev()
272 pool->netdev = NULL; in xp_clear_dev()
277 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, in xp_release_deferred() local
281 xp_clear_dev(pool); in xp_release_deferred()
284 if (pool->fq) { in xp_release_deferred()
285 xskq_destroy(pool->fq); in xp_release_deferred()
286 pool->fq = NULL; in xp_release_deferred()
289 if (pool->cq) { in xp_release_deferred()
290 xskq_destroy(pool->cq); in xp_release_deferred()
291 pool->cq = NULL; in xp_release_deferred()
294 xdp_put_umem(pool->umem, false); in xp_release_deferred()
295 xp_destroy(pool); in xp_release_deferred()
298 void xp_get_pool(struct xsk_buff_pool *pool) in xp_get_pool() argument
300 refcount_inc(&pool->users); in xp_get_pool()
303 bool xp_put_pool(struct xsk_buff_pool *pool) in xp_put_pool() argument
305 if (!pool) in xp_put_pool()
308 if (refcount_dec_and_test(&pool->users)) { in xp_put_pool()
309 INIT_WORK(&pool->work, xp_release_deferred); in xp_put_pool()
310 schedule_work(&pool->work); in xp_put_pool()
317 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool) in xp_find_dma_map() argument
321 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) { in xp_find_dma_map()
322 if (dma_map->netdev == pool->netdev) in xp_find_dma_map()
377 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) in xp_dma_unmap() argument
381 if (!pool->dma_pages) in xp_dma_unmap()
384 dma_map = xp_find_dma_map(pool); in xp_dma_unmap()
394 kvfree(pool->dma_pages); in xp_dma_unmap()
395 pool->dma_pages = NULL; in xp_dma_unmap()
396 pool->dma_pages_cnt = 0; in xp_dma_unmap()
397 pool->dev = NULL; in xp_dma_unmap()
413 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map) in xp_init_dma_info() argument
415 if (!pool->unaligned) { in xp_init_dma_info()
418 for (i = 0; i < pool->heads_cnt; i++) { in xp_init_dma_info()
419 struct xdp_buff_xsk *xskb = &pool->heads[i]; in xp_init_dma_info()
421 xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr); in xp_init_dma_info()
425 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL); in xp_init_dma_info()
426 if (!pool->dma_pages) in xp_init_dma_info()
429 pool->dev = dma_map->dev; in xp_init_dma_info()
430 pool->dma_pages_cnt = dma_map->dma_pages_cnt; in xp_init_dma_info()
431 memcpy(pool->dma_pages, dma_map->dma_pages, in xp_init_dma_info()
432 pool->dma_pages_cnt * sizeof(*pool->dma_pages)); in xp_init_dma_info()
437 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, in xp_dma_map() argument
445 dma_map = xp_find_dma_map(pool); in xp_dma_map()
447 err = xp_init_dma_info(pool, dma_map); in xp_dma_map()
455 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem); in xp_dma_map()
469 if (pool->unaligned) in xp_dma_map()
472 err = xp_init_dma_info(pool, dma_map); in xp_dma_map()
482 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool, in xp_addr_crosses_non_contig_pg() argument
485 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size); in xp_addr_crosses_non_contig_pg()
488 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr) in xp_check_unaligned() argument
491 if (*addr >= pool->addrs_cnt || in xp_check_unaligned()
492 *addr + pool->chunk_size > pool->addrs_cnt || in xp_check_unaligned()
493 xp_addr_crosses_non_contig_pg(pool, *addr)) in xp_check_unaligned()
498 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr) in xp_check_aligned() argument
500 *addr = xp_aligned_extract_addr(pool, *addr); in xp_check_aligned()
501 return *addr < pool->addrs_cnt; in xp_check_aligned()
504 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool) in __xp_alloc() argument
510 if (pool->free_heads_cnt == 0) in __xp_alloc()
514 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { in __xp_alloc()
515 pool->fq->queue_empty_descs++; in __xp_alloc()
519 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : in __xp_alloc()
520 xp_check_aligned(pool, &addr); in __xp_alloc()
522 pool->fq->invalid_descs++; in __xp_alloc()
523 xskq_cons_release(pool->fq); in __xp_alloc()
529 if (pool->unaligned) { in __xp_alloc()
530 xskb = pool->free_heads[--pool->free_heads_cnt]; in __xp_alloc()
531 xp_init_xskb_addr(xskb, pool, addr); in __xp_alloc()
532 if (pool->dma_pages) in __xp_alloc()
533 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); in __xp_alloc()
535 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; in __xp_alloc()
538 xskq_cons_release(pool->fq); in __xp_alloc()
542 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) in xp_alloc() argument
546 if (!pool->free_list_cnt) { in xp_alloc()
547 xskb = __xp_alloc(pool); in xp_alloc()
551 pool->free_list_cnt--; in xp_alloc()
552 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, in xp_alloc()
561 if (pool->dev) in xp_alloc()
562 xp_dma_sync_for_device(pool, xskb->dma, pool->frame_len); in xp_alloc()
568 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
572 if (max > pool->free_heads_cnt) in xp_alloc_new_from_fq()
573 max = pool->free_heads_cnt; in xp_alloc_new_from_fq()
574 max = xskq_cons_nb_entries(pool->fq, max); in xp_alloc_new_from_fq()
576 cached_cons = pool->fq->cached_cons; in xp_alloc_new_from_fq()
584 __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr); in xp_alloc_new_from_fq()
586 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : in xp_alloc_new_from_fq()
587 xp_check_aligned(pool, &addr); in xp_alloc_new_from_fq()
589 pool->fq->invalid_descs++; in xp_alloc_new_from_fq()
594 if (pool->unaligned) { in xp_alloc_new_from_fq()
595 xskb = pool->free_heads[--pool->free_heads_cnt]; in xp_alloc_new_from_fq()
596 xp_init_xskb_addr(xskb, pool, addr); in xp_alloc_new_from_fq()
597 if (pool->dma_pages) in xp_alloc_new_from_fq()
598 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr); in xp_alloc_new_from_fq()
600 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)]; in xp_alloc_new_from_fq()
607 xskq_cons_release_n(pool->fq, max); in xp_alloc_new_from_fq()
611 static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries) in xp_alloc_reused() argument
616 nb_entries = min_t(u32, nb_entries, pool->free_list_cnt); in xp_alloc_reused()
620 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node); in xp_alloc_reused()
626 pool->free_list_cnt -= nb_entries; in xp_alloc_reused()
631 static u32 xp_alloc_slow(struct xsk_buff_pool *pool, struct xdp_buff **xdp, in xp_alloc_slow() argument
639 buff = xp_alloc(pool); in xp_alloc_slow()
649 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) in xp_alloc_batch() argument
653 if (unlikely(pool->dev && dma_dev_need_sync(pool->dev))) in xp_alloc_batch()
654 return xp_alloc_slow(pool, xdp, max); in xp_alloc_batch()
656 if (unlikely(pool->free_list_cnt)) { in xp_alloc_batch()
657 nb_entries1 = xp_alloc_reused(pool, xdp, max); in xp_alloc_batch()
665 nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max); in xp_alloc_batch()
667 pool->fq->queue_empty_descs++; in xp_alloc_batch()
673 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count) in xp_can_alloc() argument
677 if (pool->free_list_cnt >= count) in xp_can_alloc()
680 req_count = count - pool->free_list_cnt; in xp_can_alloc()
681 avail_count = xskq_cons_nb_entries(pool->fq, req_count); in xp_can_alloc()
683 pool->fq->queue_empty_descs++; in xp_can_alloc()
694 xskb->pool->free_list_cnt++; in xp_free()
695 list_add(&xskb->free_list_node, &xskb->pool->free_list); in xp_free()
699 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr) in xp_raw_get_data() argument
701 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; in xp_raw_get_data()
702 return pool->addrs + addr; in xp_raw_get_data()
706 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) in xp_raw_get_dma() argument
708 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; in xp_raw_get_dma()
709 return (pool->dma_pages[addr >> PAGE_SHIFT] & in xp_raw_get_dma()