rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: Wedson Almeida Filho <wedsonaf@gmail.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	 Danilo Krummrich <dakr@redhat.com>,
	lyude@redhat.com, robh@kernel.org, lina@asahilina.net,
	 mcanal@igalia.com, airlied@gmail.com,
	rust-for-linux@vger.kernel.org,  dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] drm: panthor: add dev_coredumpv support
Date: Tue, 23 Jul 2024 15:45:02 +0200	[thread overview]
Message-ID: <CAH5fLgitm2qrGn3BBFyspmTD7Km+pp2qbvA9GN4fjyUnuWffWg@mail.gmail.com> (raw)
In-Reply-To: <4A1B4B2C-7FAB-4656-90AE-B30DC636349E@collabora.com>

On Tue, Jul 23, 2024 at 3:41 PM Daniel Almeida
<daniel.almeida@collabora.com> wrote:
>
> Hi Alice, thanks for the review!
>
>
> >> +        fn alloc_mem(&mut self, size: usize) -> Option<*mut u8> {
> >> +            assert!(size % 8 == 0, "Allocation size must be 8-byte aligned");
> >> +            if isize::try_from(size).unwrap() == isize::MAX {
> >> +                return None;
> >> +            } else if self.pos + size > self.capacity {
> >> +                kernel::pr_debug!("DumpAllocator out of memory");
> >> +                None
> >> +            } else {
> >> +                let offset = self.pos;
> >> +                self.pos += size;
> >> +
> >> +                // Safety: we know that this is a valid allocation, so
> >> +                // dereferencing is safe. We don't ever return two pointers to
> >> +                // the same address, so we adhere to the aliasing rules. We make
> >> +                // sure that the memory is zero-initialized before being handed
> >> +                // out (this happens when the allocator is first created) and we
> >> +                // enforce a 8 byte alignment rule.
> >> +                Some(unsafe { self.mem.as_ptr().offset(offset as isize) as *mut u8 })
> >> +            }
> >> +        }
> >> +
> >> +        pub(crate) fn alloc<T>(&mut self) -> Option<&mut T> {
> >> +            let mem = self.alloc_mem(core::mem::size_of::<T>())? as *mut T;
> >> +            // Safety: we uphold safety guarantees in alloc_mem(), so this is
> >> +            // safe to dereference.
> >
> > This code doesn't properly handle when T requires a large alignment.
> >
>
> Can you expand a bit on this? IIRC the alignment of a structure/enum will be dictated
> by the field with the largest alignment requirement, right? Given that the largest primitive
> allowed in the kernel is u64/i64, shouldn’t this suffice, e.g.:

It's possible for Rust types to have a larger alignment using e.g.
#[repr(align(64))].

>  +            assert!(size % 8 == 0, "Allocation size must be 8-byte aligned");
>
>
> >> +            Some(unsafe { &mut *mem })
> >> +        }
> >> +
> >> +        pub(crate) fn alloc_bytes(&mut self, num_bytes: usize) -> Option<&mut [u8]> {
> >> +            let mem = self.alloc_mem(num_bytes)?;
> >> +
> >> +            // Safety: we uphold safety guarantees in alloc_mem(), so this is
> >> +            // safe to build a slice
> >> +            Some(unsafe { core::slice::from_raw_parts_mut(mem, num_bytes) })
> >> +        }
> >
> > Using references for functions that allocate is generally wrong.
> > References imply that you don't have ownership of the memory, but
> > allocator functions would normally return ownership of the allocation.
> > As-is, the code seems to leak these allocations.
>
> All the memory must be given to dev_coredumpv(), which will then take
> ownership.  dev_coredumpv() will free all the memory, so there should be no
> leaks here.
>
> I’ve switched to KVec in v2, so that will also cover the error paths,
> which do leak in this version, sadly.
>
> As-is, all the memory is pre-allocated as a single chunk. When space is carved
> for a given T, a &mut is returned so that the data can be written in-place at
> the right spot in said chunk.
>
> Not only there shouldn’t be any leaks, but I can actually decode this from
> userspace.
>
> I agree that this pattern isn’t usual, but I don’t see anything
> incorrect. Maybe I missed something?

Interesting. So the memory is deallocated when self is destroyed? A
bit unusual, but I agree it is correct if so. Sorry for the confusion
:)

Alice

      reply	other threads:[~2024-07-23 13:45 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-10 22:50 [RFC PATCH] drm: panthor: add dev_coredumpv support Daniel Almeida
2024-07-11  0:01 ` Danilo Krummrich
2024-07-15  9:03   ` Daniel Vetter
2024-07-15 17:05     ` Daniel Almeida
2024-07-16  9:25       ` Daniel Vetter
2024-07-25 19:35         ` Lyude Paul
2024-07-26 13:40           ` Daniel Vetter
2024-07-29 18:34             ` Lyude Paul
2024-07-30  8:29               ` Daniel Vetter
2024-07-11 16:57 ` Liviu Dudau
2024-07-11 18:40   ` Daniel Almeida
2024-07-12  9:46 ` Steven Price
2024-07-12 14:35   ` Daniel Almeida
2024-07-12 14:53     ` Danilo Krummrich
2024-07-12 15:13       ` Daniel Almeida
2024-07-12 15:32         ` Danilo Krummrich
2024-07-13  0:48           ` Dave Airlie
2024-07-13  1:00             ` Daniel Almeida
2024-07-13  8:17             ` Miguel Ojeda
2024-07-15  9:12     ` Steven Price
2024-07-23  9:44       ` Alice Ryhl
2024-07-23 16:06       ` Boris Brezillon
2024-07-23 17:23         ` Daniel Almeida
2024-07-24  8:59         ` Steven Price
2024-07-24 10:44           ` Boris Brezillon
2024-07-24 12:37             ` Steven Price
2024-07-24 13:15           ` Rob Herring
2024-07-24 13:54             ` Steven Price
2024-07-24 14:27               ` Daniel Almeida
2024-07-24 14:35                 ` Steven Price
2024-07-24 14:38               ` Miguel Ojeda
2024-07-25 11:42               ` Carsten Haitzler
2024-07-25 11:45         ` Carsten Haitzler
2024-07-23  9:53 ` Alice Ryhl
2024-07-23 13:41   ` Daniel Almeida
2024-07-23 13:45     ` Alice Ryhl [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=CAH5fLgitm2qrGn3BBFyspmTD7Km+pp2qbvA9GN4fjyUnuWffWg@mail.gmail.com \
    --to=aliceryhl@google.com \
    --cc=airlied@gmail.com \
    --cc=dakr@redhat.com \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyude@redhat.com \
    --cc=mcanal@igalia.com \
    --cc=ojeda@kernel.org \
    --cc=robh@kernel.org \
    --cc=rust-for-linux@vger.kernel.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).