mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
Pull Rust updates from Miguel Ojeda:
"Toolchain and infrastructure:
- Add support for 'syn'.
Syn is a parsing library for parsing a stream of Rust tokens into a
syntax tree of Rust source code.
Currently this library is geared toward use in Rust procedural
macros, but contains some APIs that may be useful more generally.
'syn' allows us to greatly simplify writing complex macros such as
'pin-init' (Benno has already prepared the 'syn'-based version). We
will use it in the 'macros' crate too.
'syn' is the most downloaded Rust crate (according to crates.io),
and it is also used by the Rust compiler itself. While the amount
of code is substantial, there should not be many updates needed for
these crates, and even if there are, they should not be too big,
e.g. +7k -3k lines across the 3 crates in the last year.
'syn' requires two smaller dependencies: 'quote' and 'proc-macro2'.
I only modified their code to remove a third dependency
('unicode-ident') and to add the SPDX identifiers. The code can be
easily verified to exactly match upstream with the provided
scripts.
They are all licensed under "Apache-2.0 OR MIT", like the other
vendored 'alloc' crate we had for a while.
Please see the merge commit with the cover letter for more context.
- Allow 'unreachable_pub' and 'clippy::disallowed_names' for
doctests.
Examples (i.e. doctests) may want to do things like show public
items and use names such as 'foo'.
Nevertheless, we still try to keep examples as close to real code
as possible (this is part of why running Clippy on doctests is
important for us, e.g. for safety comments, which userspace Rust
does not support yet but we are stricter).
'kernel' crate:
- Replace our custom 'CStr' type with 'core::ffi::CStr'.
Using the standard library type reduces our custom code footprint,
and we retain needed custom functionality through an extension
trait and a new 'fmt!' macro which replaces the previous 'core'
import.
This started in 6.17 and continued in 6.18, and we finally land the
replacement now. This required quite some stamina from Tamir, who
split the changes in steps to prepare for the flag day change here.
- Replace 'kernel::c_str!' with C string literals.
C string literals were added in Rust 1.77, which produce '&CStr's
(the 'core' one), so now we can write:
c"hi"
instead of:
c_str!("hi")
- Add 'num' module for numerical features.
It includes the 'Integer' trait, implemented for all primitive
integer types.
It also includes the 'Bounded' integer wrapping type: an integer
value that requires only the 'N' least significant bits of the
wrapped type to be encoded:
// An unsigned 8-bit integer, of which only the 4 LSBs are used.
let v = Bounded::<u8, 4>::new::<15>();
assert_eq!(v.get(), 15);
'Bounded' is useful to e.g. enforce guarantees when working with
bitfields that have an arbitrary number of bits.
Values can also be constructed from simple non-constant expressions
or, for more complex ones, validated at runtime.
'Bounded' also comes with comparison and arithmetic operations
(with both their backing type and other 'Bounded's with a
compatible backing type), casts to change the backing type,
extending/shrinking and infallible/fallible conversions from/to
primitives as applicable.
- 'rbtree' module: add immutable cursor ('Cursor').
It enables to use just an immutable tree reference where
appropriate. The existing fully-featured mutable cursor is renamed
to 'CursorMut'.
kallsyms:
- Fix wrong "big" kernel symbol type read from procfs.
'pin-init' crate:
- A couple minor fixes (Benno asked me to pick these patches up for
him this cycle).
Documentation:
- Quick Start guide: add Debian 13 (Trixie).
Debian Stable is now able to build Linux, since Debian 13 (released
2025-08-09) packages Rust 1.85.0, which is recent enough.
We are planning to propose that the minimum supported Rust version
in Linux follows Debian Stable releases, with Debian 13 being the
first one we upgrade to, i.e. Rust 1.85.
MAINTAINERS:
- Add entry for the new 'num' module.
- Remove Alex as Rust maintainer: he hasn't had the time to
contribute for a few years now, so it is a no-op change in
practice.
And a few other cleanups and improvements"
* tag 'rust-6.19' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux: (53 commits)
rust: macros: support `proc-macro2`, `quote` and `syn`
rust: syn: enable support in kbuild
rust: syn: add `README.md`
rust: syn: remove `unicode-ident` dependency
rust: syn: add SPDX License Identifiers
rust: syn: import crate
rust: quote: enable support in kbuild
rust: quote: add `README.md`
rust: quote: add SPDX License Identifiers
rust: quote: import crate
rust: proc-macro2: enable support in kbuild
rust: proc-macro2: add `README.md`
rust: proc-macro2: remove `unicode_ident` dependency
rust: proc-macro2: add SPDX License Identifiers
rust: proc-macro2: import crate
rust: kbuild: support using libraries in `rustc_procmacro`
rust: kbuild: support skipping flags in `rustc_test_library`
rust: kbuild: add proc macro library support
rust: kbuild: simplify `--cfg` handling
rust: kbuild: introduce `core-flags` and `core-skip_flags`
...
259 lines
9.4 KiB
Rust
259 lines
9.4 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! A condition variable.
|
|
//!
|
|
//! This module allows Rust code to use the kernel's [`struct wait_queue_head`] as a condition
|
|
//! variable.
|
|
|
|
use super::{lock::Backend, lock::Guard, LockClassKey};
|
|
use crate::{
|
|
ffi::{c_int, c_long},
|
|
str::{CStr, CStrExt as _},
|
|
task::{
|
|
MAX_SCHEDULE_TIMEOUT, TASK_FREEZABLE, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE,
|
|
},
|
|
time::Jiffies,
|
|
types::Opaque,
|
|
};
|
|
use core::{marker::PhantomPinned, pin::Pin, ptr};
|
|
use pin_init::{pin_data, pin_init, PinInit};
|
|
|
|
/// Creates a [`CondVar`] initialiser with the given name and a newly-created lock class.
|
|
#[macro_export]
|
|
macro_rules! new_condvar {
|
|
($($name:literal)?) => {
|
|
$crate::sync::CondVar::new($crate::optional_name!($($name)?), $crate::static_lock_class!())
|
|
};
|
|
}
|
|
pub use new_condvar;
|
|
|
|
/// A conditional variable.
|
|
///
|
|
/// Exposes the kernel's [`struct wait_queue_head`] as a condition variable. It allows the caller to
|
|
/// atomically release the given lock and go to sleep. It reacquires the lock when it wakes up. And
|
|
/// it wakes up when notified by another thread (via [`CondVar::notify_one`] or
|
|
/// [`CondVar::notify_all`]) or because the thread received a signal. It may also wake up
|
|
/// spuriously.
|
|
///
|
|
/// Instances of [`CondVar`] need a lock class and to be pinned. The recommended way to create such
|
|
/// instances is with the [`pin_init`](pin_init::pin_init!) and [`new_condvar`] macros.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// The following is an example of using a condvar with a mutex:
|
|
///
|
|
/// ```
|
|
/// use kernel::sync::{new_condvar, new_mutex, CondVar, Mutex};
|
|
///
|
|
/// #[pin_data]
|
|
/// pub struct Example {
|
|
/// #[pin]
|
|
/// value: Mutex<u32>,
|
|
///
|
|
/// #[pin]
|
|
/// value_changed: CondVar,
|
|
/// }
|
|
///
|
|
/// /// Waits for `e.value` to become `v`.
|
|
/// fn wait_for_value(e: &Example, v: u32) {
|
|
/// let mut guard = e.value.lock();
|
|
/// while *guard != v {
|
|
/// e.value_changed.wait(&mut guard);
|
|
/// }
|
|
/// }
|
|
///
|
|
/// /// Increments `e.value` and notifies all potential waiters.
|
|
/// fn increment(e: &Example) {
|
|
/// *e.value.lock() += 1;
|
|
/// e.value_changed.notify_all();
|
|
/// }
|
|
///
|
|
/// /// Allocates a new boxed `Example`.
|
|
/// fn new_example() -> Result<Pin<KBox<Example>>> {
|
|
/// KBox::pin_init(pin_init!(Example {
|
|
/// value <- new_mutex!(0),
|
|
/// value_changed <- new_condvar!(),
|
|
/// }), GFP_KERNEL)
|
|
/// }
|
|
/// ```
|
|
///
|
|
/// [`struct wait_queue_head`]: srctree/include/linux/wait.h
|
|
#[pin_data]
|
|
pub struct CondVar {
|
|
#[pin]
|
|
pub(crate) wait_queue_head: Opaque<bindings::wait_queue_head>,
|
|
|
|
/// A condvar needs to be pinned because it contains a [`struct list_head`] that is
|
|
/// self-referential, so it cannot be safely moved once it is initialised.
|
|
///
|
|
/// [`struct list_head`]: srctree/include/linux/types.h
|
|
#[pin]
|
|
_pin: PhantomPinned,
|
|
}
|
|
|
|
// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on any thread.
|
|
unsafe impl Send for CondVar {}
|
|
|
|
// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on multiple threads
|
|
// concurrently.
|
|
unsafe impl Sync for CondVar {}
|
|
|
|
impl CondVar {
|
|
/// Constructs a new condvar initialiser.
|
|
pub fn new(name: &'static CStr, key: Pin<&'static LockClassKey>) -> impl PinInit<Self> {
|
|
pin_init!(Self {
|
|
_pin: PhantomPinned,
|
|
// SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
|
|
// static lifetimes so they live indefinitely.
|
|
wait_queue_head <- Opaque::ffi_init(|slot| unsafe {
|
|
bindings::__init_waitqueue_head(slot, name.as_char_ptr(), key.as_ptr())
|
|
}),
|
|
})
|
|
}
|
|
|
|
fn wait_internal<T: ?Sized, B: Backend>(
|
|
&self,
|
|
wait_state: c_int,
|
|
guard: &mut Guard<'_, T, B>,
|
|
timeout_in_jiffies: c_long,
|
|
) -> c_long {
|
|
let wait = Opaque::<bindings::wait_queue_entry>::uninit();
|
|
|
|
// SAFETY: `wait` points to valid memory.
|
|
unsafe { bindings::init_wait(wait.get()) };
|
|
|
|
// SAFETY: Both `wait` and `wait_queue_head` point to valid memory.
|
|
unsafe {
|
|
bindings::prepare_to_wait_exclusive(self.wait_queue_head.get(), wait.get(), wait_state)
|
|
};
|
|
|
|
// SAFETY: Switches to another thread. The timeout can be any number.
|
|
let ret = guard.do_unlocked(|| unsafe { bindings::schedule_timeout(timeout_in_jiffies) });
|
|
|
|
// SAFETY: Both `wait` and `wait_queue_head` point to valid memory.
|
|
unsafe { bindings::finish_wait(self.wait_queue_head.get(), wait.get()) };
|
|
|
|
ret
|
|
}
|
|
|
|
/// Releases the lock and waits for a notification in uninterruptible mode.
|
|
///
|
|
/// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
|
|
/// thread to sleep, reacquiring the lock on wake up. It wakes up when notified by
|
|
/// [`CondVar::notify_one`] or [`CondVar::notify_all`]. Note that it may also wake up
|
|
/// spuriously.
|
|
pub fn wait<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) {
|
|
self.wait_internal(TASK_UNINTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT);
|
|
}
|
|
|
|
/// Releases the lock and waits for a notification in interruptible mode.
|
|
///
|
|
/// Similar to [`CondVar::wait`], except that the wait is interruptible. That is, the thread may
|
|
/// wake up due to signals. It may also wake up spuriously.
|
|
///
|
|
/// Returns whether there is a signal pending.
|
|
#[must_use = "wait_interruptible returns if a signal is pending, so the caller must check the return value"]
|
|
pub fn wait_interruptible<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) -> bool {
|
|
self.wait_internal(TASK_INTERRUPTIBLE, guard, MAX_SCHEDULE_TIMEOUT);
|
|
crate::current!().signal_pending()
|
|
}
|
|
|
|
/// Releases the lock and waits for a notification in interruptible and freezable mode.
|
|
///
|
|
/// The process is allowed to be frozen during this sleep. No lock should be held when calling
|
|
/// this function, and there is a lockdep assertion for this. Freezing a task that holds a lock
|
|
/// can trivially deadlock vs another task that needs that lock to complete before it too can
|
|
/// hit freezable.
|
|
#[must_use = "wait_interruptible_freezable returns if a signal is pending, so the caller must check the return value"]
|
|
pub fn wait_interruptible_freezable<T: ?Sized, B: Backend>(
|
|
&self,
|
|
guard: &mut Guard<'_, T, B>,
|
|
) -> bool {
|
|
self.wait_internal(
|
|
TASK_INTERRUPTIBLE | TASK_FREEZABLE,
|
|
guard,
|
|
MAX_SCHEDULE_TIMEOUT,
|
|
);
|
|
crate::current!().signal_pending()
|
|
}
|
|
|
|
/// Releases the lock and waits for a notification in interruptible mode.
|
|
///
|
|
/// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
|
|
/// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or
|
|
/// [`CondVar::notify_all`], or when a timeout occurs, or when the thread receives a signal.
|
|
#[must_use = "wait_interruptible_timeout returns if a signal is pending, so the caller must check the return value"]
|
|
pub fn wait_interruptible_timeout<T: ?Sized, B: Backend>(
|
|
&self,
|
|
guard: &mut Guard<'_, T, B>,
|
|
jiffies: Jiffies,
|
|
) -> CondVarTimeoutResult {
|
|
let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT);
|
|
let res = self.wait_internal(TASK_INTERRUPTIBLE, guard, jiffies);
|
|
|
|
match (res as Jiffies, crate::current!().signal_pending()) {
|
|
(jiffies, true) => CondVarTimeoutResult::Signal { jiffies },
|
|
(0, false) => CondVarTimeoutResult::Timeout,
|
|
(jiffies, false) => CondVarTimeoutResult::Woken { jiffies },
|
|
}
|
|
}
|
|
|
|
/// Calls the kernel function to notify the appropriate number of threads.
|
|
fn notify(&self, count: c_int) {
|
|
// SAFETY: `wait_queue_head` points to valid memory.
|
|
unsafe {
|
|
bindings::__wake_up(
|
|
self.wait_queue_head.get(),
|
|
TASK_NORMAL,
|
|
count,
|
|
ptr::null_mut(),
|
|
)
|
|
};
|
|
}
|
|
|
|
/// Calls the kernel function to notify one thread synchronously.
|
|
///
|
|
/// This method behaves like `notify_one`, except that it hints to the scheduler that the
|
|
/// current thread is about to go to sleep, so it should schedule the target thread on the same
|
|
/// CPU.
|
|
#[inline]
|
|
pub fn notify_sync(&self) {
|
|
// SAFETY: `wait_queue_head` points to valid memory.
|
|
unsafe { bindings::__wake_up_sync(self.wait_queue_head.get(), TASK_NORMAL) };
|
|
}
|
|
|
|
/// Wakes a single waiter up, if any.
|
|
///
|
|
/// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost
|
|
/// completely (as opposed to automatically waking up the next waiter).
|
|
#[inline]
|
|
pub fn notify_one(&self) {
|
|
self.notify(1);
|
|
}
|
|
|
|
/// Wakes all waiters up, if any.
|
|
///
|
|
/// This is not 'sticky' in the sense that if no thread is waiting, the notification is lost
|
|
/// completely (as opposed to automatically waking up the next waiter).
|
|
#[inline]
|
|
pub fn notify_all(&self) {
|
|
self.notify(0);
|
|
}
|
|
}
|
|
|
|
/// The return type of `wait_timeout`.
|
|
pub enum CondVarTimeoutResult {
|
|
/// The timeout was reached.
|
|
Timeout,
|
|
/// Somebody woke us up.
|
|
Woken {
|
|
/// Remaining sleep duration.
|
|
jiffies: Jiffies,
|
|
},
|
|
/// A signal occurred.
|
|
Signal {
|
|
/// Remaining sleep duration.
|
|
jiffies: Jiffies,
|
|
},
|
|
}
|