xref: /linux-6.15/rust/kernel/workqueue.rs (revision bc2e7d5c)
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 //!
157324b889SAlice Ryhl //! 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 //!
217324b889SAlice Ryhl //! The safe API is used via the `Work` struct and `WorkItem` traits. Furthermore, it also includes
227324b889SAlice Ryhl //! a trait called `WorkItemPointer`, which is usually not used directly by the user.
237324b889SAlice Ryhl //!
247324b889SAlice Ryhl //!  * The `Work` struct is the Rust wrapper for the C `work_struct` type.
257324b889SAlice Ryhl //!  * The `WorkItem` trait is implemented for structs that can be enqueued to a workqueue.
267324b889SAlice Ryhl //!  * The `WorkItemPointer` trait is implemented for the pointer type that points at a something
277324b889SAlice Ryhl //!    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;
3815b286d1SAlice Ryhl //! use kernel::workqueue::{self, Work, WorkItem};
3915b286d1SAlice Ryhl //! use kernel::{impl_has_work, new_work};
4015b286d1SAlice Ryhl //!
4115b286d1SAlice Ryhl //! #[pin_data]
4215b286d1SAlice Ryhl //! struct MyStruct {
4315b286d1SAlice Ryhl //!     value: i32,
4415b286d1SAlice Ryhl //!     #[pin]
4515b286d1SAlice Ryhl //!     work: Work<MyStruct>,
4615b286d1SAlice Ryhl //! }
4715b286d1SAlice Ryhl //!
4815b286d1SAlice Ryhl //! impl_has_work! {
4915b286d1SAlice Ryhl //!     impl HasWork<Self> for MyStruct { self.work }
5015b286d1SAlice Ryhl //! }
5115b286d1SAlice Ryhl //!
5215b286d1SAlice Ryhl //! impl MyStruct {
5315b286d1SAlice Ryhl //!     fn new(value: i32) -> Result<Arc<Self>> {
5415b286d1SAlice Ryhl //!         Arc::pin_init(pin_init!(MyStruct {
5515b286d1SAlice Ryhl //!             value,
5615b286d1SAlice Ryhl //!             work <- new_work!("MyStruct::work"),
5715b286d1SAlice Ryhl //!         }))
5815b286d1SAlice Ryhl //!     }
5915b286d1SAlice Ryhl //! }
6015b286d1SAlice Ryhl //!
6115b286d1SAlice Ryhl //! impl WorkItem for MyStruct {
6215b286d1SAlice Ryhl //!     type Pointer = Arc<MyStruct>;
6315b286d1SAlice Ryhl //!
6415b286d1SAlice Ryhl //!     fn run(this: Arc<MyStruct>) {
6515b286d1SAlice Ryhl //!         pr_info!("The value is: {}", this.value);
6615b286d1SAlice Ryhl //!     }
6715b286d1SAlice Ryhl //! }
6815b286d1SAlice Ryhl //!
6915b286d1SAlice Ryhl //! /// This method will enqueue the struct for execution on the system workqueue, where its value
7015b286d1SAlice Ryhl //! /// will be printed.
7115b286d1SAlice Ryhl //! fn print_later(val: Arc<MyStruct>) {
7215b286d1SAlice Ryhl //!     let _ = workqueue::system().enqueue(val);
7315b286d1SAlice Ryhl //! }
7415b286d1SAlice Ryhl //! ```
7515b286d1SAlice Ryhl //!
7615b286d1SAlice Ryhl //! The following example shows how multiple `work_struct` fields can be used:
7715b286d1SAlice Ryhl //!
7815b286d1SAlice Ryhl //! ```
7915b286d1SAlice Ryhl //! use kernel::prelude::*;
8015b286d1SAlice Ryhl //! use kernel::sync::Arc;
8115b286d1SAlice Ryhl //! use kernel::workqueue::{self, Work, WorkItem};
8215b286d1SAlice Ryhl //! use kernel::{impl_has_work, new_work};
8315b286d1SAlice Ryhl //!
8415b286d1SAlice Ryhl //! #[pin_data]
8515b286d1SAlice Ryhl //! struct MyStruct {
8615b286d1SAlice Ryhl //!     value_1: i32,
8715b286d1SAlice Ryhl //!     value_2: i32,
8815b286d1SAlice Ryhl //!     #[pin]
8915b286d1SAlice Ryhl //!     work_1: Work<MyStruct, 1>,
9015b286d1SAlice Ryhl //!     #[pin]
9115b286d1SAlice Ryhl //!     work_2: Work<MyStruct, 2>,
9215b286d1SAlice Ryhl //! }
9315b286d1SAlice Ryhl //!
9415b286d1SAlice Ryhl //! impl_has_work! {
9515b286d1SAlice Ryhl //!     impl HasWork<Self, 1> for MyStruct { self.work_1 }
9615b286d1SAlice Ryhl //!     impl HasWork<Self, 2> for MyStruct { self.work_2 }
9715b286d1SAlice Ryhl //! }
9815b286d1SAlice Ryhl //!
9915b286d1SAlice Ryhl //! impl MyStruct {
10015b286d1SAlice Ryhl //!     fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> {
10115b286d1SAlice Ryhl //!         Arc::pin_init(pin_init!(MyStruct {
10215b286d1SAlice Ryhl //!             value_1,
10315b286d1SAlice Ryhl //!             value_2,
10415b286d1SAlice Ryhl //!             work_1 <- new_work!("MyStruct::work_1"),
10515b286d1SAlice Ryhl //!             work_2 <- new_work!("MyStruct::work_2"),
10615b286d1SAlice Ryhl //!         }))
10715b286d1SAlice Ryhl //!     }
10815b286d1SAlice Ryhl //! }
10915b286d1SAlice Ryhl //!
11015b286d1SAlice Ryhl //! impl WorkItem<1> for MyStruct {
11115b286d1SAlice Ryhl //!     type Pointer = Arc<MyStruct>;
11215b286d1SAlice Ryhl //!
11315b286d1SAlice Ryhl //!     fn run(this: Arc<MyStruct>) {
11415b286d1SAlice Ryhl //!         pr_info!("The value is: {}", this.value_1);
11515b286d1SAlice Ryhl //!     }
11615b286d1SAlice Ryhl //! }
11715b286d1SAlice Ryhl //!
11815b286d1SAlice Ryhl //! impl WorkItem<2> for MyStruct {
11915b286d1SAlice Ryhl //!     type Pointer = Arc<MyStruct>;
12015b286d1SAlice Ryhl //!
12115b286d1SAlice Ryhl //!     fn run(this: Arc<MyStruct>) {
12215b286d1SAlice Ryhl //!         pr_info!("The second value is: {}", this.value_2);
12315b286d1SAlice Ryhl //!     }
12415b286d1SAlice Ryhl //! }
12515b286d1SAlice Ryhl //!
12615b286d1SAlice Ryhl //! fn print_1_later(val: Arc<MyStruct>) {
12715b286d1SAlice Ryhl //!     let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val);
12815b286d1SAlice Ryhl //! }
12915b286d1SAlice Ryhl //!
13015b286d1SAlice Ryhl //! fn print_2_later(val: Arc<MyStruct>) {
13115b286d1SAlice Ryhl //!     let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val);
13215b286d1SAlice Ryhl //! }
13315b286d1SAlice Ryhl //! ```
13415b286d1SAlice Ryhl //!
135*bc2e7d5cSMiguel Ojeda //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h)
136d4d791d4SAlice Ryhl 
13747f0dbe8SAlice Ryhl use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
138115c95e9SAlice Ryhl use alloc::alloc::AllocError;
13947f0dbe8SAlice Ryhl use alloc::boxed::Box;
1407324b889SAlice Ryhl use core::marker::PhantomData;
14147f0dbe8SAlice Ryhl use core::pin::Pin;
1427324b889SAlice Ryhl 
1437324b889SAlice Ryhl /// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
1447324b889SAlice Ryhl #[macro_export]
1457324b889SAlice Ryhl macro_rules! new_work {
1467324b889SAlice Ryhl     ($($name:literal)?) => {
1477324b889SAlice Ryhl         $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
1487324b889SAlice Ryhl     };
1497324b889SAlice Ryhl }
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
171d4d791d4SAlice Ryhl     /// 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| {
202d4d791d4SAlice Ryhl                 bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr)
203d4d791d4SAlice Ryhl             })
204d4d791d4SAlice Ryhl         }
205d4d791d4SAlice Ryhl     }
206115c95e9SAlice Ryhl 
207115c95e9SAlice Ryhl     /// Tries to spawn the given function or closure as a work item.
208115c95e9SAlice Ryhl     ///
209115c95e9SAlice Ryhl     /// This method can fail because it allocates memory to store the work item.
210115c95e9SAlice Ryhl     pub fn try_spawn<T: 'static + Send + FnOnce()>(&self, func: T) -> Result<(), AllocError> {
211115c95e9SAlice Ryhl         let init = pin_init!(ClosureWork {
212115c95e9SAlice Ryhl             work <- new_work!("Queue::try_spawn"),
213115c95e9SAlice Ryhl             func: Some(func),
214115c95e9SAlice Ryhl         });
215115c95e9SAlice Ryhl 
216115c95e9SAlice Ryhl         self.enqueue(Box::pin_init(init).map_err(|_| AllocError)?);
217115c95e9SAlice Ryhl         Ok(())
218115c95e9SAlice Ryhl     }
219115c95e9SAlice Ryhl }
220115c95e9SAlice Ryhl 
221115c95e9SAlice Ryhl /// A helper type used in `try_spawn`.
222115c95e9SAlice Ryhl #[pin_data]
223115c95e9SAlice Ryhl struct ClosureWork<T> {
224115c95e9SAlice Ryhl     #[pin]
225115c95e9SAlice Ryhl     work: Work<ClosureWork<T>>,
226115c95e9SAlice Ryhl     func: Option<T>,
227115c95e9SAlice Ryhl }
228115c95e9SAlice Ryhl 
229115c95e9SAlice Ryhl impl<T> ClosureWork<T> {
230115c95e9SAlice Ryhl     fn project(self: Pin<&mut Self>) -> &mut Option<T> {
231115c95e9SAlice Ryhl         // SAFETY: The `func` field is not structurally pinned.
232115c95e9SAlice Ryhl         unsafe { &mut self.get_unchecked_mut().func }
233115c95e9SAlice Ryhl     }
234115c95e9SAlice Ryhl }
235115c95e9SAlice Ryhl 
236115c95e9SAlice Ryhl impl<T: FnOnce()> WorkItem for ClosureWork<T> {
237115c95e9SAlice Ryhl     type Pointer = Pin<Box<Self>>;
238115c95e9SAlice Ryhl 
239115c95e9SAlice Ryhl     fn run(mut this: Pin<Box<Self>>) {
240115c95e9SAlice Ryhl         if let Some(func) = this.as_mut().project().take() {
241115c95e9SAlice Ryhl             (func)()
242115c95e9SAlice Ryhl         }
243115c95e9SAlice Ryhl     }
244d4d791d4SAlice Ryhl }
245d4d791d4SAlice Ryhl 
246d4d791d4SAlice Ryhl /// A raw work item.
247d4d791d4SAlice Ryhl ///
248d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible.
249d4d791d4SAlice Ryhl ///
250d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple
251d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
252d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The
253d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields
254d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.)
255d4d791d4SAlice Ryhl ///
256d4d791d4SAlice Ryhl /// Note that the id is used only to select the right method to call during compilation. It wont be
257d4d791d4SAlice Ryhl /// part of the final executable.
258d4d791d4SAlice Ryhl ///
259d4d791d4SAlice Ryhl /// # Safety
260d4d791d4SAlice Ryhl ///
261d4d791d4SAlice Ryhl /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by `__enqueue`
262d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for
263d4d791d4SAlice Ryhl /// `__enqueue`.
264d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> {
265d4d791d4SAlice Ryhl     /// The return type of [`Queue::enqueue`].
266d4d791d4SAlice Ryhl     type EnqueueOutput;
267d4d791d4SAlice Ryhl 
268d4d791d4SAlice Ryhl     /// Enqueues this work item on a queue using the provided `queue_work_on` method.
269d4d791d4SAlice Ryhl     ///
270d4d791d4SAlice Ryhl     /// # Guarantees
271d4d791d4SAlice Ryhl     ///
272d4d791d4SAlice Ryhl     /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
273d4d791d4SAlice Ryhl     /// valid `work_struct` for the duration of the call to the closure. If the closure returns
274d4d791d4SAlice Ryhl     /// true, then it is further guaranteed that the pointer remains valid until someone calls the
275d4d791d4SAlice Ryhl     /// function pointer stored in the `work_struct`.
276d4d791d4SAlice Ryhl     ///
277d4d791d4SAlice Ryhl     /// # Safety
278d4d791d4SAlice Ryhl     ///
279d4d791d4SAlice Ryhl     /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
280d4d791d4SAlice Ryhl     ///
281d4d791d4SAlice Ryhl     /// If the work item type is annotated with any lifetimes, then you must not call the function
282d4d791d4SAlice Ryhl     /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
283d4d791d4SAlice Ryhl     ///
284d4d791d4SAlice Ryhl     /// If the work item type is not [`Send`], then the function pointer must be called on the same
285d4d791d4SAlice Ryhl     /// thread as the call to `__enqueue`.
286d4d791d4SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
287d4d791d4SAlice Ryhl     where
288d4d791d4SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool;
289d4d791d4SAlice Ryhl }
29003394130SWedson Almeida Filho 
2917324b889SAlice Ryhl /// Defines the method that should be called directly when a work item is executed.
2927324b889SAlice Ryhl ///
2937324b889SAlice Ryhl /// This trait is implemented by `Pin<Box<T>>` and `Arc<T>`, and is mainly intended to be
2947324b889SAlice Ryhl /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`]
2957324b889SAlice Ryhl /// instead. The `run` method on this trait will usually just perform the appropriate
2967324b889SAlice Ryhl /// `container_of` translation and then call into the `run` method from the [`WorkItem`] trait.
2977324b889SAlice Ryhl ///
2987324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
2997324b889SAlice Ryhl ///
3007324b889SAlice Ryhl /// # Safety
3017324b889SAlice Ryhl ///
3027324b889SAlice Ryhl /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
3037324b889SAlice Ryhl /// method of this trait as the function pointer.
3047324b889SAlice Ryhl ///
3057324b889SAlice Ryhl /// [`__enqueue`]: RawWorkItem::__enqueue
3067324b889SAlice Ryhl /// [`run`]: WorkItemPointer::run
3077324b889SAlice Ryhl pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
3087324b889SAlice Ryhl     /// Run this work item.
3097324b889SAlice Ryhl     ///
3107324b889SAlice Ryhl     /// # Safety
3117324b889SAlice Ryhl     ///
3127324b889SAlice Ryhl     /// The provided `work_struct` pointer must originate from a previous call to `__enqueue` where
3137324b889SAlice Ryhl     /// the `queue_work_on` closure returned true, and the pointer must still be valid.
3147324b889SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
3157324b889SAlice Ryhl }
3167324b889SAlice Ryhl 
3177324b889SAlice Ryhl /// Defines the method that should be called when this work item is executed.
3187324b889SAlice Ryhl ///
3197324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
3207324b889SAlice Ryhl pub trait WorkItem<const ID: u64 = 0> {
3217324b889SAlice Ryhl     /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
3227324b889SAlice Ryhl     /// `Pin<Box<Self>>`.
3237324b889SAlice Ryhl     type Pointer: WorkItemPointer<ID>;
3247324b889SAlice Ryhl 
3257324b889SAlice Ryhl     /// The method that should be called when this work item is executed.
3267324b889SAlice Ryhl     fn run(this: Self::Pointer);
3277324b889SAlice Ryhl }
3287324b889SAlice Ryhl 
3297324b889SAlice Ryhl /// Links for a work item.
3307324b889SAlice Ryhl ///
3317324b889SAlice Ryhl /// This struct contains a function pointer to the `run` function from the [`WorkItemPointer`]
3327324b889SAlice Ryhl /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
3337324b889SAlice Ryhl ///
3347324b889SAlice Ryhl /// Wraps the kernel's C `struct work_struct`.
3357324b889SAlice Ryhl ///
3367324b889SAlice Ryhl /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
3377324b889SAlice Ryhl #[repr(transparent)]
3387324b889SAlice Ryhl pub struct Work<T: ?Sized, const ID: u64 = 0> {
3397324b889SAlice Ryhl     work: Opaque<bindings::work_struct>,
3407324b889SAlice Ryhl     _inner: PhantomData<T>,
3417324b889SAlice Ryhl }
3427324b889SAlice Ryhl 
3437324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
3447324b889SAlice Ryhl //
3457324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
3467324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
3477324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
3487324b889SAlice Ryhl //
3497324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
3507324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
3517324b889SAlice Ryhl 
3527324b889SAlice Ryhl impl<T: ?Sized, const ID: u64> Work<T, ID> {
3537324b889SAlice Ryhl     /// Creates a new instance of [`Work`].
3547324b889SAlice Ryhl     #[inline]
3557324b889SAlice Ryhl     #[allow(clippy::new_ret_no_self)]
3567324b889SAlice Ryhl     pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
3577324b889SAlice Ryhl     where
3587324b889SAlice Ryhl         T: WorkItem<ID>,
3597324b889SAlice Ryhl     {
3607324b889SAlice Ryhl         // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the work
3617324b889SAlice Ryhl         // item function.
3627324b889SAlice Ryhl         unsafe {
3637324b889SAlice Ryhl             kernel::init::pin_init_from_closure(move |slot| {
3647324b889SAlice Ryhl                 let slot = Self::raw_get(slot);
3657324b889SAlice Ryhl                 bindings::init_work_with_key(
3667324b889SAlice Ryhl                     slot,
3677324b889SAlice Ryhl                     Some(T::Pointer::run),
3687324b889SAlice Ryhl                     false,
3697324b889SAlice Ryhl                     name.as_char_ptr(),
3707324b889SAlice Ryhl                     key.as_ptr(),
3717324b889SAlice Ryhl                 );
3727324b889SAlice Ryhl                 Ok(())
3737324b889SAlice Ryhl             })
3747324b889SAlice Ryhl         }
3757324b889SAlice Ryhl     }
3767324b889SAlice Ryhl 
3777324b889SAlice Ryhl     /// Get a pointer to the inner `work_struct`.
3787324b889SAlice Ryhl     ///
3797324b889SAlice Ryhl     /// # Safety
3807324b889SAlice Ryhl     ///
3817324b889SAlice Ryhl     /// The provided pointer must not be dangling and must be properly aligned. (But the memory
3827324b889SAlice Ryhl     /// need not be initialized.)
3837324b889SAlice Ryhl     #[inline]
3847324b889SAlice Ryhl     pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
3857324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is aligned and not dangling.
3867324b889SAlice Ryhl         //
3877324b889SAlice Ryhl         // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
3887324b889SAlice Ryhl         // the compiler does not complain that the `work` field is unused.
3897324b889SAlice Ryhl         unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
3907324b889SAlice Ryhl     }
3917324b889SAlice Ryhl }
3927324b889SAlice Ryhl 
3937324b889SAlice Ryhl /// Declares that a type has a [`Work<T, ID>`] field.
3947324b889SAlice Ryhl ///
3957324b889SAlice Ryhl /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
3967324b889SAlice Ryhl /// like this:
3977324b889SAlice Ryhl ///
3987324b889SAlice Ryhl /// ```no_run
3997324b889SAlice Ryhl /// use kernel::impl_has_work;
4007324b889SAlice Ryhl /// use kernel::prelude::*;
4017324b889SAlice Ryhl /// use kernel::workqueue::Work;
4027324b889SAlice Ryhl ///
4037324b889SAlice Ryhl /// struct MyWorkItem {
4047324b889SAlice Ryhl ///     work_field: Work<MyWorkItem, 1>,
4057324b889SAlice Ryhl /// }
4067324b889SAlice Ryhl ///
4077324b889SAlice Ryhl /// impl_has_work! {
4087324b889SAlice Ryhl ///     impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
4097324b889SAlice Ryhl /// }
4107324b889SAlice Ryhl /// ```
4117324b889SAlice Ryhl ///
4127324b889SAlice Ryhl /// Note that since the `Work` type is annotated with an id, you can have several `work_struct`
4137324b889SAlice Ryhl /// fields by using a different id for each one.
4147324b889SAlice Ryhl ///
4157324b889SAlice Ryhl /// # Safety
4167324b889SAlice Ryhl ///
4177324b889SAlice Ryhl /// The [`OFFSET`] constant must be the offset of a field in Self of type [`Work<T, ID>`]. The methods on
4187324b889SAlice Ryhl /// this trait must have exactly the behavior that the definitions given below have.
4197324b889SAlice Ryhl ///
4207324b889SAlice Ryhl /// [`Work<T, ID>`]: Work
4217324b889SAlice Ryhl /// [`impl_has_work!`]: crate::impl_has_work
4227324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET
4237324b889SAlice Ryhl pub unsafe trait HasWork<T, const ID: u64 = 0> {
4247324b889SAlice Ryhl     /// The offset of the [`Work<T, ID>`] field.
4257324b889SAlice Ryhl     ///
4267324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
4277324b889SAlice Ryhl     const OFFSET: usize;
4287324b889SAlice Ryhl 
4297324b889SAlice Ryhl     /// Returns the offset of the [`Work<T, ID>`] field.
4307324b889SAlice Ryhl     ///
4317324b889SAlice Ryhl     /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not Sized.
4327324b889SAlice Ryhl     ///
4337324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
4347324b889SAlice Ryhl     /// [`OFFSET`]: HasWork::OFFSET
4357324b889SAlice Ryhl     #[inline]
4367324b889SAlice Ryhl     fn get_work_offset(&self) -> usize {
4377324b889SAlice Ryhl         Self::OFFSET
4387324b889SAlice Ryhl     }
4397324b889SAlice Ryhl 
4407324b889SAlice Ryhl     /// Returns a pointer to the [`Work<T, ID>`] field.
4417324b889SAlice Ryhl     ///
4427324b889SAlice Ryhl     /// # Safety
4437324b889SAlice Ryhl     ///
4447324b889SAlice Ryhl     /// The provided pointer must point at a valid struct of type `Self`.
4457324b889SAlice Ryhl     ///
4467324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
4477324b889SAlice Ryhl     #[inline]
4487324b889SAlice Ryhl     unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
4497324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is valid.
4507324b889SAlice Ryhl         unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> }
4517324b889SAlice Ryhl     }
4527324b889SAlice Ryhl 
4537324b889SAlice Ryhl     /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
4547324b889SAlice Ryhl     ///
4557324b889SAlice Ryhl     /// # Safety
4567324b889SAlice Ryhl     ///
4577324b889SAlice Ryhl     /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
4587324b889SAlice Ryhl     ///
4597324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
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::impl_has_work;
4777324b889SAlice Ryhl /// use kernel::sync::Arc;
4787324b889SAlice Ryhl /// use kernel::workqueue::{self, Work};
4797324b889SAlice Ryhl ///
4807324b889SAlice Ryhl /// struct MyStruct {
4817324b889SAlice Ryhl ///     work_field: Work<MyStruct, 17>,
4827324b889SAlice Ryhl /// }
4837324b889SAlice Ryhl ///
4847324b889SAlice Ryhl /// impl_has_work! {
4857324b889SAlice Ryhl ///     impl HasWork<MyStruct, 17> for MyStruct { self.work_field }
4867324b889SAlice Ryhl /// }
4877324b889SAlice Ryhl /// ```
4887324b889SAlice Ryhl ///
4897324b889SAlice Ryhl /// [`HasWork<T, ID>`]: HasWork
4907324b889SAlice Ryhl #[macro_export]
4917324b889SAlice Ryhl macro_rules! impl_has_work {
4927324b889SAlice Ryhl     ($(impl$(<$($implarg:ident),*>)?
4937324b889SAlice Ryhl        HasWork<$work_type:ty $(, $id:tt)?>
4947324b889SAlice Ryhl        for $self:ident $(<$($selfarg:ident),*>)?
4957324b889SAlice Ryhl        { self.$field:ident }
4967324b889SAlice Ryhl     )*) => {$(
4977324b889SAlice Ryhl         // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right
4987324b889SAlice Ryhl         // type.
4997324b889SAlice Ryhl         unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self $(<$($selfarg),*>)? {
5007324b889SAlice Ryhl             const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
5017324b889SAlice Ryhl 
5027324b889SAlice Ryhl             #[inline]
5037324b889SAlice Ryhl             unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
5047324b889SAlice Ryhl                 // SAFETY: The caller promises that the pointer is not dangling.
5057324b889SAlice Ryhl                 unsafe {
5067324b889SAlice Ryhl                     ::core::ptr::addr_of_mut!((*ptr).$field)
5077324b889SAlice Ryhl                 }
5087324b889SAlice Ryhl             }
5097324b889SAlice Ryhl         }
5107324b889SAlice Ryhl     )*};
5117324b889SAlice Ryhl }
5127324b889SAlice Ryhl 
513115c95e9SAlice Ryhl impl_has_work! {
514115c95e9SAlice Ryhl     impl<T> HasWork<Self> for ClosureWork<T> { self.work }
515115c95e9SAlice Ryhl }
516115c95e9SAlice Ryhl 
51747f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>
51847f0dbe8SAlice Ryhl where
51947f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
52047f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
52147f0dbe8SAlice Ryhl {
52247f0dbe8SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
52347f0dbe8SAlice Ryhl         // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
52447f0dbe8SAlice Ryhl         let ptr = ptr as *mut Work<T, ID>;
52547f0dbe8SAlice Ryhl         // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
52647f0dbe8SAlice Ryhl         let ptr = unsafe { T::work_container_of(ptr) };
52747f0dbe8SAlice Ryhl         // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
52847f0dbe8SAlice Ryhl         let arc = unsafe { Arc::from_raw(ptr) };
52947f0dbe8SAlice Ryhl 
53047f0dbe8SAlice Ryhl         T::run(arc)
53147f0dbe8SAlice Ryhl     }
53247f0dbe8SAlice Ryhl }
53347f0dbe8SAlice Ryhl 
53447f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T>
53547f0dbe8SAlice Ryhl where
53647f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
53747f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
53847f0dbe8SAlice Ryhl {
53947f0dbe8SAlice Ryhl     type EnqueueOutput = Result<(), Self>;
54047f0dbe8SAlice Ryhl 
54147f0dbe8SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
54247f0dbe8SAlice Ryhl     where
54347f0dbe8SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool,
54447f0dbe8SAlice Ryhl     {
54547f0dbe8SAlice Ryhl         // Casting between const and mut is not a problem as long as the pointer is a raw pointer.
54647f0dbe8SAlice Ryhl         let ptr = Arc::into_raw(self).cast_mut();
54747f0dbe8SAlice Ryhl 
54847f0dbe8SAlice Ryhl         // SAFETY: Pointers into an `Arc` point at a valid value.
54947f0dbe8SAlice Ryhl         let work_ptr = unsafe { T::raw_get_work(ptr) };
55047f0dbe8SAlice Ryhl         // SAFETY: `raw_get_work` returns a pointer to a valid value.
55147f0dbe8SAlice Ryhl         let work_ptr = unsafe { Work::raw_get(work_ptr) };
55247f0dbe8SAlice Ryhl 
55347f0dbe8SAlice Ryhl         if queue_work_on(work_ptr) {
55447f0dbe8SAlice Ryhl             Ok(())
55547f0dbe8SAlice Ryhl         } else {
55647f0dbe8SAlice Ryhl             // SAFETY: The work queue has not taken ownership of the pointer.
55747f0dbe8SAlice Ryhl             Err(unsafe { Arc::from_raw(ptr) })
55847f0dbe8SAlice Ryhl         }
55947f0dbe8SAlice Ryhl     }
56047f0dbe8SAlice Ryhl }
56147f0dbe8SAlice Ryhl 
56247f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<Box<T>>
56347f0dbe8SAlice Ryhl where
56447f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
56547f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
56647f0dbe8SAlice Ryhl {
56747f0dbe8SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
56847f0dbe8SAlice Ryhl         // SAFETY: The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`.
56947f0dbe8SAlice Ryhl         let ptr = ptr as *mut Work<T, ID>;
57047f0dbe8SAlice Ryhl         // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`.
57147f0dbe8SAlice Ryhl         let ptr = unsafe { T::work_container_of(ptr) };
57247f0dbe8SAlice Ryhl         // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership.
57347f0dbe8SAlice Ryhl         let boxed = unsafe { Box::from_raw(ptr) };
57447f0dbe8SAlice Ryhl         // SAFETY: The box was already pinned when it was enqueued.
57547f0dbe8SAlice Ryhl         let pinned = unsafe { Pin::new_unchecked(boxed) };
57647f0dbe8SAlice Ryhl 
57747f0dbe8SAlice Ryhl         T::run(pinned)
57847f0dbe8SAlice Ryhl     }
57947f0dbe8SAlice Ryhl }
58047f0dbe8SAlice Ryhl 
58147f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<Box<T>>
58247f0dbe8SAlice Ryhl where
58347f0dbe8SAlice Ryhl     T: WorkItem<ID, Pointer = Self>,
58447f0dbe8SAlice Ryhl     T: HasWork<T, ID>,
58547f0dbe8SAlice Ryhl {
58647f0dbe8SAlice Ryhl     type EnqueueOutput = ();
58747f0dbe8SAlice Ryhl 
58847f0dbe8SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
58947f0dbe8SAlice Ryhl     where
59047f0dbe8SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool,
59147f0dbe8SAlice Ryhl     {
59247f0dbe8SAlice Ryhl         // SAFETY: We're not going to move `self` or any of its fields, so its okay to temporarily
59347f0dbe8SAlice Ryhl         // remove the `Pin` wrapper.
59447f0dbe8SAlice Ryhl         let boxed = unsafe { Pin::into_inner_unchecked(self) };
59547f0dbe8SAlice Ryhl         let ptr = Box::into_raw(boxed);
59647f0dbe8SAlice Ryhl 
59747f0dbe8SAlice Ryhl         // SAFETY: Pointers into a `Box` point at a valid value.
59847f0dbe8SAlice Ryhl         let work_ptr = unsafe { T::raw_get_work(ptr) };
59947f0dbe8SAlice Ryhl         // SAFETY: `raw_get_work` returns a pointer to a valid value.
60047f0dbe8SAlice Ryhl         let work_ptr = unsafe { Work::raw_get(work_ptr) };
60147f0dbe8SAlice Ryhl 
60247f0dbe8SAlice Ryhl         if !queue_work_on(work_ptr) {
60347f0dbe8SAlice Ryhl             // SAFETY: This method requires exclusive ownership of the box, so it cannot be in a
60447f0dbe8SAlice Ryhl             // workqueue.
60547f0dbe8SAlice Ryhl             unsafe { ::core::hint::unreachable_unchecked() }
60647f0dbe8SAlice Ryhl         }
60747f0dbe8SAlice Ryhl     }
60847f0dbe8SAlice Ryhl }
60947f0dbe8SAlice Ryhl 
61003394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`).
61103394130SWedson Almeida Filho ///
61203394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
61303394130SWedson Almeida Filho /// users which expect relatively short queue flush time.
61403394130SWedson Almeida Filho ///
61503394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long.
61603394130SWedson Almeida Filho pub fn system() -> &'static Queue {
61703394130SWedson Almeida Filho     // SAFETY: `system_wq` is a C global, always available.
61803394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_wq) }
61903394130SWedson Almeida Filho }
62003394130SWedson Almeida Filho 
62103394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`).
62203394130SWedson Almeida Filho ///
62303394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher
62403394130SWedson Almeida Filho /// scheduling priority.
62503394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue {
62603394130SWedson Almeida Filho     // SAFETY: `system_highpri_wq` is a C global, always available.
62703394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_highpri_wq) }
62803394130SWedson Almeida Filho }
62903394130SWedson Almeida Filho 
63003394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`).
63103394130SWedson Almeida Filho ///
63203394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue
63303394130SWedson Almeida Filho /// flushing might take relatively long.
63403394130SWedson Almeida Filho pub fn system_long() -> &'static Queue {
63503394130SWedson Almeida Filho     // SAFETY: `system_long_wq` is a C global, always available.
63603394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_long_wq) }
63703394130SWedson Almeida Filho }
63803394130SWedson Almeida Filho 
63903394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`).
64003394130SWedson Almeida Filho ///
64103394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
64203394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are
64303394130SWedson Almeida Filho /// available.
64403394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue {
64503394130SWedson Almeida Filho     // SAFETY: `system_unbound_wq` is a C global, always available.
64603394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_unbound_wq) }
64703394130SWedson Almeida Filho }
64803394130SWedson Almeida Filho 
64903394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`).
65003394130SWedson Almeida Filho ///
65103394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable.
65203394130SWedson Almeida Filho ///
65303394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
65403394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
65503394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue {
65603394130SWedson Almeida Filho     // SAFETY: `system_freezable_wq` is a C global, always available.
65703394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_wq) }
65803394130SWedson Almeida Filho }
65903394130SWedson Almeida Filho 
66003394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`).
66103394130SWedson Almeida Filho ///
66203394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the
66303394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
66403394130SWedson Almeida Filho /// returned by [`system`].
66503394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue {
66603394130SWedson Almeida Filho     // SAFETY: `system_power_efficient_wq` is a C global, always available.
66703394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
66803394130SWedson Almeida Filho }
66903394130SWedson Almeida Filho 
67003394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`).
67103394130SWedson Almeida Filho ///
67203394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable.
67303394130SWedson Almeida Filho ///
67403394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
67503394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
67603394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue {
67703394130SWedson Almeida Filho     // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available.
67803394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
67903394130SWedson Almeida Filho }
680