Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp25923803rwd; Mon, 3 Jul 2023 03:01:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlFiBSjoGTmnKul7tDi5BsWdIeCWYV8Olc9e94u6zMAmd5aDN+itujyiVHnIgb82TvZ8I2UV X-Received: by 2002:a17:902:c94d:b0:1b7:e49f:1d with SMTP id i13-20020a170902c94d00b001b7e49f001dmr8699980pla.62.1688378469231; Mon, 03 Jul 2023 03:01:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688378469; cv=none; d=google.com; s=arc-20160816; b=tyju7XR/PeLzH9E6J4eo1NZLs9xdb8WgEpEonyPAIE2XwnZSN27l7CzGNshuUkpWfn faJ1NdgJ9f7/xp37jkHM5dfe3r/hcb5SzE8iNMA6/yp1o/X35m5SDJ/YjomApzLt3y0w V154i7BG38zCZIUnr0t4Ipy4J4I4C4FByqn3tLGSMGUXcetAdPq04nc5ORHIJLRMF24Z KlGAJuMX9h1b7RG9SO58woxJS/82MM1VVsmOEkxGoFpegn8OZICtO3H85RB43Pq6jUkI kYwTvE57ZjmBF/DHlgsTm3v6dWVBvVW4tz+3RtlK43hsBPhN2fWBPZ0PZiJ7i0EiUHOp 4ENA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:dkim-signature; bh=qFhvCFqAXehqBRjZH7xGnUqT1VUtEV+4hwF7vSBD5kY=; fh=McE3SX3viB8cJr+PQl2oXCS5aEcbU7qL7JGBBE/+srU=; b=uO0GP5uQRH+IUS8h7s3ZdKElw5IXc43oOzulZSjyDsS2jPwgUR/lFifovhwZP20LZY V0G8dtmo2QwwxmYncy0ZRpmbB4SucSUlE/0CDfnGa5/gJBld9FoY33Jn+fFhF0OGW0Vc KZxxtbMs8bZsjefytK1iCrXmK9GdngiObQ1vPg6BDA9VPee4dwq1y1RoaTBibH5S4uoN L8moCCl0qvn1gj0jJibkNUSvgHELIKtxh9GTfb0GDoiv2m/5Cu7N3TIg5D1YK1TNJUIt tsUIt/2rpKji09q0in5Z99R1uB2L01Yqz/5LoiIQ/3xOBhVGoMTgTFqyabSMudvtWeJg YzqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=QDmX7US6; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t3-20020a170902bc4300b001b6c462acbdsi17085149plz.15.2023.07.03.03.00.53; Mon, 03 Jul 2023 03:01:09 -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=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=QDmX7US6; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230172AbjGCJe2 (ORCPT + 99 others); Mon, 3 Jul 2023 05:34:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230025AbjGCJe0 (ORCPT ); Mon, 3 Jul 2023 05:34:26 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F294CD for ; Mon, 3 Jul 2023 02:34:24 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-4f875b267d9so6510093e87.1 for ; Mon, 03 Jul 2023 02:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1688376862; x=1690968862; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=qFhvCFqAXehqBRjZH7xGnUqT1VUtEV+4hwF7vSBD5kY=; b=QDmX7US6YcMAgmXBL8+nRgR/dRajVeyGSzO9kr04HZ74tuBP+94DpzYzKT7Cy+wrFD gTYuFG9k/iLNK+DWnj9FZG9KjYoiLX1N3cDqkXPTZRprdTrHJgv49fR6h3anrlq5CC4t FzSuDQMPfXBaDzB4fuVNJbOT3ihCTPgerpbXsi/PaY4CWSCNthLNxt8x+mo9G6MUOYhM 9kfRyd7jtV4m8ldSeg+EhWtdvFIhOMZgzVHVnOg4bBasJOTbrRHpmMHH1Wmj8za42jsp D2/1IkSovyVjFdnco14dHYIUaFiMLzpMxuggT6DGojKZxn+nwFIadtRNG+eK/aaO7zUM keAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688376862; x=1690968862; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qFhvCFqAXehqBRjZH7xGnUqT1VUtEV+4hwF7vSBD5kY=; b=LRKq1QcWqKy/43DcUOdGB3UeFg0hx8iAbabZbgHP7aNuC847SeGsR5uOfodDRjP8G9 u3WMijjWzxHjIoEE7MRtDF8H5reIt7KRE637OB97bWQ0LPA9+Za1gM2l2CEY6fF6SPvq WmE3E968wUsfWOBkpBYKLW3EaEhfHZ+7yUhlL6xShCYpxGoNBcBcHLR36mkfui2op/JR fLhXq8WgVy4IrY4PxC4zAL9VRbSZuSqh3YbuMEBTXr5hpZvYbkmoVYHtfu64aO1Ws4kL Of74lO5uBNoIckYz4dl28HZJub87U+fOQVxxrZ+a1bKEh7RHC12T00GsXQtSCi5eofYJ VGEQ== X-Gm-Message-State: ABy/qLY+0mv8xAdnOEENSfzYbe0nxm0WZe3OU727XqzTuhU7vsk1MveE SOvZzes+roO/ZqHxeUsIeGd+6w== X-Received: by 2002:a2e:9844:0:b0:2b6:cd6a:17f7 with SMTP id e4-20020a2e9844000000b002b6cd6a17f7mr6719111ljj.20.1688376862267; Mon, 03 Jul 2023 02:34:22 -0700 (PDT) Received: from localhost ([79.142.230.34]) by smtp.gmail.com with ESMTPSA id e13-20020a50fb8d000000b0051e0be09297sm1954178edq.53.2023.07.03.02.34.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jul 2023 02:34:21 -0700 (PDT) References: <20230630150216.109789-1-benno.lossin@proton.me> User-agent: mu4e 1.10.4; emacs 28.2.50 From: "Andreas Hindborg (Samsung)" To: Benno Lossin Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH] rust: types: make `Opaque` be `!Unpin` Date: Mon, 03 Jul 2023 11:30:56 +0200 In-reply-to: <20230630150216.109789-1-benno.lossin@proton.me> Message-ID: <87mt0dcp9f.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, 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 Benno Lossin writes: > Adds a `PhantomPinned` field to `Opaque`. This removes the last Rust > guarantee: the assumption that the type `T` can be freely moved. This is > not the case for many types from the C side (e.g. if they contain a > `struct list_head`). This change removes the need to add a > `PhantomPinned` field manually to Rust structs that contain C structs > which must not be moved. > > Signed-off-by: Benno Lossin > --- Reviewed-by: Andreas Hindborg > This patch depends on the patch that swaps `UnsafeCell` with > `MaybeUninit` inside `Opaque` [1]. > > [1]: https://lore.kernel.org/rust-for-linux/20230614115328.2825961-1-aliceryhl@google.com/ > --- > rust/kernel/types.rs | 19 ++++++++++++++----- > 1 file changed, 14 insertions(+), 5 deletions(-) > > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index fb41635f1e1f..e664a2beef30 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -6,7 +6,7 @@ > use alloc::boxed::Box; > use core::{ > cell::UnsafeCell, > - marker::PhantomData, > + marker::{PhantomData, PhantomPinned}, > mem::MaybeUninit, > ops::{Deref, DerefMut}, > ptr::NonNull, > @@ -224,17 +224,26 @@ fn drop(&mut self) { > /// > /// This is meant to be used with FFI objects that are never interpreted by Rust code. > #[repr(transparent)] > -pub struct Opaque(UnsafeCell>); > +pub struct Opaque { > + value: UnsafeCell>, > + _pin: PhantomPinned, > +} > > impl Opaque { > /// Creates a new opaque value. > pub const fn new(value: T) -> Self { > - Self(UnsafeCell::new(MaybeUninit::new(value))) > + Self { > + value: UnsafeCell::new(MaybeUninit::new(value)), > + _pin: PhantomPinned, > + } > } > > /// Creates an uninitialised value. > pub const fn uninit() -> Self { > - Self(UnsafeCell::new(MaybeUninit::uninit())) > + Self { > + value: UnsafeCell::new(MaybeUninit::uninit()), > + _pin: PhantomPinned, > + } > } > > /// Creates a pin-initializer from the given initializer closure. > @@ -258,7 +267,7 @@ pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit { > > /// Returns a raw pointer to the opaque data. > pub fn get(&self) -> *mut T { > - UnsafeCell::get(&self.0).cast::() > + UnsafeCell::get(&self.value).cast::() > } > > /// Gets the value behind `this`.