diff --git a/net/Kconfig b/net/Kconfig index e38477393551..76ab44aa439a 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -68,7 +68,6 @@ config SKB_EXTENSIONS config NET_DEVMEM def_bool y - select GENERIC_ALLOCATOR depends on DMA_SHARED_BUFFER depends on PAGE_POOL diff --git a/net/core/devmem.c b/net/core/devmem.c index f4d60654ce7f..a9d86b5a5588 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -8,7 +8,6 @@ */ #include -#include #include #include #include @@ -30,25 +29,6 @@ static DEFINE_XARRAY_FLAGS(net_devmem_dmabuf_bindings, XA_FLAGS_ALLOC1); static const struct memory_provider_ops dmabuf_devmem_ops; -static void net_devmem_dmabuf_free_chunk_owner(struct gen_pool *genpool, - struct gen_pool_chunk *chunk, - void *not_used) -{ - struct dmabuf_genpool_chunk_owner *owner = chunk->owner; - - kvfree(owner->area.niovs); - kfree(owner); -} - -static dma_addr_t net_devmem_get_dma_addr(const struct net_iov *niov) -{ - struct dmabuf_genpool_chunk_owner *owner; - - owner = net_devmem_iov_to_chunk_owner(niov); - return owner->base_dma_addr + - ((dma_addr_t)net_iov_idx(niov) << owner->binding->niov_shift); -} - static void net_devmem_dmabuf_binding_release(struct percpu_ref *ref) { struct net_devmem_dmabuf_binding *binding = @@ -62,65 +42,55 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq) { struct net_devmem_dmabuf_binding *binding = container_of(wq, typeof(*binding), unbind_w); - size_t size, avail; - - gen_pool_for_each_chunk(binding->chunk_pool, - net_devmem_dmabuf_free_chunk_owner, NULL); - - size = gen_pool_size(binding->chunk_pool); - avail = gen_pool_avail(binding->chunk_pool); - - if (!WARN(size != avail, "can't destroy genpool. size=%zu, avail=%zu", - size, avail)) - gen_pool_destroy(binding->chunk_pool); + if (binding->freelist) + WARN(binding->free_count != binding->area.num_niovs, + "destroying dmabuf binding with outstanding net_iovs: total=%zu, free=%zu", + binding->area.num_niovs, binding->free_count); + kvfree(binding->area.niovs); dma_buf_unmap_attachment_unlocked(binding->attachment, binding->sgt, binding->direction); dma_buf_detach(binding->dmabuf, binding->attachment); dma_buf_put(binding->dmabuf); xa_destroy(&binding->bound_rxqs); percpu_ref_exit(&binding->ref); + kvfree(binding->freelist); kvfree(binding->tx_vec); kfree(binding); } -struct net_iov * -net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) +static unsigned int +net_devmem_alloc_dmabuf_bulk(struct net_devmem_dmabuf_binding *binding, + netmem_ref *netmems, unsigned int count) { - struct dmabuf_genpool_chunk_owner *owner; - unsigned long dma_addr; - struct net_iov *niov; - ssize_t offset; - ssize_t index; + unsigned int i; - dma_addr = gen_pool_alloc_owner(binding->chunk_pool, - 1UL << binding->niov_shift, - (void **)&owner); - if (!dma_addr) - return NULL; + spin_lock_bh(&binding->freelist_lock); - offset = dma_addr - owner->base_dma_addr; - index = offset >> binding->niov_shift; - niov = &owner->area.niovs[index]; + count = min_t(size_t, count, binding->free_count); + for (i = 0; i < count; i++) { + struct net_iov *niov = binding->freelist[--binding->free_count]; - niov->desc.pp_magic = 0; - niov->desc.pp = NULL; - atomic_long_set(&niov->desc.pp_ref_count, 0); + netmems[i] = net_iov_to_netmem(niov); + } - return niov; + spin_unlock_bh(&binding->freelist_lock); + + return count; } void net_devmem_free_dmabuf(struct net_iov *niov) { struct net_devmem_dmabuf_binding *binding = net_devmem_iov_binding(niov); - unsigned long dma_addr = net_devmem_get_dma_addr(niov); - size_t niov_size = 1UL << binding->niov_shift; - if (WARN_ON(!gen_pool_has_addr(binding->chunk_pool, dma_addr, - niov_size))) + spin_lock_bh(&binding->freelist_lock); + if (WARN_ON_ONCE(binding->free_count >= binding->area.num_niovs)) { + spin_unlock_bh(&binding->freelist_lock); return; + } - gen_pool_free(binding->chunk_pool, dma_addr, niov_size); + binding->freelist[binding->free_count++] = niov; + spin_unlock_bh(&binding->freelist_lock); } void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding) @@ -198,8 +168,9 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, static u32 id_alloc_next; struct scatterlist *sg; struct dma_buf *dmabuf; - unsigned int sg_idx, i; - unsigned long virtual; + unsigned int sg_idx; + size_t niov_idx; + size_t i; int err; if (!dma_dev) { @@ -249,33 +220,46 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, goto err_detach; } + if (!IS_ALIGNED(dmabuf->size, niov_size)) { + err = -EINVAL; + NL_SET_ERR_MSG_FMT(extack, + "dmabuf size %zu not aligned to niov size %zu", + dmabuf->size, niov_size); + goto err_unmap; + } + + binding->area.base_virtual = 0; + binding->area.num_niovs = dmabuf->size >> niov_shift; if (direction == DMA_TO_DEVICE) { - if (!IS_ALIGNED(dmabuf->size, PAGE_SIZE)) { - err = -EINVAL; - NL_SET_ERR_MSG(extack, "TX dma-buf size must be a multiple of PAGE_SIZE"); - goto err_unmap; - } binding->tx_vec = kvmalloc_objs(struct net_iov *, - dmabuf->size / PAGE_SIZE); + binding->area.num_niovs); if (!binding->tx_vec) { err = -ENOMEM; goto err_unmap; } + } else { + spin_lock_init(&binding->freelist_lock); + binding->freelist = kvmalloc_array(binding->area.num_niovs, + sizeof(binding->freelist[0]), + GFP_KERNEL); + if (!binding->freelist) { + err = -ENOMEM; + goto err_unmap; + } } - - binding->chunk_pool = gen_pool_create(niov_shift, - dev_to_node(&dev->dev)); - if (!binding->chunk_pool) { + binding->area.niovs = kvmalloc_objs(*binding->area.niovs, + binding->area.num_niovs); + if (!binding->area.niovs) { err = -ENOMEM; - goto err_tx_vec; + goto err_free_freelist; } - virtual = 0; + niov_idx = 0; for_each_sgtable_dma_sg(binding->sgt, sg, sg_idx) { dma_addr_t dma_addr = sg_dma_address(sg); - struct dmabuf_genpool_chunk_owner *owner; size_t len = sg_dma_len(sg); struct net_iov *niov; + size_t nr_niovs; if (!IS_ALIGNED(dma_addr, niov_size) || !IS_ALIGNED(len, niov_size)) { @@ -283,64 +267,37 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, NL_SET_ERR_MSG_FMT(extack, "dmabuf sg entry (addr=%pad, len=%zu) not aligned to niov size %zu", &dma_addr, len, niov_size); - goto err_free_chunks; + goto err_free_niovs; } - owner = kzalloc_node(sizeof(*owner), GFP_KERNEL, - dev_to_node(&dev->dev)); - if (!owner) { - err = -ENOMEM; - goto err_free_chunks; - } - - owner->area.base_virtual = virtual; - owner->base_dma_addr = dma_addr; - owner->area.num_niovs = len >> niov_shift; - owner->binding = binding; - - err = gen_pool_add_owner(binding->chunk_pool, dma_addr, - dma_addr, len, dev_to_node(&dev->dev), - owner); - if (err) { - kfree(owner); - err = -EINVAL; - goto err_free_chunks; - } - - owner->area.niovs = kvmalloc_objs(*owner->area.niovs, - owner->area.num_niovs); - if (!owner->area.niovs) { - err = -ENOMEM; - goto err_free_chunks; - } - - for (i = 0; i < owner->area.num_niovs; i++) { - niov = &owner->area.niovs[i]; - net_iov_init(niov, &owner->area, NET_IOV_DMABUF); + nr_niovs = len >> niov_shift; + for (i = 0; i < nr_niovs; i++, niov_idx++) { + niov = &binding->area.niovs[niov_idx]; + net_iov_init(niov, &binding->area, NET_IOV_DMABUF); page_pool_set_dma_addr_netmem(net_iov_to_netmem(niov), - net_devmem_get_dma_addr(niov)); + dma_addr); if (direction == DMA_TO_DEVICE) - binding->tx_vec[owner->area.base_virtual / PAGE_SIZE + i] = niov; + binding->tx_vec[niov_idx] = niov; + else + binding->freelist[binding->free_count++] = niov; + dma_addr += niov_size; } - - virtual += len; } err = xa_alloc_cyclic(&net_devmem_dmabuf_bindings, &binding->id, binding, xa_limit_32b, &id_alloc_next, GFP_KERNEL); if (err < 0) - goto err_free_chunks; + goto err_free_niovs; list_add(&binding->list, &priv->bindings); return binding; -err_free_chunks: - gen_pool_for_each_chunk(binding->chunk_pool, - net_devmem_dmabuf_free_chunk_owner, NULL); - gen_pool_destroy(binding->chunk_pool); -err_tx_vec: +err_free_niovs: + kvfree(binding->area.niovs); +err_free_freelist: + kvfree(binding->freelist); kvfree(binding->tx_vec); err_unmap: dma_buf_unmap_attachment_unlocked(binding->attachment, binding->sgt, @@ -471,20 +428,35 @@ int mp_dmabuf_devmem_init(struct page_pool *pool) netmem_ref mp_dmabuf_devmem_alloc_netmems(struct page_pool *pool, gfp_t gfp) { struct net_devmem_dmabuf_binding *binding = pool->mp_priv; - struct net_iov *niov; - netmem_ref netmem; + netmem_ref *netmems = pool->alloc.cache; + unsigned int allocated, i; - niov = net_devmem_alloc_dmabuf(binding); - if (!niov) + if (WARN_ON_ONCE(pool->alloc.count)) return 0; - netmem = net_iov_to_netmem(niov); + allocated = net_devmem_alloc_dmabuf_bulk(binding, netmems, + PP_ALLOC_CACHE_REFILL); + if (unlikely(!allocated)) + return 0; - page_pool_set_pp_info(pool, netmem); + for (i = 0; i < allocated; i++) { + struct net_iov *niov = netmem_to_net_iov(netmems[i]); - pool->pages_state_hold_cnt++; - trace_page_pool_state_hold(pool, netmem, pool->pages_state_hold_cnt); - return netmem; + niov->desc.pp_magic = 0; + niov->desc.pp = NULL; + atomic_long_set(&niov->desc.pp_ref_count, 0); + + page_pool_set_pp_info(pool, netmems[i]); + + pool->pages_state_hold_cnt++; + trace_page_pool_state_hold(pool, netmems[i], + pool->pages_state_hold_cnt); + } + + /* Return the last one, the rest stay in the page_pool cache. */ + allocated--; + pool->alloc.count = allocated; + return netmems[allocated]; } void mp_dmabuf_devmem_destroy(struct page_pool *pool) diff --git a/net/core/devmem.h b/net/core/devmem.h index 4a293a7d1149..f71d9a2bcb3d 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -16,6 +16,7 @@ struct netlink_ext_ack; struct net_devmem_dmabuf_binding { + struct net_iov_area area; struct dma_buf *dmabuf; struct dma_buf_attachment *attachment; struct sg_table *sgt; @@ -26,7 +27,6 @@ struct net_devmem_dmabuf_binding { * dereferenced. */ void *vdev; - struct gen_pool *chunk_pool; /* Protect dev */ struct mutex lock; @@ -57,6 +57,10 @@ struct net_devmem_dmabuf_binding { /* rxq's this binding is active on. */ struct xarray bound_rxqs; + spinlock_t freelist_lock ____cacheline_aligned_in_smp; + size_t free_count; + struct net_iov **freelist; + /* ID of this binding. Globally unique to all bindings currently * active. */ @@ -77,19 +81,6 @@ struct net_devmem_dmabuf_binding { }; #if defined(CONFIG_NET_DEVMEM) -/* Owner of the dma-buf chunks inserted into the gen pool. Each scatterlist - * entry from the dmabuf is inserted into the genpool as a chunk, and needs - * this owner struct to keep track of some metadata necessary to create - * allocations from this chunk. - */ -struct dmabuf_genpool_chunk_owner { - struct net_iov_area area; - struct net_devmem_dmabuf_binding *binding; - - /* dma_addr of the start of the chunk. */ - dma_addr_t base_dma_addr; -}; - void __net_devmem_dmabuf_binding_free(struct work_struct *wq); struct net_devmem_dmabuf_binding * net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, @@ -104,18 +95,11 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, struct net_devmem_dmabuf_binding *binding, struct netlink_ext_ack *extack); -static inline struct dmabuf_genpool_chunk_owner * -net_devmem_iov_to_chunk_owner(const struct net_iov *niov) -{ - struct net_iov_area *owner = net_iov_owner(niov); - - return container_of(owner, struct dmabuf_genpool_chunk_owner, area); -} - static inline struct net_devmem_dmabuf_binding * net_devmem_iov_binding(const struct net_iov *niov) { - return net_devmem_iov_to_chunk_owner(niov)->binding; + return container_of(net_iov_owner(niov), + struct net_devmem_dmabuf_binding, area); } static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov) @@ -125,11 +109,11 @@ static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov) static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov) { - struct dmabuf_genpool_chunk_owner *co = - net_devmem_iov_to_chunk_owner(niov); + struct net_devmem_dmabuf_binding *binding = + net_devmem_iov_binding(niov); return net_iov_owner(niov)->base_virtual + - ((unsigned long)net_iov_idx(niov) << co->binding->niov_shift); + ((unsigned long)net_iov_idx(niov) << binding->niov_shift); } static inline bool @@ -147,8 +131,6 @@ net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding) void net_devmem_get_net_iov(struct net_iov *niov); void net_devmem_put_net_iov(struct net_iov *niov); -struct net_iov * -net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding); void net_devmem_free_dmabuf(struct net_iov *ppiov); @@ -205,12 +187,6 @@ net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, return -EOPNOTSUPP; } -static inline struct net_iov * -net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) -{ - return NULL; -} - static inline void net_devmem_free_dmabuf(struct net_iov *ppiov) { }