xref: /linux-6.15/rust/kernel/task.rs (revision 313c4281)
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 }
76