Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1430762rwb; Thu, 19 Jan 2023 10:29:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXvP/97GccRRFZHyTOgkSFSoDTv9FAQZapXhYKY/yFHosejNuiu9375czAhj8Avr0fpBAnCR X-Received: by 2002:a50:ff12:0:b0:492:8c77:7da9 with SMTP id a18-20020a50ff12000000b004928c777da9mr10530640edu.9.1674152959044; Thu, 19 Jan 2023 10:29:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674152959; cv=none; d=google.com; s=arc-20160816; b=UVX5h3vI0XKwcTYz43IL9ik2HomgUjAWZjZCx1yg7MEwWjrnJqLnRYbghdovBHOp4q se9gDPa7XdJkL3rNiC5VKqbuucmkipT5c/IqLjz2+OkO5wGATf+mPRlsUwTKMAXGhcdt qaswOapF7urkZ1e7Lpt2AY9HmcHtLXArl65mgsNGjYmS5VzUfDdZxarcSPD8l9diWjuW mhikLO7NZzyb9+wCzWLIap87mj5mLVZ2ag8QhccmeBRQgXYtj9tED3lBiSddd+XAwaqN XwRgqoO/hJBfZyLH8N4zJm6E2d4UgHdqX63RdyJuj7U/qZzjA/05hyLyrpOminBwnXnh wcWg== 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=wGMIqOBRVBwOZ5uyAthUJactG+QhEC2sAl1iIa6Sq0Y=; b=c/9fc7hoBbTxBW3om5t4ridomD6FI/mU7ospOljPkmHv3Jq1u5X17xd0S+BTtalFI+ kvAJmf3ya8Ni7/YPk2uBSUpxej4vpoHqCHKXh6NCmUTOWjbOYABkPCQ4LHtRwbXk9LDy kWZf1L8bCgYaHprsMwwKcx5O3xOfr+bp5ljD6m7qs5VJydljXaq9i97/1JUYBBLLKBEs YikLrPrUrtECE3wXzVAiBozP+cYvCDwbLUU1FAESjEAR2LqN/QOtQ4pxT6ZgFYs6pHfv ppC6VHM9FHXT+qIrfPJxBc5SSSjQKUeeWvHAKH4UzS70z0csCO5P7XGDlHesMpBz1/Wm LygA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ri4BGOfn; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g16-20020aa7d1d0000000b00499b5e249dbsi30411427edp.60.2023.01.19.10.29.08; Thu, 19 Jan 2023 10:29:19 -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=Ri4BGOfn; 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 S230293AbjASRla (ORCPT + 45 others); Thu, 19 Jan 2023 12:41:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229593AbjASRlV (ORCPT ); Thu, 19 Jan 2023 12:41:21 -0500 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DACEC8BABC; Thu, 19 Jan 2023 09:41:05 -0800 (PST) Received: by mail-oi1-x235.google.com with SMTP id i5so2273429oih.11; Thu, 19 Jan 2023 09:41:05 -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=wGMIqOBRVBwOZ5uyAthUJactG+QhEC2sAl1iIa6Sq0Y=; b=Ri4BGOfnQOaAKbc9bsyJzNBXcozXCwdE3vwshewu1Uw/45388Y1AnLUr0W6yjWzeQy r8Ja2PwxVoRtn3538cqmTsUfue1Q3PLgQbIQe1z9tNRbdie48JAf5V5rbeqUhk405IRz K00/7flOo9EVO9c1Q38l+6BWXt1klpjeFTyBZqHVifxYHG4mpBa0Xipy41z++9UnGY7f 5JZ5gcVizyM5QJgeobaZpxlODIpA09ohumDP0eihN1QChtKrieux5kwhx3RSBRfrFxJh iA/0Goj5GdtyhqBi+ctA03dkPXFvMDUsWbkn2rfV78L6QJQK6PAhoxp7SA9YvpSLkgrQ dVDQ== 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=wGMIqOBRVBwOZ5uyAthUJactG+QhEC2sAl1iIa6Sq0Y=; b=DkSVoaOaD/n2pM0dOirrQ/io8BT/peCjUioR9fq58pAcW/K+aMrmRFjnJbVkdKUjU0 gUclPGd19ZU08Jn8baH0ghN0caAWQkv+1yf36jwnwu8PK78en4/V/BimywRYiRj2i2ON aanuLxC3Jv8aJe8IxJuZCAtTqHflroDm84NgZWyTDuIS2CPqjd29Y2i9UCzEjqdnaf5O SyFnr2qOy4TKwMs8TxRkImRtg6sJsgxXSTljpBOeljkAlVUzXo+1XRsna84UfxaywfXv PJOyDkhHmgJ8rcY0w/fsawPEtVtQSWA5o2GrmBZIi6TaEZscEEV9Ct3XM5mvkJga/7bR mnsQ== X-Gm-Message-State: AFqh2kqK0lTY/NfmMXPO2AJFXyLc5YmNufkcWCsz7X1zIFYk5/QBmcyt nSFXfa9tG2KCoU4q1ZoxFE4obdheNiaPVwfQ X-Received: by 2002:aca:1c09:0:b0:367:18f4:dbef with SMTP id c9-20020aca1c09000000b0036718f4dbefmr5472968oic.0.1674150065168; Thu, 19 Jan 2023 09:41:05 -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.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 09:41:04 -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?Bj=C3=B6rn=20Roy=20Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH 3/5] rust: types: implement `ForeignOwnable` for `Box` Date: Thu, 19 Jan 2023 14:40:34 -0300 Message-Id: <20230119174036.64046-3-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 Content-Transfer-Encoding: 8bit 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 This allows us to hand ownership of Rust dynamically allocated objects to the C side of the kernel. Signed-off-by: Wedson Almeida Filho --- rust/kernel/types.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 5475f6163002..e037c262f23e 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -63,6 +63,28 @@ pub trait ForeignOwnable { unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self; } +impl ForeignOwnable for Box { + type Borrowed<'a> = &'a T; + + fn into_foreign(self) -> *const core::ffi::c_void { + Box::into_raw(self) as _ + } + + unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T { + // SAFETY: The safety requirements for this function ensure that the object is still alive, + // so it is safe to dereference the raw pointer. + // The safety requirements of `from_foreign` also ensure that the object remains alive for + // the lifetime of the returned value. + unsafe { &*ptr.cast() } + } + + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous + // call to `Self::into_foreign`. + unsafe { Box::from_raw(ptr as _) } + } +} + /// Runs a cleanup function/closure when dropped. /// /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. -- 2.34.1