skb_frag_address() goes through struct page, so a net_iov never has an address. Only unreadable memory should lack one: XDP and BPF read fragments through this helper, and AF_XDP UMEM will back readable net_iovs. netmem_address() also returns NULL, not a bogus pointer, for an unmapped highmem page. Generic code needs the size of a net_iov to find its address, so keep it in struct net_iov_area instead of the devmem binding. Suggested-by: Mina Almasry Suggested-by: Stanislav Fomichev Signed-off-by: Björn Töpel --- include/linux/skbuff.h | 21 +++++++++------------ include/net/netmem.h | 6 ++++-- io_uring/zcrx.c | 1 + net/core/devmem.c | 8 ++++---- net/core/devmem.h | 4 +--- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 73365c5de6f9..8b0039d49905 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3759,33 +3759,30 @@ int skb_cow_data_for_xdp(struct page_pool *pool, struct sk_buff **pskb, * skb_frag_address - gets the address of the data contained in a paged fragment * @frag: the paged fragment buffer * - * Returns: the address of the data within @frag. The page must already - * be mapped. + * Returns: the address of the data within @frag, or %NULL if the CPU cannot + * read it. */ static inline void *skb_frag_address(const skb_frag_t *frag) { - if (!skb_frag_page(frag)) + void *ptr = netmem_address(skb_frag_netmem(frag)); + + if (!ptr) return NULL; - return page_address(skb_frag_page(frag)) + skb_frag_off(frag); + return ptr + skb_frag_off(frag); } /** * skb_frag_address_safe - gets the address of the data contained in a paged fragment * @frag: the paged fragment buffer * - * Returns: the address of the data within @frag. Checks that the page - * is mapped and returns %NULL otherwise. + * Returns: the address of the data within @frag. Checks that the memory + * is mapped and readable and returns %NULL otherwise. */ static inline void *skb_frag_address_safe(const skb_frag_t *frag) { - struct page *page = skb_frag_page(frag); - void *ptr; + void *ptr = netmem_address(skb_frag_netmem(frag)); - if (!page) - return NULL; - - ptr = page_address(page); if (unlikely(!ptr)) return NULL; diff --git a/include/net/netmem.h b/include/net/netmem.h index fb693bc1702a..f196b014184c 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -72,8 +72,8 @@ enum net_iov_type { /* A memory descriptor representing abstract networking I/O vectors. * - * net_iovs are allocated and used by networking code, and the size of - * the chunk is PAGE_SIZE. + * net_iovs are allocated and used by networking code. Their area sets the + * size of a chunk, see net_iov_area.niov_shift. * * Examples include imported dmabuf memory and imported io_uring memory. See * net_iov_type for all the supported types. While current net_iov types are @@ -104,6 +104,8 @@ struct net_iov_area { /* Array of net_iovs for this area. */ struct net_iov *niovs; size_t num_niovs; + /* Each net_iov covers 1 << niov_shift bytes. */ + unsigned int niov_shift; }; static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov) diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c index 6d7abb1636e7..555f27e6c1ba 100644 --- a/io_uring/zcrx.c +++ b/io_uring/zcrx.c @@ -542,6 +542,7 @@ static int __zcrx_create_area(struct io_zcrx_ifq *ifq, ifq->niov_shift = buf_size_shift; nr_iovs = area->mem.size >> ifq->niov_shift; area->nia.num_niovs = nr_iovs; + area->nia.niov_shift = buf_size_shift; ret = -ENOMEM; area->nia.niovs = kvmalloc_objs(area->nia.niovs[0], nr_iovs, diff --git a/net/core/devmem.c b/net/core/devmem.c index 5c7d9640e9d1..b9146b1f294a 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -287,8 +287,8 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, u32 xa_idx; int err; - if (binding->niov_shift != PAGE_SHIFT) - mp_params.rx_page_size = 1U << binding->niov_shift; + if (binding->area.niov_shift != PAGE_SHIFT) + mp_params.rx_page_size = 1U << binding->area.niov_shift; rxq = __netif_get_rx_queue(dev, rxq_idx); err = xa_alloc(&binding->bound_rxqs, &xa_idx, rxq, xa_limit_32b, @@ -342,7 +342,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev, binding->vdev = vdev; write_pnet(&binding->net, sock_net(nl_sk)); binding->uid = sk_uid(nl_sk); - binding->niov_shift = niov_shift; + binding->area.niov_shift = niov_shift; xa_init_flags(&binding->bound_rxqs, XA_FLAGS_ALLOC); err = percpu_ref_init(&binding->ref, @@ -605,7 +605,7 @@ int mp_dmabuf_devmem_init(struct page_pool *pool) pool->dma_sync = false; pool->dma_sync_for_cpu = false; - if (pool->p.order != binding->niov_shift - PAGE_SHIFT) + if (pool->p.order != binding->area.niov_shift - PAGE_SHIFT) return -E2BIG; net_devmem_dmabuf_binding_get(binding); diff --git a/net/core/devmem.h b/net/core/devmem.h index c4f476947dfa..448521990ad7 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -83,8 +83,6 @@ struct net_devmem_dmabuf_binding { */ struct net_iov **tx_vec; - unsigned int niov_shift; - struct work_struct unbind_w; }; @@ -127,7 +125,7 @@ static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov) struct net_devmem_dmabuf_binding *binding = net_devmem_iov_binding(niov); - return (unsigned long)net_iov_idx(niov) << binding->niov_shift; + return (unsigned long)net_iov_idx(niov) << binding->area.niov_shift; } static inline bool -- 2.56.0