io_uring fixes for 6.6-stable
cover io_uring fixes for 6.6-stable
Hi Greg,
Here are io_uring fixes for 6.6-stable.
They apply on top of v6.6.161 with the current queue-6.6 applied (stable-queue 6575ff04f2fd). They don't depend on anything in the queue, and apply to v6.6.161 alone as well.
"io_uring/io-wq: save worker type before dropping its reference" is already in queue-6.6, so it's not included here.
Patches 4, 5, 6, 7 and 8 needed changes to apply to 6.6, the [axboe: ...] note in each says what was changed.
Cheng Yongkang (1):
io_uring/timeout: count cancelled timeouts in cq_timeouts
Chris Mason (4):
io_uring/rsrc: charge buffer arrays and nodes to memcg
io_uring: charge tctx nodes and personalities
io_uring: free XArray nodes left by a failed store io_uring/sqpoll: don't wait for the thread to exit
Daan De Meyer (1):
io_uring/net: don't retry a non-blocking connect() that fails with
-EAGAIN
Josef Bacik (1):
io_uring/io-wq: no new workers once the exit has begun
Yichong Chen (1):
io_uring/net: keep recvmsg buffer select length within MAX_RW_COUNT
io_uring/io-wq.c | 6 ++++++ io_uring/io_uring.c | 3 ++- io_uring/net.c | 12 ++++++++++++ io_uring/rsrc.c | 4 ++-- io_uring/sqpoll.c | 28 +++++++++++++++++++++++----- io_uring/sqpoll.h | 3 ++- io_uring/tctx.c | 6 ++++-- io_uring/timeout.c | 3 +++ 8 files changed, 54 insertions(+), 11 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/8 io_uring/net: keep recvmsg buffer select length within MAX_RW_COUNT
From: Yichong Chen <[email protected]>
Commit 18d3f56eb6e6500a65d81cf265f0fe74fd9ecab6 upstream.
io_recvmsg() takes the transfer length from the ring provided buffer when sqe->len is 0, and such a buffer can be up to 4G. min_ret is an int, so 2G or more makes it negative and the (ret < min_ret) block is skipped, and the oversized length reaches the socket layer unchanged: an empty socket then completes with 0 instead of arming poll, and a short read is reported as complete. Clamp the length to MAX_RW_COUNT, as io_recv() does.
--- io_uring/net.c | 1 + 1 file changed, 1 insertion(+)
io_uring/net.c +1 -0
@@ -857,6 +857,7 @@ int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) buf =io_buffer_select (req, &len, issue_flags);if (!buf)return -ENOBUFS ; + len =min_t (size_t, len,MAX_RW_COUNT );if (req->flags &REQ_F_APOLL_MULTISHOT ) { ret =io_recvmsg_prep_multishot (kmsg, sr, &buf, &len);
Signature · 1 line
2.56.0
PATCH 2/8 io_uring/net: don't retry a non-blocking connect() that fails with -EAGAIN
In reply to Jens AxboeFrom: Daan De Meyer <[email protected]>
Commit 71537ed811107c514c0342e98902d489d6d2b7cd upstream.
IORING_OP_CONNECT on an O_NONBLOCK AF_UNIX stream socket retries a connect that fails with -EAGAIN because the listener's backlog is full. Polling the client socket doesn't help: an unconnected stream socket reports EPOLLOUT and EPOLLHUP right away, before the backlog drains. So io_uring retries the connect until the apoll retry limit is hit, then punts the request to io-wq. With a full backlog, io_connect() runs 128 times in the submitting task and once more in a new io-wq worker. That worker stays in the task's thread group, so a later unshare(CLONE_NEWUSER) fails with -EINVAL.
None of this changes the result. __sys_connect_file() adds the socket's own O_NONBLOCK to every attempt, so the attempt from io-wq fails with -EAGAIN as well. That -EAGAIN becomes the CQE result, and connect(2) returns the same -EAGAIN.
Let's complete the request with -EAGAIN right away when the socket has O_NONBLOCK set, like connect(2) does. io_accept() already completes -EAGAIN this way for IORING_ACCEPT_DONTWAIT. -EINPROGRESS still waits for the connection to complete. A blocking socket still ends up in io-wq, where the connect sleeps until the listener accepts.
--- io_uring/net.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
io_uring/net.c +11 -0
@@ -1564,6 +1564,17 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags)if (connect->seen_econnaborted) goto out; connect->seen_econnaborted = true; + }else if (req->file->f_flags &O_NONBLOCK ) { +/* +* __sys_connect_file() adds O_NONBLOCK from the socket +* to every attempt, so a retry from io-wq fails with +* -EAGAIN as well. A poll on the socket doesn't wait +* until a retry can succeed. For example, an AF_UNIX +* client reports EPOLLOUT right away while the +* listener's backlog is full. Complete the request +* with -EAGAIN, like connect(2) does. +*/ + goto out; }if (req_has_async_data (req))return -EAGAIN ;
Signature · 1 line
2.56.0
PATCH 3/8 io_uring/io-wq: no new workers once the exit has begun
In reply to Jens AxboeFrom: Josef Bacik <[email protected]>
Commit 47c68f925062a6013235d06add85a59f7d91e0fb upstream.
io_wq_exit_start() sets IO_WQ_BIT_EXIT, but worker creation task_work queued before that stays queued until io_wq_exit_workers() cancels it. In between the task runs its task_work while canceling requests, and a worker can still get created.
That's a problem for exec. begin_new_exec() calls io_uring_task_cancel() after de_thread(), when it's supposedly the only thread left, and io_wq_exit_workers() only waits for ->worker_done, which a worker completes before do_exit(). So the exiting worker is still in the thread group, using the mm, files and sighand, while exec replaces them. Easy to hit with a signal pending when create_io_thread() fails and the retry gets queued right before execve().
Cancel the queued creation task_work as soon as the exit bit is set. Pending work that isn't otherwise canceled now ends with -ECANCELED when the io-wq is destroyed, rather than depending on whether the task happened to run its task_work first.
[axboe: the barrier is already upstream, see 0e5663856b69, only the
cancel is new]
--- io_uring/io-wq.c | 6 ++++++ 1 file changed, 6 insertions(+)
io_uring/io-wq.c +6 -0
@@ -1318,6 +1318,12 @@ void io_wq_exit_start(struct io_wq *wq)set_bit (IO_WQ_BIT_EXIT , &wq->state);/* Pairs with task_work_add() in io_queue_worker_create(). */ smp_mb__after_atomic (); +/* +* No more workers from here on. The caller is going to run its task +* work, and on exec a thread that is created now would still be +* around, exiting, by the time that the caller must be on its own. +*/ +io_wq_cancel_tw_create (wq); }static void io_wq_cancel_tw_create (struct io_wq *wq)
Signature · 1 line
2.56.0
PATCH 4/8 io_uring/rsrc: charge buffer arrays and nodes to memcg
In reply to Jens AxboeFrom: Chris Mason <[email protected]>
Commit 075d5837bafc317e796548f721b8fbf7add6821d upstream.
The io_mapped_ubuf of a registered buffer has one bio_vec per page, unless the pages coalesce into huge pages. Huge pages only count against RLIMIT_MEMLOCK once per ring, so a buffer that maps the same 2MB folio over and over in a way that defeats coalescing gets a full bvec array per registration while only the one folio counts as locked. 1GB of that registered in 100 slots took several times a 64MB memory.max, with 2.6MB charged.
Allocate the io_mapped_ubuf and the io_rsrc_node with GFP_KERNEL_ACCOUNT. An object reused from the ring caches stays charged to whoever allocated it first, same as the io_kiocb cache.
[axboe: 6.6 predates the rsrc node rework, charge the imu kvmalloc() and the node kzalloc() instead]
--- io_uring/rsrc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
io_uring/rsrc.c +2 -2
@@ -203,7 +203,7 @@ struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)if (entry) { ref_node = container_of(entry, struct io_rsrc_node, cache); }else { - ref_node =kzalloc (sizeof (*ref_node),GFP_KERNEL ); + ref_node =kzalloc (sizeof (*ref_node),GFP_KERNEL_ACCOUNT );if (!ref_node)return NULL ; }
@@ -923,7 +923,7 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, } } - imu =kvmalloc (struct_size (imu, bvec, nr_pages),GFP_KERNEL ); + imu =kvmalloc (struct_size (imu, bvec, nr_pages),GFP_KERNEL_ACCOUNT );if (!imu) goto done;
Signature · 1 line
2.56.0
PATCH 5/8 io_uring: charge tctx nodes and personalities
In reply to Jens AxboeFrom: Chris Mason <[email protected]>
Commit 0b34e7b20a48710b6f53716bf7030920b69fdbdd upstream.
The io_tctx_node allocated the first time a task touches a ring, and the XArray nodes behind tctx->xa and ctx->personalities, are plain GFP_KERNEL. tctx->xa is indexed by the ring pointer, so nearly every entry needs nodes of its own, and a task gets one entry per ring it has used. 642 threads on 4000 rings held 2.3GB with 59MB charged against a 64MB memory.max.
Allocate the node with GFP_KERNEL_ACCOUNT and set XA_FLAGS_ACCOUNT on both XArrays.
[axboe: 6.6 has kmalloc() for the node, inline in__io_uring_add_tctx_node()]
--- io_uring/io_uring.c | 2 +- io_uring/tctx.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
io_uring/io_uring.c +1 -1
@@ -319,7 +319,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)io_alloc_cache_init (&ctx->netmsg_cache,IO_ALLOC_CACHE_MAX ,sizeof (struct io_async_msghdr ));init_completion (&ctx->ref_comp); -xa_init_flags (&ctx->personalities,XA_FLAGS_ALLOC1 ); +xa_init_flags (&ctx->personalities,XA_FLAGS_ALLOC1 |XA_FLAGS_ACCOUNT );mutex_init (&ctx->uring_lock);init_waitqueue_head (&ctx->cq_wait);init_waitqueue_head (&ctx->poll_wq);
io_uring/tctx.c +2 -2
@@ -93,7 +93,7 @@ __cold int io_uring_alloc_task_context(struct task_struct *task,return ret; } -xa_init (&tctx->xa); +xa_init_flags (&tctx->xa,XA_FLAGS_ACCOUNT );init_waitqueue_head (&tctx->wait);atomic_set (&tctx->in_cancel,0 );atomic_set (&tctx->inflight_tracked,0 );
@@ -125,7 +125,7 @@ int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) } }if (!xa_load (&tctx->xa, (unsigned long )ctx)) { - node =kmalloc (sizeof (*node),GFP_KERNEL ); + node =kmalloc (sizeof (*node),GFP_KERNEL_ACCOUNT );if (!node)return -ENOMEM ; node->ctx = ctx;
Signature · 1 line
2.56.0
PATCH 6/8 io_uring: free XArray nodes left by a failed store
In reply to Jens AxboeFrom: Chris Mason <[email protected]>
Commit 51bab09b7ca1a9a6dd9303a3b7ee346f8128c963 upstream.
A failed xa_store() can leave nodes without entries in the tree, and erasing every entry doesn't free those. An XArray needs xa_destroy() before it goes away, even an empty one. That was dropped for tctx->xa in commit ca6484cd308a ("io_uring: no need to call xa_destroy() on empty xarray") over a lockdep report from freeing the tctx in softirq context, and ctx->personalities never had it.
Now that the nodes are charged, a store can fail at memory.max and the leaked nodes stay charged. Call xa_destroy() in io_uring_clean_tctx(), which the task runs itself in process context before the tctx is freed, in the error path of __io_uring_add_tctx_node(), and for ctx->personalities in io_ring_ctx_free().
[axboe: 6.6 installs the tctx before adding the node and has no error path that frees it, drop that hunk. There's no ctx->hpage_acct either]
--- io_uring/io_uring.c | 1 + io_uring/tctx.c | 2 ++ 2 files changed, 3 insertions(+)
io_uring/io_uring.c +1 -0
@@ -3123,6 +3123,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)kfree (ctx->cancel_table.hbs);kfree (ctx->cancel_table_locked.hbs);xa_destroy (&ctx->io_bl_xa); +xa_destroy (&ctx->personalities);kfree (ctx); }
io_uring/tctx.c +2 -0
@@ -197,6 +197,8 @@ __cold void io_uring_clean_tctx(struct io_uring_task *tctx)io_uring_del_tctx_node (index);cond_resched (); } +/* A failed xa_store() leaves nodes with no entry behind. */ +xa_destroy (&tctx->xa);if (wq) {/* * Must be after io_uring_del_tctx_node() (removes nodes under
Signature · 1 line
2.56.0
PATCH 7/8 io_uring/sqpoll: don't wait for the thread to exit
In reply to Jens AxboeFrom: Chris Mason <[email protected]>
Commit 360395e8dbf9d23579f74b84a1af11190813b0f8 upstream.
io_sq_thread_stop() wakes the SQPOLL thread and waits for it to exit. The thread belongs to the thread group that created the ring, so a SIGSTOP or the cgroup freezer holds it in get_signal(), and the wake up doesn't change that. io_ring_exit_work() then sleeps in that wait until someone continues or kills the process. iou_exit has 64 slots, so 64 closed SQPOLL rings of a stopped process keep every other ring from being freed, for everyone.
Don't wait. Set the stop bit, wake the thread, and let it exit when it gets to run again. The sqd can now outlive its last ring, so give it a second refcount with one reference for the rings and one for the thread, and free it with the last one. The thread can't reach a freed ring, rings leave ctx_list before dropping their reference, and the io-wq hash is refcounted.
[axboe: 6.6 has a single exit path in io_sq_thread(), drop the thread's free_refs share there]
--- io_uring/sqpoll.c | 28 +++++++++++++++++++++++----- io_uring/sqpoll.h | 3 ++- 2 files changed, 25 insertions(+), 6 deletions(-)
io_uring/sqpoll.c +23 -5
@@ -52,6 +52,24 @@ void io_sq_thread_park(struct io_sq_data *sqd)wake_up_process (sqd->thread); } +/* +* The sqd has two holders: its rings, as one, and its thread. Neither waits +* for the other to let go. The one that lets go last frees the sqd. +*/ +static void io_sq_data_release (struct io_sq_data *sqd) +{ +if (refcount_dec_and_test (&sqd->free_refs)) +kfree (sqd); +} + +/* +* Ask the thread to exit, and don't wait for it. A thread that is stopped or +* frozen exits only when a user lets it run again, and the exit work of the +* ring would keep its place in the workqueue until then. All rings have left +* ctx_list by now and have no request left, so the thread can reach none of +* them. What it still uses is its own or counted: the sqd by free_refs, the +* hash of its io-wq by hash->refs. +*/ void io_sq_thread_stop (struct io_sq_data *sqd) {WARN_ON_ONCE (sqd->thread == current);
@@ -62,7 +80,6 @@ void io_sq_thread_stop(struct io_sq_data *sqd)if (sqd->thread)wake_up_process (sqd->thread);mutex_unlock (&sqd->lock); -wait_for_completion (&sqd->exited); }void io_put_sq_data (struct io_sq_data *sqd)
@@ -71,7 +88,7 @@ void io_put_sq_data(struct io_sq_data *sqd)WARN_ON_ONCE (atomic_read (&sqd->park_pending));io_sq_thread_stop (sqd); -kfree (sqd); +io_sq_data_release (sqd); } }
@@ -153,10 +170,10 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,atomic_set (&sqd->park_pending,0 );refcount_set (&sqd->refs,1 ); +refcount_set (&sqd->free_refs,2 );INIT_LIST_HEAD (&sqd->ctx_list);mutex_init (&sqd->lock);init_waitqueue_head (&sqd->wait); -init_completion (&sqd->exited);return sqd; }
@@ -335,7 +352,7 @@ static int io_sq_thread(void *data)io_run_task_work ();mutex_unlock (&sqd->lock); -complete (&sqd->exited); +io_sq_data_release (sqd);do_exit (0 ); }
@@ -455,7 +472,8 @@ __cold int io_sq_offload_create(struct io_ring_ctx *ctx,put_task_struct (task_to_put);return 0 ; err_sqpoll: -complete (&ctx->sq_data->exited); +/* no thread was made: let go of its share */ +io_sq_data_release (ctx->sq_data); err:io_sq_thread_finish (ctx);if (task_to_put)
io_uring/sqpoll.h +2 -1
@@ -2,6 +2,8 @@struct io_sq_data {refcount_t refs; +/* one for all the rings, one for the thread: the last put frees */ +refcount_t free_refs;atomic_t park_pending;struct mutex lock;
@@ -17,7 +19,6 @@ struct io_sq_data {pid_t task_tgid;unsigned long state; -struct completion exited; };int io_sq_offload_create (struct io_ring_ctx *ctx,struct io_uring_params *p);
Signature · 1 line
2.56.0
PATCH 8/8 io_uring/timeout: count cancelled timeouts in cq_timeouts
In reply to Jens AxboeFrom: Cheng Yongkang <[email protected]>
Commit 1c8b49851543f41fc8bb18bcd1f15cc93d14b530 upstream.
io_timeout_cancel() posts the cancelled timeout's -ECANCELED CQE without advancing ctx->cq_timeouts. io_timeout_fn() and io_kill_timeout() advance it for every timeout CQE they post, and io_flush_timeouts() computes the number of non-timeout completions as
seq = cached_cq_tail - cq_timeouts
to compare against each off timeout's target_seq. The cancelled timeout's CQE is therefore counted as a regular completion, so every cancellation makes an already armed off timeout fire one completion early.
A ring with a keep-alive timeout of off=4 completes it after two NOPs when one off=100 victim is cancelled through IORING_OP_ASYNC_CANCEL, and before any NOP when two victims are cancelled. The cancel CQE should count, but the cancelled timeouts' -ECANCELED CQEs must not: the expected counts are three and two NOPs respectively.
Advance cq_timeouts when io_timeout_extract() succeeds, under timeout_lock and before the -ECANCELED completion is queued, matching io_timeout_fn() and io_kill_timeout(). The -ENOENT and -EALREADY paths post no new timeout CQE and keep their accounting. Both the ASYNC_CANCEL and the TIMEOUT_REMOVE path go through io_timeout_cancel().
--- io_uring/timeout.c | 3 +++ 1 file changed, 3 insertions(+)
io_uring/timeout.c +3 -0
@@ -306,6 +306,9 @@ int io_timeout_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)spin_lock_irq (&ctx->timeout_lock); req =io_timeout_extract (ctx, cd); +if (!IS_ERR (req)) +atomic_set (&ctx->cq_timeouts, +atomic_read (&ctx->cq_timeouts) +1 );spin_unlock_irq (&ctx->timeout_lock);if (IS_ERR (req))
Signature · 1 line
2.56.0