aio: don't let exit_aio() return before complete() is done
Subsystems (3)
Christian Brauner
maintainerPATCH 1/1 aio: don't let exit_aio() return before complete() is done
exit_aio() kills all contexts in one go and waits for them with a single on-stack struct ctx_rq_wait. The count starts at table->nr, each killed context drops one count in free_ioctx_reqs() and exit_aio() subtracts the empty slots afterwards. Whoever drops the last count either calls complete() or, if it is exit_aio(), skips the wait.
With a full table skipped is zero and atomic_sub_and_test(0, ...) only tells us whether the count already hit zero. free_ioctx_reqs() runs from the percpu_ref RCU callback, so if exit_aio() is off the CPU long enough the last callback can have done its atomic_dec_and_test() but not yet called complete():
free_ioctx_reqs() exit_aio()
atomic_dec_and_test() // 1 -> 0
atomic_sub_and_test(0) // true
kfree(table)
return
complete(&wait.comp)complete() then locks and writes a completion on a stack frame that is gone. So hold an extra count for exit_aio() itself. Then the last count is always dropped by exit_aio() after it waited, or by a callback while exit_aio() waits for it. An unprivileged user gets there with four io_setup() calls and an exit without io_destroy().
--- fs/aio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/aio.c +2 -2
@@ -961,7 +961,7 @@ void exit_aio(struct mm_struct *mm)if (!table)return ; -atomic_set (&wait.count, table->nr); +atomic_set (&wait.count, table->nr +1 );init_completion (&wait.comp); skipped =0 ;
@@ -985,7 +985,7 @@ void exit_aio(struct mm_struct *mm)kill_ioctx (mm, ctx, &wait); } -if (!atomic_sub_and_test (skipped, &wait.count)) { +if (!atomic_sub_and_test (skipped +1 , &wait.count)) {/* Wait until all IO for the context are done. */ wait_for_completion (&wait.comp); }
---
base-commit: b44341e39d856e4cc6ad8ed94ebffd440f39846c
change-id: 20261030-work-aio-exit_aio-bf74801d34b4
Quoted 25 lines · Christian Brauner
exit_aio()kills all contexts in one go and waits for them with a single on-stack structctx_rq_wait. The count starts attable->nr, each killed context drops one count infree_ioctx_reqs()andexit_aio()subtracts the empty slots afterwards. Whoever drops the last count either callscomplete()or, if it isexit_aio(), skips the wait.With a full table skipped is zero and
atomic_sub_and_test(0, ...)only tells us whether the count already hit zero.free_ioctx_reqs()runs from thepercpu_refRCU callback, so ifexit_aio()is off the CPU long enough the last callback can have done itsatomic_dec_and_test()but not yet calledcomplete():free_ioctx_reqs() exit_aio() atomic_dec_and_test() // 1 -> 0 atomic_sub_and_test(0) // true kfree(table) return complete(&wait.comp)
complete()then locks and writes a completion on a stack frame that is gone. So hold an extra count forexit_aio()itself. Then the last count is always dropped byexit_aio()after it waited, or by a callback whileexit_aio()waits for it. An unprivileged user gets there with fourio_setup()calls and an exit withoutio_destroy().
Quoted 4 lines
Reported-by Kyle Zeng <[email protected]>Fixes dc48e56d7616 ("aio: fix serial draining in exit_aio()")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good to me. Feel free to add:
Honza
Quoted 31 lines
--- fs/aio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/aio.c +2
-3@@ -961,7 +961,7 @@ void exit_aio(struct mm_struct *mm)if (!table)return ; -atomic_set (&wait.count, table->nr); +atomic_set (&wait.count, table->nr +1 );init_completion (&wait.comp); skipped =0 ;@@ -985,7 +985,7 @@ void exit_aio(struct mm_struct *mm)kill_ioctx (mm, ctx, &wait); } -if (!atomic_sub_and_test (skipped, &wait.count)) { +if (!atomic_sub_and_test (skipped +1 , &wait.count)) {/* Wait until all IO for the context are done. */ wait_for_completion (&wait.comp); } ---base-commit:
b44341e39d856e4cc6ad8ed94ebffd440f39846c
change-id: 20261030-work-aio-exit_aio-bf74801d34b4
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerOh, hm. I think we got our wires crossed. This is fixed upstream but I took my own patch. I didn't realize you had sent one. Sorry about that.
Quoted 6 lines · Christian Brauner
exit_aio()kills all contexts in one go and waits for them with a single on-stack structctx_rq_wait. The count starts attable->nr, each killed context drops one count infree_ioctx_reqs()andexit_aio()subtracts the empty slots afterwards. Whoever drops the last count either callscomplete()or, if it isexit_aio(), skips the wait.
Quoted 7 lines
With a full table skipped is zero and
atomic_sub_and_test(0, ...)only tells us whether the count already hit zero.free_ioctx_reqs()runs from thepercpu_refRCU callback, so ifexit_aio()is off the CPU long enough the last callback can have done itsatomic_dec_and_test()but not yet calledcomplete():[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/1] aio: don't let exit_aio() return before complete() is done
https://git.kernel.org/vfs/vfs/c/ac4b449b39e6