xref: /linux-6.15/rust/kernel/workqueue.rs (revision 7324b889)
1d4d791d4SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
2d4d791d4SAlice Ryhl 
3d4d791d4SAlice Ryhl //! Work queues.
4d4d791d4SAlice Ryhl //!
5*7324b889SAlice Ryhl //! This file has two components: The raw work item API, and the safe work item API.
6*7324b889SAlice Ryhl //!
7*7324b889SAlice Ryhl //! One pattern that is used in both APIs is the `ID` const generic, which exists to allow a single
8*7324b889SAlice Ryhl //! type to define multiple `work_struct` fields. This is done by choosing an id for each field,
9*7324b889SAlice Ryhl //! and using that id to specify which field you wish to use. (The actual value doesn't matter, as
10*7324b889SAlice Ryhl //! long as you use different values for different fields of the same struct.) Since these IDs are
11*7324b889SAlice Ryhl //! generic, they are used only at compile-time, so they shouldn't exist in the final binary.
12*7324b889SAlice Ryhl //!
13*7324b889SAlice Ryhl //! # The raw API
14*7324b889SAlice Ryhl //!
15*7324b889SAlice Ryhl //! The raw API consists of the `RawWorkItem` trait, where the work item needs to provide an
16*7324b889SAlice Ryhl //! arbitrary function that knows how to enqueue the work item. It should usually not be used
17*7324b889SAlice Ryhl //! directly, but if you want to, you can use it without using the pieces from the safe API.
18*7324b889SAlice Ryhl //!
19*7324b889SAlice Ryhl //! # The safe API
20*7324b889SAlice Ryhl //!
21*7324b889SAlice Ryhl //! The safe API is used via the `Work` struct and `WorkItem` traits. Furthermore, it also includes
22*7324b889SAlice Ryhl //! a trait called `WorkItemPointer`, which is usually not used directly by the user.
23*7324b889SAlice Ryhl //!
24*7324b889SAlice Ryhl //!  * The `Work` struct is the Rust wrapper for the C `work_struct` type.
25*7324b889SAlice Ryhl //!  * The `WorkItem` trait is implemented for structs that can be enqueued to a workqueue.
26*7324b889SAlice Ryhl //!  * The `WorkItemPointer` trait is implemented for the pointer type that points at a something
27*7324b889SAlice Ryhl //!    that implements `WorkItem`.
28*7324b889SAlice Ryhl //!
29d4d791d4SAlice Ryhl //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h)
30d4d791d4SAlice Ryhl 
31*7324b889SAlice Ryhl use crate::{bindings, prelude::*, sync::LockClassKey, types::Opaque};
32*7324b889SAlice Ryhl use core::marker::PhantomData;
33*7324b889SAlice Ryhl 
34*7324b889SAlice Ryhl /// Creates a [`Work`] initialiser with the given name and a newly-created lock class.
35*7324b889SAlice Ryhl #[macro_export]
36*7324b889SAlice Ryhl macro_rules! new_work {
37*7324b889SAlice Ryhl     ($($name:literal)?) => {
38*7324b889SAlice Ryhl         $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
39*7324b889SAlice Ryhl     };
40*7324b889SAlice Ryhl }
41d4d791d4SAlice Ryhl 
42d4d791d4SAlice Ryhl /// A kernel work queue.
43d4d791d4SAlice Ryhl ///
44d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`.
45d4d791d4SAlice Ryhl ///
46d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are
47d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc.
48d4d791d4SAlice Ryhl #[repr(transparent)]
49d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>);
50d4d791d4SAlice Ryhl 
51d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
52d4d791d4SAlice Ryhl unsafe impl Send for Queue {}
53d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
54d4d791d4SAlice Ryhl unsafe impl Sync for Queue {}
55d4d791d4SAlice Ryhl 
56d4d791d4SAlice Ryhl impl Queue {
57d4d791d4SAlice Ryhl     /// Use the provided `struct workqueue_struct` with Rust.
58d4d791d4SAlice Ryhl     ///
59d4d791d4SAlice Ryhl     /// # Safety
60d4d791d4SAlice Ryhl     ///
61d4d791d4SAlice Ryhl     /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
62d4d791d4SAlice Ryhl     /// valid workqueue, and that it remains valid until the end of 'a.
63d4d791d4SAlice Ryhl     pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
64d4d791d4SAlice Ryhl         // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
65d4d791d4SAlice Ryhl         // caller promises that the pointer is not dangling.
66d4d791d4SAlice Ryhl         unsafe { &*(ptr as *const Queue) }
67d4d791d4SAlice Ryhl     }
68d4d791d4SAlice Ryhl 
69d4d791d4SAlice Ryhl     /// Enqueues a work item.
70d4d791d4SAlice Ryhl     ///
71d4d791d4SAlice Ryhl     /// This may fail if the work item is already enqueued in a workqueue.
72d4d791d4SAlice Ryhl     ///
73d4d791d4SAlice Ryhl     /// The work item will be submitted using `WORK_CPU_UNBOUND`.
74d4d791d4SAlice Ryhl     pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
75d4d791d4SAlice Ryhl     where
76d4d791d4SAlice Ryhl         W: RawWorkItem<ID> + Send + 'static,
77d4d791d4SAlice Ryhl     {
78d4d791d4SAlice Ryhl         let queue_ptr = self.0.get();
79d4d791d4SAlice Ryhl 
80d4d791d4SAlice Ryhl         // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
81d4d791d4SAlice Ryhl         // `__enqueue` requirements are not relevant since `W` is `Send` and static.
82d4d791d4SAlice Ryhl         //
83d4d791d4SAlice Ryhl         // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
84d4d791d4SAlice Ryhl         // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
85d4d791d4SAlice Ryhl         // closure.
86d4d791d4SAlice Ryhl         //
87d4d791d4SAlice Ryhl         // Furthermore, if the C workqueue code accesses the pointer after this call to
88d4d791d4SAlice Ryhl         // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
89d4d791d4SAlice Ryhl         // will have returned true. In this case, `__enqueue` promises that the raw pointer will
90d4d791d4SAlice Ryhl         // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
91d4d791d4SAlice Ryhl         unsafe {
92d4d791d4SAlice Ryhl             w.__enqueue(move |work_ptr| {
93d4d791d4SAlice Ryhl                 bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr)
94d4d791d4SAlice Ryhl             })
95d4d791d4SAlice Ryhl         }
96d4d791d4SAlice Ryhl     }
97d4d791d4SAlice Ryhl }
98d4d791d4SAlice Ryhl 
99d4d791d4SAlice Ryhl /// A raw work item.
100d4d791d4SAlice Ryhl ///
101d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible.
102d4d791d4SAlice Ryhl ///
103d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple
104d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
105d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The
106d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields
107d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.)
108d4d791d4SAlice Ryhl ///
109d4d791d4SAlice Ryhl /// Note that the id is used only to select the right method to call during compilation. It wont be
110d4d791d4SAlice Ryhl /// part of the final executable.
111d4d791d4SAlice Ryhl ///
112d4d791d4SAlice Ryhl /// # Safety
113d4d791d4SAlice Ryhl ///
114d4d791d4SAlice Ryhl /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by `__enqueue`
115d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for
116d4d791d4SAlice Ryhl /// `__enqueue`.
117d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> {
118d4d791d4SAlice Ryhl     /// The return type of [`Queue::enqueue`].
119d4d791d4SAlice Ryhl     type EnqueueOutput;
120d4d791d4SAlice Ryhl 
121d4d791d4SAlice Ryhl     /// Enqueues this work item on a queue using the provided `queue_work_on` method.
122d4d791d4SAlice Ryhl     ///
123d4d791d4SAlice Ryhl     /// # Guarantees
124d4d791d4SAlice Ryhl     ///
125d4d791d4SAlice Ryhl     /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
126d4d791d4SAlice Ryhl     /// valid `work_struct` for the duration of the call to the closure. If the closure returns
127d4d791d4SAlice Ryhl     /// true, then it is further guaranteed that the pointer remains valid until someone calls the
128d4d791d4SAlice Ryhl     /// function pointer stored in the `work_struct`.
129d4d791d4SAlice Ryhl     ///
130d4d791d4SAlice Ryhl     /// # Safety
131d4d791d4SAlice Ryhl     ///
132d4d791d4SAlice Ryhl     /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
133d4d791d4SAlice Ryhl     ///
134d4d791d4SAlice Ryhl     /// If the work item type is annotated with any lifetimes, then you must not call the function
135d4d791d4SAlice Ryhl     /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
136d4d791d4SAlice Ryhl     ///
137d4d791d4SAlice Ryhl     /// If the work item type is not [`Send`], then the function pointer must be called on the same
138d4d791d4SAlice Ryhl     /// thread as the call to `__enqueue`.
139d4d791d4SAlice Ryhl     unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
140d4d791d4SAlice Ryhl     where
141d4d791d4SAlice Ryhl         F: FnOnce(*mut bindings::work_struct) -> bool;
142d4d791d4SAlice Ryhl }
14303394130SWedson Almeida Filho 
144*7324b889SAlice Ryhl /// Defines the method that should be called directly when a work item is executed.
145*7324b889SAlice Ryhl ///
146*7324b889SAlice Ryhl /// This trait is implemented by `Pin<Box<T>>` and `Arc<T>`, and is mainly intended to be
147*7324b889SAlice Ryhl /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`]
148*7324b889SAlice Ryhl /// instead. The `run` method on this trait will usually just perform the appropriate
149*7324b889SAlice Ryhl /// `container_of` translation and then call into the `run` method from the [`WorkItem`] trait.
150*7324b889SAlice Ryhl ///
151*7324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
152*7324b889SAlice Ryhl ///
153*7324b889SAlice Ryhl /// # Safety
154*7324b889SAlice Ryhl ///
155*7324b889SAlice Ryhl /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
156*7324b889SAlice Ryhl /// method of this trait as the function pointer.
157*7324b889SAlice Ryhl ///
158*7324b889SAlice Ryhl /// [`__enqueue`]: RawWorkItem::__enqueue
159*7324b889SAlice Ryhl /// [`run`]: WorkItemPointer::run
160*7324b889SAlice Ryhl pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
161*7324b889SAlice Ryhl     /// Run this work item.
162*7324b889SAlice Ryhl     ///
163*7324b889SAlice Ryhl     /// # Safety
164*7324b889SAlice Ryhl     ///
165*7324b889SAlice Ryhl     /// The provided `work_struct` pointer must originate from a previous call to `__enqueue` where
166*7324b889SAlice Ryhl     /// the `queue_work_on` closure returned true, and the pointer must still be valid.
167*7324b889SAlice Ryhl     unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
168*7324b889SAlice Ryhl }
169*7324b889SAlice Ryhl 
170*7324b889SAlice Ryhl /// Defines the method that should be called when this work item is executed.
171*7324b889SAlice Ryhl ///
172*7324b889SAlice Ryhl /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
173*7324b889SAlice Ryhl pub trait WorkItem<const ID: u64 = 0> {
174*7324b889SAlice Ryhl     /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
175*7324b889SAlice Ryhl     /// `Pin<Box<Self>>`.
176*7324b889SAlice Ryhl     type Pointer: WorkItemPointer<ID>;
177*7324b889SAlice Ryhl 
178*7324b889SAlice Ryhl     /// The method that should be called when this work item is executed.
179*7324b889SAlice Ryhl     fn run(this: Self::Pointer);
180*7324b889SAlice Ryhl }
181*7324b889SAlice Ryhl 
182*7324b889SAlice Ryhl /// Links for a work item.
183*7324b889SAlice Ryhl ///
184*7324b889SAlice Ryhl /// This struct contains a function pointer to the `run` function from the [`WorkItemPointer`]
185*7324b889SAlice Ryhl /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
186*7324b889SAlice Ryhl ///
187*7324b889SAlice Ryhl /// Wraps the kernel's C `struct work_struct`.
188*7324b889SAlice Ryhl ///
189*7324b889SAlice Ryhl /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
190*7324b889SAlice Ryhl #[repr(transparent)]
191*7324b889SAlice Ryhl pub struct Work<T: ?Sized, const ID: u64 = 0> {
192*7324b889SAlice Ryhl     work: Opaque<bindings::work_struct>,
193*7324b889SAlice Ryhl     _inner: PhantomData<T>,
194*7324b889SAlice Ryhl }
195*7324b889SAlice Ryhl 
196*7324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
197*7324b889SAlice Ryhl //
198*7324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
199*7324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
200*7324b889SAlice Ryhl // SAFETY: Kernel work items are usable from any thread.
201*7324b889SAlice Ryhl //
202*7324b889SAlice Ryhl // We do not need to constrain `T` since the work item does not actually contain a `T`.
203*7324b889SAlice Ryhl unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
204*7324b889SAlice Ryhl 
205*7324b889SAlice Ryhl impl<T: ?Sized, const ID: u64> Work<T, ID> {
206*7324b889SAlice Ryhl     /// Creates a new instance of [`Work`].
207*7324b889SAlice Ryhl     #[inline]
208*7324b889SAlice Ryhl     #[allow(clippy::new_ret_no_self)]
209*7324b889SAlice Ryhl     pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
210*7324b889SAlice Ryhl     where
211*7324b889SAlice Ryhl         T: WorkItem<ID>,
212*7324b889SAlice Ryhl     {
213*7324b889SAlice Ryhl         // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the work
214*7324b889SAlice Ryhl         // item function.
215*7324b889SAlice Ryhl         unsafe {
216*7324b889SAlice Ryhl             kernel::init::pin_init_from_closure(move |slot| {
217*7324b889SAlice Ryhl                 let slot = Self::raw_get(slot);
218*7324b889SAlice Ryhl                 bindings::init_work_with_key(
219*7324b889SAlice Ryhl                     slot,
220*7324b889SAlice Ryhl                     Some(T::Pointer::run),
221*7324b889SAlice Ryhl                     false,
222*7324b889SAlice Ryhl                     name.as_char_ptr(),
223*7324b889SAlice Ryhl                     key.as_ptr(),
224*7324b889SAlice Ryhl                 );
225*7324b889SAlice Ryhl                 Ok(())
226*7324b889SAlice Ryhl             })
227*7324b889SAlice Ryhl         }
228*7324b889SAlice Ryhl     }
229*7324b889SAlice Ryhl 
230*7324b889SAlice Ryhl     /// Get a pointer to the inner `work_struct`.
231*7324b889SAlice Ryhl     ///
232*7324b889SAlice Ryhl     /// # Safety
233*7324b889SAlice Ryhl     ///
234*7324b889SAlice Ryhl     /// The provided pointer must not be dangling and must be properly aligned. (But the memory
235*7324b889SAlice Ryhl     /// need not be initialized.)
236*7324b889SAlice Ryhl     #[inline]
237*7324b889SAlice Ryhl     pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
238*7324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is aligned and not dangling.
239*7324b889SAlice Ryhl         //
240*7324b889SAlice Ryhl         // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
241*7324b889SAlice Ryhl         // the compiler does not complain that the `work` field is unused.
242*7324b889SAlice Ryhl         unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
243*7324b889SAlice Ryhl     }
244*7324b889SAlice Ryhl }
245*7324b889SAlice Ryhl 
246*7324b889SAlice Ryhl /// Declares that a type has a [`Work<T, ID>`] field.
247*7324b889SAlice Ryhl ///
248*7324b889SAlice Ryhl /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
249*7324b889SAlice Ryhl /// like this:
250*7324b889SAlice Ryhl ///
251*7324b889SAlice Ryhl /// ```no_run
252*7324b889SAlice Ryhl /// use kernel::impl_has_work;
253*7324b889SAlice Ryhl /// use kernel::prelude::*;
254*7324b889SAlice Ryhl /// use kernel::workqueue::Work;
255*7324b889SAlice Ryhl ///
256*7324b889SAlice Ryhl /// struct MyWorkItem {
257*7324b889SAlice Ryhl ///     work_field: Work<MyWorkItem, 1>,
258*7324b889SAlice Ryhl /// }
259*7324b889SAlice Ryhl ///
260*7324b889SAlice Ryhl /// impl_has_work! {
261*7324b889SAlice Ryhl ///     impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
262*7324b889SAlice Ryhl /// }
263*7324b889SAlice Ryhl /// ```
264*7324b889SAlice Ryhl ///
265*7324b889SAlice Ryhl /// Note that since the `Work` type is annotated with an id, you can have several `work_struct`
266*7324b889SAlice Ryhl /// fields by using a different id for each one.
267*7324b889SAlice Ryhl ///
268*7324b889SAlice Ryhl /// # Safety
269*7324b889SAlice Ryhl ///
270*7324b889SAlice Ryhl /// The [`OFFSET`] constant must be the offset of a field in Self of type [`Work<T, ID>`]. The methods on
271*7324b889SAlice Ryhl /// this trait must have exactly the behavior that the definitions given below have.
272*7324b889SAlice Ryhl ///
273*7324b889SAlice Ryhl /// [`Work<T, ID>`]: Work
274*7324b889SAlice Ryhl /// [`impl_has_work!`]: crate::impl_has_work
275*7324b889SAlice Ryhl /// [`OFFSET`]: HasWork::OFFSET
276*7324b889SAlice Ryhl pub unsafe trait HasWork<T, const ID: u64 = 0> {
277*7324b889SAlice Ryhl     /// The offset of the [`Work<T, ID>`] field.
278*7324b889SAlice Ryhl     ///
279*7324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
280*7324b889SAlice Ryhl     const OFFSET: usize;
281*7324b889SAlice Ryhl 
282*7324b889SAlice Ryhl     /// Returns the offset of the [`Work<T, ID>`] field.
283*7324b889SAlice Ryhl     ///
284*7324b889SAlice Ryhl     /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not Sized.
285*7324b889SAlice Ryhl     ///
286*7324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
287*7324b889SAlice Ryhl     /// [`OFFSET`]: HasWork::OFFSET
288*7324b889SAlice Ryhl     #[inline]
289*7324b889SAlice Ryhl     fn get_work_offset(&self) -> usize {
290*7324b889SAlice Ryhl         Self::OFFSET
291*7324b889SAlice Ryhl     }
292*7324b889SAlice Ryhl 
293*7324b889SAlice Ryhl     /// Returns a pointer to the [`Work<T, ID>`] field.
294*7324b889SAlice Ryhl     ///
295*7324b889SAlice Ryhl     /// # Safety
296*7324b889SAlice Ryhl     ///
297*7324b889SAlice Ryhl     /// The provided pointer must point at a valid struct of type `Self`.
298*7324b889SAlice Ryhl     ///
299*7324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
300*7324b889SAlice Ryhl     #[inline]
301*7324b889SAlice Ryhl     unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
302*7324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer is valid.
303*7324b889SAlice Ryhl         unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> }
304*7324b889SAlice Ryhl     }
305*7324b889SAlice Ryhl 
306*7324b889SAlice Ryhl     /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
307*7324b889SAlice Ryhl     ///
308*7324b889SAlice Ryhl     /// # Safety
309*7324b889SAlice Ryhl     ///
310*7324b889SAlice Ryhl     /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
311*7324b889SAlice Ryhl     ///
312*7324b889SAlice Ryhl     /// [`Work<T, ID>`]: Work
313*7324b889SAlice Ryhl     #[inline]
314*7324b889SAlice Ryhl     unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self
315*7324b889SAlice Ryhl     where
316*7324b889SAlice Ryhl         Self: Sized,
317*7324b889SAlice Ryhl     {
318*7324b889SAlice Ryhl         // SAFETY: The caller promises that the pointer points at a field of the right type in the
319*7324b889SAlice Ryhl         // right kind of struct.
320*7324b889SAlice Ryhl         unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self }
321*7324b889SAlice Ryhl     }
322*7324b889SAlice Ryhl }
323*7324b889SAlice Ryhl 
324*7324b889SAlice Ryhl /// Used to safely implement the [`HasWork<T, ID>`] trait.
325*7324b889SAlice Ryhl ///
326*7324b889SAlice Ryhl /// # Examples
327*7324b889SAlice Ryhl ///
328*7324b889SAlice Ryhl /// ```
329*7324b889SAlice Ryhl /// use kernel::impl_has_work;
330*7324b889SAlice Ryhl /// use kernel::sync::Arc;
331*7324b889SAlice Ryhl /// use kernel::workqueue::{self, Work};
332*7324b889SAlice Ryhl ///
333*7324b889SAlice Ryhl /// struct MyStruct {
334*7324b889SAlice Ryhl ///     work_field: Work<MyStruct, 17>,
335*7324b889SAlice Ryhl /// }
336*7324b889SAlice Ryhl ///
337*7324b889SAlice Ryhl /// impl_has_work! {
338*7324b889SAlice Ryhl ///     impl HasWork<MyStruct, 17> for MyStruct { self.work_field }
339*7324b889SAlice Ryhl /// }
340*7324b889SAlice Ryhl /// ```
341*7324b889SAlice Ryhl ///
342*7324b889SAlice Ryhl /// [`HasWork<T, ID>`]: HasWork
343*7324b889SAlice Ryhl #[macro_export]
344*7324b889SAlice Ryhl macro_rules! impl_has_work {
345*7324b889SAlice Ryhl     ($(impl$(<$($implarg:ident),*>)?
346*7324b889SAlice Ryhl        HasWork<$work_type:ty $(, $id:tt)?>
347*7324b889SAlice Ryhl        for $self:ident $(<$($selfarg:ident),*>)?
348*7324b889SAlice Ryhl        { self.$field:ident }
349*7324b889SAlice Ryhl     )*) => {$(
350*7324b889SAlice Ryhl         // SAFETY: The implementation of `raw_get_work` only compiles if the field has the right
351*7324b889SAlice Ryhl         // type.
352*7324b889SAlice Ryhl         unsafe impl$(<$($implarg),*>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self $(<$($selfarg),*>)? {
353*7324b889SAlice Ryhl             const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
354*7324b889SAlice Ryhl 
355*7324b889SAlice Ryhl             #[inline]
356*7324b889SAlice Ryhl             unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
357*7324b889SAlice Ryhl                 // SAFETY: The caller promises that the pointer is not dangling.
358*7324b889SAlice Ryhl                 unsafe {
359*7324b889SAlice Ryhl                     ::core::ptr::addr_of_mut!((*ptr).$field)
360*7324b889SAlice Ryhl                 }
361*7324b889SAlice Ryhl             }
362*7324b889SAlice Ryhl         }
363*7324b889SAlice Ryhl     )*};
364*7324b889SAlice Ryhl }
365*7324b889SAlice Ryhl 
36603394130SWedson Almeida Filho /// Returns the system work queue (`system_wq`).
36703394130SWedson Almeida Filho ///
36803394130SWedson Almeida Filho /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU multi-threaded. There are
36903394130SWedson Almeida Filho /// users which expect relatively short queue flush time.
37003394130SWedson Almeida Filho ///
37103394130SWedson Almeida Filho /// Callers shouldn't queue work items which can run for too long.
37203394130SWedson Almeida Filho pub fn system() -> &'static Queue {
37303394130SWedson Almeida Filho     // SAFETY: `system_wq` is a C global, always available.
37403394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_wq) }
37503394130SWedson Almeida Filho }
37603394130SWedson Almeida Filho 
37703394130SWedson Almeida Filho /// Returns the system high-priority work queue (`system_highpri_wq`).
37803394130SWedson Almeida Filho ///
37903394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but for work items which require higher
38003394130SWedson Almeida Filho /// scheduling priority.
38103394130SWedson Almeida Filho pub fn system_highpri() -> &'static Queue {
38203394130SWedson Almeida Filho     // SAFETY: `system_highpri_wq` is a C global, always available.
38303394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_highpri_wq) }
38403394130SWedson Almeida Filho }
38503394130SWedson Almeida Filho 
38603394130SWedson Almeida Filho /// Returns the system work queue for potentially long-running work items (`system_long_wq`).
38703394130SWedson Almeida Filho ///
38803394130SWedson Almeida Filho /// It is similar to the one returned by [`system`] but may host long running work items. Queue
38903394130SWedson Almeida Filho /// flushing might take relatively long.
39003394130SWedson Almeida Filho pub fn system_long() -> &'static Queue {
39103394130SWedson Almeida Filho     // SAFETY: `system_long_wq` is a C global, always available.
39203394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_long_wq) }
39303394130SWedson Almeida Filho }
39403394130SWedson Almeida Filho 
39503394130SWedson Almeida Filho /// Returns the system unbound work queue (`system_unbound_wq`).
39603394130SWedson Almeida Filho ///
39703394130SWedson Almeida Filho /// Workers are not bound to any specific CPU, not concurrency managed, and all queued work items
39803394130SWedson Almeida Filho /// are executed immediately as long as `max_active` limit is not reached and resources are
39903394130SWedson Almeida Filho /// available.
40003394130SWedson Almeida Filho pub fn system_unbound() -> &'static Queue {
40103394130SWedson Almeida Filho     // SAFETY: `system_unbound_wq` is a C global, always available.
40203394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_unbound_wq) }
40303394130SWedson Almeida Filho }
40403394130SWedson Almeida Filho 
40503394130SWedson Almeida Filho /// Returns the system freezable work queue (`system_freezable_wq`).
40603394130SWedson Almeida Filho ///
40703394130SWedson Almeida Filho /// It is equivalent to the one returned by [`system`] except that it's freezable.
40803394130SWedson Almeida Filho ///
40903394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
41003394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
41103394130SWedson Almeida Filho pub fn system_freezable() -> &'static Queue {
41203394130SWedson Almeida Filho     // SAFETY: `system_freezable_wq` is a C global, always available.
41303394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_wq) }
41403394130SWedson Almeida Filho }
41503394130SWedson Almeida Filho 
41603394130SWedson Almeida Filho /// Returns the system power-efficient work queue (`system_power_efficient_wq`).
41703394130SWedson Almeida Filho ///
41803394130SWedson Almeida Filho /// It is inclined towards saving power and is converted to "unbound" variants if the
41903394130SWedson Almeida Filho /// `workqueue.power_efficient` kernel parameter is specified; otherwise, it is similar to the one
42003394130SWedson Almeida Filho /// returned by [`system`].
42103394130SWedson Almeida Filho pub fn system_power_efficient() -> &'static Queue {
42203394130SWedson Almeida Filho     // SAFETY: `system_power_efficient_wq` is a C global, always available.
42303394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
42403394130SWedson Almeida Filho }
42503394130SWedson Almeida Filho 
42603394130SWedson Almeida Filho /// Returns the system freezable power-efficient work queue (`system_freezable_power_efficient_wq`).
42703394130SWedson Almeida Filho ///
42803394130SWedson Almeida Filho /// It is similar to the one returned by [`system_power_efficient`] except that is freezable.
42903394130SWedson Almeida Filho ///
43003394130SWedson Almeida Filho /// A freezable workqueue participates in the freeze phase of the system suspend operations. Work
43103394130SWedson Almeida Filho /// items on the workqueue are drained and no new work item starts execution until thawed.
43203394130SWedson Almeida Filho pub fn system_freezable_power_efficient() -> &'static Queue {
43303394130SWedson Almeida Filho     // SAFETY: `system_freezable_power_efficient_wq` is a C global, always available.
43403394130SWedson Almeida Filho     unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
43503394130SWedson Almeida Filho }
436