All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] rust: error: integrate Rust error type with `errname`
@ 2023-05-31 17:44 Alice Ryhl
  2023-05-31 19:08 ` Martin Rodriguez Reboredo
  2023-05-31 21:27 ` Sven Van Asbroeck
  0 siblings, 2 replies; 5+ messages in thread
From: Alice Ryhl @ 2023-05-31 17:44 UTC (permalink / raw)
  To: rust-for-linux
  Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl,
	linux-kernel, patches, Wedson Almeida Filho, Sven Van Asbroeck

From: Gary Guo <gary@garyguo.net>

This integrates the `Error` type with the `errname` by making it
accessible via the `name` method or via the `Debug` trait.

Co-Developed-by: Wedson Almeida Filho <walmeida@microsoft.com>
Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
Co-Developed-by: Sven Van Asbroeck <thesven73@gmail.com>
Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
Signed-off-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/bindings/bindings_helper.h |  1 +
 rust/kernel/error.rs            | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 50e7a76d5455..dc8691f83253 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -10,6 +10,7 @@
 #include <linux/refcount.h>
 #include <linux/wait.h>
 #include <linux/sched.h>
+#include <linux/errname.h>
 
 /* `bindgen` gets confused at certain things. */
 const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 5f4114b30b94..59ff362028d0 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -4,12 +4,15 @@
 //!
 //! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
 
+use crate::str::CStr;
+
 use alloc::{
     alloc::{AllocError, LayoutError},
     collections::TryReserveError,
 };
 
 use core::convert::From;
+use core::fmt;
 use core::num::TryFromIntError;
 use core::str::Utf8Error;
 
@@ -113,6 +116,42 @@ impl Error {
         // SAFETY: self.0 is a valid error due to its invariant.
         unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ }
     }
+
+    /// Returns a string representing the error, if one exists.
+    #[cfg(not(testlib))]
+    pub fn name(&self) -> Option<&'static CStr> {
+        // SAFETY: Just an FFI call, there are no extra safety requirements.
+        let ptr = unsafe { bindings::errname(-self.0) };
+        if ptr.is_null() {
+            None
+        } else {
+            // SAFETY: The string returned by `errname` is static and `NUL`-terminated.
+            Some(unsafe { CStr::from_char_ptr(ptr) })
+        }
+    }
+
+    /// Returns a string representing the error, if one exists.
+    ///
+    /// When `testlib` is configured, this always returns `None` to avoid the dependency on a
+    /// kernel function so that tests that use this (e.g., by calling [`Result::unwrap`]) can still
+    /// run in userspace.
+    #[cfg(testlib)]
+    pub fn name(&self) -> Option<&'static CStr> {
+        None
+    }
+}
+
+impl fmt::Debug for Error {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self.name() {
+            // Print out number if no name can be found.
+            None => f.debug_tuple("Error").field(&-self.0).finish(),
+            // SAFETY: These strings are ASCII-only.
+            Some(name) => f
+                .debug_tuple(unsafe { core::str::from_utf8_unchecked(name) })
+                .finish(),
+        }
+    }
 }
 
 impl From<AllocError> for Error {

base-commit: ac9a78681b921877518763ba0e89202254349d1b
-- 
2.41.0.rc0.172.g3f132b7071-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: error: integrate Rust error type with `errname`
  2023-05-31 17:44 [PATCH v1] rust: error: integrate Rust error type with `errname` Alice Ryhl
@ 2023-05-31 19:08 ` Martin Rodriguez Reboredo
  2023-05-31 21:27 ` Sven Van Asbroeck
  1 sibling, 0 replies; 5+ messages in thread
From: Martin Rodriguez Reboredo @ 2023-05-31 19:08 UTC (permalink / raw)
  To: Alice Ryhl, rust-for-linux
  Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, linux-kernel,
	patches, Wedson Almeida Filho, Sven Van Asbroeck

On 5/31/23 14:44, Alice Ryhl wrote:
> From: Gary Guo <gary@garyguo.net>
> 
> This integrates the `Error` type with the `errname` by making it
> accessible via the `name` method or via the `Debug` trait.
> 
> Co-Developed-by: Wedson Almeida Filho <walmeida@microsoft.com>
> Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
> Co-Developed-by: Sven Van Asbroeck <thesven73@gmail.com>
> Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: error: integrate Rust error type with `errname`
  2023-05-31 17:44 [PATCH v1] rust: error: integrate Rust error type with `errname` Alice Ryhl
  2023-05-31 19:08 ` Martin Rodriguez Reboredo
@ 2023-05-31 21:27 ` Sven Van Asbroeck
  2023-06-12 19:04   ` Miguel Ojeda
  1 sibling, 1 reply; 5+ messages in thread
From: Sven Van Asbroeck @ 2023-05-31 21:27 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: rust-for-linux, Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	linux-kernel, patches, Wedson Almeida Filho

Hello Alice, as we discussed through a separate channel,
you have my permission to use my name in
Co-Developed-by and Signed-off-by tags.

On Wed, May 31, 2023 at 1:45 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> From: Gary Guo <gary@garyguo.net>
>
> This integrates the `Error` type with the `errname` by making it
> accessible via the `name` method or via the `Debug` trait.
>
> Co-Developed-by: Wedson Almeida Filho <walmeida@microsoft.com>
> Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
> Co-Developed-by: Sven Van Asbroeck <thesven73@gmail.com>
> Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: error: integrate Rust error type with `errname`
  2023-05-31 21:27 ` Sven Van Asbroeck
@ 2023-06-12 19:04   ` Miguel Ojeda
  2023-06-12 23:36     ` Miguel Ojeda
  0 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2023-06-12 19:04 UTC (permalink / raw)
  To: Sven Van Asbroeck
  Cc: Alice Ryhl, rust-for-linux, Miguel Ojeda, Wedson Almeida Filho,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, linux-kernel, patches, Wedson Almeida Filho

On Wed, May 31, 2023 at 11:27 PM Sven Van Asbroeck <thesven73@gmail.com> wrote:
>
> Hello Alice, as we discussed through a separate channel,
> you have my permission to use my name in
> Co-Developed-by and Signed-off-by tags.

Thanks a lot Sven for the confirmation!

Applied to `rust-next` -- reworded for clarity. Thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: error: integrate Rust error type with `errname`
  2023-06-12 19:04   ` Miguel Ojeda
@ 2023-06-12 23:36     ` Miguel Ojeda
  0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2023-06-12 23:36 UTC (permalink / raw)
  To: Sven Van Asbroeck
  Cc: Alice Ryhl, rust-for-linux, Miguel Ojeda, Wedson Almeida Filho,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, linux-kernel, patches, Wedson Almeida Filho

On Mon, Jun 12, 2023 at 9:04 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> Applied to `rust-next` -- reworded for clarity. Thanks!

When working on the KUnit changes that require this, I noticed this
didn't include the `CONFIG_SYMBOLIC_ERRNAME=n` support we originally
had on the `rust` branch.

So I added the `errname` helper and force-pushed.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-06-12 23:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-31 17:44 [PATCH v1] rust: error: integrate Rust error type with `errname` Alice Ryhl
2023-05-31 19:08 ` Martin Rodriguez Reboredo
2023-05-31 21:27 ` Sven Van Asbroeck
2023-06-12 19:04   ` Miguel Ojeda
2023-06-12 23:36     ` Miguel Ojeda

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.