Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22447914rwd; Fri, 30 Jun 2023 08:08:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ583PI2y8sscWLS4glMhRg5kqRLt8aM9t/6k/zeMv+7n0caFrXzz87+UH1y1fVW2MLOXZNR X-Received: by 2002:a9d:4b0e:0:b0:6b7:45de:d533 with SMTP id q14-20020a9d4b0e000000b006b745ded533mr3463777otf.9.1688137716557; Fri, 30 Jun 2023 08:08:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688137716; cv=none; d=google.com; s=arc-20160816; b=DJXG+MXWd+h4HGusIDPxvs8VG3cGZD0PMAHpSbTCKSENrftUuF3Luz0voI6oKUxWSZ GGfKlTwWeZLPmkrpkk0+hdW9PZmRi5vY5g4PVeCnWeRaGis+eWRIA655NvOThYgttMEX VD6kzSTN9gkS8HG1rizjf7Yp4nigy1CdmCcvDWqF+l3uiczR+tqMLtgv7V+BPdTGTPyg ktsJ1aqwzP46z56kpTCfCnsfIShKK8pqg+eq3xcVH1fzaplf8qV58aWAY0eX8h+TiEjy R9YBN4sSzSQgD4jTrrHHM0wkKuuaVwWNdnPo0G4jZHIW34ARqhYe4HRetFKWFDte7/AN L5kw== 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:message-id:subject:cc:from:to:dkim-signature:date; bh=GOuPFgT87znF0ebWMyLwXo4ofXzgfLkd6vzl1Y3CfqA=; fh=MjAOSe9N0TSmaKf+f9PeCP42XQuaAqCEgZCvUulmBUE=; b=yeFMD945Zb6xuiLWvvlRxdIklZIZ0Vkdub7mOmccLjcQa62AXi/p6Vb3XzYIAB70M/ lJuy4lmFbhBrRBOamBbhQU2FLQ1etx85ViWxiM3VXKhEMX+ZAjHf6X8RzlXEonOw5wpk YINLQnup1FTgPzp6eG7fBu32hkQu/oWZusuIXpg6S695tO6g1bTrr0AvQ86914TSLJ5V VFFmnne0LWVz+0KkwNGRIXu3B3hal7avbuhsLWEVg/k85ygPPYtIHgsy5gp+6luMWE0+ Va+/i9mxbsL+Ns14WA53Ye86ePbuNrl/xx0LjmI/FnUJ9VJ2iyOB1/o8fU8BrhyHznDb fwTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=NrF664B6; 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 x7-20020a636307000000b0053f0cdab81esi12816841pgb.357.2023.06.30.08.08.21; Fri, 30 Jun 2023 08:08:36 -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=NrF664B6; 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 S232892AbjF3PDm (ORCPT + 99 others); Fri, 30 Jun 2023 11:03:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232883AbjF3PDk (ORCPT ); Fri, 30 Jun 2023 11:03:40 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4618F171E for ; Fri, 30 Jun 2023 08:03:36 -0700 (PDT) Date: Fri, 30 Jun 2023 15:03:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1688137413; x=1688396613; bh=GOuPFgT87znF0ebWMyLwXo4ofXzgfLkd6vzl1Y3CfqA=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=NrF664B6Yj0S5v0K2a8SBUlj4tguRpmjCqdIi0VDecC5Af+oGs8Jow/Z7UlUxgpU6 sr/lRkn5WLpKUuwrJ28b9xhWvtVm92iXjHoqHgWwALy5WJK8wMWigXQKzq1J9YbtoY uVH5+0WMMG8zV1uDUJTmaUnY8YJ+2i+atSwnmEPX3gyffdh+XdnG+oPAq8RegM1QH+ oayFkKeYdKuzUUuBX30Zi8Cq1uFdEhbv/o0k0GnPqXGRIPHirt2o3vNMtQIAcGGof+ trOWTeMVgrocDBf+d+8C2bakHzV+avyR/N5Pe+Zj8MW4uCaACwlW5L00vXRO8abiIz EQVCjKMpULetA== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH] rust: types: make `Opaque` be `!Unpin` Message-ID: <20230630150216.109789-1-benno.lossin@proton.me> 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=-1.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,URIBL_BLOCKED autolearn=no 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 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 --- 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-alicer= yhl@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 b= y Rust code. #[repr(transparent)] -pub struct Opaque(UnsafeCell>); +pub struct Opaque { + value: UnsafeCell>, + _pin: PhantomPinned, +} =20 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, + } } =20 /// Creates an uninitialised value. pub const fn uninit() -> Self { - Self(UnsafeCell::new(MaybeUninit::uninit())) + Self { + value: UnsafeCell::new(MaybeUninit::uninit()), + _pin: PhantomPinned, + } } =20 /// 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 { =20 /// Returns a raw pointer to the opaque data. pub fn get(&self) -> *mut T { - UnsafeCell::get(&self.0).cast::() + UnsafeCell::get(&self.value).cast::() } =20 /// Gets the value behind `this`. --=20 2.41.0