xref: /linux-6.15/rust/kernel/workqueue.rs (revision 03394130)
1d4d791d4SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
2d4d791d4SAlice Ryhl 
3d4d791d4SAlice Ryhl //! Work queues.
4d4d791d4SAlice Ryhl //!
5d4d791d4SAlice Ryhl //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h)
6d4d791d4SAlice Ryhl 
7d4d791d4SAlice Ryhl use crate::{bindings, types::Opaque};
8d4d791d4SAlice Ryhl 
9d4d791d4SAlice Ryhl /// A kernel work queue.
10d4d791d4SAlice Ryhl ///
11d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`.
12d4d791d4SAlice Ryhl ///
13d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are
14d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc.
15d4d791d4SAlice Ryhl #[repr(transparent)]
16d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>);
17d4d791d4SAlice Ryhl 
18d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
19d4d791d4SAlice Ryhl unsafe impl Send for Queue {}
20d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
21d4d791d4SAlice Ryhl unsafe impl Sync for Queue {}
22d4d791d4SAlice Ryhl 
23d4d791d4SAlice Ryhl impl Queue {
24d4d791d4SAlice Ryhl     /// Use the provided `struct workqueue_struct` with Rust.
25d4d791d4SAlice Ryhl     ///
26d4d791d4SAlice Ryhl     /// # Safety
27d4d791d4SAlice Ryhl     ///
28d4d791d4SAlice Ryhl     /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
29d4d791d4SAlice Ryhl     /// valid workqueue, and that it remains valid until the end of 'a.
30d4d791d4SAlice Ryhl     pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
31d4d791d4SAlice Ryhl         // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
32d4d791d4SAlice Ryhl         // caller promises that the pointer is not dangling.
33d4d791d4SAlice Ryhl         unsafe { &*(ptr as *const Queue) }
34d4d791d4SAlice Ryhl     }
35d4d791d4SAlice Ryhl 
36d4d791d4SAlice Ryhl     /// Enqueues a work item.
37d4d791d4SAlice Ryhl     ///
38d4d791d4SAlice Ryhl     /// This may fail if the work item is already enqueued in a workqueue.
39d4d791d4SAlice Ryhl     ///
40d4d791d4SAlice Ryhl     /// The work item will be submitted using `WORK_CPU_UNBOUND`.
41d4d791d4SAlice Ryhl     pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
42d4d791d4SAlice Ryhl     where
43d4d791d4SAlice Ryhl         W: RawWorkItem<ID> + Send + 'static,
44d4d791d4SAlice Ryhl     {
45d4d791d4SAlice Ryhl         let queue_ptr = self.0.get();
46d4d791d4SAlice Ryhl 
47d4d791d4SAlice Ryhl         // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
48d4d791d4SAlice Ryhl         // `__enqueue` requirements are not relevant since `W` is `Send` and static.
49d4d791d4SAlice Ryhl         //
50d4d791d4SAlice Ryhl         // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
51d4d791d4SAlice Ryhl         // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
52d4d791d4SAlice Ryhl         // closure.
53d4d791d4SAlice Ryhl         //
54d4d791d4SAlice Ryhl         // Furthermore, if the C workqueue code accesses the pointer after this call to
55d4d791d4SAlice Ryhl         // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
56d4d791d4SAlice Ryhl         // will have returned true. In this case, `__enqueue` promises that the raw pointer will
57d4d791d4SAlice Ryhl         // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
58d4d791d4SAlice Ryhl         unsafe {
59d4d791d4SAlice Ryhl             w.__enqueue(move |work_ptr| {
60d4d791d4SAlice Ryhl                 bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr)
61d4d791d4SAlice Ryhl             })
62d4d791d4SAlice Ryhl         }
63d4d791d4SAlice Ryhl     }
64d4d791d4SAlice Ryhl }
65d4d791d4SAlice Ryhl 
66d4d791d4SAlice Ryhl /// A raw work item.
67d4d791d4SAlice Ryhl ///
68d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible.
69d4d791d4SAlice Ryhl ///
70d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple
71d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
72d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The
73d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields
74d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.)
75d4d791d4SAlice Ryhl ///
76d4d791d4SAlice Ryhl /// Note that the id is used only to select the right method to call during compilation. It wont be
77d4d791d4SAlice Ryhl /// part of the final executable.
78d4d791d4SAlice Ryhl ///
79d4d791d4SAlice Ryhl /// # Safety
80d4d791d4SAlice Ryhl ///
81d4d791d4SAlice Ryhl /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by `__enqueue`
82d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for
83d4d791d4SAlice Ryhl /// `__enqueue`.
84d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> {
85d4d791d4SAlice Ryhl     /// The return type of [`Queue::enqueue`].
86d4d791d4SAlice Ryhl     type EnqueueOutput;
87d4d791d4SAlice Ryhl 
88d4d791d4SAlice Ryhl     /// Enqueues this work item on a queue using the provided `queue_work_on` method.
89d4d791d4SAlice Ryhl     ///
90d4d791d4SAlice Ryhl     /// # Guarantees
91d4d791d4SAlice Ryhl     ///
92d4d791d4SAlice Ryhl     /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
93d4d791d4SAlice Ryhl     /// valid `work_struct` for the duration of the call to the closure. If the closure returns
94d4d791d4SAlice Ryhl     /// true, then it is further guaranteed that the pointer remains valid until someone calls the
95d4d791d4SAlice Ryhl     /// function pointer stored in the `work_struct`.
96d4d791d4SAlice Ryhl     ///
97d4d791d4SAlice Ryhl     /// # Safety
98d4d791d4SAlice Ryhl     ///
99d4d791d4SAlice Ryhl     /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
100d4d791d4SAlice Ryhl     ///
101d4d791d4SAlice Ryhl     /// If the work item type is annotated with any lifetimes, then you must not call the function
102d4d791d4SAlice Ryhl     /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
103d4d791d4SAlice Ryhl     ///
104d4d791d4SAlice Ryhl     /// If the work item type is not [`Send`], then the function pointer must be called on the same
105d4d791d4SAlice Ryhl     /// thread as the call to `__enqueue`.
106d4d791d4SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
107d4d791d4SAlice Ryhl     where
108d4d791d4SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool;
109d4d791d4SAlice Ryhl }
110*03394130SWedson Almeida Filho 
111*03394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`).
112*03394130SWedson Almeida Filho ///
113*03394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
114*03394130SWedson Almeida Filho /// users which expect relatively short queue flush time.
115*03394130SWedson Almeida Filho ///
116*03394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long.
117*03394130SWedson Almeida Filho pub fn system() -> &'static Queue {
118*03394130SWedson Almeida Filho     // SAFETY: `system_wq` is a C global, always available.
119*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_wq) }
120*03394130SWedson Almeida Filho }
121*03394130SWedson Almeida Filho 
122*03394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`).
123*03394130SWedson Almeida Filho ///
124*03394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher
125*03394130SWedson Almeida Filho /// scheduling priority.
126*03394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue {
127*03394130SWedson Almeida Filho     // SAFETY: `system_highpri_wq` is a C global, always available.
128*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_highpri_wq) }
129*03394130SWedson Almeida Filho }
130*03394130SWedson Almeida Filho 
131*03394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`).
132*03394130SWedson Almeida Filho ///
133*03394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue
134*03394130SWedson Almeida Filho /// flushing might take relatively long.
135*03394130SWedson Almeida Filho pub fn system_long() -> &'static Queue {
136*03394130SWedson Almeida Filho     // SAFETY: `system_long_wq` is a C global, always available.
137*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_long_wq) }
138*03394130SWedson Almeida Filho }
139*03394130SWedson Almeida Filho 
140*03394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`).
141*03394130SWedson Almeida Filho ///
142*03394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
143*03394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are
144*03394130SWedson Almeida Filho /// available.
145*03394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue {
146*03394130SWedson Almeida Filho     // SAFETY: `system_unbound_wq` is a C global, always available.
147*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_unbound_wq) }
148*03394130SWedson Almeida Filho }
149*03394130SWedson Almeida Filho 
150*03394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`).
151*03394130SWedson Almeida Filho ///
152*03394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable.
153*03394130SWedson Almeida Filho ///
154*03394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
155*03394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
156*03394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue {
157*03394130SWedson Almeida Filho     // SAFETY: `system_freezable_wq` is a C global, always available.
158*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_wq) }
159*03394130SWedson Almeida Filho }
160*03394130SWedson Almeida Filho 
161*03394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`).
162*03394130SWedson Almeida Filho ///
163*03394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the
164*03394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
165*03394130SWedson Almeida Filho /// returned by [`system`].
166*03394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue {
167*03394130SWedson Almeida Filho     // SAFETY: `system_power_efficient_wq` is a C global, always available.
168*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
169*03394130SWedson Almeida Filho }
170*03394130SWedson Almeida Filho 
171*03394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`).
172*03394130SWedson Almeida Filho ///
173*03394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable.
174*03394130SWedson Almeida Filho ///
175*03394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
176*03394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
177*03394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue {
178*03394130SWedson Almeida Filho     // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available.
179*03394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
180*03394130SWedson Almeida Filho }
181