Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andreas Hindborg" <a.hindborg@kernel.org>,
	"Tamir Duberstein" <tamird@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>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Harry Yoo" <harry@kernel.org>, "Hao Li" <hao.li@linux.dev>,
	"Christoph Lameter" <cl@gentwo.org>,
	"David Rientjes" <rientjes@google.com>,
	"Roman Gushchin" <roman.gushchin@linux.dev>
Cc: linux-mm@kvack.org
Subject: Re: [PATCH v6 08/12] rust: mm: add abstractions for allocating from a `sheaf`
Date: Sat, 21 Nov 2026 13:12:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-227048@kernel.org> (raw)
In-Reply-To: <20261120-xarray-entry-send-v6-8-1f69d7385f70@kernel.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] rust: mm: Sheaf::alloc() bypasses alloc_hooks() by calling
  _noprof directly
- [Low] rust: mm: missing #[inline] on small C-forwarding methods in
  sheaf.rs
- [Low] rust: mm: imports in sheaf.rs do not follow kernel vertical
  import style
- [Low] rust: mm: direct call to internal PinInit::__init() in
  Sheaf::alloc()
--

commit 5e97f4874e1bec1246089d6fb000afa8148da652
Author: Andreas Hindborg <a.hindborg@kernel.org>

rust: mm: add abstractions for allocating from a `sheaf`

This commit adds Rust abstractions (KMemCache, KMemCacheInit, Sheaf, and
SBox) for pre-allocating and allocating slab objects using SLUB sheaves.

> diff --git a/rust/kernel/mm/sheaf.rs b/rust/kernel/mm/sheaf.rs
> new file mode 100644
> index 0000000000000..1bc0c3dca900f
> --- /dev/null
> +++ b/rust/kernel/mm/sheaf.rs
> @@ -0,0 +1,483 @@

[ ... ]

> +use core::{
> +    convert::Infallible,
> +    marker::PhantomData,
> +    ops::{Deref, DerefMut},
> +    ptr::NonNull,
> +};
> +
> +use kernel::prelude::*;
> +
> +use crate::sync::{Arc, ArcBorrow};

[Severity: Low]
These imports group multiple items on one line instead of using the kernel
vertical import style from Documentation/rust/coding-guidelines.rst (one item
per line with a trailing // comment).

[ ... ]

> +impl<T: KMemCacheInit<T>> Drop for KMemCache<T> {
> +    fn drop(&mut self) {
> +        // SAFETY: `self.as_raw()` returns a valid cache pointer that was
> +        // created by `__kmem_cache_create_args`. As all objects allocated from
> +        // this hold a reference on `self`, they must have been dropped for this
> +        // `drop` method to execute.
> +        unsafe { bindings::kmem_cache_destroy(self.as_raw()) };
> +    }
> +}

[Severity: Low]
KMemCache::drop(), Sheaf::alloc(), Sheaf::drop(), and SBox::drop() are small
functions in the kernel crate that forward directly to C bindings, so they
should be marked #[inline].

[ ... ]

> +    pub fn alloc(&mut self) -> Option<SBox<T>> {
> +        // SAFETY: `self.cache.as_raw()` and `self.as_raw()` return valid
> +        // pointers. The function returns NULL when the sheaf is empty.
> +        let ptr = unsafe {
> +            bindings::kmem_cache_alloc_from_sheaf_noprof(self.cache.as_raw(), 0, self.as_raw())
> +        };

[Severity: Medium]
Does calling kmem_cache_alloc_from_sheaf_noprof() directly bypass
alloc_hooks() and leave current->alloc_tag unset when
CONFIG_MEM_ALLOC_PROFILING is enabled?

In C, kmem_cache_alloc_from_sheaf() is a macro wrapping
kmem_cache_alloc_from_sheaf_noprof() in alloc_hooks() to set
current->alloc_tag before slab_post_alloc_hook() runs
__alloc_tagging_slab_alloc_hook():

mm/slub.c:__alloc_tagging_slab_alloc_hook() {
	...
	if (likely(obj_exts)) {
		...
		alloc_tag_add(ref, current->alloc_tag, s->size);
		put_slab_obj_exts(obj_exts);
	} else {
		if (!is_kfence_address(object))
			alloc_tag_set_inaccurate(current->alloc_tag);
	}
}

With current->alloc_tag left NULL, CONFIG_MEM_ALLOC_PROFILING_DEBUG triggers
WARN_ONCE() in alloc_tag_add_check() on allocation and in
alloc_tag_sub_check() on free, and if prepare_slab_obj_exts_hook() fails to
allocate obj_exts under memory pressure, alloc_tag_set_inaccurate()
dereferences NULL current->alloc_tag. Wrapping the
kmem_cache_alloc_from_sheaf() macro in a __rust_helper in
rust/helpers/slab.c would preserve the profiling hooks.

> +
> +        let ptr = NonNull::new(ptr.cast::<T>())?;
> +
> +        // SAFETY:
> +        // - `ptr` is a valid, non-null pointer as it was just returned by the
> +        //   cache.
> +        // - The initializer is infallible, so an error is never returned.
> +        unsafe { T::init().__init(ptr.as_ptr()) }.expect("Initializer is infallible");

[Severity: Low]
PinInit::__init() is an internal trait method not meant to be called
directly. Following commit ea7da3116015 ("rust: treewide: replace
__pinned_init with raw_[try_]init"), could this use
pin_init::raw_init(ptr.as_ptr(), T::init()) instead, which also avoids the
expect()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-xarray-entry-send-v6-0-1f69d7385f70@kernel.org?part=8


  reply	other threads:[~2026-11-21 13:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 13:10 [PATCH v6 00/12] rust: xarray: add entry API with preloading Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 01/12] rust: xarray: minor formatting fixes Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 02/12] rust: xarray: add debug format for `StoreError` Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 03/12] xarray: move xas_result() and xa_zero_to_null() to the header Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 04/12] rust: xarray: add `XArrayState` Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 05/12] rust: xarray: simplify `Guard::load` Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 06/12] rust: xarray: add `find_next` and `find_next_mut` Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 07/12] rust: xarray: add entry API Andreas Hindborg
2026-11-21 13:12   ` sashiko-bot
2026-11-20 13:10 ` [PATCH v6 08/12] rust: mm: add abstractions for allocating from a `sheaf` Andreas Hindborg
2026-11-21 13:12   ` sashiko-bot [this message]
2026-11-20 13:10 ` [PATCH v6 09/12] rust: mm: sheaf: allow use of C initialized static caches Andreas Hindborg
2026-11-21 13:12   ` sashiko-bot
2026-11-20 13:10 ` [PATCH v6 10/12] xarray, radix-tree: enable sheaf support for kmem_cache Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 11/12] rust: xarray: add preload API Andreas Hindborg
2026-11-20 13:10 ` [PATCH v6 12/12] rust: xarray: document `Guard` lock drop semantics Andreas Hindborg

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-227048@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=cl@gentwo.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.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