rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Gary Guo <gary@garyguo.net>
Cc: "Qingsong Chen" <changxian.cqs@antgroup.com>,
	linux-kernel@vger.kernel.org, 田洪亮 <tate.thl@antgroup.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Asahi Lina" <lina@asahilina.net>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2 1/3] rust: kernel: add ScatterList abstraction
Date: Wed, 7 Jun 2023 12:40:43 -0700	[thread overview]
Message-ID: <ZIDdO7SPvZh8fD7z@boqun-archlinux> (raw)
In-Reply-To: <20230607194210.380d195f.gary@garyguo.net>

On Wed, Jun 07, 2023 at 07:42:10PM +0100, Gary Guo wrote:
> On Mon, 5 Jun 2023 08:26:04 -0700
> Boqun Feng <boqun.feng@gmail.com> wrote:
> 
> > On Fri, Jun 02, 2023 at 06:18:17PM +0800, Qingsong Chen wrote:
> > [...]
> > > +impl<'a> ScatterList<'a> {
> > > +    /// Construct a new initializer.
> > > +    pub fn new(buf: &'a Pin<&mut [u8]>) -> impl PinInit<ScatterList<'a>> {
> > > +        // SAFETY: `slot` is valid while the closure is called, the memory
> > > +        // buffer is pinned and valid.
> > > +        unsafe {
> > > +            init::pin_init_from_closure(move |slot: *mut Self| {
> > > +                (*slot).set_buf(buf);
> > > +                (*slot).mark_end();  
> > 
> > Benno can provide more information, but you cannot dereference or create
> > a reference to `*slot`, since `slot` points to an uninitialized object
> > (see `try_pin_init` implementations), and referencing uninitialized
> > objects is UB (or may cause UB).
> 
> This is fine for `Self`, because the only non-ZST field in there is
> `Opaque`, which can be uninitialised.
> 

OK, but if you do a

	// this is OK
	let slot_ref = unsafe { &(*slot) };

	// reads uninitialised data.
	let o = slot_ref.offset();

here (before any `set_buf()` or `mark_end()`), isn't it an UB?

Regards,
Boqun

> > 
> > Note that you could do the following for `set_buf`:
> > 
> > 	// `addr_of!`[1] is special since it won't create references
> > 	// (even temporary onces).
> > 	let opaque = addr_of!((*slot).opaque); // <- *const Opaque<bindings::scatterlist>
> > 
> > 	let ptr = Opaque::raw_get(opaque); // <- *mut bindings::scatterlist
> > 
> > 	// Maybe this can be wrapped as a Rust function with a
> > 	// parameter: *mut bindings::scatterlist.
> > 	unsafe {
> > 		bindings::sg_set_buf(ptr, buf.as_ptr(), buf.len());
> > 	}
> > 
> > [1]: https://doc.rust-lang.org/core/ptr/macro.addr_of.html
> > 
> > Regards,
> > Boqun
> > 
> > > +                Ok(())
> > > +            })
> > > +        }
> > > +    }
> > > +  
> > [...]
> 

  reply	other threads:[~2023-06-07 19:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-02 10:18 [PATCH v2 0/3] Rust scatterlist abstractions Qingsong Chen
2023-06-02 10:18 ` [PATCH v2 1/3] rust: kernel: add ScatterList abstraction Qingsong Chen
2023-06-02 19:54   ` Martin Rodriguez Reboredo
2023-06-05  8:12     ` Qingsong Chen
2023-06-05 11:36     ` Miguel Ojeda
2023-06-05 15:26   ` Boqun Feng
2023-06-06  7:01     ` Qingsong Chen
2023-06-07 17:18       ` Boqun Feng
2023-06-07 18:42     ` Gary Guo
2023-06-07 19:40       ` Boqun Feng [this message]
2023-06-02 10:18 ` [PATCH v2 2/3] rust: kernel: implement iterators for ScatterList Qingsong Chen
2023-06-02 19:59   ` Martin Rodriguez Reboredo
2023-06-02 10:18 ` [PATCH v2 3/3] rust: kernel: add SgTable abstraction Qingsong Chen
2023-06-02 20:47   ` Martin Rodriguez Reboredo

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=ZIDdO7SPvZh8fD7z@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=changxian.cqs@antgroup.com \
    --cc=gary@garyguo.net \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tate.thl@antgroup.com \
    --cc=wedsonaf@gmail.com \
    --cc=yakoyoku@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).