Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4523037rwd; Sun, 11 Jun 2023 09:07:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5JfubdzWHBQ7ldCfvIJtoX99vXTqOld6rn2+083u4MZRv+GZxIZPu76spzqaIdSf7vkSCk X-Received: by 2002:a17:902:eccb:b0:1b0:4c6c:716 with SMTP id a11-20020a170902eccb00b001b04c6c0716mr5062917plh.4.1686499626617; Sun, 11 Jun 2023 09:07:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686499626; cv=none; d=google.com; s=arc-20160816; b=bqHMp2xotD/oyLUUj+DRn4Y+fTTPU9tHmToAh1eltEuSR00BL94lvZx3TjYm2H26s9 52tRA4QsFhNl7cTlv24ZAI0MZiGVQftTmkpnZJc+4rG/twt61e8wCAkp4fPYmpZe7ZB1 WcbIE+YzTMEXUUoWgxgSXWT4Dng1ekGv9Bg4QiunZMY/6zkFol+O52Mu4EnOqmJuTS6f clac4E3m1KKVy2O1DzPzTsYzURB1kAI8bKKm7wKDit+dxAbjQe/yfK3b8brT49b4NpIm DhCev6PAif5A9aQGM3aos+ZyZJHc1IFZrGjkYPPtMlm/LolqZH0vuhihnpjY42biDQ4h 3b8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=y9BC09FC+aLMdCShoO0QsSLuTmOQtTkJuLlGOqMbhaI=; b=GOHVR0BbcKxnCLj+S6PTByz2uDn/8eGrYIbqh41Prz1w7aVDwGooxFJvgX8IsW+1A8 c47l7gcW7GBEsSTzDPHlbuLtvA/MEXEbAc28p/9yzKUpK53yjElNUMQoJDpH1OJGSySB FlzLNHU2j0O1KG/pzXnpVHDS1ZDnBl595DHRlf1lmzDnaMvu2ST/bGQNeAkMswWx86SG vQh+ZqYAqD4+LN6B0V/Ay9s3YlZ/1NfNdOR3WW9C+OAjBpt2EHeNPaUljzqSMVgNXniY h8iGyK5R/Y2nsk/IcHAaiQ/SYuGAJ0iesCiiWRD7LNv5U0K5N8uUWaweLX0DLwq6V2S4 PJPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=juQfb6XQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x4-20020a63db44000000b00542b4a6d7dcsi5549030pgi.770.2023.06.11.09.06.54; Sun, 11 Jun 2023 09:07:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=juQfb6XQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231594AbjFKPrb (ORCPT + 99 others); Sun, 11 Jun 2023 11:47:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbjFKPra (ORCPT ); Sun, 11 Jun 2023 11:47:30 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98F5113D for ; Sun, 11 Jun 2023 08:47:25 -0700 (PDT) Date: Sun, 11 Jun 2023 15:47:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1686498443; x=1686757643; bh=y9BC09FC+aLMdCShoO0QsSLuTmOQtTkJuLlGOqMbhaI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=juQfb6XQqeksTC+XxFH/VGlfGeWh0FjHxzZ8C9m8ydT4m0YIB8N8uo+Ae90BemlBm yUU77QgH5mgfFDAll+fE252lnSNASVfnaoANrQZF3F5JcHrDOE9DftG2KhpktzAQue geCjs9yCpyM/TyslZU++7RzhriXaZhtVCHUvTZ1KOlFOGFc0lxv4z/9tbhI2Eu9t0J xP5Wnny556DuY9Rr9ovKDXSkqxoNqaxdc56JIYrsJUTXzv9ShogZ514fWvKPtVC/BM 2j45Xkwaad+9+dLOENEKRZbSPG7f0K4g92Wd5lyG+0CO2Z2B9eHlZvVcH2RdPjxol6 o/AnW21ivp7Kg== To: Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Wedson Almeida Filho , Tejun Heo , Lai Jiangshan , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Wedson Almeida Filho , Martin Rodriguez Reboredo Subject: Re: [PATCH v2 2/8] rust: add offset_of! macro Message-ID: <1tSKGQpv9YMkND4AD6xkmklwOysLXZpLWyHKRig89_8HbBhabheCU004aOkq1ixT1PpdN4SfGILlUaxwtU012ezdlZHmALZCEir0xtcifMc=@proton.me> In-Reply-To: <20230601134946.3887870-3-aliceryhl@google.com> References: <20230601134946.3887870-1-aliceryhl@google.com> <20230601134946.3887870-3-aliceryhl@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01.06.23 15:49, Alice Ryhl wrote: > From: Wedson Almeida Filho >=20 > This macro is used to compute the offset of a field in a struct. >=20 > This commit enables an unstable feature that is necessary for using > the macro in a constant. However, this is not a problem as the macro > will become available from the Rust standard library soon [1]. The > unstable feature can be disabled again once that happens. >=20 > The macro in this patch does not support sub-fields. That is, you cannot > write `offset_of!(MyStruct, field.sub_field)` to get the offset of > `sub_field` with `field`'s type being a struct with a field called > `sub_field`. This is because `field` might be a `Box`, which > means that you would be trying to compute the offset to something in an > entirely different allocation. There's no easy way to fix the current > macro to support subfields, but the version being added to the standard > library should support it, so the limitation is temporary and not a big > deal. >=20 > Link: https://github.com/rust-lang/rust/issues/106655 [1] > Signed-off-by: Wedson Almeida Filho > Co-developed-by: Alice Ryhl > Signed-off-by: Alice Ryhl > Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Benno Lossin --=20 Cheers, Benno > --- > rust/kernel/lib.rs | 35 +++++++++++++++++++++++++++++++++++ > scripts/Makefile.build | 2 +- > 2 files changed, 36 insertions(+), 1 deletion(-) >=20 > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index eaded02ffb01..7ea777b731e6 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -14,6 +14,7 @@ > #![no_std] > #![feature(allocator_api)] > #![feature(coerce_unsized)] > +#![feature(const_refs_to_cell)] > #![feature(dispatch_from_dyn)] > #![feature(new_uninit)] > #![feature(receiver_trait)] > @@ -98,3 +99,37 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { > // instead of `!`. See . > loop {} > } > + > +/// Calculates the offset of a field from the beginning of the struct it= belongs to. > +/// > +/// # Examples > +/// > +/// ``` > +/// #[repr(C)] > +/// struct Test { > +/// a: u64, > +/// b: u32, > +/// } > +/// > +/// assert_eq!(kernel::offset_of!(Test, b), 8); > +/// ``` > +#[macro_export] > +macro_rules! offset_of { > + ($type:path, $field:ident) =3D> {{ > + let $type { $field: _, .. }; > + let tmp =3D ::core::mem::MaybeUninit::<$type>::uninit(); > + let outer =3D tmp.as_ptr(); > + // To avoid warnings when nesting `unsafe` blocks. > + #[allow(unused_unsafe)] > + // SAFETY: The pointer is valid and aligned, just not initialise= d; `addr_of` ensures that > + // we don't actually read from `outer` (which would be UB) nor c= reate an intermediate > + // reference. > + let inner =3D unsafe { ::core::ptr::addr_of!((*outer).$field) } = as *const u8; > + // To avoid warnings when nesting `unsafe` blocks. > + #[allow(unused_unsafe)] > + // SAFETY: The two pointers are within the same allocation block= . > + unsafe { > + inner.offset_from(outer as *const u8) as usize > + } > + }}; > +} > diff --git a/scripts/Makefile.build b/scripts/Makefile.build > index 78175231c969..819510694769 100644 > --- a/scripts/Makefile.build > +++ b/scripts/Makefile.build > @@ -277,7 +277,7 @@ $(obj)/%.lst: $(src)/%.c FORCE > # Compile Rust sources (.rs) > # ---------------------------------------------------------------------= ------ >=20 > -rust_allowed_features :=3D new_uninit > +rust_allowed_features :=3D const_refs_to_cell,new_uninit >=20 > rust_common_cmd =3D \ > =09RUST_MODFILE=3D$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \ > -- > 2.41.0.rc0.172.g3f132b7071-goog >=20