Switch kbuf mappings to vm_insert_pages()
Subsystems (3)
Jens Axboe
maintainercover Switch kbuf mappings to vm_insert_pages()
Hi,
This series cleans up kbuf management a bit.
First two patches get rid of our array of buffer_lists, as in my testing there's no discernable difference between the xarray lookup and our array. This also then gets rid of any difference between lower and higher buffer group IDs, which is nice.
Patch 3 starts using vmap for the non-mmap case for provided buffer rings, which means we can clean up the buffer indexing in io_ring_buffer_select() as well as there's now no difference between how we handle mmap vs gup versionf of buffer lists.
Patches 4 and 5 are prep patches for patch 6, which switches the mmap buffer_list variant away from remap_pfn_range() and uses vm_insert_pages() instead.
This is how it should've been done initially, and as per the diffstat, it's a nice reduction in code as well.
include/linux/io_uring_types.h | 4 - io_uring/io_uring.c | 32 ++--- io_uring/io_uring.h | 3 - io_uring/kbuf.c | 298 ++++++++++++++--------------------------- io_uring/kbuf.h | 8 +- mm/nommu.c | 7 + 6 files changed, 119 insertions(+), 233 deletions(-)
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 1/6 io_uring/kbuf: get rid of lower BGID lists
Just rely on the xarray for any kind of bgid. This simplifies things, and it really doesn't bring us much, if anything.
--- include/linux/io_uring_types.h | 1 - io_uring/io_uring.c | 2 - io_uring/kbuf.c | 70 ++++------------------------------ 3 files changed, 8 insertions(+), 65 deletions(-)
include/linux/io_uring_types.h +0 -1
@@ -295,7 +295,6 @@ struct io_ring_ctx {struct io_submit_state submit_state; -struct io_buffer_list *io_bl;struct xarray io_bl_xa;struct io_hash_table cancel_table_locked;
io_uring/io_uring.c +0 -2
@@ -342,7 +342,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) err:kfree (ctx->cancel_table.hbs);kfree (ctx->cancel_table_locked.hbs); -kfree (ctx->io_bl);xa_destroy (&ctx->io_bl_xa);kfree (ctx);return NULL ;
@@ -2849,7 +2848,6 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)io_napi_free (ctx);kfree (ctx->cancel_table.hbs);kfree (ctx->cancel_table_locked.hbs); -kfree (ctx->io_bl);xa_destroy (&ctx->io_bl_xa);kfree (ctx); }
io_uring/kbuf.c +8 -62
138 lines not on this page · raw message
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH 2/6 io_uring/kbuf: get rid of bl->is_ready
In reply to Jens AxboeNow that xarray is being exclusively used for the buffer_list lookup, this check is no longer needed. Get rid of it and the is_ready member.
--- io_uring/kbuf.c | 8 -------- io_uring/kbuf.h | 2 -- 2 files changed, 10 deletions(-)
io_uring/kbuf.c +0 -8
@@ -61,7 +61,6 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx,* always under the ->uring_lock, but the RCU lookup from mmap does. */ bl->bgid = bgid; -smp_store_release (&bl->is_ready,1 );return xa_err (xa_store (&ctx->io_bl_xa, bgid, bl,GFP_KERNEL )); }
@@ -721,13 +720,6 @@ void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)if (!bl || !bl->is_mmap)return NULL ; -/* -* Ensure the list is fully setup. Only strictly needed for RCU lookup -* via mmap, and in that case only for the array indexed groups. For -* the xarray lookups, it's either visible and ready, or not at all. -*/ -if (!smp_load_acquire (&bl->is_ready)) -return NULL ;return bl->buf_ring; }
io_uring/kbuf.h +0 -2
@@ -29,8 +29,6 @@ struct io_buffer_list {__u8 is_buf_ring;/* ring mapped provided buffers, but mmap'ed by application */ __u8 is_mmap; -/* bl is visible from an RCU point of view for lookup */ -__u8 is_ready; };struct io_buffer {
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH 3/6 io_uring/kbuf: vmap pinned buffer ring
In reply to Jens AxboeThis avoids needing to care about HIGHMEM, and it makes the buffer indexing easier as both ring provided buffer methods are now virtually mapped in a contigious fashion.
--- io_uring/kbuf.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-)
io_uring/kbuf.c +15 -24
@@ -7,6 +7,7 @@#include <linux/slab.h> #include <linux/namei.h> #include <linux/poll.h> +#include <linux/vmalloc.h> #include <linux/io_uring.h> #include <uapi/linux/io_uring.h>
@@ -145,15 +146,7 @@ static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len, req->flags |=REQ_F_BL_EMPTY ; head &= bl->mask; -/* mmaped buffers are always contig */ -if (bl->is_mmap || head <IO_BUFFER_LIST_BUF_PER_PAGE ) { - buf = &br->bufs[head]; - }else { -int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE -1 ); -int index = head /IO_BUFFER_LIST_BUF_PER_PAGE ; - buf =page_address (bl->buf_pages[index]); - buf += off; - } + buf = &br->bufs[head];if (*len ==0 || *len > buf->len) *len = buf->len; req->flags |=REQ_F_BUFFER_RING ;
@@ -240,6 +233,7 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx,for (j =0 ; j < bl->buf_nr_pages; j++)unpin_user_page (bl->buf_pages[j]);kvfree (bl->buf_pages); +vunmap (bl->buf_ring); bl->buf_pages =NULL ; bl->buf_nr_pages =0 ; }
@@ -490,9 +484,9 @@ int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)static int io_pin_pbuf_ring (struct io_uring_buf_reg *reg,struct io_buffer_list *bl) { -struct io_uring_buf_ring *br; +struct io_uring_buf_ring *br =NULL ; +int nr_pages, ret, i;struct page **pages; -int i, nr_pages; pages = io_pin_pages(reg->ring_addr, flex_array_size(br, bufs, reg->ring_entries),
@@ -500,18 +494,12 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,if (IS_ERR (pages))return PTR_ERR (pages); -/* -* Apparently some 32-bit boxes (ARM) will return highmem pages, -* which then need to be mapped. We could support that, but it'd -* complicate the code and slowdown the common cases quite a bit. -* So just error out, returning -EINVAL just like we did on kernels -* that didn't support mapped buffer rings. -*/ -for (i =0 ; i < nr_pages; i++) -if (PageHighMem (pages[i])) - goto error_unpin; + br =vmap (pages, nr_pages,VM_MAP ,PAGE_KERNEL ); +if (!br) { + ret = -ENOMEM ; + goto error_unpin; + } - br =page_address (pages[0 ]);#ifdef SHM_COLOUR /* * On platforms that have specific aliasing requirements, SHM_COLOUR
@@ -522,8 +510,10 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,* should use IOU_PBUF_RING_MMAP instead, and liburing will handle * this transparently. */ -if ((reg->ring_addr | (unsigned long ) br) & (SHM_COLOUR -1 )) +if ((reg->ring_addr | (unsigned long ) br) & (SHM_COLOUR -1 )) { + ret = -EINVAL ; goto error_unpin; + }#endif bl->buf_pages = pages; bl->buf_nr_pages = nr_pages;
@@ -535,7 +525,8 @@ static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,for (i =0 ; i < nr_pages; i++)unpin_user_page (pages[i]);kvfree (pages); -return -EINVAL ; +vunmap (br); +return ret; }/*
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH 4/6 io_uring/kbuf: protect io_buffer_list teardown with a reference
In reply to Jens AxboeNo functional changes in this patch, just in preparation for being able to keep the buffer list alive outside of the ctx->uring_lock.
--- io_uring/kbuf.c | 15 +++++++++++---- io_uring/kbuf.h | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-)
io_uring/kbuf.c +11 -4
@@ -62,6 +62,7 @@ static int io_buffer_add_list(struct io_ring_ctx *ctx,* always under the ->uring_lock, but the RCU lookup from mmap does. */ bl->bgid = bgid; +atomic_set (&bl->refs,1 );return xa_err (xa_store (&ctx->io_bl_xa, bgid, bl,GFP_KERNEL )); }
@@ -259,6 +260,14 @@ static int __io_remove_buffers(struct io_ring_ctx *ctx,return i; } +static void io_put_bl (struct io_ring_ctx *ctx,struct io_buffer_list *bl) +{ +if (atomic_dec_and_test (&bl->refs)) { +__io_remove_buffers (ctx, bl,-1U ); +kfree_rcu (bl, rcu); + } +} +void io_destroy_buffers (struct io_ring_ctx *ctx) {struct io_buffer_list *bl;
@@ -268,8 +277,7 @@ void io_destroy_buffers(struct io_ring_ctx *ctx)xa_for_each (&ctx->io_bl_xa, index, bl) {xa_erase (&ctx->io_bl_xa, bl->bgid); -__io_remove_buffers (ctx, bl,-1U ); -kfree_rcu (bl, rcu); +io_put_bl (ctx, bl); }/*
@@ -671,9 +679,8 @@ int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)if (!bl->is_buf_ring)return -EINVAL ; -__io_remove_buffers (ctx, bl,-1U );xa_erase (&ctx->io_bl_xa, bl->bgid); -kfree_rcu (bl, rcu); +io_put_bl (ctx, bl);return 0 ; }
io_uring/kbuf.h +2 -0
@@ -25,6 +25,8 @@ struct io_buffer_list {__u16 head;__u16 mask; +atomic_t refs; +/* ring mapped provided buffers */ __u8 is_buf_ring;/* ring mapped provided buffers, but mmap'ed by application */
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH 5/6 mm: add nommu variant of vm_insert_pages()
In reply to Jens AxboeAn identical one exists for vm_insert_page(), add one for vm_insert_pages() to avoid needing to check for CONFIG_MMU in code using it.
--- mm/nommu.c | 7 +++++++ 1 file changed, 7 insertions(+)
mm/nommu.c +7 -0
@@ -355,6 +355,13 @@ int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, }EXPORT_SYMBOL (vm_insert_page); +int vm_insert_pages (struct vm_area_struct *vma,unsigned long addr, +struct page **pages,unsigned long *num) +{ +return -EINVAL ; +} +EXPORT_SYMBOL (vm_insert_pages); +int vm_map_pages (struct vm_area_struct *vma,struct page **pages,unsigned long num) {
Signature · 1 line
2.43.0
Jens Axboe
maintainerPATCH 6/6 io_uring/kbuf: use vm_insert_pages() for mmap'ed pbuf ring
In reply to Jens AxboeRather than use remap_pfn_range() for this and manually free later, switch to using vm_insert_page() and have it Just Work.
This requires a bit of effort on the mmap lookup side, as the ctx uring_lock isn't held, which otherwise protects buffer_lists from being torn down, and it's not safe to grab from mmap context that would introduce an ABBA deadlock between the mmap lock and the ctx uring_lock. Instead, lookup the buffer_list under RCU, as the the list is RCU freed already. Use the existing reference count to determine whether it's possible to safely grab a reference to it (eg if it's not zero already), and drop that reference when done with the mapping. If the mmap reference is the last one, the buffer_list and the associated memory can go away, since the vma insertion has references to the inserted pages at that point.
--- include/linux/io_uring_types.h | 3 - io_uring/io_uring.c | 30 ++---- io_uring/io_uring.h | 3 - io_uring/kbuf.c | 178 +++++++++++++-------------------- io_uring/kbuf.h | 4 +- 5 files changed, 82 insertions(+), 136 deletions(-)
include/linux/io_uring_types.h +0 -3
@@ -370,9 +370,6 @@ struct io_ring_ctx {struct list_head io_buffers_cache; -/* deferred free list, protected by ->uring_lock */ -struct hlist_head io_buf_list; -/* Keep this last, we don't need it for the fast path */ struct wait_queue_head poll_wq;struct io_restriction restrictions;
io_uring/io_uring.c +10 -20
@@ -303,7 +303,6 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)INIT_LIST_HEAD (&ctx->sqd_list);INIT_LIST_HEAD (&ctx->cq_overflow_list);INIT_LIST_HEAD (&ctx->io_buffers_cache); -INIT_HLIST_HEAD (&ctx->io_buf_list);io_alloc_cache_init (&ctx->rsrc_node_cache,IO_NODE_ALLOC_CACHE_MAX ,sizeof (struct io_rsrc_node )); io_alloc_cache_init(&ctx->apoll_cache, IO_ALLOC_CACHE_MAX,
@@ -2615,7 +2614,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,return READ_ONCE (rings->cq.head) ==READ_ONCE (rings->cq.tail) ? ret :0 ; } -void io_mem_free (void *ptr) +static void io_mem_free (void *ptr) {if (!ptr)return ;
@@ -2728,7 +2727,7 @@ static void io_rings_free(struct io_ring_ctx *ctx) ctx->sq_sqes =NULL ; } -void *io_mem_alloc (size_t size) +static void *io_mem_alloc (size_t size) {gfp_t gfp =GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;void *ret;
@@ -2838,7 +2837,6 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) ctx->mm_account =NULL ; }io_rings_free (ctx); -io_kbuf_mmap_list_free (ctx);percpu_ref_exit (&ctx->refs);free_uid (ctx->user);
@@ -3307,11 +3305,9 @@ static void *io_uring_validate_mmap_request(struct file *file, loff_t pgoff, size_t sz) {struct io_ring_ctx *ctx = file->private_data; -loff_t offset = pgoff <<PAGE_SHIFT ; -struct page *page;void *ptr; -switch (offset &IORING_OFF_MMAP_MASK ) { +switch ((pgoff <<PAGE_SHIFT ) &IORING_OFF_MMAP_MASK ) {case IORING_OFF_SQ_RING :case IORING_OFF_CQ_RING :/* Don't allow mmap if the ring was setup without it */
@@ -3325,23 +3321,13 @@ static void *io_uring_validate_mmap_request(struct file *file,return ERR_PTR (-EINVAL ); ptr = ctx->sq_sqes;break ; -case IORING_OFF_PBUF_RING : { -unsigned int bgid; - - bgid = (offset & ~IORING_OFF_MMAP_MASK ) >>IORING_OFF_PBUF_SHIFT ; -rcu_read_lock (); - ptr =io_pbuf_get_address (ctx, bgid); -rcu_read_unlock (); -if (!ptr) -return ERR_PTR (-EINVAL ); -break ; - } +case IORING_OFF_PBUF_RING : +return 0 ;default :return ERR_PTR (-EINVAL ); } - page =virt_to_head_page (ptr); -if (sz >page_size (page)) +if (sz >page_size (virt_to_head_page (ptr)))return ERR_PTR (-EINVAL );return ptr;
@@ -3352,6 +3338,7 @@ static void *io_uring_validate_mmap_request(struct file *file,static __cold int io_uring_mmap (struct file *file,struct vm_area_struct *vma) {size_t sz = vma->vm_end - vma->vm_start; +long offset = vma->vm_pgoff <<PAGE_SHIFT ;unsigned long pfn;void *ptr;
@@ -3359,6 +3346,9 @@ static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma)if (IS_ERR (ptr))return PTR_ERR (ptr); +if ((offset &IORING_OFF_MMAP_MASK ) ==IORING_OFF_PBUF_RING ) +return io_pbuf_mmap (file, vma); + pfn =virt_to_phys (ptr) >>PAGE_SHIFT ;return remap_pfn_range (vma, vma->vm_start, pfn, sz, vma->vm_page_prot); }
io_uring/io_uring.h +0 -3
@@ -108,9 +108,6 @@ bool __io_alloc_req_refill(struct io_ring_ctx *ctx);bool io_match_task_safe (struct io_kiocb *head,struct task_struct *task,bool cancel_all); -void *io_mem_alloc (size_t size); -void io_mem_free (void *ptr); -enum {IO_EVENTFD_OP_SIGNAL_BIT ,IO_EVENTFD_OP_FREE_BIT ,
io_uring/kbuf.c +71 -107
236 lines not on this page · raw message
io_uring/kbuf.h +1 -3
@@ -55,13 +55,11 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);int io_unregister_pbuf_ring (struct io_ring_ctx *ctx,void __user *arg);int io_register_pbuf_status (struct io_ring_ctx *ctx,void __user *arg); -void io_kbuf_mmap_list_free (struct io_ring_ctx *ctx); -void __io_put_kbuf (struct io_kiocb *req,unsigned issue_flags);bool io_kbuf_recycle_legacy (struct io_kiocb *req,unsigned issue_flags); -void *io_pbuf_get_address (struct io_ring_ctx *ctx,unsigned long bgid); +int io_pbuf_mmap (struct file *file,struct vm_area_struct *vma);static inline bool io_kbuf_recycle_ring (struct io_kiocb *req) {
Signature · 1 line
2.43.0