xref: /openbmc/linux/rust/kernel/task.rs (revision 695c312ec5a68e4373d063ee649c7b925ffb5da7)
1313c4281SWedson Almeida Filho // SPDX-License-Identifier: GPL-2.0
2313c4281SWedson Almeida Filho 
3313c4281SWedson Almeida Filho //! Tasks (threads and processes).
4313c4281SWedson Almeida Filho //!
5313c4281SWedson Almeida Filho //! C header: [`include/linux/sched.h`](../../../../include/linux/sched.h).
6313c4281SWedson Almeida Filho 
7313c4281SWedson Almeida Filho use crate::{bindings, types::Opaque};
88da7a2b7SWedson Almeida Filho use core::{marker::PhantomData, ops::Deref, ptr};
98da7a2b7SWedson Almeida Filho 
108da7a2b7SWedson Almeida Filho /// Returns the currently running task.
118da7a2b7SWedson Almeida Filho #[macro_export]
128da7a2b7SWedson Almeida Filho macro_rules! current {
138da7a2b7SWedson Almeida Filho     () => {
148da7a2b7SWedson Almeida Filho         // SAFETY: Deref + addr-of below create a temporary `TaskRef` that cannot outlive the
158da7a2b7SWedson Almeida Filho         // caller.
168da7a2b7SWedson Almeida Filho         unsafe { &*$crate::task::Task::current() }
178da7a2b7SWedson Almeida Filho     };
188da7a2b7SWedson Almeida Filho }
19313c4281SWedson Almeida Filho 
20313c4281SWedson Almeida Filho /// Wraps the kernel's `struct task_struct`.
21313c4281SWedson Almeida Filho ///
22313c4281SWedson Almeida Filho /// # Invariants
23313c4281SWedson Almeida Filho ///
24313c4281SWedson Almeida Filho /// All instances are valid tasks created by the C portion of the kernel.
25313c4281SWedson Almeida Filho ///
26313c4281SWedson Almeida Filho /// Instances of this type are always ref-counted, that is, a call to `get_task_struct` ensures
27313c4281SWedson Almeida Filho /// that the allocation remains valid at least until the matching call to `put_task_struct`.
288da7a2b7SWedson Almeida Filho ///
298da7a2b7SWedson Almeida Filho /// # Examples
308da7a2b7SWedson Almeida Filho ///
318da7a2b7SWedson Almeida Filho /// The following is an example of getting the PID of the current thread with zero additional cost
328da7a2b7SWedson Almeida Filho /// when compared to the C version:
338da7a2b7SWedson Almeida Filho ///
348da7a2b7SWedson Almeida Filho /// ```
358da7a2b7SWedson Almeida Filho /// let pid = current!().pid();
368da7a2b7SWedson Almeida Filho /// ```
378da7a2b7SWedson Almeida Filho ///
388da7a2b7SWedson Almeida Filho /// Getting the PID of the current process, also zero additional cost:
398da7a2b7SWedson Almeida Filho ///
408da7a2b7SWedson Almeida Filho /// ```
418da7a2b7SWedson Almeida Filho /// let pid = current!().group_leader().pid();
428da7a2b7SWedson Almeida Filho /// ```
438da7a2b7SWedson Almeida Filho ///
448da7a2b7SWedson Almeida Filho /// Getting the current task and storing it in some struct. The reference count is automatically
458da7a2b7SWedson Almeida Filho /// incremented when creating `State` and decremented when it is dropped:
468da7a2b7SWedson Almeida Filho ///
478da7a2b7SWedson Almeida Filho /// ```
488da7a2b7SWedson Almeida Filho /// use kernel::{task::Task, types::ARef};
498da7a2b7SWedson Almeida Filho ///
508da7a2b7SWedson Almeida Filho /// struct State {
518da7a2b7SWedson Almeida Filho ///     creator: ARef<Task>,
528da7a2b7SWedson Almeida Filho ///     index: u32,
538da7a2b7SWedson Almeida Filho /// }
548da7a2b7SWedson Almeida Filho ///
558da7a2b7SWedson Almeida Filho /// impl State {
568da7a2b7SWedson Almeida Filho ///     fn new() -> Self {
578da7a2b7SWedson Almeida Filho ///         Self {
588da7a2b7SWedson Almeida Filho ///             creator: current!().into(),
598da7a2b7SWedson Almeida Filho ///             index: 0,
608da7a2b7SWedson Almeida Filho ///         }
618da7a2b7SWedson Almeida Filho ///     }
628da7a2b7SWedson Almeida Filho /// }
638da7a2b7SWedson Almeida Filho /// ```
64313c4281SWedson Almeida Filho #[repr(transparent)]
65313c4281SWedson Almeida Filho pub struct Task(pub(crate) Opaque<bindings::task_struct>);
66313c4281SWedson Almeida Filho 
67d09a6102SAlice Ryhl // SAFETY: By design, the only way to access a `Task` is via the `current` function or via an
68d09a6102SAlice Ryhl // `ARef<Task>` obtained through the `AlwaysRefCounted` impl. This means that the only situation in
69d09a6102SAlice Ryhl // which a `Task` can be accessed mutably is when the refcount drops to zero and the destructor
70d09a6102SAlice Ryhl // runs. It is safe for that to happen on any thread, so it is ok for this type to be `Send`.
71d09a6102SAlice Ryhl unsafe impl Send for Task {}
72d09a6102SAlice Ryhl 
73d09a6102SAlice Ryhl // SAFETY: It's OK to access `Task` through shared references from other threads because we're
74d09a6102SAlice Ryhl // either accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly
75313c4281SWedson Almeida Filho // synchronised by C code (e.g., `signal_pending`).
76313c4281SWedson Almeida Filho unsafe impl Sync for Task {}
77313c4281SWedson Almeida Filho 
78313c4281SWedson Almeida Filho /// The type of process identifiers (PIDs).
79313c4281SWedson Almeida Filho type Pid = bindings::pid_t;
80313c4281SWedson Almeida Filho 
81313c4281SWedson Almeida Filho impl Task {
828da7a2b7SWedson Almeida Filho     /// Returns a task reference for the currently executing task/thread.
838da7a2b7SWedson Almeida Filho     ///
848da7a2b7SWedson Almeida Filho     /// The recommended way to get the current task/thread is to use the
85*e8e7a528SMiguel Ojeda     /// [`current`] macro because it is safe.
868da7a2b7SWedson Almeida Filho     ///
878da7a2b7SWedson Almeida Filho     /// # Safety
888da7a2b7SWedson Almeida Filho     ///
898da7a2b7SWedson Almeida Filho     /// Callers must ensure that the returned object doesn't outlive the current task/thread.
current() -> impl Deref<Target = Task>908da7a2b7SWedson Almeida Filho     pub unsafe fn current() -> impl Deref<Target = Task> {
918da7a2b7SWedson Almeida Filho         struct TaskRef<'a> {
928da7a2b7SWedson Almeida Filho             task: &'a Task,
938da7a2b7SWedson Almeida Filho             _not_send: PhantomData<*mut ()>,
948da7a2b7SWedson Almeida Filho         }
958da7a2b7SWedson Almeida Filho 
968da7a2b7SWedson Almeida Filho         impl Deref for TaskRef<'_> {
978da7a2b7SWedson Almeida Filho             type Target = Task;
988da7a2b7SWedson Almeida Filho 
998da7a2b7SWedson Almeida Filho             fn deref(&self) -> &Self::Target {
1008da7a2b7SWedson Almeida Filho                 self.task
1018da7a2b7SWedson Almeida Filho             }
1028da7a2b7SWedson Almeida Filho         }
1038da7a2b7SWedson Almeida Filho 
1048da7a2b7SWedson Almeida Filho         // SAFETY: Just an FFI call with no additional safety requirements.
1058da7a2b7SWedson Almeida Filho         let ptr = unsafe { bindings::get_current() };
1068da7a2b7SWedson Almeida Filho 
1078da7a2b7SWedson Almeida Filho         TaskRef {
1088da7a2b7SWedson Almeida Filho             // SAFETY: If the current thread is still running, the current task is valid. Given
1098da7a2b7SWedson Almeida Filho             // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread
1108da7a2b7SWedson Almeida Filho             // (where it could potentially outlive the caller).
1118da7a2b7SWedson Almeida Filho             task: unsafe { &*ptr.cast() },
1128da7a2b7SWedson Almeida Filho             _not_send: PhantomData,
1138da7a2b7SWedson Almeida Filho         }
1148da7a2b7SWedson Almeida Filho     }
1158da7a2b7SWedson Almeida Filho 
116313c4281SWedson Almeida Filho     /// Returns the group leader of the given task.
group_leader(&self) -> &Task117313c4281SWedson Almeida Filho     pub fn group_leader(&self) -> &Task {
118313c4281SWedson Almeida Filho         // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
119313c4281SWedson Almeida Filho         // have a valid group_leader.
120313c4281SWedson Almeida Filho         let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) };
121313c4281SWedson Almeida Filho 
122313c4281SWedson Almeida Filho         // SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
123313c4281SWedson Almeida Filho         // and given that a task has a reference to its group leader, we know it must be valid for
124313c4281SWedson Almeida Filho         // the lifetime of the returned task reference.
125313c4281SWedson Almeida Filho         unsafe { &*ptr.cast() }
126313c4281SWedson Almeida Filho     }
127313c4281SWedson Almeida Filho 
128313c4281SWedson Almeida Filho     /// Returns the PID of the given task.
pid(&self) -> Pid129313c4281SWedson Almeida Filho     pub fn pid(&self) -> Pid {
130313c4281SWedson Almeida Filho         // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always
131313c4281SWedson Almeida Filho         // have a valid pid.
132313c4281SWedson Almeida Filho         unsafe { *ptr::addr_of!((*self.0.get()).pid) }
133313c4281SWedson Almeida Filho     }
134313c4281SWedson Almeida Filho 
135313c4281SWedson Almeida Filho     /// Determines whether the given task has pending signals.
signal_pending(&self) -> bool136313c4281SWedson Almeida Filho     pub fn signal_pending(&self) -> bool {
137313c4281SWedson Almeida Filho         // SAFETY: By the type invariant, we know that `self.0` is valid.
138313c4281SWedson Almeida Filho         unsafe { bindings::signal_pending(self.0.get()) != 0 }
139313c4281SWedson Almeida Filho     }
140313c4281SWedson Almeida Filho 
141313c4281SWedson Almeida Filho     /// Wakes up the task.
wake_up(&self)142313c4281SWedson Almeida Filho     pub fn wake_up(&self) {
143313c4281SWedson Almeida Filho         // SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid.
144313c4281SWedson Almeida Filho         // And `wake_up_process` is safe to be called for any valid task, even if the task is
145313c4281SWedson Almeida Filho         // running.
146313c4281SWedson Almeida Filho         unsafe { bindings::wake_up_process(self.0.get()) };
147313c4281SWedson Almeida Filho     }
148313c4281SWedson Almeida Filho }
149313c4281SWedson Almeida Filho 
150313c4281SWedson Almeida Filho // SAFETY: The type invariants guarantee that `Task` is always ref-counted.
151313c4281SWedson Almeida Filho unsafe impl crate::types::AlwaysRefCounted for Task {
inc_ref(&self)152313c4281SWedson Almeida Filho     fn inc_ref(&self) {
153313c4281SWedson Almeida Filho         // SAFETY: The existence of a shared reference means that the refcount is nonzero.
154313c4281SWedson Almeida Filho         unsafe { bindings::get_task_struct(self.0.get()) };
155313c4281SWedson Almeida Filho     }
156313c4281SWedson Almeida Filho 
dec_ref(obj: ptr::NonNull<Self>)157313c4281SWedson Almeida Filho     unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
158313c4281SWedson Almeida Filho         // SAFETY: The safety requirements guarantee that the refcount is nonzero.
159313c4281SWedson Almeida Filho         unsafe { bindings::put_task_struct(obj.cast().as_ptr()) }
160313c4281SWedson Almeida Filho     }
161313c4281SWedson Almeida Filho }
162