From mboxrd@z Thu Jan 1 00:00:00 1970 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 CBF9BC761A6 for ; Wed, 29 Mar 2023 12:05:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229575AbjC2MFA (ORCPT ); Wed, 29 Mar 2023 08:05:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230019AbjC2MEw (ORCPT ); Wed, 29 Mar 2023 08:04:52 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5FAF30FF; Wed, 29 Mar 2023 05:04:51 -0700 (PDT) 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 E0317424A5; Wed, 29 Mar 2023 12:04:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091490; bh=yizatryz94HI38ZhrhHNZKorJP2yMGzaneMJyUc/yLs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=PqITEoRLJt6wek92hSnvKc+Y1lmrOZsCLkof9zV7pn1ZNqZZWcdU2I15hFi/0iu5E qAl/+y7C+v5f5jtOnENPkB4CvMhF8EgkbglkUtW8YSNF5/1YDH2cZH1qx0FuegkBws kl9MQqd6kE/7jb2NLfNZLxQrkEjByz9eOQp/qNQkZO+getG5pn8J369MZyd853W+uc bVH7NfhyspZ7pd4ArAzCusASEKVEvo8N2LT5iq36AuF2YTdzQjFXscoHiR60iDTs6A LszjKUuvn0pp7JlYzdskZzmV8QbA/TbPaJ4tWxptPoI3qOR3EJoBp4cCZd4P323+ul dVl0caXyxuPLw== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:34 +0900 Subject: [PATCH v2 2/6] rust: error: Add Error::to_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-rust-error-v2-2-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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 , Martin Rodriguez Reboredo , Andreas Hindborg , 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=1680091478; l=1617; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=yizatryz94HI38ZhrhHNZKorJP2yMGzaneMJyUc/yLs=; b=g3wfjFA5bNlmO1XJYJc+pIss8D54z8fnJrLrR9I1KnVL0/HzvcuTRtMeiraME/fJKA07ACpAh Rb62kJ4bwzLDe8glRgolrXsew//PuOyb/iOnkUki7e2ldOY6UP/20pz X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: rust-for-linux@vger.kernel.org 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 35894fa35efe..e97e652a1aec 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -76,6 +76,13 @@ impl Error { pub fn to_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 { -- 2.40.0