From: Benno Lossin <benno.lossin@proton.me>
To: "Behme Dirk (CM/ESO2)" <dirk.behme@de.bosch.com>,
Greg KH <gregkh@linuxfoundation.org>
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 12:17:35 +0000 [thread overview]
Message-ID: <2024f8b1-eef2-4666-81e5-a8bca65afef9@proton.me> (raw)
In-Reply-To: <36117a36-18e9-4961-8761-ed32f8cce02e@de.bosch.com>
On 11.01.24 09:14, Behme Dirk (CM/ESO2) wrote:
> On 11.01.2024 08:54, Greg KH wrote:
>> 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?
>
> gen_pool_alloc() returns an unsigned long.
Why does it return an unsigned long and not `void *`? I looked at some
other usages and I found several cases where the result is immediately
cast to a pointer. Maybe it might be a better idea to just change the
return type to a pointer type. If that is too much work though, I would
recommend to first turn it into a pointer on the Rust side and then
use `NonNull` or something similar like Trevor suggested.
--
Cheers,
Benno
next prev parent reply other threads:[~2024-01-11 12:18 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
2024-01-11 12:17 ` Benno Lossin [this message]
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=2024f8b1-eef2-4666-81e5-a8bca65afef9@proton.me \
--to=benno.lossin@proton.me \
--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).