From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D53A33DA7C8 for ; Wed, 4 Feb 2026 10:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770201008; cv=none; b=pmGvcZ/ypLlF0cST5ui8vsCAXXLGfwFbhsml2Pm2n2lgteXqKkdTLlz6C//P5ku6DCssp/bv2sPIlrE+EsnuOdApxM31gQDdilS6btsPCqUE052YMuDtzg+8jcmZmaScaZGtvhJlQ0cXSEYGBZYvLWtgAVQYhWiqXP2LLiE5VxY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770201008; c=relaxed/simple; bh=2UIZKStHuem4vyD9tR6NvtzZe7OLWtG1Pj1z+tGYGA4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KM7Fc7e6NgmNq/IqxXkNuFEUXZ99yVBU6aLV7xEvWrOtV7eKC1bj7UiAC9aFN551emMODSaOZaOcaQGqE4ZeksdX5y9c02IsurMM2/09lhJLoWtgFkHWR7J6fAeVfYtaL5RuvVyi56BbMVNxDTYhHARA+if0tsTwlJIMNWFodgA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=c8MjBlvc; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="c8MjBlvc" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-48057c39931so74078875e9.0 for ; Wed, 04 Feb 2026 02:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770201006; x=1770805806; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8XSWebpuCWegMlwjG4hS6R8tDkiXl6gBTB7fLfLRPN4=; b=c8MjBlvcqlQQCv3adiMbtOGJYaPrIvKhOwqvH4zpQKXxrAhCXpJwSw5pGcYEwVrYPz AD4kna/J70h6yiObSvQnXC9Mn/ghDyOqTHxqjiB5a9zEoSnfulEFiL+X9vHSHtMghLqU 53sC7zgkJVF9wrOmKmBY9NrgaP9XE7fwnDrrl2gEFRuw8dXNL0Va4nrYiOGAHqZIIrXT bxsKMrifNHUxHXuiFHkyquc5mYMpJ1NZWEMEMeE/cpjD9SuY3wsSWqm4kmC/f2iXlWT8 +YlqYVdyPw6UHVONkhy/F9mGr0EmO+gIvOk/ruAk+9v5mvL38RoP/iAl04+E4IcYIkQx g8bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770201006; x=1770805806; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8XSWebpuCWegMlwjG4hS6R8tDkiXl6gBTB7fLfLRPN4=; b=EKwBulF9/BSKZocjzWUubEoV8iH0sGFzhC2BhMeR8nrJ9ik9c4QRq13FXHDhVE+FVx F/ls8M+8SvxbGIcdTsI0YCTrMWHs0735a7U1+rNhP2TkNZ0nj0O9zXFrRbwHyJOQwGdK 8DFzeeupfLNywKl5urahiT4FBNtp3lXykyh5GKSEKC/JM/IyERuFUd5a8CgCdldQltIF fJWTHOXVXqeoasK5+3CjyZCGGt7OMxk1y4lzCYkNcnJIcmgxhBM1lQvCxdQC6OrtDQO4 0Bah6HpIiNW4wY1w+CwFpeIjkJH3BPvnAtACAi3NJvwzZsfniGl1zirtAlqAUSyARsEF e1mQ== X-Forwarded-Encrypted: i=1; AJvYcCWukp0bwo1I1+tPfbORw55ZNoWSiIy6uVvOfrneJ2UrpbEEWduBuuZSDzV+Lf0GT8VcNr8hPCM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw9JMSYjeG9iCr/I15mbYJliLXgIEPhGo5QrkeCnzjpx6Ctrzjk F7tb3KOQULr2UCZBjewIScUvQ5owjm4HA3qNccLf/QQWqYOvtvp4a3I0URlh0bVg/pzeR38C77q HL/1bAQTxH+BzKaK08A== X-Received: from wmber8.prod.google.com ([2002:a05:600c:84c8:b0:47e:e4a5:c5f2]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1c02:b0:475:dd89:acb with SMTP id 5b1f17b1804b1-4830e96fb19mr35946565e9.22.1770201006224; Wed, 04 Feb 2026 02:30:06 -0800 (PST) Date: Wed, 4 Feb 2026 10:30:05 +0000 In-Reply-To: <20260203081403.68733-3-phasta@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260203081403.68733-2-phasta@kernel.org> <20260203081403.68733-3-phasta@kernel.org> Message-ID: Subject: Re: [RFC PATCH 1/4] rust: list: Add unsafe for container_of From: Alice Ryhl To: Philipp Stanner Cc: David Airlie , Simona Vetter , Danilo Krummrich , Gary Guo , Benno Lossin , "Christian =?utf-8?B?S8O2bmln?=" , Boris Brezillon , Daniel Almeida , Joel Fernandes , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Tue, Feb 03, 2026 at 09:14:00AM +0100, Philipp Stanner wrote: > 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::::byte_sub` > is unsafe and requires unsafe block > --> rust/kernel/lib.rs:252:29 > | > 252 | let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>(); > | ^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe 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 default > --> 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 > = note: requested on the command line with `-D unsafe-op-in-unsafe-fn` > = note: this error originates in the macro `$crate::container_of` which 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 With the reason that Gary shared added to the commit message: Reviewed-by: Alice Ryhl > --- > 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 `prepare_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).*) } > } > > // 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 `prepare_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 $crate::list::ListLinks<$nu > // SAFETY: The caller promises that `me` points at a valid value of type `Self`. > let links_field = unsafe { >::view_links(me) }; > > - let container = $crate::container_of!( > + let container = unsafe { $crate::container_of!( > links_field, $crate::list::ListLinksSelfPtr, inner > - ); > + ) }; It may be cleaner to write this as: let container = unsafe { $crate::container_of!( links_field, $crate::list::ListLinksSelfPtr, inner ) }; Rustfmt has no effect on macro definitions, but if this was not a macro, then I believe that rustfmt would format it like the above. > > // SAFETY: By the same reasoning above, `links_field` is a valid pointer. > let self_ptr = unsafe { > @@ -319,9 +319,9 @@ unsafe fn view_links(me: *const Self) -> *mut $crate::list::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 = $crate::container_of!( > + let container = unsafe { $crate::container_of!( > links_field, $crate::list::ListLinksSelfPtr, inner > - ); > + ) }; Ditto here. Alice