From patchwork Fri Feb 24 08:50:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 61190 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp1205291dyk; Fri, 24 Feb 2023 01:02:57 -0800 (PST) X-Google-Smtp-Source: AK7set9AsmquNPpObmkaa4KYULcIgo7Lmt9QM0CvH8e+2YvJ881ea0L8Mm3D3pAuph9mCpOnW3Zn X-Received: by 2002:a05:6a00:1da3:b0:5a8:16b7:ce6e with SMTP id z35-20020a056a001da300b005a816b7ce6emr13114969pfw.10.1677229236036; Fri, 24 Feb 2023 01:00:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229236; cv=none; d=google.com; s=arc-20160816; b=eSPrz/JNLXQOojXAbets3dZsKS5NsuXq1RjrkZiBd/yKOd3SfIJcVFAAIvtOxvc2iI +TPb9eA8jxQBGyj8HBg/Z+dyGmFMduTOwCDX4ES8s4Viy40wcIwZZky5A+YpdCh3qDsQ YKY03P9zvChBqDoUpEt1HGWiKWP9vzJexluU1bpSN/9vK7xwY2AAoTz/pYqet7tYZfEe 4yldKVhGjNx8mKscVMPD/YbLgXHfttwggdsdVhlpXjbxkk+JIQa9lqM6eIrLS5msxcel WNFgn82oBB6uJ1Js7Pp6owvhk6i81Se1qZINCt7zV4pbe0+9MD0TIjrcRIDt4ECxsYsG vI7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=zSV4g6O++1o+wKdxtpYa3fo7cK2PVaTSiWDCmeKR+Tg=; b=t5OmkxJiFEBGHAK56mwwA2LEXtD5DTqSm8T7rt8Vhr5D06jbpgKLiyV5affWa/ejLt lx9RG6DYGP0oy+2HVfHYiIPGJI2oME9Zc/C4VRBi1gGq1XICENcJAJNfkJEguOny6PW9 QjSmWAbqow7Lbc4Qff1tsgRShHWnoxnSclC5zc0OIa0JCXMIgtYE77ZDWmpPgABg0lBZ 8Og7uArlYYaq4hqX4TkoaOA7Gif0M3XFGW/dBPrwJjKt9KKpUQbXh4XYuqKGO02XJbSx hRoMPyOwmNNcL/ZdQ190/QNxp//nqMbSRpiinU2Dz4OZqtgEsJIkZDDt1xkrt7IcArqg 5eJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=rxZyMibj; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x196-20020a6286cd000000b005d6a249a5cfsi6828380pfd.0.2023.02.24.01.00.22; Fri, 24 Feb 2023 01:00:36 -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=@asahilina.net header.s=default header.b=rxZyMibj; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229861AbjBXIun (ORCPT + 99 others); Fri, 24 Feb 2023 03:50:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229758AbjBXIuj (ORCPT ); Fri, 24 Feb 2023 03:50:39 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE38526AA; Fri, 24 Feb 2023 00:50:38 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 31A103FB17; Fri, 24 Feb 2023 08:50:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677228637; bh=M10zbadndwKZhD79Kr8HJcwiw4rH1LF/7cTiGWMgTk8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=rxZyMibjaZ6LZEVtlYDJAKnfIA8G8nCJ6WBhvtUFKt+oa44YUYkNytz/nRl7PIjK3 KsovDYXQ7NxK4j0FoX30fybbD/sTebMJBDIoqR9KSzLGyc8Lp+IO16y+OFsXKltDzy BobDpVsTTYVYOONz883rwyRgu1dv5Xlv+EEDpbCbfJtiTGRoTrf4l7KtudA93X4BTk Uf6hMIV46bR6OCV8WYI7OtjxwKTdWlAUUdfdYBzvRUfpssj719iFfVcqpQcdCasLRe Uq7EB3xIgb9z2DGg/xJgsHcQFIt/bd7Vkm/5htImkA6ybhaCY1hxgEvrC086rTLWo2 Q6uig72WcEk6g== From: Asahi Lina Date: Fri, 24 Feb 2023 17:50:19 +0900 Subject: [PATCH 1/5] rust: error: Add Error::to_ptr() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v1-1-f8f9a9a87303@asahilina.net> References: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> In-Reply-To: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1677228630; l=1624; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=M10zbadndwKZhD79Kr8HJcwiw4rH1LF/7cTiGWMgTk8=; b=z9aZrN4fCmRuP/ostz6C6/F/Fyl5yn8Ylw5KNL2xDbGwWpW4TGwxSiMXo66tBPTFXeAM/J8XP hFMbaVVmizHA2/pLsbqC/BVXiYzZ79/viIlk3yIwIoQl2id9f+fFNdr X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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?1758702323203022475?= X-GMAIL-MSGID: =?utf-8?q?1758702323203022475?= This is the Rust equivalent to ERR_PTR(), for use in C callbacks. Marked as #[allow(dead_code)] for now, since it does not have any consumers yet. Signed-off-by: Asahi Lina --- rust/helpers.c | 7 +++++++ rust/kernel/error.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 09a4d93f9d62..89f4cd1e0df3 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -20,6 +20,7 @@ #include #include +#include #include __noreturn void rust_helper_BUG(void) @@ -46,6 +47,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) } EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); +__force void *rust_helper_ERR_PTR(long err) +{ + return ERR_PTR(err); +} +EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); + /* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 5b9751d7ff1d..8611758e27f4 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -76,6 +76,13 @@ impl Error { pub fn to_kernel_errno(self) -> core::ffi::c_int { self.0 } + + /// Returns the error encoded as a pointer. + #[allow(dead_code)] + pub(crate) fn to_ptr(self) -> *mut T { + // SAFETY: Valid as long as self.0 is a valid error + unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ } + } } impl From for Error {