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 07/12] rust: xarray: add entry API
Date: Sat, 21 Nov 2026 13:12:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-227047@kernel.org> (raw)
In-Reply-To: <20261120-xarray-entry-send-v6-7-1f69d7385f70@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] rust: xarray: OccupiedEntry deref() and deref_mut() use Guard
  borrow lifetime
- [Low] rust: xarray: Guard::entry() discards walked XArrayState from
  load()
- [Low] rust: xarray: inaccurate doc comments and missing
  Guard::insert_entry()
--

commit 6b0e265e85674545670123d4c157941bef632ae4
Author: Andreas Hindborg <a.hindborg@kernel.org>

rust: xarray: add entry API

This commit adds an Entry API (Entry, VacantEntry, and OccupiedEntry) to
the Rust XArray abstraction using the xas_* state operations.

> diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
> index 07ec08223fbd2..3ff94c05a15a1 100644
> --- a/rust/kernel/xarray.rs
> +++ b/rust/kernel/xarray.rs
[ ... ]
> @@ -251,6 +257,35 @@ pub fn get_mut(&mut self, index: usize) -> Option<T::BorrowedMut<'_>> {
>          Some(unsafe { T::borrow_mut(ptr.as_ptr()) })
>      }
>  
> +    /// Gets an entry for the specified index, which can be vacant or occupied.
[ ... ]
> +    pub fn entry<'b>(&'b mut self, index: usize) -> Entry<'a, 'b, T> {
> +        match self.load(index) {
> +            None => Entry::Vacant(VacantEntry::new(self, index)),
> +            Some(ptr) => Entry::Occupied(OccupiedEntry::new(self, index, ptr)),
> +        }
> +    }

[Severity: Low]
Does Guard::entry() discard the walked XArrayState from self.load(index)
and force a second tree walk on subsequent entry operations?

self.load(index) constructs a temporary XArrayState, walks the tree via
xas_load(), and drops that state on return.  VacantEntry::new() and
OccupiedEntry::new() then construct a fresh XArrayState with xa_node reset
to XAS_RESTART, so a later insert(), replace(), or remove() walks the tree
from the root again.  Constructing a single XArrayState in Guard::entry(),
calling state.load(), and passing that state into VacantEntry or
OccupiedEntry would preserve the positioned cursor.

> diff --git a/rust/kernel/xarray/entry.rs b/rust/kernel/xarray/entry.rs
> new file mode 100644
> index 0000000000000..e2f01a49c55e0
> --- /dev/null
> +++ b/rust/kernel/xarray/entry.rs
[ ... ]
> +    /// Inserts a value into this vacant entry.
> +    ///
> +    /// Returns a reference to the newly inserted value.
> +    ///
> +    /// - This method will fail if the nodes on the path to the index
> +    ///   represented by this entry are not present in the XArray.
> +    /// - This method will not drop the XArray lock.
[ ... ]
> +    /// Swaps the value in this entry with the provided value.
> +    ///
> +    /// Returns the old value that was in the entry.
[ ... ]
> +    pub fn swap<U>(&mut self, other: &mut U)
> +    where
> +        T: ForeignOwnable<Borrowed<'b> = &'b U, BorrowedMut<'b> = &'b mut U> + 'b,
> +        U: 'b,
> +    {
> +        use core::ops::DerefMut;
> +        core::mem::swap(self.deref_mut(), other);
> +    }
> +}

[Severity: Low]
This isn't a bug, but a few documentation comments do not match the
implementation:

VacantEntry::insert() and VacantEntry::insert_entry() say they fail if the
nodes on the path to the index are not present, whereas xas_store()
allocates missing nodes with GFP_NOWAIT and only fails if that allocation
fails.

OccupiedEntry::swap() says it returns the old value, but it swaps in place
with other and returns ().

Also, the commit message mentions Guard::insert_entry, which is not added
until commit "rust: xarray: add preload API" later in this series.

> +impl<'a, 'b, T, U> core::ops::Deref for OccupiedEntry<'a, 'b, T>
> +where
> +    T: ForeignOwnable<Borrowed<'b> = &'b U, BorrowedMut<'b> = &'b mut U> + 'b,
> +    U: 'b,
> +{
> +    type Target = U;
> +
> +    #[inline]
> +    fn deref(&self) -> &Self::Target {
> +        // SAFETY: `ptr` came from `T::into_foreign`.
> +        unsafe { T::borrow(self.ptr.as_ptr()) }
> +    }
> +}
> +
> +impl<'a, 'b, T, U> core::ops::DerefMut for OccupiedEntry<'a, 'b, T>
> +where
> +    T: ForeignOwnable<Borrowed<'b> = &'b U, BorrowedMut<'b> = &'b mut U> + 'b,
> +    U: 'b,
> +{
> +    #[inline]
> +    fn deref_mut(&mut self) -> &mut Self::Target {
> +        // SAFETY: `ptr` came from `T::into_foreign`.
> +        unsafe { T::borrow_mut(self.ptr.as_ptr()) }
> +    }
> +}

[Severity: Medium]
Do OccupiedEntry::deref() and OccupiedEntry::deref_mut() violate the
ForeignOwnable::borrow() and ForeignOwnable::borrow_mut() safety contracts
by instantiating them with the Guard borrow lifetime 'b instead of the
lifetime of &self or &mut self?

Because the where-clauses only constrain T::Borrowed<'b> = &'b U and
T::BorrowedMut<'b> = &'b mut U for the fixed outer lifetime 'b, calling
T::borrow() or T::borrow_mut() here produces a borrow for the entire Guard
borrow lifetime 'b before reborrowing it for &self or &mut self.
ForeignOwnable::borrow_mut<'a>() requires that 'a not overlap with the
lifetime of any other call to borrow() or borrow_mut() on the same object,
and both methods require that any call to from_foreign() happen after 'a
ends.  Dereferencing an OccupiedEntry (for example in OccupiedEntry::swap())
and then dereferencing it again or calling OccupiedEntry::insert() or
OccupiedEntry::remove() creates overlapping 'b borrows or runs
T::from_foreign() while 'b is still active.

In addition, requiring BorrowedMut<'b> = &'b mut U on the Deref
implementation prevents shared dereferencing for ForeignOwnable types such
as Arc or ARef that do not yield &mut U from BorrowedMut.

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


  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 [this message]
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
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-227047@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