1 // SPDX-License-Identifier: GPL-2.0
5 use crate::init::{self, PinInit};
8 marker::{PhantomData, PhantomPinned},
9 mem::{ManuallyDrop, MaybeUninit},
10 ops::{Deref, DerefMut},
14 /// Used to transfer ownership to and from foreign (non-Rust) languages.
16 /// Ownership is transferred from Rust to a foreign language by calling [`Self::into_foreign`] and
17 /// later may be transferred back to Rust by calling [`Self::from_foreign`].
19 /// This trait is meant to be used in cases when Rust objects are stored in C objects and
20 /// eventually "freed" back to Rust.
21 pub trait ForeignOwnable: Sized {
22 /// Type of values borrowed between calls to [`ForeignOwnable::into_foreign`] and
23 /// [`ForeignOwnable::from_foreign`].
26 /// Converts a Rust-owned object to a foreign-owned one.
28 /// The foreign representation is a pointer to void. There are no guarantees for this pointer.
29 /// For example, it might be invalid, dangling or pointing to uninitialized memory. Using it in
30 /// any way except for [`ForeignOwnable::from_foreign`], [`ForeignOwnable::borrow`],
31 /// [`ForeignOwnable::try_from_foreign`] can result in undefined behavior.
32 fn into_foreign(self) -> *const crate::ffi::c_void;
34 /// Borrows a foreign-owned object.
38 /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
39 /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
40 unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> Self::Borrowed<'a>;
42 /// Converts a foreign-owned object back to a Rust-owned one.
46 /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
47 /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
48 /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
49 /// this object must have been dropped.
50 unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self;
52 /// Tries to convert a foreign-owned object back to a Rust-owned one.
54 /// A convenience wrapper over [`ForeignOwnable::from_foreign`] that returns [`None`] if `ptr`
59 /// `ptr` must either be null or satisfy the safety requirements for
60 /// [`ForeignOwnable::from_foreign`].
61 unsafe fn try_from_foreign(ptr: *const crate::ffi::c_void) -> Option<Self> {
65 // SAFETY: Since `ptr` is not null here, then `ptr` satisfies the safety requirements
66 // of `from_foreign` given the safety requirements of this function.
67 unsafe { Some(Self::from_foreign(ptr)) }
72 impl ForeignOwnable for () {
73 type Borrowed<'a> = ();
75 fn into_foreign(self) -> *const crate::ffi::c_void {
76 core::ptr::NonNull::dangling().as_ptr()
79 unsafe fn borrow<'a>(_: *const crate::ffi::c_void) -> Self::Borrowed<'a> {}
81 unsafe fn from_foreign(_: *const crate::ffi::c_void) -> Self {}
84 /// Runs a cleanup function/closure when dropped.
86 /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
90 /// In the example below, we have multiple exit paths and we want to log regardless of which one is
94 /// # use kernel::types::ScopeGuard;
95 /// fn example1(arg: bool) {
96 /// let _log = ScopeGuard::new(|| pr_info!("example1 completed\n"));
102 /// pr_info!("Do something...\n");
105 /// # example1(false);
106 /// # example1(true);
109 /// In the example below, we want to log the same message on all early exits but a different one on
110 /// the main exit path:
113 /// # use kernel::types::ScopeGuard;
114 /// fn example2(arg: bool) {
115 /// let log = ScopeGuard::new(|| pr_info!("example2 returned early\n"));
121 /// // (Other early returns...)
124 /// pr_info!("example2 no early return\n");
127 /// # example2(false);
128 /// # example2(true);
131 /// In the example below, we need a mutable object (the vector) to be accessible within the log
132 /// function, so we wrap it in the [`ScopeGuard`]:
135 /// # use kernel::types::ScopeGuard;
136 /// fn example3(arg: bool) -> Result {
138 /// ScopeGuard::new_with_data(KVec::new(), |v| pr_info!("vec had {} elements\n", v.len()));
140 /// vec.push(10u8, GFP_KERNEL)?;
144 /// vec.push(20u8, GFP_KERNEL)?;
148 /// # assert_eq!(example3(false), Ok(()));
149 /// # assert_eq!(example3(true), Ok(()));
154 /// The value stored in the struct is nearly always `Some(_)`, except between
155 /// [`ScopeGuard::dismiss`] and [`ScopeGuard::drop`]: in this case, it will be `None` as the value
156 /// will have been returned to the caller. Since [`ScopeGuard::dismiss`] consumes the guard,
157 /// callers won't be able to use it anymore.
158 pub struct ScopeGuard<T, F: FnOnce(T)>(Option<(T, F)>);
160 impl<T, F: FnOnce(T)> ScopeGuard<T, F> {
161 /// Creates a new guarded object wrapping the given data and with the given cleanup function.
162 pub fn new_with_data(data: T, cleanup_func: F) -> Self {
163 // INVARIANT: The struct is being initialised with `Some(_)`.
164 Self(Some((data, cleanup_func)))
167 /// Prevents the cleanup function from running and returns the guarded data.
168 pub fn dismiss(mut self) -> T {
169 // INVARIANT: This is the exception case in the invariant; it is not visible to callers
170 // because this function consumes `self`.
171 self.0.take().unwrap().0
175 impl ScopeGuard<(), fn(())> {
176 /// Creates a new guarded object with the given cleanup function.
177 pub fn new(cleanup: impl FnOnce()) -> ScopeGuard<(), impl FnOnce(())> {
178 ScopeGuard::new_with_data((), move |()| cleanup())
182 impl<T, F: FnOnce(T)> Deref for ScopeGuard<T, F> {
185 fn deref(&self) -> &T {
186 // The type invariants guarantee that `unwrap` will succeed.
187 &self.0.as_ref().unwrap().0
191 impl<T, F: FnOnce(T)> DerefMut for ScopeGuard<T, F> {
192 fn deref_mut(&mut self) -> &mut T {
193 // The type invariants guarantee that `unwrap` will succeed.
194 &mut self.0.as_mut().unwrap().0
198 impl<T, F: FnOnce(T)> Drop for ScopeGuard<T, F> {
200 // Run the cleanup function if one is still present.
201 if let Some((data, cleanup)) = self.0.take() {
207 /// Stores an opaque value.
209 /// `Opaque<T>` is meant to be used with FFI objects that are never interpreted by Rust code.
211 /// It is used to wrap structs from the C side, like for example `Opaque<bindings::mutex>`.
212 /// It gets rid of all the usual assumptions that Rust has for a value:
214 /// * The value is allowed to be uninitialized (for example have invalid bit patterns: `3` for a
216 /// * The value is allowed to be mutated, when a `&Opaque<T>` exists on the Rust side.
217 /// * No uniqueness for mutable references: it is fine to have multiple `&mut Opaque<T>` point to
219 /// * The value is not allowed to be shared with other threads (i.e. it is `!Sync`).
221 /// This has to be used for all values that the C side has access to, because it can't be ensured
222 /// that the C side is adhering to the usual constraints that Rust needs.
224 /// Using `Opaque<T>` allows to continue to use references on the Rust side even for values shared
230 /// # #![expect(unreachable_pub, clippy::disallowed_names)]
231 /// use kernel::types::Opaque;
232 /// # // Emulate a C struct binding which is from C, maybe uninitialized or not, only the C side
235 /// # pub struct Foo {
240 /// // `foo.val` is assumed to be handled on the C side, so we use `Opaque` to wrap it.
242 /// foo: Opaque<bindings::Foo>,
246 /// pub fn get_val(&self) -> u8 {
247 /// let ptr = Opaque::get(&self.foo);
249 /// // SAFETY: `Self` is valid from C side.
250 /// unsafe { (*ptr).val }
254 /// // Create an instance of `Foo` with the `Opaque` wrapper.
256 /// foo: Opaque::new(bindings::Foo { val: 0xdb }),
259 /// assert_eq!(foo.get_val(), 0xdb);
262 pub struct Opaque<T> {
263 value: UnsafeCell<MaybeUninit<T>>,
268 /// Creates a new opaque value.
269 pub const fn new(value: T) -> Self {
271 value: UnsafeCell::new(MaybeUninit::new(value)),
276 /// Creates an uninitialised value.
277 pub const fn uninit() -> Self {
279 value: UnsafeCell::new(MaybeUninit::uninit()),
284 /// Creates a pin-initializer from the given initializer closure.
286 /// The returned initializer calls the given closure with the pointer to the inner `T` of this
287 /// `Opaque`. Since this memory is uninitialized, the closure is not allowed to read from it.
289 /// This function is safe, because the `T` inside of an `Opaque` is allowed to be
290 /// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
291 /// to verify at that point that the inner value is valid.
292 pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
293 // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
294 // initialize the `T`.
296 init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
297 init_func(Self::raw_get(slot));
303 /// Creates a fallible pin-initializer from the given initializer closure.
305 /// The returned initializer calls the given closure with the pointer to the inner `T` of this
306 /// `Opaque`. Since this memory is uninitialized, the closure is not allowed to read from it.
308 /// This function is safe, because the `T` inside of an `Opaque` is allowed to be
309 /// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
310 /// to verify at that point that the inner value is valid.
311 pub fn try_ffi_init<E>(
312 init_func: impl FnOnce(*mut T) -> Result<(), E>,
313 ) -> impl PinInit<Self, E> {
314 // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
315 // initialize the `T`.
316 unsafe { init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::raw_get(slot))) }
319 /// Returns a raw pointer to the opaque data.
320 pub const fn get(&self) -> *mut T {
321 UnsafeCell::get(&self.value).cast::<T>()
324 /// Gets the value behind `this`.
326 /// This function is useful to get access to the value without creating intermediate
328 pub const fn raw_get(this: *const Self) -> *mut T {
329 UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
333 /// Types that are _always_ reference counted.
335 /// It allows such types to define their own custom ref increment and decrement functions.
336 /// Additionally, it allows users to convert from a shared reference `&T` to an owned reference
339 /// This is usually implemented by wrappers to existing structures on the C side of the code. For
340 /// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted
341 /// instances of a type.
345 /// Implementers must ensure that increments to the reference count keep the object alive in memory
346 /// at least until matching decrements are performed.
348 /// Implementers must also ensure that all instances are reference-counted. (Otherwise they
349 /// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object
351 pub unsafe trait AlwaysRefCounted {
352 /// Increments the reference count on the object.
355 /// Decrements the reference count on the object.
357 /// Frees the object when the count reaches zero.
361 /// Callers must ensure that there was a previous matching increment to the reference count,
362 /// and that the object is no longer used after its reference count is decremented (as it may
363 /// result in the object being freed), unless the caller owns another increment on the refcount
364 /// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls
365 /// [`AlwaysRefCounted::dec_ref`] once).
366 unsafe fn dec_ref(obj: NonNull<Self>);
369 /// An owned reference to an always-reference-counted object.
371 /// The object's reference count is automatically decremented when an instance of [`ARef`] is
372 /// dropped. It is also automatically incremented when a new instance is created via
377 /// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In
378 /// particular, the [`ARef`] instance owns an increment on the underlying object's reference count.
379 pub struct ARef<T: AlwaysRefCounted> {
384 // SAFETY: It is safe to send `ARef<T>` to another thread when the underlying `T` is `Sync` because
385 // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
386 // `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a
387 // mutable reference, for example, when the reference count reaches zero and `T` is dropped.
388 unsafe impl<T: AlwaysRefCounted + Sync + Send> Send for ARef<T> {}
390 // SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync`
391 // because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally,
392 // it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an
393 // `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
394 // example, when the reference count reaches zero and `T` is dropped.
395 unsafe impl<T: AlwaysRefCounted + Sync + Send> Sync for ARef<T> {}
397 impl<T: AlwaysRefCounted> ARef<T> {
398 /// Creates a new instance of [`ARef`].
400 /// It takes over an increment of the reference count on the underlying object.
404 /// Callers must ensure that the reference count was incremented at least once, and that they
405 /// are properly relinquishing one increment. That is, if there is only one increment, callers
406 /// must not use the underlying object anymore -- it is only safe to do so via the newly
407 /// created [`ARef`].
408 pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
409 // INVARIANT: The safety requirements guarantee that the new instance now owns the
410 // increment on the refcount.
417 /// Consumes the `ARef`, returning a raw pointer.
419 /// This function does not change the refcount. After calling this function, the caller is
420 /// responsible for the refcount previously managed by the `ARef`.
425 /// use core::ptr::NonNull;
426 /// use kernel::types::{ARef, AlwaysRefCounted};
430 /// # // SAFETY: TODO.
431 /// unsafe impl AlwaysRefCounted for Empty {
432 /// fn inc_ref(&self) {}
433 /// unsafe fn dec_ref(_obj: NonNull<Self>) {}
436 /// let mut data = Empty {};
437 /// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
438 /// # // SAFETY: TODO.
439 /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
440 /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);
442 /// assert_eq!(ptr, raw_ptr);
444 pub fn into_raw(me: Self) -> NonNull<T> {
445 ManuallyDrop::new(me).ptr
449 impl<T: AlwaysRefCounted> Clone for ARef<T> {
450 fn clone(&self) -> Self {
452 // SAFETY: We just incremented the refcount above.
453 unsafe { Self::from_raw(self.ptr) }
457 impl<T: AlwaysRefCounted> Deref for ARef<T> {
460 fn deref(&self) -> &Self::Target {
461 // SAFETY: The type invariants guarantee that the object is valid.
462 unsafe { self.ptr.as_ref() }
466 impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
467 fn from(b: &T) -> Self {
469 // SAFETY: We just incremented the refcount above.
470 unsafe { Self::from_raw(NonNull::from(b)) }
474 impl<T: AlwaysRefCounted> Drop for ARef<T> {
476 // SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
478 unsafe { T::dec_ref(self.ptr) };
482 /// A sum type that always holds either a value of type `L` or `R`.
487 /// use kernel::types::Either;
489 /// let left_value: Either<i32, &str> = Either::Left(7);
490 /// let right_value: Either<i32, &str> = Either::Right("right value");
492 pub enum Either<L, R> {
493 /// Constructs an instance of [`Either`] containing a value of type `L`.
496 /// Constructs an instance of [`Either`] containing a value of type `R`.
500 /// Zero-sized type to mark types not [`Send`].
502 /// Add this type as a field to your struct if your type should not be sent to a different task.
503 /// Since [`Send`] is an auto trait, adding a single field that is `!Send` will ensure that the
504 /// whole type is `!Send`.
506 /// If a type is `!Send` it is impossible to give control over an instance of the type to another
507 /// task. This is useful to include in types that store or reference task-local information. A file
508 /// descriptor is an example of such task-local information.
510 /// This type also makes the type `!Sync`, which prevents immutable access to the value from
511 /// several threads in parallel.
512 pub type NotThreadSafe = PhantomData<*mut ()>;
514 /// Used to construct instances of type [`NotThreadSafe`] similar to how `PhantomData` is
517 /// [`NotThreadSafe`]: type@NotThreadSafe
518 #[allow(non_upper_case_globals)]
519 pub const NotThreadSafe: NotThreadSafe = PhantomData;