Re: [PATCH] rust: add task_work abstraction

From: Greg KH

Date: Tue Apr 21 2026 - 02:47:30 EST


On Tue, Apr 21, 2026 at 06:38:36AM +0000, Ashutosh Desai wrote:
> The kernel's task_work API (include/linux/task_work.h) allows any
> kernel thread to queue a callback that runs on a specific task the
> next time it returns to user space (or on exit). The C interface
> requires manual lifetime management of the callback_head allocation
> and careful ordering of init_task_work() and task_work_add().
>
> Add a safe Rust abstraction consisting of:
>
> - TaskWork<T>: an owned, heap-allocated work item. Allocating
> with TaskWork::new() ties a user-supplied value T (bound by the
> new TaskWorkItem trait) to a callback_head. Scheduling with
> TaskWork::add() initializes the callback, transfers ownership to
> the kernel, and returns Err((ESRCH, data)) if the target task is
> already exiting, so callers can always recover the value.
>
> - TaskWorkItem: a trait for types that can be scheduled as a task
> work item. Implementors provide a run() method that receives the
> inner value by move when the callback fires.
>
> - NotifyMode: a type-safe enum wrapping task_work_notify_mode,
> covering TWA_NONE, TWA_RESUME, TWA_SIGNAL, TWA_SIGNAL_NO_IPI,
> and TWA_NMI_CURRENT.
>
> The repr(C) layout of the internal TaskWorkInner<T> places
> callback_head at offset 0, which lets the C callback pointer be
> cast back to the full allocation. Option<T> in that struct permits
> moving the data out before dropping the allocation without a Drop
> impl on TaskWork<T>.
>
> Signed-off-by: Ashutosh Desai <ashutoshdesai993@xxxxxxxxx>
> ---
> rust/kernel/lib.rs | 1 +
> rust/kernel/task_work.rs | 158 +++++++++++++++++++++++++++++++++++++++
> 2 files changed, 159 insertions(+)
> create mode 100644 rust/kernel/task_work.rs

Do you have a user for this binding so that we can see how it is being
used to determine if it is correct?

thanks,

greg k-h