From patchwork Fri Jun 30 15:03:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 114757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10441982vqr; Fri, 30 Jun 2023 08:10:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlGfefM9ujoMeVGBhrnUoo7cJUSvne3GqoIaDtawfNlotU4HrXpDexmXHBE76Q9thmTdqq/U X-Received: by 2002:a17:90a:13cd:b0:262:ee7d:2d1f with SMTP id s13-20020a17090a13cd00b00262ee7d2d1fmr2004594pjf.21.1688137855596; Fri, 30 Jun 2023 08:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688137855; cv=none; d=google.com; s=arc-20160816; b=F7nfzO+nEgB7YO/xJPE2GDNeqgrajB23aRyzvNDHoVanFi+ik8gWr/eztBKHPYB1ax +BrHczTK+iQ+xhehli35WVsNjhHQTao69QZ61J+n4fcvgYs+qaFVaysSaXyFr1Itm8Ax U1/tiS6dn01x1oXPeDhT0zBU56CT9P6+9a1/oDiri/wgzdFh4IASDqvsyDvXQxDCOAuO q6MmlGdAfIivzYB5/SYhrInf9pVX7qts7PXeFnDhTN4+0WwPkR9ePZxhOWzC201IgVaF Y97cF+dxspHi9wBsw50nfmaaopD0wF+rbXJwCdmO0ftCDuHqJ9zBLPgBzdTM0YSTSFdX nXmQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j190-20020a6380c7000000b00553c4551a08si2979138pgd.887.2023.06.30.08.10.41; Fri, 30 Jun 2023 08:10:55 -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 S232893AbjF3PDm (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770140840485918299?= X-GMAIL-MSGID: =?utf-8?q?1770140840485918299?= 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: Alice Ryhl Reviewed-by: Gary Guo 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`.