From nobody Tue Sep 9 11:58:56 2025 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 5D227C76196 for ; Mon, 3 Apr 2023 09:49:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232383AbjDCJtW (ORCPT ); Mon, 3 Apr 2023 05:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232203AbjDCJs4 (ORCPT ); Mon, 3 Apr 2023 05:48:56 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3028E4C35; Mon, 3 Apr 2023 02:48:25 -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 3F5D64212E; Mon, 3 Apr 2023 09:48:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515303; bh=rLpv9N/MuJdWjBDskv5c1QhUT6HdCop84/bSBVygqeY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=P4AuYska6w6taph8lKaPQjWDuQWv0/U++vpi25iK/tGHl8+k0ScMPJqDtHS5w6OQk Y2xSwFeE/ppJ2dUMq76+75ztihktpRfMi3K237uiyULMRjw8ExgQcJS0zufJOJhc1N 2cfoKzUAHdaYYIdx9toL8ZCHGBRIepXZOLBqNQx0lvWVShgnbxuo7g38eLCrsLX/Qd V+vCxcq/cYAcI3uHvIn5bWoonkMEPLaLwf8v1W+8zp9z5cQMFzvOLHIHyS80xGG8ji Tgt0QDpqEghlsRV8qCI4xraips5EJFpKKFxzKG8SB4+5Ndg0RTXwZROWQmZkBQJhhD oF3vJZNDPOOng== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:10 +0900 Subject: [PATCH v3 1/6] rust: error: Rename to_kernel_errno() -> to_errno() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v3-1-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@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=1680515295; l=1299; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=rLpv9N/MuJdWjBDskv5c1QhUT6HdCop84/bSBVygqeY=; b=9nrnwa52xgS4clQ3rmGPD/khl8o3VTE2q12KEAtx4dfi0zZ9q7qGssXBpVKJN5iqp/2XvrYZ9 b0oMHm+OeghAiE5VWRiit7j0+M5na4OVUQKEkLB5eUGHUddgfgIFxJd X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is kernel code, so specifying "kernel" is redundant. Let's simplify things and just call it to_errno(). Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 2 +- rust/macros/module.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 5b9751d7ff1d..35894fa35efe 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -73,7 +73,7 @@ pub struct Error(core::ffi::c_int); =20 impl Error { /// Returns the kernel error code. - pub fn to_kernel_errno(self) -> core::ffi::c_int { + pub fn to_errno(self) -> core::ffi::c_int { self.0 } } diff --git a/rust/macros/module.rs b/rust/macros/module.rs index a7e363c2b044..143336543866 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -258,7 +258,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { return 0; }} Err(e) =3D> {{ - return e.to_kernel_errno(); + return e.to_errno(); }} }} }} --=20 2.40.0 From nobody Tue Sep 9 11:58:56 2025 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 EDCF9C761A6 for ; Mon, 3 Apr 2023 09:49:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232280AbjDCJta (ORCPT ); Mon, 3 Apr 2023 05:49:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232251AbjDCJs6 (ORCPT ); Mon, 3 Apr 2023 05:48:58 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CE0B4C31; Mon, 3 Apr 2023 02:48:29 -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 365F3423BA; Mon, 3 Apr 2023 09:48:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515307; bh=RPuHJ6R232r0hFYOL7uwv1XyiKxx1ILr03e3JPbKDFw=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KQG/jE6FxwK7RjGbmyVlAV5GM0iFmOq0z0+oWzGPINY5h9xVL1AlD3exE8L32Zagv fkl60XlcK1WQcWom42C/V/xcjxye5weRSFGDCeA9ewuIVwKTt2w9oIOdSMN2n0Y7SM JT8fDP0Jtt3/4a/mKQCz/bdRTVPHEygr8ecDXKJEKC1s+c/ltUrD9vgbDJzBrTZeNr aVzTjz0z0sV4PeE2BFCv6XCKioxvQsMqHsxMu2IF5f4o1t3tkEyfyIUkxeCArtRNDw kTMohxBuHSJDr6Dr1eaG4U5x0QNXG+YS65LXrTWs5i/k8bk6334hlWTdNUmV6rt5xZ mSbSLr3Rz335w== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:11 +0900 Subject: [PATCH v3 2/6] rust: error: Add Error::to_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v3-2-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@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=1680515295; l=1683; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=RPuHJ6R232r0hFYOL7uwv1XyiKxx1ILr03e3JPbKDFw=; b=hvY3vvMJfh3KKdhSs9ptvvbGhmtEqmj3Guf8UT/y6cT43cn8ztSn1XJcxaGoNdJpUghwjyGUB EKJxa2nN8G3BNausNRjwPXuzOebAa0Gvuqe+0ePdMhXkiN5ZSGOGfrn X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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. Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina Reviewed-by: Gary Guo --- 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 @@ =20 #include #include +#include #include =20 __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); =20 +__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` ty= pe * 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..154d0ca6e2dc 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: self.0 is a valid error due to its invariant. + unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ } + } } =20 impl From for Error { --=20 2.40.0 From nobody Tue Sep 9 11:58:56 2025 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 011F2C761A6 for ; Mon, 3 Apr 2023 09:49:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231892AbjDCJtk (ORCPT ); Mon, 3 Apr 2023 05:49:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232369AbjDCJtD (ORCPT ); Mon, 3 Apr 2023 05:49:03 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6280DF76E; Mon, 3 Apr 2023 02:48:33 -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 2D02C42450; Mon, 3 Apr 2023 09:48:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515311; bh=pvm7WbUlb+NyTlE+c65R2hxzbHfwH1tHLDpoJLpargk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=G9veylE4GPhDrjRmkuHDPCfGZcFuNYsx42GtF8DDtfOK/LyjsgcnhvpZVbh9acbMa 2Xo4eOT7s+Vs0vBogkN00D+2lSNjEMFF1anQoXlOvvoyR5zIH5EYWxA+Rp4qngZXpX x4jJ9G20IJJnvcAgP/TxVcRCvdetDBiOZf/9UP/RX/yC2fvASojGGc+FjTTghvEFH0 Pu0BcoP+53hBF8jdroViphZdnvpTsS1VVilT3WwzBLiYL21V7VOmPi1Tf9ta4MaeNz Xz2jbunqBGPsZJmFJNV5SoDRPFz0GkVNRs9U4/oFyAdi09kf1nh5YNLd3hPfYCzucK EqiA5p8JkYNCg== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:12 +0900 Subject: [PATCH v3 3/6] rust: error: Add Error::from_errno{_unchecked}() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v3-3-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@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=1680515295; l=2409; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=NIH7G76x2xHvQYd680nRP0CtYqhzkgYdRdFs2Fnvuo0=; b=UZdEnqHEaSx8QsOcokSBiiKDI0ySOxyOpVj6+4YBFVzVl99Ef+T6kFqCtm2jWoeF8bCtxAHsh tu0CFblE0yQBEt0Y7SD/XJvsEMiPPAGm1dQIfH+Osbo6M9LeKZ1CeCU X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miguel Ojeda Add a function to create `Error` values out of a kernel error return, which safely upholds the invariant that the error code is well-formed (negative and greater than -MAX_ERRNO). If a malformed code is passed in, it will be converted to EINVAL. Lina: Imported from rust-for-linux/rust as authored by Miguel and Fox with refactoring from Wedson, renamed from_kernel_errno() to from_errno(). Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Miguel Ojeda Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 154d0ca6e2dc..1af0d75d3a73 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -72,6 +72,36 @@ pub mod code { pub struct Error(core::ffi::c_int); =20 impl Error { + /// Creates an [`Error`] from a kernel error code. + /// + /// It is a bug to pass an out-of-range `errno`. `EINVAL` would + /// be returned in such a case. + pub(crate) fn from_errno(errno: core::ffi::c_int) -> Error { + if errno < -(bindings::MAX_ERRNO as i32) || errno >=3D 0 { + // TODO: Make it a `WARN_ONCE` once available. + crate::pr_warn!( + "attempted to create `Error` with out of range `errno`: {}= ", + errno + ); + return code::EINVAL; + } + + // INVARIANT: The check above ensures the type invariant + // will hold. + Error(errno) + } + + /// Creates an [`Error`] from a kernel error code. + /// + /// # Safety + /// + /// `errno` must be within error code range (i.e. `>=3D -MAX_ERRNO && = < 0`). + unsafe fn from_errno_unchecked(errno: core::ffi::c_int) -> Error { + // INVARIANT: The contract ensures the type invariant + // will hold. + Error(errno) + } + /// Returns the kernel error code. pub fn to_errno(self) -> core::ffi::c_int { self.0 --=20 2.40.0 From nobody Tue Sep 9 11:58:56 2025 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 86303C76196 for ; Mon, 3 Apr 2023 09:49:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232174AbjDCJts (ORCPT ); Mon, 3 Apr 2023 05:49:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232366AbjDCJtH (ORCPT ); Mon, 3 Apr 2023 05:49:07 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B0FC12BE3; Mon, 3 Apr 2023 02:48:37 -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 24C1E42463; Mon, 3 Apr 2023 09:48:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515315; bh=1P5QGZkC91BVciAq6IwmhV1rY1KwoTA3fWIHUuZy+8g=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=yDLljO8EQYJSU7dJ+J6J9ferhSr78RHQpptYiIXjCN4njyWl+ikQicZrU+qxXvDEf vk5FQKn2DIO/Fw3UKfz/lVKKDX5arP2NJtjDUYjygOZoN/VQ2z3+0nCsCkZhQ8mKWw hhBWhrrXhKA//jjKt+OrZHhIEuVnGebVioKkHOOXASQKrIZGZxUyFJn+gFQd+NhMBP AYXtiwwAmWQ6eMu6L4HNmwbhpGMTJyK3Xg+OuIslqkXoPVBhaH0F2Zvxgri24KKx4j jOo6q75oulz4XEPDquYa74A37ibPMg95pnse8PosvEK8Beul9P+7zUCsUNcuyltsWw gKAQO2ba1cbZA== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:13 +0900 Subject: [PATCH v3 4/6] rust: error: Add to_result() helper MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v3-4-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@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=1680515295; l=1349; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=1wdN2vnmW6zZsxONU/uE2/YF42hR2YW74gn3/MQ5V4U=; b=EE3/RV3dvHT1L2FgxdAj0DSteyWP1D6o3slOa15LxiMp77atWOjnl7bza4WyQYjEugRjcHi0/ ZLnnlVRInMeBqD0zq36OfZoU1YH/4CWbRTujeVXXJeLoVlw2YekLDOH X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wedson Almeida Filho Add a to_result() helper to convert kernel C return values to a Rust Result, mapping >=3D0 values to Ok(()) and negative values to Err(...), with Error::from_errno() ensuring that the errno is within range. Lina: Imported from rust-for-linux/rust, originally developed by Wedson as part of the AMBA device driver support. Signed-off-by: Wedson Almeida Filho Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 1af0d75d3a73..e8697ad6b62e 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -178,3 +178,13 @@ impl From for Error { /// it should still be modeled as returning a `Result` rather than /// just an [`Error`]. pub type Result =3D core::result::Result; + +/// Converts an integer as returned by a C kernel function to an error if = it's negative, and +/// `Ok(())` otherwise. +pub fn to_result(err: core::ffi::c_int) -> Result { + if err < 0 { + Err(Error::from_errno(err)) + } else { + Ok(()) + } +} --=20 2.40.0 From nobody Tue Sep 9 11:58:56 2025 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 7AC47C761A6 for ; Mon, 3 Apr 2023 09:50:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231904AbjDCJuD (ORCPT ); Mon, 3 Apr 2023 05:50:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232391AbjDCJta (ORCPT ); Mon, 3 Apr 2023 05:49:30 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A13012BFF; Mon, 3 Apr 2023 02:48:44 -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 1D0B941E64; Mon, 3 Apr 2023 09:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515319; bh=FFczrsn4BHG2jGRMZ9qqibn2umHy3oP8Nzgbsa+sfGQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=W5xf2sVK+gyRZ9offnkTD/0SpWZxA5EunI4QAXKhn1dXM/Q7aRhUxCFzR2EaJCqxG IVGOR7TxkLrmH1QxsVJzZgGlTAdkL3wIaXF6P0eFjkyMMYKcu6NO3EYoBMlqnSBPvF 23N3SQkWxWLeh9S4DF3jiZmgPToZke2PTd8ImbVisGEYCuVHbkjVRueDW8wwcijZ41 vrCz3+HLwjyS1TQuu1l87I2y0YBsZeJeH4Dr9EqRyopO5XoBPoDe0HFBgDAOIMazO3 vh2YfqtyH0+G6hJBDKpxAaLIgFtMNmDmRezcDpOUb47v2XE+RtcE7/BjBa6szHNMQw zOWXkcjq/UjIA== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:14 +0900 Subject: [PATCH v3 5/6] rust: error: Add a helper to convert a C ERR_PTR to a `Result` MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v3-5-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@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=1680515295; l=4192; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=ISUvnAT4TyiogMaTmuiS16KnnBXAUwEeRJutcySkWLo=; b=IELaoMg8SSQorXRnN2gvxejtyyJxGzy0urJANcFWVMLquvf5coqJVqSHlHd8rPd+7VCCKIaGZ K8jx70ApDMRBPAvtOeaih1oh6vfnPfEOrxIIt9D2UQErxcmbWNPEUSu X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sven Van Asbroeck Some kernel C API functions return a pointer which embeds an optional `errno`. Callers are supposed to check the returned pointer with `IS_ERR()` and if this returns `true`, retrieve the `errno` using `PTR_ERR()`. Create a Rust helper function to implement the Rust equivalent: transform a `*mut T` to `Result<*mut T>`. Lina: Imported from rust-for-linux/linux, with subsequent refactoring and contributions squashed in and attributed below. Renamed the function to from_err_ptr(). Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Gary Guo Signed-off-by: Gary Guo Signed-off-by: Sven Van Asbroeck Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/helpers.c | 12 ++++++++++++ rust/kernel/error.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 89f4cd1e0df3..04b9be46e887 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -53,6 +53,18 @@ __force void *rust_helper_ERR_PTR(long err) } EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); =20 +bool rust_helper_IS_ERR(__force const void *ptr) +{ + return IS_ERR(ptr); +} +EXPORT_SYMBOL_GPL(rust_helper_IS_ERR); + +long rust_helper_PTR_ERR(__force const void *ptr) +{ + return PTR_ERR(ptr); +} +EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR); + /* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` ty= pe * 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 e8697ad6b62e..67637b874267 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -188,3 +188,52 @@ pub fn to_result(err: core::ffi::c_int) -> Result { Ok(()) } } + +/// Transform a kernel "error pointer" to a normal pointer. +/// +/// Some kernel C API functions return an "error pointer" which optionally +/// embeds an `errno`. Callers are supposed to check the returned pointer +/// for errors. This function performs the check and converts the "error p= ointer" +/// to a normal pointer in an idiomatic fashion. +/// +/// # Examples +/// +/// ```ignore +/// # use kernel::from_err_ptr; +/// # use kernel::bindings; +/// fn devm_platform_ioremap_resource( +/// pdev: &mut PlatformDevice, +/// index: u32, +/// ) -> Result<*mut core::ffi::c_void> { +/// // SAFETY: FFI call. +/// unsafe { +/// from_err_ptr(bindings::devm_platform_ioremap_resource( +/// pdev.to_ptr(), +/// index, +/// )) +/// } +/// } +/// ``` +// TODO: Remove `dead_code` marker once an in-kernel client is available. +#[allow(dead_code)] +pub(crate) fn from_err_ptr(ptr: *mut T) -> Result<*mut T> { + // CAST: Casting a pointer to `*const core::ffi::c_void` is always val= id. + let const_ptr: *const core::ffi::c_void =3D ptr.cast(); + // SAFETY: The FFI function does not deref the pointer. + if unsafe { bindings::IS_ERR(const_ptr) } { + // SAFETY: The FFI function does not deref the pointer. + let err =3D unsafe { bindings::PTR_ERR(const_ptr) }; + // CAST: If `IS_ERR()` returns `true`, + // then `PTR_ERR()` is guaranteed to return a + // negative value greater-or-equal to `-bindings::MAX_ERRNO`, + // which always fits in an `i16`, as per the invariant above. + // And an `i16` always fits in an `i32`. So casting `err` to + // an `i32` can never overflow, and is always valid. + // + // SAFETY: `IS_ERR()` ensures `err` is a + // negative value greater-or-equal to `-bindings::MAX_ERRNO`. + #[allow(clippy::unnecessary_cast)] + return Err(unsafe { Error::from_errno_unchecked(err as core::ffi::= c_int) }); + } + Ok(ptr) +} --=20 2.40.0 From nobody Tue Sep 9 11:58:56 2025 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 644C9C77B60 for ; Mon, 3 Apr 2023 09:50:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232026AbjDCJuG (ORCPT ); Mon, 3 Apr 2023 05:50:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231928AbjDCJti (ORCPT ); Mon, 3 Apr 2023 05:49:38 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A25341880B; Mon, 3 Apr 2023 02:48:45 -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 147614212E; Mon, 3 Apr 2023 09:48:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515323; bh=0XYYkBWr58zEeLbGvBN8vR1PLhMpCbVIUQ1o32lb38s=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=sMKCFGFxxwmp1IBV+QGIUiiZtR8eBU/H2Zb0MaeI81HRDxM60TEflmViWiZGtb/kp yXXOvxo1s72f3j3ppSMqWAF7xcSZdle6Ju7VPe1LSxK6LseybuTTnMGc8zsDVl65IB o3iumoiBBihcMRKoWNeFOfQDGSCSgzLk0Tn3Q8hF9i+wHzjKuilo/bdzg+STr7vzJw HL/RWUHhseEQ1ySx3y1EsIon7ytjmjdb6wAS5E6ZgdjJbxDddyoUQG9AOwRCahyvTV PwTktu6vhHGRQ5npCOMRC4RWHUmW26baQsA0m5w4U37Uso3WQi8tmyq8Pj9/qfYr51 UbrllLTNIpKqw== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:15 +0900 Subject: [PATCH v3 6/6] rust: error: Add from_result() helper MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v3-6-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@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=1680515295; l=2506; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=OO5WuZKnDBBhoWLs7QqycJet5HzYwjS+slg6t+nBjnQ=; b=KZpqgzMr3JkNXDpCxb17Mi7L4pw3c69TRnawuee09u5W1l95mTzU1HBFSSoFHNZzzoi37FUN4 yhLDK+Gk42GAXCVsT52I9IOiugVtKqfzPiAVAF2x4iKHcR86gUTY0yJ X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wedson Almeida Filho Add a helper function to easily return C result codes from a Rust function that calls functions which return a Result. Lina: Imported from rust-for-linux/rust, originally developed by Wedson as part of file_operations.rs. Added the allow() flags since there is no user in the kernel crate yet and fixed a typo in a comment. Replaced the macro with a function taking a closure, per discussion on the ML. Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Signed-off-by: Wedson Almeida Filho Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 67637b874267..5f4114b30b94 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -237,3 +237,42 @@ pub(crate) fn from_err_ptr(ptr: *mut T) -> Result<*= mut T> { } Ok(ptr) } + +/// Calls a closure returning a [`crate::error::Result`] and converts t= he result to +/// a C integer result. +/// +/// This is useful when calling Rust functions that return [`crate::error:= :Result`] +/// from inside `extern "C"` functions that need to return an integer erro= r result. +/// +/// `T` should be convertible from an `i16` via `From`. +/// +/// # Examples +/// +/// ```ignore +/// # use kernel::from_result; +/// # use kernel::bindings; +/// unsafe extern "C" fn probe_callback( +/// pdev: *mut bindings::platform_device, +/// ) -> core::ffi::c_int { +/// from_result(|| { +/// let ptr =3D devm_alloc(pdev)?; +/// bindings::platform_set_drvdata(pdev, ptr); +/// Ok(0) +/// }) +/// } +/// ``` +// TODO: Remove `dead_code` marker once an in-kernel client is available. +#[allow(dead_code)] +pub(crate) fn from_result(f: F) -> T +where + T: From, + F: FnOnce() -> Result, +{ + match f() { + Ok(v) =3D> v, + // NO-OVERFLOW: negative `errno`s are no smaller than `-bindings::= MAX_ERRNO`, + // `-bindings::MAX_ERRNO` fits in an `i16` as per invariant above, + // therefore a negative `errno` always fits in an `i16` and will n= ot overflow. + Err(e) =3D> T::from(e.to_errno() as i16), + } +} --=20 2.40.0