ostd/task/atomic_mode.rs
1// SPDX-License-Identifier: MPL-2.0
2//! Atomic Mode
3//!
4//! Multitasking, while powerful, can sometimes lead to undesirable
5//! or catastrophic consequences if being misused.
6//! For instance, a user of OSTD might accidentally write an IRQ handler
7//! that relies on mutexes,
8//! which could attempt to sleep within an interrupt context---something that must be avoided.
9//! Another common mistake is
10//! acquiring a spinlock in a task context and then attempting to yield or sleep,
11//! which can easily lead to deadlocks.
12//!
13//! To mitigate the risks associated with improper multitasking,
14//! we introduce the concept of atomic mode.
15//! Kernel code is considered to be running in atomic mode
16//! if one of the following conditions is met:
17//!
18//! 1. Task preemption is disabled, such as when a spinlock is held.
19//! 2. Local IRQs are disabled, such as during interrupt context.
20//!
21//! While in atomic mode,
22//! any attempt to perform "sleep-like" actions will trigger a panic:
23//!
24//! 1. Switching to another task.
25//! 2. Switching to user space.
26//!
27//! This module provides API to detect such "sleep-like" actions.
28use core::sync::atomic::Ordering;
29
30use vstd::prelude::*;
31/*
32/// Marks a function as one that might sleep.
33///
34/// This function will panic if it is executed in atomic mode.
35#[track_caller]
36pub fn might_sleep() {
37 let preempt_count = super::preempt::cpu_local::get_guard_count();
38 let is_local_irq_enabled = crate::arch::irq::is_local_enabled();
39 if (preempt_count != 0 || !is_local_irq_enabled)
40 && !crate::IN_BOOTSTRAP_CONTEXT.load(Ordering::Relaxed)
41 {
42 panic!(
43 "This function might break atomic mode (preempt_count = {}, is_local_irq_enabled = {})",
44 preempt_count, is_local_irq_enabled
45 );
46 }
47}*/
48
49/// A marker trait for guard types that enforce the atomic mode.
50///
51/// Key kernel primitives such as `SpinLock` and `Rcu` rely on
52/// [the atomic mode](crate::task::atomic_mode) for correctness or soundness.
53/// The existence of such a guard guarantees that the current task is executing
54/// in the atomic mode.
55///
56/// It requires [`core::fmt::Debug`] by default to make it easier to derive
57/// [`Debug`] for types with `&dyn InAtomicMode`.
58///
59/// # Safety
60///
61/// The implementer must ensure that the atomic mode is maintained while
62/// the guard type is alive.
63///
64/// A guard is only valid evidence about the task that created it: it proves
65/// nothing about any other task. Therefore, such guards must be `!Send` and
66/// `!Sync`, so that the guards and their references are confined to the
67/// creating task.
68#[verus_verify]
69pub unsafe trait InAtomicMode: core::fmt::Debug {}
70
71/// Abstracts any type from which one can obtain a reference to an atomic-mode guard.
72#[verus_verify]
73pub trait AsAtomicModeGuard {
74 /// Returns a guard for the atomic mode.
75 fn as_atomic_mode_guard(&self) -> &dyn InAtomicMode;
76}
77
78#[verus_verify]
79impl<G: InAtomicMode> AsAtomicModeGuard for G {
80 fn as_atomic_mode_guard(&self) -> &dyn InAtomicMode {
81 self
82 }
83}
84
85#[verus_verify]
86impl AsAtomicModeGuard for dyn InAtomicMode + '_ {
87 fn as_atomic_mode_guard(&self) -> &dyn InAtomicMode {
88 self
89 }
90}