From: Greg KH <gregkh@linuxfoundation.org>
To: "Behme Dirk (CM/ESO2)" <dirk.behme@de.bosch.com>
Cc: rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] [RFC] rust: error: Convert 0 being an error to Result
Date: Thu, 11 Jan 2024 08:54:22 +0100 [thread overview]
Message-ID: <2024011108-shortcake-underwent-e12d@gregkh> (raw)
In-Reply-To: <4b2265c7-3ef3-4bca-8237-3752d2915c0a@de.bosch.com>
On Thu, Jan 11, 2024 at 08:29:06AM +0100, Behme Dirk (CM/ESO2) wrote:
> On 11.01.2024 07:59, Greg KH wrote:
> > On Thu, Jan 11, 2024 at 07:44:15AM +0100, Dirk Behme wrote:
> > > The existing to_result() takes a (signed) integer from a kernel C function
> > > and converts it to an error if it's negative. Additionally, there are
> > > kernel C functions returning an unsigned integer where 0 is the error case.
> > > For example gen_pool_alloc() and friends. Provide a mechanism to convert
> > > this to Result too.
> > >
> > > Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
> > > ---
> > > rust/kernel/error.rs | 10 ++++++++++
> > > 1 file changed, 10 insertions(+)
> > >
> > > I'm unsure if something like this is acceptable. Therefore the RFC.
> > > But I want at least ask ;) In the end this is a slightly modified
> > > copy of the existing to_result().
> > >
> > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> > > index 376280b6a745a..3306b8f590866 100644
> > > --- a/rust/kernel/error.rs
> > > +++ b/rust/kernel/error.rs
> > > @@ -248,6 +248,16 @@ pub fn to_result(err: core::ffi::c_int) -> Result {
> > > }
> > > }
> > > +/// Converts an unsigned integer as returned by a C kernel function to EINVAL if it's zero,
> > > +/// and `Ok(u64)` otherwise.
> > > +pub fn to_result_zero(val: core::ffi::c_ulong) -> Result<u64> {
> > > + if val == 0 {
> > > + Err(code::EINVAL)
> > > + } else {
> > > + Ok(val)
> > > + }
> > > +}
> >
> > 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?
I guess a usage of the new function would be good to see to clarify this
please.
thanks,
greg k-h
next prev parent reply other threads:[~2024-01-11 7:54 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 [this message]
2024-01-11 8:14 ` Behme Dirk (CM/ESO2)
2024-01-11 9:46 ` Trevor Gross
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=2024011108-shortcake-underwent-e12d@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=dirk.behme@de.bosch.com \
--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 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.