From patchwork Mon Jan 30 06:44:02 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: 50153 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2039258wrn; Sun, 29 Jan 2023 22:49:33 -0800 (PST) X-Google-Smtp-Source: AK7set+TXjRgxmZ8+FjoPB25qnq11ONViS0AByWKtors4motzc7haFslO0bkrLoVuLWbpH2UjomG X-Received: by 2002:a05:6a21:3295:b0:bc:c663:41be with SMTP id yt21-20020a056a21329500b000bcc66341bemr6582724pzb.30.1675061373586; Sun, 29 Jan 2023 22:49:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675061373; cv=none; d=google.com; s=arc-20160816; b=TNRYsy1v10M1FnRTJXd6s9O4TmqRE71RpC6Z0XbX1DqzO3G5ea9c4mRaiVFYas7QaG DLHemi0fkPz4VfUl2/09AgoGULubUQzYKEU5gkPDNy/QYP9aGKP+FyDy01iOaNN7BIEf W5wBg74SF7+44pg9ZzQS92KEJs+Hzz6fMMYJ42tYJAOev+8O/wLWxoUZt0kMjcDo9VdQ tK5oFIrSbuw4qsd/hnODnI0U46V8c8cbR/hIznBcQWvMktIe1PXNLlpexKy3zb8xy7wB HjtyIqMkE+AL4cqe5uw6nJOExeoYhiP202CZyBWToSf1SwKcFxM4bvHrnDWvbuSZCMpf 5Wtw== 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=eWJyUEkZEQaGfIphT2kEHbYhQK8gjFMeU5IeqaqkaN8=; b=L0F9G4kIbMxFFqEBa3D1aU/DwWLnsbUQYKeyeX0jvAZ3UfFlBuUm4lx5jUQ7k7c4/o cQoZbSxB73MTCbJU/JpXWo3FPFofAPkPelzXAzNguNSSzFQR0AdbpPx6qNf8aV6kY5Nx J8M6QDDq7SCZ8Y1qid2ImxNdwz88ZkDRs9PW2lY4KFqeaXVkj+cQc+pk50qjE+Nx4WEx zNyNEFOsQWzaw7vCQ/UdnefWCPSHgSmd+crDoD4jpnk1k6s14FHkxsvmAhefadPJEW2E valFWiuTt0BRYIhw91jMcPlsNw90LViOF+TuHXGtGPrknaTtf+4aKBAWDnqSQMMQR05z 1H1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=I5vCrxtE; 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 129-20020a630087000000b004e541ab795fsi5654312pga.270.2023.01.29.22.49.21; Sun, 29 Jan 2023 22:49:33 -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=I5vCrxtE; 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 S235836AbjA3Gop (ORCPT + 99 others); Mon, 30 Jan 2023 01:44:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235820AbjA3Gok (ORCPT ); Mon, 30 Jan 2023 01:44:40 -0500 Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E11E71556F; Sun, 29 Jan 2023 22:44:36 -0800 (PST) Received: by mail-oi1-x229.google.com with SMTP id p133so9243767oig.8; Sun, 29 Jan 2023 22:44:36 -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=eWJyUEkZEQaGfIphT2kEHbYhQK8gjFMeU5IeqaqkaN8=; b=I5vCrxtE7OddIrmBOvuhwg/Vyh7yMyA0GCYe32UT8twm4O6URjG0kOMGqPjcQfRbl6 lyIjY8Hyzvcl+SigwcHOqgQ1ZW25KJg/J2VgJ4tRcU+oO/m7ce7GokbIw+64d4QaAItf AtM35hI8QevoZ4PmD9dFyb6s/VfgFKborWlJKaT4ihLADxiDmsjlA/rhVNkn6H3m/5HX gfTeB3LlOof60rNB0mQ/b0hfKxVMLXlomHU9WWCbI8XwDkNf8Sz0cSLNIaKw02KfIy1v tLBtYSlpwKeoTIQqo6dx/S754EMc/6oxHcbcWk5WEP2TFmbg734XUPyScvG4l/iXYOil vRAQ== 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=eWJyUEkZEQaGfIphT2kEHbYhQK8gjFMeU5IeqaqkaN8=; b=v6vwTMr46srDj0vZH/QBBbPwi5kEO0C+NslSTCbOxsxJuX/+0pr6QwvzOgVBDhGVpC lWzrHHu+nKi0p7Elov2zLTDP3Rjej9Eg4ETDdR+K/idRoB5XC5T+PEA4npRPsiA2FHJc 74bgpfh8RsGhsq/oira7wEV/2yL+l43cspMx5LCZVouJuyNZjWyL4MgB8KCjr/VMkdUr L1W8/vOwUh329kPvmXRuQ5mDZ2iUUb+9E3h/4rxK5p09/mqpscenK0l643tN1weYa9l8 OxpxtYmefgHwveT7OY5HrlKuL752g8SkD9bWe8WO1f9sNVlwEPBQZ3YkSj5jDHMIEruX Sr/w== X-Gm-Message-State: AO0yUKWFMwOHoyqadkfa/rPc8p6+akBBBu7uk0Is9g7XRQoU+3Jdq+oK xCumgxr8Ndsw2vsLzQ438VbJNisL8ukngi2U X-Received: by 2002:a05:6808:b25:b0:378:1ae6:86b7 with SMTP id t5-20020a0568080b2500b003781ae686b7mr2661128oij.35.1675061076234; Sun, 29 Jan 2023 22:44:36 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 22:44:36 -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 3/5] rust: types: implement `ForeignOwnable` for `Box` Date: Mon, 30 Jan 2023 03:44:02 -0300 Message-Id: <20230130064404.744600-3-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?1756429155043682333?= X-GMAIL-MSGID: =?utf-8?q?1756429155043682333?= This allows us to hand ownership of Rust dynamically allocated 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 Reviewed-by: Andreas Hindborg --- v1 -> v2: Add `use alloc::boxed::Box`, which wasn't needed before rust/kernel/types.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 72710b7442a3..411655eca3e9 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -2,6 +2,7 @@ //! Kernel types. +use alloc::boxed::Box; use core::{ cell::UnsafeCell, mem::MaybeUninit, @@ -62,6 +63,28 @@ pub trait ForeignOwnable: Sized { 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.