rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <ravi@prevas.dk>
To: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
Cc: rust-for-linux@vger.kernel.org,
	 Daniel Sedlak <daniel@sedlak.dev>,
	Miguel Ojeda <ojeda@kernel.org>
Subject: Re: [PATCH] rust-next: make from_errno use try_from_errno
Date: Thu, 07 Nov 2024 13:26:37 +0100	[thread overview]
Message-ID: <87bjyrgrgi.fsf@prevas.dk> (raw)
In-Reply-To: <20241105114819.14051-1-guilhermev2huehue@gmail.com> (Guilherme Augusto Martins da Silva's message of "Tue, 5 Nov 2024 08:48:19 -0300")

On Tue, Nov 05 2024, Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com> wrote:

> Modified the from_errno function to use try_from_errno to reduce code duplication while still maintaning all existing behavior and error handling and also reduces unsafe code.
>
> Co-developed-by: Daniel Sedlak <daniel@sedlak.dev>
> Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>
> Signed-off-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
> Suggested-by:  Miguel Ojeda <ojeda@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1125
> ---
> diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> index 7cd3bbab52f2..077f95cbb6ae 100644
> --- a/rust/kernel/error.rs
> +++ b/rust/kernel/error.rs
> @@ -101,19 +101,17 @@ impl Error {
>      /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
>      /// be returned in such a case.
>      pub fn from_errno(errno: core::ffi::c_int) -> Error {
> -        if errno < -(bindings::MAX_ERRNO as i32) || errno >= 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;
> +        match Error::try_from_errno(errno) {
> +            Some(error) => error,
> +            None => {
> +                // TODO: Make it a `WARN_ONCE` once available.
> +                crate::pr_warn!(
> +                    "attempted to create `Error` with out of range `errno`: {}",
> +                    errno
> +                );
> +                code::EINVAL
> +            }


Not really related to the change done here, but I've often wondered why
the kernel doesn't have a dedicated EBUG that can be used in places
where a genuine kernel bug is detected at run-time, but BUG() is too
much, and we can survive by just passing some error back up the stack
(possibly with a WARN or pr_warn()). In fact, an EBUG could be useful in
userspace code as well for library code that detects some
should-not-happen inconsistency.

EINVAL is already way overused, and substituting that when a programming
error rather than bad user input is detected seems to just overload it
even more.

Rasmus

  reply	other threads:[~2024-11-07 12:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-05 11:48 [PATCH] rust-next: make from_errno use try_from_errno Guilherme Augusto Martins da Silva
2024-11-07 12:26 ` Rasmus Villemoes [this message]
2024-11-14 19:42 ` Daniel Sedlak
  -- strict thread matches above, loose matches on Subject: below --
2024-11-04 20:00 Guilherme Augusto Martins da Silva
2024-11-04 20:14 ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bjyrgrgi.fsf@prevas.dk \
    --to=ravi@prevas.dk \
    --cc=daniel@sedlak.dev \
    --cc=guilhermev2huehue@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).