io_uring/sqpoll: do the return-to-userspace work in the SQPOLL thread
Subsystems (2)
PATCH 1/1 io_uring/sqpoll: do the return-to-userspace work in the SQPOLL thread
The SQPOLL thread never returns to userspace. io_sq_tw() runs its task_work, but the rest of resume_user_mode_work(), like memory.high reclaim and block cgroup throttling, only runs when the thread exits. So memory.high reclaim only happens inside a charge, with sqd->lock and ctx->uring_lock held, and not at all if the charges can't block.
This used to work. The loop called io_run_task_work(), which does this work since commit b5d3ae202fbf ("io_uring: handle TIF_NOTIFY_RESUME when checking for task_work"). Commit af5d68f8892f ("io_uring/sqpoll:
manage task_work privately") replaced that call with io_sq_tw().
Call resume_user_mode_work() at the top of the loop when TIF_NOTIFY_RESUME is set, with sqd->lock dropped. It runs once per pass, so a pass that charges two or more batches still reclaims inside the charge, as before.
Tested in a VM with memory.high=64M and swap on zram. An SQPOLL ring reads 256M from /dev/zero into untouched memory, 64K at a time. Before, the thread made 337 calls to __mem_cgroup_handle_over_high(), all from inside the charge. After, it makes 704, all from the top of the loop. memory.peak stays at 64M. With 1M reads, 296 of its 385 calls still come from inside the charge. Both runs take about 50% longer, and the extra time is in the handler, which now runs more often. Without memory.high pressure, the test never calls resume_user_mode_work() and the times do not change. Lockdep showed no warnings, also with io_uring_register() racing for sqd->lock.
---
There was similar code pattern in the same function i.e. unlock/cond-resched/lock. I kept this separate for easy backport.
io_uring/sqpoll.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
io_uring/sqpoll.c +12 -0
@@ -348,6 +348,18 @@ static int io_sq_thread(void *data) timeout = jiffies + sqd->sq_thread_idle; } +/* +* The SQPOLL thread never returns to userspace, so do the +* return-to-userspace work, like memory.high reclaim, here. +* It can sleep for a long time, so drop sqd->lock. +*/ +if (test_thread_flag (TIF_NOTIFY_RESUME )) { +mutex_unlock (&sqd->lock); +resume_user_mode_work (NULL ); +mutex_lock (&sqd->lock); + sqd->sq_cpu =raw_smp_processor_id (); + } + cap_entries = !list_is_singular (&sqd->ctx_list);list_for_each_entry (ctx, &sqd->ctx_list, sqd_list) {int ret;
Signature · 1 line
2.53.0-Meta
Quoted 6 lines · Shakeel Butt
The SQPOLL thread never returns to userspace.
io_sq_tw()runs itstask_work, but the rest ofresume_user_mode_work(), like memory.high reclaim and block cgroup throttling, only runs when the thread exits. So memory.high reclaim only happens inside a charge, withsqd->lockandctx->uring_lockheld, and not at all if the charges can't block.
Quoted 6 lines
This used to work. The loop called
io_run_task_work(), which does this work since commitb5d3ae202fbf("io_uring: handleTIF_NOTIFY_RESUMEwhen checking fortask_work"). Commitaf5d68f8892f("io_uring/sqpoll:
managetask_workprivately") replaced that call withio_sq_tw().[...]
Applied, thanks!
[1/1] io_uring/sqpoll: do the return-to-userspace work in the SQPOLL thread
commit: f013b695506491c5202a412a6568313549c81f50
Best regards,
Signature · 1 line
Jens Axboe