Skip to content

Commit d4d791d

Browse files
Darksonnhtejun
authored andcommitted
rust: workqueue: add low-level workqueue bindings
Define basic low-level bindings to a kernel workqueue. The API defined here can only be used unsafely. Later commits will provide safe wrappers. Co-developed-by: Gary Guo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: "Andreas Hindborg (Samsung)" <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent a832177 commit d4d791d

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/refcount.h>
1313
#include <linux/wait.h>
1414
#include <linux/sched.h>
15+
#include <linux/workqueue.h>
1516

1617
/* `bindgen` gets confused at certain things. */
1718
const size_t BINDINGS_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub mod str;
4646
pub mod sync;
4747
pub mod task;
4848
pub mod types;
49+
pub mod workqueue;
4950

5051
#[doc(hidden)]
5152
pub use bindings;

rust/kernel/workqueue.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Work queues.
4+
//!
5+
//! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h)
6+
7+
use crate::{bindings, types::Opaque};
8+
9+
/// A kernel work queue.
10+
///
11+
/// Wraps the kernel's C `struct workqueue_struct`.
12+
///
13+
/// It allows work items to be queued to run on thread pools managed by the kernel. Several are
14+
/// always available, for example, `system`, `system_highpri`, `system_long`, etc.
15+
#[repr(transparent)]
16+
pub struct Queue(Opaque<bindings::workqueue_struct>);
17+
18+
// SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
19+
unsafe impl Send for Queue {}
20+
// SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
21+
unsafe impl Sync for Queue {}
22+
23+
impl Queue {
24+
/// Use the provided `struct workqueue_struct` with Rust.
25+
///
26+
/// # Safety
27+
///
28+
/// The caller must ensure that the provided raw pointer is not dangling, that it points at a
29+
/// valid workqueue, and that it remains valid until the end of 'a.
30+
pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
31+
// SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
32+
// caller promises that the pointer is not dangling.
33+
unsafe { &*(ptr as *const Queue) }
34+
}
35+
36+
/// Enqueues a work item.
37+
///
38+
/// This may fail if the work item is already enqueued in a workqueue.
39+
///
40+
/// The work item will be submitted using `WORK_CPU_UNBOUND`.
41+
pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
42+
where
43+
W: RawWorkItem<ID> + Send + 'static,
44+
{
45+
let queue_ptr = self.0.get();
46+
47+
// SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
48+
// `__enqueue` requirements are not relevant since `W` is `Send` and static.
49+
//
50+
// The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
51+
// is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
52+
// closure.
53+
//
54+
// Furthermore, if the C workqueue code accesses the pointer after this call to
55+
// `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
56+
// will have returned true. In this case, `__enqueue` promises that the raw pointer will
57+
// stay valid until we call the function pointer in the `work_struct`, so the access is ok.
58+
unsafe {
59+
w.__enqueue(move |work_ptr| {
60+
bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr)
61+
})
62+
}
63+
}
64+
}
65+
66+
/// A raw work item.
67+
///
68+
/// This is the low-level trait that is designed for being as general as possible.
69+
///
70+
/// The `ID` parameter to this trait exists so that a single type can provide multiple
71+
/// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
72+
/// you will implement this trait once for each field, using a different id for each field. The
73+
/// actual value of the id is not important as long as you use different ids for different fields
74+
/// of the same struct. (Fields of different structs need not use different ids.)
75+
///
76+
/// Note that the id is used only to select the right method to call during compilation. It wont be
77+
/// part of the final executable.
78+
///
79+
/// # Safety
80+
///
81+
/// Implementers must ensure that any pointers passed to a `queue_work_on` closure by `__enqueue`
82+
/// remain valid for the duration specified in the guarantees section of the documentation for
83+
/// `__enqueue`.
84+
pub unsafe trait RawWorkItem<const ID: u64> {
85+
/// The return type of [`Queue::enqueue`].
86+
type EnqueueOutput;
87+
88+
/// Enqueues this work item on a queue using the provided `queue_work_on` method.
89+
///
90+
/// # Guarantees
91+
///
92+
/// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
93+
/// valid `work_struct` for the duration of the call to the closure. If the closure returns
94+
/// true, then it is further guaranteed that the pointer remains valid until someone calls the
95+
/// function pointer stored in the `work_struct`.
96+
///
97+
/// # Safety
98+
///
99+
/// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
100+
///
101+
/// If the work item type is annotated with any lifetimes, then you must not call the function
102+
/// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
103+
///
104+
/// If the work item type is not [`Send`], then the function pointer must be called on the same
105+
/// thread as the call to `__enqueue`.
106+
unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
107+
where
108+
F: FnOnce(*mut bindings::work_struct) -> bool;
109+
}

0 commit comments

Comments
 (0)