Skip to content

Commit d1000e8

Browse files
wedsonafDanilo Krummrich
authored andcommitted
rust: add rcu abstraction
Add a simple abstraction to guard critical code sections with an rcu read lock. Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Danilo Krummrich <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent c3c3bac commit d1000e8

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19690,6 +19690,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git dev
1969019690
F: Documentation/RCU/
1969119691
F: include/linux/rcu*
1969219692
F: kernel/rcu/
19693+
F: rust/kernel/sync/rcu.rs
1969319694
X: Documentation/RCU/torture.rst
1969419695
X: include/linux/srcu*.h
1969519696
X: kernel/rcu/srcu*.c

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "page.c"
2121
#include "pid_namespace.c"
2222
#include "rbtree.c"
23+
#include "rcu.c"
2324
#include "refcount.c"
2425
#include "security.c"
2526
#include "signal.c"

rust/helpers/rcu.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/rcupdate.h>
4+
5+
void rust_helper_rcu_read_lock(void)
6+
{
7+
rcu_read_lock();
8+
}
9+
10+
void rust_helper_rcu_read_unlock(void)
11+
{
12+
rcu_read_unlock();
13+
}

rust/kernel/sync.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod condvar;
1212
pub mod lock;
1313
mod locked_by;
1414
pub mod poll;
15+
pub mod rcu;
1516

1617
pub use arc::{Arc, ArcBorrow, UniqueArc};
1718
pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};

rust/kernel/sync/rcu.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! RCU support.
4+
//!
5+
//! C header: [`include/linux/rcupdate.h`](srctree/include/linux/rcupdate.h)
6+
7+
use crate::{bindings, types::NotThreadSafe};
8+
9+
/// Evidence that the RCU read side lock is held on the current thread/CPU.
10+
///
11+
/// The type is explicitly not `Send` because this property is per-thread/CPU.
12+
///
13+
/// # Invariants
14+
///
15+
/// The RCU read side lock is actually held while instances of this guard exist.
16+
pub struct Guard(NotThreadSafe);
17+
18+
impl Guard {
19+
/// Acquires the RCU read side lock and returns a guard.
20+
pub fn new() -> Self {
21+
// SAFETY: An FFI call with no additional requirements.
22+
unsafe { bindings::rcu_read_lock() };
23+
// INVARIANT: The RCU read side lock was just acquired above.
24+
Self(NotThreadSafe)
25+
}
26+
27+
/// Explicitly releases the RCU read side lock.
28+
pub fn unlock(self) {}
29+
}
30+
31+
impl Default for Guard {
32+
fn default() -> Self {
33+
Self::new()
34+
}
35+
}
36+
37+
impl Drop for Guard {
38+
fn drop(&mut self) {
39+
// SAFETY: By the type invariants, the RCU read side is locked, so it is ok to unlock it.
40+
unsafe { bindings::rcu_read_unlock() };
41+
}
42+
}
43+
44+
/// Acquires the RCU read side lock.
45+
pub fn read_lock() -> Guard {
46+
Guard::new()
47+
}

0 commit comments

Comments
 (0)