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: 89657 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1182256vqo; Wed, 3 May 2023 02:09:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6UIYMVakdZ71p4LBYT0wYf2fckjDUiRyoLMCqGSffkxJaApgyAhYpKIShgu4fjY/dv2vck X-Received: by 2002:a17:902:c941:b0:1ab:675:3e0c with SMTP id i1-20020a170902c94100b001ab06753e0cmr1769105pla.33.1683104998729; Wed, 03 May 2023 02:09:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683104998; cv=none; d=google.com; s=arc-20160816; b=BYnFrVEEMJjBAheJZQGSfmoYSIGUon/tlKm5Znuxu3J8EhpTORUPfX9enX9EUSJV4L MnVuJHUhn1tvDDWzJr0x5ex8sSNg07laC4HJJd2mfnHO/PbTswNK8b/k2wT1WmlRkA9i B9yLX1Bb614vX7+fwMoEilNwfWdUVoRvuvUMfpVF8J+l/JvGzwDzABCUWS/Lsj0YVCt1 t8pE2uC/HB0L7xT7+r+UeeomrKNAK4lCPDhIvTH/n/GCYKcqTSPMWcCCwjaln3dCWGWY +6nIhhhp9ZwO2YYJOata2c36odF7EJkTfucX610qpUtjetUDfFzAUMrxjetkyUUivyJ0 8UDA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=n2aCenBwPL0i3vTawCb2L8YinJvZAslW/YJEjXhFLa8=; b=FE2McqIiUa6AtJQtFCe7cX0zClv0zyKnYKLYWWjWFnJsPmFG5x/bk9FgSyluE1gHTA 9rMp3YpmPrqA1NzM4qEG5J9nDcjyOicqsfjBTKeyXzHKifgml6csLpxWxMJzYcUpF2Pd OqKjLVz+q6hodMk2VfvdQEzrpgUkaKSbz3pzLHvdw+dGmMlnb7qElUFnFOjXvrAabe09 i19LVxAvLvFWmX+objpqXttrcevaJBToY9MUKwEuf6GSeELNBVJjjvWUSkJKlzSmVGoA aDygfJx5sYFCGNJ1g8HWUAhmdgC0dYkfWM1zTfMvlOV/wisyAY/cuR6tvzqtTZRCE8dT cHbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=AkqTOFPV; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j8-20020a17090a588800b002473e16b67esi12911947pji.48.2023.05.03.02.09.43; Wed, 03 May 2023 02:09:58 -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=AkqTOFPV; 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 S230026AbjECJIB (ORCPT + 99 others); Wed, 3 May 2023 05:08:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbjECJHj (ORCPT ); Wed, 3 May 2023 05:07:39 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 129D44EDE for ; Wed, 3 May 2023 02:07:26 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3f178da21afso32606765e9.1 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=kfBZDrWq7HlkliKvl2FDXG3wUxJYacyF4LOG4bjyExpABgEUOzgjAIuVTvpO32xNIm 3sZUcrRH5fDoPJYQ8K0Q2KUgN1VkJpLb/9ZqFFuqMXpiLvI3EoXiP1twQdHLALpnmFdx fCCPVGGGid8dTECwRM0Az5C0GNAq27CY/wzUS8dshI6rFqacKBX7A/lG7J+B/n7JqtsY lJa/XUuQg1zjrXvle17CIsrLltSDFePz+hOj0iGRsabT6EMa/5Ecckh18h6yl8OgyuGJ C9EucUXwNSj+tIvaSRjhfOHwJgcGWDTAeOQg6/dLY6Rcw6oUyR0aDkRotzV0h600nEZ0 AOgw== X-Gm-Message-State: AC+VfDz5p7xqiqAVBvRf3cqp1JuM7TTRgb7nRX93BVfCBBg7ohDStEvO TEeFhVA+C35R+fM+xnKjzzWeww== 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 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764863507274744684?= X-GMAIL-MSGID: =?utf-8?q?1764863507274744684?= 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.