xref: /linux-6.15/rust/kernel/workqueue.rs (revision c34aa00d)
1d4d791d4SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
2d4d791d4SAlice Ryhl 
3d4d791d4SAlice Ryhl //! Work queues.
4d4d791d4SAlice Ryhl //!
57324b889SAlice Ryhl //! This file has two components: The raw work item API, and the safe work item API.
67324b889SAlice Ryhl //!
77324b889SAlice Ryhl //! One pattern that is used in both APIs is the `ID` const generic, which exists to allow a single
87324b889SAlice Ryhl //! type to define multiple `work_struct` fields. This is done by choosing an id for each field,
97324b889SAlice Ryhl //! and using that id to specify which field you wish to use. (The actual value doesn't matter, as
107324b889SAlice Ryhl //! long as you use different values for different fields of the same struct.) Since these IDs are
117324b889SAlice Ryhl //! generic, they are used only at compile-time, so they shouldn't exist in the final binary.
127324b889SAlice Ryhl //!
137324b889SAlice Ryhl //! # The raw API
147324b889SAlice Ryhl //!
154c799d1dSValentin Obst //! The raw API consists of the [`RawWorkItem`] trait, where the work item needs to provide an
167324b889SAlice Ryhl //! arbitrary function that knows how to enqueue the work item. It should usually not be used
177324b889SAlice Ryhl //! directly, but if you want to, you can use it without using the pieces from the safe API.
187324b889SAlice Ryhl //!
197324b889SAlice Ryhl //! # The safe API
207324b889SAlice Ryhl //!
214c799d1dSValentin Obst //! The safe API is used via the [`Work`] struct and [`WorkItem`] traits. Furthermore, it also
224c799d1dSValentin Obst //! includes a trait called [`WorkItemPointer`], which is usually not used directly by the user.
237324b889SAlice Ryhl //!
244c799d1dSValentin Obst //!  * The [`Work`] struct is the Rust wrapper for the C `work_struct` type.
254c799d1dSValentin Obst //!  * The [`WorkItem`] trait is implemented for structs that can be enqueued to a workqueue.
264c799d1dSValentin Obst //!  * The [`WorkItemPointer`] trait is implemented for the pointer type that points at a something
274c799d1dSValentin Obst //!    that implements [`WorkItem`].
287324b889SAlice Ryhl //!
2915b286d1SAlice Ryhl //! ## Example
3015b286d1SAlice Ryhl //!
3115b286d1SAlice Ryhl //! This example defines a struct that holds an integer and can be scheduled on the workqueue. When
3215b286d1SAlice Ryhl //! the struct is executed, it will print the integer. Since there is only one `work_struct` field,
3315b286d1SAlice Ryhl //! we do not need to specify ids for the fields.
3415b286d1SAlice Ryhl //!
3515b286d1SAlice Ryhl //! ```
3615b286d1SAlice Ryhl //! use kernel::prelude::*;
3715b286d1SAlice Ryhl //! use kernel::sync::Arc;
38e283ee23SAlice Ryhl //! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem};
3915b286d1SAlice Ryhl //!
4015b286d1SAlice Ryhl //! #[pin_data]
4115b286d1SAlice Ryhl //! struct MyStruct {
4215b286d1SAlice Ryhl //!     value: i32,
4315b286d1SAlice Ryhl //!     #[pin]
4415b286d1SAlice Ryhl //!     work: Work<MyStruct>,
4515b286d1SAlice Ryhl //! }
4615b286d1SAlice Ryhl //!
4715b286d1SAlice Ryhl //! impl_has_work! {
4815b286d1SAlice Ryhl //!     impl HasWork<Self> for MyStruct { self.work }
4915b286d1SAlice Ryhl //! }
5015b286d1SAlice Ryhl //!
5115b286d1SAlice Ryhl //! impl MyStruct {
5215b286d1SAlice Ryhl //!     fn new(value: i32) -> Result<Arc<Self>> {
5315b286d1SAlice Ryhl //!         Arc::pin_init(pin_init!(MyStruct {
5415b286d1SAlice Ryhl //!             value,
5515b286d1SAlice Ryhl //!             work <- new_work!("MyStruct::work"),
56*c34aa00dSWedson Almeida Filho //!         }), GFP_KERNEL)
5715b286d1SAlice Ryhl //!     }
5815b286d1SAlice Ryhl //! }
5915b286d1SAlice Ryhl //!
6015b286d1SAlice Ryhl //! impl WorkItem for MyStruct {
6115b286d1SAlice Ryhl //!     type Pointer = Arc<MyStruct>;
6215b286d1SAlice Ryhl //!
6315b286d1SAlice Ryhl //!     fn run(this: Arc<MyStruct>) {
6415b286d1SAlice Ryhl //!         pr_info!("The value is: {}", this.value);
6515b286d1SAlice Ryhl //!     }
6615b286d1SAlice Ryhl //! }
6715b286d1SAlice Ryhl //!
6815b286d1SAlice Ryhl //! /// This method will enqueue the struct for execution on the system workqueue, where its value
6915b286d1SAlice Ryhl //! /// will be printed.
7015b286d1SAlice Ryhl //! fn print_later(val: Arc<MyStruct>) {
7115b286d1SAlice Ryhl //!     let _ = workqueue::system().enqueue(val);
7215b286d1SAlice Ryhl //! }
7315b286d1SAlice Ryhl //! ```
7415b286d1SAlice Ryhl //!
7515b286d1SAlice Ryhl //! The following example shows how multiple `work_struct` fields can be used:
7615b286d1SAlice Ryhl //!
7715b286d1SAlice Ryhl //! ```
7815b286d1SAlice Ryhl //! use kernel::prelude::*;
7915b286d1SAlice Ryhl //! use kernel::sync::Arc;
80e283ee23SAlice Ryhl //! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem};
8115b286d1SAlice Ryhl //!
8215b286d1SAlice Ryhl //! #[pin_data]
8315b286d1SAlice Ryhl //! struct MyStruct {
8415b286d1SAlice Ryhl //!     value_1: i32,
8515b286d1SAlice Ryhl //!     value_2: i32,
8615b286d1SAlice Ryhl //!     #[pin]
8715b286d1SAlice Ryhl //!     work_1: Work<MyStruct, 1>,
8815b286d1SAlice Ryhl //!     #[pin]
8915b286d1SAlice Ryhl //!     work_2: Work<MyStruct, 2>,
9015b286d1SAlice Ryhl //! }
9115b286d1SAlice Ryhl //!
9215b286d1SAlice Ryhl //! impl_has_work! {
9315b286d1SAlice Ryhl //!     impl HasWork<Self, 1> for MyStruct { self.work_1 }
9415b286d1SAlice Ryhl //!     impl HasWork<Self, 2> for MyStruct { self.work_2 }
9515b286d1SAlice Ryhl //! }
9615b286d1SAlice Ryhl //!
9715b286d1SAlice Ryhl //! impl MyStruct {
9815b286d1SAlice Ryhl //!     fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> {
9915b286d1SAlice Ryhl //!         Arc::pin_init(pin_init!(MyStruct {
10015b286d1SAlice Ryhl //!             value_1,
10115b286d1SAlice Ryhl //!             value_2,
10215b286d1SAlice Ryhl //!             work_1 <- new_work!("MyStruct::work_1"),
10315b286d1SAlice Ryhl //!             work_2 <- new_work!("MyStruct::work_2"),
104*c34aa00dSWedson Almeida Filho //!         }), GFP_KERNEL)
10515b286d1SAlice Ryhl //!     }
10615b286d1SAlice Ryhl //! }
10715b286d1SAlice Ryhl //!
10815b286d1SAlice Ryhl //! impl WorkItem<1> for MyStruct {
10915b286d1SAlice Ryhl //!     type Pointer = Arc<MyStruct>;
11015b286d1SAlice Ryhl //!
11115b286d1SAlice Ryhl //!     fn run(this: Arc<MyStruct>) {
11215b286d1SAlice Ryhl //!         pr_info!("The value is: {}", this.value_1);
11315b286d1SAlice Ryhl //!     }
11415b286d1SAlice Ryhl //! }
11515b286d1SAlice Ryhl //!
11615b286d1SAlice Ryhl //! impl WorkItem<2> for MyStruct {
11715b286d1SAlice Ryhl //!     type Pointer = Arc<MyStruct>;
11815b286d1SAlice Ryhl //!
11915b286d1SAlice Ryhl //!     fn run(this: Arc<MyStruct>) {
12015b286d1SAlice Ryhl //!         pr_info!("The second value is: {}", this.value_2);
12115b286d1SAlice Ryhl //!     }
12215b286d1SAlice Ryhl //! }
12315b286d1SAlice Ryhl //!
12415b286d1SAlice Ryhl //! fn print_1_later(val: Arc<MyStruct>) {
12515b286d1SAlice Ryhl //!     let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val);
12615b286d1SAlice Ryhl //! }
12715b286d1SAlice Ryhl //!
12815b286d1SAlice Ryhl //! fn print_2_later(val: Arc<MyStruct>) {
12915b286d1SAlice Ryhl //!     let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val);
13015b286d1SAlice Ryhl //! }
13115b286d1SAlice Ryhl //! ```
13215b286d1SAlice Ryhl //!
133bc2e7d5cSMiguel Ojeda //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h)
134d4d791d4SAlice Ryhl 
135*c34aa00dSWedson Almeida Filho use crate::alloc::Flags;
13647f0dbe8SAlice Ryhl use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
137115c95e9SAlice Ryhl use alloc::alloc::AllocError;
13847f0dbe8SAlice Ryhl use alloc::boxed::Box;
1397324b889SAlice Ryhl use core::marker::PhantomData;
14047f0dbe8SAlice Ryhl use core::pin::Pin;
1417324b889SAlice Ryhl 
1427324b889SAlice Ryhl /// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
1437324b889SAlice Ryhl #[macro_export]
1447324b889SAlice Ryhl macro_rules! new_work {
1457324b889SAlice Ryhl     ($($name:literal)?) => {
1467324b889SAlice Ryhl         $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
1477324b889SAlice Ryhl     };
1487324b889SAlice Ryhl }
149e283ee23SAlice Ryhl pub use new_work;
150d4d791d4SAlice Ryhl 
151d4d791d4SAlice Ryhl /// A kernel work queue.
152d4d791d4SAlice Ryhl ///
153d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`.
154d4d791d4SAlice Ryhl ///
155d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are
156d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc.
157d4d791d4SAlice Ryhl #[repr(transparent)]
158d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>);
159d4d791d4SAlice Ryhl 
160d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
161d4d791d4SAlice Ryhl unsafe impl Send for Queue {}
162d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
163d4d791d4SAlice Ryhl unsafe impl Sync for Queue {}
164d4d791d4SAlice Ryhl 
165d4d791d4SAlice Ryhl impl Queue {
166d4d791d4SAlice Ryhl     /// Use the provided `struct workqueue_struct` with Rust.
167d4d791d4SAlice Ryhl     ///
168d4d791d4SAlice Ryhl     /// # Safety
169d4d791d4SAlice Ryhl     ///
170d4d791d4SAlice Ryhl     /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
171af8b18d7SValentin Obst     /// valid workqueue, and that it remains valid until the end of `'a`.
172d4d791d4SAlice Ryhl     pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
173d4d791d4SAlice Ryhl         // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
174d4d791d4SAlice Ryhl         // caller promises that the pointer is not dangling.
175d4d791d4SAlice Ryhl         unsafe { &*(ptr as *const Queue) }
176d4d791d4SAlice Ryhl     }
177d4d791d4SAlice Ryhl 
178d4d791d4SAlice Ryhl     /// Enqueues a work item.
179d4d791d4SAlice Ryhl     ///
180d4d791d4SAlice Ryhl     /// This may fail if the work item is already enqueued in a workqueue.
181d4d791d4SAlice Ryhl     ///
182d4d791d4SAlice Ryhl     /// The work item will be submitted using `WORK_CPU_UNBOUND`.
183d4d791d4SAlice Ryhl     pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
184d4d791d4SAlice Ryhl     where
185d4d791d4SAlice Ryhl         W: RawWorkItem<ID> + Send + 'static,
186d4d791d4SAlice Ryhl     {
187d4d791d4SAlice Ryhl         let queue_ptr = self.0.get();
188d4d791d4SAlice Ryhl 
189d4d791d4SAlice Ryhl         // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
190d4d791d4SAlice Ryhl         // `__enqueue` requirements are not relevant since `W` is `Send` and static.
191d4d791d4SAlice Ryhl         //
192d4d791d4SAlice Ryhl         // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
193d4d791d4SAlice Ryhl         // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
194d4d791d4SAlice Ryhl         // closure.
195d4d791d4SAlice Ryhl         //
196d4d791d4SAlice Ryhl         // Furthermore, if the C workqueue code accesses the pointer after this call to
197d4d791d4SAlice Ryhl         // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
198d4d791d4SAlice Ryhl         // will have returned true. In this case, `__enqueue` promises that the raw pointer will
199d4d791d4SAlice Ryhl         // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
200d4d791d4SAlice Ryhl         unsafe {
201d4d791d4SAlice Ryhl             w.__enqueue(move |work_ptr| {
2023e0bc285SMiguel Ojeda                 bindings::queue_work_on(
2033e0bc285SMiguel Ojeda                     bindings::wq_misc_consts_WORK_CPU_UNBOUND as _,
2043e0bc285SMiguel Ojeda                     queue_ptr,
2053e0bc285SMiguel Ojeda                     work_ptr,
2063e0bc285SMiguel Ojeda                 )
207d4d791d4SAlice Ryhl             })
208d4d791d4SAlice Ryhl         }
209d4d791d4SAlice Ryhl     }
210115c95e9SAlice Ryhl 
211115c95e9SAlice Ryhl     /// Tries to spawn the given function or closure as a work item.
212115c95e9SAlice Ryhl     ///
213115c95e9SAlice Ryhl     /// This method can fail because it allocates memory to store the work item.
214*c34aa00dSWedson Almeida Filho     pub fn try_spawn<T: 'static + Send + FnOnce()>(
215*c34aa00dSWedson Almeida Filho         &self,
216*c34aa00dSWedson Almeida Filho         flags: Flags,
217*c34aa00dSWedson Almeida Filho         func: T,
218*c34aa00dSWedson Almeida Filho     ) -> Result<(), AllocError> {
219115c95e9SAlice Ryhl         let init = pin_init!(ClosureWork {
220115c95e9SAlice Ryhl             work <- new_work!("Queue::try_spawn"),
221115c95e9SAlice Ryhl             func: Some(func),
222115c95e9SAlice Ryhl         });
223115c95e9SAlice Ryhl 
224*c34aa00dSWedson Almeida Filho         self.enqueue(Box::pin_init(init, flags).map_err(|_| AllocError)?);
225115c95e9SAlice Ryhl         Ok(())
226115c95e9SAlice Ryhl     }
227115c95e9SAlice Ryhl }
228115c95e9SAlice Ryhl 
2294c799d1dSValentin Obst /// A helper type used in [`try_spawn`].
2304c799d1dSValentin Obst ///
2314c799d1dSValentin Obst /// [`try_spawn`]: Queue::try_spawn
232115c95e9SAlice Ryhl #[pin_data]
233115c95e9SAlice Ryhl struct ClosureWork<T> {
234115c95e9SAlice Ryhl     #[pin]
235115c95e9SAlice Ryhl     work: Work<ClosureWork<T>>,
236115c95e9SAlice Ryhl     func: Option<T>,
237115c95e9SAlice Ryhl }
238115c95e9SAlice Ryhl 
239115c95e9SAlice Ryhl impl<T> ClosureWork<T> {
240115c95e9SAlice Ryhl     fn project(self: Pin<&mut Self>) -> &mut Option<T> {
241115c95e9SAlice Ryhl         // SAFETY: The `func` field is not structurally pinned.
242115c95e9SAlice Ryhl         unsafe { &mut self.get_unchecked_mut().func }
243115c95e9SAlice Ryhl     }
244115c95e9SAlice Ryhl }
245115c95e9SAlice Ryhl 
246115c95e9SAlice Ryhl impl<T: FnOnce()> WorkItem for ClosureWork<T> {
247115c95e9SAlice Ryhl     type Pointer = Pin<Box<Self>>;
248115c95e9SAlice Ryhl 
249115c95e9SAlice Ryhl     fn run(mut this: Pin<Box<Self>>) {
250115c95e9SAlice Ryhl         if let Some(func) = this.as_mut().project().take() {
251115c95e9SAlice Ryhl             (func)()
252115c95e9SAlice Ryhl         }
253115c95e9SAlice Ryhl     }
254d4d791d4SAlice Ryhl }
255d4d791d4SAlice Ryhl 
256d4d791d4SAlice Ryhl /// A raw work item.
257d4d791d4SAlice Ryhl ///
258d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible.
259d4d791d4SAlice Ryhl ///
260d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple
261d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
262d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The
263d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields
264d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.)
265d4d791d4SAlice Ryhl ///
266b6cda913SValentin Obst /// Note that the id is used only to select the right method to call during compilation. It won't be
267d4d791d4SAlice Ryhl /// part of the final executable.
268d4d791d4SAlice Ryhl ///
269d4d791d4SAlice Ryhl /// # Safety
270d4d791d4SAlice Ryhl ///
2714c799d1dSValentin Obst /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`]
272d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for
2734c799d1dSValentin Obst /// [`__enqueue`].
2744c799d1dSValentin Obst ///
2754c799d1dSValentin Obst /// [`__enqueue`]: RawWorkItem::__enqueue
276d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> {
277d4d791d4SAlice Ryhl     /// The return type of [`Queue::enqueue`].
278d4d791d4SAlice Ryhl     type EnqueueOutput;
279d4d791d4SAlice Ryhl 
280d4d791d4SAlice Ryhl     /// Enqueues this work item on a queue using the provided `queue_work_on` method.
281d4d791d4SAlice Ryhl     ///
282d4d791d4SAlice Ryhl     /// # Guarantees
283d4d791d4SAlice Ryhl     ///
284d4d791d4SAlice Ryhl     /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
285d4d791d4SAlice Ryhl     /// valid `work_struct` for the duration of the call to the closure. If the closure returns
286d4d791d4SAlice Ryhl     /// true, then it is further guaranteed that the pointer remains valid until someone calls the
287d4d791d4SAlice Ryhl     /// function pointer stored in the `work_struct`.
288d4d791d4SAlice Ryhl     ///
289d4d791d4SAlice Ryhl     /// # Safety
290d4d791d4SAlice Ryhl     ///
291d4d791d4SAlice Ryhl     /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
292d4d791d4SAlice Ryhl     ///
293d4d791d4SAlice Ryhl     /// If the work item type is annotated with any lifetimes, then you must not call the function
294d4d791d4SAlice Ryhl     /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
295d4d791d4SAlice Ryhl     ///
296d4d791d4SAlice Ryhl     /// If the work item type is not [`Send`], then the function pointer must be called on the same
297d4d791d4SAlice Ryhl     /// thread as the call to `__enqueue`.
298d4d791d4SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
299d4d791d4SAlice Ryhl     where
300d4d791d4SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool;
301d4d791d4SAlice Ryhl }
30203394130SWedson Almeida Filho 
3037324b889SAlice Ryhl /// Defines the method that should be called directly when a work item is executed.
3047324b889SAlice Ryhl ///
3054c799d1dSValentin Obst /// This trait is implemented by `Pin<Box<T>>` and [`Arc<T>`], and is mainly intended to be
3067324b889SAlice Ryhl /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`]
3074c799d1dSValentin Obst /// instead. The [`run`] method on this trait will usually just perform the appropriate
3084c799d1dSValentin Obst /// `container_of` translation and then call into the [`run`][WorkItem::run] method from the
3094c799d1dSValentin Obst /// [`WorkItem`] trait.
3107324b889SAlice Ryhl ///
3117324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
3127324b889SAlice Ryhl ///
3137324b889SAlice Ryhl /// # Safety
3147324b889SAlice Ryhl ///
3157324b889SAlice Ryhl /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
3167324b889SAlice Ryhl /// method of this trait as the function pointer.
3177324b889SAlice Ryhl ///
3187324b889SAlice Ryhl /// [`__enqueue`]: RawWorkItem::__enqueue
3197324b889SAlice Ryhl /// [`run`]: WorkItemPointer::run
3207324b889SAlice Ryhl pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
3217324b889SAlice Ryhl     /// Run this work item.
3227324b889SAlice Ryhl     ///
3237324b889SAlice Ryhl     /// # Safety
3247324b889SAlice Ryhl     ///
3254c799d1dSValentin Obst     /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`]
3264c799d1dSValentin Obst     /// where the `queue_work_on` closure returned true, and the pointer must still be valid.
3274c799d1dSValentin Obst     ///
3284c799d1dSValentin Obst     /// [`__enqueue`]: RawWorkItem::__enqueue
3297324b889SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
3307324b889SAlice Ryhl }
3317324b889SAlice Ryhl 
3327324b889SAlice Ryhl /// Defines the method that should be called when this work item is executed.
3337324b889SAlice Ryhl ///
3347324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
3357324b889SAlice Ryhl pub trait WorkItem<const ID: u64 = 0> {
3367324b889SAlice Ryhl     /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
3377324b889SAlice Ryhl     /// `Pin<Box<Self>>`.
3387324b889SAlice Ryhl     type Pointer: WorkItemPointer<ID>;
3397324b889SAlice Ryhl 
3407324b889SAlice Ryhl     /// The method that should be called when this work item is executed.
3417324b889SAlice Ryhl     fn run(this: Self::Pointer);
3427324b889SAlice Ryhl }
3437324b889SAlice Ryhl 
3447324b889SAlice Ryhl /// Links for a work item.
3457324b889SAlice Ryhl ///
3464c799d1dSValentin Obst /// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`]
3477324b889SAlice Ryhl /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
3487324b889SAlice Ryhl ///
3497324b889SAlice Ryhl /// Wraps the kernel's C `struct work_struct`.
3507324b889SAlice Ryhl ///
3517324b889SAlice Ryhl /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
3524c799d1dSValentin Obst ///
3534c799d1dSValentin Obst /// [`run`]: WorkItemPointer::run
3548db31d3fSBenno Lossin #[pin_data]
3557324b889SAlice Ryhl #[repr(transparent)]
3567324b889SAlice Ryhl pub struct Work<T: ?Sized, const ID: u64 = 0> {
3578db31d3fSBenno Lossin     #[pin]
3587324b889SAlice Ryhl     work: Opaque<bindings::work_struct>,
3597324b889SAlice Ryhl     _inner: PhantomData<T>,
3607324b889SAlice Ryhl }
3617324b889SAlice Ryhl 
3627324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
3637324b889SAlice Ryhl //
3647324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
3657324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
3667324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
3677324b889SAlice Ryhl //
3687324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
3697324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
3707324b889SAlice Ryhl 
3717324b889SAlice Ryhl impl<T: ?Sized, const ID: u64> Work<T, ID> {
3727324b889SAlice Ryhl     /// Creates a new instance of [`Work`].
3737324b889SAlice Ryhl     #[inline]
3747324b889SAlice Ryhl     #[allow(clippy::new_ret_no_self)]
3757324b889SAlice Ryhl     pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
3767324b889SAlice Ryhl     where
3777324b889SAlice Ryhl         T: WorkItem<ID>,
3787324b889SAlice Ryhl     {
3798db31d3fSBenno Lossin         pin_init!(Self {
3808db31d3fSBenno Lossin             work <- Opaque::ffi_init(|slot| {
3818db31d3fSBenno Lossin                 // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as
3828db31d3fSBenno Lossin                 // the work item function.
3837324b889SAlice Ryhl                 unsafe {
3847324b889SAlice Ryhl                     bindings::init_work_with_key(
3857324b889SAlice Ryhl                         slot,
3867324b889SAlice Ryhl                         Some(T::Pointer::run),
3877324b889SAlice Ryhl                         false,
3887324b889SAlice Ryhl                         name.as_char_ptr(),
3897324b889SAlice Ryhl                         key.as_ptr(),
3908db31d3fSBenno Lossin                     )
3917324b889SAlice Ryhl                 }
3928db31d3fSBenno Lossin             }),
3938db31d3fSBenno Lossin             _inner: PhantomData,
3948db31d3fSBenno Lossin         })
3957324b889SAlice Ryhl     }
3967324b889SAlice Ryhl 
3977324b889SAlice Ryhl     /// Get a pointer to the inner `work_struct`.
3987324b889SAlice Ryhl     ///
3997324b889SAlice Ryhl     /// # Safety
4007324b889SAlice Ryhl     ///
4017324b889SAlice Ryhl     /// The provided pointer must not be dangling and must be properly aligned. (But the memory
4027324b889SAlice Ryhl     /// need not be initialized.)
4037324b889SAlice Ryhl     #[inline]
4047324b889SAlice Ryhl     pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
4057324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is aligned and not dangling.
4067324b889SAlice Ryhl         //
4077324b889SAlice Ryhl         // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
4087324b889SAlice Ryhl         // the compiler does not complain that the `work` field is unused.
4097324b889SAlice Ryhl         unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
4107324b889SAlice Ryhl     }
4117324b889SAlice Ryhl }
4127324b889SAlice Ryhl 
4137324b889SAlice Ryhl /// Declares that a type has a [`Work<T, ID>`] field.
4147324b889SAlice Ryhl ///
4157324b889SAlice Ryhl /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
4167324b889SAlice Ryhl /// like this:
4177324b889SAlice Ryhl ///
4187324b889SAlice Ryhl /// ```no_run
4197324b889SAlice Ryhl /// use kernel::prelude::*;
420e283ee23SAlice Ryhl /// use kernel::workqueue::{impl_has_work, Work};
4217324b889SAlice Ryhl ///
4227324b889SAlice Ryhl /// struct MyWorkItem {
4237324b889SAlice Ryhl ///     work_field: Work<MyWorkItem, 1>,
4247324b889SAlice Ryhl /// }
4257324b889SAlice Ryhl ///
4267324b889SAlice Ryhl /// impl_has_work! {
4277324b889SAlice Ryhl ///     impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
4287324b889SAlice Ryhl /// }
4297324b889SAlice Ryhl /// ```
4307324b889SAlice Ryhl ///
4314c799d1dSValentin Obst /// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct`
4327324b889SAlice Ryhl /// fields by using a different id for each one.
4337324b889SAlice Ryhl ///
4347324b889SAlice Ryhl /// # Safety
4357324b889SAlice Ryhl ///
436af8b18d7SValentin Obst /// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The
437af8b18d7SValentin Obst /// methods on this trait must have exactly the behavior that the definitions given below have.
4387324b889SAlice Ryhl ///
4397324b889SAlice Ryhl /// [`impl_has_work!`]: crate::impl_has_work
4407324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET
4417324b889SAlice Ryhl pub unsafe trait HasWork<T, const ID: u64 = 0> {
4427324b889SAlice Ryhl     /// The offset of the [`Work<T, ID>`] field.
4437324b889SAlice Ryhl     const OFFSET: usize;
4447324b889SAlice Ryhl 
4457324b889SAlice Ryhl     /// Returns the offset of the [`Work<T, ID>`] field.
4467324b889SAlice Ryhl     ///
447af8b18d7SValentin Obst     /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not
4484c799d1dSValentin Obst     /// [`Sized`].
4497324b889SAlice Ryhl     ///
4507324b889SAlice Ryhl     /// [`OFFSET`]: HasWork::OFFSET
4517324b889SAlice Ryhl     #[inline]
4527324b889SAlice Ryhl     fn get_work_offset(&self) -> usize {
4537324b889SAlice Ryhl         Self::OFFSET
4547324b889SAlice Ryhl     }
4557324b889SAlice Ryhl 
4567324b889SAlice Ryhl     /// Returns a pointer to the [`Work<T, ID>`] field.
4577324b889SAlice Ryhl     ///
4587324b889SAlice Ryhl     /// # Safety
4597324b889SAlice Ryhl     ///
4607324b889SAlice Ryhl     /// The provided pointer must point at a valid struct of type `Self`.
4617324b889SAlice Ryhl     #[inline]
4627324b889SAlice Ryhl     unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
4637324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is valid.
4647324b889SAlice Ryhl         unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> }
4657324b889SAlice Ryhl     }
4667324b889SAlice Ryhl 
4677324b889SAlice Ryhl     /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
4687324b889SAlice Ryhl     ///
4697324b889SAlice Ryhl     /// # Safety
4707324b889SAlice Ryhl     ///
4717324b889SAlice Ryhl     /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
4727324b889SAlice Ryhl     #[inline]
4737324b889SAlice Ryhl     unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self
4747324b889SAlice Ryhl     where
4757324b889SAlice Ryhl         Self: Sized,
4767324b889SAlice Ryhl     {
4777324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer points at a field of the right type in the
4787324b889SAlice Ryhl         // right kind of struct.
4797324b889SAlice Ryhl         unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self }
4807324b889SAlice Ryhl     }
4817324b889SAlice Ryhl }
4827324b889SAlice Ryhl 
4837324b889SAlice Ryhl /// Used to safely implement the [`HasWork<T, ID>`] trait.
4847324b889SAlice Ryhl ///
4857324b889SAlice Ryhl /// # Examples
4867324b889SAlice Ryhl ///
4877324b889SAlice Ryhl /// ```
4887324b889SAlice Ryhl /// use kernel::sync::Arc;
489e283ee23SAlice Ryhl /// use kernel::workqueue::{self, impl_has_work, Work};
4907324b889SAlice Ryhl ///
4917324b889SAlice Ryhl /// struct MyStruct {
4927324b889SAlice Ryhl ///     work_field: Work<MyStruct, 17>,
4937324b889SAlice Ryhl /// }
4947324b889SAlice Ryhl ///
4957324b889SAlice Ryhl /// impl_has_work! {
4967324b889SAlice Ryhl ///     impl HasWork<MyStruct, 17> for MyStruct { self.work_field }
4977324b889SAlice Ryhl /// }
4987324b889SAlice Ryhl /// ```
4997324b889SAlice Ryhl #[macro_export]
5007324b889SAlice Ryhl macro_rules! impl_has_work {
5017324b889SAlice Ryhl     ($(impl$(<$($implarg:ident),*>)?
5027324b889SAlice Ryhl        HasWork<$work_type:ty $(, $id:tt)?>
5037324b889SAlice Ryhl        for $self:ident $(<$($selfarg:ident),*>)?
5047324b889SAlice Ryhl        { self.$field:ident }
5057324b889SAlice Ryhl     )*) => {$(
5067324b889SAlice Ryhl         // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right
5077324b889SAlice Ryhl         // type.
5087324b889SAlice Ryhl         unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self $(<$($selfarg),*>)? {
5097324b889SAlice Ryhl             const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
5107324b889SAlice Ryhl 
5117324b889SAlice Ryhl             #[inline]
5127324b889SAlice Ryhl             unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
5137324b889SAlice Ryhl                 // SAFETY: The caller promises that the pointer is not dangling.
5147324b889SAlice Ryhl                 unsafe {
5157324b889SAlice Ryhl                     ::core::ptr::addr_of_mut!((*ptr).$field)
5167324b889SAlice Ryhl                 }
5177324b889SAlice Ryhl             }
5187324b889SAlice Ryhl         }
5197324b889SAlice Ryhl     )*};
5207324b889SAlice Ryhl }
521e283ee23SAlice Ryhl pub use impl_has_work;
5227324b889SAlice Ryhl 
523115c95e9SAlice Ryhl impl_has_work! {
524115c95e9SAlice Ryhl     impl<T> HasWork<Self> for ClosureWork<T> { self.work }
525115c95e9SAlice Ryhl }
526115c95e9SAlice Ryhl 
52747f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>
52847f0dbe8SAlice Ryhl where
52947f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
53047f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
53147f0dbe8SAlice Ryhl {
53247f0dbe8SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
53347f0dbe8SAlice Ryhl         // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
53447f0dbe8SAlice Ryhl         let ptr = ptr as *mut Work<T, ID>;
53547f0dbe8SAlice Ryhl         // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
53647f0dbe8SAlice Ryhl         let ptr = unsafe { T::work_container_of(ptr) };
53747f0dbe8SAlice Ryhl         // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
53847f0dbe8SAlice Ryhl         let arc = unsafe { Arc::from_raw(ptr) };
53947f0dbe8SAlice Ryhl 
54047f0dbe8SAlice Ryhl         T::run(arc)
54147f0dbe8SAlice Ryhl     }
54247f0dbe8SAlice Ryhl }
54347f0dbe8SAlice Ryhl 
54447f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T>
54547f0dbe8SAlice Ryhl where
54647f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
54747f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
54847f0dbe8SAlice Ryhl {
54947f0dbe8SAlice Ryhl     type EnqueueOutput = Result<(), Self>;
55047f0dbe8SAlice Ryhl 
55147f0dbe8SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
55247f0dbe8SAlice Ryhl     where
55347f0dbe8SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool,
55447f0dbe8SAlice Ryhl     {
55547f0dbe8SAlice Ryhl         // Casting between const and mut is not a problem as long as the pointer is a raw pointer.
55647f0dbe8SAlice Ryhl         let ptr = Arc::into_raw(self).cast_mut();
55747f0dbe8SAlice Ryhl 
55847f0dbe8SAlice Ryhl         // SAFETY: Pointers into an `Arc` point at a valid value.
55947f0dbe8SAlice Ryhl         let work_ptr = unsafe { T::raw_get_work(ptr) };
56047f0dbe8SAlice Ryhl         // SAFETY: `raw_get_work` returns a pointer to a valid value.
56147f0dbe8SAlice Ryhl         let work_ptr = unsafe { Work::raw_get(work_ptr) };
56247f0dbe8SAlice Ryhl 
56347f0dbe8SAlice Ryhl         if queue_work_on(work_ptr) {
56447f0dbe8SAlice Ryhl             Ok(())
56547f0dbe8SAlice Ryhl         } else {
56647f0dbe8SAlice Ryhl             // SAFETY: The work queue has not taken ownership of the pointer.
56747f0dbe8SAlice Ryhl             Err(unsafe { Arc::from_raw(ptr) })
56847f0dbe8SAlice Ryhl         }
56947f0dbe8SAlice Ryhl     }
57047f0dbe8SAlice Ryhl }
57147f0dbe8SAlice Ryhl 
57247f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<Box<T>>
57347f0dbe8SAlice Ryhl where
57447f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
57547f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
57647f0dbe8SAlice Ryhl {
57747f0dbe8SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
57847f0dbe8SAlice Ryhl         // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
57947f0dbe8SAlice Ryhl         let ptr = ptr as *mut Work<T, ID>;
58047f0dbe8SAlice Ryhl         // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
58147f0dbe8SAlice Ryhl         let ptr = unsafe { T::work_container_of(ptr) };
58247f0dbe8SAlice Ryhl         // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
58347f0dbe8SAlice Ryhl         let boxed = unsafe { Box::from_raw(ptr) };
58447f0dbe8SAlice Ryhl         // SAFETY: The box was already pinned when it was enqueued.
58547f0dbe8SAlice Ryhl         let pinned = unsafe { Pin::new_unchecked(boxed) };
58647f0dbe8SAlice Ryhl 
58747f0dbe8SAlice Ryhl         T::run(pinned)
58847f0dbe8SAlice Ryhl     }
58947f0dbe8SAlice Ryhl }
59047f0dbe8SAlice Ryhl 
59147f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<Box<T>>
59247f0dbe8SAlice Ryhl where
59347f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
59447f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
59547f0dbe8SAlice Ryhl {
59647f0dbe8SAlice Ryhl     type EnqueueOutput = ();
59747f0dbe8SAlice Ryhl 
59847f0dbe8SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
59947f0dbe8SAlice Ryhl     where
60047f0dbe8SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool,
60147f0dbe8SAlice Ryhl     {
60247f0dbe8SAlice Ryhl         // SAFETY: We're not going to move `self` or any of its fields, so its okay to temporarily
60347f0dbe8SAlice Ryhl         // remove the `Pin` wrapper.
60447f0dbe8SAlice Ryhl         let boxed = unsafe { Pin::into_inner_unchecked(self) };
60547f0dbe8SAlice Ryhl         let ptr = Box::into_raw(boxed);
60647f0dbe8SAlice Ryhl 
60747f0dbe8SAlice Ryhl         // SAFETY: Pointers into a `Box` point at a valid value.
60847f0dbe8SAlice Ryhl         let work_ptr = unsafe { T::raw_get_work(ptr) };
60947f0dbe8SAlice Ryhl         // SAFETY: `raw_get_work` returns a pointer to a valid value.
61047f0dbe8SAlice Ryhl         let work_ptr = unsafe { Work::raw_get(work_ptr) };
61147f0dbe8SAlice Ryhl 
61247f0dbe8SAlice Ryhl         if !queue_work_on(work_ptr) {
61347f0dbe8SAlice Ryhl             // SAFETY: This method requires exclusive ownership of the box, so it cannot be in a
61447f0dbe8SAlice Ryhl             // workqueue.
61547f0dbe8SAlice Ryhl             unsafe { ::core::hint::unreachable_unchecked() }
61647f0dbe8SAlice Ryhl         }
61747f0dbe8SAlice Ryhl     }
61847f0dbe8SAlice Ryhl }
61947f0dbe8SAlice Ryhl 
62003394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`).
62103394130SWedson Almeida Filho ///
62203394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
62303394130SWedson Almeida Filho /// users which expect relatively short queue flush time.
62403394130SWedson Almeida Filho ///
62503394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long.
62603394130SWedson Almeida Filho pub fn system() -> &'static Queue {
62703394130SWedson Almeida Filho     // SAFETY: `system_wq` is a C global, always available.
62803394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_wq) }
62903394130SWedson Almeida Filho }
63003394130SWedson Almeida Filho 
63103394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`).
63203394130SWedson Almeida Filho ///
63303394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher
63403394130SWedson Almeida Filho /// scheduling priority.
63503394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue {
63603394130SWedson Almeida Filho     // SAFETY: `system_highpri_wq` is a C global, always available.
63703394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_highpri_wq) }
63803394130SWedson Almeida Filho }
63903394130SWedson Almeida Filho 
64003394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`).
64103394130SWedson Almeida Filho ///
64203394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue
64303394130SWedson Almeida Filho /// flushing might take relatively long.
64403394130SWedson Almeida Filho pub fn system_long() -> &'static Queue {
64503394130SWedson Almeida Filho     // SAFETY: `system_long_wq` is a C global, always available.
64603394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_long_wq) }
64703394130SWedson Almeida Filho }
64803394130SWedson Almeida Filho 
64903394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`).
65003394130SWedson Almeida Filho ///
65103394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
65203394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are
65303394130SWedson Almeida Filho /// available.
65403394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue {
65503394130SWedson Almeida Filho     // SAFETY: `system_unbound_wq` is a C global, always available.
65603394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_unbound_wq) }
65703394130SWedson Almeida Filho }
65803394130SWedson Almeida Filho 
65903394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`).
66003394130SWedson Almeida Filho ///
66103394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable.
66203394130SWedson Almeida Filho ///
66303394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
66403394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
66503394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue {
66603394130SWedson Almeida Filho     // SAFETY: `system_freezable_wq` is a C global, always available.
66703394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_wq) }
66803394130SWedson Almeida Filho }
66903394130SWedson Almeida Filho 
67003394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`).
67103394130SWedson Almeida Filho ///
67203394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the
67303394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
67403394130SWedson Almeida Filho /// returned by [`system`].
67503394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue {
67603394130SWedson Almeida Filho     // SAFETY: `system_power_efficient_wq` is a C global, always available.
67703394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
67803394130SWedson Almeida Filho }
67903394130SWedson Almeida Filho 
68003394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`).
68103394130SWedson Almeida Filho ///
68203394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable.
68303394130SWedson Almeida Filho ///
68403394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
68503394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
68603394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue {
68703394130SWedson Almeida Filho     // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available.
68803394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
68903394130SWedson Almeida Filho }
690