Skip to content

Commit fdc7f4b

Browse files
wedsonafojeda
authored andcommitted
rust: add basic Task
It is an abstraction for C's `struct task_struct`. It implements `AlwaysRefCounted`, so the refcount of the wrapped object is managed safely on the Rust side. Cc: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 39da92d commit fdc7f4b

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <linux/slab.h>
1010
#include <linux/refcount.h>
11+
#include <linux/sched.h>
1112

1213
/* `bindgen` gets confused at certain things. */
1314
const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;

rust/helpers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/refcount.h>
2525
#include <linux/mutex.h>
2626
#include <linux/spinlock.h>
27+
#include <linux/sched/signal.h>
2728

2829
__noreturn void rust_helper_BUG(void)
2930
{
@@ -60,6 +61,12 @@ void rust_helper_spin_unlock(spinlock_t *lock)
6061
}
6162
EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
6263

64+
int rust_helper_signal_pending(struct task_struct *t)
65+
{
66+
return signal_pending(t);
67+
}
68+
EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
69+
6370
refcount_t rust_helper_REFCOUNT_INIT(int n)
6471
{
6572
return (refcount_t)REFCOUNT_INIT(n);
@@ -96,6 +103,18 @@ long rust_helper_PTR_ERR(__force const void *ptr)
96103
}
97104
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
98105

106+
void rust_helper_get_task_struct(struct task_struct *t)
107+
{
108+
get_task_struct(t);
109+
}
110+
EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
111+
112+
void rust_helper_put_task_struct(struct task_struct *t)
113+
{
114+
put_task_struct(t);
115+
}
116+
EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
117+
99118
/*
100119
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
101120
* as the Rust `usize` type, so we can use it in contexts where Rust

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod static_assert;
4444
pub mod std_vendor;
4545
pub mod str;
4646
pub mod sync;
47+
pub mod task;
4748
pub mod types;
4849

4950
#[doc(hidden)]

rust/kernel/task.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Tasks (threads and processes).
4+
//!
5+
//! C header: [`include/linux/sched.h`](../../../../include/linux/sched.h).
6+
7+
use crate::{bindings, types::Opaque};
8+
use core::ptr;
9+
10+
/// Wraps the kernel's `struct task_struct`.
11+
///
12+
/// # Invariants
13+
///
14+
/// All instances are valid tasks created by the C portion of the kernel.
15+
///
16+
/// Instances of this type are always ref-counted, that is, a call to `get_task_struct` ensures
17+
/// that the allocation remains valid at least until the matching call to `put_task_struct`.
18+
#[repr(transparent)]
19+
pub struct Task(pub(crate) Opaque<bindings::task_struct>);
20+
21+
// SAFETY: It's OK to access `Task` through references from other threads because we're either
22+
// accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly
23+
// synchronised by C code (e.g., `signal_pending`).
24+
unsafe impl Sync for Task {}
25+
26+
/// The type of process identifiers (PIDs).
27+
type Pid = bindings::pid_t;
28+
29+
impl Task {
30+
/// Returns the group leader of the given task.
31+
pub fn group_leader(&self) -> &Task {
32+
// SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
33+
// have a valid group_leader.
34+
let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) };
35+
36+
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
37+
// and given that a task has a reference to its group leader, we know it must be valid for
38+
// the lifetime of the returned task reference.
39+
unsafe { &*ptr.cast() }
40+
}
41+
42+
/// Returns the PID of the given task.
43+
pub fn pid(&self) -> Pid {
44+
// SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
45+
// have a valid pid.
46+
unsafe { *ptr::addr_of!((*self.0.get()).pid) }
47+
}
48+
49+
/// Determines whether the given task has pending signals.
50+
pub fn signal_pending(&self) -> bool {
51+
// SAFETY: By the type invariant, we know that `self.0` is valid.
52+
unsafe { bindings::signal_pending(self.0.get()) != 0 }
53+
}
54+
55+
/// Wakes up the task.
56+
pub fn wake_up(&self) {
57+
// SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
58+
// And `wake_up_process` is safe to be called for any valid task, even if the task is
59+
// running.
60+
unsafe { bindings::wake_up_process(self.0.get()) };
61+
}
62+
}
63+
64+
// SAFETY: The type invariants guarantee that `Task` is always ref-counted.
65+
unsafe impl crate::types::AlwaysRefCounted for Task {
66+
fn inc_ref(&self) {
67+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
68+
unsafe { bindings::get_task_struct(self.0.get()) };
69+
}
70+
71+
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
72+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
73+
unsafe { bindings::put_task_struct(obj.cast().as_ptr()) }
74+
}
75+
}

0 commit comments

Comments
 (0)