Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1403878rdb; Sat, 10 Feb 2024 00:05:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnNEmvTSTR4BKXLNGo0OZVrBd1E9kmbAz6o79q/RY1aB6c3u7pdnstIIHa5nVSZ7lG4CBP X-Received: by 2002:a05:620a:1aa4:b0:785:ba26:10a2 with SMTP id bl36-20020a05620a1aa400b00785ba2610a2mr5910047qkb.4.1707552351674; Sat, 10 Feb 2024 00:05:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707552351; cv=pass; d=google.com; s=arc-20160816; b=Q2rFrvnDpgN4fBu8ZEcgfZSPz6pV2c3iV+21fCqopaOYAOS2DpCqyEhrkgP1Q5E+6Q KuGTgC/Tx4Af1T8Q29CUFqBtwc+r+ZaBtYOn7xaQlp+GQw6t0qSAtC3Re8M4tP3aMOC5 GNvVX22e9kjsaWoosSMPRF4LHr2haUKHBCclkcFu4a3swy6fO18rgz3ff4vFp7g8zaq2 vQFVN0cjoCFW+P7sbV58ORzR2mgKYNTBGMNkM1GLeTNx4oF8NxcD3cNFLVSzXW9ALGqS 8AqKOrczs59YsQJTadv8as3qXjIM7y/5pZBaJW9z7R/0JcX/YLGE2Bhjquc4rfOOu2Cm vS+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=ZaycG/k+YujivUfeaD+PrPikDMJQEOMrsakJhGd6CLE=; fh=x3VPcqsC+xbI0cDJiCyUn6I0TU+Stj1zrCyfBa/9e3I=; b=QBUOhQdHWL1XQm3FfoQjwM09Z8DXwIXnlGsCtea7w4Ni9WX9N112Sy8soXXJarBumo z5PC9uXWdv0eunMnZ9ZArX9c8vgpca8xVDUeWpAAMkoeZkxU1RETMdFtULXWCBUFcP2L 3yZ35BpnBSbsFB5jezqIEtQAZpytyU3V/IWpaHtuiBiwEIuoEX6YgnuDW+WwSdO7hwNC NmxgERsixb24MOG30vujutMGud6dOtcJQ2w1aDspo4cYE9dGbu0A4lOb7RAjiedL1P3g rNpu/7Llgywk2ezG8Nhm88GmXr3LbpsuVrzHCi90AJesaEcsfS/Un4mkeX4PfrCh+Bc/ HNpQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@umich.edu header.s=google-2016-06-03 header.b=A7SRCENf; arc=pass (i=1 spf=pass spfdomain=umich.edu dkim=pass dkdomain=umich.edu dmarc=pass fromdomain=umich.edu); spf=pass (google.com: domain of linux-kernel+bounces-60345-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umich.edu X-Forwarded-Encrypted: i=2; AJvYcCXjKFKGiJwR33TrpKUsjXQvo5PStVnBWNYCE7phK6IDsakZS2syxPzbj3gPBqTQG91wf4ZIxbiJHMblja9QVaeUp9Gf9CLSvc56RTMtmA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id kk7-20020a05622a2c0700b0042c59340e59si2949401qtb.598.2024.02.10.00.05.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 00:05:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60345-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@umich.edu header.s=google-2016-06-03 header.b=A7SRCENf; arc=pass (i=1 spf=pass spfdomain=umich.edu dkim=pass dkdomain=umich.edu dmarc=pass fromdomain=umich.edu); spf=pass (google.com: domain of linux-kernel+bounces-60345-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=umich.edu Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4B16A1C21BA4 for ; Sat, 10 Feb 2024 08:05:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A1342B9D0; Sat, 10 Feb 2024 08:05:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=umich.edu header.i=@umich.edu header.b="A7SRCENf" Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.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 BC5136AB7 for ; Sat, 10 Feb 2024 08:05:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707552343; cv=none; b=RACz3ekF5N0iIAAn+AMlxL4FrW5YeMbVWZpo2shS8aBCTbpVUbKd96vXZ+C0WZ7mdfrMvvSDvMJOD2wmtX/MZhgh05ortyknYmCH9reI3XIdjX2hpHbbY1XaL6NkRCGAentsx+fY53TeYAEAnB7vnYWz3lCP9oFvW7I1hXPJFv8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707552343; c=relaxed/simple; bh=2kk8zl7wmBntT6bZOZ9oR09/coMM4tEFb/OHSEp1i7Q=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=doFALNvNpAl5Q3f+KhOcDabJksG3pwOhSbAoLlBwpKihjGQLVlQffrADFgU24yL5u1l4rPb/1jzfLlFuAAIw6pechZXken/vJSnqzoejXqlAwymX0l7rubGBrQQaWGAeqH/AtGRQgPYqhx1ZAjCNjkP98Tp/gzn1irQLeThiXwc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=umich.edu; spf=pass smtp.mailfrom=umich.edu; dkim=pass (2048-bit key) header.d=umich.edu header.i=@umich.edu header.b=A7SRCENf; arc=none smtp.client-ip=209.85.128.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=umich.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=umich.edu Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-603fd31f5c2so23642847b3.0 for ; Sat, 10 Feb 2024 00:05:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=umich.edu; s=google-2016-06-03; t=1707552341; x=1708157141; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ZaycG/k+YujivUfeaD+PrPikDMJQEOMrsakJhGd6CLE=; b=A7SRCENfhkGrc2TRwgkBiKuVohyNrM5wRC+Z0RcEduqDUwl841BMJ9dFsOp8qvF76S +WQP/FyjTf3yvuGmnRKjnN9VW1BaPghA5UP5pjBCXG+jeubFdm4z0Vj1g5oT9nM7H7Lm +nvQefJh7uPrMNjSz9WUeb/SaQkLZN7ZLjfPoW0SzHI/dGDGn+ga+0h+jeaWDcyZasDy rx0NJ3/3VQ6eoEg8ZYCvb6tMqXEUc+JZCyJKjtz8ez0ihCbL9yzgnocbfV1QkWMRaN/K YcNvrSjsfWqppXn0nxwYUfpYuv4crfkIgBziiMooPDUEaaEtBotcHQV3bvQR7Eetocmf wuiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707552341; x=1708157141; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZaycG/k+YujivUfeaD+PrPikDMJQEOMrsakJhGd6CLE=; b=DSIm++6lBnQiovlv2gpgO71N/h9J7GmnBB5FQ5Xt89OTd3tfKkC30Xi0vkTFNTt6lw d/XrC1++xWnyYi5s+Ku6fyQLtCUNXXsKj2JYb6uzYzA58hQX/glQJeIMlBGNV1DgWKDx PZf3nTKqelN6M9DPoQ/FFVr2b3V+BkqhDJO6wwCHmp0kdYXH8cR6KbwiOMs9wuYsJM8h URy7Xve1guyjtM4BgtzgCEquaxQoM5Bly073eOIXIPKHiHXIjSQt9YXnZaQRMkgN/Td+ kU/9BImUWxTaj1qCNgtEqhcXCfDcdzYAL4OmmCJabdTqzQmZfDj7jecHUDgH/xGOvtqd CkEg== X-Gm-Message-State: AOJu0Yx/V1dNl7a2RVZ2RNXo5pfdxReUe0lp+75KTvY/RoEunKaHVkTd +42wPrRto+jE5ACKledY1ZMYH2rjZxD6pm03JAeAGhWzedcXCHUAJI0IkBvJqxe3f1LzU/k2yQz Dq4/7k/B/egxKFV0dOHCWEjyZfuLczK+fAJyLTw== X-Received: by 2002:a25:e0d3:0:b0:dc7:51eb:36dc with SMTP id x202-20020a25e0d3000000b00dc751eb36dcmr2566819ybg.5.1707552340707; Sat, 10 Feb 2024 00:05:40 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240205-b4-rbtree-v1-0-995e3eee38c0@google.com> <20240205-b4-rbtree-v1-1-995e3eee38c0@google.com> In-Reply-To: <20240205-b4-rbtree-v1-1-995e3eee38c0@google.com> From: Trevor Gross Date: Sat, 10 Feb 2024 02:05:29 -0600 Message-ID: Subject: Re: [PATCH 1/6] rust: add `container_of!` macro To: mattgilbride@google.com Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Greg Kroah-Hartman , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Christian Brauner , Rob Landley , Davidlohr Bueso , Michel Lespinasse , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 5, 2024 at 9:50=E2=80=AFAM wrote: > > From: Wedson Almeida Filho > > This macro is used to obtain a pointer to an entire struct > when given a pointer to a field in that struct. > > Signed-off-by: Wedson Almeida Filho > Signed-off-by: Matt Gilbride > --- > rust/kernel/lib.rs | 32 ++++++++++++++++++++++++++++++++ > 1 file changed, 32 insertions(+) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 7ac39874aeac..c7963efd1318 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -102,3 +102,35 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { > // SAFETY: FFI call. > unsafe { bindings::BUG() }; > } > + > +/// Produces a pointer to an object from a pointer to one of its fields. It is in the examples but a note would be good to make it obvious: /// /// This macro must be called from within an `unsafe { }` block. > +/// # Safety > +/// > +/// The pointer passed to this macro, and the pointer returned by this m= acro, must both be in > +/// bounds of the same allocation. > +/// > +/// # Examples > +/// > +/// ``` > +/// # use kernel::container_of; > +/// struct Test { > +/// a: u64, > +/// b: u32, > +/// } > +/// > +/// let test =3D Test { a: 10, b: 20 }; > +/// let b_ptr =3D &test.b; > +/// // SAFETY: The pointer points at the `b` field of a `Test`, so the r= esulting pointer will be > +/// // in-bounds of the same allocation as `b_ptr`. > +/// let test_alias =3D unsafe { container_of!(b_ptr, Test, b) }; > +/// assert!(core::ptr::eq(&test, test_alias)); > +/// ``` > +#[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)*); `offset_of` will be stable in 1.77 BUT only for a single field [1]. I don't know if there are other blockers in the kernel already, but if this could be changed to call `offset_of!` recursively then it will work with the stable version. We might want an `offset_of_many!(a, b, c)` macro somewhere if there are other places that need this nesting. [1]: https://github.com/rust-lang/rust/pull/118799 > + ptr.sub(offset) as *const $type Instead of casting to and from `u8`, you should be able to use `byte_sub` > + }} > +} - Trevor