rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
To: Daniel Almeida <daniel.almeida@collabora.com>,
	wedsonaf@gmail.com, ojeda@kernel.org
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	virtualization@lists.linux-foundation.org
Subject: Re: [PATCH v2 1/2] rust: add scatterlist support
Date: Thu, 6 Apr 2023 11:21:06 -0300	[thread overview]
Message-ID: <365497cf-6a04-de2d-f694-d63424a5b779@gmail.com> (raw)
In-Reply-To: <20230405201416.395840-2-daniel.almeida@collabora.com>

On 4/5/23 17:14, Daniel Almeida wrote:
> This patch adds a scatterlist abstraction. It is then used and tested
> by the new rust virtio sample module.
> 
> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
> ---
>  rust/kernel/lib.rs         |  1 +
>  rust/kernel/scatterlist.rs | 40 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 41 insertions(+)
>  create mode 100644 rust/kernel/scatterlist.rs
> 
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index c20b37e88ab2..b23be69919cc 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -71,6 +71,7 @@ pub mod net;
>  pub mod pages;
>  pub mod power;
>  pub mod revocable;
> +pub mod scatterlist;
>  pub mod security;
>  pub mod task;
>  pub mod workqueue;
> diff --git a/rust/kernel/scatterlist.rs b/rust/kernel/scatterlist.rs
> new file mode 100644
> index 000000000000..fe036af13995
> --- /dev/null
> +++ b/rust/kernel/scatterlist.rs
> @@ -0,0 +1,40 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Scatterlist abstractions.
> +//!
> +//! C header: [`include/linux/virtio.h`](../../../../include/linux/scatterlist.h)

Little typo, I think that you've meant this to be scatterlist.h instead
of virtio.h.

> +
> +use core::cell::UnsafeCell;
> +
> +/// Scatterlist abstraction over the C side `struct scatterlist`.
> +pub struct Scatterlist {
> +    /// The C side `struct scatterlist`.
> +    inner: UnsafeCell<bindings::scatterlist>,
> +}
> +

Wouldn't be better to use a transparent tuple struct [1] since this only
has one non-zero sized field and could be easily transmuted into its
inner type.

> +impl Scatterlist {
> +    /// A wrapper over the C-side `sg_init_one()`. Initialize a single entry sg
> +    /// list.
> +    ///
> +    /// # Safety
> +    ///
> +    /// Caller must ensure that `buf` is valid and `buflen` really
> +    /// represents the size of `buf`.
> +    pub unsafe fn init_one(buf: *const core::ffi::c_void, buflen: u32) -> Self {
> +        // SAFETY: There are no references or function pointers in this
> +        // `Scatterlist`.
> +        let mut sg: Scatterlist = unsafe { core::mem::zeroed() };
> +        // SAFETY: we pass a valid sg entry, which points to stack-allocated
> +        // variable above. `buf` and `buflen` are presumed valid as per this
> +        // function's safety requirements.
> +        unsafe {
> +            bindings::sg_init_one(sg.inner.get_mut(), buf, buflen);
> +        }
> +
> +        sg
> +    }

If this function was meant to be used for internal use only then it
should be `pub(crate)`, or else, a function that takes an slice should
be provided as a safe alternative.

> +
> +    pub(crate) fn inner(&self) -> &UnsafeCell<bindings::scatterlist> {
> +        &self.inner
> +    }
> +}

Link: https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent [1]
Regards
-> Martin

  reply	other threads:[~2023-04-06 14:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-05 20:14 [PATCH v2 0/2] rust: virtio: add virtio support Daniel Almeida
2023-04-05 20:14 ` [PATCH v2 1/2] rust: add scatterlist support Daniel Almeida
2023-04-06 14:21   ` Martin Rodriguez Reboredo [this message]
2023-04-05 20:14 ` [PATCH v2 2/2] rust: virtio: add virtio support Daniel Almeida
2023-04-06 14:22   ` Martin Rodriguez Reboredo
2023-04-06 18:26     ` Wedson Almeida Filho
2023-04-06 19:04   ` Wedson Almeida Filho
2023-04-07 23:36     ` Daniel Almeida
2023-04-05 23:19 ` [PATCH v2 0/2] " Miguel Ojeda

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=365497cf-6a04-de2d-f694-d63424a5b779@gmail.com \
    --to=yakoyoku@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=wedsonaf@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).