From nobody Sun Oct 5 16:14:53 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F2E21AA7A6; Thu, 31 Jul 2025 15:49:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976977; cv=none; b=CZVTL0jkr1b2GXhZI9kL4yzK5BAEl6bd85ZheiK3mwcKVYAmvVSEARozIHV/lz2ZNsngy9ekhxJoplDvan0lXlmqfNqE/o7dEpe2n4zufjJE8xuu+Wf63O64os7OZyoebREw0NZ0p/KRVMw8nP19PO2tznVW/j+ypvZdioeu4QU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976977; c=relaxed/simple; bh=Ocb1NOgQl74B18can9Y+UZgv0q32Y4dyFqG+Vs8rtHM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q863OrdWKjQeSFpuVqCl3pDkKis/rL6Csf/SBtEEL9IoZhaTuHPgpNXbEO6522R1qRAUtOIO8zd39XlyA2F/vbigSSsuQPN34NyxfhRTzGivTWTakXtzHovXZDCtMs7j8O7Aqpk3hPkdXbWKWUZLlwm4mPoGIE2UufAneAC8nhU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lSW3jS9p; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lSW3jS9p" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 538FDC4CEFB; Thu, 31 Jul 2025 15:49:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753976976; bh=Ocb1NOgQl74B18can9Y+UZgv0q32Y4dyFqG+Vs8rtHM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lSW3jS9pAFF4r+5a4P3adZuyRu69mYynP+fGwHmQINZf6iHMUIV1er69DyJR+z6Xt IxSMgZC1paJiWDzLaVVEQXtuz/qiGXKd6dmASOWX1G/8OpTVmU2OopKUvMv3+08CA0 8bNqLr6t+HFS9c2VoyAtcwgR8zDE4vF+upWg7myYTFxOwgV7OleMphRm5MoBCqENx0 /nRw8PWhwaGfXmppVBg6tt9MZ9uFBAlKUaoNcR1k5k/opaOWpCFX06HlIZu4/eIxD0 phTbYqFklM84ioz4DQbhuL5+DqoqMhmlyet+L+Qc/gjF+RjF7ef8/OOAEXh1SDgBQp +bPEBOcNy900Q== From: Danilo Krummrich To: 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, aliceryhl@google.com, tmgross@umich.edu, maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@gmail.com, simona@ffwll.ch Cc: rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 1/4] rust: alloc: replace aligned_size() with Kmalloc::aligned_layout() Date: Thu, 31 Jul 2025 17:48:06 +0200 Message-ID: <20250731154919.4132-2-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250731154919.4132-1-dakr@kernel.org> References: <20250731154919.4132-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" aligned_size() dates back to when Rust did support kmalloc() only, but is now used in ReallocFunc::call() and hence for all allocators. However, the additional padding applied by aligned_size() is only required by the kmalloc() allocator backend. Hence, replace aligned_size() with Kmalloc::aligned_layout() and use it for the affected allocators, i.e. kmalloc() and kvmalloc(), only. While at it, make Kmalloc::aligned_layout() public, such that Rust abstractions, which have to call subsystem specific kmalloc() based allocation primitives directly, can make use of it. Fixes: 8a799831fc63 ("rust: alloc: implement `ReallocFunc`") Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/alloc/allocator.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index aa2dfa9dca4c..3331ef338f3b 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -43,17 +43,6 @@ /// For more details see [self]. pub struct KVmalloc; =20 -/// Returns a proper size to alloc a new object aligned to `new_layout`'s = alignment. -fn aligned_size(new_layout: Layout) -> usize { - // Customized layouts from `Layout::from_size_align()` can have size <= align, so pad first. - let layout =3D new_layout.pad_to_align(); - - // Note that `layout.size()` (after padding) is guaranteed to be a mul= tiple of `layout.align()` - // which together with the slab guarantees means the `krealloc` will r= eturn a properly aligned - // object (see comments in `kmalloc()` for more information). - layout.size() -} - /// # Invariants /// /// One of the following: `krealloc`, `vrealloc`, `kvrealloc`. @@ -88,7 +77,7 @@ unsafe fn call( old_layout: Layout, flags: Flags, ) -> Result, AllocError> { - let size =3D aligned_size(layout); + let size =3D layout.size(); let ptr =3D match ptr { Some(ptr) =3D> { if old_layout.size() =3D=3D 0 { @@ -123,6 +112,17 @@ unsafe fn call( } } =20 +impl Kmalloc { + /// Returns a [`Layout`] that makes [`Kmalloc`] fulfill the requested = size and alignment of + /// `layout`. + pub const fn aligned_layout(layout: Layout) -> Layout { + // Note that `layout.size()` (after padding) is guaranteed to be a= multiple of + // `layout.align()` which together with the slab guarantees means = that `Kmalloc` will return + // a properly aligned object (see comments in `kmalloc()` for more= information). + layout.pad_to_align() + } +} + // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees th= at // - memory remains valid until it is explicitly freed, // - passing a pointer to a valid memory allocation is OK, @@ -135,6 +135,8 @@ unsafe fn realloc( old_layout: Layout, flags: Flags, ) -> Result, AllocError> { + let layout =3D Kmalloc::aligned_layout(layout); + // SAFETY: `ReallocFunc::call` has the same safety requirements as= `Allocator::realloc`. unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags= ) } } @@ -176,6 +178,10 @@ unsafe fn realloc( old_layout: Layout, flags: Flags, ) -> Result, AllocError> { + // `KVmalloc` may use the `Kmalloc` backend, hence we have to enfo= rce a `Kmalloc` + // compatible layout. + let layout =3D Kmalloc::aligned_layout(layout); + // TODO: Support alignments larger than PAGE_SIZE. if layout.align() > bindings::PAGE_SIZE { pr_warn!("KVmalloc does not support alignments larger than PAG= E_SIZE yet.\n"); --=20 2.50.0 From nobody Sun Oct 5 16:14:53 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F25DF1DF991; Thu, 31 Jul 2025 15:49:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976982; cv=none; b=Uh8oF099++eqxTp3hMHmR8ivgvv24P9YL3H1PATngqodJa2HVp0bC+YprcsFmyt35vMBx5Uwbi1++/tHmtIUDqs+9hEUgRHAMrgd6h72DQE4TH5gNoFwoZ2YBIbVC2obFhfCXuXXetNKGxNgAevdBMfFj/QHe3irGtfO6hKfHWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976982; c=relaxed/simple; bh=r97UwXG1579xAWFCkWZJzKyFqYZzuvi9ibi+ySY8Gs0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hhFFpOSJQp5XNuuHP0ogkn9+zWAenPEPpuv/dVVCsTiZtkMFdrBWwS3CZ2ygiEoEKXJlg+iMz4oq4f3M9SP8V1E9RX5YzW1s2AJtfXNOJOMM3iokti3nVfCum5LZCm9mGuBArHDb00TRuN0kgAC8wp9QgZHp6jGDB34jPI0ls8Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EqSTVuN2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EqSTVuN2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26D84C4CEF7; Thu, 31 Jul 2025 15:49:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753976981; bh=r97UwXG1579xAWFCkWZJzKyFqYZzuvi9ibi+ySY8Gs0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EqSTVuN2zxfwZ1frunNoQ63lBeWmPwz6UKKe2gisXeuR5NNASuWfzv4HNoKu/iLs4 7tpvVxDzKyToaxJ28prMBA+9RiELT7DrqKzJCis7ex3zEEkbNVL/APlEHN3EBB+imV lSMvZlQNwLxCdRHR/Qfhd5ffsDWXC1CaIsoY+U+QYmDc+84OXEXnE+UtmXS75Lgm6x MXJ3RjHkqHqeykxrBTo4tfHoC9UNZYNrwbGPuWZrXsvwJL7MWaBdbcmJFewG8c5qAu lLh3fs7zqYeZVM6cnUG6//29/+25RQpg7ZE3tv3nXB3tl0c7yxKJy838Tam4MSEN6s MSpY6Vlh1B0/g== From: Danilo Krummrich To: 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, aliceryhl@google.com, tmgross@umich.edu, maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@gmail.com, simona@ffwll.ch Cc: rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 2/4] rust: drm: ensure kmalloc() compatible Layout Date: Thu, 31 Jul 2025 17:48:07 +0200 Message-ID: <20250731154919.4132-3-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250731154919.4132-1-dakr@kernel.org> References: <20250731154919.4132-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" drm::Device is allocated through __drm_dev_alloc() (which uses kmalloc()) and the driver private data, ::Data, is initialized in-place. Due to the order of fields in drm::Device pub struct Device { dev: Opaque, data: T::Data, } 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 Reviewed-by: Alice Ryhl --- 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) =20 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}; =20 #[cfg(CONFIG_DRM_LEGACY)] macro_rules! drm_legacy_fields { @@ -96,6 +97,10 @@ impl Device { =20 /// Create a new `drm::Device` for a `drm::Driver`. pub fn new(dev: &device::Device, data: impl PinInit) -= > Result> { + // `__drm_dev_alloc` uses `kmalloc()` to allocate memory, hence en= sure a `kmalloc()` + // compatible `Layout`. + let layout =3D Kmalloc::aligned_layout(Layout::new::()); + // 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) -> Result(), + layout.size(), mem::offset_of!(Self, dev), ) } --=20 2.50.0 From nobody Sun Oct 5 16:14:53 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C68911E835C; Thu, 31 Jul 2025 15:49:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976986; cv=none; b=YZu5Lp+6Wu20jEimath/NvP+O8EetsyDOTrYqyKiafyBwEClSTt5mqnKOMYvZvAKZiDb/VhOoSTU5IfLmgjPC1ly5BcLiwCsLMlxRl480TwNUpKqx8CbGYK7i9KyyaNDJ5Z2wiJQqJkWEctUCQhYP92WpqdxHBswQU5F2eLPTas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976986; c=relaxed/simple; bh=2foXzLUbcLlOFhsxqoB4OFU/K2Gw1RJLAqLvJ8zuC4w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q1t0Z4tuUiTFiW25ydwzMM4dX/bW5tIPVaGgQfj2aWaK8yj7GTkC22ja0Rva3/WXwFQfhFlw7c6M0SH5M5t0kE4vVXQ+PqnyQIwobFBlQXMhcr5iNp/yLHbDAeIzTUXbV6/XJfIHcdzlAAJmIXr8TDR2JuJSmaka+ZDcE8JE5nA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rYNIg5RZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rYNIg5RZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE135C4CEEF; Thu, 31 Jul 2025 15:49:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753976986; bh=2foXzLUbcLlOFhsxqoB4OFU/K2Gw1RJLAqLvJ8zuC4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rYNIg5RZY61hQko6uvctqhWsnSpHp/10vuTOBp1HVYI9OR9oy826wUPat+pdZ0jSa 7u6UYmFaVk8HpPIaOzSAwG/2XbWOajAg/a2ta3QBg6arjea28wY9RwSB0O6G8dpBmy 0a01GUaKlbeqpmf5xnyuem64iykjbTd1ttOfz90+4DUDZkjBIiMqzLji3NkP9gTmNn NqYb6san8e5ve5bsMAGZ1MUvhHvY0IbinNvsgvK39m+kXxYGjU68T3bVnUPLTybCDA PSHJwtwMQfk7HJlGRiwJaMR55n50lG28R+FRz5JHLp8HtniDl6w2085N/KuDEQsbuY 1c6GJsHbNd/hA== From: Danilo Krummrich To: 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, aliceryhl@google.com, tmgross@umich.edu, maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@gmail.com, simona@ffwll.ch Cc: rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 3/4] rust: drm: remove pin annotations from drm::Device Date: Thu, 31 Jul 2025 17:48:08 +0200 Message-ID: <20250731154919.4132-4-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250731154919.4132-1-dakr@kernel.org> References: <20250731154919.4132-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The #[pin_data] and #[pin] annotations are not necessary for drm::Device, since we don't use any pin-init macros, but only __pinned_init() on the impl PinInit argument of drm::Device::new(). Fixes: 1e4b8896c0f3 ("rust: drm: add device abstraction") Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/drm/device.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index d19410deaf6c..d0a9528121f1 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -54,10 +54,8 @@ macro_rules! drm_legacy_fields { /// /// `self.dev` is a valid instance of a `struct device`. #[repr(C)] -#[pin_data] pub struct Device { dev: Opaque, - #[pin] data: T::Data, } =20 --=20 2.50.0 From nobody Sun Oct 5 16:14:53 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8ECFF1F30B2; Thu, 31 Jul 2025 15:49:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976991; cv=none; b=M+tmv5WznujijSz0Cehmycy/olggA0A9Z/Jxzu7tG9B7u2SUByyx1S6AJUDIBBTYzbwUAoZ5zvi7Jkb5xFkKeyW1YNJoqI5oC0JA/r1AAE0jmfFo4/eV0z2KHhgP3JlaUB24k7CbA393aNNO9G3OrLRW4Z/6cY7rYFrfxSbm5h8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753976991; c=relaxed/simple; bh=TRPn+DgynwTBU4suc4xP+QjfUEO+i9ltjPYhmhrkr2A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fqhp6QRtSSNiPrKJPgumZJv+gOnp1Vy3tTqWpIoDbRxPo6ZD+HjnxvQ0T8eLghakBGnW4N7gcaxTvglnSNUMW7qXDQkrWyT5VidnduIbAe0hoIkDzR5GkWEAmhpsz4XDXLIIdOucUucflFZhneGlXNat9kqKTnej9EQ5DYP8SUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N/X/JNrt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="N/X/JNrt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C20FCC4CEF7; Thu, 31 Jul 2025 15:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753976991; bh=TRPn+DgynwTBU4suc4xP+QjfUEO+i9ltjPYhmhrkr2A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N/X/JNrtyFakbTS/O9iOKZ67QuJWdk2GrjRdWBqwrDqXwaiu2XP315wrn0r0Z7bBY UUPrHbbI2WuDp6O0ej47Bxii1HSJW7FKIYKQTuB7tSmfB/SIwnRpAUb3zVam9ibvQ4 mQR02O6FWGqKnPce2tJPaxVy14al1kNBVFaP2f4O6gH8Sheg2zlVb8/p4j64G9Byqx BocWeAFMemBYxQ99Ju4Sq5UTSJg8deTAkU7kTr3tlEpKrte3IzPFD1XXcDI955jsTm 1l0+ifKC7nsak++M9SE0fkaRB7svvRG8+GXEchuaHrs7ASH7lhektT3SUjeftJQx6G Q9IUshOilxNaA== From: Danilo Krummrich To: 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, aliceryhl@google.com, tmgross@umich.edu, maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@gmail.com, simona@ffwll.ch Cc: rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 4/4] rust: drm: don't pass the address of drm::Device to drm_dev_put() Date: Thu, 31 Jul 2025 17:48:09 +0200 Message-ID: <20250731154919.4132-5-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250731154919.4132-1-dakr@kernel.org> References: <20250731154919.4132-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In drm_dev_put() call in AlwaysRefCounted::dec_ref() we rely on struct drm_device to be the first field in drm::Device, whereas everywhere else we correctly obtain the address of the actual struct drm_device. Analogous to the from_drm_device() helper, provide the into_drm_device() helper in order to address this. Fixes: 1e4b8896c0f3 ("rust: drm: add device abstraction") Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/drm/device.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index d0a9528121f1..d29c477e89a8 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -120,9 +120,13 @@ pub fn new(dev: &device::Device, data: impl PinInit) -> Result *mut Self { unsafe { crate::container_of!(Opaque::cast_from(ptr), Self, dev) }= .cast_mut() } =20 + /// # Safety + /// + /// `ptr` must be a valid pointer to `Self`. + unsafe fn into_drm_device(ptr: NonNull) -> *mut bindings::drm_de= vice { + // SAFETY: By the safety requirements of this function, `ptr` is a= valid pointer to `Self`. + unsafe { &raw mut (*ptr.as_ptr()).dev }.cast() + } + /// Not intended to be called externally, except via declare_drm_ioctl= s!() /// /// # Safety @@ -192,8 +204,11 @@ fn inc_ref(&self) { } =20 unsafe fn dec_ref(obj: NonNull) { + // SAFETY: `obj` is a valid pointer to `Self`. + let drm_dev =3D unsafe { Self::into_drm_device(obj) }; + // SAFETY: The safety requirements guarantee that the refcount is = non-zero. - unsafe { bindings::drm_dev_put(obj.cast().as_ptr()) }; + unsafe { bindings::drm_dev_put(drm_dev) }; } } =20 --=20 2.50.0