Add an abstraction for the per-task allocation policies exposed by the kernel through paired save/restore helpers in `linux/sched/mm.h`: `memalloc_noio`, `memalloc_nofs`, `memalloc_noreclaim` and `memalloc_pin`. Each pair wraps `memalloc_flags_save` and `memalloc_flags_restore`. The save call sets bits in `current->flags` and returns the bits it added, which the restore call clears again. The pairing assumes strict LIFO nesting; restoring out of order corrupts the per-task state. Expose each policy as a variant of the `ScopeFlag` enum, with `ScopeFlags` as the corresponding set type, both generated by `impl_flags!`. Add `alloc_scope`, which applies a set of policies, runs a closure and restores the previous policies before returning. The save and restore operations are not exposed on their own, so safe code cannot leave a scope open or restore out of order. Signed-off-by: Andreas Hindborg --- Changes in v4: - Replace the `ScopeKind` trait and its tag types with an `alloc_scope()` function taking `ScopeFlags` built with `impl_flags!` (Gary). - Replace the per-policy C helpers with a single `memalloc_flags_save()` and `memalloc_flags_restore()` helper pair. - Do not expose a safe/restore functions public (Sashiko). - Mark `alloc_scope()` as `#[inline]` (Sashiko). - Link to v3: https://msgid.link/20261109-gfp-noio-v3-1-a573e7be9c44@kernel.org Changes in v3: - Rebase on v7.4-rc1. - Replace the pinned `Scope` guard and `memalloc_scope!` macro with a closure-based `ScopeKind::with` entry point (Alice, Gary). - Make the doctests compile and run under KUnit instead of `ignore`. - Document that `NoReclaim` has no effect in hard interrupt context. - Link to v2: https://msgid.link/20260605-gfp-noio-v2-1-cfa2e236c2a3@kernel.org Changes in v2: - Rewrite the patch to use scoped allocation flags instead of exposing a `GFP_NOIO` flag constant. - Link to v1: https://lore.kernel.org/r/20260128-gfp-noio-v1-1-9a808fc49b44@kernel.org --- rust/bindings/bindings_helper.h | 1 + rust/helpers/mm.c | 10 +++ rust/kernel/alloc.rs | 1 + rust/kernel/alloc/scoped.rs | 134 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 3791af0aadc7..df21b5b129a4 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -86,6 +86,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index b5540997bd20..42424583e7b3 100644 --- a/rust/helpers/mm.c +++ b/rust/helpers/mm.c @@ -48,3 +48,13 @@ __rust_helper void rust_helper_vma_end_read(struct vm_area_struct *vma) { vma_end_read(vma); } + +__rust_helper unsigned int rust_helper_memalloc_flags_save(unsigned int flags) +{ + return memalloc_flags_save(flags); +} + +__rust_helper void rust_helper_memalloc_flags_restore(unsigned int flags) +{ + memalloc_flags_restore(flags); +} diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs index 68d80f3fa6c8..5e4c142b5682 100644 --- a/rust/kernel/alloc.rs +++ b/rust/kernel/alloc.rs @@ -6,6 +6,7 @@ pub mod kbox; pub mod kvec; pub mod layout; +pub mod scoped; pub use self::kbox::Box; pub use self::kbox::KBox; diff --git a/rust/kernel/alloc/scoped.rs b/rust/kernel/alloc/scoped.rs new file mode 100644 index 000000000000..7fec1c974192 --- /dev/null +++ b/rust/kernel/alloc/scoped.rs @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Scoped allocation policies for the current task. +//! +//! The kernel exposes several per-task allocation policies through save/restore pairs in +//! [`include/linux/sched/mm.h`]: `memalloc_noio`, `memalloc_nofs`, `memalloc_noreclaim` and +//! `memalloc_pin`. Each pair is a wrapper around `memalloc_flags_save` and +//! `memalloc_flags_restore`. The save call sets bits in `current->flags` and returns the bits it +//! added, which the restore call clears again. The save/restore APIs assume strict LIFO nesting; +//! restoring out of order corrupts the per-task state. +//! +//! This module exposes each policy as a [`ScopeFlag`]. [`alloc_scope`] applies a set of policies, +//! runs a closure and restores the previous policies before returning. +//! +//! [`include/linux/sched/mm.h`]: srctree/include/linux/sched/mm.h +//! +//! # Examples +//! +//! ``` +//! use kernel::alloc::scoped::{ +//! alloc_scope, +//! ScopeFlag, // +//! }; +//! +//! // Every allocation inside the closure behaves as if `GFP_NOIO` were set, even though the call +//! // site passes `GFP_KERNEL`. +//! let b = alloc_scope(ScopeFlag::NoIo, || KBox::new(42_u32, GFP_KERNEL))?; +//! assert_eq!(*b, 42); +//! +//! // Policies can be combined with the bitwise OR (`|`) operator. +//! let b = alloc_scope(ScopeFlag::NoIo | ScopeFlag::Pin, || KBox::new(42_u32, GFP_KERNEL))?; +//! assert_eq!(*b, 42); +//! # Ok::<(), Error>(()) +//! ``` +// # Implementation details +// +// Since the closure used in this API is synchronous it cannot contain a suspension point, so the +// save and restore always execute in the same call frame, on the same task, and nested scopes +// restore in LIFO order. The save and restore operations are not exposed on their own, so safe +// code cannot begin a scope without ending it. +use crate::impl_flags; + +impl_flags!( + /// A set of per-task allocation policies. + /// + /// Build a set by combining [`ScopeFlag`] values with the bitwise operators, then pass it to + /// [`alloc_scope`] to run code under the policies. + /// + /// # Invariants + /// + /// Only bits that correspond to a [`ScopeFlag`] are set. + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub struct ScopeFlags(u32); + + /// A per-task allocation policy. + /// + /// Each policy corresponds to a `memalloc_*_save` / `memalloc_*_restore` pair in + /// [`include/linux/sched/mm.h`](srctree/include/linux/sched/mm.h). + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub enum ScopeFlag { + /// `GFP_NOIO` scope. + /// + /// Allocations on the current task behave as if `GFP_NOIO` were set, making them safe to + /// issue from the IO completion path. + /// + /// Corresponds to `memalloc_noio_save` / `memalloc_noio_restore`. + NoIo = bindings::PF_MEMALLOC_NOIO, + + /// `GFP_NOFS` scope. + /// + /// Allocations on the current task behave as if `GFP_NOFS` were set, making them safe to + /// issue from a filesystem critical section. + /// + /// Corresponds to `memalloc_nofs_save` / `memalloc_nofs_restore`. + NoFs = bindings::PF_MEMALLOC_NOFS, + + /// No-reclaim scope. + /// + /// Allocations on the current task may dip into the memory reserves. Callers must be sure + /// their allocations will help free more memory shortly; see the kernel C documentation + /// for the full contract. + /// + /// The scope has no effect in hard interrupt context, where `PF_MEMALLOC` does not grant + /// access to the reserves; see `__gfp_pfmemalloc_flags()` in + /// [`mm/page_alloc.c`](srctree/mm/page_alloc.c). + /// + /// Corresponds to `memalloc_noreclaim_save` / `memalloc_noreclaim_restore`. + NoReclaim = bindings::PF_MEMALLOC, + + /// Long-term pin scope. + /// + /// Allocations on the current task are restricted to zones that allow long-term pinning. + /// + /// Corresponds to `memalloc_pin_save` / `memalloc_pin_restore`. + Pin = bindings::PF_MEMALLOC_PIN, + } +); + +/// Run `f` with the allocation policies in `flags` applied to the current task. +/// +/// The policies in `flags` are applied before `f` runs. When `f` returns, the policies that were +/// not already in effect before the call are removed again. `flags` can be a single [`ScopeFlag`] +/// or a [`ScopeFlags`] set. +/// +/// # Examples +/// +/// ``` +/// use kernel::alloc::scoped::{ +/// alloc_scope, +/// ScopeFlag, // +/// }; +/// +/// let b = alloc_scope(ScopeFlag::NoFs, || KBox::new(42_u32, GFP_KERNEL))?; +/// assert_eq!(*b, 42); +/// # Ok::<(), Error>(()) +/// ``` +#[inline] +pub fn alloc_scope(flags: impl Into, f: impl FnOnce() -> R) -> R { + let flags: ScopeFlags = flags.into(); + + // SAFETY: By the type invariant of `ScopeFlags`, `flags` only contains `PF_MEMALLOC*` bits. + // Setting these on the current task is sound from any context. In hard interrupt context + // `PF_MEMALLOC` is ignored rather than misinterpreted. + let added_flags = unsafe { bindings::memalloc_flags_save(flags.into()) }; + + let ret = f(); + + // SAFETY: `added_flags` was produced by the `memalloc_flags_save` call above on this task. `f` + // is a synchronous closure, so it has already returned and any scope it began has already + // been restored. + unsafe { bindings::memalloc_flags_restore(added_flags) }; + + ret +} --- base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 change-id: 20260128-gfp-noio-fbd41e135088 Best regards, -- Andreas Hindborg