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. Signed-off-by: Gabriel Krisman Bertazi --- io_uring/io_uring.c | 15 ++++++++++----- io_uring/opdef.h | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 87a87396e940..158e9823a72a 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -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. diff --git a/io_uring/opdef.h b/io_uring/opdef.h index aa37846880ff..5b81f82c2359 100644 --- a/io_uring/opdef.h +++ b/io_uring/opdef.h @@ -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 */ -- 2.52.0