rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trevor Gross <tmgross@umich.edu>
To: "Behme Dirk (CM/ESO2)" <dirk.behme@de.bosch.com>
Cc: Greg KH <gregkh@linuxfoundation.org>, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] [RFC] rust: error: Convert 0 being an error to Result
Date: Thu, 11 Jan 2024 04:46:17 -0500	[thread overview]
Message-ID: <CALNs47u6MSJRGyqx9mjQCF41j8fAnfTwNgYdrQozB48KLgOnpQ@mail.gmail.com> (raw)
In-Reply-To: <36117a36-18e9-4961-8761-ed32f8cce02e@de.bosch.com>

On Thu, Jan 11, 2024 at 3:14 AM Behme Dirk (CM/ESO2)
<dirk.behme@de.bosch.com> wrote:
>>>> How would this be used?
>>>
>>> One (randomly selected) usage example:
>>>
>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/cpu/mce/genpool.c#n107
>>>
>>> node = (void *)gen_pool_alloc(mce_evt_pool, sizeof(*node));
>>> if (!node) {
>>>     pr_warn_ratelimited("MCE records pool full!\n");
>>>     return -ENOMEM;
>>> }
>>>
>>>> 0 is normally not an error, why would you
>>>> need/want to turn that into an error value?
>>
>> So you are treating NULL as 0 in rust bindings somehow?
>
> gen_pool_alloc() returns an unsigned long. So we are talking about 0
> being the error case we need to catch via Result. Similar to the
> existing to_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(())
>     }
> }
>
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/error.rs#n241
>
> > I guess a usage of the new function would be good to see to clarify this
> > please.
> Something like
>
> let pool = to_result_zero(unsafe { bindings::gen_pool_alloc(pool_ptr,
> size) })?;

Are these cases mostly from where unsigned long is used as a pointer?
If so, maybe it would be convenient to return a `*mut u8` or
`NonNull<u8>`. It should probably take the error type as a parameter
so you can specify.

For what it's worth, you can also do this using NonZero* numbers or
NonNull pointer [1,2], with ok_or to provide the error type:

    let tmp = unsafe { bindings::gen_pool_alloc(pool_ptr, size) } as
*mut mce_evt_llist;
    let pool = NonNull::new(tmp).ok_or(ENOMEM)?;

- Trevor

> Best regards
>
> Dirk
>

[1]: https://doc.rust-lang.org/core/num/struct.NonZeroU64.html#method.new
[2]: https://doc.rust-lang.org/core/ptr/struct.NonNull.html#method.new

  reply	other threads:[~2024-01-11  9:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  6:44 [PATCH] [RFC] rust: error: Convert 0 being an error to Result Dirk Behme
2024-01-11  6:59 ` Greg KH
2024-01-11  7:29   ` Behme Dirk (CM/ESO2)
2024-01-11  7:54     ` Greg KH
2024-01-11  8:14       ` Behme Dirk (CM/ESO2)
2024-01-11  9:46         ` Trevor Gross [this message]
2024-01-11 12:17         ` Benno Lossin
2024-01-11 20:52           ` Trevor Gross
2024-01-11  6:59 ` Michael Büsch
2024-01-11 14:14 ` Alice Ryhl
2024-01-12  7:42   ` Behme Dirk (CM/ESO2)

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=CALNs47u6MSJRGyqx9mjQCF41j8fAnfTwNgYdrQozB48KLgOnpQ@mail.gmail.com \
    --to=tmgross@umich.edu \
    --cc=dirk.behme@de.bosch.com \
    --cc=gregkh@linuxfoundation.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).