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 //! } 72*3f4223c0SDirk Behme //! # print_later(MyStruct::new(42).unwrap()); 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::sync::Arc; 79e283ee23SAlice Ryhl //! use kernel::workqueue::{self, impl_has_work, new_work, Work, WorkItem}; 8015b286d1SAlice Ryhl //! 8115b286d1SAlice Ryhl //! #[pin_data] 8215b286d1SAlice Ryhl //! struct MyStruct { 8315b286d1SAlice Ryhl //! value_1: i32, 8415b286d1SAlice Ryhl //! value_2: i32, 8515b286d1SAlice Ryhl //! #[pin] 8615b286d1SAlice Ryhl //! work_1: Work<MyStruct, 1>, 8715b286d1SAlice Ryhl //! #[pin] 8815b286d1SAlice Ryhl //! work_2: Work<MyStruct, 2>, 8915b286d1SAlice Ryhl //! } 9015b286d1SAlice Ryhl //! 9115b286d1SAlice Ryhl //! impl_has_work! { 9215b286d1SAlice Ryhl //! impl HasWork<Self, 1> for MyStruct { self.work_1 } 9315b286d1SAlice Ryhl //! impl HasWork<Self, 2> for MyStruct { self.work_2 } 9415b286d1SAlice Ryhl //! } 9515b286d1SAlice Ryhl //! 9615b286d1SAlice Ryhl //! impl MyStruct { 9715b286d1SAlice Ryhl //! fn new(value_1: i32, value_2: i32) -> Result<Arc<Self>> { 9815b286d1SAlice Ryhl //! Arc::pin_init(pin_init!(MyStruct { 9915b286d1SAlice Ryhl //! value_1, 10015b286d1SAlice Ryhl //! value_2, 10115b286d1SAlice Ryhl //! work_1 <- new_work!("MyStruct::work_1"), 10215b286d1SAlice Ryhl //! work_2 <- new_work!("MyStruct::work_2"), 103c34aa00dSWedson Almeida Filho //! }), GFP_KERNEL) 10415b286d1SAlice Ryhl //! } 10515b286d1SAlice Ryhl //! } 10615b286d1SAlice Ryhl //! 10715b286d1SAlice Ryhl //! impl WorkItem<1> for MyStruct { 10815b286d1SAlice Ryhl //! type Pointer = Arc<MyStruct>; 10915b286d1SAlice Ryhl //! 11015b286d1SAlice Ryhl //! fn run(this: Arc<MyStruct>) { 11115b286d1SAlice Ryhl //! pr_info!("The value is: {}", this.value_1); 11215b286d1SAlice Ryhl //! } 11315b286d1SAlice Ryhl //! } 11415b286d1SAlice Ryhl //! 11515b286d1SAlice Ryhl //! impl WorkItem<2> for MyStruct { 11615b286d1SAlice Ryhl //! type Pointer = Arc<MyStruct>; 11715b286d1SAlice Ryhl //! 11815b286d1SAlice Ryhl //! fn run(this: Arc<MyStruct>) { 11915b286d1SAlice Ryhl //! pr_info!("The second value is: {}", this.value_2); 12015b286d1SAlice Ryhl //! } 12115b286d1SAlice Ryhl //! } 12215b286d1SAlice Ryhl //! 12315b286d1SAlice Ryhl //! fn print_1_later(val: Arc<MyStruct>) { 12415b286d1SAlice Ryhl //! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 1>(val); 12515b286d1SAlice Ryhl //! } 12615b286d1SAlice Ryhl //! 12715b286d1SAlice Ryhl //! fn print_2_later(val: Arc<MyStruct>) { 12815b286d1SAlice Ryhl //! let _ = workqueue::system().enqueue::<Arc<MyStruct>, 2>(val); 12915b286d1SAlice Ryhl //! } 130*3f4223c0SDirk Behme //! # print_1_later(MyStruct::new(24, 25).unwrap()); 131*3f4223c0SDirk Behme //! # print_2_later(MyStruct::new(41, 42).unwrap()); 13215b286d1SAlice Ryhl //! ``` 13315b286d1SAlice Ryhl //! 134bc2e7d5cSMiguel Ojeda //! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) 135d4d791d4SAlice Ryhl 1362c109285SWedson Almeida Filho use crate::alloc::{AllocError, Flags}; 13700280272SMiguel Ojeda use crate::{prelude::*, sync::Arc, sync::LockClassKey, types::Opaque}; 1387324b889SAlice Ryhl use core::marker::PhantomData; 1397324b889SAlice Ryhl 1407324b889SAlice Ryhl /// Creates a [`Work`] initialiser with the given name and a newly-created lock class. 1417324b889SAlice Ryhl #[macro_export] 1427324b889SAlice Ryhl macro_rules! new_work { 1437324b889SAlice Ryhl ($($name:literal)?) => { 1447324b889SAlice Ryhl $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!()) 1457324b889SAlice Ryhl }; 1467324b889SAlice Ryhl } 147e283ee23SAlice Ryhl pub use new_work; 148d4d791d4SAlice Ryhl 149d4d791d4SAlice Ryhl /// A kernel work queue. 150d4d791d4SAlice Ryhl /// 151d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`. 152d4d791d4SAlice Ryhl /// 153d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are 154d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc. 155d4d791d4SAlice Ryhl #[repr(transparent)] 156d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>); 157d4d791d4SAlice Ryhl 158d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe. 159d4d791d4SAlice Ryhl unsafe impl Send for Queue {} 160d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe. 161d4d791d4SAlice Ryhl unsafe impl Sync for Queue {} 162d4d791d4SAlice Ryhl 163d4d791d4SAlice Ryhl impl Queue { 164d4d791d4SAlice Ryhl /// Use the provided `struct workqueue_struct` with Rust. 165d4d791d4SAlice Ryhl /// 166d4d791d4SAlice Ryhl /// # Safety 167d4d791d4SAlice Ryhl /// 168d4d791d4SAlice Ryhl /// The caller must ensure that the provided raw pointer is not dangling, that it points at a 169af8b18d7SValentin Obst /// valid workqueue, and that it remains valid until the end of `'a`. 170d4d791d4SAlice Ryhl pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue { 171d4d791d4SAlice Ryhl // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The 172d4d791d4SAlice Ryhl // caller promises that the pointer is not dangling. 173d4d791d4SAlice Ryhl unsafe { &*(ptr as *const Queue) } 174d4d791d4SAlice Ryhl } 175d4d791d4SAlice Ryhl 176d4d791d4SAlice Ryhl /// Enqueues a work item. 177d4d791d4SAlice Ryhl /// 178d4d791d4SAlice Ryhl /// This may fail if the work item is already enqueued in a workqueue. 179d4d791d4SAlice Ryhl /// 180d4d791d4SAlice Ryhl /// The work item will be submitted using `WORK_CPU_UNBOUND`. 181d4d791d4SAlice Ryhl pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput 182d4d791d4SAlice Ryhl where 183d4d791d4SAlice Ryhl W: RawWorkItem<ID> + Send + 'static, 184d4d791d4SAlice Ryhl { 185d4d791d4SAlice Ryhl let queue_ptr = self.0.get(); 186d4d791d4SAlice Ryhl 187d4d791d4SAlice Ryhl // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other 188d4d791d4SAlice Ryhl // `__enqueue` requirements are not relevant since `W` is `Send` and static. 189d4d791d4SAlice Ryhl // 190d4d791d4SAlice Ryhl // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which 191d4d791d4SAlice Ryhl // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this 192d4d791d4SAlice Ryhl // closure. 193d4d791d4SAlice Ryhl // 194d4d791d4SAlice Ryhl // Furthermore, if the C workqueue code accesses the pointer after this call to 195d4d791d4SAlice Ryhl // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on` 196d4d791d4SAlice Ryhl // will have returned true. In this case, `__enqueue` promises that the raw pointer will 197d4d791d4SAlice Ryhl // stay valid until we call the function pointer in the `work_struct`, so the access is ok. 198d4d791d4SAlice Ryhl unsafe { 199d4d791d4SAlice Ryhl w.__enqueue(move |work_ptr| { 2003e0bc285SMiguel Ojeda bindings::queue_work_on( 2013e0bc285SMiguel Ojeda bindings::wq_misc_consts_WORK_CPU_UNBOUND as _, 2023e0bc285SMiguel Ojeda queue_ptr, 2033e0bc285SMiguel Ojeda work_ptr, 2043e0bc285SMiguel Ojeda ) 205d4d791d4SAlice Ryhl }) 206d4d791d4SAlice Ryhl } 207d4d791d4SAlice Ryhl } 208115c95e9SAlice Ryhl 209115c95e9SAlice Ryhl /// Tries to spawn the given function or closure as a work item. 210115c95e9SAlice Ryhl /// 211115c95e9SAlice Ryhl /// This method can fail because it allocates memory to store the work item. 212c34aa00dSWedson Almeida Filho pub fn try_spawn<T: 'static + Send + FnOnce()>( 213c34aa00dSWedson Almeida Filho &self, 214c34aa00dSWedson Almeida Filho flags: Flags, 215c34aa00dSWedson Almeida Filho func: T, 216c34aa00dSWedson Almeida Filho ) -> Result<(), AllocError> { 217115c95e9SAlice Ryhl let init = pin_init!(ClosureWork { 218115c95e9SAlice Ryhl work <- new_work!("Queue::try_spawn"), 219115c95e9SAlice Ryhl func: Some(func), 220115c95e9SAlice Ryhl }); 221115c95e9SAlice Ryhl 2228373147cSDanilo Krummrich self.enqueue(KBox::pin_init(init, flags).map_err(|_| AllocError)?); 223115c95e9SAlice Ryhl Ok(()) 224115c95e9SAlice Ryhl } 225115c95e9SAlice Ryhl } 226115c95e9SAlice Ryhl 2274c799d1dSValentin Obst /// A helper type used in [`try_spawn`]. 2284c799d1dSValentin Obst /// 2294c799d1dSValentin Obst /// [`try_spawn`]: Queue::try_spawn 230115c95e9SAlice Ryhl #[pin_data] 231115c95e9SAlice Ryhl struct ClosureWork<T> { 232115c95e9SAlice Ryhl #[pin] 233115c95e9SAlice Ryhl work: Work<ClosureWork<T>>, 234115c95e9SAlice Ryhl func: Option<T>, 235115c95e9SAlice Ryhl } 236115c95e9SAlice Ryhl 237115c95e9SAlice Ryhl impl<T> ClosureWork<T> { 238115c95e9SAlice Ryhl fn project(self: Pin<&mut Self>) -> &mut Option<T> { 239115c95e9SAlice Ryhl // SAFETY: The `func` field is not structurally pinned. 240115c95e9SAlice Ryhl unsafe { &mut self.get_unchecked_mut().func } 241115c95e9SAlice Ryhl } 242115c95e9SAlice Ryhl } 243115c95e9SAlice Ryhl 244115c95e9SAlice Ryhl impl<T: FnOnce()> WorkItem for ClosureWork<T> { 2458373147cSDanilo Krummrich type Pointer = Pin<KBox<Self>>; 246115c95e9SAlice Ryhl 2478373147cSDanilo Krummrich fn run(mut this: Pin<KBox<Self>>) { 248115c95e9SAlice Ryhl if let Some(func) = this.as_mut().project().take() { 249115c95e9SAlice Ryhl (func)() 250115c95e9SAlice Ryhl } 251115c95e9SAlice Ryhl } 252d4d791d4SAlice Ryhl } 253d4d791d4SAlice Ryhl 254d4d791d4SAlice Ryhl /// A raw work item. 255d4d791d4SAlice Ryhl /// 256d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible. 257d4d791d4SAlice Ryhl /// 258d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple 259d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then 260d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The 261d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields 262d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.) 263d4d791d4SAlice Ryhl /// 264b6cda913SValentin Obst /// Note that the id is used only to select the right method to call during compilation. It won't be 265d4d791d4SAlice Ryhl /// part of the final executable. 266d4d791d4SAlice Ryhl /// 267d4d791d4SAlice Ryhl /// # Safety 268d4d791d4SAlice Ryhl /// 2694c799d1dSValentin Obst /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`] 270d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for 2714c799d1dSValentin Obst /// [`__enqueue`]. 2724c799d1dSValentin Obst /// 2734c799d1dSValentin Obst /// [`__enqueue`]: RawWorkItem::__enqueue 274d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> { 275d4d791d4SAlice Ryhl /// The return type of [`Queue::enqueue`]. 276d4d791d4SAlice Ryhl type EnqueueOutput; 277d4d791d4SAlice Ryhl 278d4d791d4SAlice Ryhl /// Enqueues this work item on a queue using the provided `queue_work_on` method. 279d4d791d4SAlice Ryhl /// 280d4d791d4SAlice Ryhl /// # Guarantees 281d4d791d4SAlice Ryhl /// 282d4d791d4SAlice Ryhl /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a 283d4d791d4SAlice Ryhl /// valid `work_struct` for the duration of the call to the closure. If the closure returns 284d4d791d4SAlice Ryhl /// true, then it is further guaranteed that the pointer remains valid until someone calls the 285d4d791d4SAlice Ryhl /// function pointer stored in the `work_struct`. 286d4d791d4SAlice Ryhl /// 287d4d791d4SAlice Ryhl /// # Safety 288d4d791d4SAlice Ryhl /// 289d4d791d4SAlice Ryhl /// The provided closure may only return `false` if the `work_struct` is already in a workqueue. 290d4d791d4SAlice Ryhl /// 291d4d791d4SAlice Ryhl /// If the work item type is annotated with any lifetimes, then you must not call the function 292d4d791d4SAlice Ryhl /// pointer after any such lifetime expires. (Never calling the function pointer is okay.) 293d4d791d4SAlice Ryhl /// 294d4d791d4SAlice Ryhl /// If the work item type is not [`Send`], then the function pointer must be called on the same 295d4d791d4SAlice Ryhl /// thread as the call to `__enqueue`. 296d4d791d4SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 297d4d791d4SAlice Ryhl where 298d4d791d4SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool; 299d4d791d4SAlice Ryhl } 30003394130SWedson Almeida Filho 3017324b889SAlice Ryhl /// Defines the method that should be called directly when a work item is executed. 3027324b889SAlice Ryhl /// 3038373147cSDanilo Krummrich /// This trait is implemented by `Pin<KBox<T>>` and [`Arc<T>`], and is mainly intended to be 3047324b889SAlice Ryhl /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`] 3054c799d1dSValentin Obst /// instead. The [`run`] method on this trait will usually just perform the appropriate 3064c799d1dSValentin Obst /// `container_of` translation and then call into the [`run`][WorkItem::run] method from the 3074c799d1dSValentin Obst /// [`WorkItem`] trait. 3087324b889SAlice Ryhl /// 3097324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper. 3107324b889SAlice Ryhl /// 3117324b889SAlice Ryhl /// # Safety 3127324b889SAlice Ryhl /// 3137324b889SAlice Ryhl /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`] 3147324b889SAlice Ryhl /// method of this trait as the function pointer. 3157324b889SAlice Ryhl /// 3167324b889SAlice Ryhl /// [`__enqueue`]: RawWorkItem::__enqueue 3177324b889SAlice Ryhl /// [`run`]: WorkItemPointer::run 3187324b889SAlice Ryhl pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> { 3197324b889SAlice Ryhl /// Run this work item. 3207324b889SAlice Ryhl /// 3217324b889SAlice Ryhl /// # Safety 3227324b889SAlice Ryhl /// 3234c799d1dSValentin Obst /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`] 3244c799d1dSValentin Obst /// where the `queue_work_on` closure returned true, and the pointer must still be valid. 3254c799d1dSValentin Obst /// 3264c799d1dSValentin Obst /// [`__enqueue`]: RawWorkItem::__enqueue 3277324b889SAlice Ryhl unsafe extern "C" fn run(ptr: *mut bindings::work_struct); 3287324b889SAlice Ryhl } 3297324b889SAlice Ryhl 3307324b889SAlice Ryhl /// Defines the method that should be called when this work item is executed. 3317324b889SAlice Ryhl /// 3327324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper. 3337324b889SAlice Ryhl pub trait WorkItem<const ID: u64 = 0> { 3347324b889SAlice Ryhl /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or 3358373147cSDanilo Krummrich /// `Pin<KBox<Self>>`. 3367324b889SAlice Ryhl type Pointer: WorkItemPointer<ID>; 3377324b889SAlice Ryhl 3387324b889SAlice Ryhl /// The method that should be called when this work item is executed. 3397324b889SAlice Ryhl fn run(this: Self::Pointer); 3407324b889SAlice Ryhl } 3417324b889SAlice Ryhl 3427324b889SAlice Ryhl /// Links for a work item. 3437324b889SAlice Ryhl /// 3444c799d1dSValentin Obst /// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`] 3457324b889SAlice Ryhl /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue. 3467324b889SAlice Ryhl /// 3477324b889SAlice Ryhl /// Wraps the kernel's C `struct work_struct`. 3487324b889SAlice Ryhl /// 3497324b889SAlice Ryhl /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it. 3504c799d1dSValentin Obst /// 3514c799d1dSValentin Obst /// [`run`]: WorkItemPointer::run 3528db31d3fSBenno Lossin #[pin_data] 3537324b889SAlice Ryhl #[repr(transparent)] 3547324b889SAlice Ryhl pub struct Work<T: ?Sized, const ID: u64 = 0> { 3558db31d3fSBenno Lossin #[pin] 3567324b889SAlice Ryhl work: Opaque<bindings::work_struct>, 3577324b889SAlice Ryhl _inner: PhantomData<T>, 3587324b889SAlice Ryhl } 3597324b889SAlice Ryhl 3607324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread. 3617324b889SAlice Ryhl // 3627324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`. 3637324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {} 3647324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread. 3657324b889SAlice Ryhl // 3667324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`. 3677324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {} 3687324b889SAlice Ryhl 3697324b889SAlice Ryhl impl<T: ?Sized, const ID: u64> Work<T, ID> { 3707324b889SAlice Ryhl /// Creates a new instance of [`Work`]. 3717324b889SAlice Ryhl #[inline] 3727324b889SAlice Ryhl pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> 3737324b889SAlice Ryhl where 3747324b889SAlice Ryhl T: WorkItem<ID>, 3757324b889SAlice Ryhl { 3768db31d3fSBenno Lossin pin_init!(Self { 3778db31d3fSBenno Lossin work <- Opaque::ffi_init(|slot| { 3788db31d3fSBenno Lossin // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as 3798db31d3fSBenno Lossin // the work item function. 3807324b889SAlice Ryhl unsafe { 3817324b889SAlice Ryhl bindings::init_work_with_key( 3827324b889SAlice Ryhl slot, 3837324b889SAlice Ryhl Some(T::Pointer::run), 3847324b889SAlice Ryhl false, 3857324b889SAlice Ryhl name.as_char_ptr(), 3867324b889SAlice Ryhl key.as_ptr(), 3878db31d3fSBenno Lossin ) 3887324b889SAlice Ryhl } 3898db31d3fSBenno Lossin }), 3908db31d3fSBenno Lossin _inner: PhantomData, 3918db31d3fSBenno Lossin }) 3927324b889SAlice Ryhl } 3937324b889SAlice Ryhl 3947324b889SAlice Ryhl /// Get a pointer to the inner `work_struct`. 3957324b889SAlice Ryhl /// 3967324b889SAlice Ryhl /// # Safety 3977324b889SAlice Ryhl /// 3987324b889SAlice Ryhl /// The provided pointer must not be dangling and must be properly aligned. (But the memory 3997324b889SAlice Ryhl /// need not be initialized.) 4007324b889SAlice Ryhl #[inline] 4017324b889SAlice Ryhl pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct { 4027324b889SAlice Ryhl // SAFETY: The caller promises that the pointer is aligned and not dangling. 4037324b889SAlice Ryhl // 4047324b889SAlice Ryhl // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that 4057324b889SAlice Ryhl // the compiler does not complain that the `work` field is unused. 4067324b889SAlice Ryhl unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) } 4077324b889SAlice Ryhl } 4087324b889SAlice Ryhl } 4097324b889SAlice Ryhl 4107324b889SAlice Ryhl /// Declares that a type has a [`Work<T, ID>`] field. 4117324b889SAlice Ryhl /// 4127324b889SAlice Ryhl /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro 4137324b889SAlice Ryhl /// like this: 4147324b889SAlice Ryhl /// 4157324b889SAlice Ryhl /// ```no_run 416e283ee23SAlice Ryhl /// use kernel::workqueue::{impl_has_work, Work}; 4177324b889SAlice Ryhl /// 4187324b889SAlice Ryhl /// struct MyWorkItem { 4197324b889SAlice Ryhl /// work_field: Work<MyWorkItem, 1>, 4207324b889SAlice Ryhl /// } 4217324b889SAlice Ryhl /// 4227324b889SAlice Ryhl /// impl_has_work! { 4237324b889SAlice Ryhl /// impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field } 4247324b889SAlice Ryhl /// } 4257324b889SAlice Ryhl /// ``` 4267324b889SAlice Ryhl /// 4274c799d1dSValentin Obst /// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct` 4287324b889SAlice Ryhl /// fields by using a different id for each one. 4297324b889SAlice Ryhl /// 4307324b889SAlice Ryhl /// # Safety 4317324b889SAlice Ryhl /// 432af8b18d7SValentin Obst /// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The 433af8b18d7SValentin Obst /// methods on this trait must have exactly the behavior that the definitions given below have. 4347324b889SAlice Ryhl /// 4357324b889SAlice Ryhl /// [`impl_has_work!`]: crate::impl_has_work 4367324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET 4377324b889SAlice Ryhl pub unsafe trait HasWork<T, const ID: u64 = 0> { 4387324b889SAlice Ryhl /// The offset of the [`Work<T, ID>`] field. 4397324b889SAlice Ryhl const OFFSET: usize; 4407324b889SAlice Ryhl 4417324b889SAlice Ryhl /// Returns the offset of the [`Work<T, ID>`] field. 4427324b889SAlice Ryhl /// 443af8b18d7SValentin Obst /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not 4444c799d1dSValentin Obst /// [`Sized`]. 4457324b889SAlice Ryhl /// 4467324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET 4477324b889SAlice Ryhl #[inline] 4487324b889SAlice Ryhl fn get_work_offset(&self) -> usize { 4497324b889SAlice Ryhl Self::OFFSET 4507324b889SAlice Ryhl } 4517324b889SAlice Ryhl 4527324b889SAlice Ryhl /// Returns a pointer to the [`Work<T, ID>`] field. 4537324b889SAlice Ryhl /// 4547324b889SAlice Ryhl /// # Safety 4557324b889SAlice Ryhl /// 4567324b889SAlice Ryhl /// The provided pointer must point at a valid struct of type `Self`. 4577324b889SAlice Ryhl #[inline] 4587324b889SAlice Ryhl unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> { 4597324b889SAlice Ryhl // SAFETY: The caller promises that the pointer is valid. 4607324b889SAlice Ryhl unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> } 4617324b889SAlice Ryhl } 4627324b889SAlice Ryhl 4637324b889SAlice Ryhl /// Returns a pointer to the struct containing the [`Work<T, ID>`] field. 4647324b889SAlice Ryhl /// 4657324b889SAlice Ryhl /// # Safety 4667324b889SAlice Ryhl /// 4677324b889SAlice Ryhl /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`. 4687324b889SAlice Ryhl #[inline] 4697324b889SAlice Ryhl unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self 4707324b889SAlice Ryhl where 4717324b889SAlice Ryhl Self: Sized, 4727324b889SAlice Ryhl { 4737324b889SAlice Ryhl // SAFETY: The caller promises that the pointer points at a field of the right type in the 4747324b889SAlice Ryhl // right kind of struct. 4757324b889SAlice Ryhl unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self } 4767324b889SAlice Ryhl } 4777324b889SAlice Ryhl } 4787324b889SAlice Ryhl 4797324b889SAlice Ryhl /// Used to safely implement the [`HasWork<T, ID>`] trait. 4807324b889SAlice Ryhl /// 4817324b889SAlice Ryhl /// # Examples 4827324b889SAlice Ryhl /// 4837324b889SAlice Ryhl /// ``` 4847324b889SAlice Ryhl /// use kernel::sync::Arc; 485e283ee23SAlice Ryhl /// use kernel::workqueue::{self, impl_has_work, Work}; 4867324b889SAlice Ryhl /// 487fe7d9d80SRoland Xu /// struct MyStruct<'a, T, const N: usize> { 488fe7d9d80SRoland Xu /// work_field: Work<MyStruct<'a, T, N>, 17>, 489fe7d9d80SRoland Xu /// f: fn(&'a [T; N]), 4907324b889SAlice Ryhl /// } 4917324b889SAlice Ryhl /// 4927324b889SAlice Ryhl /// impl_has_work! { 493fe7d9d80SRoland Xu /// impl{'a, T, const N: usize} HasWork<MyStruct<'a, T, N>, 17> 494fe7d9d80SRoland Xu /// for MyStruct<'a, T, N> { self.work_field } 4957324b889SAlice Ryhl /// } 4967324b889SAlice Ryhl /// ``` 4977324b889SAlice Ryhl #[macro_export] 4987324b889SAlice Ryhl macro_rules! impl_has_work { 499fe7d9d80SRoland Xu ($(impl$({$($generics:tt)*})? 5007324b889SAlice Ryhl HasWork<$work_type:ty $(, $id:tt)?> 501fe7d9d80SRoland Xu for $self:ty 5027324b889SAlice Ryhl { self.$field:ident } 5037324b889SAlice Ryhl )*) => {$( 5047324b889SAlice Ryhl // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right 5057324b889SAlice Ryhl // type. 506fe7d9d80SRoland Xu unsafe impl$(<$($generics)+>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self { 5077324b889SAlice Ryhl const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; 5087324b889SAlice Ryhl 5097324b889SAlice Ryhl #[inline] 5107324b889SAlice Ryhl unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> { 5117324b889SAlice Ryhl // SAFETY: The caller promises that the pointer is not dangling. 5127324b889SAlice Ryhl unsafe { 5137324b889SAlice Ryhl ::core::ptr::addr_of_mut!((*ptr).$field) 5147324b889SAlice Ryhl } 5157324b889SAlice Ryhl } 5167324b889SAlice Ryhl } 5177324b889SAlice Ryhl )*}; 5187324b889SAlice Ryhl } 519e283ee23SAlice Ryhl pub use impl_has_work; 5207324b889SAlice Ryhl 521115c95e9SAlice Ryhl impl_has_work! { 522fe7d9d80SRoland Xu impl{T} HasWork<Self> for ClosureWork<T> { self.work } 523115c95e9SAlice Ryhl } 524115c95e9SAlice Ryhl 525db4f72c9SMiguel Ojeda // SAFETY: TODO. 52647f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T> 52747f0dbe8SAlice Ryhl where 52847f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 52947f0dbe8SAlice Ryhl T: HasWork<T, ID>, 53047f0dbe8SAlice Ryhl { 53147f0dbe8SAlice Ryhl unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { 532c28bfe76SMiguel Ojeda // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`. 53347f0dbe8SAlice Ryhl let ptr = ptr as *mut Work<T, ID>; 53447f0dbe8SAlice Ryhl // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`. 53547f0dbe8SAlice Ryhl let ptr = unsafe { T::work_container_of(ptr) }; 53647f0dbe8SAlice Ryhl // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership. 53747f0dbe8SAlice Ryhl let arc = unsafe { Arc::from_raw(ptr) }; 53847f0dbe8SAlice Ryhl 53947f0dbe8SAlice Ryhl T::run(arc) 54047f0dbe8SAlice Ryhl } 54147f0dbe8SAlice Ryhl } 54247f0dbe8SAlice Ryhl 543db4f72c9SMiguel Ojeda // SAFETY: TODO. 54447f0dbe8SAlice Ryhl unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T> 54547f0dbe8SAlice Ryhl where 54647f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 54747f0dbe8SAlice Ryhl T: HasWork<T, ID>, 54847f0dbe8SAlice Ryhl { 54947f0dbe8SAlice Ryhl type EnqueueOutput = Result<(), Self>; 55047f0dbe8SAlice Ryhl 55147f0dbe8SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 55247f0dbe8SAlice Ryhl where 55347f0dbe8SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool, 55447f0dbe8SAlice Ryhl { 55547f0dbe8SAlice Ryhl // Casting between const and mut is not a problem as long as the pointer is a raw pointer. 55647f0dbe8SAlice Ryhl let ptr = Arc::into_raw(self).cast_mut(); 55747f0dbe8SAlice Ryhl 55847f0dbe8SAlice Ryhl // SAFETY: Pointers into an `Arc` point at a valid value. 55947f0dbe8SAlice Ryhl let work_ptr = unsafe { T::raw_get_work(ptr) }; 56047f0dbe8SAlice Ryhl // SAFETY: `raw_get_work` returns a pointer to a valid value. 56147f0dbe8SAlice Ryhl let work_ptr = unsafe { Work::raw_get(work_ptr) }; 56247f0dbe8SAlice Ryhl 56347f0dbe8SAlice Ryhl if queue_work_on(work_ptr) { 56447f0dbe8SAlice Ryhl Ok(()) 56547f0dbe8SAlice Ryhl } else { 56647f0dbe8SAlice Ryhl // SAFETY: The work queue has not taken ownership of the pointer. 56747f0dbe8SAlice Ryhl Err(unsafe { Arc::from_raw(ptr) }) 56847f0dbe8SAlice Ryhl } 56947f0dbe8SAlice Ryhl } 57047f0dbe8SAlice Ryhl } 57147f0dbe8SAlice Ryhl 572db4f72c9SMiguel Ojeda // SAFETY: TODO. 5738373147cSDanilo Krummrich unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>> 57447f0dbe8SAlice Ryhl where 57547f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 57647f0dbe8SAlice Ryhl T: HasWork<T, ID>, 57747f0dbe8SAlice Ryhl { 57847f0dbe8SAlice Ryhl unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { 579c28bfe76SMiguel Ojeda // The `__enqueue` method always uses a `work_struct` stored in a `Work<T, ID>`. 58047f0dbe8SAlice Ryhl let ptr = ptr as *mut Work<T, ID>; 58147f0dbe8SAlice Ryhl // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`. 58247f0dbe8SAlice Ryhl let ptr = unsafe { T::work_container_of(ptr) }; 58347f0dbe8SAlice Ryhl // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership. 5848373147cSDanilo Krummrich let boxed = unsafe { KBox::from_raw(ptr) }; 58547f0dbe8SAlice Ryhl // SAFETY: The box was already pinned when it was enqueued. 58647f0dbe8SAlice Ryhl let pinned = unsafe { Pin::new_unchecked(boxed) }; 58747f0dbe8SAlice Ryhl 58847f0dbe8SAlice Ryhl T::run(pinned) 58947f0dbe8SAlice Ryhl } 59047f0dbe8SAlice Ryhl } 59147f0dbe8SAlice Ryhl 592db4f72c9SMiguel Ojeda // SAFETY: TODO. 5938373147cSDanilo Krummrich unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<KBox<T>> 59447f0dbe8SAlice Ryhl where 59547f0dbe8SAlice Ryhl T: WorkItem<ID, Pointer = Self>, 59647f0dbe8SAlice Ryhl T: HasWork<T, ID>, 59747f0dbe8SAlice Ryhl { 59847f0dbe8SAlice Ryhl type EnqueueOutput = (); 59947f0dbe8SAlice Ryhl 60047f0dbe8SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 60147f0dbe8SAlice Ryhl where 60247f0dbe8SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool, 60347f0dbe8SAlice Ryhl { 60447f0dbe8SAlice Ryhl // SAFETY: We're not going to move `self` or any of its fields, so its okay to temporarily 60547f0dbe8SAlice Ryhl // remove the `Pin` wrapper. 60647f0dbe8SAlice Ryhl let boxed = unsafe { Pin::into_inner_unchecked(self) }; 6078373147cSDanilo Krummrich let ptr = KBox::into_raw(boxed); 60847f0dbe8SAlice Ryhl 6098373147cSDanilo Krummrich // SAFETY: Pointers into a `KBox` point at a valid value. 61047f0dbe8SAlice Ryhl let work_ptr = unsafe { T::raw_get_work(ptr) }; 61147f0dbe8SAlice Ryhl // SAFETY: `raw_get_work` returns a pointer to a valid value. 61247f0dbe8SAlice Ryhl let work_ptr = unsafe { Work::raw_get(work_ptr) }; 61347f0dbe8SAlice Ryhl 61447f0dbe8SAlice Ryhl if !queue_work_on(work_ptr) { 61547f0dbe8SAlice Ryhl // SAFETY: This method requires exclusive ownership of the box, so it cannot be in a 61647f0dbe8SAlice Ryhl // workqueue. 61747f0dbe8SAlice Ryhl unsafe { ::core::hint::unreachable_unchecked() } 61847f0dbe8SAlice Ryhl } 61947f0dbe8SAlice Ryhl } 62047f0dbe8SAlice Ryhl } 62147f0dbe8SAlice Ryhl 62203394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`). 62303394130SWedson Almeida Filho /// 62403394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are 62503394130SWedson Almeida Filho /// users which expect relatively short queue flush time. 62603394130SWedson Almeida Filho /// 62703394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long. 62803394130SWedson Almeida Filho pub fn system() -> &'static Queue { 62903394130SWedson Almeida Filho // SAFETY: `system_wq` is a C global, always available. 63003394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_wq) } 63103394130SWedson Almeida Filho } 63203394130SWedson Almeida Filho 63303394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`). 63403394130SWedson Almeida Filho /// 63503394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher 63603394130SWedson Almeida Filho /// scheduling priority. 63703394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue { 63803394130SWedson Almeida Filho // SAFETY: `system_highpri_wq` is a C global, always available. 63903394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_highpri_wq) } 64003394130SWedson Almeida Filho } 64103394130SWedson Almeida Filho 64203394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`). 64303394130SWedson Almeida Filho /// 64403394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue 64503394130SWedson Almeida Filho /// flushing might take relatively long. 64603394130SWedson Almeida Filho pub fn system_long() -> &'static Queue { 64703394130SWedson Almeida Filho // SAFETY: `system_long_wq` is a C global, always available. 64803394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_long_wq) } 64903394130SWedson Almeida Filho } 65003394130SWedson Almeida Filho 65103394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`). 65203394130SWedson Almeida Filho /// 65303394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items 65403394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are 65503394130SWedson Almeida Filho /// available. 65603394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue { 65703394130SWedson Almeida Filho // SAFETY: `system_unbound_wq` is a C global, always available. 65803394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_unbound_wq) } 65903394130SWedson Almeida Filho } 66003394130SWedson Almeida Filho 66103394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`). 66203394130SWedson Almeida Filho /// 66303394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable. 66403394130SWedson Almeida Filho /// 66503394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work 66603394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed. 66703394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue { 66803394130SWedson Almeida Filho // SAFETY: `system_freezable_wq` is a C global, always available. 66903394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_freezable_wq) } 67003394130SWedson Almeida Filho } 67103394130SWedson Almeida Filho 67203394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`). 67303394130SWedson Almeida Filho /// 67403394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the 67503394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one 67603394130SWedson Almeida Filho /// returned by [`system`]. 67703394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue { 67803394130SWedson Almeida Filho // SAFETY: `system_power_efficient_wq` is a C global, always available. 67903394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_power_efficient_wq) } 68003394130SWedson Almeida Filho } 68103394130SWedson Almeida Filho 68203394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`). 68303394130SWedson Almeida Filho /// 68403394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable. 68503394130SWedson Almeida Filho /// 68603394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work 68703394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed. 68803394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue { 68903394130SWedson Almeida Filho // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available. 69003394130SWedson Almeida Filho unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) } 69103394130SWedson Almeida Filho } 692