Skip to content

Commit 4f7aeb3

Browse files
Darksonnfbq
authored andcommitted
rust: types: add NotThreadSafe
This introduces a new marker type for types that shouldn't be thread safe. By adding a field of this type to a struct, it becomes non-Send and non-Sync, which means that it cannot be accessed in any way from threads other than the one it was created on. This is useful for APIs that require globals such as `current` to remain constant while the value exists. We update two existing users in the Kernel to use this helper: * `Task::current()` - moving the return type of this value to a different thread would not be safe as you can no longer be guaranteed that the `current` pointer remains valid. * Lock guards. Mutexes and spinlocks should be unlocked on the same thread as where they were locked, so we enforce this using the Send trait. There are also additional users in later patches of this patchset. See [1] and [2] for the discussion that led to the introducion of this patch. Link: https://lore.kernel.org/all/nFDPJFnzE9Q5cqY7FwSMByRH2OAn_BpI4H53NQfWIlN6I2qfmAqnkp2wRqn0XjMO65OyZY4h6P4K2nAGKJpAOSzksYXaiAK_FoH_8QbgBI4=@proton.me/ [1] Link: https://lore.kernel.org/all/nFDPJFnzE9Q5cqY7FwSMByRH2OAn_BpI4H53NQfWIlN6I2qfmAqnkp2wRqn0XjMO65OyZY4h6P4K2nAGKJpAOSzksYXaiAK_FoH_8QbgBI4=@proton.me/ [2] Suggested-by: Benno Lossin <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 2fa09b3 commit 4f7aeb3

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

rust/kernel/sync/lock.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
//! spinlocks, raw spinlocks) to be provided with minimal effort.
77
88
use super::LockClassKey;
9-
use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard};
10-
use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
9+
use crate::{
10+
bindings,
11+
init::PinInit,
12+
pin_init,
13+
str::CStr,
14+
types::ScopeGuard,
15+
types::{NotThreadSafe, Opaque},
16+
};
17+
use core::{cell::UnsafeCell, marker::PhantomPinned};
1118
use macros::pin_data;
1219

1320
pub mod mutex;
@@ -132,7 +139,7 @@ impl<T: ?Sized, B: Backend> Lock<T, B> {
132139
pub struct Guard<'a, T: ?Sized, B: Backend> {
133140
pub(crate) lock: &'a Lock<T, B>,
134141
pub(crate) state: B::GuardState,
135-
_not_send: PhantomData<*mut ()>,
142+
_not_send: NotThreadSafe,
136143
}
137144

138145
// SAFETY: `Guard` is sync when the data protected by the lock is also sync.
@@ -184,7 +191,7 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
184191
Self {
185192
lock,
186193
state,
187-
_not_send: PhantomData,
194+
_not_send: NotThreadSafe,
188195
}
189196
}
190197
}

rust/kernel/task.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
//!
55
//! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h).
66
7-
use crate::{bindings, types::Opaque};
7+
use crate::{
8+
bindings,
9+
types::{NotThreadSafe, Opaque},
10+
};
11+
812
use core::{
913
ffi::{c_int, c_long, c_uint},
10-
marker::PhantomData,
1114
ops::Deref,
1215
ptr,
1316
};
@@ -106,7 +109,7 @@ impl Task {
106109
pub unsafe fn current() -> impl Deref<Target = Task> {
107110
struct TaskRef<'a> {
108111
task: &'a Task,
109-
_not_send: PhantomData<*mut ()>,
112+
_not_send: NotThreadSafe,
110113
}
111114

112115
impl Deref for TaskRef<'_> {
@@ -125,7 +128,7 @@ impl Task {
125128
// that `TaskRef` is not `Send`, we know it cannot be transferred to another thread
126129
// (where it could potentially outlive the caller).
127130
task: unsafe { &*ptr.cast() },
128-
_not_send: PhantomData,
131+
_not_send: NotThreadSafe,
129132
}
130133
}
131134

rust/kernel/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,20 @@ pub enum Either<L, R> {
432432
/// Constructs an instance of [`Either`] containing a value of type `R`.
433433
Right(R),
434434
}
435+
436+
/// Zero-sized type to mark types not [`Send`].
437+
///
438+
/// Add this type as a field to your struct if your type should not be sent to a different task.
439+
/// Since [`Send`] is an auto trait, adding a single field that is `!Send` will ensure that the
440+
/// whole type is `!Send`.
441+
///
442+
/// If a type is `!Send` it is impossible to give control over an instance of the type to another
443+
/// task. This is useful when a type stores task-local information for example file descriptors.
444+
pub type NotThreadSafe = PhantomData<*mut ()>;
445+
446+
/// Used to construct instances of type [`NotThreadSafe`] similar to how we construct
447+
/// `PhantomData`.
448+
///
449+
/// [`NotThreadSafe`]: type@NotThreadSafe
450+
#[allow(non_upper_case_globals)]
451+
pub const NotThreadSafe: NotThreadSafe = PhantomData;

0 commit comments

Comments
 (0)