From: "Danilo Krummrich" <dakr@kernel.org>
To: "Vitaly Wool" <vitaly.wool@konsulko.se>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Vlastimil Babka" <vbabka@suse.cz>,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Liam R . Howlett" <Liam.Howlett@oracle.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Bjorn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Yosry Ahmed" <yosry.ahmed@linux.dev>,
"Nhat Pham" <nphamcs@gmail.com>, <linux-mm@kvack.org>
Subject: Re: [PATCH v3] rust: zpool: add abstraction for zpool drivers
Date: Fri, 22 Aug 2025 16:13:43 +0200 [thread overview]
Message-ID: <DC90ONOZLKAZ.35P6HOCCAIPW1@kernel.org> (raw)
In-Reply-To: <20250822133935.674282-1-vitaly.wool@konsulko.se>
On Fri Aug 22, 2025 at 3:39 PM CEST, Vitaly Wool wrote:
> diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
> index b39c279236f5..0fec5337908c 100644
> --- a/rust/kernel/alloc.rs
> +++ b/rust/kernel/alloc.rs
> @@ -41,6 +41,11 @@
> pub struct Flags(u32);
>
> impl Flags {
> + /// Create from the raw representation
> + pub fn new(f: u32) -> Self {
> + Self(f)
> + }
I didn't notice this change before.
This function should be crate private, called from_raw() and document that the
given value must be a valid combination of GFP flags.
Please also make the addition of alloc::Flags::from_raw() a separate patch.
> +/// # Example
> +///
> +/// A zpool driver implementation which does nothing but prints pool name on its creation and
> +/// destruction, and panics if zswap tries to actually read from a pool's alleged object.
> +///
> +/// ```
> +/// use core::ptr::NonNull;
> +/// use kernel::alloc::{Flags, KBox, NumaNode};
> +/// use kernel::zpool::*;
> +///
> +/// struct MyZpool {
> +/// name: &'static CStr,
> +/// }
> +///
> +/// struct MyZpoolDriver;
> +///
> +/// impl ZpoolDriver for MyZpoolDriver {
> +/// type Pool = KBox<MyZpool>;
> +///
> +/// fn create(name: &'static CStr, gfp: Flags) -> Result<KBox<MyZpool>> {
> +/// let myPool = MyZpool { name };
> +/// let mut pool = KBox::new(myPool, gfp)?;
Why mutable?
> +///
> +/// pr_info!("Created pool {}\n", pool.name);
I think this print is unnecessary.
> +/// Ok(pool)
> +/// }
Please add empty lines between functions.
> +/// fn destroy(p: KBox<MyZpool>) {
> +/// let pool = KBox::into_inner(p);
Why the call to into_inner()? What do we get from moving the value?
> +/// pr_info!("Removed pool {}\n", pool.name);
Same as above, I think the print is unnecessary.
> +/// }
> +/// fn malloc(_pool: &mut MyZpool, _size: usize, _gfp: Flags, _nid: NumaNode) -> Result<usize> {
> +/// Ok(0) // TODO
> +/// }
> +/// unsafe fn free(_pool: &MyZpool, _handle: usize) {
> +/// // TODO
I'm not sure the TODO comments add any value.
> +/// }
> +/// unsafe fn read_begin(_pool: &MyZpool, _handle: usize) -> NonNull<u8> {
> +/// panic!("read_begin not implemented\n"); // TODO
Please don't use panic!() here, we only ever panic the kernel when there is no
other way to prevent undefined bahvior, i.e. as a last resort.
I know it's just an example and it's not even run, but it might trick people
into thinking that is is OK, given that it is an example.
> +/// }
> +/// unsafe fn read_end(_pool: &MyZpool, _handle: usize, _handle_mem: NonNull<u8>) {}
> +/// unsafe fn write(_p: &MyZpool, _h: usize, _handle_mem: NonNull<u8>, _mem_len: usize) {}
> +/// fn total_pages(_pool: &MyZpool) -> u64 { 0 }
> +/// }
Overall, I feel like the example doesn't add much value as it is. I think it
would be better to provide a real example that actually implements the
callbacks.
What about doing adding the most simple implementation using a plain VVec for
backing the pool? This way we can even run the example. :)
> +/// ```
> +pub trait ZpoolDriver {
> + /// Opaque Rust representation of `struct zpool`.
> + type Pool: ForeignOwnable;
> +
> + /// Create a pool.
> + fn create(name: &'static CStr, gfp: Flags) -> Result<Self::Pool>;
> +
> + /// Destroy the pool.
> + fn destroy(pool: Self::Pool);
> +
> + /// Allocate an object of size `size` using GFP flags `gfp` from the pool `pool`, with the
I think you can just write "Allocate an object of `size` bytes from `pool`",
which should sound a bit more organic.
> + /// preferred NUMA node `nid`. If the allocation is successful, an opaque handle is returned.
> + fn malloc(
> + pool: <Self::Pool as ForeignOwnable>::BorrowedMut<'_>,
> + size: usize,
> + gfp: Flags,
> + nid: NumaNode,
> + ) -> Result<usize>;
> +
> + /// Free a previously allocated from the `pool` object, represented by `handle`.
> + ///
> + /// # Safety
> + ///
> + /// - `handle` should be a valid handle previously returned by `malloc`
"must be"; please also add a period at the end of a sentences (applies to the
whole patch).
> + unsafe fn free(pool: <Self::Pool as ForeignOwnable>::Borrowed<'_>, handle: usize);
> +
> + /// Make all the necessary preparations for the caller to be able to read from the object
> + /// represented by `handle` and return a valid pointer to the `handle` memory to be read.
> + ///
> + /// # Safety
> + ///
> + /// - `handle` should be a valid handle previously returned by `malloc`
A handle that has been given to free() already cannot be used here anymore.
However, as by your safety documentation this would be valid.
More generally, for the safety documentation I recommend having a look at the
documentation of the Allocator trait [1].
[1] https://rust.docs.kernel.org/src/kernel/alloc.rs.html#139-216
prev parent reply other threads:[~2025-08-22 14:13 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-22 13:39 Vitaly Wool
2025-08-22 14:13 ` Danilo Krummrich [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=DC90ONOZLKAZ.35P6HOCCAIPW1@kernel.org \
--to=dakr@kernel.org \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=nphamcs@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@suse.cz \
--cc=vitaly.wool@konsulko.se \
--cc=yosry.ahmed@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox