fs: gate final fput task_work on PF_NO_TASKWORK
authorJens Axboe <axboe@kernel.dk>
Tue, 4 Jun 2024 16:51:32 +0000 (10:51 -0600)
committerJens Axboe <axboe@kernel.dk>
Mon, 21 Apr 2025 11:13:28 +0000 (05:13 -0600)
commit169591106a957b21b4b04f84de3c8757d64aea72
tree2aa5b0de0209a1af0fecf4cb6eaab45c5d55af92
parent9c2a1c50844265152b7011599a1a9dfe473d1f51
fs: gate final fput task_work on PF_NO_TASKWORK

fput currently gates whether or not a task can run task_work on the
PF_KTHREAD flag, which excludes kernel threads as they don't usually run
task_work as they never exit to userspace. This punts the final fput
done from a kthread to a delayed work item instead of using task_work.

It's perfectly viable to have the final fput done by the kthread itself,
as long as it will actually run the task_work. Add a PF_NO_TASKWORK flag
which is set by default by a kernel thread, and gate the task_work fput
on that instead. This enables a kernel thread to clear this flag
temporarily while putting files, as long as it runs its task_work
manually.

This enables users like io_uring to ensure that when the final fput of a
file is done as part of ring teardown to run the local task_work and
hence know that all files have been properly put, without needing to
resort to workqueue flushing tricks which can deadlock.

No functional changes in this patch.

Acked-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
fs/file_table.c
include/linux/sched.h
kernel/fork.c