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 370E9C61D97 for ; Sat, 28 Jan 2023 11:15:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233232AbjA1LPr (ORCPT ); Sat, 28 Jan 2023 06:15:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229530AbjA1LPo (ORCPT ); Sat, 28 Jan 2023 06:15:44 -0500 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E530FF17; Sat, 28 Jan 2023 03:15:43 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id fi26so6861610edb.7; Sat, 28 Jan 2023 03:15:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=NblkQfp5UobRkkFY0Xa3j7JtUtj1KMEC5tKh7feD6wQ=; b=QaphRaF0CjZse1VY5jxor5wjrjI0Re9lrfq6VU2GhZG/G3AWNo3wvmocYU5z2CmQK0 bvAN2Uq3P8FOvHYPmuRJN0IXsjehPANLAaU0JgSlGJ3Vc21hV0MDrTv7shEGnJpkvJ3V IDIoozn7G+JpNfYh+JvFxgmujdhI2rYH0P8GdY/Yanm0W8gW2ZyKH+y04N9bcxeqH/ud TpmNYUMw/i9Gf6gLgsiPiUNFAe4VbKcQV5teKAnVy5QJLtbP9mWP84ttGAqsPjGKVeUz YYQXaeMKiXbqVsE7F1H1hG5Br81mv35ZQzfVxv2o0RojdBzXNxtaJdC39IwjZiHz2+Sa IXfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=NblkQfp5UobRkkFY0Xa3j7JtUtj1KMEC5tKh7feD6wQ=; b=tPJ1VeASvD511Xk4vragIT0lZrkStykOXltY2qyfqafa2skBE/oMSFlkd4rmZaqJy0 LvLsUWy7Mr7k5kb9uZQID7aczMZAw5k+/QzbEFgHh/YKrg6+Muwht6TO4njq21TSra4F ahBJESDDulF9gWVUIAjapjmX5+vZVx5PgaRRAEGg2qoqWE6LhiFrgRVtTaOfiK1hKVUC RhLtLFLsdSeGi7LdU7XBbUZIlDuUR5acfrqUfxg1VFtWBUqk3Htq7tdZQXm+QDj4jTRJ +NLj/rlPDytGOkuRSWGAaP6elusGeiIoKlVO5JszLc/eNYUboURyRD7emJg3LCmLfuR1 mvtg== X-Gm-Message-State: AFqh2kq8EuFtBwJ5c4HxnNyLIBN7b4X6XpBZssVviXxP1OtDXFVBUy1i /AmALrg6xDzFo4hb8kgS7m4= X-Google-Smtp-Source: AMrXdXvAbr1LdsmnFm8NQlq8Tg+exnS4E+q6qnKEgOhrOeO1rqyv1GdsFNUn+RfsLKvxOIqEjvko9A== X-Received: by 2002:a05:6402:3214:b0:49d:bc8c:c3eb with SMTP id g20-20020a056402321400b0049dbc8cc3ebmr52335717eda.15.1674904541584; Sat, 28 Jan 2023 03:15:41 -0800 (PST) Received: from localhost ([2001:b07:5d37:537d:645c:8576:916a:8c99]) by smtp.gmail.com with ESMTPSA id e22-20020a17090658d600b0085214114218sm3736327ejs.185.2023.01.28.03.15.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 28 Jan 2023 03:15:41 -0800 (PST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sat, 28 Jan 2023 12:15:39 +0100 Message-Id: Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Subject: Re: [PATCH 5/5] rust: types: implement `ForeignOwnable` for `Arc` From: "Vincenzo Palazzo" To: "Wedson Almeida Filho" , X-Mailer: aerc 0.8.2 References: <20230119174036.64046-1-wedsonaf@gmail.com> <20230119174036.64046-5-wedsonaf@gmail.com> In-Reply-To: <20230119174036.64046-5-wedsonaf@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu Jan 19, 2023 at 6:40 PM CET, Wedson Almeida Filho wrote: > 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: Vincenzo Palazzo > 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 > =20 > -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 { > } > } > =20 > +impl ForeignOwnable for Arc { > + type Borrowed<'a> =3D 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 t= hat `ptr` came from > + // a previous call to `Arc::into_foreign`. > + let inner =3D 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 saf= ety requirements of > + // `ForeignOwnable::borrow_mut` ensure that no new mutable refer= ences 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 t= hat `ptr` came from > + // a previous call to `Arc::into_foreign`, which owned guarantee= s that `ptr` is valid and > + // owns a reference. > + unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) } > + } > +} > + > impl Deref for Arc { > type Target =3D T; > =20 > --=20 > 2.34.1