All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: Asahi Lina <lina@asahilina.net>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Sven Van Asbroeck" <thesven73@gmail.com>,
	"Fox Chen" <foxhlchen@gmail.com>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	asahi@lists.linux.dev
Subject: Re: [PATCH v2 3/6] rust: error: Add Error::from_errno()
Date: Wed, 29 Mar 2023 21:35:48 +0100	[thread overview]
Message-ID: <20230329213548.14a47ede.gary@garyguo.net> (raw)
In-Reply-To: <20230224-rust-error-v2-3-3900319812da@asahilina.net>

On Wed, 29 Mar 2023 21:04:35 +0900
Asahi Lina <lina@asahilina.net> wrote:

> From: Miguel Ojeda <ojeda@kernel.org>
> 
> 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 <foxhlchen@gmail.com>
> Signed-off-by: Fox Chen <foxhlchen@gmail.com>
> Co-developed-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>
> Signed-off-by: Asahi Lina <lina@asahilina.net>
> ---
>  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);
>  
>  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 >= 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
> 

Reviewed-by: Gary Guo <gary@garyguo.net>

  parent reply	other threads:[~2023-03-29 20:35 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-29 12:04 [PATCH v2 0/6] rust: error: Add missing wrappers to convert to/from kernel error codes Asahi Lina
2023-03-29 12:04 ` [PATCH v2 1/6] rust: error: Rename to_kernel_errno() -> to_errno() Asahi Lina
2023-03-29 14:47   ` Martin Rodriguez Reboredo
2023-03-29 15:04     ` Miguel Ojeda
2023-03-29 18:16       ` Martin Rodriguez Reboredo
2023-03-29 22:33         ` Miguel Ojeda
2023-03-29 20:32   ` Gary Guo
2023-03-29 22:33     ` Miguel Ojeda
2023-03-29 12:04 ` [PATCH v2 2/6] rust: error: Add Error::to_ptr() Asahi Lina
2023-03-29 14:49   ` Martin Rodriguez Reboredo
2023-03-29 20:34   ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 3/6] rust: error: Add Error::from_errno() Asahi Lina
2023-03-29 14:51   ` Martin Rodriguez Reboredo
2023-03-29 20:35   ` Gary Guo [this message]
2023-03-29 12:04 ` [PATCH v2 4/6] rust: error: Add to_result() helper Asahi Lina
2023-03-29 14:52   ` Martin Rodriguez Reboredo
2023-03-29 20:36   ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 5/6] rust: error: Add a helper to convert a C ERR_PTR to a `Result` Asahi Lina
2023-03-29 14:53   ` Martin Rodriguez Reboredo
2023-03-29 20:42   ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 6/6] rust: error: Add from_result() helper Asahi Lina
2023-03-29 14:55   ` Martin Rodriguez Reboredo

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=20230329213548.14a47ede.gary@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=asahi@lists.linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=foxhlchen@gmail.com \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=thesven73@gmail.com \
    --cc=wedsonaf@gmail.com \
    --cc=yakoyoku@gmail.com \
    /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 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.