rust: refactor `to_result`
Subsystems (23)
Onur Özkan
reviewerPATCH v3 2/2 rust: drop `error::to_result` and utilize `ToResult`
In reply to Onur ÖzkanCurrent to_result helper takes a c_int and returns Ok(()) on success and this has some issues like:
- Callers lose the original return value and often have to store it in a temporary variable before calling to_result.
- It only supports c_int, which makes callers to unnecessarily cast when working with other types (e.g. u16 in phy abstractions). We even have some places that ignore to use to_result helper because the input doesn't fit in c_int (see [1]).
[1]: https://lore.kernel.org/all/[email protected]/
This patch removes the error::to_result function and replaces it with a more advanced helper, ToResult trait. This change brings three main benefits:
- A better calling convention. Instead of wrapping function calls with to_result, we can now call .to_result() directly as an extension function.
- The returned value is preserved as an unsigned integer on success which was previously discarded by error::to_result.
- It's no longer limited with a single input type. E.g., right now we can call to_result on isize without manually casting them into i32.
So that the code that previously looked like:
let ret = unsafe { bindings::some_ffi_call() };
to_result(ret).map(|()| SomeType::new(ret))can now be written more directly as:
unsafe { bindings::some_ffi_call() }.to_result()
.map(SomeType::new)
Similarly, code such as:
let res: isize = unsafe { bindings::some_ffi_call() };
if res < 0 {
return Err(Error::from_errno(res as i32));
}can be done without manually casting into i32:
unsafe { bindings::some_ffi_call() }.to_result()?;This patch only fixes the callers that broke after changes on to_result. I haven't included all the improvements made possible by the new design since that could conflict with other ongoing patches [2]. Once this patch is approved and applied, I am planning to follow up with creating a "good first issue" on [3] for those additional changes.
[2]: https://lore.kernel.org/rust-for-linux/?q=to_result
[3]: https://github.com/Rust-for-Linux/linux
--- drivers/android/binder/rust_binder_main.rs | 3 +- rust/kernel/auxiliary.rs | 9 +-- rust/kernel/block/mq/gen_disk.rs | 12 ++-- rust/kernel/block/mq/tag_set.rs | 5 +- rust/kernel/clk.rs | 18 ++++-- rust/kernel/configfs.rs | 9 +-- rust/kernel/cpufreq.rs | 12 ++-- rust/kernel/device/property.rs | 12 ++-- rust/kernel/devres.rs | 13 ++-- rust/kernel/dma.rs | 18 ++++-- rust/kernel/drm/driver.rs | 4 +- rust/kernel/drm/gem/mod.rs | 12 ++-- rust/kernel/error.rs | 45 -------------- rust/kernel/fs/file.rs | 4 +- rust/kernel/irq/request.rs | 10 +-- rust/kernel/maple_tree.rs | 12 ++-- rust/kernel/miscdevice.rs | 6 +- rust/kernel/mm/virt.rs | 6 +- rust/kernel/net/phy.rs | 43 +++++++++---- rust/kernel/net/phy/reg.rs | 22 ++++--- rust/kernel/opp.rs | 72 +++++++++++++--------- rust/kernel/pci.rs | 13 ++-- rust/kernel/platform.rs | 6 +- rust/kernel/regulator.rs | 32 +++++++--- rust/kernel/scatterlist.rs | 13 ++-- rust/kernel/security.rs | 23 ++++--- rust/kernel/str.rs | 5 +- rust/kernel/uaccess.rs | 15 ++--- rust/kernel/usb.rs | 9 +-- 29 files changed, 253 insertions(+), 210 deletions(-)
drivers/android/binder/rust_binder_main.rs +2 -1
@@ -13,6 +13,7 @@use kernel::{ bindings::{self, seq_file}, + error::ToResult , fs::File , list::{ListArc ,ListArcSafe ,ListLinksSelfPtr ,TryNewListArc }, prelude::*,
@@ -291,7 +292,7 @@ fn init(_module: &'static kernel::ThisModule) -> Result<Self> {BINDER_SHRINKER .register (kernel::c_str!("android-binder" ))?;// SAFETY: The module is being loaded, so we can initialize binderfs. -unsafe { kernel::error::to_result (binderfs::init_rust_binderfs ())? }; +unsafe { binderfs::init_rust_binderfs ().to_result ()? };Ok (Self {}) }
rust/kernel/auxiliary.rs +5 -4
@@ -8,7 +8,7 @@ bindings, container_of, device, device_id::{RawDeviceId, RawDeviceIdIndex}, driver, - error::{from_result, to_result, Result}, + error::{from_result, Result, ToResult}, prelude::*, types::Opaque, ThisModule,
@@ -40,9 +40,10 @@ unsafe fn register( }// SAFETY: `adrv` is guaranteed to be a valid `RegType`. -to_result (unsafe { - bindings::__auxiliary_driver_register (adrv.get (), module.0 , name.as_char_ptr ()) - }) +unsafe { bindings::__auxiliary_driver_register (adrv.get (), module.0 , name.as_char_ptr ()) } + .to_result ()?; + +Ok (()) }unsafe fn unregister (adrv: &Opaque <Self ::RegType >) {
rust/kernel/block/mq/gen_disk.rs +5 -7
@@ -8,6 +8,7 @@use crate ::{ bindings, block::mq::{Operations ,TagSet }, + error::ToResult , error::{self, from_err_ptr,Result }, fmt::{self,Write }, prelude::*,
@@ -167,13 +168,10 @@ pub fn build<T: Operations>(// operation, so we will not race. unsafe { bindings::set_capacity (gendisk, self.capacity_sectors) }; -crate ::error::to_result ( -// SAFETY: `gendisk` points to a valid and initialized instance of -// `struct gendisk`. -unsafe { - bindings::device_add_disk (core::ptr::null_mut (), gendisk, core::ptr::null_mut ()) - }, - )?; +// SAFETY: `gendisk` points to a valid and initialized instance of +// `struct gendisk`. +unsafe { bindings::device_add_disk (core::ptr::null_mut (), gendisk, core::ptr::null_mut ()) } + .to_result ()?; recover_data.dismiss ();
rust/kernel/block/mq/tag_set.rs +3 -2
@@ -9,7 +9,7 @@use crate ::{ bindings, block::mq::{operations::OperationsVTable , request::RequestDataWrapper ,Operations }, - error::{self,Result }, + error::{self,Result ,ToResult }, prelude::try_pin_init, types::Opaque , };
@@ -65,7 +65,8 @@ pub fn new(// SAFETY: we do not move out of `tag_set`. let tag_set: &mut Opaque <_ > =unsafe {Pin ::get_unchecked_mut (tag_set) };// SAFETY: `tag_set` is a reference to an initialized `blk_mq_tag_set`. - error::to_result (unsafe { bindings::blk_mq_alloc_tag_set (tag_set.get ())}) +unsafe { bindings::blk_mq_alloc_tag_set (tag_set.get ())}.to_result ()?; +Ok (()) }), _p: PhantomData, })
rust/kernel/clk.rs +13 -5
@@ -81,7 +81,7 @@ mod common_clk {use super ::Hertz ;use crate ::{ device::Device , - error::{from_err_ptr, to_result,Result }, + error::{from_err_ptr,Result ,ToResult }, prelude::*, };
@@ -161,7 +161,9 @@ pub fn as_raw(&self) -> *mut bindings::clk {pub fn enable (&self) ->Result {// SAFETY: By the type invariants, self.as_raw() is a valid argument for // [`clk_enable`]. -to_result (unsafe { bindings::clk_enable (self.as_raw ()) }) +unsafe { bindings::clk_enable (self.as_raw ()) }.to_result ()?; + +Ok (()) }/// Disable the clock.
@@ -185,7 +187,9 @@ pub fn disable(&self) {pub fn prepare (&self) ->Result {// SAFETY: By the type invariants, self.as_raw() is a valid argument for // [`clk_prepare`]. -to_result (unsafe { bindings::clk_prepare (self.as_raw ()) }) +unsafe { bindings::clk_prepare (self.as_raw ()) }.to_result ()?; + +Ok (()) }/// Unprepare the clock.
@@ -207,7 +211,9 @@ pub fn unprepare(&self) {pub fn prepare_enable (&self) ->Result {// SAFETY: By the type invariants, self.as_raw() is a valid argument for // [`clk_prepare_enable`]. -to_result (unsafe { bindings::clk_prepare_enable (self.as_raw ()) }) +unsafe { bindings::clk_prepare_enable (self.as_raw ()) }.to_result ()?; + +Ok (()) }/// Disable and unprepare the clock.
@@ -241,7 +247,9 @@ pub fn rate(&self) -> Hertz {pub fn set_rate (&self, rate:Hertz ) ->Result {// SAFETY: By the type invariants, self.as_raw() is a valid argument for // [`clk_set_rate`]. -to_result (unsafe { bindings::clk_set_rate (self.as_raw (), rate.as_hz ()) }) +unsafe { bindings::clk_set_rate (self.as_raw (), rate.as_hz ()) }.to_result ()?; + +Ok (()) } }
rust/kernel/configfs.rs +5 -4
@@ -112,6 +112,7 @@use crate ::alloc::flags;use crate ::container_of; +use crate ::error::ToResult ;use crate ::page::PAGE_SIZE ;use crate ::prelude::*;use crate ::str::CString ;
@@ -176,10 +177,10 @@ pub fn new( data <- data, }) .pin_chain (|this| { -crate ::error::to_result ( -// SAFETY: We initialized `this.subsystem` according to C API contract above. -unsafe { bindings::configfs_register_subsystem (this.subsystem.get ()) }, - ) +// SAFETY: We initialized `this.subsystem` according to C API contract above. +unsafe { bindings::configfs_register_subsystem (this.subsystem.get ()) }.to_result ()?; + +Ok (()) }) } }
rust/kernel/cpufreq.rs +8 -4
@@ -14,7 +14,7 @@ cpumask, device::{Bound, Device}, devres, - error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR}, + error::{code::*, from_err_ptr, from_result, Result, ToResult, VTABLE_DEFAULT_ERROR}, ffi::{c_char, c_ulong}, prelude::*, types::ForeignOwnable,
@@ -157,7 +157,9 @@ pub fn as_raw(&self) -> *mut bindings::cpufreq_policy_data {#[inline] pub fn generic_verify (&self) ->Result {// SAFETY: By the type invariant, the pointer stored in `self` is valid. -to_result (unsafe { bindings::cpufreq_generic_frequency_table_verify (self.as_raw ()) }) +unsafe { bindings::cpufreq_generic_frequency_table_verify (self.as_raw ()) }.to_result ()?; + +Ok (()) } }
@@ -520,7 +522,9 @@ pub fn set_suspend_freq(&mut self, freq: Hertz) -> &mut Self {#[inline] pub fn generic_suspend (&mut self) ->Result {// SAFETY: By the type invariant, the pointer stored in `self` is valid. -to_result (unsafe { bindings::cpufreq_generic_suspend (self.as_mut_ref ()) }) +unsafe { bindings::cpufreq_generic_suspend (self.as_mut_ref ()) }.to_result ()?; + +Ok (()) }/// Provides a wrapper to the generic get routine.
@@ -1038,7 +1042,7 @@ pub fn new() -> Result<Self> {let mut drv =KBox ::new (UnsafeCell ::new (Self ::VTABLE ),GFP_KERNEL )?;// SAFETY: `drv` is guaranteed to be valid for the lifetime of `Registration`. -to_result (unsafe { bindings::cpufreq_register_driver (drv.get_mut ()) })?; +unsafe { bindings::cpufreq_register_driver (drv.get_mut ()) }.to_result ()?;Ok (Self (drv,PhantomData )) }
rust/kernel/device/property.rs +6 -6
@@ -10,7 +10,7 @@use crate ::{ alloc::KVec , bindings, - error::{to_result,Result }, + error::{Result ,ToResult }, fmt, prelude::*, str::{CStr ,CString },
@@ -122,7 +122,7 @@ pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usi match_str.as_char_ptr(), ) }; -to_result (ret)?; + ret.to_result ()?;Ok (retas usize ) }
@@ -290,7 +290,7 @@ pub fn property_get_reference_args( &mut out_args.0, ) }; -to_result (ret)?; + ret.to_result ()?;Ok (out_args) }
@@ -452,7 +452,7 @@ fn read_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<Self> {let ret =unsafe { bindings::fwnode_property_read_string (fwnode.as_raw (), name.as_char_ptr (), pstr.cast ()) }; -to_result (ret)?; + ret.to_result ()?;// SAFETY: // - `pstr` is a valid pointer to a NUL-terminated C string.
@@ -514,7 +514,7 @@ fn read_array_from_fwnode_property<'a>( out.len(), ) }; -to_result (ret)?; + ret.to_result ()?;// SAFETY: Transmuting from `&'a mut [MaybeUninit<Self>]` to // `&'a mut [Self]` is sound, because the previous call to a // `fwnode_property_read_*_array` function (which didn't fail)
@@ -536,7 +536,7 @@ fn read_array_len_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<u 0, ) }; -to_result (ret)?; + ret.to_result ()?;Ok (retas usize ) } }
rust/kernel/devres.rs +8 -5
@@ -9,7 +9,7 @@ alloc::Flags, bindings, device::{Bound, Device}, - error::{to_result, Error, Result}, + error::{Error, Result, ToResult}, ffi::c_void, prelude::*, revocable::{Revocable, RevocableGuard},
@@ -157,9 +157,9 @@ pub fn new<'a, E>(// - `devm_add_action()` is guaranteed not to call `callback` until `this` has been // properly initialized, because we require `dev` (i.e. the *bound* device) to // live at least as long as the returned `impl PinInit<Self, Error>`. -to_result (unsafe { +unsafe { bindings::devm_add_action (dev.as_raw (),Some (*callback), inner.cast ()) - }).inspect_err (|_| { + }.to_result ().inspect_err (|_| {let inner =Opaque ::cast_into (inner);// SAFETY: `inner` is a valid pointer to an `Inner<T>` and valid for both reads
@@ -321,11 +321,14 @@ fn register_foreign<P>(dev: &Device<Bound>, data: P) -> Result// SAFETY: // - `dev.as_raw()` is a pointer to a valid and bound device. // - `ptr` is a valid pointer the `ForeignOwnable` devres takes ownership of. -to_result (unsafe { +unsafe {// `devm_add_action_or_reset()` also calls `callback` on failure, such that the // `ForeignOwnable` is released eventually. bindings::devm_add_action_or_reset (dev.as_raw (),Some (callback ::<P >), ptr.cast ()) - }) + } + .to_result ()?; + +Ok (()) }/// Encapsulate `data` in a [`KBox`] and [`Drop::drop`] `data` once `dev` is unbound.
rust/kernel/dma.rs +12 -6
@@ -7,7 +7,7 @@use crate ::{ bindings, build_assert, device, device::{Bound ,Core }, - error::{to_result,Result }, + error::{Result ,ToResult }, prelude::*, sync::aref::ARef , transmute::{AsBytes ,FromBytes },
@@ -43,7 +43,9 @@ unsafe fn dma_set_mask(&self, mask: DmaMask) -> Result {// - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. // - The safety requirement of this function guarantees that there are no concurrent calls // to DMA allocation and mapping primitives using this mask. -to_result (unsafe { bindings::dma_set_mask (self.as_ref ().as_raw (), mask.value ()) }) +unsafe { bindings::dma_set_mask (self.as_ref ().as_raw (), mask.value ()) }.to_result ()?; + +Ok (()) }/// Set up the device's DMA coherent addressing capabilities.
@@ -60,7 +62,10 @@ unsafe fn dma_set_coherent_mask(&self, mask: DmaMask) -> Result {// - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. // - The safety requirement of this function guarantees that there are no concurrent calls // to DMA allocation and mapping primitives using this mask. -to_result (unsafe { bindings::dma_set_coherent_mask (self.as_ref ().as_raw (), mask.value ()) }) +unsafe { bindings::dma_set_coherent_mask (self.as_ref ().as_raw (), mask.value ()) } + .to_result ()?; + +Ok (()) }/// Set up the device's DMA addressing capabilities.
@@ -79,9 +84,10 @@ unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {// - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. // - The safety requirement of this function guarantees that there are no concurrent calls // to DMA allocation and mapping primitives using this mask. -to_result (unsafe { - bindings::dma_set_mask_and_coherent (self.as_ref ().as_raw (), mask.value ()) - }) +unsafe { bindings::dma_set_mask_and_coherent (self.as_ref ().as_raw (), mask.value ()) } + .to_result ()?; + +Ok (()) } }
rust/kernel/drm/driver.rs +2 -2
@@ -6,7 +6,7 @@use crate ::{ bindings, device, devres, drm, - error::{to_result,Result }, + error::{Result ,ToResult }, prelude::*, sync::aref::ARef , };
@@ -124,7 +124,7 @@ impl<T: Driver> Registration<T> {/// Creates a new [`Registration`] and registers it. fn new (drm: &drm::Device <T >, flags:usize ) ->Result <Self > {// SAFETY: `drm.as_raw()` is valid by the invariants of `drm::Device`. -to_result (unsafe { bindings::drm_dev_register (drm.as_raw (), flags) })?; +unsafe { bindings::drm_dev_register (drm.as_raw (), flags) }.to_result ()?;Ok (Self (drm.into ())) }
rust/kernel/drm/gem/mod.rs +7 -5
@@ -8,7 +8,7 @@ alloc::flags::*, bindings, drm, drm::driver::{AllocImpl, AllocOps}, - error::{to_result, Result}, + error::{Result, ToResult}, prelude::*, sync::aref::{ARef, AlwaysRefCounted}, types::Opaque,
@@ -136,9 +136,11 @@ fn create_handle<D, F>(&self, file: &drm::File<F>) -> Result<u32> {let mut handle:u32 =0 ;// SAFETY: The arguments are all valid per the type invariants. -to_result (unsafe { +unsafe { bindings::drm_gem_handle_create (file.as_raw ().cast (), self.as_raw (), &mut handle) - })?; + } + .to_result ()?; +Ok (handle) }
@@ -173,7 +175,7 @@ fn lookup_handle<D, F>(file: &drm::File<F>, handle: u32) -> Result<ARef<Self>>/// Creates an mmap offset to map the object from userspace. fn create_mmap_offset (&self) ->Result <u64 > {// SAFETY: The arguments are valid per the type invariant. -to_result (unsafe { bindings::drm_gem_create_mmap_offset (self.as_raw ()) })?; +unsafe { bindings::drm_gem_create_mmap_offset (self.as_raw ()) }.to_result ()?;// SAFETY: The arguments are valid per the type invariant. Ok (unsafe { bindings::drm_vma_node_offset_addr (&raw mut (*self.as_raw ()).vma_node) })
@@ -233,7 +235,7 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {unsafe { (*obj.as_raw ()).funcs = &Self ::OBJECT_FUNCS };// SAFETY: The arguments are all valid per the type invariants. -to_result (unsafe { bindings::drm_gem_object_init (dev.as_raw (), obj.obj.get (), size) })?; +unsafe { bindings::drm_gem_object_init (dev.as_raw (), obj.obj.get (), size) }.to_result ()?;// SAFETY: We never move out of `Self`. let ptr =KBox ::into_raw (unsafe {Pin ::into_inner_unchecked (obj) });
rust/kernel/error.rs +0 -45
51 lines not on this page · raw message
rust/kernel/fs/file.rs +2 -2
@@ -10,7 +10,7 @@use crate ::{ bindings, cred::Credential , - error::{code::*, to_result,Error ,Result }, + error::{code::*,Error ,Result ,ToResult }, fmt, sync::aref::{ARef ,AlwaysRefCounted }, types::{NotThreadSafe ,Opaque },
@@ -400,7 +400,7 @@ impl FileDescriptorReservation {pub fn get_unused_fd_flags (flags:u32 ) ->Result <Self > {// SAFETY: FFI call, there are no safety requirements on `flags`. let fd:i32 =unsafe { bindings::get_unused_fd_flags (flags) }; -to_result (fd)?; + fd.to_result ()?;Ok (Self { fd: fdas u32 ,
rust/kernel/irq/request.rs +5 -5
@@ -10,7 +10,7 @@use crate ::alloc::Allocator ;use crate ::device::{Bound ,Device };use crate ::devres::Devres ; -use crate ::error::to_result; +use crate ::error::ToResult ;use crate ::irq::flags::Flags ;use crate ::prelude::*;use crate ::str::CStr ;
@@ -219,7 +219,7 @@ pub fn new<'a>(// - When request_irq is called, everything that handle_irq_callback will // touch has already been initialized, so it's safe for the callback to // be called immediately. -to_result (unsafe { +unsafe { bindings::request_irq ( request.irq, Some(handle_irq_callback::<T>),
@@ -227,7 +227,7 @@ pub fn new<'a>( name.as_char_ptr(), this.as_ptr().cast::<c_void>(), ) - })?; + }.to_result ()?; request.irq } })
@@ -437,7 +437,7 @@ pub fn new<'a>(// - When request_threaded_irq is called, everything that the two callbacks // will touch has already been initialized, so it's safe for the // callbacks to be called immediately. -to_result (unsafe { +unsafe { bindings::request_threaded_irq ( request.irq, Some(handle_threaded_irq_callback::<T>),
@@ -446,7 +446,7 @@ pub fn new<'a>( name.as_char_ptr(), this.as_ptr().cast::<c_void>(), ) - })?; + }.to_result ()?; request.irq } })
rust/kernel/maple_tree.rs +7 -5
@@ -14,7 +14,7 @@use kernel::{ alloc::Flags , - error::to_result, + error::ToResult , prelude::*, types::{ForeignOwnable ,Opaque }, };
@@ -180,9 +180,10 @@ pub fn insert_range<R>(&self, range: R, value: T, gfp: Flags) -> Result<(), Inselet ptr =T ::into_foreign (value);// SAFETY: The tree is valid, and we are passing a pointer to an owned instance of `T`. -let res =to_result (unsafe { +let res =unsafe { bindings::mtree_insert_range (self.tree.get (), first, last, ptr, gfp.as_raw ()) - }); + } + .to_result ();if let Err (err) = res {// SAFETY: As `mtree_insert_range` failed, it is safe to take back ownership.
@@ -449,7 +450,7 @@ pub fn alloc_range<R>(let mut index =0 ;// SAFETY: The tree is valid, and we are passing a pointer to an owned instance of `T`. - let res = to_result(unsafe { +let res =unsafe { bindings::mtree_alloc_range ( self.tree.tree.get (), &mut index,
@@ -459,7 +460,8 @@ pub fn alloc_range<R>( max, gfp.as_raw(), ) - }); + } + .to_result ();if let Err (err) = res {// SAFETY: As `mtree_alloc_range` failed, it is safe to take back ownership.
rust/kernel/miscdevice.rs +4 -2
@@ -11,7 +11,7 @@use crate ::{ bindings, device::Device , - error::{to_result,Error ,Result ,VTABLE_DEFAULT_ERROR }, + error::{Error ,Result ,ToResult ,VTABLE_DEFAULT_ERROR }, ffi::{c_int, c_long, c_uint, c_ulong}, fs::{File ,Kiocb }, iov::{IovIterDest ,IovIterSource },
@@ -80,7 +80,9 @@ pub fn register(opts: MiscDeviceOptions) -> impl PinInit<Self, Error> {// the destructor of this type deallocates the memory. // INVARIANT: If this returns `Ok(())`, then the `slot` will contain a registered // misc device. -to_result (unsafe { bindings::misc_register (slot) }) +unsafe { bindings::misc_register (slot) }.to_result ()?; + +Ok ::<(),Error >(()) }), _t: PhantomData, })
rust/kernel/mm/virt.rs +4 -2
@@ -16,7 +16,7 @@use crate ::{ bindings, - error::{code::EINVAL , to_result,Result }, + error::{code::EINVAL ,Result ,ToResult }, mm::MmWithUser , page::Page , types::Opaque ,
@@ -194,7 +194,9 @@ pub unsafe fn from_raw<'a>(vma: *const bindings::vm_area_struct) -> &'a Self {pub fn vm_insert_page (&self, address:usize , page: &Page ) ->Result {// SAFETY: By the type invariant of `Self` caller has read access and has verified that // `VM_MIXEDMAP` is set. By invariant on `Page` the page has order 0. -to_result (unsafe { bindings::vm_insert_page (self.as_ptr (), address, page.as_ptr ()) }) +unsafe { bindings::vm_insert_page (self.as_ptr (), address, page.as_ptr ()) }.to_result ()?; + +Ok (()) } }
rust/kernel/net/phy.rs +30 -13
104 lines not on this page · raw message
rust/kernel/net/phy/reg.rs +13 -9
@@ -110,7 +110,7 @@ fn read(&self, dev: &mut Device) -> Result<u16> {let ret =unsafe { bindings::mdiobus_read ((*phydev).mdio.bus, (*phydev).mdio.addr, self.0 .into ()) }; -to_result (ret)?; + ret.to_result ()?;Ok (retas u16 ) }
@@ -119,9 +119,12 @@ fn write(&self, dev: &mut Device, val: u16) -> Result {// SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`. // So it's just an FFI call, open code of `phy_write()` with a valid `phy_device` pointer // `phydev`. -to_result (unsafe { +unsafe { bindings::mdiobus_write ((*phydev).mdio.bus, (*phydev).mdio.addr, self.0 .into (), val) - }) + } + .to_result ()?; + +Ok (()) }fn read_status (dev: &mut Device ) ->Result <u16 > {
@@ -129,7 +132,7 @@ fn read_status(dev: &mut Device) -> Result<u16> {// SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. // So it's just an FFI call. let ret =unsafe { bindings::genphy_read_status (phydev) }; -to_result (ret)?; + ret.to_result ()?;Ok (retas u16 ) } }
@@ -200,7 +203,7 @@ fn read(&self, dev: &mut Device) -> Result<u16> {// So it's just an FFI call. let ret =unsafe { bindings::phy_read_mmd (phydev, self.devad.0 .into (), self.regnum.into ()) }; -to_result (ret)?; + ret.to_result ()?;Ok (retas u16 ) }
@@ -208,9 +211,10 @@ fn write(&self, dev: &mut Device, val: u16) -> Result {let phydev = dev.0 .get ();// SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`. // So it's just an FFI call. -to_result (unsafe { - bindings::phy_write_mmd (phydev, self.devad.0 .into (), self.regnum.into (), val) - }) +unsafe { bindings::phy_write_mmd (phydev, self.devad.0 .into (), self.regnum.into (), val) } + .to_result ()?; + +Ok (()) }fn read_status (dev: &mut Device ) ->Result <u16 > {
@@ -218,7 +222,7 @@ fn read_status(dev: &mut Device) -> Result<u16> {// SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. // So it's just an FFI call. let ret =unsafe { bindings::genphy_c45_read_status (phydev) }; -to_result (ret)?; + ret.to_result ()?;Ok (retas u16 ) } }
rust/kernel/opp.rs +42 -30
180 lines not on this page · raw message
rust/kernel/pci.rs +8 -5
@@ -9,7 +9,7 @@ device_id::{RawDeviceId, RawDeviceIdIndex}, devres::Devres, driver, - error::{from_result, to_result, Result}, + error::{from_result, Result, ToResult}, io::{Io, IoRaw}, irq::{self, IrqRequest}, str::CStr,
@@ -50,9 +50,10 @@ unsafe fn register( }// SAFETY: `pdrv` is guaranteed to be a valid `RegType`. -to_result (unsafe { - bindings::__pci_register_driver (pdrv.get (), module.0 , name.as_char_ptr ()) - }) +unsafe { bindings::__pci_register_driver (pdrv.get (), module.0 , name.as_char_ptr ()) } + .to_result ()?; + +Ok (()) }unsafe fn unregister (pdrv: &Opaque <Self ::RegType >) {
@@ -582,7 +583,9 @@ impl Device<device::Core> {/// Enable memory resources for this device. pub fn enable_device_mem (&self) ->Result {// SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`. -to_result (unsafe { bindings::pci_enable_device_mem (self.as_raw ()) }) +unsafe { bindings::pci_enable_device_mem (self.as_raw ()) }.to_result ()?; + +Ok (()) }/// Enable bus-mastering for this device.
rust/kernel/platform.rs +4 -2
@@ -8,7 +8,7 @@ acpi, bindings, container_of, device::{self, Bound}, driver, - error::{from_result, to_result, Result}, + error::{from_result, Result, ToResult}, io::{mem::IoRequest, Resource}, irq::{self, IrqRequest}, of,
@@ -55,7 +55,9 @@ unsafe fn register( }// SAFETY: `pdrv` is guaranteed to be a valid `RegType`. -to_result (unsafe { bindings::__platform_driver_register (pdrv.get (), module.0 ) }) +unsafe { bindings::__platform_driver_register (pdrv.get (), module.0 ) }.to_result ()?; + +Ok (()) }unsafe fn unregister (pdrv: &Opaque <Self ::RegType >) {
rust/kernel/regulator.rs +22 -10
@@ -19,7 +19,7 @@use crate ::{ bindings, device::{Bound ,Device }, - error::{from_err_ptr, to_result,Result }, + error::{from_err_ptr,Result ,ToResult }, prelude::*, };
@@ -84,7 +84,9 @@ pub struct Error<State: RegulatorState> {pub fn devm_enable (dev: &Device <Bound >, name: &CStr ) ->Result {// SAFETY: `dev` is a valid and bound device, while `name` is a valid C // string. -to_result (unsafe { bindings::devm_regulator_get_enable (dev.as_raw (), name.as_ptr ()) }) +unsafe { bindings::devm_regulator_get_enable (dev.as_raw (), name.as_ptr ()) }.to_result ()?; + +Ok (()) }/// Same as [`devm_enable`], but calls `devm_regulator_get_enable_optional`
@@ -102,7 +104,10 @@ pub fn devm_enable(dev: &Device<Bound>, name: &CStr) -> Result {pub fn devm_enable_optional (dev: &Device <Bound >, name: &CStr ) ->Result {// SAFETY: `dev` is a valid and bound device, while `name` is a valid C // string. -to_result (unsafe { bindings::devm_regulator_get_enable_optional (dev.as_raw (), name.as_ptr ()) }) +unsafe { bindings::devm_regulator_get_enable_optional (dev.as_raw (), name.as_ptr ()) } + .to_result ()?; + +Ok (()) }/// A `struct regulator` abstraction.
@@ -248,21 +253,24 @@ impl<T: RegulatorState> Regulator<T> {/// This can be used to ensure that the device powers up cleanly. pub fn set_voltage (&self, min_voltage:Voltage , max_voltage:Voltage ) ->Result {// SAFETY: Safe as per the type invariants of `Regulator`. -to_result (unsafe { +unsafe { bindings::regulator_set_voltage ( self.inner.as_ptr (), min_voltage.as_microvolts (), max_voltage.as_microvolts (), ) - }) + } + .to_result ()?; + +Ok (()) }/// Gets the current voltage of the regulator. pub fn get_voltage (&self) ->Result <Voltage > {// SAFETY: Safe as per the type invariants of `Regulator`. -let voltage =unsafe { bindings::regulator_get_voltage (self.inner.as_ptr ()) }; - -to_result (voltage).map (|()|Voltage ::from_microvolts (voltage)) +unsafe { bindings::regulator_get_voltage (self.inner.as_ptr ()) } + .to_result () + .map (|v|Voltage ::from_microvolts (vas i32 )) }fn get_internal (dev: &Device , name: &CStr ) ->Result <Regulator <T >> {
@@ -282,12 +290,16 @@ fn get_internal(dev: &Device, name: &CStr) -> Result<Regulator<T>> {fn enable_internal (&self) ->Result {// SAFETY: Safe as per the type invariants of `Regulator`. -to_result (unsafe { bindings::regulator_enable (self.inner.as_ptr ()) }) +unsafe { bindings::regulator_enable (self.inner.as_ptr ()) }.to_result ()?; + +Ok (()) }fn disable_internal (&self) ->Result {// SAFETY: Safe as per the type invariants of `Regulator`. -to_result (unsafe { bindings::regulator_disable (self.inner.as_ptr ()) }) +unsafe { bindings::regulator_disable (self.inner.as_ptr ()) }.to_result ()?; + +Ok (()) } }
rust/kernel/scatterlist.rs +7 -6
@@ -34,7 +34,8 @@ bindings, device::{Bound, Device}, devres::Devres, - dma, error, + dma, + error::ToResult, io::resource::ResourceSize, page, prelude::*,
@@ -208,9 +209,8 @@ unsafe fn new(// - `dev.as_raw()` is a valid pointer to a `struct device`, which is guaranteed to be // bound to a driver for the duration of this call. // - `sgt` is a valid pointer to a `struct sg_table`. - error::to_result (unsafe { - bindings::dma_map_sgtable (dev.as_raw (), sgt.as_ptr (), dir.into (),0 ) - })?; +unsafe { bindings::dma_map_sgtable (dev.as_raw (), sgt.as_ptr (), dir.into (),0 ) } + .to_result ()?;// INVARIANT: By the safety requirements of this function it is guaranteed that `sgt` is // valid for the entire lifetime of this object instance.
@@ -273,7 +273,7 @@ unsafe fn new(// SAFETY: // - `sgt.get()` is a valid pointer to uninitialized memory. // - As by the check above, `pages` is not empty. - error::to_result (unsafe { +unsafe { bindings::sg_alloc_table_from_pages_segment ( sgt.get (), pages.as_mut_ptr(),
@@ -283,7 +283,8 @@ unsafe fn new( max_segment, flags.as_raw(), ) - })?; + } + .to_result ()?;Ok (Self (sgt)) }
rust/kernel/security.rs +15 -8
@@ -9,7 +9,7 @@use crate ::{ bindings, cred::Credential , - error::{to_result,Result }, + error::{Result ,ToResult }, fs::File , };
@@ -18,7 +18,9 @@#[inline] pub fn binder_set_context_mgr (mgr: &Credential ) ->Result {// SAFETY: `mrg.0` is valid because the shared reference guarantees a nonzero refcount. -to_result (unsafe { bindings::security_binder_set_context_mgr (mgr.as_ptr ()) }) +unsafe { bindings::security_binder_set_context_mgr (mgr.as_ptr ()) }.to_result ()?; + +Ok (()) }/// Calls the security modules to determine if binder transactions are allowed from task `from` to
@@ -26,7 +28,9 @@ pub fn binder_set_context_mgr(mgr: &Credential) -> Result {#[inline] pub fn binder_transaction (from: &Credential , to: &Credential ) ->Result {// SAFETY: `from` and `to` are valid because the shared references guarantee nonzero refcounts. -to_result (unsafe { bindings::security_binder_transaction (from.as_ptr (), to.as_ptr ()) }) +unsafe { bindings::security_binder_transaction (from.as_ptr (), to.as_ptr ()) }.to_result ()?; + +Ok (()) }/// Calls the security modules to determine if task `from` is allowed to send binder objects
@@ -34,7 +38,9 @@ pub fn binder_transaction(from: &Credential, to: &Credential) -> Result {#[inline] pub fn binder_transfer_binder (from: &Credential , to: &Credential ) ->Result {// SAFETY: `from` and `to` are valid because the shared references guarantee nonzero refcounts. -to_result (unsafe { bindings::security_binder_transfer_binder (from.as_ptr (), to.as_ptr ()) }) +unsafe { bindings::security_binder_transfer_binder (from.as_ptr (), to.as_ptr ()) }.to_result ()?; + +Ok (()) }/// Calls the security modules to determine if task `from` is allowed to send the given file to
@@ -43,9 +49,10 @@ pub fn binder_transfer_binder(from: &Credential, to: &Credential) -> Result {pub fn binder_transfer_file (from: &Credential , to: &Credential , file: &File ) ->Result {// SAFETY: `from`, `to` and `file` are valid because the shared references guarantee nonzero // refcounts. -to_result (unsafe { - bindings::security_binder_transfer_file (from.as_ptr (), to.as_ptr (), file.as_ptr ()) - }) +unsafe { bindings::security_binder_transfer_file (from.as_ptr (), to.as_ptr (), file.as_ptr ()) } + .to_result ()?; + +Ok (()) }/// A security context string.
@@ -66,7 +73,7 @@ pub fn from_secid(secid: u32) -> Result<Self> {let mut ctx: bindings::lsm_context =unsafe { core::mem::zeroed () };// SAFETY: Just a C FFI call. The pointer is valid for writes. -to_result (unsafe { bindings::security_secid_to_secctx (secid, &mut ctx) })?; +unsafe { bindings::security_secid_to_secctx (secid, &mut ctx) }.to_result ()?;// INVARIANT: If the above call did not fail, then we have a valid security context. Ok (Self { ctx })
rust/kernel/str.rs +3 -2
@@ -4,7 +4,7 @@use crate ::{ alloc::{flags::*,AllocError ,KVec }, - error::{to_result,Result }, + error::{Result ,ToResult }, fmt::{self,Write }, prelude::*, };
@@ -929,7 +929,8 @@ unsafe fn kstrtobool_raw(string: *const u8) -> Result<bool> {// SAFETY: // - By function safety requirement, `string` is a valid null-terminated string. // - `result` is a valid `bool` that we own. -to_result (unsafe { bindings::kstrtobool (string, &mut result) })?; +unsafe { bindings::kstrtobool (string, &mut result) }.to_result ()?; +Ok (result) }
rust/kernel/uaccess.rs +6 -9
@@ -7,7 +7,7 @@use crate ::{ alloc::{Allocator ,Flags }, bindings, - error::Result , + error::{Result ,ToResult }, ffi::{c_char, c_void}, prelude::*, transmute::{AsBytes ,FromBytes },
@@ -483,26 +483,23 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {/// initialized and non-zero. Furthermore, if `len < dst.len()`, then `dst[len]` is a NUL byte. #[inline] fn raw_strncpy_from_user (dst: &mut [MaybeUninit <u8 >], src:UserPtr ) ->Result <usize > { -// CAST: Slice lengths are guaranteed to be `<= isize::MAX`. -let len = dst.len ()as isize ; +let len = dst.len ();// SAFETY: `dst` is valid for writing `dst.len()` bytes. let res =unsafe { bindings::strncpy_from_user ( dst.as_mut_ptr ().cast ::<c_char >(), src.as_const_ptr ().cast ::<c_char >(), - len, +// CAST: Slice lengths are guaranteed to be `<= isize::MAX`. + lenas isize , ) - }; - -if res <0 { -return Err (Error ::from_errno (resas i32 )); } + .to_result ()?;#[cfg( CONFIG_RUST_OVERFLOW_CHECKS )] assert! (res <= len);// GUARANTEES: `strncpy_from_user` was successful, so `dst` has contents in accordance with the // guarantees of this function. -Ok (resas usize ) +Ok (res) }
rust/kernel/usb.rs +5 -4
@@ -9,7 +9,7 @@ bindings, device, device_id::{RawDeviceId, RawDeviceIdIndex}, driver, - error::{from_result, to_result, Result}, + error::{from_result, Result, ToResult}, prelude::*, str::CStr, types::{AlwaysRefCounted, Opaque},
@@ -39,9 +39,10 @@ unsafe fn register( }// SAFETY: `udrv` is guaranteed to be a valid `RegType`. -to_result (unsafe { - bindings::usb_register_driver (udrv.get (), module.0 , name.as_char_ptr ()) - }) +unsafe { bindings::usb_register_driver (udrv.get (), module.0 , name.as_char_ptr ()) } + .to_result ()?; + +Ok (()) }unsafe fn unregister (udrv: &Opaque <Self ::RegType >) {
Signature · 1 line
2.51.0
Onur Özkan
reviewerv3 cover rust: refactor `to_result`
Changes in v3:
- New trait error::ToResult added and implemented it on isize and i32 types (first patch).
- Removed error::to_result entirely and replaced its usage with error::ToResult (second patch).
Changes in v2:
- Removed map(|_| ()) calls from v1 and replaced them with
`to_result(...)?` and `Ok(())` (except miscdevice.rs, as it
required `Ok::<(), Error>(())` which is less clean).- Rebased on latest regulator/for-next and fixed the build error.
Onur Özkan (2):
rust: add ToResult trait
rust: drop error::to_result and utilize ToResult
drivers/android/binder/rust_binder_main.rs | 3 +- rust/kernel/auxiliary.rs | 9 +- rust/kernel/block/mq/gen_disk.rs | 12 +-- rust/kernel/block/mq/tag_set.rs | 5 +- rust/kernel/clk.rs | 18 +++- rust/kernel/configfs.rs | 9 +- rust/kernel/cpufreq.rs | 12 ++- rust/kernel/device/property.rs | 12 +-- rust/kernel/devres.rs | 13 ++- rust/kernel/dma.rs | 18 ++-- rust/kernel/drm/driver.rs | 4 +- rust/kernel/drm/gem/mod.rs | 12 ++- rust/kernel/error.rs | 113 +++++++++++++-------- rust/kernel/fs/file.rs | 4 +- rust/kernel/irq/request.rs | 10 +- rust/kernel/maple_tree.rs | 12 ++- rust/kernel/miscdevice.rs | 6 +- rust/kernel/mm/virt.rs | 6 +- rust/kernel/net/phy.rs | 43 +++++--- rust/kernel/net/phy/reg.rs | 22 ++-- rust/kernel/opp.rs | 72 +++++++------ rust/kernel/pci.rs | 13 ++- rust/kernel/platform.rs | 6 +- rust/kernel/regulator.rs | 32 ++++-- rust/kernel/scatterlist.rs | 13 +-- rust/kernel/security.rs | 23 +++-- rust/kernel/str.rs | 5 +- rust/kernel/uaccess.rs | 15 ++- rust/kernel/usb.rs | 9 +- 29 files changed, 324 insertions(+), 207 deletions(-)
Signature · 1 line
2.51.0
Onur Özkan
reviewerPATCH v3 1/2 rust: add `ToResult` trait
Adds ToResult trait to handle integer return values from C kernel functions.
Example:
let _value = unsafe { bindings::foo() }.to_result()?;This will replace the existing error::to_result function, but it will be handled in the next commit to keep the diff more readable.
--- rust/kernel/error.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+)
rust/kernel/error.rs +74 -0
@@ -392,6 +392,80 @@ fn from(e: core::convert::Infallible) -> Error {/// [Rust documentation]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html pub type Result <T = (),E =Error > = core::result::Result <T ,E >; +/// Trait for handling integer return values from C kernel functions by converting +/// them into idiomatic Rust [`Result`]s. +pub trait ToResult { +/// The unsigned version of the integer type used for successful return values. +type Unsigned ; + +/// Converts an integer as returned by a C kernel function to a [`Result`]. +/// +/// If the integer is negative, an [`Err`] with an [`Error`] as given by [`Error::from_errno`] +/// is returned. This means the integer must be `>= -MAX_ERRNO`. +/// +/// Otherwise, it returns the original value as an unsigned integer. +/// +/// It is a bug to pass an out-of-range negative integer. `Err(EINVAL)` is returned +/// in such a case. +/// +/// # Examples +/// +/// This function may be used to easily perform early returns with the [`?`] operator +/// when working with C APIs within Rust abstractions: +/// +/// ``` +/// # use kernel::error::ToResult; +/// # mod bindings { +/// # #![expect(clippy::missing_safety_doc)] +/// # use kernel::prelude::*; +/// # pub(super) unsafe fn f1() -> c_int { 0 } +/// # pub(super) unsafe fn f2() -> c_int { EINVAL.to_errno() } +/// # } +/// fn f() -> Result { +/// // SAFETY: ... +/// let _value = unsafe { bindings::f1() }.to_result()?; +/// +/// // SAFETY: ... +/// let _value = unsafe { bindings::f2() }.to_result()?; +/// +/// // ... +/// +/// Ok(()) +/// } +/// # assert_eq!(f(), Err(EINVAL)); +/// ``` +/// +/// [`?`]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator +fn to_result (self) ->Result <Self ::Unsigned >; +} + +impl ToResult for i32 { +type Unsigned =u32 ; + +fn to_result (self) ->Result <Self ::Unsigned > { +if self <0 { +Err (Error ::from_errno (self)) + }else { +Ok (selfas u32 ) + } + } +} + +impl ToResult for isize { +type Unsigned =usize ; + +fn to_result (self) ->Result <Self ::Unsigned > { +// Try casting into `i32`. +let casted:crate ::ffi::c_int = self.try_into ().map_err (|_| code::EINVAL )?; + +if casted <0 { +Err (Error ::from_errno (casted)) + }else { +Ok (selfas usize ) + } + } +} +/// Converts an integer as returned by a C kernel function to a [`Result`]. /// /// If the integer is negative, an [`Err`] with an [`Error`] as given by [`Error::from_errno`] is
Signature · 1 line
2.51.0
Onur Özkan
reviewerRe: [PATCH v3 1/2] rust: add `ToResult` trait
Quoted 97 lines · On Mon, 13 Oct 2025 15:41:38 +0300 Onur Özkan <[email protected]> wrote:
Adds
ToResulttrait to handle integer return values from C kernel functions.Example:
let _value = unsafe { bindings::foo() }.to_result()?;This will replace the existing
error::to_resultfunction, but it will be handled in the next commit to keep the diff more readable.Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/error.rs | 74++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+)
rust/kernel/error.rs +0
-0@@ -392,6 +392,80 @@ fn from(e: core::convert::Infallible) -> Error {/// [Rust documentation]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html
pub type Result<T = (), E = Error> = core::result::Result<T, E>; +/// Trait for handling integer return values from C kernel functions by converting +/// them into idiomatic Rust [Result]s.rust/kernel/error.rs +2
-0+pub trait ToResult { +/// The unsigned version of the integer type used for successful return values.
rust/kernel/error.rs +3
-0+type Unsigned ; + +/// Converts an integer as returned by a C kernel function to a [
Result].rust/kernel/error.rs +2
-0+/// +/// If the integer is negative, an [`Err`] with an [`Error`] as given by [
Error::from_errno]rust/kernel/error.rs +3
-0+/// is returned. This means the integer must be `>= -MAX_ERRNO`. +/// +/// Otherwise, it returns the original value as an unsigned integer.
rust/kernel/error.rs +2
-0+/// +/// It is a bug to pass an out-of-range negative integer.
Err(EINVAL)is returnedrust/kernel/error.rs +5
-0+/// in such a case. +/// +/// # Examples +/// +/// This function may be used to easily perform early returns with the [
?] operatorrust/kernel/error.rs +9
-0+/// when working with C APIs within Rust abstractions: +/// +/// ``` +/// # use kernel::error::ToResult; +/// # mod bindings { +/// # #![expect(clippy::missing_safety_doc)] +/// # use kernel::prelude::*; +/// # pub(super) unsafe fn f1() -> c_int { 0 } +/// # pub(super) unsafe fn f2() -> c_int { EINVAL.to_errno() }
rust/kernel/error.rs +16
-0+/// # } +/// fn f() -> Result { +/// // SAFETY: ... +/// let _value = unsafe { bindings::f1() }.to_result()?; +/// +/// // SAFETY: ... +/// let _value = unsafe { bindings::f2() }.to_result()?; +/// +/// // ... +/// +/// Ok(()) +/// } +/// # assert_eq!(f(), Err(EINVAL)); +/// ``` +/// +/// [`?`]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator
rust/kernel/error.rs +21
-0+fn to_result (self) ->Result <Self ::Unsigned >; +} + +impl ToResult for i32 { +type Unsigned =u32 ; + +fn to_result (self) ->Result <Self ::Unsigned > { +if self <0 { +Err (Error ::from_errno (self)) + }else { +Ok (selfas u32 ) + } + } +} + +impl ToResult for isize { +type Unsigned =usize ; + +fn to_result (self) ->Result <Self ::Unsigned > { +// Try casting into `i32`. + let casted: crate::ffi::c_int = self.try_into().map_err(|_|
Quoted 1 line
code::EINVAL)?; +
I should do this casting only when self < 0, will fix it later in the next version.
Quoted 12 lines
rust/kernel/error.rs +8
-0+if casted <0 { +Err (Error ::from_errno (casted)) + }else { +Ok (selfas usize ) + } + } +} +/// Converts an integer as returned by a C kernel function to a [
Result]. ///
/// If the integer is negative, an [Err] with an [Error] as
given by [Error::from_errno] is
Re: [PATCH v3 2/2] rust: drop `error::to_result` and utilize `ToResult`
In reply to Onur ÖzkanQuoted 2 lines · Onur Özkan
29 files changed, 253 insertions(+), 210 deletions(-)
Something like this will be hard to merge if we wait for all Acked-bys. It is best to do the migration over time and split it per subsystem.
Also, you should Cc all the relevant maintainers/reviewers -- you are touching quite a few subsystems. :)
Quoted 4 lines
I haven't included all the improvements made possible by the new design since that could conflict with other ongoing patches [2]. Once this patch is approved and applied, I am planning to follow up with creating a "good first issue" on [3] for those additional changes.
I would suggest including a few examples of the improvements in an RFC patch on top. In other words, we need to show the improvements (or at least some key examples) to justify a change, rather than doing it later.
For instance, with the changes in this patch, I see a lot of Ok(()) added because now one needs to discard the value, which isn't great... It indicates that the common case may not be keeping the value, and thus we may want two APIs for this.
Thus, similarly, we may be able to spot a similar pattern in those examples too.
Thanks!
Cheers,
Miguel
Alice Ryhl
maintainerRe: [PATCH v3 2/2] rust: drop `error::to_result` and utilize `ToResult`
In reply to Onur ÖzkanQuoted 55 lines · Onur Özkan
Current
to_resulthelper takes ac_intand returnsOk(())on success and this has some issues like:- Callers lose the original return value and often have to store it in a temporary variable before calling
to_result.- It only supports
c_int, which makes callers to unnecessarily cast when working with other types (e.g.u16in phy abstractions). We even have some places that ignore to useto_resulthelper because the input doesn't fit inc_int(see [1]).[1]: https://lore.kernel.org/all/[email protected]/
This patch removes the
error::to_resultfunction and replaces it with a more advanced helper,ToResulttrait. This change brings three main benefits:- A better calling convention. Instead of wrapping function calls with
to_result, we can now call.to_result()directly as an extension function.- The returned value is preserved as an unsigned integer on success which was previously discarded by
error::to_result.- It's no longer limited with a single input type. E.g., right now we can call
to_resulton isize without manually casting them into i32.So that the code that previously looked like:
let ret = unsafe { bindings::some_ffi_call() }; to_result(ret).map(|()| SomeType::new(ret))can now be written more directly as:
unsafe {
bindings::some_ffi_call()}.to_result()
.map(SomeType::new)Similarly, code such as:
let res: isize = unsafe { bindings::some_ffi_call() }; if res < 0 { return Err(Error::from_errno(res as i32)); }can be done without manually casting into i32:
unsafe { bindings::some_ffi_call() }.to_result()?;This patch only fixes the callers that broke after changes on
to_result. I haven't included all the improvements made possible by the new design since that could conflict with other ongoing patches [2]. Once this patch is approved and applied, I am planning to follow up with creating a "good first issue" on [3] for those additional changes.
Quoted 5 lines
[2]: https://lore.kernel.org/rust-for-linux/?q=to_result
[3]: https://github.com/Rust-for-Linux/linux
I think we should keep the function call syntax and just change its return type based on the new trait.
Or maybe even introduce a new method to keep the existing method that returns Result<()>. Many functions only return 0 or an error, and I don't want to map from int to () either.
Alice
Onur Özkan
reviewerRe: [PATCH v3 2/2] rust: drop `error::to_result` and utilize `ToResult`
In reply to Miguel OjedaQuoted 7 lines · On Mon, 13 Oct 2025 19:04:09 +0200 Miguel Ojeda…
Quoted 2 lines · Onur Özkan
29 files changed, 253 insertions(+), 210 deletions(-)Something like this will be hard to merge if we wait for all Acked-bys. It is best to do the migration over time and split it per subsystem.
Sure. To do that, I need to keep the current error::to_result untouched. I guess that would be fine.
Quoted 3 lines
Also, you should Cc all the relevant maintainers/reviewers -- you are touching quite a few subsystems. :)
Right, sorry.
Quoted 11 lines
Quoted 5 lines
I haven't included all the improvements made possible by the new design since that could conflict with other ongoing patches [2]. Once this patch is approved and applied, I am planning to follow up with creating a "good first issue" on [3] for those additional changes.
I would suggest including a few examples of the improvements in an RFC patch on top. In other words, we need to show the improvements (or at least some key examples) to justify a change, rather than doing it later.
Quoted 8 lines
For instance, with the changes in this patch, I see a lot of
Ok(())added because now one needs to discard the value, which isn't great... It indicates that the common case may not be keeping the value, and thus we may want two APIs for this.Thus, similarly, we may be able to spot a similar pattern in those examples too.
Thank you for your feedback Miguel. I will wait sometime to collect more feedback and then I will prepare an RFC based on all the input. I would feel more confident sending the RFC after receiving more feedback from more Rust kernel developers.
-Onur
Quoted 4 lines
Thanks!
Cheers,
Miguel