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