From patchwork Mon Mar 24 21:33:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14027873 X-Patchwork-Delegate: bhelgaas@google.com Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) (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 53FB919005D; Mon, 24 Mar 2025 21:33:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852042; cv=none; b=jMEKUAW8YPauwii+ePdrelcTI71+2FzjseMW4bZu81c4Is5N/gqUvIrIGxV5sh/VtDfdqwYMaH/e6W9fsQ0wq3CcqlmnAoUL3AXN4FUxg76HvudI8uU69qL+6a89AO5V60yONMpPC9QuDD3w+L9GIMC8E1oT/DzbwCNqZIhCUgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852042; c=relaxed/simple; bh=jXBOpo5UWtwyrzWvahiJ2pLNqcTsAoQ2rJCCE9+N488=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SWohPDmzquJ8vD/t5CbWfaVqNktsjlLPIG7zxOLVjwrjecWNbV445DT/V6DzONkyacfharLp1PeQJK4B1dXv4EPxp2x3Nc1PUsmED8ZaxnlWZPuiFJPI9aX5hVb4vJI1zaZZ4KphKWU9851tf7kaQrgTxVD/Bl7O7de+uSdhiTk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D7wnA3d2; arc=none smtp.client-ip=209.85.222.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D7wnA3d2" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7c5ba363f1aso537575785a.0; Mon, 24 Mar 2025 14:33:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742852038; x=1743456838; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=VLYdB8oz2i/GfcI4zN53mys18Ki2XN46F1DPNODr+Tk=; b=D7wnA3d2q2RvpG1ZV6mowS1KDLazGK8FMfdZRL7cBTqvd+XXzA854N+5o0w2z9FIjM /OrzBG+QtIG8LIqM0EFyc+iXErelZ2BWEQHaJWFVXzkQF8mnJaQcZPTCGNNDTarotAZf yCA5d6IRmqlGC5HD31Fm6DOV9dEqGTArFbTvPvxap46FP96abOrdEgsSf7APfR2L6uY2 N2fV8FI9+lmr/npOEgE/FZ4tz5GA/lWsuiL58zXUCcGh4kPdeRFsduXTGJeATl/EEMH0 f6scGGYzh2bqfVWMsybwcCe1v+W5vkKZ2XgrXUDoU8TY95KPAxC0RLSA7LjSpInVFQx1 kmCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742852038; x=1743456838; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VLYdB8oz2i/GfcI4zN53mys18Ki2XN46F1DPNODr+Tk=; b=EAOhkb+eu1pv+zOGrAclHrHENr+LrDUPOxd9hDhizgKh5cYXBmBD57Mjki3+gu4Tf5 571sd3UqxDOxK2jTmXBSbelYHYgK5YigDDYhrnprwdlT5LxmjDRrPthzHJn03MDrCPxl MXv9N/6fvdswDRDOQZP5rqPUzzLhJRPi2WAEbH4XAR03TAhJ1EgYaLHfx0iD8KZulYot 3hECbVM+BRxbZ8XMTqLPAMsH0Hcuj7vdjIOQyrNcyx3JY2ULzNaCWIc+UH+bpn6q322s mZ7lMxLghwWUuUOSw09OIZ9Js/MzbRW/PhvU+mnTq/8hsntvuo955xJU6XZR31BYutUt wxAw== X-Forwarded-Encrypted: i=1; AJvYcCV9htNihED/eEIklK1X8g23zut7AsxULQINeEzVkyJrHJ+ldN25DMteLib1nL6ydFd6bsO5WyJPi/py@vger.kernel.org, AJvYcCVcRnNHxGKullXpPlRvfV4WExaIZKGt6sBQFZj5e8Kq7yW9IHdseVrER7KXWOZSCfoZMJfkraSbAJK2mqw=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+xMmBMD047K8ONWQrpQ9IMYVFynABNlq77fHCvQUdP2EAPqBq su5pn47msvaDeE67ZcjZhAHwlYsuFXkgrVY8GmrT0mIgFGXXuv83 X-Gm-Gg: ASbGncuuGBupIZGIAuTu78Kjx3YI/CN21w345NHIt6XsJ01iAQX/z1eNoEr645DrlQP Rt5ZJn9+uW5nGPKOqcQ887389fT7hVEkZApBUNRLraZnYXlms8gyjxsiciIXz31c68B+VbiPLDO DYZWT69ktjPMWJcAI+JaGRasM3/3EkdwR6BHZO+aGZKMKitHpbTn0fjLpILryTYAie4zFEnM1ez 7eGkQKC0w4x3GDFXJTT7f18IRAoOYiirEVkn5bRAq+tiO9qmFaqRLGqCRcHtfj/BaMN3P/vubEq KvJAPgypNBObhg6L/3Wmjm0mfrFFkZWDv1tX1I1jQIJmzw5nd9q+KxTEn7A+BcH9pqW1xoSvGEA 8WWl0313wd1VXO6k2OXGiv4Co8ITHq790So4zUsdFmFhb9Z+oeI/a+g== X-Google-Smtp-Source: AGHT+IEhrv6Ny4E6jksCPXyYsMYyMKqcU946IpYnjimcFYzESWuGYTSeITIPQrWWoMP2jB9I87MPNg== X-Received: by 2002:a05:620a:4089:b0:7c5:4daa:2511 with SMTP id af79cd13be357-7c5ba1f1102mr1963053785a.52.1742852037913; Mon, 24 Mar 2025 14:33:57 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:43c7]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c5d7eb96fesm63232185a.90.2025.03.24.14.33.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 14:33:57 -0700 (PDT) From: Tamir Duberstein Date: Mon, 24 Mar 2025 17:33:43 -0400 Subject: [PATCH 1/5] rust: retain pointer mut-ness in `container_of!` Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250324-list-no-offset-v1-1-afd2b7fc442a@gmail.com> References: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> In-Reply-To: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Avoid casting the input pointer to `*const _`, allowing the output pointer to be `*mut` if the input is `*mut`. This allows a number of `*const` to `*mut` conversions to be removed at the cost of slightly worse ergonomics when the macro is used with a reference rather than a pointer; the only example of this was in the macro's own doctest. Reviewed-by: Benno Lossin Reviewed-by: Alice Ryhl Signed-off-by: Tamir Duberstein --- rust/kernel/lib.rs | 5 ++--- rust/kernel/pci.rs | 2 +- rust/kernel/platform.rs | 2 +- rust/kernel/rbtree.rs | 23 ++++++++++------------- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index ba0f3b0297b2..cffa0d837f06 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -190,7 +190,7 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { /// } /// /// let test = Test { a: 10, b: 20 }; -/// let b_ptr = &test.b; +/// let b_ptr: *const _ = &test.b; /// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be /// // in-bounds of the same allocation as `b_ptr`. /// let test_alias = unsafe { container_of!(b_ptr, Test, b) }; @@ -199,9 +199,8 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { #[macro_export] macro_rules! container_of { ($ptr:expr, $type:ty, $($f:tt)*) => {{ - let ptr = $ptr as *const _ as *const u8; let offset: usize = ::core::mem::offset_of!($type, $($f)*); - ptr.sub(offset) as *const $type + $ptr.byte_sub(offset).cast::<$type>() }} } diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index f7b2743828ae..271a7690a9a0 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -364,7 +364,7 @@ pub unsafe fn from_dev(dev: ARef) -> Self { fn as_raw(&self) -> *mut bindings::pci_dev { // SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device` // embedded in `struct pci_dev`. - unsafe { container_of!(self.0.as_raw(), bindings::pci_dev, dev) as _ } + unsafe { container_of!(self.0.as_raw(), bindings::pci_dev, dev) } } /// Returns the PCI vendor ID. diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 1297f5292ba9..84a4ecc642a1 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -189,7 +189,7 @@ unsafe fn from_dev(dev: ARef) -> Self { fn as_raw(&self) -> *mut bindings::platform_device { // SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device` // embedded in `struct platform_device`. - unsafe { container_of!(self.0.as_raw(), bindings::platform_device, dev) }.cast_mut() + unsafe { container_of!(self.0.as_raw(), bindings::platform_device, dev) } } } diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs index 5246b2c8a4ff..8d978c896747 100644 --- a/rust/kernel/rbtree.rs +++ b/rust/kernel/rbtree.rs @@ -424,7 +424,7 @@ pub fn cursor_lower_bound(&mut self, key: &K) -> Option> while !node.is_null() { // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self` // point to the links field of `Node` objects. - let this = unsafe { container_of!(node, Node, links) }.cast_mut(); + let this = unsafe { container_of!(node, Node, links) }; // SAFETY: `this` is a non-null node so it is valid by the type invariants. let this_key = unsafe { &(*this).key }; // SAFETY: `node` is a non-null node so it is valid by the type invariants. @@ -496,7 +496,7 @@ fn drop(&mut self) { // but it is not observable. The loop invariant is still maintained. // SAFETY: `this` is valid per the loop invariant. - unsafe { drop(KBox::from_raw(this.cast_mut())) }; + unsafe { drop(KBox::from_raw(this)) }; } } } @@ -761,7 +761,7 @@ pub fn remove_current(self) -> (Option, RBTreeNode) { let next = self.get_neighbor_raw(Direction::Next); // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self` // point to the links field of `Node` objects. - let this = unsafe { container_of!(self.current.as_ptr(), Node, links) }.cast_mut(); + let this = unsafe { container_of!(self.current.as_ptr(), Node, links) }; // SAFETY: `this` is valid by the type invariants as described above. let node = unsafe { KBox::from_raw(this) }; let node = RBTreeNode { node }; @@ -806,7 +806,7 @@ fn remove_neighbor(&mut self, direction: Direction) -> Option> unsafe { bindings::rb_erase(neighbor, addr_of_mut!(self.tree.root)) }; // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self` // point to the links field of `Node` objects. - let this = unsafe { container_of!(neighbor, Node, links) }.cast_mut(); + let this = unsafe { container_of!(neighbor, Node, links) }; // SAFETY: `this` is valid by the type invariants as described above. let node = unsafe { KBox::from_raw(this) }; return Some(RBTreeNode { node }); @@ -912,7 +912,7 @@ unsafe fn to_key_value_mut<'b>(node: NonNull) -> (&'b K, &'b unsafe fn to_key_value_raw<'b>(node: NonNull) -> (&'b K, *mut V) { // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self` // point to the links field of `Node` objects. - let this = unsafe { container_of!(node.as_ptr(), Node, links) }.cast_mut(); + let this = unsafe { container_of!(node.as_ptr(), Node, links) }; // SAFETY: The passed `node` is the current node or a non-null neighbor, // thus `this` is valid by the type invariants. let k = unsafe { &(*this).key }; @@ -1021,7 +1021,7 @@ fn next(&mut self) -> Option { // SAFETY: By the type invariant of `IterRaw`, `self.next` is a valid node in an `RBTree`, // and by the type invariant of `RBTree`, all nodes point to the links field of `Node` objects. - let cur = unsafe { container_of!(self.next, Node, links) }.cast_mut(); + let cur = unsafe { container_of!(self.next, Node, links) }; // SAFETY: `self.next` is a valid tree node by the type invariants. self.next = unsafe { bindings::rb_next(self.next) }; @@ -1216,7 +1216,7 @@ pub fn get_mut(&mut self) -> &mut V { // SAFETY: // - `self.node_links` is a valid pointer to a node in the tree. // - We have exclusive access to the underlying tree, and can thus give out a mutable reference. - unsafe { &mut (*(container_of!(self.node_links, Node, links).cast_mut())).value } + unsafe { &mut (*(container_of!(self.node_links, Node, links))).value } } /// Converts the entry into a mutable reference to its value. @@ -1226,7 +1226,7 @@ pub fn into_mut(self) -> &'a mut V { // SAFETY: // - `self.node_links` is a valid pointer to a node in the tree. // - This consumes the `&'a mut RBTree`, therefore it can give out a mutable reference that lives for `'a`. - unsafe { &mut (*(container_of!(self.node_links, Node, links).cast_mut())).value } + unsafe { &mut (*(container_of!(self.node_links, Node, links))).value } } /// Remove this entry from the [`RBTree`]. @@ -1239,9 +1239,7 @@ pub fn remove_node(self) -> RBTreeNode { RBTreeNode { // SAFETY: The node was a node in the tree, but we removed it, so we can convert it // back into a box. - node: unsafe { - KBox::from_raw(container_of!(self.node_links, Node, links).cast_mut()) - }, + node: unsafe { KBox::from_raw(container_of!(self.node_links, Node, links)) }, } } @@ -1272,8 +1270,7 @@ fn replace(self, node: RBTreeNode) -> RBTreeNode { // SAFETY: // - `self.node_ptr` produces a valid pointer to a node in the tree. // - Now that we removed this entry from the tree, we can convert the node to a box. - let old_node = - unsafe { KBox::from_raw(container_of!(self.node_links, Node, links).cast_mut()) }; + let old_node = unsafe { KBox::from_raw(container_of!(self.node_links, Node, links)) }; RBTreeNode { node: old_node } } From patchwork Mon Mar 24 21:33:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14027874 X-Patchwork-Delegate: bhelgaas@google.com Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (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 195741B21BD; Mon, 24 Mar 2025 21:34:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852042; cv=none; b=HELCjM3FY19k0hW6fQFulYDBrAOJJORmNNBvtm0C6kuFV6Fr/LTH+t4GojRbleNLJKuWcnkKuMvBMJCJXLads1ntf8QKQXV6OL8MQXfWTxP3OwQgctxGvmh4tVwnHUbpesWnbUTzdhz1yJA60f02R1EXUIy+DobnqHEQ3LcWDDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852042; c=relaxed/simple; bh=7btLvyhH9iVv2PqOSFlWfFuJYdzdZSPAxYmuaLLlzQ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RnlUIvQpVf03KaNhHyyeFhsZzlnz6lT0cu6fqsXSsrd1OJOfmrac3/uaLbeBqeCRzQVeh0rUwxMlhEfk+g7mNj5kg0M2TJzcF0frj6lZPeXGYRiXrTIty09gaGfFkxtjjc7zZz7A0Qv+Leyp+BPMTltTHMOGHd+HqQuMCRFSRs0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l3ofSrmu; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l3ofSrmu" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-7c5aecec8f3so724716985a.1; Mon, 24 Mar 2025 14:34:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742852040; x=1743456840; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=qiJt3uTSVh93mv9thEPIRmNnr7a1YuilRvvelKO7uBs=; b=l3ofSrmudoY5EusnQnHeNRtc29MOfnmTIJVaCWDwy0FktHBuFab94KfhMW5gu2WpQS /opnd3tkjz62+qi7M0SYOEEB+HHW+ujnZk2JqOLbbE4WF/PjRDIp/b9/HdIgQscEddIo tPBWrNdVqYUQdqhSVeINqeznQNBckf+2E8WTQZxin67t4xVrND4a2a5n2Xp9/G45MCRD W75Z1oQr53Yx9XJPtxxG/WTjUAcJhpOmFvEP/hdpBCYdPf3dYQZKp3knOL+DAwIV8z3S LF7nY+9D80bISzBk3euYYFNumm9uE/+UF5+gkI3c7wtKeuktdoipl3nOMoC5w19SAdTu lymQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742852040; x=1743456840; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qiJt3uTSVh93mv9thEPIRmNnr7a1YuilRvvelKO7uBs=; b=uQAGM72UP39ZEkNEp+67fj0uVNjrMQ1YhxXwWEPkK4O8J3KujooqO8Ao9RMvTq6xI5 cCMBk2F5JRuWroEIe7fTEZ9dWAjvhMLtzgMYSkpnzurospJ7LCF2ul/eehs7+KggAEw/ PhX8f3fIKIEEi6ieH8ZihcJ6ZlXpZp/Wty1PhGJY53HqEACf9moG6TbW8JuHS0zoBq7j rfKQrH4fp3NblrlVNfw7WNvqYfqCtPDJZyadc/UhGN5F8oqvZGO7gkyEz8anw8PFJamd HViJ9OaGWVCoLKZhm6BCqSI5VpbkRustIPPsEwvbTCaWD+UmUAcFDLQZcZugYEBdw+pI WVRQ== X-Forwarded-Encrypted: i=1; AJvYcCVySWOMhVyDqvbuwsyBKdgcEBjnidBLvSbFyrZ2TEUvifsfsuEZFlWxqLORnqZ4iHwtzLjoxEGN/AC2XLA=@vger.kernel.org, AJvYcCW+HlOtYV8HDPQd0dBDpRkDmx8Ox3BDaErAOrvxwqLMBUB131brAextbDjRHyGJPxYpq58D+lqZroSc@vger.kernel.org X-Gm-Message-State: AOJu0YwKnr73+cQiLY040xmmSiVsu/mQjoOJVcg/LcsMtb+2fOGSrrXN TnxFlMG+R/Lwugy4TC7QrbTDfiXakJByuhCC0Ve5aKApVo4dT5FH X-Gm-Gg: ASbGncs4ithslP9tdEB13c3DzVtnXFEpUmHiAJsTAel69yD4ebojQDHM+u/sYIxYvFq PoAwvoaGaZYKFH2T6aljqjcHPQ5JPtRBNy+clpjRCSIhsYqEj6rmE9//Omhg8CN5Fa6s8OH+apy FmXZFF9WPB3dBZ6XgpY0/rPSgtcAUFNV/XYoBR5VkBxcjSiz1NBjdv9WgL9lksVhyRS+xHe00p7 E4pIfocsYjsbzaFS/Y7PZXz6HZ5BRoSoob9/QymJVM1DZHn3w93odyDKTmxwn8VGZUlJgKtD7UW PdptLhcoPALKGZO78niDxSSilDuqEMitpXppZbNq9z3/K6hGeRdpu0iTQh1NkWujKbBFNuQCA4z lbDZRqQh1BjrbB/+fylE9701lxd5A+DY8UAJWDQd05jDfCQAIKSvCZQ== X-Google-Smtp-Source: AGHT+IFeeTW5DcojPaGvy4zB1UmY0TMPXv3ig/pMcvGf1V3UIaKRKh1hBjHMa3Ymkjv2blszpWvEzg== X-Received: by 2002:a05:620a:258a:b0:7c5:4dc4:ae48 with SMTP id af79cd13be357-7c5ba209231mr2136739785a.39.1742852039856; Mon, 24 Mar 2025 14:33:59 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:43c7]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c5d7eb96fesm63232185a.90.2025.03.24.14.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 14:33:59 -0700 (PDT) From: Tamir Duberstein Date: Mon, 24 Mar 2025 17:33:44 -0400 Subject: [PATCH 2/5] rust: list: simplify macro capture Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250324-list-no-offset-v1-2-afd2b7fc442a@gmail.com> References: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> In-Reply-To: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Avoid manually capturing generics; use `ty` to capture the whole type instead. Signed-off-by: Tamir Duberstein --- rust/kernel/list/impl_list_item_mod.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl_list_item_mod.rs index a0438537cee1..5ed66fdce953 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -43,7 +43,7 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks { macro_rules! impl_has_list_links { ($(impl$(<$($implarg:ident),*>)? HasListLinks$(<$id:tt>)? - for $self:ident $(<$($selfarg:ty),*>)? + for $self:ty { self$(.$field:ident)* } )*) => {$( // SAFETY: The implementation of `raw_get_list_links` only compiles if the field has the @@ -51,9 +51,7 @@ macro_rules! impl_has_list_links { // // The behavior of `raw_get_list_links` is not changed since the `addr_of_mut!` macro is // equivalent to the pointer offset operation in the trait definition. - unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)? for - $self $(<$($selfarg),*>)? - { + unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)? for $self { const OFFSET: usize = ::core::mem::offset_of!(Self, $($field).*) as usize; #[inline] @@ -85,18 +83,14 @@ pub unsafe trait HasSelfPtr macro_rules! impl_has_list_links_self_ptr { ($(impl$({$($implarg:tt)*})? HasSelfPtr<$item_type:ty $(, $id:tt)?> - for $self:ident $(<$($selfarg:ty),*>)? + for $self:ty { self.$field:ident } )*) => {$( // SAFETY: The implementation of `raw_get_list_links` only compiles if the field has the // right type. - unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type $(, $id)?> for - $self $(<$($selfarg),*>)? - {} + unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {} - unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? for - $self $(<$($selfarg),*>)? - { + unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? for $self { const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; #[inline] From patchwork Mon Mar 24 21:33:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14027875 X-Patchwork-Delegate: bhelgaas@google.com Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 4597F1E9907; Mon, 24 Mar 2025 21:34:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852043; cv=none; b=D/yS4gt6sSxI0RK1aOFRvfF9pAHuQgXxh/M1sO+yw2phbklLCLWZKmzYMttPAEEJrRDLRIg9XO7AApqci+aDt59sor2S81bcIixHYJLJQdtk50Vio89RosKkommgEg2stFpgNDIPgWlVLwPknntjO2OpeyAOejtnV5/L5Z1kTEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852043; c=relaxed/simple; bh=UPIkAUVgrLgoo10ZulYm0QWcMYrd1P6bki8vAC5nVdw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g/P2gSIbPFEDJY4Q/2RSbVDg/mAO+J+BCzVELqfyMG/GADM8kK3tyhQkcoc8j3sNJf3s8G/ncQ8sYKH9aa72FSAUY54bkWAEZWLD06TRtl+bTcyE9kh+zMg1hqej1W2SwwC8/y45SUESx0cRW2vzAnDdEJDYeNsfvPmpnMcT6Jg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nh4KJPgp; arc=none smtp.client-ip=209.85.222.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nh4KJPgp" Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7c0e135e953so507067385a.2; Mon, 24 Mar 2025 14:34:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742852041; x=1743456841; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ylNRb+T5+GBQppZT9y7HdwTw6rPxl9tLyEp3IloGtoo=; b=nh4KJPgpX/srC27VVQ1lD4cVli6e71PiQyNZOKH3ZfQF12OarbLzYc3dgKNiN12oca WB5r3nhaoM4i0Z0uWS6AgPiaBygwa4g1il/QqjkkepzAcAnuuzfEbTUbm4130AR4nTxu Jkqlfep0Tw+zP7gz2HW3Szm//9OS7h563E6ADSP5yaf35/PYdtZDy2j2KtFT8lKjhkTh dlvW7wnjk6ESbzSXbt3qsrB3CeqtfGDSCE3brFikcJpUcLOGpKrjnQFmxDPJ2KqyunKb 7yZC8yGrsnbt68+KqWs7XqtFAx62jjX+7v0oqgSTyCcNoNyt54lLsaYkLbMMrSc6mOzN xwjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742852041; x=1743456841; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ylNRb+T5+GBQppZT9y7HdwTw6rPxl9tLyEp3IloGtoo=; b=lLkU+08Gwze4Z5j5TGNXD/4AeJS64vGn9cv09slSFp9HcEmawWQBW910qy6Z+zk6t0 StvJZmN/nCh8KrnLXxObXLA2n8UjKa95RV/q5ueQzEdV0T1MJ6tyRFX9CU16bU1T3Mqw o0LU2psYkcwNb0O1bzF3TkFS3KDXwZBcfnu4/sfRmlDavOIea7U+5Qk9S/pBXXEtUh4j pNP/dEmI7IpABL4OzrOVhmR8W6SgbZ64Xtf2M7kBpfo/1wJf6wZe91yulRrEEA7fCwCO 0R2Q8L+wMxYBnKEqhiHP0uQhuGr9TDmXIu4/PN2S4BI3mEig0/wl115NJp/h5BF6jJ8N 9DJQ== X-Forwarded-Encrypted: i=1; AJvYcCUSYZZQ1CF1+KPEgKfv+BSOW08+wSTZVkxmyGBA5z2IR7ID1rRA4zZX5n+unavtNy4JUXS5hUz2uYHOMdo=@vger.kernel.org, AJvYcCXqD0pbaAWXOvDKD+DcaAD4GqtstxvDoFJ+amn9YA+nDyDzj76cb2IuxrkGn5+hX6ffTg6nRrvxpHsR@vger.kernel.org X-Gm-Message-State: AOJu0Yw9FxNm3psWhgsVmxmk84F0WGQH36vhDVMoEl2xrsRQ02CmKtPL wq353VX1Qs0inlEPUS98clFHBj8O5oKGVF9s0P3q28bwobE+0N8p X-Gm-Gg: ASbGncuSTqJJVZI5D7PyrGfMtmjfQ17c1Sz1GoE+GrHCGhygcsTjfYrLpohVbFlGQfw nXAHzDza/THqiYlA6jZDI2Ya7Wh+SDahIEswPM0N07MoG5JE+uSGhE80Qi5laUBjOB/6BPNl3HQ by+i7Yy3BxgCQrK79qCCdkeW4EqHMjKI5hRDKovUS3Owvof+5UW59bSWWmuuGiyW8JdaZS2WDdJ sZGeyg5mvRkyf36NTElhbnu/DvpvB2t+D+mEkaVDUmKjYCo2CWiMjeZXrW1EGoICHYb6ryrRwR1 kQ8uFFeiVA+tyESPKKVIZoTO1zh9iM/bmo6d5/HggCubHMk94EEgPB8J+S22d7wk/aRpg7IKAU3 LYarCetrfgvsH9bP1jm+RT4OeifChhbL+tcr5X+gU5korMa1pbyXcKw== X-Google-Smtp-Source: AGHT+IGjgpgbERblUHABST7Ycy5fXnhiGKSNNWdan9XOoeRU1z/0D1Puc5uILKt0eD70cFsUQbS1/Q== X-Received: by 2002:a05:620a:1787:b0:7c5:96a1:16c9 with SMTP id af79cd13be357-7c5ba13a4ffmr1948351285a.5.1742852040873; Mon, 24 Mar 2025 14:34:00 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:43c7]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c5d7eb96fesm63232185a.90.2025.03.24.14.33.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 14:34:00 -0700 (PDT) From: Tamir Duberstein Date: Mon, 24 Mar 2025 17:33:45 -0400 Subject: [PATCH 3/5] rust: list: use consistent type parameter names Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250324-list-no-offset-v1-3-afd2b7fc442a@gmail.com> References: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> In-Reply-To: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Refer to the type parameters of `impl_has_list_links{,_self_ptr}!` by the same name used in `impl_list_item!`. Signed-off-by: Tamir Duberstein --- rust/kernel/list/impl_list_item_mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl_list_item_mod.rs index 5ed66fdce953..9d2102138c48 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -41,7 +41,7 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks { /// Implements the [`HasListLinks`] trait for the given type. #[macro_export] macro_rules! impl_has_list_links { - ($(impl$(<$($implarg:ident),*>)? + ($(impl$(<$($generics:ident),*>)? HasListLinks$(<$id:tt>)? for $self:ty { self$(.$field:ident)* } @@ -51,7 +51,7 @@ macro_rules! impl_has_list_links { // // The behavior of `raw_get_list_links` is not changed since the `addr_of_mut!` macro is // equivalent to the pointer offset operation in the trait definition. - unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)? for $self { + unsafe impl$(<$($generics),*>)? $crate::list::HasListLinks$(<$id>)? for $self { const OFFSET: usize = ::core::mem::offset_of!(Self, $($field).*) as usize; #[inline] @@ -81,16 +81,16 @@ pub unsafe trait HasSelfPtr /// Implements the [`HasListLinks`] and [`HasSelfPtr`] traits for the given type. #[macro_export] macro_rules! impl_has_list_links_self_ptr { - ($(impl$({$($implarg:tt)*})? + ($(impl$({$($generics:tt)*})? HasSelfPtr<$item_type:ty $(, $id:tt)?> for $self:ty { self.$field:ident } )*) => {$( // SAFETY: The implementation of `raw_get_list_links` only compiles if the field has the // right type. - unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {} + unsafe impl$(<$($generics)*>)? $crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {} - unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? for $self { + unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)? for $self { const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; #[inline] From patchwork Mon Mar 24 21:33:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14027876 X-Patchwork-Delegate: bhelgaas@google.com Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (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 4F1D91EDA22; Mon, 24 Mar 2025 21:34:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852044; cv=none; b=otZwO4KXckPgxfg6vMmcHR6CQiXS1+M//8iHp3qbc1gOk65QX6KR7yxysAj6kz9P74eSyb/AsZ8vu19/OdSQ5hF4vpvVBY2eRd/UNiV05X9ffQa+qAJKbT2ZrZHzD1O2Yp3GMc28v5e0eJ6hT9XPCtsqxjYZbgNtcfntz6L3rFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852044; c=relaxed/simple; bh=yh8j1BJZqcNaOViRUXHo+n8R+JB96pf3PLd2pg6YpqI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QdOLnfbSIWxJQTuUblr0b4/zROpY+8vcJvw2LJkV6mn6viGwZBEFOG+FyaUI1hlB4bl2EWXLCL8Pibsd4p6geulE1AWD+77xXORyMs5WpIFY0ibw9FbL6daFiTGzZHcJ2kskcMqZj/RK3yW5GNNozWD4AApDwYBsbd72j8T1uJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QmwtLKQM; arc=none smtp.client-ip=209.85.222.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QmwtLKQM" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-7c59e7039eeso684609485a.2; Mon, 24 Mar 2025 14:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742852042; x=1743456842; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=M7f6jbjUsppRuEOrY1E+FMO2KFZ0EMMyVLzWv4tcTkY=; b=QmwtLKQM9gRUp5vsLDHtc3F0ON8i6BFXDavZ3gJzx2EiIJ3ckBDolpNMfH/bU1Ge7g 7HlWQAucmXHSIlydtNU6aVn9iXY7yeRgABM0L/2e6zdCxncsgqZ3pECWDRZywZc9zaQP HnhVgwAM19rQU5qcivvYSJCSYlt3c+ZQ07IYKh6v66t52JbimPRB9jbpo25N/a9Jxdv+ 67zgjVxBbXUcmMoZuSe9EiYXZLLehc1CjW+6fIGn9M9cmahOabfqCfFbTUFNh3OxXx+p 8X+R4GWJgjOI50cP1AjeThOp22FGA5Tc6rscNAZt2ryH5HBqfJ2pQxSJME+j9UOKb3Pr kGaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742852042; x=1743456842; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M7f6jbjUsppRuEOrY1E+FMO2KFZ0EMMyVLzWv4tcTkY=; b=VIyoJ8LXOHx78ItcIJGVt3pS2w9pcpwfPTQnsIz7mMlW8Z0SZHxWHNTfzVuAae50Nk Ojm2+ZWBI4rBRMTjFSchUaMITIkq2HMsIMywef5o5wF4OGUBCXPdV3btK9hVLcIJcaSC XsVict+H+MR2bhnfE8NL0DlwBdn2JLGrnRGxTW3+C4xmyzI8EmGOJZk4CqTHaCIxSw2G DE2ZRKLQKp8j7XIh0UFsBPA6e3VnzXt8THU9Z4Ug7jUmAhKOJ5ZqHLFpsr6jE8Ppn/ZE 6WraoFu3/c56wKtYvTtEhoBrarZcQZsOwxVaOl8+vog2hl39MIMOMPg4VczOJTcgQKv8 tr6w== X-Forwarded-Encrypted: i=1; AJvYcCUdhah7O4MJyE/uJ3gRsZMQCPVrWPkIZZ6y/pbYyYtf9RN1llnHB+z97zZFWOITpGTv65Y56t+WF/td@vger.kernel.org, AJvYcCWutKp5g6BiarpF4kXAh6GQ74yAvRh/I5N5NiNlJKnr06/wjjGi6V99q1RHFQrMvMLayU2pIhcSUu43/2s=@vger.kernel.org X-Gm-Message-State: AOJu0YxjCiDKkeHVg3/F4dERPJ+jjYVQOgpEZyH+Y3UtY+mf0sFcCFpW J8koOYlojT6aW8MHejU41ZUyiHp7zIJ5do2T+e1UdJqeosDHsWnL X-Gm-Gg: ASbGnct1hbwfTyq3lMrv0KE1VC98Ucljy/5u07xWrLail73Oy6xcy1tcLFJqYNXVPqV pRwsz3xB98barpUnbaVlT1aM0n62yiYCJeIOfz6wNycQUmyspxWnP1+Mi4ZqbKOlX5FlmYvRtBg 3Qlrb55kluTWaLgKt164t4uzfJKvsfwYTM/IVQll9Zf6WU/wr3/RlcXskuMqR19DiQaw34nhfid Z6dLVv5KTYge865NVPdDJUkJw3H727v9qXedqeBbtBQMsj+Z+gaBzn7So6129nG2HRil1xbTB4I r61C+1P1hff0oB1G7DjepNCzvKsrGHGSbEuWk6RY6oimsGrpYvkk3hUfjevrLVS0s2jo/1/ERI0 z8ZtXQoLOdDCFtQ7H+dYJQ4FNJehwxgzU6vvmzU1DtcE6brabtj8D0Q== X-Google-Smtp-Source: AGHT+IHBBppkA5P6kDHFKkn9EMKpLq42qkA/PD+O6pTDyDgDPYueCmlOuvUnjSi8OryUSMFnXPy2bw== X-Received: by 2002:a05:620a:31a4:b0:7c5:3ef2:8c40 with SMTP id af79cd13be357-7c5ba13371emr2057320785a.12.1742852042015; Mon, 24 Mar 2025 14:34:02 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:43c7]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c5d7eb96fesm63232185a.90.2025.03.24.14.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 14:34:01 -0700 (PDT) From: Tamir Duberstein Date: Mon, 24 Mar 2025 17:33:46 -0400 Subject: [PATCH 4/5] rust: list: use consistent self parameter name Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250324-list-no-offset-v1-4-afd2b7fc442a@gmail.com> References: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> In-Reply-To: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Refer to the self parameter of `impl_list_item!` by the same name used in `impl_has_list_links{,_self_ptr}!`. Signed-off-by: Tamir Duberstein --- rust/kernel/list/impl_list_item_mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl_list_item_mod.rs index 9d2102138c48..705b46150b97 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -114,12 +114,12 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::list::ListLinks$(<$ #[macro_export] macro_rules! impl_list_item { ( - $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { using ListLinks; })* ) => {$( // SAFETY: See GUARANTEES comment on each method. - unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t { + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $self { // GUARANTEES: // * This returns the same pointer as `prepare_to_insert` because `prepare_to_insert` // is implemented in terms of `view_links`. @@ -178,12 +178,12 @@ unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) -> *const Self { )*}; ( - $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { using ListLinksSelfPtr; })* ) => {$( // SAFETY: See GUARANTEES comment on each method. - unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t { + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $self { // GUARANTEES: // This implementation of `ListItem` will not give out exclusive access to the same // `ListLinks` several times because calls to `prepare_to_insert` and `post_remove` From patchwork Mon Mar 24 21:33:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamir Duberstein X-Patchwork-Id: 14027877 X-Patchwork-Delegate: bhelgaas@google.com Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (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 A57291EF096; Mon, 24 Mar 2025 21:34:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852046; cv=none; b=KzT8I7eWBCXleSWFb5EV0R7uJYRoInQq8RGdQZ2z3TNeHMhpzEksDdR3PrvidZcmkbMbbRyyoaJVLgFiZCLDa/XaF/3gBAFyu+Yy/X4a34iPtG7jde/KZJtRytkWFu413hdFrOJ8gJ48m2YL7soJXs5k03w5nl3u6mxNy8vpkNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742852046; c=relaxed/simple; bh=C1obtIULhiW+CO4ZwbunBr4ZlG7pPqY0H1JxizgfcUg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OZh94keSCufpciQ2V4axQN21RfgSHXcEQ5sieCw4iEInYFu2xEwWToZxjSfLp/mklmMBbvf+w4W3fRuAaBg13epUL4ZL9HmUehyQvqpUCJ/X1PfkI1+7G9b0fZwPBKU9b6wZg278sempebEiSWBmyB+S22ehW1qjSJQ9NAbpJBk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UKLY8cA2; arc=none smtp.client-ip=209.85.222.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UKLY8cA2" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-7c54c52d8easo642990785a.0; Mon, 24 Mar 2025 14:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742852043; x=1743456843; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=TC2xz/s8H2Dsg/rSNArWhY5/RoG6DRauQh0fZ8Dj8/k=; b=UKLY8cA2BqpCWlDW9U6TfNVmWfjJUNY49q3yIA51AOCYBipqiNoErRTSl3mx1kH+ny sJv+oXIHs970VzvPXRSGkvUpJPuPVxQDXafK6IqquDLxYswB1hhDVxHVsQDRSL/q0+G9 BYNPGUl/46kJ7AuSHsKBuA0fvUYMjhoZch1OsPGF/tM1JIm+YKgWpQHm5IbjeOsr294B l/18m8CUffSrKdcTsnAkw1/31i03NqLRBuZhi+vVX7GwMxOvAk4FDj3Q345eEAskS9IZ ajoemaLtoHrdPsq8MWh3rBnNRZ662P8h4oSTNaCzpMuuTE24Y9OSM4qZkI7jWKknbbzo 9JYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742852043; x=1743456843; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TC2xz/s8H2Dsg/rSNArWhY5/RoG6DRauQh0fZ8Dj8/k=; b=ZaRyuH9rnsAYfjmqrypdiTw/4VhOThytAsoQlACaeWciZiC8RdLIiIM0p2mAqH6Sn6 e7OGCSB8gTNvOxwomezShm/AsUnnIwnuYNJ4rTxaRvUjIGvJf5V7y73b4mOZArTSuvHj ppbKGfDpCo/sMNnhQRttOp8XQKmc9NuZ3aGx6FgvOH0cwx8DVDufrXF1nEOOK6d//79b Cxl5BcWJQXDrOXCrRHoHcdaNVPFjYeOGy324Ut6im0RR0Ff4Rwx8kJyHM+t3cw10bGgp d90cr+aL5NycrcNSY/GJTVVTSSbTJOZpKQS8cOoBU1ONK1P+vxW7dtOHragyBYC9rLAb cSeQ== X-Forwarded-Encrypted: i=1; AJvYcCU212EvgQsJ3oEVX3dZ28vdKkAl+0jbwArajoWJGA3DViiIATqbs+G0dcjEL9rimiGbhq431qSkUfI6@vger.kernel.org, AJvYcCXgUDpIgkv4duhdyMnk9z5319K8qts7xXTR/BzDZInA75pIIXdlffhtPsaHX3+6sP05XLLXh0NoRe4Gs1c=@vger.kernel.org X-Gm-Message-State: AOJu0YwFOu6dnnlaP1nu0i9HRA0C5mjYzdHtWWuJzHqS2bJTcrXoV/aF NDEvkG4dBJEC9BIJPNugkFBRRNX9r8Agj0TzqP55cjAej1BIzwop X-Gm-Gg: ASbGnctW2/dkg1XGSYT71yrsizzCd2Bx35cE900XyzBnCFhfEpuVuplbjMZ49DFFfqm 3gCVOgEeDCRUE0g+sb+3GDtx7IY0kfJqcOiaZbneVT/7rfT7Yzsod7ln3860kgaLqMsBcsgZpKC ubm3qXUyYI9MC2N5VEw8iSa7m8ZIwpdtzQZxv+8j1pHX5Edju7D6cB+Vk1mjj1tIRQyzxY7+NTZ JfXfXxKHHv8DFlDBaOazJkmfgOrdUM2CDgiTR/5vQCpqKC6vuzKlVcUkn3TSWD+MyGZODtvl56f WOhfMZaueNGU02ZcpkF+uEZZ1HaAjwtN2ySWeD3pm0Qs1CXOMf/3n7bW70z/G6ogaUddbnu2HKl 0hwxIXBNsOHWDce9DBAq4iXMCZzed86DnaW+0BfMwvIS5k5eqSWptXQ== X-Google-Smtp-Source: AGHT+IFJ+ZxG3avXosr8A+XuicnrVvTCI/jfhyjrVPYL/SG0M2+rugRoy6ZsmsBkluJRpCpKk0P50Q== X-Received: by 2002:a05:620a:4806:b0:7c5:b03d:4f4b with SMTP id af79cd13be357-7c5b9add52bmr2341131685a.10.1742852043080; Mon, 24 Mar 2025 14:34:03 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:43c7]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c5d7eb96fesm63232185a.90.2025.03.24.14.34.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 14:34:02 -0700 (PDT) From: Tamir Duberstein Date: Mon, 24 Mar 2025 17:33:47 -0400 Subject: [PATCH 5/5] rust: list: remove OFFSET constants Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250324-list-no-offset-v1-5-afd2b7fc442a@gmail.com> References: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> In-Reply-To: <20250324-list-no-offset-v1-0-afd2b7fc442a@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Replace `ListLinksSelfPtr::LIST_LINKS_SELF_PTR_OFFSET` with `unsafe fn raw_get_self_ptr` which returns a pointer to the field rather than requiring the caller to do pointer arithmetic. Implement `HasListLinks::raw_get_list_links` in `impl_has_list_links!`, narrowing the interface of `HasListLinks` and replacing pointer arithmetic with `container_of!`. Modify `impl_list_item` to also invoke `impl_has_list_links!` or `impl_has_list_links_self_ptr!`. This is necessary to allow `impl_list_item` to see more of the tokens used by `impl_has_list_links{,_self_ptr}!`. A similar API change was discussed on the hrtimer series[1]. Link: https://lore.kernel.org/all/20250224-hrtimer-v3-v6-12-rc2-v9-1-5bd3bf0ce6cc@kernel.org/ [1] Signed-off-by: Tamir Duberstein --- rust/kernel/list.rs | 18 +++--- rust/kernel/list/impl_list_item_mod.rs | 100 +++++++++++++++------------------ 2 files changed, 56 insertions(+), 62 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index a335c3b1ff5e..f370a8c1df98 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -212,9 +212,6 @@ unsafe impl Send for ListLinksSelfPtr {} unsafe impl Sync for ListLinksSelfPtr {} impl ListLinksSelfPtr { - /// The offset from the [`ListLinks`] to the self pointer field. - pub const LIST_LINKS_SELF_PTR_OFFSET: usize = core::mem::offset_of!(Self, self_ptr); - /// Creates a new initializer for this type. pub fn new() -> impl PinInit { // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will @@ -229,6 +226,16 @@ pub fn new() -> impl PinInit { self_ptr: Opaque::uninit(), } } + + /// Returns a pointer to the self pointer. + /// + /// # Safety + /// + /// The provided pointer must point at a valid struct of type `Self`. + pub unsafe fn raw_get_self_ptr(me: *mut Self) -> *const Opaque<*const T> { + // SAFETY: The caller promises that the pointer is valid. + unsafe { ptr::addr_of!((*me).self_ptr) } + } } impl, const ID: u64> List { @@ -603,14 +610,11 @@ fn next(&mut self) -> Option> { /// } /// } /// -/// kernel::list::impl_has_list_links! { -/// impl HasListLinks<0> for ListItem { self.links } -/// } /// kernel::list::impl_list_arc_safe! { /// impl ListArcSafe<0> for ListItem { untracked; } /// } /// kernel::list::impl_list_item! { -/// impl ListItem<0> for ListItem { using ListLinks; } +/// impl ListItem<0> for ListItem { using ListLinks { self.links }; } /// } /// /// // Use a cursor to remove the first element with the given value. diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl_list_item_mod.rs index 705b46150b97..4f9100aadbce 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -6,21 +6,18 @@ use crate::list::ListLinks; -/// Declares that this type has a `ListLinks` field at a fixed offset. +/// Declares that this type has a [`ListLinks`] field. /// -/// This trait is only used to help implement `ListItem` safely. If `ListItem` is implemented +/// This trait is only used to help implement [`ListItem`] safely. If [`ListItem`] is implemented /// manually, then this trait is not needed. Use the [`impl_has_list_links!`] macro to implement /// this trait. /// /// # Safety /// -/// All values of this type must have a `ListLinks` field at the given offset. +/// The methods on this trait must have exactly the behavior that the definitions given below have. /// -/// The behavior of `raw_get_list_links` must not be changed. +/// [`ListItem`]: crate::list::ListItem pub unsafe trait HasListLinks { - /// The offset of the `ListLinks` field. - const OFFSET: usize; - /// Returns a pointer to the [`ListLinks`] field. /// /// # Safety @@ -28,14 +25,7 @@ pub unsafe trait HasListLinks { /// The provided pointer must point at a valid struct of type `Self`. /// /// [`ListLinks`]: ListLinks - // We don't really need this method, but it's necessary for the implementation of - // `impl_has_list_links!` to be correct. - #[inline] - unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks { - // SAFETY: The caller promises that the pointer is valid. The implementer promises that the - // `OFFSET` constant is correct. - unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut ListLinks } - } + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks; } /// Implements the [`HasListLinks`] trait for the given type. @@ -48,14 +38,11 @@ macro_rules! impl_has_list_links { )*) => {$( // SAFETY: The implementation of `raw_get_list_links` only compiles if the field has the // right type. - // - // The behavior of `raw_get_list_links` is not changed since the `addr_of_mut!` macro is - // equivalent to the pointer offset operation in the trait definition. unsafe impl$(<$($generics),*>)? $crate::list::HasListLinks$(<$id>)? for $self { - const OFFSET: usize = ::core::mem::offset_of!(Self, $($field).*) as usize; - #[inline] unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::list::ListLinks$(<$id>)? { + const _: usize = ::core::mem::offset_of!($self, $($field).*); + // SAFETY: The caller promises that the pointer is not dangling. We know that this // expression doesn't follow any pointers, as the `offset_of!` invocation above // would otherwise not compile. @@ -66,12 +53,15 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::list::ListLinks$(<$ } pub use impl_has_list_links; -/// Declares that the `ListLinks` field in this struct is inside a `ListLinksSelfPtr`. +/// Declares that the [`ListLinks`] field in this struct is inside a +/// [`ListLinksSelfPtr`]. /// /// # Safety /// -/// The `ListLinks` field of this struct at the offset `HasListLinks::OFFSET` must be -/// inside a `ListLinksSelfPtr`. +/// The [`ListLinks`] field of this struct at [`HasListLinks::raw_get_list_links`] must be +/// inside a [`ListLinksSelfPtr`]. +/// +/// [`ListLinksSelfPtr`]: crate::list::ListLinksSelfPtr pub unsafe trait HasSelfPtr where Self: HasListLinks, @@ -91,8 +81,6 @@ macro_rules! impl_has_list_links_self_ptr { unsafe impl$(<$($generics)*>)? $crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {} unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)? for $self { - const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize; - #[inline] unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::list::ListLinks$(<$id>)? { // SAFETY: The caller promises that the pointer is not dangling. @@ -115,9 +103,13 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::list::ListLinks$(<$ macro_rules! impl_list_item { ( $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { - using ListLinks; + using ListLinks { self$(.$field:ident)* }; })* ) => {$( + $crate::list::impl_has_list_links! { + impl$({$($generics:tt)*})? HasListLinks<$num> for $self { self$(.$field)* } + } + // SAFETY: See GUARANTEES comment on each method. unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $self { // GUARANTEES: @@ -133,20 +125,19 @@ unsafe fn view_links(me: *const Self) -> *mut $crate::list::ListLinks<$num> { } // GUARANTEES: - // * `me` originates from the most recent call to `prepare_to_insert`, which just added - // `offset` to the pointer passed to `prepare_to_insert`. This method subtracts - // `offset` from `me` so it returns the pointer originally passed to - // `prepare_to_insert`. + // * `me` originates from the most recent call to `prepare_to_insert`, which calls + // `raw_get_list_link`, which is implemented using `addr_of_mut!((*self).$field)`. + // This method uses `container_of` to perform the inverse operation, so it returns the + // pointer originally passed to `prepare_to_insert`. // * The pointer remains valid until the next call to `post_remove` because the caller // of the most recent call to `prepare_to_insert` promised to retain ownership of the // `ListArc` containing `Self` until the next call to `post_remove`. The value cannot // be destroyed while a `ListArc` reference exists. unsafe fn view_value(me: *mut $crate::list::ListLinks<$num>) -> *const Self { - let offset = >::OFFSET; // SAFETY: `me` originates from the most recent call to `prepare_to_insert`, so it - // points at the field at offset `offset` in a value of type `Self`. Thus, - // subtracting `offset` from `me` is still in-bounds of the allocation. - unsafe { (me as *const u8).sub(offset) as *const Self } + // 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)*) } // GUARANTEES: @@ -163,25 +154,28 @@ unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate::list::ListLinks<$nu } // GUARANTEES: - // * `me` originates from the most recent call to `prepare_to_insert`, which just added - // `offset` to the pointer passed to `prepare_to_insert`. This method subtracts - // `offset` from `me` so it returns the pointer originally passed to - // `prepare_to_insert`. + // * `me` originates from the most recent call to `prepare_to_insert`, which calls + // `raw_get_list_link`, which is implemented using `addr_of_mut!((*self).$field)`. + // This method uses `container_of` to perform the inverse operation, so it returns the + // pointer originally passed to `prepare_to_insert`. unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) -> *const Self { - let offset = >::OFFSET; // SAFETY: `me` originates from the most recent call to `prepare_to_insert`, so it - // points at the field at offset `offset` in a value of type `Self`. Thus, - // subtracting `offset` from `me` is still in-bounds of the allocation. - unsafe { (me as *const u8).sub(offset) as *const Self } + // 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)*) } } )*}; ( $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $self:ty { - using ListLinksSelfPtr; + using ListLinksSelfPtr { self$(.$field:ident)* }; })* ) => {$( + $crate::list::impl_has_list_links_self_ptr! { + impl$({$($generics:tt)*})? HasListLinks<$num> for $self { self$(.$field)* } + } + // SAFETY: See GUARANTEES comment on each method. unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $self { // GUARANTEES: @@ -196,13 +190,10 @@ 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 spoff = $crate::list::ListLinksSelfPtr::::LIST_LINKS_SELF_PTR_OFFSET; - // Goes via the offset as the field is private. - // - // SAFETY: The constant is equal to `offset_of!(ListLinksSelfPtr, self_ptr)`, so - // the pointer stays in bounds of the allocation. - let self_ptr = unsafe { (links_field as *const u8).add(spoff) } - as *const $crate::types::Opaque<*const Self>; + // SAFETY: By the same reasoning above, `links_field` is a valid pointer. + let self_ptr = unsafe { + $crate::list::ListLinksSelfPtr::::raw_get_self_ptr(links_field) + }; let cell_inner = $crate::types::Opaque::raw_get(self_ptr); // SAFETY: This value is not accessed in any other places than `prepare_to_insert`, @@ -241,11 +232,10 @@ 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 spoff = $crate::list::ListLinksSelfPtr::::LIST_LINKS_SELF_PTR_OFFSET; - // SAFETY: The constant is equal to `offset_of!(ListLinksSelfPtr, self_ptr)`, so - // the pointer stays in bounds of the allocation. - let self_ptr = unsafe { (links_field as *const u8).add(spoff) } - as *const ::core::cell::UnsafeCell<*const Self>; + // SAFETY: By the same reasoning above, `links_field` is a valid pointer. + let self_ptr = unsafe { + $crate::list::ListLinksSelfPtr::::raw_get_self_ptr(links_field) + }; let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr); // SAFETY: This is not a data race, because the only function that writes to this // value is `prepare_to_insert`, but by the safety requirements the