All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Philipp Stanner" <phasta@kernel.org>
Cc: ojeda@kernel.org, media-ci@linuxtv.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v6 4/6] rust: xarray: Add support for index reservations
Date: Wed, 22 Jul 2026 14:59:31 +0000	[thread overview]
Message-ID: <20260722145932.69D6E1F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260722144931.1070312-6-phasta@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Calling `XArray::reserve()` or dropping `Reservation` while holding the XArray lock (`Guard`) causes a guaranteed A-A self-deadlock.
- [High] SAFETY and INVARIANT comments in `XArray::reserve` and `Reservation::drop` are factually wrong, reference nonexistent variables, and actively promote deadlocking code.
- [Medium] The `Reservation::drop` behavior allows multiple threads to accidentally destroy each other's reservations, leading to unexpected `-ENOMEM` errors during atomic stores.
--

--- Patch [4]: [PATCH v6 4/6] rust: xarray: Add support for index reservations ---
commit b249894b0dc68234b47a8d9e38c201513786806c
Author: Philipp Stanner <phasta@kernel.org>

rust: xarray: Add support for index reservations

This commit adds support for xa_reserve() to the Rust XArray abstractions
by introducing reserve() and a Reservation drop guard.

> diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
> index 987c9c0c21989..fa5d47935f0a7 100644
> --- a/rust/kernel/xarray.rs
> +++ b/rust/kernel/xarray.rs
[ ... ]
> @@ -147,6 +150,46 @@ pub fn lock(&self) -> Guard<'_, T> {
>              _not_send: NotThreadSafe,
>          }
>      }
> +
> +    /// Reserve memory in the XArray at the appropriate index. If this call
> +    /// succeeds, later storing at the index will not require an allocation.
> +    ///
> +    /// Loading from reserved entries will return `None`.
> +    pub fn reserve(&self, index: usize, gfp: alloc::Flags) -> Result<Reservation<'_, T>> {
> +        // SAFETY:
> +        // - `self.xa.xa` is always valid by the type invariant.
> +        // - The caller holds the lock.
> +        //
> +        // INVARIANT: `new` came from `T::into_foreign`.

[Severity: High]
The safety comment in reserve() explicitly states "The caller holds the
lock." However, both xa_reserve() and xa_release() internally acquire the
xa_lock spinlock.

If the caller follows this documentation and acquires the lock via
XArray::lock() before calling reserve(), won't this result in an A-A
self-deadlock when xa_reserve() attempts to acquire the same non-reentrant
spinlock?

Also, this isn't a bug, but the INVARIANT references a nonexistent variable
new. Is this a copy-paste error from another function?

> +        let ret = unsafe { bindings::xa_reserve(self.xa.get(), index, gfp.as_raw()) };
> +        match ret {
> +            0 => Ok(Reservation { xa: self, index }),

[Severity: Medium]
In C, xa_reserve() returns 0 both when it successfully reserves an empty
slot and when the slot is already reserved (XA_ZERO_ENTRY).

If two threads concurrently call reserve() on the same index, won't both
succeed and receive a Reservation guard?

When the first thread's guard drops and unconditionally calls xa_release(),
won't it overwrite XA_ZERO_ENTRY with NULL, silently destroying the second
thread's active reservation and potentially leading to an unexpected -ENOMEM
on a later atomic store?

> +            _ => Err(Error::from_errno(ret)),
> +        }
> +    }
> +}
> +
> +/// An index reservation object.
> +///
> +/// As long as you keep this object alive, your reservation will stay valid. If
> +/// someone has stored to this index in the meantime, dropping your reservation
> +/// does nothing, which is why you can always drop this reservation object once
> +/// you performed your store operation.
> +///
> +/// Refer to the main C xarray documentation for more details.
> +pub struct Reservation<'a, T: ForeignOwnable> {
> +    xa: &'a XArray<T>,
> +    index: usize,
> +}
> +
> +impl<T: ForeignOwnable> Drop for Reservation<'_, T> {
> +    fn drop(&mut self) {
> +        // SAFETY:
> +        // - `self.xa` is always valid by the type invariant.
> +        // - If `self.id` is not used or has been stored to by a racing party,

[Severity: High]
This isn't a bug, but the safety comment here in the drop() method
references self.id which does not exist on this struct. Should this be
self.index instead?

> +        //   `xa_release()` will do nothing.
> +        unsafe { bindings::xa_release(self.xa.xa.get(), self.index) };

[Severity: High]
Since xa_release() internally acquires the xa_lock spinlock, will this
cause a deadlock if the Reservation guard is dropped in Drop for Reservation
while the caller still holds the XArray::lock() guard?

> +    }
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260722144931.1070312-2-phasta@kernel.org?part=4

  reply	other threads:[~2026-07-22 14:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22 14:49 [PATCH v6 0/6] rust / dma_buf: Add abstractions for dma_fence Philipp Stanner
2026-07-22 14:49 ` [PATCH v6 1/6] rust: types: implement ForeignOwnable for ARef<T> Philipp Stanner
2026-07-22 14:49 ` [PATCH v6 2/6] rust: error: Add ECANCELED error code Philipp Stanner
2026-07-22 17:44   ` Alistair Francis
2026-07-22 18:38   ` Timur Tabi
2026-07-22 14:49 ` [PATCH v6 3/6] rust: sync: Add abstraction for rcu_barrier() Philipp Stanner
2026-07-22 14:49 ` [PATCH v6 4/6] rust: xarray: Add support for index reservations Philipp Stanner
2026-07-22 14:59   ` sashiko-bot [this message]
2026-07-22 14:49 ` [PATCH v6 5/6] rust: Add dma_fence abstractions Philipp Stanner
2026-07-22 15:03   ` sashiko-bot
2026-07-22 14:49 ` [PATCH v6 6/6] MAINTAINERS: Add entry for Rust dma-buf Philipp Stanner

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=20260722145932.69D6E1F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=media-ci@linuxtv.org \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=sashiko-reviews@lists.linux.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.