io_uring fixes for 6.1-stable
cover io_uring fixes for 6.1-stable
Hi Greg,
Here are io_uring fixes for 6.1-stable. All 4 patches are in Linus's tree. Patches 2-4 came with the io_uring-7.4 pull, merged as commit 78d9aedf2342 (tag io_uring-7.4-20261119); the others were merged earlier.
They apply on top of v6.1.192 plus queue-6.1 at stable-queue commit 3dd4b2d3c1f7, and also to v6.1.192 alone.
Patches 1-4 needed changes for 6.1; the [axboe: ...] note at the end of each says what was changed and why. The rest are clean backports.
Patch 1/4 (io_uring: enable toggle of iowait usage when waiting on CQEs) is a feature, not a fix. It is in stable because the QEMU developers asked for it: QEMU uses IORING_ENTER_NO_IOWAIT and wants it supported on all current stable kernels. Nothing changes unless the flag is set.
Testing: ran the liburing-2.15-75-ga10946dc test suite in KVM guests (PROVE_LOCKING on) on the baseline v6.1.192 kernel and on the patched kernel, which is v6.1.192 plus these patches and the other io_uring commits already queued. The patched kernel failed the same tests as the baseline, except connect-backlog, which failed only on the baseline (a flaky test). The failures that remain are on both kernels (napi-test timeout, send-zerocopy, timestamp and similar). No new dmesg splat (WARNING, BUG, lockdep, KASAN, Oops). io_uring/ builds without warnings on the release plus queue plus patches.
CVE assessment, by the io_uring maintainer:
Should not get a CVE:
1/4 io_uring: enable toggle of iowait usage when waiting on CQEs
Feature, not a fix. IORING_ENTER_NO_IOWAIT only skips setting in_iowait; nothing changes unless the flag is set.
2/4 io_uring: fail soft links on negative IOU_COMPLETE results
io_issue_sqe() now calls req_set_fail() for negative IOU_COMPLETE results, so IOSQE_IO_LINK dependents of a failed request are cancelled. This only changes link semantics of the caller's own requests.
3/4 io_uring/rw: fail soft links on IOPOLL short or error IO
The IOPOLL completion path now calls req_set_fail() when the result changed, so linked requests are cancelled after a short or failed read/write. Only the caller's own link chain is affected.
4/4 io_uring/rsrc: keep the old file when a file update fails
The old file was dropped from the registered slot before the new fd was checked, so a failed update left the slot empty and posted its tag CQE. Only the caller's own file table, no refcount or lifetime problem.
Cheng Yongkang (1):
io_uring/rsrc: keep the old file when a file update fails
Jens Axboe (1):
io_uring: enable toggle of iowait usage when waiting on CQEs
Yang Xiuwei (2):
io_uring: fail soft links on negative IOU_COMPLETE results io_uring/rw: fail soft links on IOPOLL short or error IO
include/uapi/linux/io_uring.h | 2 ++ io_uring/io_uring.c | 23 ++++++++++++++--------- io_uring/rsrc.c | 26 ++++++++++++++++---------- io_uring/rw.c | 1 + 4 files changed, 33 insertions(+), 19 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/4 io_uring: enable toggle of iowait usage when waiting on CQEs
Commit 07754bfd9aee59063f8549f6e4d455eae636ecc7 upstream.
By default, io_uring marks a waiting task as being in iowait, if it's sleeping waiting on events and there are pending requests. This isn't necessarily always useful, and may be confusing on non-storage setups where iowait isn't expected. It can also cause extra power usage, by preventing the CPU from entering lower sleep states.
This adds a new enter flag, IORING_ENTER_NO_IOWAIT. If set, then io_uring will not account the sleeping task as being in iowait. If the kernel supports this feature, then it will be marked by having the IORING_FEAT_NO_IOWAIT feature flag set.
As the kernel currently does not support separating the iowait accounting and CPU frequency boosting, the IORING_ENTER_NO_IOWAIT controls both of these at the same time. In the future, if those do end up being split, then it'd be possible to control them separately. However, it seems more likely that the kernel will decouple iowait and CPU frequency boosting anyway.
[ 6.1: no struct ext_arg here. Pass the enter flags to io_cqring_wait()
and keep the setting in struct io_wait_queue. Flag bits match upstream. ]
--- include/uapi/linux/io_uring.h | 2 ++ io_uring/io_uring.c | 15 +++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-)
include/uapi/linux/io_uring.h +2 -0
@@ -434,6 +434,7 @@ struct io_cqring_offsets {#define IORING_ENTER_SQ_WAIT (1U << 2)#define IORING_ENTER_EXT_ARG (1U << 3)#define IORING_ENTER_REGISTERED_RING (1U << 4) +#define IORING_ENTER_NO_IOWAIT (1U << 7)/* * Passed in for io_uring_setup(2). Copied back with updated info on success
@@ -467,6 +468,7 @@ struct io_uring_params {#define IORING_FEAT_RSRC_TAGS (1U << 10)#define IORING_FEAT_CQE_SKIP (1U << 11)#define IORING_FEAT_LINKED_FILE (1U << 12) +#define IORING_FEAT_NO_IOWAIT (1U << 17)/* * io_uring_register(2) opcodes and arguments
io_uring/io_uring.c +9 -6
@@ -2354,6 +2354,7 @@ struct io_wait_queue {struct io_ring_ctx *ctx;unsigned cq_tail;unsigned nr_timeouts; +bool iowait; };static inline bool io_has_work (struct io_ring_ctx *ctx)
@@ -2439,7 +2440,7 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,* to be important for low QD IO. */ io_wait = current->in_iowait; -if (current_pending_io ()) +if (iowq->iowait &¤t_pending_io ()) current->in_iowait =1 ; ret =1 ;if (!schedule_hrtimeout (timeout,HRTIMER_MODE_ABS ))
@@ -2452,7 +2453,7 @@ static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,* Wait until events become available, if we don't already have some. The * application must reap them itself, as they reside on the shared cq ring. */ -static int io_cqring_wait (struct io_ring_ctx *ctx,int min_events, +static int io_cqring_wait (struct io_ring_ctx *ctx,int min_events,u32 flags,const sigset_t __user *sig,size_t sigsz,struct __kernel_timespec __user *uts) {
@@ -2501,6 +2502,7 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, iowq.wq.private = current;INIT_LIST_HEAD (&iowq.wq.entry); iowq.ctx = ctx; + iowq.iowait = !(flags &IORING_ENTER_NO_IOWAIT ); iowq.nr_timeouts =atomic_read (&ctx->cq_timeouts); iowq.cq_tail =READ_ONCE (ctx->rings->cq.head) + min_events;
@@ -3427,7 +3429,8 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,if (unlikely (flags & ~(IORING_ENTER_GETEVENTS |IORING_ENTER_SQ_WAKEUP |IORING_ENTER_SQ_WAIT |IORING_ENTER_EXT_ARG | -IORING_ENTER_REGISTERED_RING ))) +IORING_ENTER_REGISTERED_RING | +IORING_ENTER_NO_IOWAIT )))return -EINVAL ;/*
@@ -3530,8 +3533,8 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,if (likely (!ret2)) { min_complete =min (min_complete, ctx->cq_entries); - ret2 =io_cqring_wait (ctx, min_complete, sig, - argsz, ts); + ret2 =io_cqring_wait (ctx, min_complete, flags, + sig, argsz, ts); } }
@@ -3785,7 +3788,7 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p,IORING_FEAT_POLL_32BITS |IORING_FEAT_SQPOLL_NONFIXED |IORING_FEAT_EXT_ARG |IORING_FEAT_NATIVE_WORKERS |IORING_FEAT_RSRC_TAGS |IORING_FEAT_CQE_SKIP | -IORING_FEAT_LINKED_FILE ; +IORING_FEAT_LINKED_FILE |IORING_FEAT_NO_IOWAIT ;if (copy_to_user (params, p,sizeof (*p))) { ret = -EFAULT ;
Signature · 1 line
2.55.0
PATCH 2/4 io_uring: fail soft links on negative IOU_COMPLETE results
In reply to Jens AxboeFrom: Yang Xiuwei <[email protected]>
Commit 9ed19503a7b34e1ffb2b81236843204c473b0ae0 upstream.
Handlers such as fsync return IOU_COMPLETE with a negative cqe.res. The old io_req_complete() path called req_set_fail() for res < 0. After completions moved into the core, that check was dropped, so IOSQE_IO_LINK dependents kept running after the head failed.
--- io_uring/io_uring.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
io_uring/io_uring.c +5 -3
@@ -1756,9 +1756,11 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) }if (ret ==IOU_OK ) { -/* not all handlers take care of the internal restart codes */ -if (unlikely (req->cqe.res <= -ERESTARTSYS )) - req->cqe.res =io_fixup_restart_res (req->cqe.res); +if (unlikely (req->cqe.res <0 )) { +if (req->cqe.res <= -ERESTARTSYS ) + req->cqe.res =io_fixup_restart_res (req->cqe.res); +req_set_fail (req); + }if (issue_flags &IO_URING_F_COMPLETE_DEFER )io_req_complete_defer (req);else
Signature · 1 line
2.55.0
PATCH 3/4 io_uring/rw: fail soft links on IOPOLL short or error IO
In reply to Jens AxboeFrom: Yang Xiuwei <[email protected]>
Commit 4d3db66c5363dcf42c3d43ebaea330ac2d1888bb upstream.
io_complete_rw_iopoll() updates cqe.res when the transfer does not match the requested length, but it does not call req_set_fail(). Soft linked requests then still run after a short or failed IOPOLL read or write.
[axboe: 6.1 reissues from inside the res != cqe.res branch, fail the request after the reissue check there]
--- io_uring/rw.c | 1 + 1 file changed, 1 insertion(+)
io_uring/rw.c +1 -0
@@ -304,6 +304,7 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) req->flags |=REQ_F_REISSUE |REQ_F_PARTIAL_IO ;return ; } +req_set_fail (req); req->cqe.res = res; }
Signature · 1 line
2.55.0
PATCH 4/4 io_uring/rsrc: keep the old file when a file update fails
In reply to Jens AxboeFrom: Cheng Yongkang <[email protected]>
Commit e5a346e4309d720c47fe8331a00068608fad47dd upstream.
__io_sqe_files_update() resets the slot before it calls fget() on the new fd. If fget() fails, the fd is an io_uring file, or the node allocation fails, the update stops and reports that entry as not done, but the old file has already been removed. Later requests that use the slot fail with -EBADF, and if the old file was registered with a tag, its tag CQE is posted as if it had been removed on purpose.
Reset the slot only after the new file has been checked and its node allocated, as io_install_fixed_file() does. Removal with fd == -1 still resets the slot right away.
[axboe: 6.1 has no rsrc nodes for files, the old file is queued for removal with io_queue_rsrc_removal(), which can fail. Get and check the new file first, and only remove the old one once that has succeeded]
--- io_uring/rsrc.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
io_uring/rsrc.c +16 -10
@@ -438,7 +438,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,__s32 __user *fds =u64_to_user_ptr (up->data);struct io_rsrc_data *data = ctx->file_data;struct io_fixed_file *file_slot; -struct file *file;int fd, i, err =0 ;unsigned int done;bool needs_switch = false;
@@ -449,6 +448,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,return -EINVAL ;for (done =0 ; done < nr_args; done++) { +struct file *file =NULL ;u64 tag =0 ; if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
@@ -466,15 +466,6 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, i =array_index_nospec (up->offset + done, ctx->nr_user_files); file_slot =io_fixed_file_slot (&ctx->file_table, i); -if (file_slot->file_ptr) { - file = (struct file *)(file_slot->file_ptr &FFS_MASK ); - err =io_queue_rsrc_removal (data, i, ctx->rsrc_node, file); -if (err) -break ; - file_slot->file_ptr =0 ; -io_file_bitmap_clear (&ctx->file_table, i); - needs_switch = true; - }if (fd !=-1 ) { file =fget (fd);if (!file) {
@@ -494,6 +485,21 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, err = -EBADF ;break ; } + } +if (file_slot->file_ptr) { +struct file *old = (struct file *)(file_slot->file_ptr &FFS_MASK ); + + err =io_queue_rsrc_removal (data, i, ctx->rsrc_node, old); +if (err) { +if (file) +fput (file); +break ; + } + file_slot->file_ptr =0 ; +io_file_bitmap_clear (&ctx->file_table, i); + needs_switch = true; + } +if (file) { *io_get_tag_slot (data, i) = tag;io_fixed_file_set (file_slot, file);io_file_bitmap_set (&ctx->file_table, i);
Signature · 1 line
2.55.0
Quoted 2 lines
Here are
io_uringfixes for 6.1-stable. All 4 patches are in Linus's tree.
Queued the series for 6.1, thanks.
Signature · 2 lines
Thanks, Sasha