linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: Alvin Sun <alvin.sun@linux.dev>
Cc: "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>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org
Subject: Re: [PATCH 4/4] rust: xarray: Add Guard::find() helper
Date: Fri, 17 Apr 2026 11:28:35 +0300	[thread overview]
Message-ID: <20260417082836.29530-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260417-tyr-ioctls-deps-v1-4-41c6e9bb061c@linux.dev>

On Fri, 17 Apr 2026 09:05:54 +0800
Alvin Sun <alvin.sun@linux.dev> wrote:

> Add a helper to find the first present entry in the XArray.
> 
> Returns the index of the first present entry, or None if the array
> is empty.
> 
> Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
> ---
>  rust/kernel/xarray.rs | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
> index 235fda0e394ba..e43129d032d9d 100644
> --- a/rust/kernel/xarray.rs
> +++ b/rust/kernel/xarray.rs
> @@ -217,6 +217,28 @@ pub fn remove(&mut self, index: usize) -> Option<T> {
>          unsafe { T::try_from_foreign(ptr) }
>      }
>  
> +    /// Finds the first present entry.
> +    ///
> +    /// Returns the index of the first present entry, or `None` if the array is empty.
> +    pub fn find(&mut self) -> Option<usize> {
> +        let mut index = 0usize;

Nit: I don't know if this verbosity can ever be useful, it can simply be `= 0`;

> +        // SAFETY: `self.xa.xa` is always valid by the type invariant, and we hold the lock.
> +        let ptr = unsafe {
> +            bindings::xa_find(
> +                self.xa.xa.get(),
> +                &mut index,
> +                usize::MAX,
> +                bindings::XA_PRESENT,
> +            )
> +        };
> +
> +        if ptr.is_null() {
> +            None
> +        } else {
> +            Some(index)
> +        }

This can be written with the `then_some` chain e.g., !ptr.is_null().then_some(..
.) but I don't know if it ever makes it more readable or simpler. I guess it's
up to preference.

-Onur

> +    }
> +
>      /// Stores an element at the given index.
>      ///
>      /// May drop the lock if needed to allocate memory, and then reacquire it afterwards.
> 
> -- 
> 2.43.0
> 


  parent reply	other threads:[~2026-04-17  8:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-17  1:05 [PATCH 0/4] rust: Add helper functions and constants for Tyr driver Alvin Sun
2026-04-17  1:05 ` [PATCH 1/4] rust: sizes: add SZ_4G constant Alvin Sun
2026-04-17  3:18   ` Alexandre Courbot
2026-04-17 11:56     ` Alvin Sun
2026-04-17  1:05 ` [PATCH 2/4] rust: mm: Add task_size() method to Mm Alvin Sun
2026-04-17  1:05 ` [PATCH 3/4] rust: xarray: Update StoreError comments for alloc() Alvin Sun
2026-04-17  1:05 ` [PATCH 4/4] rust: xarray: Add Guard::find() helper Alvin Sun
2026-04-17  1:18   ` Matthew Wilcox
2026-04-17  2:16     ` Alvin Sun
2026-04-17  8:28   ` Onur Özkan [this message]
2026-04-17  7:43 ` [PATCH 0/4] rust: Add helper functions and constants for Tyr driver Onur Özkan

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=20260417082836.29530-1-work@onurozkan.dev \
    --to=work@onurozkan.dev \
    --cc=Liam.Howlett@oracle.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=alvin.sun@linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    /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