[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <ZIDdO7SPvZh8fD7z@boqun-archlinux>
Date: Wed, 7 Jun 2023 12:40:43 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Gary Guo <gary@...yguo.net>
Cc: Qingsong Chen <changxian.cqs@...group.com>,
linux-kernel@...r.kernel.org,
田洪亮 <tate.thl@...group.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Martin Rodriguez Reboredo <yakoyoku@...il.com>,
Alice Ryhl <aliceryhl@...gle.com>,
Asahi Lina <lina@...hilina.net>, rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v2 1/3] rust: kernel: add ScatterList abstraction
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@...il.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(())
> > > + })
> > > + }
> > > + }
> > > +
> > [...]
>
Powered by blists - more mailing lists