From nobody Tue Dec 2 02:56:49 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 CF358228CB0; Tue, 18 Nov 2025 13:27:28 +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=1763472448; cv=none; b=D3aLVVLgMmPSPZlAS0k/bISKYGgXdlezCoGVG+1UT8GppncTqJ6OJAd5Sm9mbCjh7pC77lOlXZ9liM5z90d9oS20MGEnUQFsX765zODtj+MKG4VHg35NmACr4rU/PtFV0uH6X41LDp6r+ple7hVYUc7W0kQJkRHXuRqAQyzabbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763472448; c=relaxed/simple; bh=+vC2aosMFk7TAuVXidYijONEtqq2NTUeKHA8bbK0DN4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HzyE12KWJ/H+LAJ6Vak2JnbyM+a4UI7R7wIU8KSi1pA1eOQmvzFoslxrFytXVOqrz0OS0+E1HIyojOxQ1/b/XcVoebAdGjVy7dq1oWcdQzBnnXMEzb75fPhemtXulf/zRmoGn6+S3tVJvR+N9Sp+paj8Z/jn4IcO/rma8caC6DA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N0CXGW3J; 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="N0CXGW3J" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9321C116D0; Tue, 18 Nov 2025 13:27:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1763472448; bh=+vC2aosMFk7TAuVXidYijONEtqq2NTUeKHA8bbK0DN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N0CXGW3JWp0veavKLZUuVe4IUD5q8w2IEqBoBiSrO4nckhqYK2crhsipIQzSFmjWz CAjqIGMxUP8y1uBCfBRL9CToTthbkTqw/9yZQcPYJAdT6uXBBRIcMPZmdDoHK8XR8j uermgdp41qsnYS7ZoN8867QLUu7/oTMjRSM1JVIyfnVVnN/dUKQIgrD+lniDdN2mY1 DMa9NKKvnkIGmulzgwnm3H04ya0afDOCsfkF20omdRJy7zOYHy8u0uDFyIwlnhfSNv Nq93mNa8bOP16kjzUEfUoJtw3NIJcqyjP6U3/OWmxj1Jwx9YN4ePzfBKauo8PUWYUZ 6wHGY47oCKS0A== From: Philipp Stanner To: Alice Ryhl , Danilo Krummrich , =?UTF-8?q?Christian=20K=C3=B6nig?= , Tvrtko Ursulin , Alexandre Courbot , Daniel Almeida , Boris Brezillon , Dave Airlie , Lyude Paul , Peter Colberg Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Philipp Stanner , stable@vger.kernel.org Subject: [RFC WIP 1/3] rust: list: Add unsafe for container_of Date: Tue, 18 Nov 2025 14:25:17 +0100 Message-ID: <20251118132520.266179-3-phasta@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20251118132520.266179-2-phasta@kernel.org> References: <20251118132520.266179-2-phasta@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" impl_list_item_mod.rs calls container_of() without unsafe blocks at a couple of places. Since container_of() is an unsafe macro / function, the blocks are strictly necessary. For unknown reasons, that problem was so far not visible and only gets visible once one utilizes the list implementation from within the core crate: error[E0133]: call to unsafe function `core::ptr::mut_ptr::::b= yte_sub` is unsafe and requires unsafe block --> rust/kernel/lib.rs:252:29 | 252 | let container_ptr =3D field_ptr.byte_sub(offset).cast::<$Co= ntainer>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsa= fe function | ::: rust/kernel/drm/jq.rs:98:1 | 98 | / impl_list_item! { 99 | | impl ListItem<0> for BasicItem { using ListLinks { self.links }= ; } 100 | | } | |_- in this macro invocation | note: an unsafe function restricts its caller, but its body is safe by defa= ult --> rust/kernel/list/impl_list_item_mod.rs:216:13 | 216 | unsafe fn view_value(me: *mut $crate::list::ListLinks<$= num>) -> *const Self { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^= ^^^^^^^^^^^^^^^^^^^^ | ::: rust/kernel/drm/jq.rs:98:1 | 98 | / impl_list_item! { 99 | | impl ListItem<0> for BasicItem { using ListLinks { self.links }= ; } 100 | | } | |_- in this macro invocation =3D note: requested on the command line with `-D unsafe-op-in-unsafe-fn` =3D note: this error originates in the macro `$crate::container_of` whi= ch comes from the expansion of the macro `impl_list_item` Add unsafe blocks to container_of to fix the issue. Cc: stable@vger.kernel.org # v6.17+ Fixes: c77f85b347dd ("rust: list: remove OFFSET constants") Suggested-by: Alice Ryhl Signed-off-by: Philipp Stanner --- rust/kernel/list/impl_list_item_mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs index 202bc6f97c13..7052095efde5 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -217,7 +217,7 @@ unsafe fn view_value(me: *mut $crate::list::ListLinks<$= num>) -> *const Self { // SAFETY: `me` originates from the most recent call to `p= repare_to_insert`, so it // points at the field `$field` in a value of type `Self`.= Thus, reversing that // operation is still in-bounds of the allocation. - $crate::container_of!(me, Self, $($field).*) + unsafe { $crate::container_of!(me, Self, $($field).*) } } =20 // GUARANTEES: @@ -242,7 +242,7 @@ unsafe fn post_remove(me: *mut $crate::list::ListLinks<= $num>) -> *const Self { // SAFETY: `me` originates from the most recent call to `p= repare_to_insert`, so it // points at the field `$field` in a value of type `Self`.= Thus, reversing that // operation is still in-bounds of the allocation. - $crate::container_of!(me, Self, $($field).*) + unsafe { $crate::container_of!(me, Self, $($field).*) } } } )*}; @@ -270,9 +270,9 @@ unsafe fn prepare_to_insert(me: *const Self) -> *mut $c= rate::list::ListLinks<$nu // SAFETY: The caller promises that `me` points at a valid= value of type `Self`. let links_field =3D unsafe { >::view_links(me) }; =20 - let container =3D $crate::container_of!( + let container =3D unsafe { $crate::container_of!( links_field, $crate::list::ListLinksSelfPtr, inner - ); + ) }; =20 // SAFETY: By the same reasoning above, `links_field` is a= valid pointer. let self_ptr =3D unsafe { @@ -319,9 +319,9 @@ unsafe fn view_links(me: *const Self) -> *mut $crate::l= ist::ListLinks<$num> { // `ListArc` containing `Self` until the next call to `post_= remove`. The value cannot // be destroyed while a `ListArc` reference exists. unsafe fn view_value(links_field: *mut $crate::list::ListLinks= <$num>) -> *const Self { - let container =3D $crate::container_of!( + let container =3D unsafe { $crate::container_of!( links_field, $crate::list::ListLinksSelfPtr, inner - ); + ) }; =20 // SAFETY: By the same reasoning above, `links_field` is a= valid pointer. let self_ptr =3D unsafe { --=20 2.49.0