| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | //! Tasks (threads and processes). |
| 4 | //! |
| 5 | //! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h). |
| 6 | |
| 7 | use crate:: |
| 8 | bindings, |
| 9 | MmWithUser, |
| 10 | PidNamespace, |
| 11 | *, |
| 12 | ARef, |
| 13 | , |
| 14 | ; |
| 15 | use |
| 16 | Deref, |
| 17 | ptr, // |
| 18 | ; |
| 19 | |
| 20 | /// A sentinel value used for infinite timeouts. |
| 21 | pub const MAX_SCHEDULE_TIMEOUT: c_long = c_longMAX; |
| 22 | |
| 23 | /// Bitmask for tasks that are sleeping in an interruptible state. |
| 24 | pub const TASK_INTERRUPTIBLE: c_int = TASK_INTERRUPTIBLE as c_int; |
| 25 | /// Bitmask for tasks that are sleeping in an uninterruptible state. |
| 26 | pub const TASK_UNINTERRUPTIBLE: c_int = TASK_UNINTERRUPTIBLE as c_int; |
| 27 | /// Bitmask for tasks that are sleeping in a freezable state. |
| 28 | pub const TASK_FREEZABLE: c_int = TASK_FREEZABLE as c_int; |
| 29 | /// Convenience constant for waking up tasks regardless of whether they are in interruptible or |
| 30 | /// uninterruptible sleep. |
| 31 | pub const TASK_NORMAL: c_uint = TASK_NORMAL as c_uint; |
| 32 | |
| 33 | /// Returns the currently running task. |
| 34 | |
| 35 | |
| 36 | => |
| 37 | // SAFETY: This expression creates a temporary value that is dropped at the end of the |
| 38 | // caller's scope. The following mechanisms ensure that the resulting `&CurrentTask` cannot |
| 39 | // leave current task context: |
| 40 | // |
| 41 | // * To return to userspace, the caller must leave the current scope. |
| 42 | // * Operations such as `begin_new_exec()` are necessarily unsafe and the caller of |
| 43 | // `begin_new_exec()` is responsible for safety. |
| 44 | // * Rust abstractions for things such as a `kthread_use_mm()` scope must require the |
| 45 | // closure to be `Send`, so the `NotThreadSafe` field of `CurrentTask` ensures that the |
| 46 | // `&CurrentTask` cannot cross the scope in either direction. |
| 47 | unsafe |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | /// Wraps the kernel's `struct task_struct`. |
| 52 | /// |
| 53 | /// # Invariants |
| 54 | /// |
| 55 | /// All instances are valid tasks created by the C portion of the kernel. |
| 56 | /// |
| 57 | /// Instances of this type are always refcounted, that is, a call to `get_task_struct` ensures |
| 58 | /// that the allocation remains valid at least until the matching call to `put_task_struct`. |
| 59 | /// |
| 60 | /// # Examples |
| 61 | /// |
| 62 | /// The following is an example of getting the PID of the current thread with zero additional cost |
| 63 | /// when compared to the C version: |
| 64 | /// |
| 65 | /// ``` |
| 66 | /// let pid = current!().pid(); |
| 67 | /// ``` |
| 68 | /// |
| 69 | /// Getting the PID of the current process, also zero additional cost: |
| 70 | /// |
| 71 | /// ``` |
| 72 | /// let pid = current!().group_leader().pid(); |
| 73 | /// ``` |
| 74 | /// |
| 75 | /// Getting the current task and storing it in some struct. The reference count is automatically |
| 76 | /// incremented when creating `State` and decremented when it is dropped: |
| 77 | /// |
| 78 | /// ``` |
| 79 | /// use kernel::{task::Task, sync::aref::ARef}; |
| 80 | /// |
| 81 | /// struct State { |
| 82 | /// creator: ARef<Task>, |
| 83 | /// index: u32, |
| 84 | /// } |
| 85 | /// |
| 86 | /// impl State { |
| 87 | /// fn new() -> Self { |
| 88 | /// Self { |
| 89 | /// creator: ARef::from(&**current!()), |
| 90 | /// index: 0, |
| 91 | /// } |
| 92 | /// } |
| 93 | /// } |
| 94 | /// ``` |
| 95 | |
| 96 | ); |
| 97 | |
| 98 | // SAFETY: By design, the only way to access a `Task` is via the `current` function or via an |
| 99 | // `ARef<Task>` obtained through the `AlwaysRefCounted` impl. This means that the only situation in |
| 100 | // which a `Task` can be accessed mutably is when the refcount drops to zero and the destructor |
| 101 | // runs. It is safe for that to happen on any thread, so it is ok for this type to be `Send`. |
| 102 | unsafe |
| 103 | |
| 104 | // SAFETY: It's OK to access `Task` through shared references from other threads because we're |
| 105 | // either accessing properties that don't change (e.g., `pid`, `group_leader`) or that are properly |
| 106 | // synchronised by C code (e.g., `signal_pending`). |
| 107 | unsafe |
| 108 | |
| 109 | /// Represents the [`Task`] in the `current` global. |
| 110 | /// |
| 111 | /// This type exists to provide more efficient operations that are only valid on the current task. |
| 112 | /// For example, to retrieve the pid-namespace of a task, you must use rcu protection unless it is |
| 113 | /// the current task. |
| 114 | /// |
| 115 | /// # Invariants |
| 116 | /// |
| 117 | /// Each value of this type must only be accessed from the task context it was created within. |
| 118 | /// |
| 119 | /// Of course, every thread is in a different task context, but for the purposes of this invariant, |
| 120 | /// these operations also permanently leave the task context: |
| 121 | /// |
| 122 | /// * Returning to userspace from system call context. |
| 123 | /// * Calling `release_task()`. |
| 124 | /// * Calling `begin_new_exec()` in a binary format loader. |
| 125 | /// |
| 126 | /// Other operations temporarily create a new sub-context: |
| 127 | /// |
| 128 | /// * Calling `kthread_use_mm()` creates a new context, and `kthread_unuse_mm()` returns to the |
| 129 | /// old context. |
| 130 | /// |
| 131 | /// This means that a `CurrentTask` obtained before a `kthread_use_mm()` call may be used again |
| 132 | /// once `kthread_unuse_mm()` is called, but it must not be used between these two calls. |
| 133 | /// Conversely, a `CurrentTask` obtained between a `kthread_use_mm()`/`kthread_unuse_mm()` pair |
| 134 | /// must not be used after `kthread_unuse_mm()`. |
| 135 | |
| 136 | ; |
| 137 | |
| 138 | // Make all `Task` methods available on `CurrentTask`. |
| 139 | |
| 140 | type Target = Task; |
| 141 | |
| 142 | |
| 143 | &self.0 |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// The type of process identifiers (PIDs). |
| 148 | pub type Pid = pid_t; |
| 149 | |
| 150 | /// The type of user identifiers (UIDs). |
| 151 | |
| 152 | |
| 153 | kuid: kuid_t, |
| 154 | |
| 155 | |
| 156 | |
| 157 | /// Returns a raw pointer to the current task. |
| 158 | /// |
| 159 | /// It is up to the user to use the pointer correctly. |
| 160 | |
| 161 | *mut task_struct |
| 162 | // SAFETY: Getting the current pointer is always safe. |
| 163 | unsafe |
| 164 | |
| 165 | |
| 166 | /// Returns a task reference for the currently executing task/thread. |
| 167 | /// |
| 168 | /// The recommended way to get the current task/thread is to use the |
| 169 | /// [`current`] macro because it is safe. |
| 170 | /// |
| 171 | /// # Safety |
| 172 | /// |
| 173 | /// Callers must ensure that the returned object is only used to access a [`CurrentTask`] |
| 174 | /// within the task context that was active when this function was called. For more details, |
| 175 | /// see the invariants section for [`CurrentTask`]. |
| 176 | |
| 177 | pub unsafe |
| 178 | |
| 179 | task: *const CurrentTask, |
| 180 | |
| 181 | |
| 182 | |
| 183 | type Target = CurrentTask; |
| 184 | |
| 185 | |
| 186 | // SAFETY: The returned reference borrows from this `TaskRef`, so it cannot outlive |
| 187 | // the `TaskRef`, which the caller of `Task::current()` has promised will not |
| 188 | // outlive the task/thread for which `self.task` is the `current` pointer. Thus, it |
| 189 | // is okay to return a `CurrentTask` reference here. |
| 190 | unsafe |
| 191 | |
| 192 | |
| 193 | |
| 194 | TaskRef |
| 195 | // CAST: The layout of `struct task_struct` and `CurrentTask` is identical. |
| 196 | task: current_raw.cast, |
| 197 | |
| 198 | |
| 199 | |
| 200 | /// Returns a raw pointer to the task. |
| 201 | |
| 202 | *mut task_struct |
| 203 | self.0.get |
| 204 | |
| 205 | |
| 206 | /// Returns the PID of the given task. |
| 207 | |
| 208 | // SAFETY: The pid of a task never changes after initialization, so reading this field is |
| 209 | // not a data race. |
| 210 | unsafe |
| 211 | |
| 212 | |
| 213 | /// Returns the TGID (Thread Group ID / Process ID) of the given task. |
| 214 | |
| 215 | // SAFETY: The tgid of a task never changes after initialization, so reading this field is |
| 216 | // not a data race. |
| 217 | unsafe |
| 218 | |
| 219 | |
| 220 | /// Returns the objective real UID of the given task. |
| 221 | |
| 222 | |
| 223 | // SAFETY: It's always safe to call `task_uid` on a valid task. |
| 224 | from_raw |
| 225 | |
| 226 | |
| 227 | /// Determines whether the given task has pending signals. |
| 228 | |
| 229 | |
| 230 | // SAFETY: It's always safe to call `signal_pending` on a valid task. |
| 231 | unsafe |
| 232 | |
| 233 | |
| 234 | /// Returns task's pid namespace with elevated reference count |
| 235 | |
| 236 | |
| 237 | // SAFETY: By the type invariant, we know that `self.0` is valid. |
| 238 | let ptr = unsafe ; |
| 239 | if ptr.is_null |
| 240 | None |
| 241 | else |
| 242 | // SAFETY: `ptr` is valid by the safety requirements of this function. And we own a |
| 243 | // reference count via `task_get_pid_ns()`. |
| 244 | // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::pid_namespace`. |
| 245 | Some |
| 246 | |
| 247 | |
| 248 | |
| 249 | /// Returns the given task's pid in the provided pid namespace. |
| 250 | |
| 251 | |
| 252 | |
| 253 | let pidns = match pidns |
| 254 | Some => pidns.as_ptr, |
| 255 | None => null_mut, |
| 256 | ; |
| 257 | // SAFETY: By the type invariant, we know that `self.0` is valid. We received a valid |
| 258 | // PidNamespace that we can use as a pointer or we received an empty PidNamespace and |
| 259 | // thus pass a null pointer. The underlying C function is safe to be used with NULL |
| 260 | // pointers. |
| 261 | unsafe |
| 262 | |
| 263 | |
| 264 | /// Wakes up the task. |
| 265 | |
| 266 | |
| 267 | // SAFETY: It's always safe to call `wake_up_process` on a valid task, even if the task |
| 268 | // running. |
| 269 | unsafe ; |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | /// Access the address space of the current task. |
| 275 | /// |
| 276 | /// This function does not touch the refcount of the mm. |
| 277 | |
| 278 | |
| 279 | // SAFETY: The `mm` field of `current` is not modified from other threads, so reading it is |
| 280 | // not a data race. |
| 281 | let mm = unsafe ; |
| 282 | |
| 283 | if mm.is_null |
| 284 | return None; |
| 285 | |
| 286 | |
| 287 | // SAFETY: If `current->mm` is non-null, then it references a valid mm with a non-zero |
| 288 | // value of `mm_users`. Furthermore, the returned `&MmWithUser` borrows from this |
| 289 | // `CurrentTask`, so it cannot escape the scope in which the current pointer was obtained. |
| 290 | // |
| 291 | // This is safe even if `kthread_use_mm()`/`kthread_unuse_mm()` are used. There are two |
| 292 | // relevant cases: |
| 293 | // * If the `&CurrentTask` was created before `kthread_use_mm()`, then it cannot be |
| 294 | // accessed during the `kthread_use_mm()`/`kthread_unuse_mm()` scope due to the |
| 295 | // `NotThreadSafe` field of `CurrentTask`. |
| 296 | // * If the `&CurrentTask` was created within a `kthread_use_mm()`/`kthread_unuse_mm()` |
| 297 | // scope, then the `&CurrentTask` cannot escape that scope, so the returned `&MmWithUser` |
| 298 | // also cannot escape that scope. |
| 299 | // In either case, it's not possible to read `current->mm` and keep using it after the |
| 300 | // scope is ended with `kthread_unuse_mm()`. |
| 301 | Some |
| 302 | |
| 303 | |
| 304 | /// Access the pid namespace of the current task. |
| 305 | /// |
| 306 | /// This function does not touch the refcount of the namespace or use RCU protection. |
| 307 | /// |
| 308 | /// To access the pid namespace of another task, see [`Task::get_pid_ns`]. |
| 309 | |
| 310 | |
| 311 | |
| 312 | // SAFETY: It is safe to call `task_active_pid_ns` without RCU protection when calling it |
| 313 | // on the current task. |
| 314 | let active_ns = unsafe ; |
| 315 | |
| 316 | if active_ns.is_null |
| 317 | return None; |
| 318 | |
| 319 | |
| 320 | // The lifetime of `PidNamespace` is bound to `Task` and `struct pid`. |
| 321 | // |
| 322 | // The `PidNamespace` of a `Task` doesn't ever change once the `Task` is alive. |
| 323 | // |
| 324 | // From system call context retrieving the `PidNamespace` for the current task is always |
| 325 | // safe and requires neither RCU locking nor a reference count to be held. Retrieving the |
| 326 | // `PidNamespace` after `release_task()` for current will return `NULL` but no codepath |
| 327 | // like that is exposed to Rust. |
| 328 | // |
| 329 | // SAFETY: If `current`'s pid ns is non-null, then it references a valid pid ns. |
| 330 | // Furthermore, the returned `&PidNamespace` borrows from this `CurrentTask`, so it cannot |
| 331 | // escape the scope in which the current pointer was obtained, e.g. it cannot live past a |
| 332 | // `release_task()` call. |
| 333 | Some |
| 334 | |
| 335 | |
| 336 | /// Returns the group leader of the current task. |
| 337 | |
| 338 | // SAFETY: The group leader of a task never changes while the task is running, and `self` |
| 339 | // is the current task, which is guaranteed running. |
| 340 | let ptr = unsafe ; |
| 341 | |
| 342 | // SAFETY: `current->group_leader` stays valid for at least the duration in which `current` |
| 343 | // is running, and the signature of this function ensures that the returned `&Task` can |
| 344 | // only be used while `current` is still valid, thus still running. |
| 345 | unsafe |
| 346 | |
| 347 | |
| 348 | |
| 349 | // SAFETY: The type invariants guarantee that `Task` is always refcounted. |
| 350 | unsafe |
| 351 | |
| 352 | |
| 353 | // SAFETY: The existence of a shared reference means that the refcount is nonzero. |
| 354 | unsafe ; |
| 355 | |
| 356 | |
| 357 | |
| 358 | unsafe |
| 359 | // SAFETY: The safety requirements guarantee that the refcount is nonzero. |
| 360 | unsafe |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | eq |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | /// Get the current subjective effective UID. |
| 375 | |
| 376 | |
| 377 | // SAFETY: Just an FFI call. |
| 378 | Selffrom_raw |
| 379 | |
| 380 | |
| 381 | /// Create a `Kuid` given the raw C type. |
| 382 | |
| 383 | |
| 384 | Self |
| 385 | |
| 386 | |
| 387 | /// Turn this kuid into the raw C type. |
| 388 | |
| 389 | |
| 390 | self.kuid |
| 391 | |
| 392 | |
| 393 | /// Converts this kernel UID into a userspace UID. |
| 394 | /// |
| 395 | /// Uses the namespace of the current task. |
| 396 | |
| 397 | |
| 398 | // SAFETY: Just an FFI call. |
| 399 | unsafe |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | // SAFETY: Just an FFI call. |
| 407 | unsafe |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | /// Annotation for functions that can sleep. |
| 414 | /// |
| 415 | /// Equivalent to the C side [`might_sleep()`], this function serves as |
| 416 | /// a debugging aid and a potential scheduling point. |
| 417 | /// |
| 418 | /// This function can only be used in a nonatomic context. |
| 419 | /// |
| 420 | /// [`might_sleep()`]: https://docs.kernel.org/driver-api/basics.html#c.might_sleep |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | let loc = caller; |
| 427 | let file = file_from_location; |
| 428 | |
| 429 | // SAFETY: `file.as_ptr()` is valid for reading and guaranteed to be nul-terminated. |
| 430 | unsafe |
| 431 | |
| 432 | |
| 433 | // SAFETY: Always safe to call. |
| 434 | unsafe |
| 435 |