Skip to contentSkip to conversation

rust: add task_work abstraction

PATCH In review Ashutosh Desai 2 participants 4 messages Apr 21 – Apr 22 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: add task_work abstraction

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>.

---
 rust/kernel/lib.rs       |   1 +
 rust/kernel/task_work.rs | 158 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 159 insertions(+)
 create mode 100644 rust/kernel/task_work.rs
rust/kernel/lib.rs +1 -0
index d93292d47420..22878fd73528 100644
@@ -154,6 +154,7 @@
 pub mod str;
 pub mod sync;
 pub mod task;
+pub mod task_work;
 pub mod time;
 pub mod tracepoint;
 pub mod transmute;
rust/kernel/task_work.rs new file +158 -0

158 lines not on this page · raw message

base-commit: 7f87a5ea75f011d2c9bc8ac0167e5e2d1adb1594
prerequisite-patch-id: 161541863a5d4d8c2a41c6f5cfdf712463bf50c9
prerequisite-patch-id: 3b286037e1aeb5a942ff450ced7ec52048bfea7a
prerequisite-patch-id: 14ac8c330c1207cbba2096c4771e000ae82bb765

Signature · 1 line
2.34.1
#1 lore raw
Quoted 29 lines · Ashutosh Desai

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>.

Quoted 6 lines
---
 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

#2 lore raw
Quoted 2 lines · Greg KH

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

The motivating user is drivers/android/binder/deferred_close.rs, which calls bindings::init_task_work() and bindings::task_work_add() directly.

Converting it is non-trivial - the current code schedules the task work before closing the fd on purpose, so it can bail out if scheduling fails without having already removed the fd. Our API requires the data to be ready before calling add(), which would reverse that order and change the error semantics of close_fd().

Happy to send a v2 with a conversion patch if that is the preferred approach, or to discuss the design first if needed.

Thanks,
Ashutosh

#3 lore raw
Quoted 7 lines · Ashutosh Desai
Quoted 2 lines · Greg KH

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

The motivating user is drivers/android/binder/deferred_close.rs, which calls bindings::init_task_work() and bindings::task_work_add() directly.

Quoted 8 lines

Converting it is non-trivial - the current code schedules the task work before closing the fd on purpose, so it can bail out if scheduling fails without having already removed the fd. Our API requires the data to be ready before calling add(), which would reverse that order and change the error semantics of close_fd().

Happy to send a v2 with a conversion patch if that is the preferred approach, or to discuss the design first if needed.

Having a user of it is key to see if the new api actually does work well or not. On it's own, it can be hard to determine.

thanks,

greg k-h