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 26CCEC05027 for ; Wed, 1 Feb 2023 10:36:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231790AbjBAKf6 (ORCPT ); Wed, 1 Feb 2023 05:35:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231707AbjBAKf5 (ORCPT ); Wed, 1 Feb 2023 05:35:57 -0500 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51EF253E7E for ; Wed, 1 Feb 2023 02:35:55 -0800 (PST) Received: by mail-ej1-x630.google.com with SMTP id p26so38831258ejx.13 for ; Wed, 01 Feb 2023 02:35:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20210112.gappssmtp.com; s=20210112; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=aSHKCE9vJDcU6FD7uBVxN+faUF9zdCQJNIAv8u5C1R8=; b=DUABZjJxTjMTArmt5AilcvhMfi/ZzdMLhADxcEMvvzsfRVtsrZ+CwUPxUfEtGR3rmT 11LQ6AMy29373ZOcBZpYICqmSSw+zG5cK809rmRh3iXJjDJ64VFb9frqCR/yUuZV/s56 QSlPYP3Ou4e1J+yh0pdeYYkODo6BMwtXimdivq1sbQuRMJUPxlEucoIdE87UM49L/zke cXZJtd+hO+xlOQXPU+RQJ2qdN/V35pNa2fn4bTzjsekCtbR6IQO+vp2ZbJFcThRX0hwq nyzDrfQYYZGuWehp563eUJTKVu6aMcbvsNrYwSiFtWauYhzTTxe2kt2UidfoPQtaNENM 1x9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=aSHKCE9vJDcU6FD7uBVxN+faUF9zdCQJNIAv8u5C1R8=; b=IT8vQqKlAgi4NWqa35iNVWV+nTBr2kmNR8JVYZWJA1ynQo6r4lJABH+VrzOZOCUcqa B1Fz6z6a6G/xYLHDxgN5vJZJPOTyz7SCd5NoqmgAsvpVRSe3Q0nWpY4e/FEVdR4944XQ j+OPIghzr01nQzawau/qGaity15YHEELq3GnGAVZVT8X0gMLMq1qkjcr1SMjdEcblOsb NOXY5y+dgfJq9KzA08Sg96xXwvDg3q4+6MsUB7MDc5NYN51dy/uX/RLRA5DnfzZs/wh3 xHD4P70FY4tani/RQiy9gYEmvjaBXmepKihuNHG+y+Ytx1E6UJj3fhwp3ZyZopc7Hq3L zx3g== X-Gm-Message-State: AO0yUKV4DeoHhPitItG+2HO5FuUZxGt9Naqz+FRbg2lM2mBtTv3xZpli DiCOvN63SLjDhxv/i2Tnsx7bdA== X-Google-Smtp-Source: AK7set+fsG7o4K0HNSeLZhFcCnBgSznqi+fResUXsIDQBgxMYEkfQSbBWKyjnJA+nWSPM4KYC0GB6g== X-Received: by 2002:a17:906:5450:b0:887:31c8:9794 with SMTP id d16-20020a170906545000b0088731c89794mr1749511ejp.37.1675247753823; Wed, 01 Feb 2023 02:35:53 -0800 (PST) Received: from localhost ([79.142.230.49]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5411014edw.70.2023.02.01.02.35.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 02:35:53 -0800 (PST) References: <20230130064404.744600-1-wedsonaf@gmail.com> <20230130064404.744600-5-wedsonaf@gmail.com> User-agent: mu4e 1.9.18; emacs 28.2.50 From: Andreas Hindborg To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , linux-kernel@vger.kernel.org, Vincenzo Palazzo , Alice Ferrazzi , Andreas Hindborg Subject: Re: [PATCH v2 5/5] rust: types: implement `ForeignOwnable` for `Arc` Date: Wed, 01 Feb 2023 11:17:16 +0100 In-reply-to: <20230130064404.744600-5-wedsonaf@gmail.com> Message-ID: <87h6w5isfq.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Wedson Almeida Filho writes: > 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. The last part of the sentence does not read clearly to me. Would this make sense instead: // SAFETY: By the safety requirement of this function, we know that `ptr` came from // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and // holds a reference count increment that is transferrable to us. > + unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) } > + } > +} > + > impl Deref for Arc { > type Target = T; Cheers, Andreas