mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: <lorenzo.stoakes@oracle.com>, <vbabka@suse.cz>,
	<Liam.Howlett@oracle.com>, <urezki@gmail.com>, <ojeda@kernel.org>,
	<alex.gaynor@gmail.com>, <boqun.feng@gmail.com>,
	<gary@garyguo.net>, <bjorn3_gh@protonmail.com>,
	<lossin@kernel.org>, <a.hindborg@kernel.org>, <tmgross@umich.edu>,
	<maarten.lankhorst@linux.intel.com>, <mripard@kernel.org>,
	<tzimmermann@suse.de>, <airlied@gmail.com>, <simona@ffwll.ch>,
	<rust-for-linux@vger.kernel.org>,
	<dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/4] rust: drm: ensure kmalloc() compatible Layout
Date: Fri, 01 Aug 2025 11:29:23 +0200	[thread overview]
Message-ID: <DBQZHIS4VQBN.WSKBML2WYQE@kernel.org> (raw)
In-Reply-To: <aIyGdr8vKV4XE6Io@google.com>

On Fri Aug 1, 2025 at 11:18 AM CEST, Alice Ryhl wrote:
> On Thu, Jul 31, 2025 at 05:48:07PM +0200, Danilo Krummrich wrote:
>> drm::Device is allocated through __drm_dev_alloc() (which uses
>> kmalloc()) and the driver private data, <T as drm::Driver>::Data, is
>> initialized in-place.
>> 
>> Due to the order of fields in drm::Device
>> 
>>   pub struct Device<T: drm::Driver> {
>>      dev: Opaque<bindings::drm_device>,
>>      data: T::Data,
>>   }
>
> I'm not convinced this patch is right.
>
> Imagine this scenario: T::Data has size and alignment both equal to 16,
> and lets say that drm_device has a size that is a multiple of 8 but not
> 16 such as 72. In that case, you will allocate 72+16=88 bytes for
> Device, but actually the size of Device is 96 because there is 8 bytes
> of padding between dev and data.

Are you saying that there is an issue with

  (1) the existing implementation with uses mem::size_of::<Self>() or

  (2) the proper one that uses Kmalloc::aligned_layout(Layout::new::<Self>())?

I think neither has, because we're not allocating
size_of::<Opaque<bindings::drm_device>>() + size_of::<T::Data>() as you seem to
assume above, but size_of::<Device<T>>().

>> even with an arbitrary large alignment requirement of T::Data it can't
>> happen that the size of Device is smaller than its alignment requirement.
>> 
>> However, let's not rely on this subtle circumstance and create a proper
>> kmalloc() compatible Layout.
>> 
>> Fixes: 1e4b8896c0f3 ("rust: drm: add device abstraction")
>> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>> ---
>>  rust/kernel/drm/device.rs | 9 +++++++--
>>  1 file changed, 7 insertions(+), 2 deletions(-)
>> 
>> diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs
>> index 3bb7c83966cf..d19410deaf6c 100644
>> --- a/rust/kernel/drm/device.rs
>> +++ b/rust/kernel/drm/device.rs
>> @@ -5,6 +5,7 @@
>>  //! C header: [`include/linux/drm/drm_device.h`](srctree/include/linux/drm/drm_device.h)
>>  
>>  use crate::{
>> +    alloc::allocator::Kmalloc,
>>      bindings, device, drm,
>>      drm::driver::AllocImpl,
>>      error::from_err_ptr,
>> @@ -12,7 +13,7 @@
>>      prelude::*,
>>      types::{ARef, AlwaysRefCounted, Opaque},
>>  };
>> -use core::{mem, ops::Deref, ptr, ptr::NonNull};
>> +use core::{alloc::Layout, mem, ops::Deref, ptr, ptr::NonNull};
>>  
>>  #[cfg(CONFIG_DRM_LEGACY)]
>>  macro_rules! drm_legacy_fields {
>> @@ -96,6 +97,10 @@ impl<T: drm::Driver> Device<T> {
>>  
>>      /// Create a new `drm::Device` for a `drm::Driver`.
>>      pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) -> Result<ARef<Self>> {
>> +        // `__drm_dev_alloc` uses `kmalloc()` to allocate memory, hence ensure a `kmalloc()`
>> +        // compatible `Layout`.
>> +        let layout = Kmalloc::aligned_layout(Layout::new::<Self>());
>> +
>>          // SAFETY:
>>          // - `VTABLE`, as a `const` is pinned to the read-only section of the compilation,
>>          // - `dev` is valid by its type invarants,
>> @@ -103,7 +108,7 @@ pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) -> Result<A
>>              bindings::__drm_dev_alloc(
>>                  dev.as_raw(),
>>                  &Self::VTABLE,
>> -                mem::size_of::<Self>(),
>> +                layout.size(),
>>                  mem::offset_of!(Self, dev),
>>              )
>>          }
>> -- 
>> 2.50.0
>> 


  reply	other threads:[~2025-08-01  9:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-31 15:48 [PATCH 0/4] Alloc and drm::Device fixes Danilo Krummrich
2025-07-31 15:48 ` [PATCH 1/4] rust: alloc: replace aligned_size() with Kmalloc::aligned_layout() Danilo Krummrich
2025-08-01  9:14   ` Alice Ryhl
2025-08-12 19:52   ` Miguel Ojeda
2025-08-12 20:00     ` Danilo Krummrich
2025-08-12 20:12       ` Miguel Ojeda
2025-08-12 20:35         ` Danilo Krummrich
2025-08-16 20:40   ` Miguel Ojeda
2025-08-16 23:44     ` Danilo Krummrich
2025-07-31 15:48 ` [PATCH 2/4] rust: drm: ensure kmalloc() compatible Layout Danilo Krummrich
2025-08-01  9:18   ` Alice Ryhl
2025-08-01  9:29     ` Danilo Krummrich [this message]
2025-08-04 13:44       ` Alice Ryhl
2025-08-04 14:00   ` Alice Ryhl
2025-07-31 15:48 ` [PATCH 3/4] rust: drm: remove pin annotations from drm::Device Danilo Krummrich
2025-07-31 18:54   ` Benno Lossin
2025-08-01  8:21     ` Danilo Krummrich
2025-08-01  9:00       ` Benno Lossin
2025-08-01  9:52   ` Benno Lossin
2025-07-31 15:48 ` [PATCH 4/4] rust: drm: don't pass the address of drm::Device to drm_dev_put() Danilo Krummrich
2025-08-01  9:13   ` Alice Ryhl
2025-08-11 21:41 ` [PATCH 0/4] Alloc and drm::Device fixes Danilo Krummrich

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=DBQZHIS4VQBN.WSKBML2WYQE@kernel.org \
    --to=dakr@kernel.org \
    --cc=Liam.Howlett@oracle.com \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=lossin@kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=tzimmermann@suse.de \
    --cc=urezki@gmail.com \
    --cc=vbabka@suse.cz \
    /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

all inboxes | Powered by JetHome®