From nobody Sat Feb 7 21:15:55 2026 Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (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 E6F68228CA5; Wed, 9 Apr 2025 10:03:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744193018; cv=none; b=ujEw5RlHvgcJGtTfyeYHzlJ92FylnXhekdMVJmvpGTWCNRoSxELx9P4dXLjf7vyRcWex4lJqhCZ0MoVKepcraw186K5h+VsQ1o5qao+nZqn+x0aBdrzIoLreZQpGBuehqdS9PT66vdawlEmvYnNNHawtVZB6Ew/Erj+1boOJa5k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744193018; c=relaxed/simple; bh=YTJhL9POJYoOfWPi7cmSzRLm5vXe5zAp+v4fPIVZ7pI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Uprp/anMjSZgCoixMWsBcrCGRW4uhx47H7D/uJ8i53Z+gkGVzXpplTttM2KN+qIWlpBH03kPtWsXNvxKva+pXhawcBuwfnZn6oaTfBRD/ogcZoQqN3NLghWL+MVz0J6dZf9zvqR1AdEj94sg3d6F1hx7AsYIY66TRx3meHVQU58= 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=nmfAgq6T; arc=none smtp.client-ip=209.85.160.173 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="nmfAgq6T" Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-47677b77725so69471231cf.3; Wed, 09 Apr 2025 03:03:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744193016; x=1744797816; 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=/8z0YQi4YLnBqqRN0VZf4Fw4EtNxgeWDKD/TMdUA4iQ=; b=nmfAgq6TaOPo8cP0yggoSpnYVkh5UllOgmdCeEzOIYSGxLW9yxxBPx7OqvLDznh4VM 3bCJQVV05CwxkFRCY7wnpZ0P5HUBPFwttSGFolbHoRhWPLsoOuLN8Vot/77GeZthw/0I qXe4rX1Llx3J6fuShCA3kQT79jA+i1Sfusyt5cslFyksD2Pe4RDe7SjVRGqLBuZxyXm9 l3tFavsDQanmInIeqd62d2W03GPMzl0VBvF72lH3HJ8Jeh1Nffpjdc4/ETJhkedRrs0l bXcHOsvd7R4Ee1gniBQVTbtb5k586EXol/+7ZBm4NfQ/UYrgB/1dp5D5GAqU6goHzTxx nX9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744193016; x=1744797816; 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=/8z0YQi4YLnBqqRN0VZf4Fw4EtNxgeWDKD/TMdUA4iQ=; b=nBEk6Qg56tj4pgjHr6eD3EhPFfqjiZ3Mfmvc9cfcUxJ0wtTYCIN5t2+JD3XQ8BxojT HQlb3dOvAnAQU2+KS+OFRyRKKl4fz0o/7jv0AKd7Gt8oIq2aM0+3L9fk9nQ6/Pjwc6X5 MGP9stHLxWCWcyd6UOIB1pyfJwSDlWoa7BhTNN8WlKSr480kvQ5h9Xk/6yS0TkCwGPLc 9xAkfJ7T0FnezghA8qvsKJomT0gCE6miM9s7/yGksbcPn3I5nEzYa8DZcORpHZIQDWM/ g5ARkMdfhNm8OKQkdvNmLWrtIDeG6pD9Si6LSLEbeFz6XphWHN00tUXjZgF+eGv3E/04 hRdA== X-Forwarded-Encrypted: i=1; AJvYcCWulxEY9t1CBMHWbFvPCls7xSM1MzjmtkUUK6A7bNuEj8p2oXCMj3FgjJUZ6Ikw0Lw0qG8tn5ILCobpvAkvcuE=@vger.kernel.org, AJvYcCX1vJBZmwm/z1kj2JiOn9Lr8qaEsfNxYWAQQ00NeDSDzGu0ie8MSywt+oHTfPyOcG2HwC0NIkEhkb8X@vger.kernel.org, AJvYcCXwGvoHuoA0tDPK/CvmPoPolhd5BBySqs7cxPeSSMOTGy00Pyusl783XxQ5J/ryl4AjMTxUL3ss6GuJuWs=@vger.kernel.org X-Gm-Message-State: AOJu0YxuAixCPBqTS+9NYHflt6jPMCZMybx9G1SZ7RNll2ecsq2angz4 BrXd/NHExInQyuDMf2SmVotE5vgfXWikE7MqXIDGMb3lAofJjeIe X-Gm-Gg: ASbGncsOlsxEpUGLyMs5BKN0vRCjRsi8jg+NYkLPgjR9exfvkvXzj7kQ2Iug0wwOHPx tQEjbFy0vvp6wff3+ow+G/g2z0BNIgCE4+Vh+6UL8nPl0LeTFCcoOd43nCGW/rxJyDmmp83E1p5 h+Rv/wsD/+eP3Bv4AK5DmpqrWvI5m56oO1Qrq24La91sCIErhYI9UNjkJ0qlCp55ShqG/vYL1mX zfngw9/RLMzspzynK2I2yMF55PoKsxrPGZzcjurL2ck4u3oxFvhi5DIxWTu+mRavw4kxRu81Q3b JcgTMhPeVKO/rnHunolk46xVMcapYNsEVqvhN2inzDb9IKaRX7QwKG3IVrDyYQ== X-Google-Smtp-Source: AGHT+IGpSQMkPSuyGczqelODInn+o06rFtSM/uvcuHZLXSjNIjg/D3Vf7tgo5SrrzTuK2GH2sm3/nw== X-Received: by 2002:a05:622a:1981:b0:476:9e90:101d with SMTP id d75a77b69052e-4795f35260amr36524261cf.38.1744193015431; Wed, 09 Apr 2025 03:03:35 -0700 (PDT) Received: from [192.168.1.159] ([2600:4041:5be7:7c00:70db:4589:e2e1:f14]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-47964de11absm5149601cf.34.2025.04.09.03.03.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 03:03:34 -0700 (PDT) From: Tamir Duberstein Date: Wed, 09 Apr 2025 06:03:21 -0400 Subject: [PATCH v2 1/2] rust: retain pointer mut-ness in `container_of!` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250409-no-offset-v2-1-dda8e141a909@gmail.com> References: <20250409-no-offset-v2-0-dda8e141a909@gmail.com> In-Reply-To: <20250409-no-offset-v2-0-dda8e141a909@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 , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich Cc: Tejun Heo , Lai Jiangshan , 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 Reviewed-by: Andreas Hindborg --- rust/kernel/lib.rs | 5 ++--- rust/kernel/rbtree.rs | 23 ++++++++++------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index de07aadd1ff5..1df11156302a 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -190,7 +190,7 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { /// } /// /// let test =3D Test { a: 10, b: 20 }; -/// let b_ptr =3D &test.b; +/// let b_ptr: *const _ =3D &test.b; /// // SAFETY: The pointer points at the `b` field of a `Test`, so the res= ulting pointer will be /// // in-bounds of the same allocation as `b_ptr`. /// let test_alias =3D 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)*) =3D> {{ - let ptr =3D $ptr as *const _ as *const u8; let offset: usize =3D ::core::mem::offset_of!($type, $($f)*); - ptr.sub(offset) as *const $type + $ptr.byte_sub(offset).cast::<$type>() }} } =20 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_n= ode` pointers stored in `self` // point to the links field of `Node` objects. - let this =3D unsafe { container_of!(node, Node, links) }= .cast_mut(); + let this =3D unsafe { container_of!(node, Node, links) }; // SAFETY: `this` is a non-null node so it is valid by the typ= e invariants. let this_key =3D unsafe { &(*this).key }; // SAFETY: `node` is a non-null node so it is valid by the typ= e invariants. @@ -496,7 +496,7 @@ fn drop(&mut self) { // but it is not observable. The loop invariant is still maint= ained. =20 // 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, RBTreeNod= e) { let next =3D 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 =3D unsafe { container_of!(self.current.as_ptr(), Node, links) }.cast_mut(); + let this =3D unsafe { container_of!(self.current.as_ptr(), Node, links) }; // SAFETY: `this` is valid by the type invariants as described abo= ve. let node =3D unsafe { KBox::from_raw(this) }; let node =3D RBTreeNode { node }; @@ -806,7 +806,7 @@ fn remove_neighbor(&mut self, direction: Direction) -> = Option> unsafe { bindings::rb_erase(neighbor, addr_of_mut!(self.tree.r= oot)) }; // SAFETY: By the type invariant of `Self`, all non-null `rb_n= ode` pointers stored in `self` // point to the links field of `Node` objects. - let this =3D unsafe { container_of!(neighbor, Node, link= s) }.cast_mut(); + let this =3D unsafe { container_of!(neighbor, Node, link= s) }; // SAFETY: `this` is valid by the type invariants as described= above. let node =3D 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 =3D unsafe { container_of!(node.as_ptr(), Node, lin= ks) }.cast_mut(); + let this =3D unsafe { container_of!(node.as_ptr(), Node, lin= ks) }; // SAFETY: The passed `node` is the current node or a non-null nei= ghbor, // thus `this` is valid by the type invariants. let k =3D unsafe { &(*this).key }; @@ -1021,7 +1021,7 @@ fn next(&mut self) -> Option { =20 // SAFETY: By the type invariant of `IterRaw`, `self.next` is a va= lid node in an `RBTree`, // and by the type invariant of `RBTree`, all nodes point to the l= inks field of `Node` objects. - let cur =3D unsafe { container_of!(self.next, Node, links) }= .cast_mut(); + let cur =3D unsafe { container_of!(self.next, Node, links) }; =20 // SAFETY: `self.next` is a valid tree node by the type invariants. self.next =3D 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 } } =20 /// 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 gi= ve 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 } } =20 /// 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, N= ode, links)) }, } } =20 @@ -1272,8 +1270,7 @@ fn replace(self, node: RBTreeNode) -> RBTreeNod= e { // SAFETY: // - `self.node_ptr` produces a valid pointer to a node in the tre= e. // - Now that we removed this entry from the tree, we can convert = the node to a box. - let old_node =3D - unsafe { KBox::from_raw(container_of!(self.node_links, Node, links).cast_mut()) }; + let old_node =3D unsafe { KBox::from_raw(container_of!(self.node_l= inks, Node, links)) }; =20 RBTreeNode { node: old_node } } --=20 2.49.0 From nobody Sat Feb 7 21:15:55 2026 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.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 C2C5425D8E2; Wed, 9 Apr 2025 10:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744193020; cv=none; b=lNWe0O1S2fJ22h9TwecEMc0QaNWbKM1UvajBPRNteKDmc6DM7XHg3ctCgSleHVc3t0zW2j7UdMIpaGSFEf25wZRbS9tisr0dvImeIlUZknwglnzfec94ujB2Gwf6crz2PNNu13fZJKP3i/fg1F8HMTnYLA8MTFm9VAk+tI7wMgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744193020; c=relaxed/simple; bh=4HUFzLijaqqmDi2vGvHYj35FaaFCBBHbUp2YtC3ZOZs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ROSFfBvGDTJ7uYQML4WZm1KgJKkuI+cVyPSbwJml5D8rh1LBEKpSbY17EP2NoIEV+an9Az0vV8Ot5+jekRw/TcT7VnXlrUpjbzeVCwFMiDNuQp9THx1WZbXkCoEeiBu2qYz2Oa4NbwrlAodIKE9P0HpceqjVbscUbZdFjKXIChM= 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=hvs7gwMG; arc=none smtp.client-ip=209.85.160.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="hvs7gwMG" Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-47692b9d059so30909311cf.3; Wed, 09 Apr 2025 03:03:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744193017; x=1744797817; 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=HLbUqyimeGVgLsepfBGSq56KRRSrML5/eyV4qttU/Qc=; b=hvs7gwMGNj0PdgWsLRWVvUkqo6FlvwRlqoNV63AZVxig/tIfoTeEUB+AArODTPA0sk XUKhHgvywftlJAn0CuyuX99f6NahgMHySgeU6NLtFd3CYkb8M5mbq0HFbC8wviVNiHJH gfF7fh8h1cBOXhtF4fDOud8xCMFMqxH83bZxHBu3of/t6y1hwmJaB60M4uyD3r2tVAXl fsUHRMBG1zSSN8j3QKcUFpdExkG1OWf+DgcxhkhV40VEOzBqjWq4csC6xppypwrlwAKN AZ5S9jZfd3ZoV+wAVCpkkad/nYnTFnko9+DDLPwTZsXDqpB3nUbSsi+LDMUZ6pi/PPX4 17Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744193017; x=1744797817; 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=HLbUqyimeGVgLsepfBGSq56KRRSrML5/eyV4qttU/Qc=; b=FwcDNOZoDD3kpQJdnXoXT6cCX9q7ti34jIadca//QVFihphvVihipGTfv6fP0s3voY i1zkzHwQI2zgTPe6jliazox42iR6c1hwMzuQZexUL5Qbt95FA46T2pDyer9ZGt2aDLO8 T9bgUljmtdnODjqRu+rGckTwfqw10vqHAYtHkUyQc7CvGtcHD78Kch2IsQ5aK+mxVPGf nl6/TpTOT28F9sDSrYETCIJw3hmuUNQG6rcvq8tPovDRZsNFmBuKGOFRZ83GFZIpPyB7 /sBssxVW4Y2TaPIPv8KynmHIVP3xa9kTJost2njeF7fd9sNtYL50yxFDlf2x9EH2MwIr xzkA== X-Forwarded-Encrypted: i=1; AJvYcCU087j11aenNoS2hwjNog5N/CNAFxLLmfLeu2coBfZ26Zc0p4LmM6LsUQLnHhv9k+y52Og6Dy983MX+jce4tM8=@vger.kernel.org, AJvYcCU3U1U9QK++O1Coze9KJ2duT02f4t555P6c72s4QizCKp0B6O+0jrOY57uvJq8i3lfBu4JYmOVLEQxWAiQ=@vger.kernel.org, AJvYcCWkbA03xnWLOBxEy9yx5037axzEUuaiYG3Huzl4qRyacjyaXxK43Oax8vcEpxD2EVbr7G+jtmfJ/JD2@vger.kernel.org X-Gm-Message-State: AOJu0YwY4PP1mUaPxXiA1aZAyTmyiRxlguSlD0IIPkjpLa+H7ZFzXjbP ZMPryilpi0TPm/GgNURn2rVh5WeRzI1IscWmHJupeapFmEclvQIe X-Gm-Gg: ASbGncsCXpXwkx9q06clWh550RICqcYYZxAGQ79u4fgmiSIHxfryyosAZ8PODxeoU8c hTsnx/wUHuCtPz415eXFS83wwGfs5RToQvQiDIrhM9bzZV2Mvnk9GqSdwSm0t+4THa+AmNn/uN5 hKdh2Ni5vbZ5I71NDqys02BoBVjtjEKmlR8g5CwoRrsZag+adXyNLYOU2dRnX339QFOOPCmmUqy bLDYXAcLoRybbWcEn4zRwkOPoIbtBCOOs4alAcyoCtMNEVtjjWwWVhdx57dur9PIWZbkEWCD+EY +OzSAjlCCYxffcq3EcLxvvTTz7kjvOkVQdX5cQSKhe0NIWsH75YfPxJYptJiAA== X-Google-Smtp-Source: AGHT+IFFQbGuyHGDbxmZnvE7TtoQqlMptM6af/CxtRq0PDqB0wtWM621NnohNAdBvFkAmzbxIl56GQ== X-Received: by 2002:ac8:5dd1:0:b0:477:dcc:6c18 with SMTP id d75a77b69052e-479600a2faamr24389841cf.14.1744193017296; Wed, 09 Apr 2025 03:03:37 -0700 (PDT) Received: from [192.168.1.159] ([2600:4041:5be7:7c00:70db:4589:e2e1:f14]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-47964de11absm5149601cf.34.2025.04.09.03.03.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 03:03:36 -0700 (PDT) From: Tamir Duberstein Date: Wed, 09 Apr 2025 06:03:22 -0400 Subject: [PATCH v2 2/2] rust: workqueue: remove HasWork::OFFSET Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250409-no-offset-v2-2-dda8e141a909@gmail.com> References: <20250409-no-offset-v2-0-dda8e141a909@gmail.com> In-Reply-To: <20250409-no-offset-v2-0-dda8e141a909@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 , Bjorn Helgaas , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich Cc: Tejun Heo , Lai Jiangshan , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Implement `HasWork::work_container_of` in `impl_has_work!`, narrowing the interface of `HasWork` and replacing pointer arithmetic with `container_of!`. Remove the provided implementation of `HasWork::get_work_offset` without replacement; an implementation is already generated in `impl_has_work!`. Remove the `Self: Sized` bound on `HasWork::work_container_of` which was apparently necessary to access `OFFSET` as `OFFSET` no longer exists. 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-5bd3bf= 0ce6cc@kernel.org/ [1] Reviewed-by: Benno Lossin Reviewed-by: Alice Ryhl Tested-by: Alice Ryhl Signed-off-by: Tamir Duberstein Reviewed-by: Andreas Hindborg --- rust/kernel/workqueue.rs | 45 ++++++++++++--------------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index f98bd02b838f..1d640dbdc6ad 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -429,51 +429,23 @@ pub unsafe fn raw_get(ptr: *const Self) -> *mut bindi= ngs::work_struct { /// /// # Safety /// -/// The [`OFFSET`] constant must be the offset of a field in `Self` of typ= e [`Work`]. The -/// methods on this trait must have exactly the behavior that the definiti= ons given below have. +/// The methods on this trait must have exactly the behavior that the defi= nitions given below have. /// /// [`impl_has_work!`]: crate::impl_has_work -/// [`OFFSET`]: HasWork::OFFSET pub unsafe trait HasWork { - /// The offset of the [`Work`] field. - const OFFSET: usize; - - /// Returns the offset of the [`Work`] field. - /// - /// This method exists because the [`OFFSET`] constant cannot be acces= sed if the type is not - /// [`Sized`]. - /// - /// [`OFFSET`]: HasWork::OFFSET - #[inline] - fn get_work_offset(&self) -> usize { - Self::OFFSET - } - /// Returns a pointer to the [`Work`] field. /// /// # Safety /// /// The provided pointer must point at a valid struct of type `Self`. - #[inline] - unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work { - // SAFETY: The caller promises that the pointer is valid. - unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut Work } - } + unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work; =20 /// Returns a pointer to the struct containing the [`Work`] fie= ld. /// /// # Safety /// /// The pointer must point at a [`Work`] field in a struct of t= ype `Self`. - #[inline] - unsafe fn work_container_of(ptr: *mut Work) -> *mut Self - where - Self: Sized, - { - // SAFETY: The caller promises that the pointer points at a field = of the right type in the - // right kind of struct. - unsafe { (ptr as *mut u8).sub(Self::OFFSET) as *mut Self } - } + unsafe fn work_container_of(ptr: *mut Work) -> *mut Self; } =20 /// Used to safely implement the [`HasWork`] trait. @@ -504,8 +476,6 @@ macro_rules! impl_has_work { // SAFETY: The implementation of `raw_get_work` only compiles if t= he field has the right // type. unsafe impl$(<$($generics)+>)? $crate::workqueue::HasWork<$work_ty= pe $(, $id)?> for $self { - const OFFSET: usize =3D ::core::mem::offset_of!(Self, $field) = as usize; - #[inline] unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workque= ue::Work<$work_type $(, $id)?> { // SAFETY: The caller promises that the pointer is not dan= gling. @@ -513,6 +483,15 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate:= :workqueue::Work<$work_typ ::core::ptr::addr_of_mut!((*ptr).$field) } } + + #[inline] + unsafe fn work_container_of( + ptr: *mut $crate::workqueue::Work<$work_type $(, $id)?>, + ) -> *mut Self { + // SAFETY: The caller promises that the pointer points at = a field of the right type + // in the right kind of struct. + unsafe { $crate::container_of!(ptr, Self, $field) } + } } )*}; } --=20 2.49.0