xref: /linux-6.15/rust/kernel/workqueue.rs (revision e283ee23)
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;
38*e283ee23SAlice 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"),
5615b286d1SAlice Ryhl //!         }))
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;
80*e283ee23SAlice 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"),
10415b286d1SAlice Ryhl //!         }))
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 
13547f0dbe8SAlice Ryhl use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
136115c95e9SAlice Ryhl use alloc::alloc::AllocError;
13747f0dbe8SAlice Ryhl use alloc::boxed::Box;
1387324b889SAlice Ryhl use core::marker::PhantomData;
13947f0dbe8SAlice Ryhl use core::pin::Pin;
1407324b889SAlice Ryhl 
1417324b889SAlice Ryhl /// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
1427324b889SAlice Ryhl #[macro_export]
1437324b889SAlice Ryhl macro_rules! new_work {
1447324b889SAlice Ryhl     ($($name:literal)?) => {
1457324b889SAlice Ryhl         $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
1467324b889SAlice Ryhl     };
1477324b889SAlice Ryhl }
148*e283ee23SAlice Ryhl pub use new_work;
149d4d791d4SAlice Ryhl 
150d4d791d4SAlice Ryhl /// A kernel work queue.
151d4d791d4SAlice Ryhl ///
152d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`.
153d4d791d4SAlice Ryhl ///
154d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are
155d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc.
156d4d791d4SAlice Ryhl #[repr(transparent)]
157d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>);
158d4d791d4SAlice Ryhl 
159d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
160d4d791d4SAlice Ryhl unsafe impl Send for Queue {}
161d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
162d4d791d4SAlice Ryhl unsafe impl Sync for Queue {}
163d4d791d4SAlice Ryhl 
164d4d791d4SAlice Ryhl impl Queue {
165d4d791d4SAlice Ryhl     /// Use the provided `struct workqueue_struct` with Rust.
166d4d791d4SAlice Ryhl     ///
167d4d791d4SAlice Ryhl     /// # Safety
168d4d791d4SAlice Ryhl     ///
169d4d791d4SAlice Ryhl     /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
170af8b18d7SValentin Obst     /// valid workqueue, and that it remains valid until the end of `'a`.
171d4d791d4SAlice Ryhl     pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
172d4d791d4SAlice Ryhl         // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
173d4d791d4SAlice Ryhl         // caller promises that the pointer is not dangling.
174d4d791d4SAlice Ryhl         unsafe { &*(ptr as *const Queue) }
175d4d791d4SAlice Ryhl     }
176d4d791d4SAlice Ryhl 
177d4d791d4SAlice Ryhl     /// Enqueues a work item.
178d4d791d4SAlice Ryhl     ///
179d4d791d4SAlice Ryhl     /// This may fail if the work item is already enqueued in a workqueue.
180d4d791d4SAlice Ryhl     ///
181d4d791d4SAlice Ryhl     /// The work item will be submitted using `WORK_CPU_UNBOUND`.
182d4d791d4SAlice Ryhl     pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
183d4d791d4SAlice Ryhl     where
184d4d791d4SAlice Ryhl         W: RawWorkItem<ID> + Send + 'static,
185d4d791d4SAlice Ryhl     {
186d4d791d4SAlice Ryhl         let queue_ptr = self.0.get();
187d4d791d4SAlice Ryhl 
188d4d791d4SAlice Ryhl         // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
189d4d791d4SAlice Ryhl         // `__enqueue` requirements are not relevant since `W` is `Send` and static.
190d4d791d4SAlice Ryhl         //
191d4d791d4SAlice Ryhl         // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
192d4d791d4SAlice Ryhl         // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
193d4d791d4SAlice Ryhl         // closure.
194d4d791d4SAlice Ryhl         //
195d4d791d4SAlice Ryhl         // Furthermore, if the C workqueue code accesses the pointer after this call to
196d4d791d4SAlice Ryhl         // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
197d4d791d4SAlice Ryhl         // will have returned true. In this case, `__enqueue` promises that the raw pointer will
198d4d791d4SAlice Ryhl         // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
199d4d791d4SAlice Ryhl         unsafe {
200d4d791d4SAlice Ryhl             w.__enqueue(move |work_ptr| {
201d4d791d4SAlice Ryhl                 bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr)
202d4d791d4SAlice Ryhl             })
203d4d791d4SAlice Ryhl         }
204d4d791d4SAlice Ryhl     }
205115c95e9SAlice Ryhl 
206115c95e9SAlice Ryhl     /// Tries to spawn the given function or closure as a work item.
207115c95e9SAlice Ryhl     ///
208115c95e9SAlice Ryhl     /// This method can fail because it allocates memory to store the work item.
209115c95e9SAlice Ryhl     pub fn try_spawn<T: 'static + Send + FnOnce()>(&self, func: T) -> Result<(), AllocError> {
210115c95e9SAlice Ryhl         let init = pin_init!(ClosureWork {
211115c95e9SAlice Ryhl             work <- new_work!("Queue::try_spawn"),
212115c95e9SAlice Ryhl             func: Some(func),
213115c95e9SAlice Ryhl         });
214115c95e9SAlice Ryhl 
215115c95e9SAlice Ryhl         self.enqueue(Box::pin_init(init).map_err(|_| AllocError)?);
216115c95e9SAlice Ryhl         Ok(())
217115c95e9SAlice Ryhl     }
218115c95e9SAlice Ryhl }
219115c95e9SAlice Ryhl 
2204c799d1dSValentin Obst /// A helper type used in [`try_spawn`].
2214c799d1dSValentin Obst ///
2224c799d1dSValentin Obst /// [`try_spawn`]: Queue::try_spawn
223115c95e9SAlice Ryhl #[pin_data]
224115c95e9SAlice Ryhl struct ClosureWork<T> {
225115c95e9SAlice Ryhl     #[pin]
226115c95e9SAlice Ryhl     work: Work<ClosureWork<T>>,
227115c95e9SAlice Ryhl     func: Option<T>,
228115c95e9SAlice Ryhl }
229115c95e9SAlice Ryhl 
230115c95e9SAlice Ryhl impl<T> ClosureWork<T> {
231115c95e9SAlice Ryhl     fn project(self: Pin<&mut Self>) -> &mut Option<T> {
232115c95e9SAlice Ryhl         // SAFETY: The `func` field is not structurally pinned.
233115c95e9SAlice Ryhl         unsafe { &mut self.get_unchecked_mut().func }
234115c95e9SAlice Ryhl     }
235115c95e9SAlice Ryhl }
236115c95e9SAlice Ryhl 
237115c95e9SAlice Ryhl impl<T: FnOnce()> WorkItem for ClosureWork<T> {
238115c95e9SAlice Ryhl     type Pointer = Pin<Box<Self>>;
239115c95e9SAlice Ryhl 
240115c95e9SAlice Ryhl     fn run(mut this: Pin<Box<Self>>) {
241115c95e9SAlice Ryhl         if let Some(func) = this.as_mut().project().take() {
242115c95e9SAlice Ryhl             (func)()
243115c95e9SAlice Ryhl         }
244115c95e9SAlice Ryhl     }
245d4d791d4SAlice Ryhl }
246d4d791d4SAlice Ryhl 
247d4d791d4SAlice Ryhl /// A raw work item.
248d4d791d4SAlice Ryhl ///
249d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible.
250d4d791d4SAlice Ryhl ///
251d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple
252d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
253d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The
254d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields
255d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.)
256d4d791d4SAlice Ryhl ///
257b6cda913SValentin Obst /// Note that the id is used only to select the right method to call during compilation. It won't be
258d4d791d4SAlice Ryhl /// part of the final executable.
259d4d791d4SAlice Ryhl ///
260d4d791d4SAlice Ryhl /// # Safety
261d4d791d4SAlice Ryhl ///
2624c799d1dSValentin Obst /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`]
263d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for
2644c799d1dSValentin Obst /// [`__enqueue`].
2654c799d1dSValentin Obst ///
2664c799d1dSValentin Obst /// [`__enqueue`]: RawWorkItem::__enqueue
267d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> {
268d4d791d4SAlice Ryhl     /// The return type of [`Queue::enqueue`].
269d4d791d4SAlice Ryhl     type EnqueueOutput;
270d4d791d4SAlice Ryhl 
271d4d791d4SAlice Ryhl     /// Enqueues this work item on a queue using the provided `queue_work_on` method.
272d4d791d4SAlice Ryhl     ///
273d4d791d4SAlice Ryhl     /// # Guarantees
274d4d791d4SAlice Ryhl     ///
275d4d791d4SAlice Ryhl     /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
276d4d791d4SAlice Ryhl     /// valid `work_struct` for the duration of the call to the closure. If the closure returns
277d4d791d4SAlice Ryhl     /// true, then it is further guaranteed that the pointer remains valid until someone calls the
278d4d791d4SAlice Ryhl     /// function pointer stored in the `work_struct`.
279d4d791d4SAlice Ryhl     ///
280d4d791d4SAlice Ryhl     /// # Safety
281d4d791d4SAlice Ryhl     ///
282d4d791d4SAlice Ryhl     /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
283d4d791d4SAlice Ryhl     ///
284d4d791d4SAlice Ryhl     /// If the work item type is annotated with any lifetimes, then you must not call the function
285d4d791d4SAlice Ryhl     /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
286d4d791d4SAlice Ryhl     ///
287d4d791d4SAlice Ryhl     /// If the work item type is not [`Send`], then the function pointer must be called on the same
288d4d791d4SAlice Ryhl     /// thread as the call to `__enqueue`.
289d4d791d4SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
290d4d791d4SAlice Ryhl     where
291d4d791d4SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool;
292d4d791d4SAlice Ryhl }
29303394130SWedson Almeida Filho 
2947324b889SAlice Ryhl /// Defines the method that should be called directly when a work item is executed.
2957324b889SAlice Ryhl ///
2964c799d1dSValentin Obst /// This trait is implemented by `Pin<Box<T>>` and [`Arc<T>`], and is mainly intended to be
2977324b889SAlice Ryhl /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`]
2984c799d1dSValentin Obst /// instead. The [`run`] method on this trait will usually just perform the appropriate
2994c799d1dSValentin Obst /// `container_of` translation and then call into the [`run`][WorkItem::run] method from the
3004c799d1dSValentin Obst /// [`WorkItem`] trait.
3017324b889SAlice Ryhl ///
3027324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
3037324b889SAlice Ryhl ///
3047324b889SAlice Ryhl /// # Safety
3057324b889SAlice Ryhl ///
3067324b889SAlice Ryhl /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
3077324b889SAlice Ryhl /// method of this trait as the function pointer.
3087324b889SAlice Ryhl ///
3097324b889SAlice Ryhl /// [`__enqueue`]: RawWorkItem::__enqueue
3107324b889SAlice Ryhl /// [`run`]: WorkItemPointer::run
3117324b889SAlice Ryhl pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
3127324b889SAlice Ryhl     /// Run this work item.
3137324b889SAlice Ryhl     ///
3147324b889SAlice Ryhl     /// # Safety
3157324b889SAlice Ryhl     ///
3164c799d1dSValentin Obst     /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`]
3174c799d1dSValentin Obst     /// where the `queue_work_on` closure returned true, and the pointer must still be valid.
3184c799d1dSValentin Obst     ///
3194c799d1dSValentin Obst     /// [`__enqueue`]: RawWorkItem::__enqueue
3207324b889SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
3217324b889SAlice Ryhl }
3227324b889SAlice Ryhl 
3237324b889SAlice Ryhl /// Defines the method that should be called when this work item is executed.
3247324b889SAlice Ryhl ///
3257324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
3267324b889SAlice Ryhl pub trait WorkItem<const ID: u64 = 0> {
3277324b889SAlice Ryhl     /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
3287324b889SAlice Ryhl     /// `Pin<Box<Self>>`.
3297324b889SAlice Ryhl     type Pointer: WorkItemPointer<ID>;
3307324b889SAlice Ryhl 
3317324b889SAlice Ryhl     /// The method that should be called when this work item is executed.
3327324b889SAlice Ryhl     fn run(this: Self::Pointer);
3337324b889SAlice Ryhl }
3347324b889SAlice Ryhl 
3357324b889SAlice Ryhl /// Links for a work item.
3367324b889SAlice Ryhl ///
3374c799d1dSValentin Obst /// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`]
3387324b889SAlice Ryhl /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
3397324b889SAlice Ryhl ///
3407324b889SAlice Ryhl /// Wraps the kernel's C `struct work_struct`.
3417324b889SAlice Ryhl ///
3427324b889SAlice Ryhl /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
3434c799d1dSValentin Obst ///
3444c799d1dSValentin Obst /// [`run`]: WorkItemPointer::run
3457324b889SAlice Ryhl #[repr(transparent)]
3467324b889SAlice Ryhl pub struct Work<T: ?Sized, const ID: u64 = 0> {
3477324b889SAlice Ryhl     work: Opaque<bindings::work_struct>,
3487324b889SAlice Ryhl     _inner: PhantomData<T>,
3497324b889SAlice Ryhl }
3507324b889SAlice Ryhl 
3517324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
3527324b889SAlice Ryhl //
3537324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
3547324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
3557324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
3567324b889SAlice Ryhl //
3577324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
3587324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
3597324b889SAlice Ryhl 
3607324b889SAlice Ryhl impl<T: ?Sized, const ID: u64> Work<T, ID> {
3617324b889SAlice Ryhl     /// Creates a new instance of [`Work`].
3627324b889SAlice Ryhl     #[inline]
3637324b889SAlice Ryhl     #[allow(clippy::new_ret_no_self)]
3647324b889SAlice Ryhl     pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
3657324b889SAlice Ryhl     where
3667324b889SAlice Ryhl         T: WorkItem<ID>,
3677324b889SAlice Ryhl     {
3687324b889SAlice Ryhl         // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the work
3697324b889SAlice Ryhl         // item function.
3707324b889SAlice Ryhl         unsafe {
3717324b889SAlice Ryhl             kernel::init::pin_init_from_closure(move |slot| {
3727324b889SAlice Ryhl                 let slot = Self::raw_get(slot);
3737324b889SAlice Ryhl                 bindings::init_work_with_key(
3747324b889SAlice Ryhl                     slot,
3757324b889SAlice Ryhl                     Some(T::Pointer::run),
3767324b889SAlice Ryhl                     false,
3777324b889SAlice Ryhl                     name.as_char_ptr(),
3787324b889SAlice Ryhl                     key.as_ptr(),
3797324b889SAlice Ryhl                 );
3807324b889SAlice Ryhl                 Ok(())
3817324b889SAlice Ryhl             })
3827324b889SAlice Ryhl         }
3837324b889SAlice Ryhl     }
3847324b889SAlice Ryhl 
3857324b889SAlice Ryhl     /// Get a pointer to the inner `work_struct`.
3867324b889SAlice Ryhl     ///
3877324b889SAlice Ryhl     /// # Safety
3887324b889SAlice Ryhl     ///
3897324b889SAlice Ryhl     /// The provided pointer must not be dangling and must be properly aligned. (But the memory
3907324b889SAlice Ryhl     /// need not be initialized.)
3917324b889SAlice Ryhl     #[inline]
3927324b889SAlice Ryhl     pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
3937324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is aligned and not dangling.
3947324b889SAlice Ryhl         //
3957324b889SAlice Ryhl         // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
3967324b889SAlice Ryhl         // the compiler does not complain that the `work` field is unused.
3977324b889SAlice Ryhl         unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
3987324b889SAlice Ryhl     }
3997324b889SAlice Ryhl }
4007324b889SAlice Ryhl 
4017324b889SAlice Ryhl /// Declares that a type has a [`Work<T, ID>`] field.
4027324b889SAlice Ryhl ///
4037324b889SAlice Ryhl /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
4047324b889SAlice Ryhl /// like this:
4057324b889SAlice Ryhl ///
4067324b889SAlice Ryhl /// ```no_run
4077324b889SAlice Ryhl /// use kernel::prelude::*;
408*e283ee23SAlice Ryhl /// use kernel::workqueue::{impl_has_work, Work};
4097324b889SAlice Ryhl ///
4107324b889SAlice Ryhl /// struct MyWorkItem {
4117324b889SAlice Ryhl ///     work_field: Work<MyWorkItem, 1>,
4127324b889SAlice Ryhl /// }
4137324b889SAlice Ryhl ///
4147324b889SAlice Ryhl /// impl_has_work! {
4157324b889SAlice Ryhl ///     impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
4167324b889SAlice Ryhl /// }
4177324b889SAlice Ryhl /// ```
4187324b889SAlice Ryhl ///
4194c799d1dSValentin Obst /// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct`
4207324b889SAlice Ryhl /// fields by using a different id for each one.
4217324b889SAlice Ryhl ///
4227324b889SAlice Ryhl /// # Safety
4237324b889SAlice Ryhl ///
424af8b18d7SValentin Obst /// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The
425af8b18d7SValentin Obst /// methods on this trait must have exactly the behavior that the definitions given below have.
4267324b889SAlice Ryhl ///
4277324b889SAlice Ryhl /// [`impl_has_work!`]: crate::impl_has_work
4287324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET
4297324b889SAlice Ryhl pub unsafe trait HasWork<T, const ID: u64 = 0> {
4307324b889SAlice Ryhl     /// The offset of the [`Work<T, ID>`] field.
4317324b889SAlice Ryhl     const OFFSET: usize;
4327324b889SAlice Ryhl 
4337324b889SAlice Ryhl     /// Returns the offset of the [`Work<T, ID>`] field.
4347324b889SAlice Ryhl     ///
435af8b18d7SValentin Obst     /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not
4364c799d1dSValentin Obst     /// [`Sized`].
4377324b889SAlice Ryhl     ///
4387324b889SAlice Ryhl     /// [`OFFSET`]: HasWork::OFFSET
4397324b889SAlice Ryhl     #[inline]
4407324b889SAlice Ryhl     fn get_work_offset(&self) -> usize {
4417324b889SAlice Ryhl         Self::OFFSET
4427324b889SAlice Ryhl     }
4437324b889SAlice Ryhl 
4447324b889SAlice Ryhl     /// Returns a pointer to the [`Work<T, ID>`] field.
4457324b889SAlice Ryhl     ///
4467324b889SAlice Ryhl     /// # Safety
4477324b889SAlice Ryhl     ///
4487324b889SAlice Ryhl     /// The provided pointer must point at a valid struct of type `Self`.
4497324b889SAlice Ryhl     #[inline]
4507324b889SAlice Ryhl     unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
4517324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is valid.
4527324b889SAlice Ryhl         unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> }
4537324b889SAlice Ryhl     }
4547324b889SAlice Ryhl 
4557324b889SAlice Ryhl     /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
4567324b889SAlice Ryhl     ///
4577324b889SAlice Ryhl     /// # Safety
4587324b889SAlice Ryhl     ///
4597324b889SAlice Ryhl     /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
4607324b889SAlice Ryhl     #[inline]
4617324b889SAlice Ryhl     unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self
4627324b889SAlice Ryhl     where
4637324b889SAlice Ryhl         Self: Sized,
4647324b889SAlice Ryhl     {
4657324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer points at a field of the right type in the
4667324b889SAlice Ryhl         // right kind of struct.
4677324b889SAlice Ryhl         unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self }
4687324b889SAlice Ryhl     }
4697324b889SAlice Ryhl }
4707324b889SAlice Ryhl 
4717324b889SAlice Ryhl /// Used to safely implement the [`HasWork<T, ID>`] trait.
4727324b889SAlice Ryhl ///
4737324b889SAlice Ryhl /// # Examples
4747324b889SAlice Ryhl ///
4757324b889SAlice Ryhl /// ```
4767324b889SAlice Ryhl /// use kernel::sync::Arc;
477*e283ee23SAlice Ryhl /// use kernel::workqueue::{self, impl_has_work, Work};
4787324b889SAlice Ryhl ///
4797324b889SAlice Ryhl /// struct MyStruct {
4807324b889SAlice Ryhl ///     work_field: Work<MyStruct, 17>,
4817324b889SAlice Ryhl /// }
4827324b889SAlice Ryhl ///
4837324b889SAlice Ryhl /// impl_has_work! {
4847324b889SAlice Ryhl ///     impl HasWork<MyStruct, 17> for MyStruct { self.work_field }
4857324b889SAlice Ryhl /// }
4867324b889SAlice Ryhl /// ```
4877324b889SAlice Ryhl #[macro_export]
4887324b889SAlice Ryhl macro_rules! impl_has_work {
4897324b889SAlice Ryhl     ($(impl$(<$($implarg:ident),*>)?
4907324b889SAlice Ryhl        HasWork<$work_type:ty $(, $id:tt)?>
4917324b889SAlice Ryhl        for $self:ident $(<$($selfarg:ident),*>)?
4927324b889SAlice Ryhl        { self.$field:ident }
4937324b889SAlice Ryhl     )*) => {$(
4947324b889SAlice Ryhl         // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right
4957324b889SAlice Ryhl         // type.
4967324b889SAlice Ryhl         unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self $(<$($selfarg),*>)? {
4977324b889SAlice Ryhl             const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
4987324b889SAlice Ryhl 
4997324b889SAlice Ryhl             #[inline]
5007324b889SAlice Ryhl             unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
5017324b889SAlice Ryhl                 // SAFETY: The caller promises that the pointer is not dangling.
5027324b889SAlice Ryhl                 unsafe {
5037324b889SAlice Ryhl                     ::core::ptr::addr_of_mut!((*ptr).$field)
5047324b889SAlice Ryhl                 }
5057324b889SAlice Ryhl             }
5067324b889SAlice Ryhl         }
5077324b889SAlice Ryhl     )*};
5087324b889SAlice Ryhl }
509*e283ee23SAlice Ryhl pub use impl_has_work;
5107324b889SAlice Ryhl 
511115c95e9SAlice Ryhl impl_has_work! {
512115c95e9SAlice Ryhl     impl<T> HasWork<Self> for ClosureWork<T> { self.work }
513115c95e9SAlice Ryhl }
514115c95e9SAlice Ryhl 
51547f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>
51647f0dbe8SAlice Ryhl where
51747f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
51847f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
51947f0dbe8SAlice Ryhl {
52047f0dbe8SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
52147f0dbe8SAlice Ryhl         // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
52247f0dbe8SAlice Ryhl         let ptr = ptr as *mut Work<T, ID>;
52347f0dbe8SAlice Ryhl         // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
52447f0dbe8SAlice Ryhl         let ptr = unsafe { T::work_container_of(ptr) };
52547f0dbe8SAlice Ryhl         // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
52647f0dbe8SAlice Ryhl         let arc = unsafe { Arc::from_raw(ptr) };
52747f0dbe8SAlice Ryhl 
52847f0dbe8SAlice Ryhl         T::run(arc)
52947f0dbe8SAlice Ryhl     }
53047f0dbe8SAlice Ryhl }
53147f0dbe8SAlice Ryhl 
53247f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T>
53347f0dbe8SAlice Ryhl where
53447f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
53547f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
53647f0dbe8SAlice Ryhl {
53747f0dbe8SAlice Ryhl     type EnqueueOutput = Result<(), Self>;
53847f0dbe8SAlice Ryhl 
53947f0dbe8SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
54047f0dbe8SAlice Ryhl     where
54147f0dbe8SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool,
54247f0dbe8SAlice Ryhl     {
54347f0dbe8SAlice Ryhl         // Casting between const and mut is not a problem as long as the pointer is a raw pointer.
54447f0dbe8SAlice Ryhl         let ptr = Arc::into_raw(self).cast_mut();
54547f0dbe8SAlice Ryhl 
54647f0dbe8SAlice Ryhl         // SAFETY: Pointers into an `Arc` point at a valid value.
54747f0dbe8SAlice Ryhl         let work_ptr = unsafe { T::raw_get_work(ptr) };
54847f0dbe8SAlice Ryhl         // SAFETY: `raw_get_work` returns a pointer to a valid value.
54947f0dbe8SAlice Ryhl         let work_ptr = unsafe { Work::raw_get(work_ptr) };
55047f0dbe8SAlice Ryhl 
55147f0dbe8SAlice Ryhl         if queue_work_on(work_ptr) {
55247f0dbe8SAlice Ryhl             Ok(())
55347f0dbe8SAlice Ryhl         } else {
55447f0dbe8SAlice Ryhl             // SAFETY: The work queue has not taken ownership of the pointer.
55547f0dbe8SAlice Ryhl             Err(unsafe { Arc::from_raw(ptr) })
55647f0dbe8SAlice Ryhl         }
55747f0dbe8SAlice Ryhl     }
55847f0dbe8SAlice Ryhl }
55947f0dbe8SAlice Ryhl 
56047f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<Box<T>>
56147f0dbe8SAlice Ryhl where
56247f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
56347f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
56447f0dbe8SAlice Ryhl {
56547f0dbe8SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
56647f0dbe8SAlice Ryhl         // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
56747f0dbe8SAlice Ryhl         let ptr = ptr as *mut Work<T, ID>;
56847f0dbe8SAlice Ryhl         // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
56947f0dbe8SAlice Ryhl         let ptr = unsafe { T::work_container_of(ptr) };
57047f0dbe8SAlice Ryhl         // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
57147f0dbe8SAlice Ryhl         let boxed = unsafe { Box::from_raw(ptr) };
57247f0dbe8SAlice Ryhl         // SAFETY: The box was already pinned when it was enqueued.
57347f0dbe8SAlice Ryhl         let pinned = unsafe { Pin::new_unchecked(boxed) };
57447f0dbe8SAlice Ryhl 
57547f0dbe8SAlice Ryhl         T::run(pinned)
57647f0dbe8SAlice Ryhl     }
57747f0dbe8SAlice Ryhl }
57847f0dbe8SAlice Ryhl 
57947f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<Box<T>>
58047f0dbe8SAlice Ryhl where
58147f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
58247f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
58347f0dbe8SAlice Ryhl {
58447f0dbe8SAlice Ryhl     type EnqueueOutput = ();
58547f0dbe8SAlice Ryhl 
58647f0dbe8SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
58747f0dbe8SAlice Ryhl     where
58847f0dbe8SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool,
58947f0dbe8SAlice Ryhl     {
59047f0dbe8SAlice Ryhl         // SAFETY: We're not going to move `self` or any of its fields, so its okay to temporarily
59147f0dbe8SAlice Ryhl         // remove the `Pin` wrapper.
59247f0dbe8SAlice Ryhl         let boxed = unsafe { Pin::into_inner_unchecked(self) };
59347f0dbe8SAlice Ryhl         let ptr = Box::into_raw(boxed);
59447f0dbe8SAlice Ryhl 
59547f0dbe8SAlice Ryhl         // SAFETY: Pointers into a `Box` point at a valid value.
59647f0dbe8SAlice Ryhl         let work_ptr = unsafe { T::raw_get_work(ptr) };
59747f0dbe8SAlice Ryhl         // SAFETY: `raw_get_work` returns a pointer to a valid value.
59847f0dbe8SAlice Ryhl         let work_ptr = unsafe { Work::raw_get(work_ptr) };
59947f0dbe8SAlice Ryhl 
60047f0dbe8SAlice Ryhl         if !queue_work_on(work_ptr) {
60147f0dbe8SAlice Ryhl             // SAFETY: This method requires exclusive ownership of the box, so it cannot be in a
60247f0dbe8SAlice Ryhl             // workqueue.
60347f0dbe8SAlice Ryhl             unsafe { ::core::hint::unreachable_unchecked() }
60447f0dbe8SAlice Ryhl         }
60547f0dbe8SAlice Ryhl     }
60647f0dbe8SAlice Ryhl }
60747f0dbe8SAlice Ryhl 
60803394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`).
60903394130SWedson Almeida Filho ///
61003394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
61103394130SWedson Almeida Filho /// users which expect relatively short queue flush time.
61203394130SWedson Almeida Filho ///
61303394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long.
61403394130SWedson Almeida Filho pub fn system() -> &'static Queue {
61503394130SWedson Almeida Filho     // SAFETY: `system_wq` is a C global, always available.
61603394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_wq) }
61703394130SWedson Almeida Filho }
61803394130SWedson Almeida Filho 
61903394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`).
62003394130SWedson Almeida Filho ///
62103394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher
62203394130SWedson Almeida Filho /// scheduling priority.
62303394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue {
62403394130SWedson Almeida Filho     // SAFETY: `system_highpri_wq` is a C global, always available.
62503394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_highpri_wq) }
62603394130SWedson Almeida Filho }
62703394130SWedson Almeida Filho 
62803394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`).
62903394130SWedson Almeida Filho ///
63003394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue
63103394130SWedson Almeida Filho /// flushing might take relatively long.
63203394130SWedson Almeida Filho pub fn system_long() -> &'static Queue {
63303394130SWedson Almeida Filho     // SAFETY: `system_long_wq` is a C global, always available.
63403394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_long_wq) }
63503394130SWedson Almeida Filho }
63603394130SWedson Almeida Filho 
63703394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`).
63803394130SWedson Almeida Filho ///
63903394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
64003394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are
64103394130SWedson Almeida Filho /// available.
64203394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue {
64303394130SWedson Almeida Filho     // SAFETY: `system_unbound_wq` is a C global, always available.
64403394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_unbound_wq) }
64503394130SWedson Almeida Filho }
64603394130SWedson Almeida Filho 
64703394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`).
64803394130SWedson Almeida Filho ///
64903394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable.
65003394130SWedson Almeida Filho ///
65103394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
65203394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
65303394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue {
65403394130SWedson Almeida Filho     // SAFETY: `system_freezable_wq` is a C global, always available.
65503394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_wq) }
65603394130SWedson Almeida Filho }
65703394130SWedson Almeida Filho 
65803394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`).
65903394130SWedson Almeida Filho ///
66003394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the
66103394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
66203394130SWedson Almeida Filho /// returned by [`system`].
66303394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue {
66403394130SWedson Almeida Filho     // SAFETY: `system_power_efficient_wq` is a C global, always available.
66503394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
66603394130SWedson Almeida Filho }
66703394130SWedson Almeida Filho 
66803394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`).
66903394130SWedson Almeida Filho ///
67003394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable.
67103394130SWedson Almeida Filho ///
67203394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
67303394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
67403394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue {
67503394130SWedson Almeida Filho     // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available.
67603394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
67703394130SWedson Almeida Filho }
678