From patchwork Thu Jan 19 17:40:36 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: 45880 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp460709wrn; Thu, 19 Jan 2023 09:46:50 -0800 (PST) X-Google-Smtp-Source: AMrXdXtc7OS/HX8iGOEEbH5lBDXEJuCC8jG1be4bDjcEdKAjwgJi663JBlo9MOc4Vklrbc/MUoy2 X-Received: by 2002:a17:902:edd1:b0:194:706c:d05d with SMTP id q17-20020a170902edd100b00194706cd05dmr10585449plk.13.1674150410360; Thu, 19 Jan 2023 09:46:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674150410; cv=none; d=google.com; s=arc-20160816; b=0mUa1ZBO4KIF+NcCGukw7Fbl2Wuiw6inF1NsQJ3ykXxCP1Sgza5zDaROQph1McCdO2 DEaUX7H9mw6p5XlsDGz1cQ9cg0w6jmeePO/BPtHLArAoEmZG2xVcy7IWZvgKGkM+9j05 8MT+vmQpTkvMgWAiFE99HApT4TQ2lyIC9VtlBlPfEcfq4v+PrjRFanhWfn7VIef/Hzam 8DOt4ZKEGqCx54j/0Pe4s6IMCKxx47/i2KVfaJoeZDIvc852ihcoHn56J+l66lWBZIWA MVLzftOz7yVSFeJYCFcqmWnJYaLuu4UspJqyU7EQvahTiU0I+p2oEBdQUzfRU1ySQfUN wApg== 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=ewZt9xECZEPZ1KuMvtcM156VR5vfWcAYIR5vc+17fj0=; b=nmhonyEldAeQLtBxn3y5HW6KS3UpAkXt9ycHUr3UgZawZBtQ3/Hmdq18gYhzgAJ6KS zI2oKgv5DmRYnTc2QlObZG0/CyKGti3tjbV6H59fngK+WMwXVwxgoKZGxaRqNIMkiCi2 kGEVFcf52pm9k7zdZhDBChZdEEAjqRhj+YUmhm2/eHDCYy+sLOP21mTqZnBVhGb/U4/+ +Pjs9wlGfz/rYxMZAIFgmaj38zeBrMS8NXbJiESLWVumIZcIq+I0d0PZfwxUNhaWcqWa ETVS+GleAt3Rh3xNj/ONeiBdOq2cgjJID/iGCK4VLC/B132M/i8uOtxpUmViF70E/0Wv bg/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="ec4/wN56"; 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 l8-20020a170902f68800b00192ab6aa296si7432973plg.243.2023.01.19.09.46.38; Thu, 19 Jan 2023 09:46:50 -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="ec4/wN56"; 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 S230106AbjASRlg (ORCPT + 99 others); Thu, 19 Jan 2023 12:41:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230095AbjASRlX (ORCPT ); Thu, 19 Jan 2023 12:41:23 -0500 Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3692D8BAA9; Thu, 19 Jan 2023 09:41:12 -0800 (PST) Received: by mail-oi1-x22f.google.com with SMTP id p185so2313484oif.2; Thu, 19 Jan 2023 09:41:12 -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=ewZt9xECZEPZ1KuMvtcM156VR5vfWcAYIR5vc+17fj0=; b=ec4/wN56BlwB+jpQj7V9tlgsO2eAholWn2jaBPb+Aq4vRxqrhQ/euBYEL0rsNd8ILt CpnFtaM95PSAXdbDqW0OS1S/TlE4cXSsgKc/sb1H0j96NkENDO6Vy5EY5BytRdybIIFn HLMyXsWSfQjwBH8IyFtDKe2CrX2a44HSKJprlQsyyq+yDwEDiu9LuCE4sOeJz7c0UUg8 P+krDC8NSc5kGvXQoJlKWSYQG19xUhffer3/ZiK5FhOZ7VHTVzBJ9r1PXW2eVO7UEMhS AewRsNU7+sf7azdBkf7RKBIglXHBB7GeKrjJ5Fvy8u4WLV4hzintzuY/N6EqKHcTxIk5 beXQ== 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=ewZt9xECZEPZ1KuMvtcM156VR5vfWcAYIR5vc+17fj0=; b=vv3qNBywbHidakBeumB1yvk+9/oALAdHKDZivKlprn7viyWrDyhjmyv56gOD1XP13X 2XYrDUlhdpBG/ewet88fXu8Ld6AhnhkcsHogIigj93Gt6qqN5oQa4Le57kYk8yJBTTmH IZ7jb6pibHF0iFWnKtJ6p3QTwlu2SuB3ZnH8NSs+knnxhgCm1LcEYlESVEhz22R4JcBp ZhE7JZztbKeu0B0wiZFrX/N4yc4FY45MCGx4KlUVOufo/eVaxf7pyECD7ss59mXINpLM YvyVX3whrMJLKM20ZAluUcmeE7nXC2cTeJ0o5cOEDc1yVdjdh+4Kj6RHNWlgnLFEGce2 HQpA== X-Gm-Message-State: AFqh2kog79lYjL96/5Ofrb2n8sd7ivHM1BF55vaudFov4dH9kSBZotse A4T1A5XbnVJpQEyYiBeAbuUkCcRGA9d5CLqU X-Received: by 2002:aca:230f:0:b0:364:d7f0:3847 with SMTP id e15-20020aca230f000000b00364d7f03847mr4873263oie.38.1674150071475; Thu, 19 Jan 2023 09:41:11 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id i7-20020a056808054700b003631fe1810dsm10226906oig.47.2023.01.19.09.41.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 09:41:11 -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 Subject: [PATCH 5/5] rust: types: implement `ForeignOwnable` for `Arc` Date: Thu, 19 Jan 2023 14:40:36 -0300 Message-Id: <20230119174036.64046-5-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230119174036.64046-1-wedsonaf@gmail.com> References: <20230119174036.64046-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?1755473940916009647?= X-GMAIL-MSGID: =?utf-8?q?1755473940916009647?= 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 --- 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;