| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 |
|
| 188 | use crate::{
|
| 189 | alloc::{AllocError, Flags},
|
| 190 | container_of,
|
| 191 | prelude::*,
|
| 192 | sync::{
|
| 193 | aref::{
|
| 194 | ARef,
|
| 195 | AlwaysRefCounted, |
| 196 | },
|
| 197 | Arc,
|
| 198 | LockClassKey, |
| 199 | },
|
| 200 | time::Jiffies,
|
| 201 | types::Opaque,
|
| 202 | };
|
| 203 | use core::{marker::PhantomData, ptr::NonNull};
|
| 204 |
|
| 205 | |
| 206 | #[macro_export]
|
| 207 | macro_rules! new_work {
|
| 208 | ($($name:literal)?) => {
|
| 209 | $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
|
| 210 | };
|
| 211 | }
|
| 212 | pub use new_work;
|
| 213 |
|
| 214 | /// Creates a [`DelayedWork`] initialiser with the given name and a newly-created lock class.
|
| 215 | #[macro_export]
|
| 216 | macro_rules! new_delayed_work {
|
| 217 | () => {
|
| 218 | $crate::workqueue::DelayedWork::new(
|
| 219 | $crate::optional_name!(),
|
| 220 | $crate::static_lock_class!(),
|
| 221 | $crate::c_str!(::core::concat!(
|
| 222 | ::core::file!(),
|
| 223 | ":",
|
| 224 | ::core::line!(),
|
| 225 | "_timer"
|
| 226 | )),
|
| 227 | $crate::static_lock_class!(),
|
| 228 | )
|
| 229 | };
|
| 230 | ($name:literal) => {
|
| 231 | $crate::workqueue::DelayedWork::new(
|
| 232 | $crate::c_str!($name),
|
| 233 | $crate::static_lock_class!(),
|
| 234 | $crate::c_str!(::core::concat!($name, "_timer")),
|
| 235 | $crate::static_lock_class!(),
|
| 236 | )
|
| 237 | };
|
| 238 | }
|
| 239 | pub use new_delayed_work;
|
| 240 |
|
| 241 | /// A kernel work queue.
|
| 242 | ///
|
| 243 | /// Wraps the kernel's C `struct workqueue_struct`.
|
| 244 | ///
|
| 245 | /// It allows work items to be queued to run on thread pools managed by the kernel. Several are
|
| 246 | /// always available, for example, `system`, `system_highpri`, `system_long`, etc.
|
| 247 | #[repr(transparent)]
|
| 248 | pub struct Queue(Opaque<bindings::workqueue_struct>);
|
| 249 |
|
| 250 | // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
|
| 251 | unsafe impl Send for Queue {}
|
| 252 | // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
|
| 253 | unsafe impl Sync for Queue {}
|
| 254 |
|
| 255 | impl Queue {
|
| 256 | /// Use the provided `struct workqueue_struct` with Rust.
|
| 257 | ///
|
| 258 | /// # Safety
|
| 259 | ///
|
| 260 | /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
|
| 261 | /// valid workqueue, and that it remains valid until the end of `'a`.
|
| 262 | pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
|
| 263 | // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
|
| 264 | // caller promises that the pointer is not dangling.
|
| 265 | unsafe { &*ptr.cast::<Queue>() }
|
| 266 | }
|
| 267 |
|
| 268 | /// Enqueues a work item.
|
| 269 | ///
|
| 270 | /// This may fail if the work item is already enqueued in a workqueue.
|
| 271 | ///
|
| 272 | /// The work item will be submitted using `WORK_CPU_UNBOUND`.
|
| 273 | pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
|
| 274 | where
|
| 275 | W: RawWorkItem<ID> + Send + 'static,
|
| 276 | {
|
| 277 | let queue_ptr = self.0.get();
|
| 278 |
|
| 279 | // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
|
| 280 | // `__enqueue` requirements are not relevant since `W` is `Send` and static.
|
| 281 | //
|
| 282 | // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
|
| 283 | // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
|
| 284 | // closure.
|
| 285 | //
|
| 286 | // Furthermore, if the C workqueue code accesses the pointer after this call to
|
| 287 | // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
|
| 288 | // will have returned true. In this case, `__enqueue` promises that the raw pointer will
|
| 289 | // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
|
| 290 | unsafe {
|
| 291 | w.__enqueue(move |work_ptr| {
|
| 292 | bindings::queue_work_on(
|
| 293 | bindings::wq_misc_consts_WORK_CPU_UNBOUND as ffi::c_int,
|
| 294 | queue_ptr,
|
| 295 | work_ptr,
|
| 296 | )
|
| 297 | })
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | /// Enqueues a delayed work item.
|
| 302 | ///
|
| 303 | /// This may fail if the work item is already enqueued in a workqueue.
|
| 304 | ///
|
| 305 | /// The work item will be submitted using `WORK_CPU_UNBOUND`.
|
| 306 | pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::EnqueueOutput
|
| 307 | where
|
| 308 | W: RawDelayedWorkItem<ID> + Send + 'static,
|
| 309 | {
|
| 310 | let queue_ptr = self.0.get();
|
| 311 |
|
| 312 | // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
|
| 313 | // `__enqueue` requirements are not relevant since `W` is `Send` and static.
|
| 314 | //
|
| 315 | // The call to `bindings::queue_delayed_work_on` will dereference the provided raw pointer,
|
| 316 | // which is ok because `__enqueue` guarantees that the pointer is valid for the duration of
|
| 317 | // this closure, and the safety requirements of `RawDelayedWorkItem` expands this
|
| 318 | // requirement to apply to the entire `delayed_work`.
|
| 319 | //
|
| 320 | // Furthermore, if the C workqueue code accesses the pointer after this call to
|
| 321 | // `__enqueue`, then the work item was successfully enqueued, and
|
| 322 | // `bindings::queue_delayed_work_on` will have returned true. In this case, `__enqueue`
|
| 323 | // promises that the raw pointer will stay valid until we call the function pointer in the
|
| 324 | // `work_struct`, so the access is ok.
|
| 325 | unsafe {
|
| 326 | w.__enqueue(move |work_ptr| {
|
| 327 | bindings::queue_delayed_work_on(
|
| 328 | bindings::wq_misc_consts_WORK_CPU_UNBOUND as ffi::c_int,
|
| 329 | queue_ptr,
|
| 330 | container_of!(work_ptr, bindings::delayed_work, work),
|
| 331 | delay,
|
| 332 | )
|
| 333 | })
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | /// Tries to spawn the given function or closure as a work item.
|
| 338 | ///
|
| 339 | /// This method can fail because it allocates memory to store the work item.
|
| 340 | pub fn try_spawn<T: 'static + Send + FnOnce()>(
|
| 341 | &self,
|
| 342 | flags: Flags,
|
| 343 | func: T,
|
| 344 | ) -> Result<(), AllocError> {
|
| 345 | let init = pin_init!(ClosureWork {
|
| 346 | work <- new_work!("Queue::try_spawn"),
|
| 347 | func: Some(func),
|
| 348 | });
|
| 349 |
|
| 350 | self.enqueue(KBox::pin_init(init, flags).map_err(|_| AllocError)?);
|
| 351 | Ok(())
|
| 352 | }
|
| 353 | }
|
| 354 |
|
| 355 | /// A helper type used in [`try_spawn`].
|
| 356 | ///
|
| 357 | /// [`try_spawn`]: Queue::try_spawn
|
| 358 | #[pin_data]
|
| 359 | struct ClosureWork<T> {
|
| 360 | #[pin]
|
| 361 | work: Work<ClosureWork<T>>,
|
| 362 | func: Option<T>,
|
| 363 | }
|
| 364 |
|
| 365 | impl<T: FnOnce()> WorkItem for ClosureWork<T> {
|
| 366 | type Pointer = Pin<KBox<Self>>;
|
| 367 |
|
| 368 | fn run(mut this: Pin<KBox<Self>>) {
|
| 369 | if let Some(func) = this.as_mut().project().func.take() {
|
| 370 | (func)()
|
| 371 | }
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | /// A raw work item.
|
| 376 | ///
|
| 377 | /// This is the low-level trait that is designed for being as general as possible.
|
| 378 | ///
|
| 379 | /// The `ID` parameter to this trait exists so that a single type can provide multiple
|
| 380 | /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
|
| 381 | /// you will implement this trait once for each field, using a different id for each field. The
|
| 382 | /// actual value of the id is not important as long as you use different ids for different fields
|
| 383 | /// of the same struct. (Fields of different structs need not use different ids.)
|
| 384 | ///
|
| 385 | /// Note that the id is used only to select the right method to call during compilation. It won't be
|
| 386 | /// part of the final executable.
|
| 387 | ///
|
| 388 | /// # Safety
|
| 389 | ///
|
| 390 | /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`]
|
| 391 | /// remain valid for the duration specified in the guarantees section of the documentation for
|
| 392 | /// [`__enqueue`].
|
| 393 | ///
|
| 394 | /// [`__enqueue`]: RawWorkItem::__enqueue
|
| 395 | pub unsafe trait RawWorkItem<const ID: u64> {
|
| 396 | /// The return type of [`Queue::enqueue`].
|
| 397 | type EnqueueOutput;
|
| 398 |
|
| 399 | /// Enqueues this work item on a queue using the provided `queue_work_on` method.
|
| 400 | ///
|
| 401 | /// # Guarantees
|
| 402 | ///
|
| 403 | /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
|
| 404 | /// valid `work_struct` for the duration of the call to the closure. If the closure returns
|
| 405 | /// true, then it is further guaranteed that the pointer remains valid until someone calls the
|
| 406 | /// function pointer stored in the `work_struct`.
|
| 407 | ///
|
| 408 | /// # Safety
|
| 409 | ///
|
| 410 | /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
|
| 411 | ///
|
| 412 | /// If the work item type is annotated with any lifetimes, then you must not call the function
|
| 413 | /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
|
| 414 | ///
|
| 415 | /// If the work item type is not [`Send`], then the function pointer must be called on the same
|
| 416 | /// thread as the call to `__enqueue`.
|
| 417 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 418 | where
|
| 419 | F: FnOnce(*mut bindings::work_struct) -> bool;
|
| 420 | }
|
| 421 |
|
| 422 | /// A raw delayed work item.
|
| 423 | ///
|
| 424 | /// # Safety
|
| 425 | ///
|
| 426 | /// If the `__enqueue` method in the `RawWorkItem` implementation calls the closure, then the
|
| 427 | /// provided pointer must point at the `work` field of a valid `delayed_work`, and the guarantees
|
| 428 | /// that `__enqueue` provides about accessing the `work_struct` must also apply to the rest of the
|
| 429 | /// `delayed_work` struct.
|
| 430 | pub unsafe trait RawDelayedWorkItem<const ID: u64>: RawWorkItem<ID> {}
|
| 431 |
|
| 432 | /// Defines the method that should be called directly when a work item is executed.
|
| 433 | ///
|
| 434 | /// This trait is implemented by `Pin<KBox<T>>`, [`Arc<T>`] and [`ARef<T>`], and
|
| 435 | /// is mainly intended to be implemented for smart pointer types. For your own
|
| 436 | /// structs, you would implement [`WorkItem`] instead. The [`run`] method on
|
| 437 | /// this trait will usually just perform the appropriate `container_of`
|
| 438 | /// translation and then call into the [`run`][WorkItem::run] method from the
|
| 439 | /// [`WorkItem`] trait.
|
| 440 | ///
|
| 441 | /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
|
| 442 | ///
|
| 443 | /// # Safety
|
| 444 | ///
|
| 445 | /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
|
| 446 | /// method of this trait as the function pointer.
|
| 447 | ///
|
| 448 | /// [`__enqueue`]: RawWorkItem::__enqueue
|
| 449 | /// [`run`]: WorkItemPointer::run
|
| 450 | pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
|
| 451 | /// Run this work item.
|
| 452 | ///
|
| 453 | /// # Safety
|
| 454 | ///
|
| 455 | /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`]
|
| 456 | /// where the `queue_work_on` closure returned true, and the pointer must still be valid.
|
| 457 | ///
|
| 458 | /// [`__enqueue`]: RawWorkItem::__enqueue
|
| 459 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
|
| 460 | }
|
| 461 |
|
| 462 | /// Defines the method that should be called when this work item is executed.
|
| 463 | ///
|
| 464 | /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
|
| 465 | pub trait WorkItem<const ID: u64 = 0> {
|
| 466 | /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
|
| 467 | /// `Pin<KBox<Self>>`.
|
| 468 | type Pointer: WorkItemPointer<ID>;
|
| 469 |
|
| 470 | /// The method that should be called when this work item is executed.
|
| 471 | fn run(this: Self::Pointer);
|
| 472 | }
|
| 473 |
|
| 474 | /// Links for a work item.
|
| 475 | ///
|
| 476 | /// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`]
|
| 477 | /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
|
| 478 | ///
|
| 479 | /// Wraps the kernel's C `struct work_struct`.
|
| 480 | ///
|
| 481 | /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
|
| 482 | ///
|
| 483 | /// [`run`]: WorkItemPointer::run
|
| 484 | #[pin_data]
|
| 485 | #[repr(transparent)]
|
| 486 | pub struct Work<T: ?Sized, const ID: u64 = 0> {
|
| 487 | #[pin]
|
| 488 | work: Opaque<bindings::work_struct>,
|
| 489 | _inner: PhantomData<T>,
|
| 490 | }
|
| 491 |
|
| 492 | // SAFETY: Kernel work items are usable from any thread.
|
| 493 | //
|
| 494 | // We do not need to constrain `T` since the work item does not actually contain a `T`.
|
| 495 | unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
|
| 496 | // SAFETY: Kernel work items are usable from any thread.
|
| 497 | //
|
| 498 | // We do not need to constrain `T` since the work item does not actually contain a `T`.
|
| 499 | unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
|
| 500 |
|
| 501 | impl<T: ?Sized, const ID: u64> Work<T, ID> {
|
| 502 | /// Creates a new instance of [`Work`].
|
| 503 | #[inline]
|
| 504 | pub fn new(name: &'static CStr, key: Pin<&'static LockClassKey>) -> impl PinInit<Self>
|
| 505 | where
|
| 506 | T: WorkItem<ID>,
|
| 507 | {
|
| 508 | pin_init!(Self {
|
| 509 | work <- Opaque::ffi_init(|slot| {
|
| 510 | // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as
|
| 511 | // the work item function.
|
| 512 | unsafe {
|
| 513 | bindings::init_work_with_key(
|
| 514 | slot,
|
| 515 | Some(T::Pointer::run),
|
| 516 | false,
|
| 517 | name.as_char_ptr(),
|
| 518 | key.as_ptr(),
|
| 519 | )
|
| 520 | }
|
| 521 | }),
|
| 522 | _inner: PhantomData,
|
| 523 | })
|
| 524 | }
|
| 525 |
|
| 526 | /// Get a pointer to the inner `work_struct`.
|
| 527 | ///
|
| 528 | /// # Safety
|
| 529 | ///
|
| 530 | /// The provided pointer must not be dangling and must be properly aligned. (But the memory
|
| 531 | /// need not be initialized.)
|
| 532 | #[inline]
|
| 533 | pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
|
| 534 | // SAFETY: The caller promises that the pointer is aligned and not dangling.
|
| 535 | //
|
| 536 | // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
|
| 537 | // the compiler does not complain that the `work` field is unused.
|
| 538 | unsafe { Opaque::cast_into(core::ptr::addr_of!((*ptr).work)) }
|
| 539 | }
|
| 540 | }
|
| 541 |
|
| 542 | /// Declares that a type contains a [`Work<T, ID>`].
|
| 543 | ///
|
| 544 | /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
|
| 545 | /// like this:
|
| 546 | ///
|
| 547 | /// ```no_run
|
| 548 | /// use kernel::workqueue::{impl_has_work, Work};
|
| 549 | ///
|
| 550 | /// struct MyWorkItem {
|
| 551 | /// work_field: Work<MyWorkItem, 1>,
|
| 552 | /// }
|
| 553 | ///
|
| 554 | /// impl_has_work! {
|
| 555 | /// impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
|
| 556 | /// }
|
| 557 | /// ```
|
| 558 | ///
|
| 559 | /// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct`
|
| 560 | /// fields by using a different id for each one.
|
| 561 | ///
|
| 562 | /// # Safety
|
| 563 | ///
|
| 564 | /// The methods [`raw_get_work`] and [`work_container_of`] must return valid pointers and must be
|
| 565 | /// true inverses of each other; that is, they must satisfy the following invariants:
|
| 566 | /// - `work_container_of(raw_get_work(ptr)) == ptr` for any `ptr: *mut Self`.
|
| 567 | /// - `raw_get_work(work_container_of(ptr)) == ptr` for any `ptr: *mut Work<T, ID>`.
|
| 568 | ///
|
| 569 | /// [`impl_has_work!`]: crate::impl_has_work
|
| 570 | /// [`raw_get_work`]: HasWork::raw_get_work
|
| 571 | /// [`work_container_of`]: HasWork::work_container_of
|
| 572 | pub unsafe trait HasWork<T, const ID: u64 = 0> {
|
| 573 | /// Returns a pointer to the [`Work<T, ID>`] field.
|
| 574 | ///
|
| 575 | /// # Safety
|
| 576 | ///
|
| 577 | /// The provided pointer must point at a valid struct of type `Self`.
|
| 578 | unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID>;
|
| 579 |
|
| 580 | /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
|
| 581 | ///
|
| 582 | /// # Safety
|
| 583 | ///
|
| 584 | /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
|
| 585 | unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self;
|
| 586 | }
|
| 587 |
|
| 588 | /// Used to safely implement the [`HasWork<T, ID>`] trait.
|
| 589 | ///
|
| 590 | /// # Examples
|
| 591 | ///
|
| 592 | /// ```
|
| 593 | /// use kernel::sync::Arc;
|
| 594 | /// use kernel::workqueue::{self, impl_has_work, Work};
|
| 595 | ///
|
| 596 | /// struct MyStruct<'a, T, const N: usize> {
|
| 597 | /// work_field: Work<MyStruct<'a, T, N>, 17>,
|
| 598 | /// f: fn(&'a [T; N]),
|
| 599 | /// }
|
| 600 | ///
|
| 601 | /// impl_has_work! {
|
| 602 | /// impl{'a, T, const N: usize} HasWork<MyStruct<'a, T, N>, 17>
|
| 603 | /// for MyStruct<'a, T, N> { self.work_field }
|
| 604 | /// }
|
| 605 | /// ```
|
| 606 | #[macro_export]
|
| 607 | macro_rules! impl_has_work {
|
| 608 | ($(impl$({$($generics:tt)*})?
|
| 609 | HasWork<$work_type:ty $(, $id:tt)?>
|
| 610 | for $self:ty
|
| 611 | { self.$field:ident }
|
| 612 | )*) => {$(
|
| 613 | |
| 614 | |
| 615 | unsafe impl$(<$($generics)+>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self {
|
| 616 | #[inline]
|
| 617 | unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
|
| 618 | |
| 619 | unsafe {
|
| 620 | ::core::ptr::addr_of_mut!((*ptr).$field)
|
| 621 | }
|
| 622 | }
|
| 623 |
|
| 624 | #[inline]
|
| 625 | unsafe fn work_container_of(
|
| 626 | ptr: *mut $crate::workqueue::Work<$work_type $(, $id)?>,
|
| 627 | ) -> *mut Self {
|
| 628 | |
| 629 | |
| 630 | unsafe { $crate::container_of!(ptr, Self, $field) }
|
| 631 | }
|
| 632 | }
|
| 633 | )*};
|
| 634 | }
|
| 635 | pub use impl_has_work;
|
| 636 |
|
| 637 | impl_has_work! {
|
| 638 | impl{T} HasWork<Self> for ClosureWork<T> { self.work }
|
| 639 | }
|
| 640 |
|
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | #[pin_data]
|
| 653 | #[repr(transparent)]
|
| 654 | pub struct DelayedWork<T: ?Sized, const ID: u64 = 0> {
|
| 655 | #[pin]
|
| 656 | dwork: Opaque<bindings::delayed_work>,
|
| 657 | _inner: PhantomData<T>,
|
| 658 | }
|
| 659 |
|
| 660 | |
| 661 | |
| 662 | |
| 663 | unsafe impl<T: ?Sized, const ID: u64> Send for DelayedWork<T, ID> {}
|
| 664 | |
| 665 | |
| 666 | |
| 667 | unsafe impl<T: ?Sized, const ID: u64> Sync for DelayedWork<T, ID> {}
|
| 668 |
|
| 669 | impl<T: ?Sized, const ID: u64> DelayedWork<T, ID> {
|
| 670 | |
| 671 | #[inline]
|
| 672 | pub fn new(
|
| 673 | work_name: &'static CStr,
|
| 674 | work_key: Pin<&'static LockClassKey>,
|
| 675 | timer_name: &'static CStr,
|
| 676 | timer_key: Pin<&'static LockClassKey>,
|
| 677 | ) -> impl PinInit<Self>
|
| 678 | where
|
| 679 | T: WorkItem<ID>,
|
| 680 | {
|
| 681 | pin_init!(Self {
|
| 682 | dwork <- Opaque::ffi_init(|slot: *mut bindings::delayed_work| {
|
| 683 | |
| 684 | |
| 685 | unsafe {
|
| 686 | bindings::init_work_with_key(
|
| 687 | core::ptr::addr_of_mut!((*slot).work),
|
| 688 | Some(T::Pointer::run),
|
| 689 | false,
|
| 690 | work_name.as_char_ptr(),
|
| 691 | work_key.as_ptr(),
|
| 692 | )
|
| 693 | }
|
| 694 |
|
| 695 | |
| 696 | |
| 697 | |
| 698 | unsafe {
|
| 699 | bindings::timer_init_key(
|
| 700 | core::ptr::addr_of_mut!((*slot).timer),
|
| 701 | Some(bindings::delayed_work_timer_fn),
|
| 702 | bindings::TIMER_IRQSAFE,
|
| 703 | timer_name.as_char_ptr(),
|
| 704 | timer_key.as_ptr(),
|
| 705 | )
|
| 706 | }
|
| 707 | }),
|
| 708 | _inner: PhantomData,
|
| 709 | })
|
| 710 | }
|
| 711 |
|
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | #[inline]
|
| 719 | pub unsafe fn raw_as_work(ptr: *const Self) -> *mut Work<T, ID> {
|
| 720 | |
| 721 | let dw: *mut bindings::delayed_work =
|
| 722 | unsafe { Opaque::cast_into(core::ptr::addr_of!((*ptr).dwork)) };
|
| 723 | |
| 724 | let wrk: *mut bindings::work_struct = unsafe { core::ptr::addr_of_mut!((*dw).work) };
|
| 725 | |
| 726 | wrk.cast()
|
| 727 | }
|
| 728 | }
|
| 729 |
|
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | pub unsafe trait HasDelayedWork<T, const ID: u64 = 0>: HasWork<T, ID> {}
|
| 737 |
|
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | #[macro_export]
|
| 760 | macro_rules! impl_has_delayed_work {
|
| 761 | ($(impl$({$($generics:tt)*})?
|
| 762 | HasDelayedWork<$work_type:ty $(, $id:tt)?>
|
| 763 | for $self:ty
|
| 764 | { self.$field:ident }
|
| 765 | )*) => {$(
|
| 766 | |
| 767 | |
| 768 | unsafe impl$(<$($generics)+>)?
|
| 769 | $crate::workqueue::HasDelayedWork<$work_type $(, $id)?> for $self {}
|
| 770 |
|
| 771 | |
| 772 | |
| 773 | unsafe impl$(<$($generics)+>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self {
|
| 774 | #[inline]
|
| 775 | unsafe fn raw_get_work(
|
| 776 | ptr: *mut Self
|
| 777 | ) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
|
| 778 | |
| 779 | let ptr: *mut $crate::workqueue::DelayedWork<$work_type $(, $id)?> = unsafe {
|
| 780 | ::core::ptr::addr_of_mut!((*ptr).$field)
|
| 781 | };
|
| 782 |
|
| 783 | |
| 784 | unsafe { $crate::workqueue::DelayedWork::raw_as_work(ptr) }
|
| 785 | }
|
| 786 |
|
| 787 | #[inline]
|
| 788 | unsafe fn work_container_of(
|
| 789 | ptr: *mut $crate::workqueue::Work<$work_type $(, $id)?>,
|
| 790 | ) -> *mut Self {
|
| 791 | |
| 792 | |
| 793 | let ptr = unsafe { $crate::workqueue::Work::raw_get(ptr) };
|
| 794 |
|
| 795 | |
| 796 | |
| 797 | let delayed_work = unsafe {
|
| 798 | $crate::container_of!(ptr, $crate::bindings::delayed_work, work)
|
| 799 | };
|
| 800 |
|
| 801 | let delayed_work: *mut $crate::workqueue::DelayedWork<$work_type $(, $id)?> =
|
| 802 | delayed_work.cast();
|
| 803 |
|
| 804 | |
| 805 | |
| 806 | unsafe { $crate::container_of!(delayed_work, Self, $field) }
|
| 807 | }
|
| 808 | }
|
| 809 | )*};
|
| 810 | }
|
| 811 | pub use impl_has_delayed_work;
|
| 812 |
|
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>
|
| 823 | where
|
| 824 | T: WorkItem<ID, Pointer = Self>,
|
| 825 | T: HasWork<T, ID>,
|
| 826 | {
|
| 827 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
|
| 828 | |
| 829 | let ptr = ptr.cast::<Work<T, ID>>();
|
| 830 | |
| 831 | let ptr = unsafe { T::work_container_of(ptr) };
|
| 832 | |
| 833 | let arc = unsafe { Arc::from_raw(ptr) };
|
| 834 |
|
| 835 | T::run(arc)
|
| 836 | }
|
| 837 | }
|
| 838 |
|
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T>
|
| 847 | where
|
| 848 | T: WorkItem<ID, Pointer = Self>,
|
| 849 | T: HasWork<T, ID>,
|
| 850 | {
|
| 851 | type EnqueueOutput = Result<(), Self>;
|
| 852 |
|
| 853 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 854 | where
|
| 855 | F: FnOnce(*mut bindings::work_struct) -> bool,
|
| 856 | {
|
| 857 | |
| 858 | let ptr = Arc::into_raw(self).cast_mut();
|
| 859 |
|
| 860 | |
| 861 | let work_ptr = unsafe { T::raw_get_work(ptr) };
|
| 862 | |
| 863 | let work_ptr = unsafe { Work::raw_get(work_ptr) };
|
| 864 |
|
| 865 | if queue_work_on(work_ptr) {
|
| 866 | Ok(())
|
| 867 | } else {
|
| 868 | |
| 869 | Err(unsafe { Arc::from_raw(ptr) })
|
| 870 | }
|
| 871 | }
|
| 872 | }
|
| 873 |
|
| 874 | |
| 875 | |
| 876 | |
| 877 | unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Arc<T>
|
| 878 | where
|
| 879 | T: WorkItem<ID, Pointer = Self>,
|
| 880 | T: HasDelayedWork<T, ID>,
|
| 881 | {
|
| 882 | }
|
| 883 |
|
| 884 | |
| 885 | unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<KBox<T>>
|
| 886 | where
|
| 887 | T: WorkItem<ID, Pointer = Self>,
|
| 888 | T: HasWork<T, ID>,
|
| 889 | {
|
| 890 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
|
| 891 | |
| 892 | let ptr = ptr.cast::<Work<T, ID>>();
|
| 893 | |
| 894 | let ptr = unsafe { T::work_container_of(ptr) };
|
| 895 | |
| 896 | let boxed = unsafe { KBox::from_raw(ptr) };
|
| 897 | |
| 898 | let pinned = unsafe { Pin::new_unchecked(boxed) };
|
| 899 |
|
| 900 | T::run(pinned)
|
| 901 | }
|
| 902 | }
|
| 903 |
|
| 904 | |
| 905 | unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<KBox<T>>
|
| 906 | where
|
| 907 | T: WorkItem<ID, Pointer = Self>,
|
| 908 | T: HasWork<T, ID>,
|
| 909 | {
|
| 910 | type EnqueueOutput = ();
|
| 911 |
|
| 912 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 913 | where
|
| 914 | F: FnOnce(*mut bindings::work_struct) -> bool,
|
| 915 | {
|
| 916 | |
| 917 | |
| 918 | let boxed = unsafe { Pin::into_inner_unchecked(self) };
|
| 919 | let ptr = KBox::into_raw(boxed);
|
| 920 |
|
| 921 | |
| 922 | let work_ptr = unsafe { T::raw_get_work(ptr) };
|
| 923 | |
| 924 | let work_ptr = unsafe { Work::raw_get(work_ptr) };
|
| 925 |
|
| 926 | if !queue_work_on(work_ptr) {
|
| 927 | |
| 928 | |
| 929 | unsafe { ::core::hint::unreachable_unchecked() }
|
| 930 | }
|
| 931 | }
|
| 932 | }
|
| 933 |
|
| 934 | |
| 935 | |
| 936 | |
| 937 | unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for Pin<KBox<T>>
|
| 938 | where
|
| 939 | T: WorkItem<ID, Pointer = Self>,
|
| 940 | T: HasDelayedWork<T, ID>,
|
| 941 | {
|
| 942 | }
|
| 943 |
|
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | unsafe impl<T, const ID: u64> WorkItemPointer<ID> for ARef<T>
|
| 956 | where
|
| 957 | T: AlwaysRefCounted,
|
| 958 | T: WorkItem<ID, Pointer = Self>,
|
| 959 | T: HasWork<T, ID>,
|
| 960 | {
|
| 961 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
|
| 962 | |
| 963 | let ptr = ptr.cast::<Work<T, ID>>();
|
| 964 |
|
| 965 | |
| 966 | |
| 967 | let ptr = unsafe { T::work_container_of(ptr) };
|
| 968 |
|
| 969 | |
| 970 | |
| 971 | let ptr = unsafe { NonNull::new_unchecked(ptr) };
|
| 972 |
|
| 973 | |
| 974 | |
| 975 | let aref = unsafe { ARef::from_raw(ptr) };
|
| 976 |
|
| 977 | T::run(aref)
|
| 978 | }
|
| 979 | }
|
| 980 |
|
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | unsafe impl<T, const ID: u64> RawWorkItem<ID> for ARef<T>
|
| 989 | where
|
| 990 | T: AlwaysRefCounted,
|
| 991 | T: WorkItem<ID, Pointer = Self>,
|
| 992 | T: HasWork<T, ID>,
|
| 993 | {
|
| 994 | type EnqueueOutput = Result<(), Self>;
|
| 995 |
|
| 996 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 997 | where
|
| 998 | F: FnOnce(*mut bindings::work_struct) -> bool,
|
| 999 | {
|
| 1000 | let ptr = ARef::into_raw(self);
|
| 1001 |
|
| 1002 | |
| 1003 | let work_ptr = unsafe { T::raw_get_work(ptr.as_ptr()) };
|
| 1004 | |
| 1005 | let work_ptr = unsafe { Work::raw_get(work_ptr) };
|
| 1006 |
|
| 1007 | if queue_work_on(work_ptr) {
|
| 1008 | Ok(())
|
| 1009 | } else {
|
| 1010 | |
| 1011 | Err(unsafe { ARef::from_raw(ptr) })
|
| 1012 | }
|
| 1013 | }
|
| 1014 | }
|
| 1015 |
|
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | unsafe impl<T, const ID: u64> RawDelayedWorkItem<ID> for ARef<T>
|
| 1020 | where
|
| 1021 | T: WorkItem<ID, Pointer = Self>,
|
| 1022 | T: HasDelayedWork<T, ID>,
|
| 1023 | T: AlwaysRefCounted,
|
| 1024 | {
|
| 1025 | }
|
| 1026 |
|
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | pub fn system() -> &'static Queue {
|
| 1034 | |
| 1035 | unsafe { Queue::from_raw(bindings::system_wq) }
|
| 1036 | }
|
| 1037 |
|
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | pub fn system_highpri() -> &'static Queue {
|
| 1043 | |
| 1044 | unsafe { Queue::from_raw(bindings::system_highpri_wq) }
|
| 1045 | }
|
| 1046 |
|
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | pub fn system_long() -> &'static Queue {
|
| 1052 | |
| 1053 | unsafe { Queue::from_raw(bindings::system_long_wq) }
|
| 1054 | }
|
| 1055 |
|
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | pub fn system_unbound() -> &'static Queue {
|
| 1062 | |
| 1063 | unsafe { Queue::from_raw(bindings::system_unbound_wq) }
|
| 1064 | }
|
| 1065 |
|
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | pub fn system_freezable() -> &'static Queue {
|
| 1073 | |
| 1074 | unsafe { Queue::from_raw(bindings::system_freezable_wq) }
|
| 1075 | }
|
| 1076 |
|
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | pub fn system_power_efficient() -> &'static Queue {
|
| 1083 | |
| 1084 | unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
|
| 1085 | }
|
| 1086 |
|
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | pub fn system_freezable_power_efficient() -> &'static Queue {
|
| 1094 | |
| 1095 | unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
|
| 1096 | }
|
| 1097 |
|
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | pub fn system_bh() -> &'static Queue {
|
| 1103 | |
| 1104 | unsafe { Queue::from_raw(bindings::system_bh_wq) }
|
| 1105 | }
|
| 1106 |
|
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | pub fn system_bh_highpri() -> &'static Queue {
|
| 1112 | |
| 1113 | unsafe { Queue::from_raw(bindings::system_bh_highpri_wq) }
|
| 1114 | }
|