1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 2 3 //! Tasks (threads and processes). 3 //! Tasks (threads and processes). 4 //! 4 //! 5 //! C header: [`include/linux/sched.h`](srctre !! 5 //! C header: [`include/linux/sched.h`](../../../../include/linux/sched.h). 6 6 7 use crate::types::Opaque; !! 7 use crate::{bindings, types::Opaque}; 8 use core::{ !! 8 use core::{marker::PhantomData, ops::Deref, ptr}; 9 ffi::{c_int, c_long, c_uint}, << 10 marker::PhantomData, << 11 ops::Deref, << 12 ptr, << 13 }; << 14 << 15 /// A sentinel value used for infinite timeout << 16 pub const MAX_SCHEDULE_TIMEOUT: c_long = c_lon << 17 << 18 /// Bitmask for tasks that are sleeping in an << 19 pub const TASK_INTERRUPTIBLE: c_int = bindings << 20 /// Bitmask for tasks that are sleeping in an << 21 pub const TASK_UNINTERRUPTIBLE: c_int = bindin << 22 /// Convenience constant for waking up tasks r << 23 /// uninterruptible sleep. << 24 pub const TASK_NORMAL: c_uint = bindings::TASK << 25 9 26 /// Returns the currently running task. 10 /// Returns the currently running task. 27 #[macro_export] 11 #[macro_export] 28 macro_rules! current { 12 macro_rules! current { 29 () => { 13 () => { 30 // SAFETY: Deref + addr-of below creat 14 // SAFETY: Deref + addr-of below create a temporary `TaskRef` that cannot outlive the 31 // caller. 15 // caller. 32 unsafe { &*$crate::task::Task::current 16 unsafe { &*$crate::task::Task::current() } 33 }; 17 }; 34 } 18 } 35 19 36 /// Wraps the kernel's `struct task_struct`. 20 /// Wraps the kernel's `struct task_struct`. 37 /// 21 /// 38 /// # Invariants 22 /// # Invariants 39 /// 23 /// 40 /// All instances are valid tasks created by t 24 /// All instances are valid tasks created by the C portion of the kernel. 41 /// 25 /// 42 /// Instances of this type are always refcount !! 26 /// Instances of this type are always ref-counted, that is, a call to `get_task_struct` ensures 43 /// that the allocation remains valid at least 27 /// that the allocation remains valid at least until the matching call to `put_task_struct`. 44 /// 28 /// 45 /// # Examples 29 /// # Examples 46 /// 30 /// 47 /// The following is an example of getting the 31 /// The following is an example of getting the PID of the current thread with zero additional cost 48 /// when compared to the C version: 32 /// when compared to the C version: 49 /// 33 /// 50 /// ``` 34 /// ``` 51 /// let pid = current!().pid(); 35 /// let pid = current!().pid(); 52 /// ``` 36 /// ``` 53 /// 37 /// 54 /// Getting the PID of the current process, al 38 /// Getting the PID of the current process, also zero additional cost: 55 /// 39 /// 56 /// ``` 40 /// ``` 57 /// let pid = current!().group_leader().pid(); 41 /// let pid = current!().group_leader().pid(); 58 /// ``` 42 /// ``` 59 /// 43 /// 60 /// Getting the current task and storing it in 44 /// Getting the current task and storing it in some struct. The reference count is automatically 61 /// incremented when creating `State` and decr 45 /// incremented when creating `State` and decremented when it is dropped: 62 /// 46 /// 63 /// ``` 47 /// ``` 64 /// use kernel::{task::Task, types::ARef}; 48 /// use kernel::{task::Task, types::ARef}; 65 /// 49 /// 66 /// struct State { 50 /// struct State { 67 /// creator: ARef<Task>, 51 /// creator: ARef<Task>, 68 /// index: u32, 52 /// index: u32, 69 /// } 53 /// } 70 /// 54 /// 71 /// impl State { 55 /// impl State { 72 /// fn new() -> Self { 56 /// fn new() -> Self { 73 /// Self { 57 /// Self { 74 /// creator: current!().into(), 58 /// creator: current!().into(), 75 /// index: 0, 59 /// index: 0, 76 /// } 60 /// } 77 /// } 61 /// } 78 /// } 62 /// } 79 /// ``` 63 /// ``` 80 #[repr(transparent)] 64 #[repr(transparent)] 81 pub struct Task(pub(crate) Opaque<bindings::ta 65 pub struct Task(pub(crate) Opaque<bindings::task_struct>); 82 66 83 // SAFETY: By design, the only way to access a 67 // SAFETY: By design, the only way to access a `Task` is via the `current` function or via an 84 // `ARef<Task>` obtained through the `AlwaysRe 68 // `ARef<Task>` obtained through the `AlwaysRefCounted` impl. This means that the only situation in 85 // which a `Task` can be accessed mutably is w 69 // which a `Task` can be accessed mutably is when the refcount drops to zero and the destructor 86 // runs. It is safe for that to happen on any 70 // runs. It is safe for that to happen on any thread, so it is ok for this type to be `Send`. 87 unsafe impl Send for Task {} 71 unsafe impl Send for Task {} 88 72 89 // SAFETY: It's OK to access `Task` through sh 73 // SAFETY: It's OK to access `Task` through shared references from other threads because we're 90 // either accessing properties that don't chan 74 // either accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly 91 // synchronised by C code (e.g., `signal_pendi 75 // synchronised by C code (e.g., `signal_pending`). 92 unsafe impl Sync for Task {} 76 unsafe impl Sync for Task {} 93 77 94 /// The type of process identifiers (PIDs). 78 /// The type of process identifiers (PIDs). 95 type Pid = bindings::pid_t; 79 type Pid = bindings::pid_t; 96 80 97 impl Task { 81 impl Task { 98 /// Returns a task reference for the curre 82 /// Returns a task reference for the currently executing task/thread. 99 /// 83 /// 100 /// The recommended way to get the current 84 /// The recommended way to get the current task/thread is to use the 101 /// [`current`] macro because it is safe. 85 /// [`current`] macro because it is safe. 102 /// 86 /// 103 /// # Safety 87 /// # Safety 104 /// 88 /// 105 /// Callers must ensure that the returned 89 /// Callers must ensure that the returned object doesn't outlive the current task/thread. 106 pub unsafe fn current() -> impl Deref<Targ 90 pub unsafe fn current() -> impl Deref<Target = Task> { 107 struct TaskRef<'a> { 91 struct TaskRef<'a> { 108 task: &'a Task, 92 task: &'a Task, 109 _not_send: PhantomData<*mut ()>, 93 _not_send: PhantomData<*mut ()>, 110 } 94 } 111 95 112 impl Deref for TaskRef<'_> { 96 impl Deref for TaskRef<'_> { 113 type Target = Task; 97 type Target = Task; 114 98 115 fn deref(&self) -> &Self::Target { 99 fn deref(&self) -> &Self::Target { 116 self.task 100 self.task 117 } 101 } 118 } 102 } 119 103 120 // SAFETY: Just an FFI call with no ad 104 // SAFETY: Just an FFI call with no additional safety requirements. 121 let ptr = unsafe { bindings::get_curre 105 let ptr = unsafe { bindings::get_current() }; 122 106 123 TaskRef { 107 TaskRef { 124 // SAFETY: If the current thread i 108 // SAFETY: If the current thread is still running, the current task is valid. Given 125 // that `TaskRef` is not `Send`, w 109 // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread 126 // (where it could potentially out 110 // (where it could potentially outlive the caller). 127 task: unsafe { &*ptr.cast() }, 111 task: unsafe { &*ptr.cast() }, 128 _not_send: PhantomData, 112 _not_send: PhantomData, 129 } 113 } 130 } 114 } 131 115 132 /// Returns the group leader of the given 116 /// Returns the group leader of the given task. 133 pub fn group_leader(&self) -> &Task { 117 pub fn group_leader(&self) -> &Task { 134 // SAFETY: By the type invariant, we k 118 // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always 135 // have a valid `group_leader`. !! 119 // have a valid group_leader. 136 let ptr = unsafe { *ptr::addr_of!((*se 120 let ptr = unsafe { *ptr::addr_of!((*self.0.get()).group_leader) }; 137 121 138 // SAFETY: The lifetime of the returne 122 // SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`, 139 // and given that a task has a referen 123 // and given that a task has a reference to its group leader, we know it must be valid for 140 // the lifetime of the returned task r 124 // the lifetime of the returned task reference. 141 unsafe { &*ptr.cast() } 125 unsafe { &*ptr.cast() } 142 } 126 } 143 127 144 /// Returns the PID of the given task. 128 /// Returns the PID of the given task. 145 pub fn pid(&self) -> Pid { 129 pub fn pid(&self) -> Pid { 146 // SAFETY: By the type invariant, we k 130 // SAFETY: By the type invariant, we know that `self.0` is a valid task. Valid tasks always 147 // have a valid pid. 131 // have a valid pid. 148 unsafe { *ptr::addr_of!((*self.0.get() 132 unsafe { *ptr::addr_of!((*self.0.get()).pid) } 149 } 133 } 150 134 151 /// Determines whether the given task has 135 /// Determines whether the given task has pending signals. 152 pub fn signal_pending(&self) -> bool { 136 pub fn signal_pending(&self) -> bool { 153 // SAFETY: By the type invariant, we k 137 // SAFETY: By the type invariant, we know that `self.0` is valid. 154 unsafe { bindings::signal_pending(self 138 unsafe { bindings::signal_pending(self.0.get()) != 0 } 155 } 139 } 156 140 157 /// Wakes up the task. 141 /// Wakes up the task. 158 pub fn wake_up(&self) { 142 pub fn wake_up(&self) { 159 // SAFETY: By the type invariant, we k 143 // SAFETY: By the type invariant, we know that `self.0.get()` is non-null and valid. 160 // And `wake_up_process` is safe to be 144 // And `wake_up_process` is safe to be called for any valid task, even if the task is 161 // running. 145 // running. 162 unsafe { bindings::wake_up_process(sel 146 unsafe { bindings::wake_up_process(self.0.get()) }; 163 } 147 } 164 } 148 } 165 149 166 // SAFETY: The type invariants guarantee that !! 150 // SAFETY: The type invariants guarantee that `Task` is always ref-counted. 167 unsafe impl crate::types::AlwaysRefCounted for 151 unsafe impl crate::types::AlwaysRefCounted for Task { 168 fn inc_ref(&self) { 152 fn inc_ref(&self) { 169 // SAFETY: The existence of a shared r 153 // SAFETY: The existence of a shared reference means that the refcount is nonzero. 170 unsafe { bindings::get_task_struct(sel 154 unsafe { bindings::get_task_struct(self.0.get()) }; 171 } 155 } 172 156 173 unsafe fn dec_ref(obj: ptr::NonNull<Self>) 157 unsafe fn dec_ref(obj: ptr::NonNull<Self>) { 174 // SAFETY: The safety requirements gua 158 // SAFETY: The safety requirements guarantee that the refcount is nonzero. 175 unsafe { bindings::put_task_struct(obj 159 unsafe { bindings::put_task_struct(obj.cast().as_ptr()) } 176 } 160 } 177 } 161 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.