From patchwork Mon Jan 30 06:44:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 50157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2039762wrn; Sun, 29 Jan 2023 22:51:45 -0800 (PST) X-Google-Smtp-Source: AK7set/YQW4e8h/lH0t373AEvdV81nZ9wto5ODnvxZL1yswGpszBdaEmbgnxZ380Ql6z88pJZdo7 X-Received: by 2002:a17:906:28c4:b0:882:2027:c8e2 with SMTP id p4-20020a17090628c400b008822027c8e2mr6727090ejd.56.1675061505057; Sun, 29 Jan 2023 22:51:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675061505; cv=none; d=google.com; s=arc-20160816; b=l90VApYUNQDA3sZuAiszviyHLLdF1sFHudTizesEuk3LlCFOLcrNGsFo/mqq6eQooL a5hPTtO1HW989zPUe6xoV/6bOAKe9D6OPJTKY/dM8u8u0VZtG0S2xp9UjAZfrfltW6EW Z+JjUr8EjsH4xmCIo0mCjLqUCste6S+W5/sgWnQNWOXRYR51nuhnCtiHuf/G8mYxz2dO 9trnk/NdBd04HBTte4Itd1N4JLCKQQ17g+5/YmKoW6npdEQVrA9xdMt7Z2wADYnoecMy 7i9VVIOkbp1mtal4iKvwSbCrE+d5WRXOmgxzKXTTWpaqvYXCKCON4n4IXazi6zNCouaP MXOA== 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=qQ9qgUpJKoZ5vFNL/Ds3EfhNmCcB+s3S4YIjtxQ6XEM=; b=HwqEXqkffwx9voDyphq0QWn1UyAOFrCIi8Eo6QcYBJ9pZgaBGmi2SAeAWvCGLLnLNh dgHymVkuNujlsIqsCfVaaNkvgCbWdGKvP4qVQD86SPK6L+x48mBkFVQHKYcNCHXEsd27 4yPGJBu8hpxUHy4ATPNyAhiRNiuduwCErE0ndqHjf9O9AEb+LvmH9XzEynFOeot43Qa+ +VUapLYGXdaH6IjuheJsNX/yKMbyw3b6NryBNeDjyDrir3FjhdIGyu80lFcMqHMNMn6O ga1iYrwpIKRy/Si3liWxzNXJru8MUnjv2a2HUr6J/b96Eyx1x37Q3bk3uS4LGSjHzwoO FG8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=CYBfd2h4; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id et4-20020a170907294400b00886e4a97694si3627669ejc.863.2023.01.29.22.51.22; Sun, 29 Jan 2023 22:51:45 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=CYBfd2h4; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235557AbjA3Gqq (ORCPT + 99 others); Mon, 30 Jan 2023 01:46:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235850AbjA3Gou (ORCPT ); Mon, 30 Jan 2023 01:44:50 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E061A27D6F; Sun, 29 Jan 2023 22:44:45 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id r9so9240877oig.12; Sun, 29 Jan 2023 22:44:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=qQ9qgUpJKoZ5vFNL/Ds3EfhNmCcB+s3S4YIjtxQ6XEM=; b=CYBfd2h4juLb8ljnjOPnGGQy975kHYuXZKKiex0EiXvVdjsCLTYYd8A1e8Zlzam45F YzlFB1FKHC5fK48nD2AWqSM+4J6FVykrjrEvIpiDw0HeTkQm+m8QSf6JPDOq+sOdz2Vk TF2+6e1unJq8erVP4qARMVkJ5YJpiHIozLI3Sf1VanWf1norVjiC+i1cJQY7wTvMptOa OmMQgcQDjpoqUrZ/2mbksTBeGldJflLL1VUalb6djHZylczS7bIZs7FZm5Z8/gadrjEj lHrxNC/mLW1BbhjONgH+x/0JchUC/If7sKtjMbXMtyxJ4Q+V9yNHQZ8f/r8QuCuxjjLe Xriw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=qQ9qgUpJKoZ5vFNL/Ds3EfhNmCcB+s3S4YIjtxQ6XEM=; b=4yFbdX94cKP3Q3NiWswb2raKWpbVjYQjTYx6rXo+I+ESl1dOLmqxTO7Yz9bdb+C5H6 dxJBIoAZO6x4s4MjjYtAcG0GfPkMDbbo+XhrJX0Mfv2ckwlSJEXAub2wUDT6gj830PNg 4GjW04dqb9NTgPYPa9jqmYk5eCebhxQRNl42n5t3RNqo6RUSZp/Lt4L3ayfF+BRn8JXi 9ABxmN/ly9CBY5+WJ8+rNCF1dPPjDZUxykEZL0+ZhhrDvkMgHn3D9pGn0pcI6j2Iawty 8zSx0k0HZL6fjbuHvkMRtQuAzJAdoOhRLUbrvK0Lg+9kxYa1jVSweJ4m53YpsreyrJqT 0RIA== X-Gm-Message-State: AO0yUKXCKPuUV9tLWKlazneo4f6iU2ijhz7kok8gGXTJnMH9MAGkEcEF zhNlhWQo6Cm9mynGOca8UqOjz1DqV4KeiiwB X-Received: by 2002:a05:6808:2394:b0:378:2aa6:ddcd with SMTP id bp20-20020a056808239400b003782aa6ddcdmr2723386oib.17.1675061085231; Sun, 29 Jan 2023 22:44:45 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id o64-20020aca5a43000000b003670342726fsm4427883oib.12.2023.01.29.22.44.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 22:44:45 -0800 (PST) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Vincenzo Palazzo , Alice Ferrazzi Subject: [PATCH v2 5/5] rust: types: implement `ForeignOwnable` for `Arc` Date: Mon, 30 Jan 2023 03:44:04 -0300 Message-Id: <20230130064404.744600-5-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230130064404.744600-1-wedsonaf@gmail.com> References: <20230130064404.744600-1-wedsonaf@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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?1756429293087622593?= X-GMAIL-MSGID: =?utf-8?q?1756429293087622593?= This allows us to hand ownership of Rust ref-counted objects to the C side of the kernel. Signed-off-by: Wedson Almeida Filho Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo Reviewed-by: Alice Ferrazzi --- v1 -> v2: Unmodified rust/kernel/sync/arc.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index ff73f9240ca1..519a6ec43644 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -15,7 +15,11 @@ //! //! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html -use crate::{bindings, error::Result, types::Opaque}; +use crate::{ + bindings, + error::Result, + types::{ForeignOwnable, Opaque}, +}; use alloc::boxed::Box; use core::{ marker::{PhantomData, Unsize}, @@ -189,6 +193,32 @@ impl Arc { } } +impl ForeignOwnable for Arc { + type Borrowed<'a> = ArcBorrow<'a, T>; + + fn into_foreign(self) -> *const core::ffi::c_void { + ManuallyDrop::new(self).ptr.as_ptr() as _ + } + + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { + // SAFETY: By the safety requirement of this function, we know that `ptr` came from + // a previous call to `Arc::into_foreign`. + let inner = NonNull::new(ptr as *mut ArcInner).unwrap(); + + // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive + // for the lifetime of the returned value. Additionally, the safety requirements of + // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created. + unsafe { ArcBorrow::new(inner) } + } + + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + // SAFETY: By the safety requirement of this function, we know that `ptr` came from + // a previous call to `Arc::into_foreign`, which owned guarantees that `ptr` is valid and + // owns a reference. + unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) } + } +} + impl Deref for Arc { type Target = T;