From patchwork Wed May 3 09:07:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Hindborg X-Patchwork-Id: 13229902 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92A02C77B7F for ; Wed, 3 May 2023 09:08:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230024AbjECJIA (ORCPT ); Wed, 3 May 2023 05:08:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229694AbjECJHj (ORCPT ); Wed, 3 May 2023 05:07:39 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 162E34EE0 for ; Wed, 3 May 2023 02:07:27 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f4000ec74aso1039245e9.3 for ; Wed, 03 May 2023 02:07:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1683104845; x=1685696845; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n2aCenBwPL0i3vTawCb2L8YinJvZAslW/YJEjXhFLa8=; b=AkqTOFPVsQoS7ielM6GeSVWnFtz9yMESQff8kVfpSdZiy3bdaaWLpuG1t40bS0s7on TDgZIlo0iiLY/gG5xlQwmXkLkeWaeeMQho78fmcfwXr9vbm6f7BCJ5iI1WrR7q3Hq/B8 eriq0v/fAaOJFcSS6bHc2AuiUNCaQsbgtGUGWsqZWxzyvj4YwzKANe2QUZrEgauRlAi0 NXz0GQAAphVn7VtICawZK7usZ2YbQdSVM0vxmLmJsR+gx1YoAZPAl1wyrseet8M/HpaE 0YWtzJTugKmoNajsIAjcEDi5kVkiG0HUiPkLga9N2S03hYveh/RedYTApIvgbetEAu5S 8+zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683104845; x=1685696845; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n2aCenBwPL0i3vTawCb2L8YinJvZAslW/YJEjXhFLa8=; b=NP1+46UXF9FZuCMkRjaj10UibowFB5b9OkFnLGr8UMg98v5Xjp+olg+Ic53GbR5vWx c958W6EYRA9i1C4gPtD3KGCDN1FqAY1kuijTacOuceAgCz3+WbZ3BWtK9id7a/aRvjy9 jLh+ZgqSbxZ6XRCZGy78YnVMds4BYDEwThZymG8HcUtpCjfdi9yvz8+/0QZpssyfDrAj uNtWDNij/lr9dPUUGr/IPBQlhAz0ws5fZfjaC2ygdRugkuQaccdZTyulZhwSNKbLplkW kzTp25KtxFQpLHQ2AAx1VXwXt1NOtsUMqAS4BhewO9+Y+0v6zRaQO9YR/Gd7zwwpRfx/ oPgA== X-Gm-Message-State: AC+VfDymQ0l9fBA+alANGGe0+zwBmATZjOZ2iBYSv3ZzzWJgXC/6jY56 jZDba8w9xTLOKJKo7hsteLYC3Q== X-Google-Smtp-Source: ACHHUZ6TOXRdg3DPcncxdnXc/lCXM7ZZOYLzK+Nmqw1ltFhclc2DCjmvwo383Z/Dy4ERb34WRGJ4yg== X-Received: by 2002:a1c:7916:0:b0:3f1:952c:3c70 with SMTP id l22-20020a1c7916000000b003f1952c3c70mr13719294wme.40.1683104845321; Wed, 03 May 2023 02:07:25 -0700 (PDT) Received: from localhost ([147.161.155.99]) by smtp.gmail.com with ESMTPSA id g10-20020a7bc4ca000000b003f046ad52efsm1235624wmk.31.2023.05.03.02.07.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 02:07:25 -0700 (PDT) From: Andreas Hindborg To: Jens Axboe , Christoph Hellwig , Keith Busch , Damien Le Moal , Hannes Reinecke , lsf-pc@lists.linux-foundation.org, rust-for-linux@vger.kernel.org, linux-block@vger.kernel.org Cc: Andreas Hindborg , Matthew Wilcox , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , linux-kernel@vger.kernel.org (open list), gost.dev@samsung.com Subject: [RFC PATCH 09/11] RUST: implement `ForeignOwnable` for `Pin` Date: Wed, 3 May 2023 11:07:06 +0200 Message-Id: <20230503090708.2524310-10-nmi@metaspace.dk> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230503090708.2524310-1-nmi@metaspace.dk> References: <20230503090708.2524310-1-nmi@metaspace.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Andreas Hindborg Implement `ForeignOwnable for Pin where T: ForeignOwnable + Deref`. Imported from rust tree [1] [1] https://github.com/Rust-for-Linux/linux/tree/bc22545f38d74473cfef3e9fd65432733435b79f Cc: Wedson Almeida Filho --- rust/kernel/types.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 29db59d6119a..98e71e96a7fc 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -9,6 +9,7 @@ use core::{ marker::PhantomData, mem::MaybeUninit, ops::{Deref, DerefMut}, + pin::Pin, ptr::NonNull, }; @@ -100,6 +101,29 @@ impl ForeignOwnable for () { unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} } +impl ForeignOwnable for Pin { + type Borrowed<'a> = T::Borrowed<'a>; + + fn into_foreign(self) -> *const core::ffi::c_void { + // SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to + // the caller. + let inner = unsafe { Pin::into_inner_unchecked(self) }; + inner.into_foreign() + } + + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a> { + // SAFETY: The safety requirements for this function are the same as the ones for + // `T::borrow`. + unsafe { T::borrow(ptr) } + } + + unsafe fn from_foreign(p: *const core::ffi::c_void) -> Self { + // SAFETY: The object was originally pinned. + // The passed pointer comes from a previous call to `T::into_foreign`. + unsafe { Pin::new_unchecked(T::from_foreign(p)) } + } +} + /// Runs a cleanup function/closure when dropped. /// /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.