From patchwork Mon Feb 19 11:48:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Gilbride X-Patchwork-Id: 203023 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1231142dyc; Mon, 19 Feb 2024 03:56:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVIGdDyQvR5cmuUIjSmHat4SPd9wzaTtEZEqKxg2h/+D3X14csE7uIJ1F4aq19SvFXga4v8XUlQBe8BQmy0gLG1OZvFtA== X-Google-Smtp-Source: AGHT+IFNzfQFksjySF2oAxBkoCvdxZJxrusFNa1oNdNUm3v0apivvZMYrNE3NXCac9rHwrQjkRHV X-Received: by 2002:a17:90a:df08:b0:299:7637:3dd5 with SMTP id gp8-20020a17090adf0800b0029976373dd5mr2251006pjb.36.1708343769946; Mon, 19 Feb 2024 03:56:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708343769; cv=pass; d=google.com; s=arc-20160816; b=wKXSow66Qpp3C8+KaLGep9fdGM6NbojlHDkWhm+9YXTF/ZQOHanmbT4vDVyi/rjtjW nIPYTgYIfrO6hwxg4t3vTday27a5KT5IN2kwkQJCsxO0VKeUb47pwcX+Lz58ekd4e95Q VYK+UtrFCL1xHPa0z34aCmtlAEV3MQ2uohaWAlk0krWEcTGZq1/FERG3YDip6vYAonpT jZWeX1jqXaPXljiARVJZrGWxfDVb8YjzvgWuPehBBOKdWZlB0eS1eGaXUVzIrCYKckOS PujN7nZkPVr9PRlmaGygFeQ8q0EdD9swh+UIywytt0smXXkD2+lDM7h47banJkZP1c5u PIaQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=kQYC/9pHL/SpAUfLef4ziz+f1NtXPaV0zydD9EqqSTw=; fh=Qb0oVewIb5/OQu2TNeOJkeuAz7A6pMS/MDSQWrw8gUo=; b=nMVn1wqzCC/gCBEG8cP8rSr/a0a/Wgs1pehg4+KYFtS0Fv9SRmei7lXrePJqnwDnEq cdW1q5cBFu5PE8QxLn9FV9tlcXA2Dc6KkYEO6MQrwKi+sa3AXUUMuiBGz95YLGVeuZ7V 5Fxz6LTxFxpg2XkE51oMnev9GD3zRjErJIhK+izALkn708robFFj03pakYUqAaKCC7Fe BXk79E8gL0Ktc2N+617Qj8A/otqZaDXgq6KcSnKf74CuzytWPrBfWynW3uMXlYwXq0RT KA/aq3nwL+Wzlj4SsXzF4/nVB2jZb2TMeFsF1woT+l5jLdTObfjbudvRLbKwwyf3sdd9 Lfww==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qtDFEN0+; arc=pass (i=1 spf=pass spfdomain=flex--mattgilbride.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-71241-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71241-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id oe11-20020a17090b394b00b002998c912f21si2202628pjb.14.2024.02.19.03.56.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 03:56:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-71241-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=qtDFEN0+; arc=pass (i=1 spf=pass spfdomain=flex--mattgilbride.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-71241-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71241-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 89353285582 for ; Mon, 19 Feb 2024 11:49:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A0F02E410; Mon, 19 Feb 2024 11:48:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qtDFEN0+" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 A2FF72C69E for ; Mon, 19 Feb 2024 11:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708343317; cv=none; b=L5dfRy+r/QiSFNDJLY4vxTYAfpASaj0+p0g9utJ6iYbpKaQGpqKH2KykOmxa2fYPvN1pMnIfngedVdMlbFW8arXlWQMGZwvldfp5aVbbZT1jmuG4GcOrj11TDv+hIaFAYtHEduHg5gf5o0ruGgWQUkFdjFksb+p9JAsfzm+3jBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708343317; c=relaxed/simple; bh=innSX49CwmZV7ZYVg2pc4oPmPaVu0uYAbVQ1SRQo7W0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G8TK2uOPRDcc37SaY8VfDAQKHQLwZmoM8zgsByKIJO3qTvLb1azs2XS99eIY0q5e0PgKXBsgi48DM8n8WUf1FjFZPxYMqurWu/CXwCu6Ll1/+HwcUdScYy6keGhUel6tBgKZiPEzupAFhEGslGlF3h2n8YkXzWwKzyNrpPMGJHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mattgilbride.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qtDFEN0+; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mattgilbride.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc64e0fc7c8so5875207276.2 for ; Mon, 19 Feb 2024 03:48:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708343314; x=1708948114; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kQYC/9pHL/SpAUfLef4ziz+f1NtXPaV0zydD9EqqSTw=; b=qtDFEN0+epX1qOO3GYygJ39pJdj5Ny8r6NzpJLVSlMIEHfkXz/4jPaPXOaFZ8isRE6 RUSEarDOJblEIExN0iwOxMpF0aBc2QSNiEPhGLHzhRTTfYRCCLX05dUrlcbM1OvlnEyw LVZyTftuKYD1XeKcqkoofr/ryhRHINwa84e0VVLED1T78gmu/og9XGYnP04Q9zTtAn8p Xb0Szzn6eLeeYlBkI1wVtpFZahMaZ0NcGpyp8vB7GP1XhXKp4PjGENa1uW0B3e1+T5J7 AdqXGgLS4YdOQhOs0n0FYWMSB90FZvu4/YGfYhclP34XUGDmC2JeROjcldlUBXGvziq8 StXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708343314; x=1708948114; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kQYC/9pHL/SpAUfLef4ziz+f1NtXPaV0zydD9EqqSTw=; b=GBM9gHr2/gkfjUz0P1Jh9qh7zczmkqjP7hxH9Zm01naW0ie6oBIFUSNPsGPNaoCIIa 1VgDHANEfn9eMjspRrbxgSFK9W0rIkhWPoxnR3KgeVAb2JLy2ifIza4ggxQJygWHlgQB 1psy6nB5bwOUUgbE926rDxJ0puLjrJYyfjH92nW1jKzllE0PV/AvR7ltwGyr/M6raNuT VW9+dFTZid9wQzDxF4ewx4pkG/ENSt5ehgWerLnYxR/bkOmcOv4oXDXbrx3oWyEiH2T4 sO0Zdhxd+th1GcsyGY8+y9pTDFzqzwZ4Rh6NP5ViWXkZAYl6F7d+JkTAA4orTOh8PBsb koLw== X-Forwarded-Encrypted: i=1; AJvYcCXE9ST9vo2/9beBH7396dy9g6E9EgyMhMHO3AJBpfcPdVFyEZUiPud2TN7C8apYaIhPsCmbUzzTwoGUv7VEnxeaMvui+rMmcjgjPbBh X-Gm-Message-State: AOJu0Yx5zHF4ZASX0gFfY0Fx+x9O0hiG0stg4XUbj3BcCpJffdLnyQsX YKM3vycx9URhP6yTt7WgQLyLqeCsr8x7xJHX213iX7A/BqedKAOes9K0sfs7wgh4H7O04YxQs7O zO4j9ulYx88rzihUttqKc0HHeKw== X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:1b85:b0:dc2:26f6:fbc8 with SMTP id ei5-20020a0569021b8500b00dc226f6fbc8mr479735ybb.7.1708343314722; Mon, 19 Feb 2024 03:48:34 -0800 (PST) Date: Mon, 19 Feb 2024 11:48:08 +0000 In-Reply-To: <20240219-b4-rbtree-v2-0-0b113aab330d@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240219-b4-rbtree-v2-0-0b113aab330d@google.com> X-Mailer: b4 0.12.4 Message-ID: <20240219-b4-rbtree-v2-1-0b113aab330d@google.com> Subject: [PATCH v2 1/6] rust: add `container_of!` macro From: Matt Gilbride To: 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?q?Arve_Hj=C3=B8n?= =?utf-8?q?nev=C3=A5g?= " , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Christian Brauner Cc: Rob Landley , Davidlohr Bueso , Michel Lespinasse , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matt Gilbride X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791328276571707119 X-GMAIL-MSGID: 1791328276571707119 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 Reviewed-by: Alice Ryhl Tested-by: Alice Ryhl 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. +/// +/// # Safety +/// +/// The pointer passed to this macro, and the pointer returned by this macro, must both be in +/// bounds of the same allocation. +/// +/// # Examples +/// +/// ``` +/// # use kernel::container_of; +/// struct Test { +/// a: u64, +/// b: u32, +/// } +/// +/// let test = Test { a: 10, b: 20 }; +/// let b_ptr = &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) }; +/// assert!(core::ptr::eq(&test, test_alias)); +/// ``` +#[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 + }} +}