From nobody Mon Feb 9 08:55:56 2026 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 6DF60C77B60 for ; Wed, 29 Mar 2023 12:05:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229786AbjC2MFD (ORCPT ); Wed, 29 Mar 2023 08:05:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbjC2MEs (ORCPT ); Wed, 29 Mar 2023 08:04:48 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEFC4C0; Wed, 29 Mar 2023 05:04:47 -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 DF14441F98; Wed, 29 Mar 2023 12:04:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091486; bh=7wja8b7e1cR7sIIhKDHqBGrFHH4ZjjMoaG3eC2S/l8g=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=LkP9Ew+YkGZHBzX3b2qYbkE7xE0CHA/iZmwS4N+CxN7LgjWneIu9PHW5BFwVvChfx 004xcRMCJ9EmycJwq1YC7TsehLfdcq5z0BR/EBb6xlotuU6Dwt0cYsjj+zM0vwYCd4 A78F7/TuXmCFRUnvkgv0JTWx+uVVt3dijOdkNp6jva+WSSG/9qlmq9U0n+Ql6E1msi j1md0IBdYgcyQP+ksBGqWNgWgijcaOFAoxdUbyKFzBwWXWLdQNfZKVH8jpJJBJ7V5f bHJtjnH4WBsb9chQsRPvS+4bb4wBvGFK0fH1NtFFZYPMob4Jy9RaaL5b1RzBnHNpMe gAn2Z/VKBNwVQ== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:33 +0900 Subject: [PATCH v2 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-v2-1-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=1196; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=7wja8b7e1cR7sIIhKDHqBGrFHH4ZjjMoaG3eC2S/l8g=; b=kne0zLSHrTRwLBNWWdJZlyhEqojrap6mEiUNwT9Hya5xcb05TI6ZxL69UCWKZQx6xmRUYUVM3 Jzl9RWFoI49D0aF6WsvriRs9AcTLEXYceVVElED7tqThMqYoX15j38o 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(). Signed-off-by: Asahi Lina Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo --- 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 Mon Feb 9 08:55:56 2026 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 9BF0FC77B6C for ; Wed, 29 Mar 2023 12:05:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229835AbjC2MFG (ORCPT ); Wed, 29 Mar 2023 08:05:06 -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: quoted-printable 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: 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. Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo --- 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..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 _ } + } } =20 impl From for Error { --=20 2.40.0 From nobody Mon Feb 9 08:55:56 2026 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 7CFD0C6FD18 for ; Wed, 29 Mar 2023 12:05:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229737AbjC2MFI (ORCPT ); Wed, 29 Mar 2023 08:05:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230058AbjC2ME5 (ORCPT ); Wed, 29 Mar 2023 08:04:57 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E041830ED; Wed, 29 Mar 2023 05:04:55 -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 19E7E41F46; Wed, 29 Mar 2023 12:04:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091494; bh=i4OiR3LCVFp1o7pcdF1+sBJPH/zxSaRWGdgRE2WJDNU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=LhnYkk72xZfQ9ysMIZ32YxC6aiSSoincImJAGgNxUoaYeiBX78sfY5rq8WLcal/i+ XZggEobuj3q0kLkwBWwiU1VzJcw7M5MQoytDIm3CSC3xz8lHu3fituOehDia+s9xM9 b0LG3l0mae6+Ne93MosdY45QU/Z1omitZFuDQi/weoTWOpycW1SyqdKAAZCuaseFOc 3uiFUPxxlbUY59CE7drn6qZ/fHmvwND3KiLdMKPtvB8Taj8pQ1B78VQlNGSQOzuT4h Qndab1aq5ByHy6j6TqikiV8CgHpncvJjdGzpVDkFJqdv+8XxTvj47rjqsxnBd9Wzby Hh2eeuGFnGuzQ== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:35 +0900 Subject: [PATCH v2 3/6] rust: error: Add Error::from_errno() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-error-v2-3-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=1923; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=VSgp8GJmq0GyyWZ2FrIFWGZbohpKXUspc+RJ4lAro+A=; b=MI7BIrTtZmn6MoA5cs6z3VjONdRNDkoCAfIOu+dk9btyw14filP7UP7mvZNtWRVQSD36I41+j sB23CuOjfluCfsXkohXWQdCKwXyqrrETbQlWVggmrqD6pBoRt9D6iIp 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 Signed-off-by: Asahi Lina Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo --- rust/kernel/error.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index e97e652a1aec..659468bd1735 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -72,6 +72,25 @@ 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) + } + /// Returns the kernel error code. pub fn to_errno(self) -> core::ffi::c_int { self.0 --=20 2.40.0 From nobody Mon Feb 9 08:55:56 2026 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 B2DCFC6FD18 for ; Wed, 29 Mar 2023 12:05:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229644AbjC2MFL (ORCPT ); Wed, 29 Mar 2023 08:05:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjC2MFD (ORCPT ); Wed, 29 Mar 2023 08:05:03 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03166468B; Wed, 29 Mar 2023 05:04:59 -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 1B3A4424A5; Wed, 29 Mar 2023 12:04:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091498; bh=D4/twBr+bzIjY4ZGn+sbXBAJvi8KroM1AK8OX54iYyI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=O9YVbyPosEDBz1Y3e376jGTJdPZ56RObMPGEQKs2kbZDSMJLGz0iqM48RP9Bgg4jP 9j92qyyx8XYyVdP9cHvMvyyaXSRb9OtgPm1wKg/eiWnptEAI6XELAMSSY5aNs1Ar58 X9N0AK2DO4ogwJQwYo2KRRQAeLgzB9Wip9ZJ8n6/0XSPCiOka5Xs06nUzf1sWZ5JgZ LkPFlZMXMaX7E8pZ70aWPsUFsyoarZEJnCR6uF1GjvRmWt+nOWCFN/dmINb2qEsQUW svC9yWrzKhMMRJH07fzM0S9pvF5tlqwpfp2DDeaywrYCtFsFwugn3rLElXy4x5NthK px9nunpoJGpJg== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:36 +0900 Subject: [PATCH v2 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-v2-4-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=1246; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=Pyrepc31fu1j2hIHl+GlAJgfJXkFKrgb4Ue04icQgQk=; b=Qi1Fy6I/4bHQHbB9VuqBIwCJsY+ax2RlS2ro/Kyr4QO8xcgJz+/zdD8IOdnkYaufWCDB//8ot kA5XhtU7hJlADXksO7SehAh9Q98rL6Ptixy/XzzymQEtuAjJBKlp5vR 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 Signed-off-by: Asahi Lina Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo --- rust/kernel/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 659468bd1735..4f599c4d1752 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -167,3 +167,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 Mon Feb 9 08:55:56 2026 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 B4029C74A5B for ; Wed, 29 Mar 2023 12:05:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230007AbjC2MFU (ORCPT ); Wed, 29 Mar 2023 08:05:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229627AbjC2MFH (ORCPT ); Wed, 29 Mar 2023 08:05:07 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1754B4EC1; Wed, 29 Mar 2023 05:05:04 -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 1A545424D0; Wed, 29 Mar 2023 12:04:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091502; bh=Z2Msqmu+TDL/Y0kqemtpmsxm6/7qz/c1AG9xBrS/GVA=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=LgcC9RbcMlP1MIG3M4+1vSvrPC2561Y7xnNXcvGF8kl5gBLmrZuTAFzICs2gk8F9I 1IDOMz1otzPrd6sNLh5zALuVGasmJM5TM7vbbiIKnFQgopxpnep80BxsWR6UHV3W9v AXuHq8/2En4kIX6zRy9PIujREUHDndBeGsrQplvEYv9Tr/nCIejII+Apk8WRTZO2x3 0m6SRNcNNQKYIhvJyzScDbtooJi4tFx04XtZhmeBuChvjYFs7F8Oym/NsQXS4LgeWD Z7ishp6wiYMZqG4xuAN1vFiJ07KGK2Nu+lTPk2zRfKZJ2MPdLZRQluIIvVd7z3rfJR xEESjrku4pW/w== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:37 +0900 Subject: [PATCH v2 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-v2-5-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=4224; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=qOm0Nqiw7jlI1w438+NRLYRRJvpIZVUQLi5w4HxGf24=; b=lVOZaAXcQ20z1kEBxLI7TArlo/VvpaBP4j7TWhGxiiK4DT7IbyYMONGf6nwTeO6cm/ltdQLmJ RMkzsjzmSoyCyPkM/4gpY50WP+qwFE6VRK3bkuDvZxFtMyqf+J7rz4S 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. Replaced usage of from_kernel_errno_unchecked() with an open-coded constructor, since this is the only user anyway. 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 Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo --- 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 4f599c4d1752..6b10129075a7 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -177,3 +177,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`. + #[cfg_attr(CONFIG_ARM, allow(clippy::unnecessary_cast))] + return Err(Error(err as i32)); + } + Ok(ptr) +} --=20 2.40.0 From nobody Mon Feb 9 08:55:56 2026 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 E28B4C761A6 for ; Wed, 29 Mar 2023 12:05:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229608AbjC2MFd (ORCPT ); Wed, 29 Mar 2023 08:05:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230071AbjC2MFU (ORCPT ); Wed, 29 Mar 2023 08:05:20 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5037344A8; Wed, 29 Mar 2023 05:05:08 -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 1C55241F46; Wed, 29 Mar 2023 12:05:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091506; bh=70azhY624dmqDR1WbAO/UkRG4/uvUwfXnn1O/f8fXaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Dcx7JmfYEJOvEoHOUZ0fYa8ZVFzUaYpAK5mmZz51FeSgXeupwRNpt4uhxB3kdh6Kx I3WFNita+VmGIE0BV7TYP1oAJ++hgFK3kCyD/ZIiiVYHgm3eJtTnnGOPvdQNtJvqLx 7jbEz+IqnqU8uwkOYIm/cv0zRKszm60IWa1Moh7TW/BS0WcJZZqmR+/PTx+PVKSyz8 0EtFdvd7DtDatbAhe+vvqqku9Q3zPI3vUybkdJ19cst5iehOGWYWIvoAoreY2v5n+w 0nWp4cKK5IAtscHKfWTEJiuxmVJkmnMBcEWQoeb+43MSQm7f0KdChKYYyD5prnOkp/ K9Zfxsz3jgzfw== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:38 +0900 Subject: [PATCH v2 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-v2-6-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=2445; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=Ai/iEgQ/fBNHagg7igl9ARuv9+jNWr+C78oJzdHwp1Q=; b=FtgUuaSd6mC3+e/DMioQcQS/rPK7XBseDo6+v/MVJi3rTWw7yGSMv3Q7DU/Oyw7ISdeBXG5+d BPUZKi78hqqB9vP31WhnZuhDT44q/p8pQiS+4Z+ejaIpN6TNuA0Z4Fz 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 Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo --- rust/kernel/error.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6b10129075a7..4d8b19d5967d 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -226,3 +226,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