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::sync::Arc; 37e283ee23SAlice Ryhl //! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem}; 3815b286d1SAlice Ryhl //! 3915b286d1SAlice Ryhl //! #[pin_data] 4015b286d1SAlice Ryhl //! struct MyStruct { 4115b286d1SAlice Ryhl //! value: i32, 4215b286d1SAlice Ryhl //! #[pin] 4315b286d1SAlice Ryhl //! work: Work<MyStruct>, 4415b286d1SAlice Ryhl //! } 4515b286d1SAlice Ryhl //! 4615b286d1SAlice Ryhl //! impl_has_work! { 4715b286d1SAlice Ryhl //! impl HasWork<Self> for MyStruct { self.work } 4815b286d1SAlice Ryhl //! } 4915b286d1SAlice Ryhl //! 5015b286d1SAlice Ryhl //! impl MyStruct { 5115b286d1SAlice Ryhl //! fn new(value: i32) -> Result<Arc<Self>> { 5215b286d1SAlice Ryhl //! Arc::pin_init(pin_init!(MyStruct { 5315b286d1SAlice Ryhl //! value, 5415b286d1SAlice Ryhl //! work <- new_work!("MyStruct::work"), 55c34aa00dSWedson Almeida Filho //! }), GFP_KERNEL) 5615b286d1SAlice Ryhl //! } 5715b286d1SAlice Ryhl //! } 5815b286d1SAlice Ryhl //! 5915b286d1SAlice Ryhl //! impl WorkItem for MyStruct { 6015b286d1SAlice Ryhl //! type Pointer = Arc<MyStruct>; 6115b286d1SAlice Ryhl //! 6215b286d1SAlice Ryhl //! fn run(this: Arc<MyStruct>) { 6315b286d1SAlice Ryhl //! pr_info!("The value is: {}", this.value); 6415b286d1SAlice Ryhl //! } 6515b286d1SAlice Ryhl //! } 6615b286d1SAlice Ryhl //! 6715b286d1SAlice Ryhl //! /// This method will enqueue the struct for execution on the system workqueue, where its value 6815b286d1SAlice Ryhl //! /// will be printed. 6915b286d1SAlice Ryhl //! fn print_later(val: Arc<MyStruct>) { 7015b286d1SAlice Ryhl //! let _ = workqueue::system().enqueue(val); 7115b286d1SAlice Ryhl //! } 7215b286d1SAlice Ryhl //! ``` 7315b286d1SAlice Ryhl //! 7415b286d1SAlice Ryhl //! The following example shows how multiple `work_struct` fields can be used: 7515b286d1SAlice Ryhl //! 7615b286d1SAlice Ryhl //! ``` 7715b286d1SAlice Ryhl //! use kernel::sync::Arc; 78e283ee23SAlice Ryhl //! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem}; 7915b286d1SAlice Ryhl //! 8015b286d1SAlice Ryhl //! #[pin_data] 8115b286d1SAlice Ryhl //! struct MyStruct { 8215b286d1SAlice Ryhl //! value_1: i32, 8315b286d1SAlice Ryhl //! value_2: i32, 8415b286d1SAlice Ryhl //! #[pin] 8515b286d1SAlice Ryhl //! work_1: Work<MyStruct, 1>, 8615b286d1SAlice Ryhl //! #[pin] 8715b286d1SAlice Ryhl //! work_2: Work<MyStruct, 2>, 8815b286d1SAlice Ryhl //! } 8915b286d1SAlice Ryhl //! 9015b286d1SAlice Ryhl //! impl_has_work! { 9115b286d1SAlice Ryhl //! impl HasWork<Self, 1> for MyStruct { self.work_1 } 9215b286d1SAlice Ryhl //! impl HasWork<Self, 2> for MyStruct { self.work_2 } 9315b286d1SAlice Ryhl //! } 9415b286d1SAlice Ryhl //! 9515b286d1SAlice Ryhl //! impl MyStruct { 9615b286d1SAlice Ryhl //! fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> { 9715b286d1SAlice Ryhl //! Arc::pin_init(pin_init!(MyStruct { 9815b286d1SAlice Ryhl //! value_1, 9915b286d1SAlice Ryhl //! value_2, 10015b286d1SAlice Ryhl //! work_1 <- new_work!("MyStruct::work_1"), 10115b286d1SAlice Ryhl //! work_2 <- new_work!("MyStruct::work_2"), 102c34aa00dSWedson Almeida Filho //! }), GFP_KERNEL) 10315b286d1SAlice Ryhl //! } 10415b286d1SAlice Ryhl //! } 10515b286d1SAlice Ryhl //! 10615b286d1SAlice Ryhl //! impl WorkItem<1> for MyStruct { 10715b286d1SAlice Ryhl //! type Pointer = Arc<MyStruct>; 10815b286d1SAlice Ryhl //! 10915b286d1SAlice Ryhl //! fn run(this: Arc<MyStruct>) { 11015b286d1SAlice Ryhl //! pr_info!("The value is: {}", this.value_1); 11115b286d1SAlice Ryhl //! } 11215b286d1SAlice Ryhl //! } 11315b286d1SAlice Ryhl //! 11415b286d1SAlice Ryhl //! impl WorkItem<2> for MyStruct { 11515b286d1SAlice Ryhl //! type Pointer = Arc<MyStruct>; 11615b286d1SAlice Ryhl //! 11715b286d1SAlice Ryhl //! fn run(this: Arc<MyStruct>) { 11815b286d1SAlice Ryhl //! pr_info!("The second value is: {}", this.value_2); 11915b286d1SAlice Ryhl //! } 12015b286d1SAlice Ryhl //! } 12115b286d1SAlice Ryhl //! 12215b286d1SAlice Ryhl //! fn print_1_later(val: Arc<MyStruct>) { 12315b286d1SAlice Ryhl //! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val); 12415b286d1SAlice Ryhl //! } 12515b286d1SAlice Ryhl //! 12615b286d1SAlice Ryhl //! fn print_2_later(val: Arc<MyStruct>) { 12715b286d1SAlice Ryhl //! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val); 12815b286d1SAlice Ryhl //! } 12915b286d1SAlice Ryhl //! ``` 13015b286d1SAlice Ryhl //! 131bc2e7d5cSMiguel Ojeda //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) 132d4d791d4SAlice Ryhl 1332c109285SWedson Almeida Filho use crate::alloc::{AllocError, Flags}; 13400280272SMiguel Ojeda use crate::{prelude::*, sync::Arc, sync::LockClassKey, types::Opaque}; 1357324b889SAlice Ryhl use core::marker::PhantomData; 1367324b889SAlice Ryhl 1377324b889SAlice Ryhl /// Creates a [`Work`] initialiser with the given name and a newly-created lock class. 1387324b889SAlice Ryhl #[macro_export] 1397324b889SAlice Ryhl macro_rules! new_work { 1407324b889SAlice Ryhl ($($name:literal)?) => { 1417324b889SAlice Ryhl $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!()) 1427324b889SAlice Ryhl }; 1437324b889SAlice Ryhl } 144e283ee23SAlice Ryhl pub use new_work; 145d4d791d4SAlice Ryhl 146d4d791d4SAlice Ryhl /// A kernel work queue. 147d4d791d4SAlice Ryhl /// 148d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`. 149d4d791d4SAlice Ryhl /// 150d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are 151d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc. 152d4d791d4SAlice Ryhl #[repr(transparent)] 153d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>); 154d4d791d4SAlice Ryhl 155d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe. 156d4d791d4SAlice Ryhl unsafe impl Send for Queue {} 157d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe. 158d4d791d4SAlice Ryhl unsafe impl Sync for Queue {} 159d4d791d4SAlice Ryhl 160d4d791d4SAlice Ryhl impl Queue { 161d4d791d4SAlice Ryhl /// Use the provided `struct workqueue_struct` with Rust. 162d4d791d4SAlice Ryhl /// 163d4d791d4SAlice Ryhl /// # Safety 164d4d791d4SAlice Ryhl /// 165d4d791d4SAlice Ryhl /// The caller must ensure that the provided raw pointer is not dangling, that it points at a 166af8b18d7SValentin Obst /// valid workqueue, and that it remains valid until the end of `'a`. 167d4d791d4SAlice Ryhl pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue { 168d4d791d4SAlice Ryhl // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The 169d4d791d4SAlice Ryhl // caller promises that the pointer is not dangling. 170d4d791d4SAlice Ryhl unsafe { &*(ptr as *const Queue) } 171d4d791d4SAlice Ryhl } 172d4d791d4SAlice Ryhl 173d4d791d4SAlice Ryhl /// Enqueues a work item. 174d4d791d4SAlice Ryhl /// 175d4d791d4SAlice Ryhl /// This may fail if the work item is already enqueued in a workqueue. 176d4d791d4SAlice Ryhl /// 177d4d791d4SAlice Ryhl /// The work item will be submitted using `WORK_CPU_UNBOUND`. 178d4d791d4SAlice Ryhl pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput 179d4d791d4SAlice Ryhl where 180d4d791d4SAlice Ryhl W: RawWorkItem<ID> + Send + 'static, 181d4d791d4SAlice Ryhl { 182d4d791d4SAlice Ryhl let queue_ptr = self.0.get(); 183d4d791d4SAlice Ryhl 184d4d791d4SAlice Ryhl // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other 185d4d791d4SAlice Ryhl // `__enqueue` requirements are not relevant since `W` is `Send` and static. 186d4d791d4SAlice Ryhl // 187d4d791d4SAlice Ryhl // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which 188d4d791d4SAlice Ryhl // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this 189d4d791d4SAlice Ryhl // closure. 190d4d791d4SAlice Ryhl // 191d4d791d4SAlice Ryhl // Furthermore, if the C workqueue code accesses the pointer after this call to 192d4d791d4SAlice Ryhl // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on` 193d4d791d4SAlice Ryhl // will have returned true. In this case, `__enqueue` promises that the raw pointer will 194d4d791d4SAlice Ryhl // stay valid until we call the function pointer in the `work_struct`, so the access is ok. 195d4d791d4SAlice Ryhl unsafe { 196d4d791d4SAlice Ryhl w.__enqueue(move |work_ptr| { 1973e0bc285SMiguel Ojeda bindings::queue_work_on( 1983e0bc285SMiguel Ojeda bindings::wq_misc_consts_WORK_CPU_UNBOUND as _, 1993e0bc285SMiguel Ojeda queue_ptr, 2003e0bc285SMiguel Ojeda work_ptr, 2013e0bc285SMiguel Ojeda ) 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. 209c34aa00dSWedson Almeida Filho pub fn try_spawn<T: 'static + Send + FnOnce()>( 210c34aa00dSWedson Almeida Filho &self, 211c34aa00dSWedson Almeida Filho flags: Flags, 212c34aa00dSWedson Almeida Filho func: T, 213c34aa00dSWedson Almeida Filho ) -> Result<(), AllocError> { 214115c95e9SAlice Ryhl let init = pin_init!(ClosureWork { 215115c95e9SAlice Ryhl work <- new_work!("Queue::try_spawn"), 216115c95e9SAlice Ryhl func: Some(func), 217115c95e9SAlice Ryhl }); 218115c95e9SAlice Ryhl 219c34aa00dSWedson Almeida Filho self.enqueue(Box::pin_init(init, flags).map_err(|_| AllocError)?); 220115c95e9SAlice Ryhl Ok(()) 221115c95e9SAlice Ryhl } 222115c95e9SAlice Ryhl } 223115c95e9SAlice Ryhl 2244c799d1dSValentin Obst /// A helper type used in [`try_spawn`]. 2254c799d1dSValentin Obst /// 2264c799d1dSValentin Obst /// [`try_spawn`]: Queue::try_spawn 227115c95e9SAlice Ryhl #[pin_data] 228115c95e9SAlice Ryhl struct ClosureWork<T> { 229115c95e9SAlice Ryhl #[pin] 230115c95e9SAlice Ryhl work: Work<ClosureWork<T>>, 231115c95e9SAlice Ryhl func: Option<T>, 232115c95e9SAlice Ryhl } 233115c95e9SAlice Ryhl 234115c95e9SAlice Ryhl impl<T> ClosureWork<T> { 235115c95e9SAlice Ryhl fn project(self: Pin<&mut Self>) -> &mut Option<T> { 236115c95e9SAlice Ryhl // SAFETY: The `func` field is not structurally pinned. 237115c95e9SAlice Ryhl unsafe { &mut self.get_unchecked_mut().func } 238115c95e9SAlice Ryhl } 239115c95e9SAlice Ryhl } 240115c95e9SAlice Ryhl 241115c95e9SAlice Ryhl impl<T: FnOnce()> WorkItem for ClosureWork<T> { 242115c95e9SAlice Ryhl type Pointer = Pin<Box<Self>>; 243115c95e9SAlice Ryhl 244115c95e9SAlice Ryhl fn run(mut this: Pin<Box<Self>>) { 245115c95e9SAlice Ryhl if let Some(func) = this.as_mut().project().take() { 246115c95e9SAlice Ryhl (func)() 247115c95e9SAlice Ryhl } 248115c95e9SAlice Ryhl } 249d4d791d4SAlice Ryhl } 250d4d791d4SAlice Ryhl 251d4d791d4SAlice Ryhl /// A raw work item. 252d4d791d4SAlice Ryhl /// 253d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible. 254d4d791d4SAlice Ryhl /// 255d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple 256d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then 257d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The 258d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields 259d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.) 260d4d791d4SAlice Ryhl /// 261b6cda913SValentin Obst /// Note that the id is used only to select the right method to call during compilation. It won't be 262d4d791d4SAlice Ryhl /// part of the final executable. 263d4d791d4SAlice Ryhl /// 264d4d791d4SAlice Ryhl /// # Safety 265d4d791d4SAlice Ryhl /// 2664c799d1dSValentin Obst /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`] 267d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for 2684c799d1dSValentin Obst /// [`__enqueue`]. 2694c799d1dSValentin Obst /// 2704c799d1dSValentin Obst /// [`__enqueue`]: RawWorkItem::__enqueue 271d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> { 272d4d791d4SAlice Ryhl /// The return type of [`Queue::enqueue`]. 273d4d791d4SAlice Ryhl type EnqueueOutput; 274d4d791d4SAlice Ryhl 275d4d791d4SAlice Ryhl /// Enqueues this work item on a queue using the provided `queue_work_on` method. 276d4d791d4SAlice Ryhl /// 277d4d791d4SAlice Ryhl /// # Guarantees 278d4d791d4SAlice Ryhl /// 279d4d791d4SAlice Ryhl /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a 280d4d791d4SAlice Ryhl /// valid `work_struct` for the duration of the call to the closure. If the closure returns 281d4d791d4SAlice Ryhl /// true, then it is further guaranteed that the pointer remains valid until someone calls the 282d4d791d4SAlice Ryhl /// function pointer stored in the `work_struct`. 283d4d791d4SAlice Ryhl /// 284d4d791d4SAlice Ryhl /// # Safety 285d4d791d4SAlice Ryhl /// 286d4d791d4SAlice Ryhl /// The provided closure may only return `false` if the `work_struct` is already in a workqueue. 287d4d791d4SAlice Ryhl /// 288d4d791d4SAlice Ryhl /// If the work item type is annotated with any lifetimes, then you must not call the function 289d4d791d4SAlice Ryhl /// pointer after any such lifetime expires. (Never calling the function pointer is okay.) 290d4d791d4SAlice Ryhl /// 291d4d791d4SAlice Ryhl /// If the work item type is not [`Send`], then the function pointer must be called on the same 292d4d791d4SAlice Ryhl /// thread as the call to `__enqueue`. 293d4d791d4SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 294d4d791d4SAlice Ryhl where 295d4d791d4SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool; 296d4d791d4SAlice Ryhl } 29703394130SWedson Almeida Filho 2987324b889SAlice Ryhl /// Defines the method that should be called directly when a work item is executed. 2997324b889SAlice Ryhl /// 3004c799d1dSValentin Obst /// This trait is implemented by `Pin<Box<T>>` and [`Arc<T>`], and is mainly intended to be 3017324b889SAlice Ryhl /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`] 3024c799d1dSValentin Obst /// instead. The [`run`] method on this trait will usually just perform the appropriate 3034c799d1dSValentin Obst /// `container_of` translation and then call into the [`run`][WorkItem::run] method from the 3044c799d1dSValentin Obst /// [`WorkItem`] trait. 3057324b889SAlice Ryhl /// 3067324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper. 3077324b889SAlice Ryhl /// 3087324b889SAlice Ryhl /// # Safety 3097324b889SAlice Ryhl /// 3107324b889SAlice Ryhl /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`] 3117324b889SAlice Ryhl /// method of this trait as the function pointer. 3127324b889SAlice Ryhl /// 3137324b889SAlice Ryhl /// [`__enqueue`]: RawWorkItem::__enqueue 3147324b889SAlice Ryhl /// [`run`]: WorkItemPointer::run 3157324b889SAlice Ryhl pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> { 3167324b889SAlice Ryhl /// Run this work item. 3177324b889SAlice Ryhl /// 3187324b889SAlice Ryhl /// # Safety 3197324b889SAlice Ryhl /// 3204c799d1dSValentin Obst /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`] 3214c799d1dSValentin Obst /// where the `queue_work_on` closure returned true, and the pointer must still be valid. 3224c799d1dSValentin Obst /// 3234c799d1dSValentin Obst /// [`__enqueue`]: RawWorkItem::__enqueue 3247324b889SAlice Ryhl unsafe extern "C" fn run(ptr: *mut bindings::work_struct); 3257324b889SAlice Ryhl } 3267324b889SAlice Ryhl 3277324b889SAlice Ryhl /// Defines the method that should be called when this work item is executed. 3287324b889SAlice Ryhl /// 3297324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper. 3307324b889SAlice Ryhl pub trait WorkItem<const ID: u64 = 0> { 3317324b889SAlice Ryhl /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or 3327324b889SAlice Ryhl /// `Pin<Box<Self>>`. 3337324b889SAlice Ryhl type Pointer: WorkItemPointer<ID>; 3347324b889SAlice Ryhl 3357324b889SAlice Ryhl /// The method that should be called when this work item is executed. 3367324b889SAlice Ryhl fn run(this: Self::Pointer); 3377324b889SAlice Ryhl } 3387324b889SAlice Ryhl 3397324b889SAlice Ryhl /// Links for a work item. 3407324b889SAlice Ryhl /// 3414c799d1dSValentin Obst /// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`] 3427324b889SAlice Ryhl /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue. 3437324b889SAlice Ryhl /// 3447324b889SAlice Ryhl /// Wraps the kernel's C `struct work_struct`. 3457324b889SAlice Ryhl /// 3467324b889SAlice Ryhl /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it. 3474c799d1dSValentin Obst /// 3484c799d1dSValentin Obst /// [`run`]: WorkItemPointer::run 3498db31d3fSBenno Lossin #[pin_data] 3507324b889SAlice Ryhl #[repr(transparent)] 3517324b889SAlice Ryhl pub struct Work<T: ?Sized, const ID: u64 = 0> { 3528db31d3fSBenno Lossin #[pin] 3537324b889SAlice Ryhl work: Opaque<bindings::work_struct>, 3547324b889SAlice Ryhl _inner: PhantomData<T>, 3557324b889SAlice Ryhl } 3567324b889SAlice Ryhl 3577324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread. 3587324b889SAlice Ryhl // 3597324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`. 3607324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {} 3617324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread. 3627324b889SAlice Ryhl // 3637324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`. 3647324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {} 3657324b889SAlice Ryhl 3667324b889SAlice Ryhl impl<T: ?Sized, const ID: u64> Work<T, ID> { 3677324b889SAlice Ryhl /// Creates a new instance of [`Work`]. 3687324b889SAlice Ryhl #[inline] 3697324b889SAlice Ryhl pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> 3707324b889SAlice Ryhl where 3717324b889SAlice Ryhl T: WorkItem<ID>, 3727324b889SAlice Ryhl { 3738db31d3fSBenno Lossin pin_init!(Self { 3748db31d3fSBenno Lossin work <- Opaque::ffi_init(|slot| { 3758db31d3fSBenno Lossin // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as 3768db31d3fSBenno Lossin // the work item function. 3777324b889SAlice Ryhl unsafe { 3787324b889SAlice Ryhl bindings::init_work_with_key( 3797324b889SAlice Ryhl slot, 3807324b889SAlice Ryhl Some(T::Pointer::run), 3817324b889SAlice Ryhl false, 3827324b889SAlice Ryhl name.as_char_ptr(), 3837324b889SAlice Ryhl key.as_ptr(), 3848db31d3fSBenno Lossin ) 3857324b889SAlice Ryhl } 3868db31d3fSBenno Lossin }), 3878db31d3fSBenno Lossin _inner: PhantomData, 3888db31d3fSBenno Lossin }) 3897324b889SAlice Ryhl } 3907324b889SAlice Ryhl 3917324b889SAlice Ryhl /// Get a pointer to the inner `work_struct`. 3927324b889SAlice Ryhl /// 3937324b889SAlice Ryhl /// # Safety 3947324b889SAlice Ryhl /// 3957324b889SAlice Ryhl /// The provided pointer must not be dangling and must be properly aligned. (But the memory 3967324b889SAlice Ryhl /// need not be initialized.) 3977324b889SAlice Ryhl #[inline] 3987324b889SAlice Ryhl pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct { 3997324b889SAlice Ryhl // SAFETY: The caller promises that the pointer is aligned and not dangling. 4007324b889SAlice Ryhl // 4017324b889SAlice Ryhl // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that 4027324b889SAlice Ryhl // the compiler does not complain that the `work` field is unused. 4037324b889SAlice Ryhl unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) } 4047324b889SAlice Ryhl } 4057324b889SAlice Ryhl } 4067324b889SAlice Ryhl 4077324b889SAlice Ryhl /// Declares that a type has a [`Work<T, ID>`] field. 4087324b889SAlice Ryhl /// 4097324b889SAlice Ryhl /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro 4107324b889SAlice Ryhl /// like this: 4117324b889SAlice Ryhl /// 4127324b889SAlice Ryhl /// ```no_run 413e283ee23SAlice Ryhl /// use kernel::workqueue::{impl_has_work, Work}; 4147324b889SAlice Ryhl /// 4157324b889SAlice Ryhl /// struct MyWorkItem { 4167324b889SAlice Ryhl /// work_field: Work<MyWorkItem, 1>, 4177324b889SAlice Ryhl /// } 4187324b889SAlice Ryhl /// 4197324b889SAlice Ryhl /// impl_has_work! { 4207324b889SAlice Ryhl /// impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field } 4217324b889SAlice Ryhl /// } 4227324b889SAlice Ryhl /// ``` 4237324b889SAlice Ryhl /// 4244c799d1dSValentin Obst /// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct` 4257324b889SAlice Ryhl /// fields by using a different id for each one. 4267324b889SAlice Ryhl /// 4277324b889SAlice Ryhl /// # Safety 4287324b889SAlice Ryhl /// 429af8b18d7SValentin Obst /// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The 430af8b18d7SValentin Obst /// methods on this trait must have exactly the behavior that the definitions given below have. 4317324b889SAlice Ryhl /// 4327324b889SAlice Ryhl /// [`impl_has_work!`]: crate::impl_has_work 4337324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET 4347324b889SAlice Ryhl pub unsafe trait HasWork<T, const ID: u64 = 0> { 4357324b889SAlice Ryhl /// The offset of the [`Work<T, ID>`] field. 4367324b889SAlice Ryhl const OFFSET: usize; 4377324b889SAlice Ryhl 4387324b889SAlice Ryhl /// Returns the offset of the [`Work<T, ID>`] field. 4397324b889SAlice Ryhl /// 440af8b18d7SValentin Obst /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not 4414c799d1dSValentin Obst /// [`Sized`]. 4427324b889SAlice Ryhl /// 4437324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET 4447324b889SAlice Ryhl #[inline] 4457324b889SAlice Ryhl fn get_work_offset(&self) -> usize { 4467324b889SAlice Ryhl Self::OFFSET 4477324b889SAlice Ryhl } 4487324b889SAlice Ryhl 4497324b889SAlice Ryhl /// Returns a pointer to the [`Work<T, ID>`] field. 4507324b889SAlice Ryhl /// 4517324b889SAlice Ryhl /// # Safety 4527324b889SAlice Ryhl /// 4537324b889SAlice Ryhl /// The provided pointer must point at a valid struct of type `Self`. 4547324b889SAlice Ryhl #[inline] 4557324b889SAlice Ryhl unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> { 4567324b889SAlice Ryhl // SAFETY: The caller promises that the pointer is valid. 4577324b889SAlice Ryhl unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> } 4587324b889SAlice Ryhl } 4597324b889SAlice Ryhl 4607324b889SAlice Ryhl /// Returns a pointer to the struct containing the [`Work<T, ID>`] field. 4617324b889SAlice Ryhl /// 4627324b889SAlice Ryhl /// # Safety 4637324b889SAlice Ryhl /// 4647324b889SAlice Ryhl /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`. 4657324b889SAlice Ryhl #[inline] 4667324b889SAlice Ryhl unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self 4677324b889SAlice Ryhl where 4687324b889SAlice Ryhl Self: Sized, 4697324b889SAlice Ryhl { 4707324b889SAlice Ryhl // SAFETY: The caller promises that the pointer points at a field of the right type in the 4717324b889SAlice Ryhl // right kind of struct. 4727324b889SAlice Ryhl unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self } 4737324b889SAlice Ryhl } 4747324b889SAlice Ryhl } 4757324b889SAlice Ryhl 4767324b889SAlice Ryhl /// Used to safely implement the [`HasWork<T, ID>`] trait. 4777324b889SAlice Ryhl /// 4787324b889SAlice Ryhl /// # Examples 4797324b889SAlice Ryhl /// 4807324b889SAlice Ryhl /// ``` 4817324b889SAlice Ryhl /// use kernel::sync::Arc; 482e283ee23SAlice Ryhl /// use kernel::workqueue::{self, impl_has_work, Work}; 4837324b889SAlice Ryhl /// 484fe7d9d80SRoland Xu /// struct MyStruct<'a, T, const N: usize> { 485fe7d9d80SRoland Xu /// work_field: Work<MyStruct<'a, T, N>, 17>, 486fe7d9d80SRoland Xu /// f: fn(&'a [T; N]), 4877324b889SAlice Ryhl /// } 4887324b889SAlice Ryhl /// 4897324b889SAlice Ryhl /// impl_has_work! { 490fe7d9d80SRoland Xu /// impl{'a, T, const N: usize} HasWork<MyStruct<'a, T, N>, 17> 491fe7d9d80SRoland Xu /// for MyStruct<'a, T, N> { self.work_field } 4927324b889SAlice Ryhl /// } 4937324b889SAlice Ryhl /// ``` 4947324b889SAlice Ryhl #[macro_export] 4957324b889SAlice Ryhl macro_rules! impl_has_work { 496fe7d9d80SRoland Xu ($(impl$({$($generics:tt)*})? 4977324b889SAlice Ryhl HasWork<$work_type:ty $(, $id:tt)?> 498fe7d9d80SRoland Xu for $self:ty 4997324b889SAlice Ryhl { self.$field:ident } 5007324b889SAlice Ryhl )*) => {$( 5017324b889SAlice Ryhl // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right 5027324b889SAlice Ryhl // type. 503fe7d9d80SRoland Xu unsafe impl$(<$($generics)+>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self { 5047324b889SAlice Ryhl const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; 5057324b889SAlice Ryhl 5067324b889SAlice Ryhl #[inline] 5077324b889SAlice Ryhl unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> { 5087324b889SAlice Ryhl // SAFETY: The caller promises that the pointer is not dangling. 5097324b889SAlice Ryhl unsafe { 5107324b889SAlice Ryhl ::core::ptr::addr_of_mut!((*ptr).$field) 5117324b889SAlice Ryhl } 5127324b889SAlice Ryhl } 5137324b889SAlice Ryhl } 5147324b889SAlice Ryhl )*}; 5157324b889SAlice Ryhl } 516e283ee23SAlice Ryhl pub use impl_has_work; 5177324b889SAlice Ryhl 518115c95e9SAlice Ryhl impl_has_work! { 519fe7d9d80SRoland Xu impl{T} HasWork<Self> for ClosureWork<T> { self.work } 520115c95e9SAlice Ryhl } 521115c95e9SAlice Ryhl 522db4f72c9SMiguel Ojeda // SAFETY: TODO. 52347f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T> 52447f0dbe8SAlice Ryhl where 52547f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 52647f0dbe8SAlice Ryhl T: HasWork<T, ID>, 52747f0dbe8SAlice Ryhl { 52847f0dbe8SAlice Ryhl unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { 529*c28bfe76SMiguel Ojeda // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`. 53047f0dbe8SAlice Ryhl let ptr = ptr as *mut Work<T, ID>; 53147f0dbe8SAlice Ryhl // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`. 53247f0dbe8SAlice Ryhl let ptr = unsafe { T::work_container_of(ptr) }; 53347f0dbe8SAlice Ryhl // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership. 53447f0dbe8SAlice Ryhl let arc = unsafe { Arc::from_raw(ptr) }; 53547f0dbe8SAlice Ryhl 53647f0dbe8SAlice Ryhl T::run(arc) 53747f0dbe8SAlice Ryhl } 53847f0dbe8SAlice Ryhl } 53947f0dbe8SAlice Ryhl 540db4f72c9SMiguel Ojeda // SAFETY: TODO. 54147f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T> 54247f0dbe8SAlice Ryhl where 54347f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 54447f0dbe8SAlice Ryhl T: HasWork<T, ID>, 54547f0dbe8SAlice Ryhl { 54647f0dbe8SAlice Ryhl type EnqueueOutput = Result<(), Self>; 54747f0dbe8SAlice Ryhl 54847f0dbe8SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 54947f0dbe8SAlice Ryhl where 55047f0dbe8SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool, 55147f0dbe8SAlice Ryhl { 55247f0dbe8SAlice Ryhl // Casting between const and mut is not a problem as long as the pointer is a raw pointer. 55347f0dbe8SAlice Ryhl let ptr = Arc::into_raw(self).cast_mut(); 55447f0dbe8SAlice Ryhl 55547f0dbe8SAlice Ryhl // SAFETY: Pointers into an `Arc` point at a valid value. 55647f0dbe8SAlice Ryhl let work_ptr = unsafe { T::raw_get_work(ptr) }; 55747f0dbe8SAlice Ryhl // SAFETY: `raw_get_work` returns a pointer to a valid value. 55847f0dbe8SAlice Ryhl let work_ptr = unsafe { Work::raw_get(work_ptr) }; 55947f0dbe8SAlice Ryhl 56047f0dbe8SAlice Ryhl if queue_work_on(work_ptr) { 56147f0dbe8SAlice Ryhl Ok(()) 56247f0dbe8SAlice Ryhl } else { 56347f0dbe8SAlice Ryhl // SAFETY: The work queue has not taken ownership of the pointer. 56447f0dbe8SAlice Ryhl Err(unsafe { Arc::from_raw(ptr) }) 56547f0dbe8SAlice Ryhl } 56647f0dbe8SAlice Ryhl } 56747f0dbe8SAlice Ryhl } 56847f0dbe8SAlice Ryhl 569db4f72c9SMiguel Ojeda // SAFETY: TODO. 57047f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<Box<T>> 57147f0dbe8SAlice Ryhl where 57247f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 57347f0dbe8SAlice Ryhl T: HasWork<T, ID>, 57447f0dbe8SAlice Ryhl { 57547f0dbe8SAlice Ryhl unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { 576*c28bfe76SMiguel Ojeda // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`. 57747f0dbe8SAlice Ryhl let ptr = ptr as *mut Work<T, ID>; 57847f0dbe8SAlice Ryhl // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`. 57947f0dbe8SAlice Ryhl let ptr = unsafe { T::work_container_of(ptr) }; 58047f0dbe8SAlice Ryhl // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership. 58147f0dbe8SAlice Ryhl let boxed = unsafe { Box::from_raw(ptr) }; 58247f0dbe8SAlice Ryhl // SAFETY: The box was already pinned when it was enqueued. 58347f0dbe8SAlice Ryhl let pinned = unsafe { Pin::new_unchecked(boxed) }; 58447f0dbe8SAlice Ryhl 58547f0dbe8SAlice Ryhl T::run(pinned) 58647f0dbe8SAlice Ryhl } 58747f0dbe8SAlice Ryhl } 58847f0dbe8SAlice Ryhl 589db4f72c9SMiguel Ojeda // SAFETY: TODO. 59047f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<Box<T>> 59147f0dbe8SAlice Ryhl where 59247f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 59347f0dbe8SAlice Ryhl T: HasWork<T, ID>, 59447f0dbe8SAlice Ryhl { 59547f0dbe8SAlice Ryhl type EnqueueOutput = (); 59647f0dbe8SAlice Ryhl 59747f0dbe8SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 59847f0dbe8SAlice Ryhl where 59947f0dbe8SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool, 60047f0dbe8SAlice Ryhl { 60147f0dbe8SAlice Ryhl // SAFETY: We're not going to move `self` or any of its fields, so its okay to temporarily 60247f0dbe8SAlice Ryhl // remove the `Pin` wrapper. 60347f0dbe8SAlice Ryhl let boxed = unsafe { Pin::into_inner_unchecked(self) }; 60447f0dbe8SAlice Ryhl let ptr = Box::into_raw(boxed); 60547f0dbe8SAlice Ryhl 60647f0dbe8SAlice Ryhl // SAFETY: Pointers into a `Box` point at a valid value. 60747f0dbe8SAlice Ryhl let work_ptr = unsafe { T::raw_get_work(ptr) }; 60847f0dbe8SAlice Ryhl // SAFETY: `raw_get_work` returns a pointer to a valid value. 60947f0dbe8SAlice Ryhl let work_ptr = unsafe { Work::raw_get(work_ptr) }; 61047f0dbe8SAlice Ryhl 61147f0dbe8SAlice Ryhl if !queue_work_on(work_ptr) { 61247f0dbe8SAlice Ryhl // SAFETY: This method requires exclusive ownership of the box, so it cannot be in a 61347f0dbe8SAlice Ryhl // workqueue. 61447f0dbe8SAlice Ryhl unsafe { ::core::hint::unreachable_unchecked() } 61547f0dbe8SAlice Ryhl } 61647f0dbe8SAlice Ryhl } 61747f0dbe8SAlice Ryhl } 61847f0dbe8SAlice Ryhl 61903394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`). 62003394130SWedson Almeida Filho /// 62103394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are 62203394130SWedson Almeida Filho /// users which expect relatively short queue flush time. 62303394130SWedson Almeida Filho /// 62403394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long. 62503394130SWedson Almeida Filho pub fn system() -> &'static Queue { 62603394130SWedson Almeida Filho // SAFETY: `system_wq` is a C global, always available. 62703394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_wq) } 62803394130SWedson Almeida Filho } 62903394130SWedson Almeida Filho 63003394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`). 63103394130SWedson Almeida Filho /// 63203394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher 63303394130SWedson Almeida Filho /// scheduling priority. 63403394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue { 63503394130SWedson Almeida Filho // SAFETY: `system_highpri_wq` is a C global, always available. 63603394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_highpri_wq) } 63703394130SWedson Almeida Filho } 63803394130SWedson Almeida Filho 63903394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`). 64003394130SWedson Almeida Filho /// 64103394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue 64203394130SWedson Almeida Filho /// flushing might take relatively long. 64303394130SWedson Almeida Filho pub fn system_long() -> &'static Queue { 64403394130SWedson Almeida Filho // SAFETY: `system_long_wq` is a C global, always available. 64503394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_long_wq) } 64603394130SWedson Almeida Filho } 64703394130SWedson Almeida Filho 64803394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`). 64903394130SWedson Almeida Filho /// 65003394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items 65103394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are 65203394130SWedson Almeida Filho /// available. 65303394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue { 65403394130SWedson Almeida Filho // SAFETY: `system_unbound_wq` is a C global, always available. 65503394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_unbound_wq) } 65603394130SWedson Almeida Filho } 65703394130SWedson Almeida Filho 65803394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`). 65903394130SWedson Almeida Filho /// 66003394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable. 66103394130SWedson Almeida Filho /// 66203394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work 66303394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed. 66403394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue { 66503394130SWedson Almeida Filho // SAFETY: `system_freezable_wq` is a C global, always available. 66603394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_freezable_wq) } 66703394130SWedson Almeida Filho } 66803394130SWedson Almeida Filho 66903394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`). 67003394130SWedson Almeida Filho /// 67103394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the 67203394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one 67303394130SWedson Almeida Filho /// returned by [`system`]. 67403394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue { 67503394130SWedson Almeida Filho // SAFETY: `system_power_efficient_wq` is a C global, always available. 67603394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_power_efficient_wq) } 67703394130SWedson Almeida Filho } 67803394130SWedson Almeida Filho 67903394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`). 68003394130SWedson Almeida Filho /// 68103394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable. 68203394130SWedson Almeida Filho /// 68303394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work 68403394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed. 68503394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue { 68603394130SWedson Almeida Filho // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available. 68703394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) } 68803394130SWedson Almeida Filho } 689