| 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 | use crate::alloc::{AllocError, Flags};
|
| 134 | use crate::{prelude::*, sync::Arc, sync::LockClassKey, types::Opaque};
|
| 135 | use core::marker::PhantomData;
|
| 136 |
|
| 137 | |
| 138 | #[macro_export]
|
| 139 | macro_rules! new_work {
|
| 140 | ($($name:literal)?) => {
|
| 141 | $crate::workqueue::Work::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
|
| 142 | };
|
| 143 | }
|
| 144 | pub use new_work;
|
| 145 |
|
| 146 | /// A kernel work queue.
|
| 147 | ///
|
| 148 | /// Wraps the kernel's C `struct workqueue_struct`.
|
| 149 | ///
|
| 150 | /// It allows work items to be queued to run on thread pools managed by the kernel. Several are
|
| 151 | /// always available, for example, `system`, `system_highpri`, `system_long`, etc.
|
| 152 | #[repr(transparent)]
|
| 153 | pub struct Queue(Opaque<bindings::workqueue_struct>);
|
| 154 |
|
| 155 | // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
|
| 156 | unsafe impl Send for Queue {}
|
| 157 | // SAFETY: Accesses to workqueues used by [`Queue`] are thread-safe.
|
| 158 | unsafe impl Sync for Queue {}
|
| 159 |
|
| 160 | impl Queue {
|
| 161 | /// Use the provided `struct workqueue_struct` with Rust.
|
| 162 | ///
|
| 163 | /// # Safety
|
| 164 | ///
|
| 165 | /// The caller must ensure that the provided raw pointer is not dangling, that it points at a
|
| 166 | /// valid workqueue, and that it remains valid until the end of `'a`.
|
| 167 | pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue {
|
| 168 | // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The
|
| 169 | // caller promises that the pointer is not dangling.
|
| 170 | unsafe { &*(ptr as *const Queue) }
|
| 171 | }
|
| 172 |
|
| 173 | /// Enqueues a work item.
|
| 174 | ///
|
| 175 | /// This may fail if the work item is already enqueued in a workqueue.
|
| 176 | ///
|
| 177 | /// The work item will be submitted using `WORK_CPU_UNBOUND`.
|
| 178 | pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
|
| 179 | where
|
| 180 | W: RawWorkItem<ID> + Send + 'static,
|
| 181 | {
|
| 182 | let queue_ptr = self.0.get();
|
| 183 |
|
| 184 | // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other
|
| 185 | // `__enqueue` requirements are not relevant since `W` is `Send` and static.
|
| 186 | //
|
| 187 | // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which
|
| 188 | // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this
|
| 189 | // closure.
|
| 190 | //
|
| 191 | // Furthermore, if the C workqueue code accesses the pointer after this call to
|
| 192 | // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on`
|
| 193 | // will have returned true. In this case, `__enqueue` promises that the raw pointer will
|
| 194 | // stay valid until we call the function pointer in the `work_struct`, so the access is ok.
|
| 195 | unsafe {
|
| 196 | w.__enqueue(move |work_ptr| {
|
| 197 | bindings::queue_work_on(
|
| 198 | bindings::wq_misc_consts_WORK_CPU_UNBOUND as _,
|
| 199 | queue_ptr,
|
| 200 | work_ptr,
|
| 201 | )
|
| 202 | })
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | /// Tries to spawn the given function or closure as a work item.
|
| 207 | ///
|
| 208 | /// This method can fail because it allocates memory to store the work item.
|
| 209 | pub fn try_spawn<T: 'static + Send + FnOnce()>(
|
| 210 | &self,
|
| 211 | flags: Flags,
|
| 212 | func: T,
|
| 213 | ) -> Result<(), AllocError> {
|
| 214 | let init = pin_init!(ClosureWork {
|
| 215 | work <- new_work!("Queue::try_spawn"),
|
| 216 | func: Some(func),
|
| 217 | });
|
| 218 |
|
| 219 | self.enqueue(Box::pin_init(init, flags).map_err(|_| AllocError)?);
|
| 220 | Ok(())
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | /// A helper type used in [`try_spawn`].
|
| 225 | ///
|
| 226 | /// [`try_spawn`]: Queue::try_spawn
|
| 227 | #[pin_data]
|
| 228 | struct ClosureWork<T> {
|
| 229 | #[pin]
|
| 230 | work: Work<ClosureWork<T>>,
|
| 231 | func: Option<T>,
|
| 232 | }
|
| 233 |
|
| 234 | impl<T> ClosureWork<T> {
|
| 235 | fn project(self: Pin<&mut Self>) -> &mut Option<T> {
|
| 236 | // SAFETY: The `func` field is not structurally pinned.
|
| 237 | unsafe { &mut self.get_unchecked_mut().func }
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | impl<T: FnOnce()> WorkItem for ClosureWork<T> {
|
| 242 | type Pointer = Pin<Box<Self>>;
|
| 243 |
|
| 244 | fn run(mut this: Pin<Box<Self>>) {
|
| 245 | if let Some(func) = this.as_mut().project().take() {
|
| 246 | (func)()
|
| 247 | }
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | /// A raw work item.
|
| 252 | ///
|
| 253 | /// This is the low-level trait that is designed for being as general as possible.
|
| 254 | ///
|
| 255 | /// The `ID` parameter to this trait exists so that a single type can provide multiple
|
| 256 | /// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then
|
| 257 | /// you will implement this trait once for each field, using a different id for each field. The
|
| 258 | /// actual value of the id is not important as long as you use different ids for different fields
|
| 259 | /// of the same struct. (Fields of different structs need not use different ids.)
|
| 260 | ///
|
| 261 | /// Note that the id is used only to select the right method to call during compilation. It won't be
|
| 262 | /// part of the final executable.
|
| 263 | ///
|
| 264 | /// # Safety
|
| 265 | ///
|
| 266 | /// Implementers must ensure that any pointers passed to a `queue_work_on` closure by [`__enqueue`]
|
| 267 | /// remain valid for the duration specified in the guarantees section of the documentation for
|
| 268 | /// [`__enqueue`].
|
| 269 | ///
|
| 270 | /// [`__enqueue`]: RawWorkItem::__enqueue
|
| 271 | pub unsafe trait RawWorkItem<const ID: u64> {
|
| 272 | /// The return type of [`Queue::enqueue`].
|
| 273 | type EnqueueOutput;
|
| 274 |
|
| 275 | /// Enqueues this work item on a queue using the provided `queue_work_on` method.
|
| 276 | ///
|
| 277 | /// # Guarantees
|
| 278 | ///
|
| 279 | /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a
|
| 280 | /// valid `work_struct` for the duration of the call to the closure. If the closure returns
|
| 281 | /// true, then it is further guaranteed that the pointer remains valid until someone calls the
|
| 282 | /// function pointer stored in the `work_struct`.
|
| 283 | ///
|
| 284 | /// # Safety
|
| 285 | ///
|
| 286 | /// The provided closure may only return `false` if the `work_struct` is already in a workqueue.
|
| 287 | ///
|
| 288 | /// If the work item type is annotated with any lifetimes, then you must not call the function
|
| 289 | /// pointer after any such lifetime expires. (Never calling the function pointer is okay.)
|
| 290 | ///
|
| 291 | /// If the work item type is not [`Send`], then the function pointer must be called on the same
|
| 292 | /// thread as the call to `__enqueue`.
|
| 293 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 294 | where
|
| 295 | F: FnOnce(*mut bindings::work_struct) -> bool;
|
| 296 | }
|
| 297 |
|
| 298 | /// Defines the method that should be called directly when a work item is executed.
|
| 299 | ///
|
| 300 | /// This trait is implemented by `Pin<Box<T>>` and [`Arc<T>`], and is mainly intended to be
|
| 301 | /// implemented for smart pointer types. For your own structs, you would implement [`WorkItem`]
|
| 302 | /// instead. The [`run`] method on this trait will usually just perform the appropriate
|
| 303 | /// `container_of` translation and then call into the [`run`][WorkItem::run] method from the
|
| 304 | /// [`WorkItem`] trait.
|
| 305 | ///
|
| 306 | /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
|
| 307 | ///
|
| 308 | /// # Safety
|
| 309 | ///
|
| 310 | /// Implementers must ensure that [`__enqueue`] uses a `work_struct` initialized with the [`run`]
|
| 311 | /// method of this trait as the function pointer.
|
| 312 | ///
|
| 313 | /// [`__enqueue`]: RawWorkItem::__enqueue
|
| 314 | /// [`run`]: WorkItemPointer::run
|
| 315 | pub unsafe trait WorkItemPointer<const ID: u64>: RawWorkItem<ID> {
|
| 316 | /// Run this work item.
|
| 317 | ///
|
| 318 | /// # Safety
|
| 319 | ///
|
| 320 | /// The provided `work_struct` pointer must originate from a previous call to [`__enqueue`]
|
| 321 | /// where the `queue_work_on` closure returned true, and the pointer must still be valid.
|
| 322 | ///
|
| 323 | /// [`__enqueue`]: RawWorkItem::__enqueue
|
| 324 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct);
|
| 325 | }
|
| 326 |
|
| 327 | /// Defines the method that should be called when this work item is executed.
|
| 328 | ///
|
| 329 | /// This trait is used when the `work_struct` field is defined using the [`Work`] helper.
|
| 330 | pub trait WorkItem<const ID: u64 = 0> {
|
| 331 | /// The pointer type that this struct is wrapped in. This will typically be `Arc<Self>` or
|
| 332 | /// `Pin<Box<Self>>`.
|
| 333 | type Pointer: WorkItemPointer<ID>;
|
| 334 |
|
| 335 | /// The method that should be called when this work item is executed.
|
| 336 | fn run(this: Self::Pointer);
|
| 337 | }
|
| 338 |
|
| 339 | /// Links for a work item.
|
| 340 | ///
|
| 341 | /// This struct contains a function pointer to the [`run`] function from the [`WorkItemPointer`]
|
| 342 | /// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
|
| 343 | ///
|
| 344 | /// Wraps the kernel's C `struct work_struct`.
|
| 345 | ///
|
| 346 | /// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
|
| 347 | ///
|
| 348 | /// [`run`]: WorkItemPointer::run
|
| 349 | #[pin_data]
|
| 350 | #[repr(transparent)]
|
| 351 | pub struct Work<T: ?Sized, const ID: u64 = 0> {
|
| 352 | #[pin]
|
| 353 | work: Opaque<bindings::work_struct>,
|
| 354 | _inner: PhantomData<T>,
|
| 355 | }
|
| 356 |
|
| 357 | // SAFETY: Kernel work items are usable from any thread.
|
| 358 | //
|
| 359 | // We do not need to constrain `T` since the work item does not actually contain a `T`.
|
| 360 | unsafe impl<T: ?Sized, const ID: u64> Send for Work<T, ID> {}
|
| 361 | // SAFETY: Kernel work items are usable from any thread.
|
| 362 | //
|
| 363 | // We do not need to constrain `T` since the work item does not actually contain a `T`.
|
| 364 | unsafe impl<T: ?Sized, const ID: u64> Sync for Work<T, ID> {}
|
| 365 |
|
| 366 | impl<T: ?Sized, const ID: u64> Work<T, ID> {
|
| 367 | /// Creates a new instance of [`Work`].
|
| 368 | #[inline]
|
| 369 | #[allow(clippy::new_ret_no_self)]
|
| 370 | pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
|
| 371 | where
|
| 372 | T: WorkItem<ID>,
|
| 373 | {
|
| 374 | pin_init!(Self {
|
| 375 | work <- Opaque::ffi_init(|slot| {
|
| 376 | // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as
|
| 377 | // the work item function.
|
| 378 | unsafe {
|
| 379 | bindings::init_work_with_key(
|
| 380 | slot,
|
| 381 | Some(T::Pointer::run),
|
| 382 | false,
|
| 383 | name.as_char_ptr(),
|
| 384 | key.as_ptr(),
|
| 385 | )
|
| 386 | }
|
| 387 | }),
|
| 388 | _inner: PhantomData,
|
| 389 | })
|
| 390 | }
|
| 391 |
|
| 392 | /// Get a pointer to the inner `work_struct`.
|
| 393 | ///
|
| 394 | /// # Safety
|
| 395 | ///
|
| 396 | /// The provided pointer must not be dangling and must be properly aligned. (But the memory
|
| 397 | /// need not be initialized.)
|
| 398 | #[inline]
|
| 399 | pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
|
| 400 | // SAFETY: The caller promises that the pointer is aligned and not dangling.
|
| 401 | //
|
| 402 | // A pointer cast would also be ok due to `#[repr(transparent)]`. We use `addr_of!` so that
|
| 403 | // the compiler does not complain that the `work` field is unused.
|
| 404 | unsafe { Opaque::raw_get(core::ptr::addr_of!((*ptr).work)) }
|
| 405 | }
|
| 406 | }
|
| 407 |
|
| 408 | /// Declares that a type has a [`Work<T, ID>`] field.
|
| 409 | ///
|
| 410 | /// The intended way of using this trait is via the [`impl_has_work!`] macro. You can use the macro
|
| 411 | /// like this:
|
| 412 | ///
|
| 413 | /// ```no_run
|
| 414 | /// use kernel::workqueue::{impl_has_work, Work};
|
| 415 | ///
|
| 416 | /// struct MyWorkItem {
|
| 417 | /// work_field: Work<MyWorkItem, 1>,
|
| 418 | /// }
|
| 419 | ///
|
| 420 | /// impl_has_work! {
|
| 421 | /// impl HasWork<MyWorkItem, 1> for MyWorkItem { self.work_field }
|
| 422 | /// }
|
| 423 | /// ```
|
| 424 | ///
|
| 425 | /// Note that since the [`Work`] type is annotated with an id, you can have several `work_struct`
|
| 426 | /// fields by using a different id for each one.
|
| 427 | ///
|
| 428 | /// # Safety
|
| 429 | ///
|
| 430 | /// The [`OFFSET`] constant must be the offset of a field in `Self` of type [`Work<T, ID>`]. The
|
| 431 | /// methods on this trait must have exactly the behavior that the definitions given below have.
|
| 432 | ///
|
| 433 | /// [`impl_has_work!`]: crate::impl_has_work
|
| 434 | /// [`OFFSET`]: HasWork::OFFSET
|
| 435 | pub unsafe trait HasWork<T, const ID: u64 = 0> {
|
| 436 | /// The offset of the [`Work<T, ID>`] field.
|
| 437 | const OFFSET: usize;
|
| 438 |
|
| 439 | /// Returns the offset of the [`Work<T, ID>`] field.
|
| 440 | ///
|
| 441 | /// This method exists because the [`OFFSET`] constant cannot be accessed if the type is not
|
| 442 | /// [`Sized`].
|
| 443 | ///
|
| 444 | /// [`OFFSET`]: HasWork::OFFSET
|
| 445 | #[inline]
|
| 446 | fn get_work_offset(&self) -> usize {
|
| 447 | Self::OFFSET
|
| 448 | }
|
| 449 |
|
| 450 | /// Returns a pointer to the [`Work<T, ID>`] field.
|
| 451 | ///
|
| 452 | /// # Safety
|
| 453 | ///
|
| 454 | /// The provided pointer must point at a valid struct of type `Self`.
|
| 455 | #[inline]
|
| 456 | unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<T, ID> {
|
| 457 | // SAFETY: The caller promises that the pointer is valid.
|
| 458 | unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work<T, ID> }
|
| 459 | }
|
| 460 |
|
| 461 | /// Returns a pointer to the struct containing the [`Work<T, ID>`] field.
|
| 462 | ///
|
| 463 | /// # Safety
|
| 464 | ///
|
| 465 | /// The pointer must point at a [`Work<T, ID>`] field in a struct of type `Self`.
|
| 466 | #[inline]
|
| 467 | unsafe fn work_container_of(ptr: *mut Work<T, ID>) -> *mut Self
|
| 468 | where
|
| 469 | Self: Sized,
|
| 470 | {
|
| 471 | // SAFETY: The caller promises that the pointer points at a field of the right type in the
|
| 472 | // right kind of struct.
|
| 473 | unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self }
|
| 474 | }
|
| 475 | }
|
| 476 |
|
| 477 | /// Used to safely implement the [`HasWork<T, ID>`] trait.
|
| 478 | ///
|
| 479 | /// # Examples
|
| 480 | ///
|
| 481 | /// ```
|
| 482 | /// use kernel::sync::Arc;
|
| 483 | /// use kernel::workqueue::{self, impl_has_work, Work};
|
| 484 | ///
|
| 485 | /// struct MyStruct<'a, T, const N: usize> {
|
| 486 | /// work_field: Work<MyStruct<'a, T, N>, 17>,
|
| 487 | /// f: fn(&'a [T; N]),
|
| 488 | /// }
|
| 489 | ///
|
| 490 | /// impl_has_work! {
|
| 491 | /// impl{'a, T, const N: usize} HasWork<MyStruct<'a, T, N>, 17>
|
| 492 | /// for MyStruct<'a, T, N> { self.work_field }
|
| 493 | /// }
|
| 494 | /// ```
|
| 495 | #[macro_export]
|
| 496 | macro_rules! impl_has_work {
|
| 497 | ($(impl$({$($generics:tt)*})?
|
| 498 | HasWork<$work_type:ty $(, $id:tt)?>
|
| 499 | for $self:ty
|
| 500 | { self.$field:ident }
|
| 501 | )*) => {$(
|
| 502 | |
| 503 | |
| 504 | unsafe impl$(<$($generics)+>)? $crate::workqueue::HasWork<$work_type $(, $id)?> for $self {
|
| 505 | const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
|
| 506 |
|
| 507 | #[inline]
|
| 508 | unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_type $(, $id)?> {
|
| 509 | |
| 510 | unsafe {
|
| 511 | ::core::ptr::addr_of_mut!((*ptr).$field)
|
| 512 | }
|
| 513 | }
|
| 514 | }
|
| 515 | )*};
|
| 516 | }
|
| 517 | pub use impl_has_work;
|
| 518 |
|
| 519 | impl_has_work! {
|
| 520 | impl{T} HasWork<Self> for ClosureWork<T> { self.work }
|
| 521 | }
|
| 522 |
|
| 523 | unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>
|
| 524 | where
|
| 525 | T: WorkItem<ID, Pointer = Self>,
|
| 526 | T: HasWork<T, ID>,
|
| 527 | {
|
| 528 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
|
| 529 | |
| 530 | let ptr = ptr as *mut Work<T, ID>;
|
| 531 | |
| 532 | let ptr = unsafe { T::work_container_of(ptr) };
|
| 533 | |
| 534 | let arc = unsafe { Arc::from_raw(ptr) };
|
| 535 |
|
| 536 | T::run(arc)
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | unsafe impl<T, const ID: u64> RawWorkItem<ID> for Arc<T>
|
| 541 | where
|
| 542 | T: WorkItem<ID, Pointer = Self>,
|
| 543 | T: HasWork<T, ID>,
|
| 544 | {
|
| 545 | type EnqueueOutput = Result<(), Self>;
|
| 546 |
|
| 547 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 548 | where
|
| 549 | F: FnOnce(*mut bindings::work_struct) -> bool,
|
| 550 | {
|
| 551 | |
| 552 | let ptr = Arc::into_raw(self).cast_mut();
|
| 553 |
|
| 554 | |
| 555 | let work_ptr = unsafe { T::raw_get_work(ptr) };
|
| 556 | |
| 557 | let work_ptr = unsafe { Work::raw_get(work_ptr) };
|
| 558 |
|
| 559 | if queue_work_on(work_ptr) {
|
| 560 | Ok(())
|
| 561 | } else {
|
| 562 | |
| 563 | Err(unsafe { Arc::from_raw(ptr) })
|
| 564 | }
|
| 565 | }
|
| 566 | }
|
| 567 |
|
| 568 | unsafe impl<T, const ID: u64> WorkItemPointer<ID> for Pin<Box<T>>
|
| 569 | where
|
| 570 | T: WorkItem<ID, Pointer = Self>,
|
| 571 | T: HasWork<T, ID>,
|
| 572 | {
|
| 573 | unsafe extern "C" fn run(ptr: *mut bindings::work_struct) {
|
| 574 | |
| 575 | let ptr = ptr as *mut Work<T, ID>;
|
| 576 | |
| 577 | let ptr = unsafe { T::work_container_of(ptr) };
|
| 578 | |
| 579 | let boxed = unsafe { Box::from_raw(ptr) };
|
| 580 | |
| 581 | let pinned = unsafe { Pin::new_unchecked(boxed) };
|
| 582 |
|
| 583 | T::run(pinned)
|
| 584 | }
|
| 585 | }
|
| 586 |
|
| 587 | unsafe impl<T, const ID: u64> RawWorkItem<ID> for Pin<Box<T>>
|
| 588 | where
|
| 589 | T: WorkItem<ID, Pointer = Self>,
|
| 590 | T: HasWork<T, ID>,
|
| 591 | {
|
| 592 | type EnqueueOutput = ();
|
| 593 |
|
| 594 | unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutput
|
| 595 | where
|
| 596 | F: FnOnce(*mut bindings::work_struct) -> bool,
|
| 597 | {
|
| 598 | |
| 599 | |
| 600 | let boxed = unsafe { Pin::into_inner_unchecked(self) };
|
| 601 | let ptr = Box::into_raw(boxed);
|
| 602 |
|
| 603 | |
| 604 | let work_ptr = unsafe { T::raw_get_work(ptr) };
|
| 605 | |
| 606 | let work_ptr = unsafe { Work::raw_get(work_ptr) };
|
| 607 |
|
| 608 | if !queue_work_on(work_ptr) {
|
| 609 | |
| 610 | |
| 611 | unsafe { ::core::hint::unreachable_unchecked() }
|
| 612 | }
|
| 613 | }
|
| 614 | }
|
| 615 |
|
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | pub fn system() -> &'static Queue {
|
| 623 | |
| 624 | unsafe { Queue::from_raw(bindings::system_wq) }
|
| 625 | }
|
| 626 |
|
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | pub fn system_highpri() -> &'static Queue {
|
| 632 | |
| 633 | unsafe { Queue::from_raw(bindings::system_highpri_wq) }
|
| 634 | }
|
| 635 |
|
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | pub fn system_long() -> &'static Queue {
|
| 641 | |
| 642 | unsafe { Queue::from_raw(bindings::system_long_wq) }
|
| 643 | }
|
| 644 |
|
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | pub fn system_unbound() -> &'static Queue {
|
| 651 | |
| 652 | unsafe { Queue::from_raw(bindings::system_unbound_wq) }
|
| 653 | }
|
| 654 |
|
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | pub fn system_freezable() -> &'static Queue {
|
| 662 | |
| 663 | unsafe { Queue::from_raw(bindings::system_freezable_wq) }
|
| 664 | }
|
| 665 |
|
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | pub fn system_power_efficient() -> &'static Queue {
|
| 672 | |
| 673 | unsafe { Queue::from_raw(bindings::system_power_efficient_wq) }
|
| 674 | }
|
| 675 |
|
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | pub fn system_freezable_power_efficient() -> &'static Queue {
|
| 683 | |
| 684 | unsafe { Queue::from_raw(bindings::system_freezable_power_efficient_wq) }
|
| 685 | }
|