Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andreas Hindborg" <a.hindborg@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: linux-mm@kvack.org
Subject: Re: [PATCH v3] rust: alloc: add per-task memalloc scope abstractions
Date: Tue, 10 Nov 2026 10:38:00 +0000	[thread overview]
Message-ID: <sashiko-outbox-206880@kernel.org> (raw)
In-Reply-To: <20261109-gfp-noio-v3-1-a573e7be9c44@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] rust: alloc: safe ScopeKind::save() allows violating
  restore() safety
- [Low] rust: alloc: missing #[inline] on ScopeKind::with() and C
  forwarders
--

commit 41cec7ee242c1aa90cf2533b8574e3fdf2331dac
Author: Andreas Hindborg <a.hindborg@kernel.org>

rust: alloc: add per-task memalloc scope abstractions

This commit adds a ScopeKind trait and the tag types NoIo, NoFs, NoReclaim,
and MemallocPin to expose the per-task memalloc_*_save() and
memalloc_*_restore() helpers in Rust through a closure-based ScopeKind::with()
method.

> diff --git a/rust/kernel/alloc/scoped.rs b/rust/kernel/alloc/scoped.rs
> new file mode 100644
> index 0000000000000..6745c06d6725c
> --- /dev/null
> +++ b/rust/kernel/alloc/scoped.rs

[ ... ]

> +pub trait ScopeKind {
> +    /// Begin a scope on the current task and return the flags added to `current->flags`.
> +    ///
> +    /// Every call must be paired with a [`restore`] call on the same task, in LIFO order with
> +    /// respect to other scopes. Prefer [`with`], which arranges this automatically.
> +    ///
> +    /// [`restore`]: ScopeKind::restore
> +    /// [`with`]: ScopeKind::with
> +    fn save() -> c_uint;
> +
> +    /// End a scope on the current task.
> +    ///
> +    /// # Safety
> +    ///
> +    /// `prev` must be the value returned by the matching [`save`] call, the call must execute on
> +    /// the same task that ran [`save`], and no scope begun after the matching [`save`] may still be
> +    /// active.
> +    ///
> +    /// [`save`]: ScopeKind::save
> +    unsafe fn restore(prev: c_uint);
> +
> +    /// Run `f` with this policy applied.
> +    ///
> +    /// The current policy is saved before `f` runs and restored when `f` returns.
> +    fn with<R>(f: impl FnOnce() -> R) -> R {
> +        let prev = Self::save();
> +        let ret = f();
> +        // SAFETY: `prev` was produced by the `save` above on this task. `f` is a synchronous
> +        // closure, so it has already returned and any scope it began has already been restored.
> +        unsafe { Self::restore(prev) };
> +        ret
> +    }
> +}

[Severity: Medium]
Should ScopeKind::save() be marked unsafe or moved to a private sealed
supertrait?

Because ScopeKind::save() is a safe public method, safe code can call
NoIo::save(), NoFs::save(), NoReclaim::save(), or MemallocPin::save() without
a matching restore(), permanently leaving PF_MEMALLOC_NOIO, PF_MEMALLOC_NOFS,
PF_MEMALLOC, or PF_MEMALLOC_PIN set in current->flags. Safe code inside the
closure passed to ScopeKind::with() can also call save() and return without
restoring it, violating the safety precondition on ScopeKind::restore() that
no scope begun after the matching save() is still active.

[ ... ]

> +pub enum NoIo {}
> +
> +impl ScopeKind for NoIo {
> +    fn save() -> c_uint {
> +        // SAFETY: Updates a per-task flag and is documented as safe from any context.
> +        unsafe { bindings::memalloc_noio_save() }
> +    }
> +
> +    unsafe fn restore(prev: c_uint) {
> +        // SAFETY: Per the trait contract, `prev` is the value returned by the matching `save`, on
> +        // the same task.
> +        unsafe { bindings::memalloc_noio_restore(prev) };
> +    }
> +}

[Severity: Low]
This isn't a bug, but ScopeKind::with() and the save() and restore()
implementations for NoIo, NoFs, NoReclaim, and MemallocPin are small kernel
crate helpers and direct C binding forwarders that are missing #[inline]
attributes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261109-gfp-noio-v3-1-a573e7be9c44@kernel.org?part=1


      parent reply	other threads:[~2026-11-10 10:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 10:37 [PATCH v3] rust: alloc: add per-task memalloc scope abstractions Andreas Hindborg
2026-11-09 12:18 ` Gary Guo
2026-11-09 13:51   ` Andreas Hindborg
2026-11-10 10:38 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-206880@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@kernel.org \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox