mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
Pull module updates from Daniel Gomez:
"Rust module parameter support:
- Add Rust module parameter support, enabling Rust kernel modules to
declare and use module parameters. The rust_minimal sample module
demonstrates this, and the rust null block driver will be the first
to use it in the next cycle. This also adds the Rust module files
under the modules subsystem as agreed between the Rust and modules
maintainers.
Hardening:
- Add compile-time check for embedded NUL characters in MODULE_*()
macros. This module metadata was once used (and maybe still) to
bypass license enforcement (LWN article from 2003):
https://lwn.net/Articles/82305/ [1]
MAINTAINERS:
- Add Aaron Tomlin as reviewer for the Modules subsystem"
* tag 'modules-6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/modules/linux:
MAINTAINERS: Add myself as reviewer for module support
module: Add compile-time check for embedded NUL characters
media: radio: si470x: Fix DRIVER_AUTHOR macro definition
media: dvb-usb-v2: lmedm04: Fix firmware macro definitions
modules: add rust modules files to MAINTAINERS
rust: samples: add a module parameter to the rust_minimal sample
rust: module: update the module macro with module parameter support
rust: module: use a reference in macros::module::module
rust: introduce module_param module
rust: str: add radix prefixed integer parsing functions
rust: sync: add `SetOnce`
126 lines
4.1 KiB
Rust
126 lines
4.1 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! Synchronisation primitives.
|
|
//!
|
|
//! This module contains the kernel APIs related to synchronisation that have been ported or
|
|
//! wrapped for usage by Rust code in the kernel.
|
|
|
|
use crate::prelude::*;
|
|
use crate::types::Opaque;
|
|
use pin_init;
|
|
|
|
mod arc;
|
|
pub mod aref;
|
|
pub mod atomic;
|
|
pub mod barrier;
|
|
pub mod completion;
|
|
mod condvar;
|
|
pub mod lock;
|
|
mod locked_by;
|
|
pub mod poll;
|
|
pub mod rcu;
|
|
mod refcount;
|
|
mod set_once;
|
|
|
|
pub use arc::{Arc, ArcBorrow, UniqueArc};
|
|
pub use completion::Completion;
|
|
pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
|
|
pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
|
|
pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
|
|
pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
|
|
pub use locked_by::LockedBy;
|
|
pub use refcount::Refcount;
|
|
pub use set_once::SetOnce;
|
|
|
|
/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
|
|
#[repr(transparent)]
|
|
#[pin_data(PinnedDrop)]
|
|
pub struct LockClassKey {
|
|
#[pin]
|
|
inner: Opaque<bindings::lock_class_key>,
|
|
}
|
|
|
|
// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
|
|
// provides its own synchronization.
|
|
unsafe impl Sync for LockClassKey {}
|
|
|
|
impl LockClassKey {
|
|
/// Initializes a dynamically allocated lock class key. In the common case of using a
|
|
/// statically allocated lock class key, the static_lock_class! macro should be used instead.
|
|
///
|
|
/// # Examples
|
|
/// ```
|
|
/// # use kernel::alloc::KBox;
|
|
/// # use kernel::types::ForeignOwnable;
|
|
/// # use kernel::sync::{LockClassKey, SpinLock};
|
|
/// # use pin_init::stack_pin_init;
|
|
///
|
|
/// let key = KBox::pin_init(LockClassKey::new_dynamic(), GFP_KERNEL)?;
|
|
/// let key_ptr = key.into_foreign();
|
|
///
|
|
/// {
|
|
/// stack_pin_init!(let num: SpinLock<u32> = SpinLock::new(
|
|
/// 0,
|
|
/// c"my_spinlock",
|
|
/// // SAFETY: `key_ptr` is returned by the above `into_foreign()`, whose
|
|
/// // `from_foreign()` has not yet been called.
|
|
/// unsafe { <Pin<KBox<LockClassKey>> as ForeignOwnable>::borrow(key_ptr) }
|
|
/// ));
|
|
/// }
|
|
///
|
|
/// // SAFETY: We dropped `num`, the only use of the key, so the result of the previous
|
|
/// // `borrow` has also been dropped. Thus, it's safe to use from_foreign.
|
|
/// unsafe { drop(<Pin<KBox<LockClassKey>> as ForeignOwnable>::from_foreign(key_ptr)) };
|
|
///
|
|
/// # Ok::<(), Error>(())
|
|
/// ```
|
|
pub fn new_dynamic() -> impl PinInit<Self> {
|
|
pin_init!(Self {
|
|
// SAFETY: lockdep_register_key expects an uninitialized block of memory
|
|
inner <- Opaque::ffi_init(|slot| unsafe { bindings::lockdep_register_key(slot) })
|
|
})
|
|
}
|
|
|
|
pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
|
|
self.inner.get()
|
|
}
|
|
}
|
|
|
|
#[pinned_drop]
|
|
impl PinnedDrop for LockClassKey {
|
|
fn drop(self: Pin<&mut Self>) {
|
|
// SAFETY: self.as_ptr was registered with lockdep and self is pinned, so the address
|
|
// hasn't changed. Thus, it's safe to pass to unregister.
|
|
unsafe { bindings::lockdep_unregister_key(self.as_ptr()) }
|
|
}
|
|
}
|
|
|
|
/// Defines a new static lock class and returns a pointer to it.
|
|
#[doc(hidden)]
|
|
#[macro_export]
|
|
macro_rules! static_lock_class {
|
|
() => {{
|
|
static CLASS: $crate::sync::LockClassKey =
|
|
// Lockdep expects uninitialized memory when it's handed a statically allocated `struct
|
|
// lock_class_key`.
|
|
//
|
|
// SAFETY: `LockClassKey` transparently wraps `Opaque` which permits uninitialized
|
|
// memory.
|
|
unsafe { ::core::mem::MaybeUninit::uninit().assume_init() };
|
|
$crate::prelude::Pin::static_ref(&CLASS)
|
|
}};
|
|
}
|
|
|
|
/// Returns the given string, if one is provided, otherwise generates one based on the source code
|
|
/// location.
|
|
#[doc(hidden)]
|
|
#[macro_export]
|
|
macro_rules! optional_name {
|
|
() => {
|
|
$crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
|
|
};
|
|
($name:literal) => {
|
|
$crate::c_str!($name)
|
|
};
|
|
}
|