Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2360162rdb; Mon, 12 Feb 2024 02:05:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUYUy3Y7kmEutcakTrNLfObb0iju9n0z7LsOM45aJTOwnBXh8pYzymNQJot+9fXgH1yzF+3pR5fu1s3tYlV+0GejeeabbxhTYVKEXJu7Q== X-Google-Smtp-Source: AGHT+IGf3OJjFFNgPvfA+sRVxgwjY+LEJCVC409LvVPXm1aLHQG5U8U5/KaGCyYvNSPLJDUogeIQ X-Received: by 2002:a05:6870:be94:b0:218:b628:186 with SMTP id nx20-20020a056870be9400b00218b6280186mr9286369oab.46.1707732344372; Mon, 12 Feb 2024 02:05:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707732344; cv=pass; d=google.com; s=arc-20160816; b=N+iiDz7LBUWZHn2f2FydWi6IZjDBSWw/1Bke23+JCc1/16QShazDB7/07UWiBBXhzD 2QlMc/LGjA8xeNz433L6mvJU/bF6hxgy8WuSWBCxuwZibXp2NKsjuK3lwpY58oL5tBIp 59PCgNIpr/FlIM1kyI86FE+Knzdb27lQGOtAWAGf0LhDAM6qG5BWsu0iMVoKFoBXjDnZ fbICD2JDhpNEixZDCMticHLrmfShkahEvVDMiBnizYHBRcq/3WSS9vIkg7l7AfjxXW5G oJO6L8r92G776UzpVMWTdnL8gGjta5I8srVc22sIw+3wv1rKThXjcl4gLzy3tBpmzY4I aCDg== 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=hj/cyHB0z1e1XClr1rbLHnWGhhAbJzVqElu75k2EOnM=; fh=UQhhnqyn2IbA3YWegAPg5meio6CYoDXVZ+ewI9OR6g0=; b=HpPmUPGFDCbWBpMHVp+dVw6G1YO+XJeYA5RznIKM6laG2zUqh2PeoCHvaIqzQxIG6h kqRVd/ydLQnLFP6YbpMb8vuJYltLaW0U9Z+qWCNmKip2nBPQtQUXIY5VtghI0DS2V2Ib v5rmxpy/r4GTQG3sD/ly0646FljAz/Hc1tdbVmErf/V+e8fb2YmHg9aU+q6ahjKGARKP OkC5bAaLJ9YbiZRsaXuAscQBQkxJoQtfGfuV8AQyGMTsBPzn0qVo8yAn4/eAT23S9QOJ JcHnlfe0+K7xZ0qQWRqf8C1839oj6f18DlEVh8N2AL9HCof9AowwbHmKU4NvgvYDLpUD aNrg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2rbKER83; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-61267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61267-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCUmeloy8xy4SZx7AR2qYmjxyX3meGdNaQeaBDAxCYldzQC6wsdvMAZyjBLs3y3YceQVkzx5h350WOanGVNR3TSU32RhH1fJuz/ciz2SDQ== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id t64-20020a638143000000b005cec620f0a5si15115pgd.487.2024.02.12.02.05.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 02:05:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2rbKER83; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-61267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61267-linux.lists.archive=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 9ED71287125 for ; Mon, 12 Feb 2024 09:57:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7468917BDF; Mon, 12 Feb 2024 09:56:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2rbKER83" Received: from mail-ua1-f44.google.com (mail-ua1-f44.google.com [209.85.222.44]) (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 36F5117BB6 for ; Mon, 12 Feb 2024 09:56:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707731816; cv=none; b=YF+iWPUc95zu9KF5OGiV2HsP+jZvd4pFTva5W0vk+CQkYhPYqmRQRtaJdAkwrSYzg+6iUF/4IObK2izechtqCKJM9JUoe8Jjr15Sf89+OUA0fruglq/dLThol4gK3Cw+/FnIFoLD6OYbEqUlh0QN0SSxrdfC3DIgaYVQ9+TbUpY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707731816; c=relaxed/simple; bh=SC/Bvc3Wfc7h52TGzU5tpw8Q+pcsuz6wa67xmHV3x0c=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=OUyAsXIiKKKC4QeaGDBT3SvnA4us4XN7XmOGZIMSkt4ku39DuAQkxXXVmgjPGhWMbIDBq4rIarIe7sgLI3daMbTRMCsL+l9/bQQquanMuXyztUjPUVdrxZCHonIOOTCfHysZ9ZQ41C1FLCcF6etg8lm4336L7tgtQAJ1rIMQHLs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2rbKER83; arc=none smtp.client-ip=209.85.222.44 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=google.com Received: by mail-ua1-f44.google.com with SMTP id a1e0cc1a2514c-7d2e1a0337bso1446493241.3 for ; Mon, 12 Feb 2024 01:56:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707731814; x=1708336614; 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=hj/cyHB0z1e1XClr1rbLHnWGhhAbJzVqElu75k2EOnM=; b=2rbKER8385ymguatyLRtYsLk5LxiBVCijck8iduZK70Yy8QYobkPTrzJa1+MDp43cG f3WsbGrMeutFtzFZxlPW33ohLCNStzrwVa1N6CCYzIzWGL04ktycPMaToARiOUywP7ov e97xmP+rTi36/XFyDgooWicteKfxfaOQECUY0MlFUePesqxGuysOKwQr24GtdLNz1XcR nkl8kVgO54nmTqR93NroHE8vEC2KgF9B0VZ3AOI9wvL+ruVSVY6q9oSOakKxM25jn40O fi73XKShS8NgskzG0aBg1bTVxj5GohR0m5tbaAzNHCNUCjvesb5+Cnp3+OQHZupT3u8G 5S4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707731814; x=1708336614; 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=hj/cyHB0z1e1XClr1rbLHnWGhhAbJzVqElu75k2EOnM=; b=pVu2V4hkghQi+T9hkwraWUj8yMfE963UZBd5tK6fGyRLYjXs0e3wMzdigaDxzfvEwE C529q57xTbSRDxElyRAvoS9fC3apG8sMjAYdk8rlH4fGCUcVBBAXauhYtq+/nncDJlbl C9j/c1IPc6bT6KtOfIo/dB9gVuc1FKoIR/jlxvCottnDPtyL/WQE5EcXjZJAP3Fs3M5s 58vJUYH69nZpPOO/fzsbOwNtSGYznV1xR69wsUTtupcLH/6R/hWybGqOPEDSXqY90Hb1 ioPDDw+U1cAw3aTajmmNArRInAK5MxkkNqx4dspT30ffCRPSt1BSVKBsQ97D0Iq4foEW nO6w== X-Forwarded-Encrypted: i=1; AJvYcCUdaAn5yjAcFn25cJG3olC257dpJW+xSYH16VQkH94HPZ1i7gciZxNlU+YMPanXhAg7940cZiDeBhB8nOiMBTTEvPDJ2kZhU5lulcrN X-Gm-Message-State: AOJu0Yxp8Hz7EpNWQIvS+HFF5ksC6r1OtwTcRT/qYhgjpoeQUgNh5uQg d4R8L+L+aaCF+zFh7YbNWOyXegw69seorZcPGjfmCU8Fl7E/k8wGg1kqffGKStlWpIJkZGLgdR/ p1QoOSqLyx8Qm5EP0FgWsOC2E4dZ5JslkLzB3 X-Received: by 2002:a05:6102:394a:b0:46d:370f:9813 with SMTP id f10-20020a056102394a00b0046d370f9813mr3841120vsu.18.1707731813990; Mon, 12 Feb 2024 01:56:53 -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: From: Alice Ryhl Date: Mon, 12 Feb 2024 10:56:43 +0100 Message-ID: Subject: Re: [PATCH 1/6] rust: add `container_of!` macro To: Trevor Gross Cc: mattgilbride@google.com, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , 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 Sat, Feb 10, 2024 at 9:05=E2=80=AFAM Trevor Gross wr= ote: > > On Mon, Feb 5, 2024 at 9:50=E2=80=AFAM wrote: > > +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 Rust Binder *does* need that in one place. Creating a nested offset_of! is kind of tricky, because you have to fail to compile when you're following a pointer with the Deref trait. I haven't figured out how to do that yet. > > + ptr.sub(offset) as *const $type > > Instead of casting to and from `u8`, you should be able to use `byte_sub` Casting to and from u8 also has the side-effect of making it not work when the target type is !Sized. Not allowing this might be desirable. Alice