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 ffi::{c_int, c_long, c_uint}, 10 mm::MmWithUser, 11 pid_namespace::PidNamespace, 12 types::{ARef, NotThreadSafe, Opaque}, 13 }; 14 use core::{ 15 cmp::{Eq, PartialEq}, 16 ops::Deref, 17 ptr, 18 }; 19 20 /// A sentinel value used for infinite timeouts. 21 pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX; 22 23 /// Bitmask for tasks that are sleeping in an interruptible state. 24 pub const TASK_INTERRUPTIBLE: c_int = bindings::TASK_INTERRUPTIBLE as c_int; 25 /// Bitmask for tasks that are sleeping in an uninterruptible state. 26 pub const TASK_UNINTERRUPTIBLE: c_int = bindings::TASK_UNINTERRUPTIBLE as c_int; 27 /// Bitmask for tasks that are sleeping in a freezable state. 28 pub const TASK_FREEZABLE: c_int = bindings::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 = bindings::TASK_NORMAL as c_uint; 32 33 /// Returns the currently running task. 34 #[macro_export] 35 macro_rules! current { 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 { &*$crate::task::Task::current() } 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, types::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 #[repr(transparent)] 96 pub struct Task(pub(crate) Opaque<bindings::task_struct>); 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 impl Send for Task {} 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 impl Sync for Task {} 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 #[repr(transparent)] 136 pub struct CurrentTask(Task, NotThreadSafe); 137 138 // Make all `Task` methods available on `CurrentTask`. 139 impl Deref for CurrentTask { 140 type Target = Task; 141 #[inline] deref(&self) -> &Task142 fn deref(&self) -> &Task { 143 &self.0 144 } 145 } 146 147 /// The type of process identifiers (PIDs). 148 pub type Pid = bindings::pid_t; 149 150 /// The type of user identifiers (UIDs). 151 #[derive(Copy, Clone)] 152 pub struct Kuid { 153 kuid: bindings::kuid_t, 154 } 155 156 impl Task { 157 /// Returns a raw pointer to the current task. 158 /// 159 /// It is up to the user to use the pointer correctly. 160 #[inline] current_raw() -> *mut bindings::task_struct161 pub fn current_raw() -> *mut bindings::task_struct { 162 // SAFETY: Getting the current pointer is always safe. 163 unsafe { bindings::get_current() } 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`]. current() -> impl Deref<Target = CurrentTask>176 pub unsafe fn current() -> impl Deref<Target = CurrentTask> { 177 struct TaskRef { 178 task: *const CurrentTask, 179 } 180 181 impl Deref for TaskRef { 182 type Target = CurrentTask; 183 184 fn deref(&self) -> &Self::Target { 185 // SAFETY: The returned reference borrows from this `TaskRef`, so it cannot outlive 186 // the `TaskRef`, which the caller of `Task::current()` has promised will not 187 // outlive the task/thread for which `self.task` is the `current` pointer. Thus, it 188 // is okay to return a `CurrentTask` reference here. 189 unsafe { &*self.task } 190 } 191 } 192 193 TaskRef { 194 // CAST: The layout of `struct task_struct` and `CurrentTask` is identical. 195 task: Task::current_raw().cast(), 196 } 197 } 198 199 /// Returns a raw pointer to the task. 200 #[inline] as_ptr(&self) -> *mut bindings::task_struct201 pub fn as_ptr(&self) -> *mut bindings::task_struct { 202 self.0.get() 203 } 204 205 /// Returns the group leader of the given task. group_leader(&self) -> &Task206 pub fn group_leader(&self) -> &Task { 207 // SAFETY: The group leader of a task never changes after initialization, so reading this 208 // field is not a data race. 209 let ptr = unsafe { *ptr::addr_of!((*self.as_ptr()).group_leader) }; 210 211 // SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`, 212 // and given that a task has a reference to its group leader, we know it must be valid for 213 // the lifetime of the returned task reference. 214 unsafe { &*ptr.cast() } 215 } 216 217 /// Returns the PID of the given task. pid(&self) -> Pid218 pub fn pid(&self) -> Pid { 219 // SAFETY: The pid of a task never changes after initialization, so reading this field is 220 // not a data race. 221 unsafe { *ptr::addr_of!((*self.as_ptr()).pid) } 222 } 223 224 /// Returns the UID of the given task. uid(&self) -> Kuid225 pub fn uid(&self) -> Kuid { 226 // SAFETY: It's always safe to call `task_uid` on a valid task. 227 Kuid::from_raw(unsafe { bindings::task_uid(self.as_ptr()) }) 228 } 229 230 /// Returns the effective UID of the given task. euid(&self) -> Kuid231 pub fn euid(&self) -> Kuid { 232 // SAFETY: It's always safe to call `task_euid` on a valid task. 233 Kuid::from_raw(unsafe { bindings::task_euid(self.as_ptr()) }) 234 } 235 236 /// Determines whether the given task has pending signals. signal_pending(&self) -> bool237 pub fn signal_pending(&self) -> bool { 238 // SAFETY: It's always safe to call `signal_pending` on a valid task. 239 unsafe { bindings::signal_pending(self.as_ptr()) != 0 } 240 } 241 242 /// Returns task's pid namespace with elevated reference count get_pid_ns(&self) -> Option<ARef<PidNamespace>>243 pub fn get_pid_ns(&self) -> Option<ARef<PidNamespace>> { 244 // SAFETY: By the type invariant, we know that `self.0` is valid. 245 let ptr = unsafe { bindings::task_get_pid_ns(self.0.get()) }; 246 if ptr.is_null() { 247 None 248 } else { 249 // SAFETY: `ptr` is valid by the safety requirements of this function. And we own a 250 // reference count via `task_get_pid_ns()`. 251 // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::pid_namespace`. 252 Some(unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(ptr.cast::<PidNamespace>())) }) 253 } 254 } 255 256 /// Returns the given task's pid in the provided pid namespace. 257 #[doc(alias = "task_tgid_nr_ns")] tgid_nr_ns(&self, pidns: Option<&PidNamespace>) -> Pid258 pub fn tgid_nr_ns(&self, pidns: Option<&PidNamespace>) -> Pid { 259 let pidns = match pidns { 260 Some(pidns) => pidns.as_ptr(), 261 None => core::ptr::null_mut(), 262 }; 263 // SAFETY: By the type invariant, we know that `self.0` is valid. We received a valid 264 // PidNamespace that we can use as a pointer or we received an empty PidNamespace and 265 // thus pass a null pointer. The underlying C function is safe to be used with NULL 266 // pointers. 267 unsafe { bindings::task_tgid_nr_ns(self.0.get(), pidns) } 268 } 269 270 /// Wakes up the task. wake_up(&self)271 pub fn wake_up(&self) { 272 // SAFETY: It's always safe to call `wake_up_process` on a valid task, even if the task 273 // running. 274 unsafe { bindings::wake_up_process(self.as_ptr()) }; 275 } 276 277 /// Check if the task has the given capability without logging to the audit log. has_capability_noaudit(&self, capability: i32) -> bool278 pub fn has_capability_noaudit(&self, capability: i32) -> bool { 279 // SAFETY: By the type invariant, we know that `self.0.get()` is valid. 280 unsafe { bindings::has_capability_noaudit(self.0.get(), capability) } 281 } 282 283 /// Returns the current scheduling policy. policy(&self) -> u32284 pub fn policy(&self) -> u32 { 285 // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount. 286 // 287 // This uses a volatile read because C code may be modifying this field in parallel using 288 // non-atomic unsynchronized writes. This corresponds to how the C macro READ_ONCE is 289 // implemented. 290 unsafe { core::ptr::addr_of!((*self.0.get()).policy).read_volatile() } 291 } 292 293 /// Returns the current normal priority. normal_prio(&self) -> i32294 pub fn normal_prio(&self) -> i32 { 295 // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount. 296 // 297 // This uses a volatile read because C code may be modifying this field in parallel using 298 // non-atomic unsynchronized writes. This corresponds to how the C macro READ_ONCE is 299 // implemented. 300 unsafe { core::ptr::addr_of!((*self.0.get()).normal_prio).read_volatile() } 301 } 302 303 /// Get the rlimit value for RTPRIO. rlimit_rtprio(&self) -> i32304 pub fn rlimit_rtprio(&self) -> i32 { 305 // SAFETY: By the type invariant, we know that `self.0.get()` is valid, and RLIMIT_RTPRIO 306 // is a valid limit type. 307 unsafe { bindings::task_rlimit(self.0.get(), bindings::RLIMIT_RTPRIO) as i32 } 308 } 309 310 /// Get the rlimit value for NICE, converted to a nice value. rlimit_nice(&self) -> i32311 pub fn rlimit_nice(&self) -> i32 { 312 // SAFETY: By the type invariant, we know that `self.0.get()` is valid, and RLIMIT_NICE 313 // is a valid limit type. 314 let prio = unsafe { bindings::task_rlimit(self.0.get(), bindings::RLIMIT_NICE) as i32 }; 315 // Convert rlimit style value [1,40] to nice value [-20, 19]. 316 bindings::MAX_NICE as i32 - prio + 1 317 } 318 319 /// Set the scheduling properties for this task without checking whether the task is allowed to 320 /// set them. sched_setscheduler_nocheck( &self, policy: i32, sched_priority: i32, reset_on_fork: bool, )321 pub fn sched_setscheduler_nocheck( 322 &self, 323 policy: i32, 324 sched_priority: i32, 325 reset_on_fork: bool, 326 ) { 327 let params = bindings::sched_param { sched_priority }; 328 329 let mut policy = policy; 330 if reset_on_fork { 331 policy |= bindings::SCHED_RESET_ON_FORK as i32; 332 } 333 unsafe { bindings::sched_setscheduler_nocheck(self.0.get(), policy, ¶ms) }; 334 } 335 336 /// Set the nice value of this task. set_user_nice(&self, nice: i32)337 pub fn set_user_nice(&self, nice: i32) { 338 unsafe { bindings::set_user_nice(self.0.get(), nice as _) }; 339 } 340 } 341 342 impl CurrentTask { 343 /// Access the address space of the current task. 344 /// 345 /// This function does not touch the refcount of the mm. 346 #[inline] mm(&self) -> Option<&MmWithUser>347 pub fn mm(&self) -> Option<&MmWithUser> { 348 // SAFETY: The `mm` field of `current` is not modified from other threads, so reading it is 349 // not a data race. 350 let mm = unsafe { (*self.as_ptr()).mm }; 351 352 if mm.is_null() { 353 return None; 354 } 355 356 // SAFETY: If `current->mm` is non-null, then it references a valid mm with a non-zero 357 // value of `mm_users`. Furthermore, the returned `&MmWithUser` borrows from this 358 // `CurrentTask`, so it cannot escape the scope in which the current pointer was obtained. 359 // 360 // This is safe even if `kthread_use_mm()`/`kthread_unuse_mm()` are used. There are two 361 // relevant cases: 362 // * If the `&CurrentTask` was created before `kthread_use_mm()`, then it cannot be 363 // accessed during the `kthread_use_mm()`/`kthread_unuse_mm()` scope due to the 364 // `NotThreadSafe` field of `CurrentTask`. 365 // * If the `&CurrentTask` was created within a `kthread_use_mm()`/`kthread_unuse_mm()` 366 // scope, then the `&CurrentTask` cannot escape that scope, so the returned `&MmWithUser` 367 // also cannot escape that scope. 368 // In either case, it's not possible to read `current->mm` and keep using it after the 369 // scope is ended with `kthread_unuse_mm()`. 370 Some(unsafe { MmWithUser::from_raw(mm) }) 371 } 372 373 /// Access the pid namespace of the current task. 374 /// 375 /// This function does not touch the refcount of the namespace or use RCU protection. 376 /// 377 /// To access the pid namespace of another task, see [`Task::get_pid_ns`]. 378 #[doc(alias = "task_active_pid_ns")] 379 #[inline] active_pid_ns(&self) -> Option<&PidNamespace>380 pub fn active_pid_ns(&self) -> Option<&PidNamespace> { 381 // SAFETY: It is safe to call `task_active_pid_ns` without RCU protection when calling it 382 // on the current task. 383 let active_ns = unsafe { bindings::task_active_pid_ns(self.as_ptr()) }; 384 385 if active_ns.is_null() { 386 return None; 387 } 388 389 // The lifetime of `PidNamespace` is bound to `Task` and `struct pid`. 390 // 391 // The `PidNamespace` of a `Task` doesn't ever change once the `Task` is alive. 392 // 393 // From system call context retrieving the `PidNamespace` for the current task is always 394 // safe and requires neither RCU locking nor a reference count to be held. Retrieving the 395 // `PidNamespace` after `release_task()` for current will return `NULL` but no codepath 396 // like that is exposed to Rust. 397 // 398 // SAFETY: If `current`'s pid ns is non-null, then it references a valid pid ns. 399 // Furthermore, the returned `&PidNamespace` borrows from this `CurrentTask`, so it cannot 400 // escape the scope in which the current pointer was obtained, e.g. it cannot live past a 401 // `release_task()` call. 402 Some(unsafe { PidNamespace::from_ptr(active_ns) }) 403 } 404 } 405 406 // SAFETY: The type invariants guarantee that `Task` is always refcounted. 407 unsafe impl crate::types::AlwaysRefCounted for Task { inc_ref(&self)408 fn inc_ref(&self) { 409 // SAFETY: The existence of a shared reference means that the refcount is nonzero. 410 unsafe { bindings::get_task_struct(self.as_ptr()) }; 411 } 412 dec_ref(obj: ptr::NonNull<Self>)413 unsafe fn dec_ref(obj: ptr::NonNull<Self>) { 414 // SAFETY: The safety requirements guarantee that the refcount is nonzero. 415 unsafe { bindings::put_task_struct(obj.cast().as_ptr()) } 416 } 417 } 418 419 impl Kuid { 420 /// Get the current euid. 421 #[inline] current_euid() -> Kuid422 pub fn current_euid() -> Kuid { 423 // SAFETY: Just an FFI call. 424 Self::from_raw(unsafe { bindings::current_euid() }) 425 } 426 427 /// Create a `Kuid` given the raw C type. 428 #[inline] from_raw(kuid: bindings::kuid_t) -> Self429 pub fn from_raw(kuid: bindings::kuid_t) -> Self { 430 Self { kuid } 431 } 432 433 /// Turn this kuid into the raw C type. 434 #[inline] into_raw(self) -> bindings::kuid_t435 pub fn into_raw(self) -> bindings::kuid_t { 436 self.kuid 437 } 438 439 /// Converts this kernel UID into a userspace UID. 440 /// 441 /// Uses the namespace of the current task. 442 #[inline] into_uid_in_current_ns(self) -> bindings::uid_t443 pub fn into_uid_in_current_ns(self) -> bindings::uid_t { 444 // SAFETY: Just an FFI call. 445 unsafe { bindings::from_kuid(bindings::current_user_ns(), self.kuid) } 446 } 447 } 448 449 impl PartialEq for Kuid { 450 #[inline] eq(&self, other: &Kuid) -> bool451 fn eq(&self, other: &Kuid) -> bool { 452 // SAFETY: Just an FFI call. 453 unsafe { bindings::uid_eq(self.kuid, other.kuid) } 454 } 455 } 456 457 impl Eq for Kuid {} 458