1*d4d791d4SAlice Ryhl // SPDX-License-Identifier: GPL-2.0 2*d4d791d4SAlice Ryhl 3*d4d791d4SAlice Ryhl //! Work queues. 4*d4d791d4SAlice Ryhl //! 5*d4d791d4SAlice Ryhl //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h) 6*d4d791d4SAlice Ryhl 7*d4d791d4SAlice Ryhl use crate::{bindings, types::Opaque}; 8*d4d791d4SAlice Ryhl 9*d4d791d4SAlice Ryhl /// A kernel work queue. 10*d4d791d4SAlice Ryhl /// 11*d4d791d4SAlice Ryhl /// Wraps the kernel's C `struct workqueue_struct`. 12*d4d791d4SAlice Ryhl /// 13*d4d791d4SAlice Ryhl /// It allows work items to be queued to run on thread pools managed by the kernel. Several are 14*d4d791d4SAlice Ryhl /// always available, for example, `system`, `system_highpri`, `system_long`, etc. 15*d4d791d4SAlice Ryhl #[repr(transparent)] 16*d4d791d4SAlice Ryhl pub struct Queue(Opaque<bindings::workqueue_struct>); 17*d4d791d4SAlice Ryhl 18*d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe. 19*d4d791d4SAlice Ryhl unsafe impl Send for Queue {} 20*d4d791d4SAlice Ryhl // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe. 21*d4d791d4SAlice Ryhl unsafe impl Sync for Queue {} 22*d4d791d4SAlice Ryhl 23*d4d791d4SAlice Ryhl impl Queue { 24*d4d791d4SAlice Ryhl /// Use the provided `struct workqueue_struct` with Rust. 25*d4d791d4SAlice Ryhl /// 26*d4d791d4SAlice Ryhl /// # Safety 27*d4d791d4SAlice Ryhl /// 28*d4d791d4SAlice Ryhl /// The caller must ensure that the provided raw pointer is not dangling, that it points at a 29*d4d791d4SAlice Ryhl /// valid workqueue, and that it remains valid until the end of 'a. 30*d4d791d4SAlice Ryhl pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue { 31*d4d791d4SAlice Ryhl // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The 32*d4d791d4SAlice Ryhl // caller promises that the pointer is not dangling. 33*d4d791d4SAlice Ryhl unsafe { &*(ptr as *const Queue) } 34*d4d791d4SAlice Ryhl } 35*d4d791d4SAlice Ryhl 36*d4d791d4SAlice Ryhl /// Enqueues a work item. 37*d4d791d4SAlice Ryhl /// 38*d4d791d4SAlice Ryhl /// This may fail if the work item is already enqueued in a workqueue. 39*d4d791d4SAlice Ryhl /// 40*d4d791d4SAlice Ryhl /// The work item will be submitted using `WORK_CPU_UNBOUND`. 41*d4d791d4SAlice Ryhl pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput 42*d4d791d4SAlice Ryhl where 43*d4d791d4SAlice Ryhl W: RawWorkItem<ID> + Send + 'static, 44*d4d791d4SAlice Ryhl { 45*d4d791d4SAlice Ryhl let queue_ptr = self.0.get(); 46*d4d791d4SAlice Ryhl 47*d4d791d4SAlice Ryhl // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other 48*d4d791d4SAlice Ryhl // `__enqueue` requirements are not relevant since `W` is `Send` and static. 49*d4d791d4SAlice Ryhl // 50*d4d791d4SAlice Ryhl // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which 51*d4d791d4SAlice Ryhl // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this 52*d4d791d4SAlice Ryhl // closure. 53*d4d791d4SAlice Ryhl // 54*d4d791d4SAlice Ryhl // Furthermore, if the C workqueue code accesses the pointer after this call to 55*d4d791d4SAlice Ryhl // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on` 56*d4d791d4SAlice Ryhl // will have returned true. In this case, `__enqueue` promises that the raw pointer will 57*d4d791d4SAlice Ryhl // stay valid until we call the function pointer in the `work_struct`, so the access is ok. 58*d4d791d4SAlice Ryhl unsafe { 59*d4d791d4SAlice Ryhl w.__enqueue(move |work_ptr| { 60*d4d791d4SAlice Ryhl bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr) 61*d4d791d4SAlice Ryhl }) 62*d4d791d4SAlice Ryhl } 63*d4d791d4SAlice Ryhl } 64*d4d791d4SAlice Ryhl } 65*d4d791d4SAlice Ryhl 66*d4d791d4SAlice Ryhl /// A raw work item. 67*d4d791d4SAlice Ryhl /// 68*d4d791d4SAlice Ryhl /// This is the low-level trait that is designed for being as general as possible. 69*d4d791d4SAlice Ryhl /// 70*d4d791d4SAlice Ryhl /// The `ID` parameter to this trait exists so that a single type can provide multiple 71*d4d791d4SAlice Ryhl /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then 72*d4d791d4SAlice Ryhl /// you will implement this trait once for each field, using a different id for each field. The 73*d4d791d4SAlice Ryhl /// actual value of the id is not important as long as you use different ids for different fields 74*d4d791d4SAlice Ryhl /// of the same struct. (Fields of different structs need not use different ids.) 75*d4d791d4SAlice Ryhl /// 76*d4d791d4SAlice Ryhl /// Note that the id is used only to select the right method to call during compilation. It wont be 77*d4d791d4SAlice Ryhl /// part of the final executable. 78*d4d791d4SAlice Ryhl /// 79*d4d791d4SAlice Ryhl /// # Safety 80*d4d791d4SAlice Ryhl /// 81*d4d791d4SAlice Ryhl /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by `__enqueue` 82*d4d791d4SAlice Ryhl /// remain valid for the duration specified in the guarantees section of the documentation for 83*d4d791d4SAlice Ryhl /// `__enqueue`. 84*d4d791d4SAlice Ryhl pub unsafe trait RawWorkItem<const ID: u64> { 85*d4d791d4SAlice Ryhl /// The return type of [`Queue::enqueue`]. 86*d4d791d4SAlice Ryhl type EnqueueOutput; 87*d4d791d4SAlice Ryhl 88*d4d791d4SAlice Ryhl /// Enqueues this work item on a queue using the provided `queue_work_on` method. 89*d4d791d4SAlice Ryhl /// 90*d4d791d4SAlice Ryhl /// # Guarantees 91*d4d791d4SAlice Ryhl /// 92*d4d791d4SAlice Ryhl /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a 93*d4d791d4SAlice Ryhl /// valid `work_struct` for the duration of the call to the closure. If the closure returns 94*d4d791d4SAlice Ryhl /// true, then it is further guaranteed that the pointer remains valid until someone calls the 95*d4d791d4SAlice Ryhl /// function pointer stored in the `work_struct`. 96*d4d791d4SAlice Ryhl /// 97*d4d791d4SAlice Ryhl /// # Safety 98*d4d791d4SAlice Ryhl /// 99*d4d791d4SAlice Ryhl /// The provided closure may only return `false` if the `work_struct` is already in a workqueue. 100*d4d791d4SAlice Ryhl /// 101*d4d791d4SAlice Ryhl /// If the work item type is annotated with any lifetimes, then you must not call the function 102*d4d791d4SAlice Ryhl /// pointer after any such lifetime expires. (Never calling the function pointer is okay.) 103*d4d791d4SAlice Ryhl /// 104*d4d791d4SAlice Ryhl /// If the work item type is not [`Send`], then the function pointer must be called on the same 105*d4d791d4SAlice Ryhl /// thread as the call to `__enqueue`. 106*d4d791d4SAlice Ryhl unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput 107*d4d791d4SAlice Ryhl where 108*d4d791d4SAlice Ryhl F: FnOnce(*mut bindings::work_struct) -> bool; 109*d4d791d4SAlice Ryhl } 110