Introduce IORING_OP_MMAP
Subsystems (2)
cover Introduce IORING_OP_MMAP
Hi,
There's been a few requests over time for supporting mmap(2) over io_uring. The reasoning are twofold: 1) serving as base for batching multiple mappings in a single operation 2) supporting mmap of fixed files.
Since mmap can operate on either anonymous memory and file descriptors, patch 1 adds support for optional fds in io_uring commands. Patch 2 implements the mmap operation itself.
Note this patchset doesn't do any kind of smarter batching in MM. While we can potentially do some interesting optimizations already, like holding the MM write lock instead of reacquiring it for each mapping, I wanted to focus on the API discussion first. This is left as future work.
liburing support, including testcases, will be sent shortly to the list, but can also be found at:
https://github.com/krisman/liburing -b mmap
Thanks,
Gabriel Krisman Bertazi (2):
io_uring: Support commands with optional file descriptors
io_uring: introduce IORING_OP_MMAP
include/uapi/linux/io_uring.h | 10 +++ io_uring/Makefile | 2 +- io_uring/io_uring.c | 15 ++-- io_uring/mmap.c | 147 ++++++++++++++++++++++++++++++++++ io_uring/mmap.h | 4 + io_uring/opdef.c | 9 +++ io_uring/opdef.h | 2 + 7 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 io_uring/mmap.c create mode 100644 io_uring/mmap.h
Signature · 1 line
2.52.0
PATCH 1/2 io_uring: Support commands with optional file descriptors
mmap can be called either for file-backed memory or for anonymous memory in which case no fd is provided. This patch allows an io_uring command to request optional files for io_uring. If a fd is provided, io_uring loads it in the regular paths. Otherwise, req->file stays NULL.
At the SQE level, Use the ancient mmap semantics of fd == -1 to indicate no fd. This feels more useful than a flag or using 0. The later because I'd expect 0 to be commonly used for direct FDs. We can abstract this details in liburing. It is a bit ugly and it could be a flag elsewhere, but we don't need to waste a flag on that.
--- io_uring/io_uring.c | 15 ++++++++++----- io_uring/opdef.h | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-)
io_uring/io_uring.c +10 -5
@@ -1756,10 +1756,17 @@ static __cold void io_drain_req(struct io_kiocb *req) ctx->drain_active = false; } +static inline bool op_wants_file (const struct io_issue_def *def, +struct io_kiocb *req) +{ +return (def->needs_file || + (def->opt_file && req->cqe.fd !=-1 )); +} +static bool io_assign_file (struct io_kiocb *req,const struct io_issue_def *def,unsigned int issue_flags) { -if (req->file || !def->needs_file) +if (req->file || !op_wants_file (def, req))return true;if (req->flags &REQ_F_FIXED_FILE )
@@ -2200,11 +2207,9 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,if (!def->iopoll && (ctx->flags &IORING_SETUP_IOPOLL ))return io_init_fail_req (req, -EINVAL ); -if (def->needs_file) { + req->cqe.fd =READ_ONCE (sqe->fd); +if (op_wants_file (def, req)) {struct io_submit_state *state = &ctx->submit_state; - - req->cqe.fd =READ_ONCE (sqe->fd); -/* * Plug now if we have more than 2 IO left after this, and the * target is potentially a read/write to block based storage.
io_uring/opdef.h +2 -0
@@ -5,6 +5,8 @@struct io_issue_def {/* needs req->file assigned */ unsigned needs_file :1 ; +/* Optional req->file assigned, if available. */ +unsigned opt_file :1 ;/* should block plug */ unsigned plug :1 ;/* supports ioprio */
Signature · 1 line
2.52.0
PATCH 2/2 io_uring: introduce IORING_OP_MMAP
In reply to Gabriel Krisman BertaziThis enables mmap(2) over io_uring. The interesting part is allowing the mapping of multiple regions with different parameters in a single operation. This is not explored in this patch, but coalescing multiple operations can enable batching deeper in the MM layer.
The SQE provides an array of memory descriptors to be mapped backed by fd, or to anonymous memory if fd == -1. All descriptors are mapped against the same file, but protections and flags can vary.
The API also tries to be very clear about what failed in case of an error. The number of maps that succeeded is returned on the CQE, and the error code of the first failed map is passed back via the descriptor structure (which must live until completion).
--- include/uapi/linux/io_uring.h | 10 +++ io_uring/Makefile | 2 +- io_uring/mmap.c | 147 ++++++++++++++++++++++++++++++++++ io_uring/mmap.h | 4 + io_uring/opdef.c | 9 +++ 5 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 io_uring/mmap.c create mode 100644 io_uring/mmap.h
include/uapi/linux/io_uring.h +10 -0
@@ -74,6 +74,7 @@ struct io_uring_sqe {__u32 install_fd_flags;__u32 nop_flags;__u32 pipe_flags; +__u32 mmap_flags; };__u64 user_data;/* data to be passed back at completion time */ /* pack this to avoid bogus arm OABI complaints */
@@ -303,6 +304,7 @@ enum io_uring_op {IORING_OP_PIPE ,IORING_OP_NOP128 ,IORING_OP_URING_CMD128 , +IORING_OP_MMAP ,/* this goes last, obviously */ IORING_OP_LAST,
@@ -1113,6 +1115,14 @@ struct zcrx_ctrl { }; }; +struct io_uring_mmap_desc { +void __user *addr; +unsigned long len; +unsigned long pgoff; +unsigned int prot; +unsigned int flags; +}; + #ifdef __cplusplus }#endif
io_uring/Makefile +1 -1
@@ -13,7 +13,7 @@ obj-$(CONFIG_IO_URING) += io_uring.o opdef.o kbuf.o rsrc.o notif.o \ sync.o msg_ring.o advise.o openclose.o \ statx.o timeout.o cancel.o \ waitid.o register.o truncate.o \ - memmap.o alloc_cache.o query.o + memmap.o mmap.o alloc_cache.o query.o obj-$(CONFIG_IO_URING_ZCRX) += zcrx.o obj-$(CONFIG_IO_WQ) += io-wq.o obj-$(CONFIG_FUTEX) += futex.o
io_uring/mmap.c new file +147 -0
147 lines not on this page · raw message
io_uring/mmap.h new file +4 -0
@@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +int io_mmap_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); +int io_mmap (struct io_kiocb *req,unsigned int issue_flags);
io_uring/opdef.c +9 -0
@@ -29,6 +29,7 @@#include "epoll.h" #include "statx.h" #include "net.h" +#include "mmap.h" #include "msg_ring.h" #include "timeout.h" #include "poll.h"
@@ -593,6 +594,11 @@ const struct io_issue_def io_issue_defs[] = { .prep = io_uring_cmd_prep, .issue = io_uring_cmd, }, + [IORING_OP_MMAP] = { + .prep = io_mmap_prep, + .issue = io_mmap, + .opt_file = 1, + } };const struct io_cold_def io_cold_defs[] = {
@@ -851,6 +857,9 @@ const struct io_cold_def io_cold_defs[] = { .sqe_copy = io_uring_cmd_sqe_copy, .cleanup = io_uring_cmd_cleanup, }, + [IORING_OP_MMAP] = { + .name = "MMAP", + }, };const char *io_uring_get_opcode (u8 opcode)
Signature · 1 line
2.52.0
kernel test robot
build botRe: [PATCH 2/2] io_uring: introduce IORING_OP_MMAP
kernel test robot noticed the following build warnings:
Hi Gabriel,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v6.19-rc7]
[also build test WARNING on linus/master]
[cannot apply to axboe/for-next next-20260129]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Gabriel-Krisman-Bertazi/io_uring-Support-commands-with-optional-file-descriptors/20260130-061445 base: v6.19-rc7 patch link: https://lore.kernel.org/r/20260129221138.897715-3-krisman%40suse.de patch subject: [PATCH 2/2] io_uring: introduce IORING_OP_MMAP config: m68k-randconfig-r122-20260130 (https://download.01.org/0day-ci/archive/20260130/[email protected]/config) compiler: m68k-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260130/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> io_uring/mmap.c:116:36: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef] __user *addr @@ got void * @@
io_uring/mmap.c:116:36: sparse: expected void [noderef] __user *addr
io_uring/mmap.c:116:36: sparse: got void *
io_uring/mmap.c:125:44: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef] __user *addr @@ got void * @@
io_uring/mmap.c:125:44: sparse: expected void [noderef] __user *addr
io_uring/mmap.c:125:44: sparse: got void *
io_uring/mmap.c:130:28: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef] __user *addr @@ got void * @@
io_uring/mmap.c:130:28: sparse: expected void [noderef] __user *addr
io_uring/mmap.c:130:28: sparse: got void *
vim +116 io_uring/mmap.c
83
84 int io_mmap(struct io_kiocb *req, unsigned int issue_flags)
85 {
86 struct io_mmap_data *mmap = io_kiocb_to_cmd(req, struct io_mmap_data);
87 struct io_mmap_async *data = (struct io_mmap_async *) req->async_data;
88 int i, mapped, ret;
89
90 if (unlikely(mmap->flags & MAP_HUGETLB && req->file &&
91 !is_file_hugepages(req->file))) {
92 ret = -EINVAL;
93 goto out;
94 }
95
96 for (i = 0; i < data->nr_maps; i++) {
97 struct io_uring_mmap_desc *desc = &data->maps[i];
98
99 if (copy_from_user(desc, &mmap->uaddr[i], sizeof(*desc))) {
100 ret = -EFAULT;
101 goto out;
102 }
103 }
104
105 mapped = 0;
106 while (mapped < data->nr_maps) {
107 struct io_uring_mmap_desc *desc = &data->maps[mapped++];
108 unsigned long flags = (mmap->flags | desc->flags);
109 unsigned long len = desc->len;
110 struct file *file = req->file;
111
112 /* These cannot be mixed and matched. need to be passed
113 * on the SQE.
114 */
115 if (unlikely(desc->flags & (MAP_ANONYMOUS|MAP_HUGETLB))) {
> 116 desc->addr = ERR_PTR(-EINVAL);Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH 2/2] io_uring: introduce IORING_OP_MMAP
Quoted 26 lines · kernel test robot
Hi Gabriel,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v6.19-rc7]
[also build test WARNING on linus/master]
[cannot apply to axboe/for-next next-20260129]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url: https://github.com/intel-lab-lkp/linux/commits/Gabriel-Krisman-Bertazi/io_uring-Support-commands-with-optional-file-descriptors/20260130-061445 base: v6.19-rc7 patch link: https://lore.kernel.org/r/20260129221138.897715-3-krisman%40suse.de patch subject: [PATCH 2/2] io_uring: introduce IORING_OP_MMAP config: m68k-randconfig-r122-20260130 (https://download.01.org/0day-ci/archive/20260130/[email protected]/config) compiler: m68k-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260130/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/sparse warnings: (new ones prefixed by >>)
Quoted 1 line
Quoted 1 line
io_uring/mmap.c:116:36: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef]__user*addr @@ got void * @@
Quoted 8 lines
io_uring/mmap.c:116:36: sparse: expected void [noderef]__user*addr
io_uring/mmap.c:116:36: sparse: got void *
io_uring/mmap.c:125:44: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef]__user*addr @@ got void * @@
io_uring/mmap.c:125:44: sparse: expected void [noderef]__user*addr
io_uring/mmap.c:125:44: sparse: got void *
io_uring/mmap.c:130:28: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef]__user*addr @@ got void * @@
io_uring/mmap.c:130:28: sparse: expected void [noderef]__user*addr
io_uring/mmap.c:130:28: sparse: got void *
FWIW, for reviewers, these are false positives. The issue is I'm using "void* __user addr" to either return a pointer or the error code to user. It is properly copied back through copy_to_user, but sparse still complains. I'll look into silencing it.
Signature · 1 line
Gabriel Krisman Bertazi
Jens Axboe
maintainerRe: [PATCH 2/2] io_uring: introduce IORING_OP_MMAP
In reply to Gabriel Krisman BertaziQuoted 21 lines · Gabriel Krisman Bertazi
include/uapi/linux/io_uring.h +2
-0@@ -74,6 +74,7 @@ struct io_uring_sqe {__u32 install_fd_flags;__u32 nop_flags;__u32 pipe_flags; +__u32 mmap_flags; };__u64 user_data;/* data to be passed back at completion time */ /* pack this to avoid bogus arm OABI complaints */ @@ -303,6 +304,7 @@ enum io_uring_op {IORING_OP_PIPE ,IORING_OP_NOP128 ,IORING_OP_URING_CMD128 , +IORING_OP_MMAP ,/* this goes last, obviously */ IORING_OP_LAST,
include/uapi/linux/io_uring.h struct zcrx_ctrl { · line 1124
Quoted 10 lines
include/uapi/linux/io_uring.h +7
-0@@ -1113,6 +1115,14 @@ struct zcrx_ctrl { }; }; +struct io_uring_mmap_desc { +void __user *addr; +unsigned long len; +unsigned long pgoff; +unsigned int prot; +unsigned int flags; +};
You can't use pointers or unsigned long or unsigned int in a uapi, as they'd be different sizes on 32-bit and 64-bit. And then you need compat handling. It's much better to make this:
struct io_uring_mmap_desc {
__u64 addr
__u64 len;
__u64 pgoff;
__u32 prot;
__u32 flags;
};and then generally also a good idea to have a bit of expansion space there, so you don't need a new desc down the line.
Quoted 6 lines
include/uapi/linux/io_uring.h +6
-0+int io_mmap_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe) +{ + struct io_mmap_data *mmap = io_kiocb_to_cmd(req, struct io_mmap_data); +struct io_mmap_async *maps; +int nr_maps; +
include/uapi/linux/io_uring.h struct zcrx_ctrl {
Quoted 10 lines
include/uapi/linux/io_uring.h +10
-0+ mmap->uaddr =u64_to_user_ptr (READ_ONCE (sqe->addr)); + mmap->flags =READ_ONCE (sqe->mmap_flags); + nr_maps =READ_ONCE (sqe->len); + +if (mmap->flags &MAP_ANONYMOUS && req->cqe.fd !=-1 ) +return -EINVAL ; +if (nr_maps <0 || nr_maps >MMAP_MAX_BATCH ) +return -EINVAL ; +if (!access_ok (mmap->uaddr, nr_maps*sizeof (struct io_uring_mmap_desc ))) +return -EFAULT ;
Does this access_ok actually provide anything? We're copying it in later anyway, no?
Quoted 7 lines
include/uapi/linux/io_uring.h +7
-0+static int io_prep_mmap_hugetlb (struct file **filp,unsigned long *len, +int flags) +{ +if (*filp) { + *len =ALIGN (*len,huge_page_size (hstate_file (*filp))); + }else { +struct hstate *hs;
include/uapi/linux/io_uring.h
Quoted 10 lines
include/uapi/linux/io_uring.h +10
-0+unsigned long nlen = *len; + + hs =hstate_sizelog ((flags >>MAP_HUGE_SHIFT ) &MAP_HUGE_MASK ); +if (!hs) +return -EINVAL ; + nlen =ALIGN (nlen,huge_page_size (hs)); + *filp =hugetlb_file_setup (HUGETLB_ANON_FILE , nlen, +VM_NORESERVE , +HUGETLB_ANONHUGE_INODE , + (flags >>MAP_HUGE_SHIFT ) &MAP_HUGE_MASK );
This looks like it dips into vm_mmap_pgoff(). More on that below.
include/uapi/linux/io_uring.h
Quoted 3 lines
include/uapi/linux/io_uring.h +3
-0+ desc->addr = (void *)vm_mmap_pgoff (file, + (unsigned long ) desc->addr, + len, desc->prot, flags, desc->pgoff);
One concern here is that vm_mmap_pgoff() ends up doing:
mmap_write_lock_killable(mm)
grabs mm lock, can block, for a long time?
which could potentially stall the io_uring pipeline for a long time. Ideally you'd be able to do something where you try to grab the mm lock from io_mmap(), and if it fails, then either fail the request (if it's a killable thing) or punt it with -EAGAIN to let an io-wq thread handle it.
I'm not so sure simply wrapping vm_mmap_pgoff() either directly or indirectly via the hugetlb stuff is going to be super useful, if we can end up blocking for a long time on these operations.
Signature · 1 line
Jens Axboe
Re: [PATCH 2/2] io_uring: introduce IORING_OP_MMAP
Quoted 26 lines · Jens Axboe
Quoted 24 lines · Gabriel Krisman Bertazi
include/uapi/linux/io_uring.h +2
-0@@ -74,6 +74,7 @@ struct io_uring_sqe {__u32 install_fd_flags;__u32 nop_flags;__u32 pipe_flags; +__u32 mmap_flags; };__u64 user_data;/* data to be passed back at completion time */ /* pack this to avoid bogus arm OABI complaints */ @@ -303,6 +304,7 @@ enum io_uring_op {IORING_OP_PIPE ,IORING_OP_NOP128 ,IORING_OP_URING_CMD128 , +IORING_OP_MMAP ,/* this goes last, obviously */ IORING_OP_LAST,@@ -1113,6 +1115,14 @@ struct zcrx_ctrl { }; };
Quoted 9 lines
include/uapi/linux/io_uring.h struct zcrx_ctrl { · line 1124
Quoted 7 lines
include/uapi/linux/io_uring.h +7
-0+struct io_uring_mmap_desc { +void __user *addr; +unsigned long len; +unsigned long pgoff; +unsigned int prot; +unsigned int flags; +};You can't use pointers or unsigned long or unsigned int in a uapi, as
Thanks for the review, Jens.
I went on vacation right after sending this, but I will follow up with a v2 shortly.
Signature · 1 line
Gabriel Krisman Bertazi
Quoted 11 lines · Gabriel Krisman Bertazi
Hi,
There's been a few requests over time for supporting mmap(2) over
io_uring. The reasoning are twofold: 1) serving as base for batching multiple mappings in a single operation 2) supporting mmap of fixed files.Since mmap can operate on either anonymous memory and file descriptors, patch 1 adds support for optional fds in
io_uringcommands. Patch 2 implements the mmap operation itself.
Quoted 8 lines
Note this patchset doesn't do any kind of smarter batching in MM. While we can potentially do some interesting optimizations already, like holding the MM write lock instead of reacquiring it for each mapping, I wanted to focus on the API discussion first. This is left as future work.
liburing support, including testcases, will be sent shortly to the list, but can also be found at:
Just a general question: why do we unlock each syscall individually, and not in some intelligent way, all syscalls at once? :)
I assume supporting arbitrary syscalls could be rather hard (or am I wrong? :) ).
I also assume that we might want to block some syscalls (do we?), which could be done in some generic way (allow-list, deny-list etc).
Again, just curious.
Signature · 3 lines
Cheers David
Jens Axboe
maintainerQuoted 18 lines · David Hildenbrand (arm)
Quoted 17 lines · Gabriel Krisman Bertazi
Hi,
There's been a few requests over time for supporting mmap(2) over
io_uring. The reasoning are twofold: 1) serving as base for batching multiple mappings in a single operation 2) supporting mmap of fixed files.Since mmap can operate on either anonymous memory and file descriptors, patch 1 adds support for optional fds in
io_uringcommands. Patch 2 implements the mmap operation itself.Note this patchset doesn't do any kind of smarter batching in MM. While we can potentially do some interesting optimizations already, like holding the MM write lock instead of reacquiring it for each mapping, I wanted to focus on the API discussion first. This is left as future work.
Quoted 5 lines
Quoted 2 lines
liburing support, including testcases, will be sent shortly to the list, but can also be found at:
Just a general question: why do we unlock each syscall individually, and not in some intelligent way, all syscalls at once? :)
The hard part isn't enabling all syscalls at once, that could be trivially done with an IORING_OP_SYSCALL and the SQE carries arg0..argN. And for any nonblocking/simple syscall, that would Just Work. The challenge is for syscalls that block - the whole point of io_uring is that you should be able to do nonblock issues with sane retries. The futex series I did some time back is a good example of that - you modify the existing syscall to expose the waitqueue mechanism, which you can then use to wait in an async way, and get a callback when some action needs to be taken.
If you just allow blocking, then you're blocking the entire io_uring issue pipeline. Which was exactly my main complaint on this patchset, see the review reply to patch 2.
Quoted 2 lines
I assume supporting arbitrary syscalls could be rather hard (or am I wrong? :) ).
See above - it's both trivially easy if you ignore the problems, or somewhat harder as you'd need to refactor the underlying bits of that particular syscall first. For some of them, that's hard. Traditionally syscalls have been fully sync, they will just block and prevent the task from returning back to userspace until they are done. syscalls with io_uring is more involved as we simply cannot allow that kind of blocking.
Signature · 1 line
Jens Axboe
Quoted 22 lines · Jens Axboe
Quoted 21 lines · David Hildenbrand (arm)
Quoted 19 lines · Gabriel Krisman Bertazi
Hi,
There's been a few requests over time for supporting mmap(2) over
io_uring. The reasoning are twofold: 1) serving as base for batching multiple mappings in a single operation 2) supporting mmap of fixed files.Since mmap can operate on either anonymous memory and file descriptors, patch 1 adds support for optional fds in
io_uringcommands. Patch 2 implements the mmap operation itself.Note this patchset doesn't do any kind of smarter batching in MM. While we can potentially do some interesting optimizations already, like holding the MM write lock instead of reacquiring it for each mapping, I wanted to focus on the API discussion first. This is left as future work.
liburing support, including testcases, will be sent shortly to the list, but can also be found at:
Quoted 6 lines
Quoted 2 lines
Just a general question: why do we unlock each syscall individually, and not in some intelligent way, all syscalls at once? :)
The hard part isn't enabling all syscalls at once, that could be trivially done with an
IORING_OP_SYSCALLand the SQE carries arg0..argN. And for any nonblocking/simple syscall, that would Just Work.
Right, that's what I had in mind.
Quoted 11 lines
The
challenge is for syscalls that block - the whole point ofio_uringis that you should be able to do nonblock issues with sane retries. The futex series I did some time back is a good example of that - you modify the existing syscall to expose the waitqueue mechanism, which you can then use to wait in an async way, and get a callback when some action needs to be taken.If you just allow blocking, then you're blocking the entire
io_uringissue pipeline. Which was exactly my main complaint on this patchset, see the review reply to patch 2.
Makes sense. I was wondering whether that could be optimized internally in the stream of IORING_OP_SYSCALL.
But likely that would make it more tricky to optimize.
The patch set says "serving as base for batching
multiple mappings in a single operation", and I was wondering, why one wouldn't just also batch with mremap/munmap/ etc. in the future.
(BUT I am also skeptical whether holding the mmap lock in write mode longer instead of repeatedly grabbing it, allowing other operations that need it in read mode etc to make progress, is actually preferrable)
Quoted 11 lines
Quoted 3 lines
I assume supporting arbitrary syscalls could be rather hard (or am I wrong? :) ).
See above - it's both trivially easy if you ignore the problems, or somewhat harder as you'd need to refactor the underlying bits of that particular syscall first. For some of them, that's hard. Traditionally syscalls have been fully sync, they will just block and prevent the task from returning back to userspace until they are done. syscalls with
io_uringis more involved as we simply cannot allow that kind of blocking.
Thanks for pointing that out.
Signature · 3 lines
Cheers David
Jens Axboe
maintainerQuoted 40 lines · David Hildenbrand (arm)
Quoted 22 lines · Jens Axboe
Quoted 21 lines · David Hildenbrand (arm)
Quoted 19 lines · Gabriel Krisman Bertazi
Hi,
There's been a few requests over time for supporting mmap(2) over
io_uring. The reasoning are twofold: 1) serving as base for batching multiple mappings in a single operation 2) supporting mmap of fixed files.Since mmap can operate on either anonymous memory and file descriptors, patch 1 adds support for optional fds in
io_uringcommands. Patch 2 implements the mmap operation itself.Note this patchset doesn't do any kind of smarter batching in MM. While we can potentially do some interesting optimizations already, like holding the MM write lock instead of reacquiring it for each mapping, I wanted to focus on the API discussion first. This is left as future work.
liburing support, including testcases, will be sent shortly to the list, but can also be found at:
Quoted 6 lines
Quoted 2 lines
Just a general question: why do we unlock each syscall individually, and not in some intelligent way, all syscalls at once? :)
The hard part isn't enabling all syscalls at once, that could be trivially done with an
IORING_OP_SYSCALLand the SQE carries arg0..argN. And for any nonblocking/simple syscall, that would Just Work.Right, that's what I had in mind.
Quoted 8 lines
The
challenge is for syscalls that block - the whole point ofio_uringis that you should be able to do nonblock issues with sane retries. The futex series I did some time back is a good example of that - you modify the existing syscall to expose the waitqueue mechanism, which you can then use to wait in an async way, and get a callback when some action needs to be taken.
Quoted 8 lines
Quoted 3 lines
If you just allow blocking, then you're blocking the entire
io_uringissue pipeline. Which was exactly my main complaint on this patchset, see the review reply to patch 2.Makes sense. I was wondering whether that could be optimized internally in the stream of
IORING_OP_SYSCALL.But likely that would make it more tricky to optimize.
Are we talking generically, or mmap/munmap/mremap? You could trivially make IORING_OP_SYSCALL available and use it for everything, it'd just require a basically all of those to be offloaded to io-wq internally in io_uring. And that's not a great approach. The fast path for io_uring is running the opcode inline, which means that by the time the syscall returns, you have also posted the completion. If the operation can't complete inline, then the next best thing is to have it be triggered when it can complete, and then retry and post the completion. Think of reading from a pipe - if the data is there, the read is done inside io_uring_enter() when the read is attempted, and we're done. If no data is available, the operation is queued. When data becomes available, a retry is triggered, data is read, and a completion is posted.
For an old school kind of syscall "do this thing, and just block the task until it's done" doesn't work that way at all. Running those in io_uring would necessitate punting the operation to io-wq, which are helper userspace threads for io_uring. As there's no way of knowing whether syscallN will complete fast inline or block for 2 seconds, io_uring has no other option than to offload it to io-wq. If it's a 2 second operation, that's fine, you won't see any difference in the application, other than it can now do syscallN async in an efficient way. If syscallN would've completed inline in 1 usec, then offloading to io-wq is suddenly a big performance problem.
Quoted 7 lines
The patch set says "serving as base for batching
multiple mappings in a single operation", and I was wondering, why one wouldn't just also batch with mremap/munmap/ etc. in the future.(BUT I am also skeptical whether holding the mmap lock in write mode longer instead of repeatedly grabbing it, allowing other operations that need it in read mode etc to make progress, is actually preferrable)
That's always a trade off - if the frequency is high, then a certain level of batching makes sense. The good news is that you get to control that, you can just batch more or less.
Outside of mmap locking frequencies, I suspect potentially nicer wins might be around TLB flush reductions for this family of operations.
Signature · 1 line
Jens Axboe
Quoted 21 lines · Jens Axboe
Quoted 20 lines · David Hildenbrand (arm)
Quoted 4 lines · Jens Axboe
The hard part isn't enabling all syscalls at once, that could be trivially done with an
IORING_OP_SYSCALLand the SQE carries arg0..argN. And for any nonblocking/simple syscall, that would Just Work.Right, that's what I had in mind.
Quoted 11 lines
The
challenge is for syscalls that block - the whole point ofio_uringis that you should be able to do nonblock issues with sane retries. The futex series I did some time back is a good example of that - you modify the existing syscall to expose the waitqueue mechanism, which you can then use to wait in an async way, and get a callback when some action needs to be taken.If you just allow blocking, then you're blocking the entire
io_uringissue pipeline. Which was exactly my main complaint on this patchset, see the review reply to patch 2.
Quoted 6 lines
Quoted 4 lines
Makes sense. I was wondering whether that could be optimized internally in the stream of
IORING_OP_SYSCALL.But likely that would make it more tricky to optimize.
Are we talking generically, or mmap/munmap/mremap?
Well, a bit of both :)
munmap() could be a bit challenging as it downgrades the mmap_lock for removal of the page tables. So quite a bit of rework would be required to batch that over multiple operations I suppose.
Quoted 4 lines
You could trivially
makeIORING_OP_SYSCALLavailable and use it for everything, it'd just require a basically all of those to be offloaded to io-wq internally inio_uring. And that's not a great approach. The fast path forio_uringis
Quoted 8 lines
running the opcode inline, which means that by the time the syscall returns, you have also posted the completion. If the operation can't complete inline, then the next best thing is to have it be triggered when it can complete, and then retry and post the completion. Think of reading from a pipe - if the data is there, the read is done inside
io_uring_enter()when the read is attempted, and we're done. If no data is available, the operation is queued. When data becomes available, a retry is triggered, data is read, and a completion is posted.
Thanks for the explanation.
Quoted 20 lines
For an old school kind of syscall "do this thing, and just block the task until it's done" doesn't work that way at all. Running those in
io_uringwould necessitate punting the operation to io-wq, which are helper userspace threads forio_uring. As there's no way of knowing whether syscallN will complete fast inline or block for 2 seconds,io_uringhas no other option than to offload it to io-wq. If it's a 2 second operation, that's fine, you won't see any difference in the application, other than it can now do syscallN async in an efficient way. If syscallN would've completed inline in 1 usec, then offloading to io-wq is suddenly a big performance problem.Quoted 7 lines
The patch set says "serving as base for batching
multiple mappings in a single operation", and I was wondering, why one wouldn't just also batch with mremap/munmap/ etc. in the future.(BUT I am also skeptical whether holding the mmap lock in write mode longer instead of repeatedly grabbing it, allowing other operations that need it in read mode etc to make progress, is actually preferrable)
Quoted 6 lines
That's always a trade off - if the frequency is high, then a certain level of batching makes sense. The good news is that you get to control that, you can just batch more or less.
Outside of mmap locking frequencies, I suspect potentially nicer wins might be around TLB flush reductions for this family of operations.
For mremap() and munmap(), yes, just like for MADV_DONTNEED.
mmap() maybe if we do a MAP_FIXED that implies an munmap() IIRC.
But then we are again in "hairy to reasonably batch" territory I think. These are all extremely involved operations.
Is there any use case for the patch set at hand, in particular, in an un-optimized form?
Signature · 3 lines
Cheers, David