Skip to content

Commit

Permalink
Merge pull request raspberrypi#813 from wedsonaf/to_result
Browse files Browse the repository at this point in the history
rust: `to_result` only needs the return code
  • Loading branch information
ojeda authored Jul 5, 2022
2 parents 1ae6be8 + 9016fef commit 156dcc5
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion rust/kernel/amba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
}
// SAFETY: By the safety requirements of this function, `reg` is valid and fully
// initialised.
to_result(|| unsafe { bindings::amba_driver_register(reg) })
to_result(unsafe { bindings::amba_driver_register(reg) })
}

unsafe fn unregister(reg: *mut bindings::amba_driver) {
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/clk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Clk {
/// This function should not be called in atomic context.
pub fn prepare_enable(self) -> Result<EnabledClk> {
// SAFETY: The pointer is valid by the type invariant.
to_result(|| unsafe { bindings::clk_prepare_enable(self.0) })?;
to_result(unsafe { bindings::clk_prepare_enable(self.0) })?;
Ok(EnabledClk(self))
}
}
Expand Down
7 changes: 3 additions & 4 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,9 @@ pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
Ok(ptr)
}

/// Calls a kernel function that returns an integer error code on failure and converts the result
/// to a [`Result`].
pub fn to_result(func: impl FnOnce() -> core::ffi::c_int) -> Result {
let err = func();
/// 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_kernel_errno(err))
} else {
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/hwrng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<T: Operations> Registration<T> {
);

// SAFETY: `bindings::hwrng` is initialized above which guarantees safety.
to_result(|| unsafe { bindings::hwrng_register(this.hwrng.get()) })?;
to_result(unsafe { bindings::hwrng_register(this.hwrng.get()) })?;

this.registered = true;
this.name = Some(name);
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl<T: PointerWrapper> InternalRegistration<T> {
unsafe { T::from_pointer(ptr) };
});
// SAFETY: `name` and `ptr` remain valid as long as the registration is alive.
to_result(|| unsafe {
to_result(unsafe {
bindings::request_threaded_irq(
irq,
handler,
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod virt {
// SAFETY: The page is guaranteed to be order 0 by the type system. The range of
// `address` is already checked by `vm_insert_page`. `self.vma` and `page.pages` are
// guaranteed by their repective type invariants to be valid.
to_result(|| unsafe { bindings::vm_insert_page(self.vma, address as _, page.pages) })
to_result(unsafe { bindings::vm_insert_page(self.vma, address as _, page.pages) })
}
}

Expand Down
8 changes: 4 additions & 4 deletions rust/kernel/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl TcpListener {
};

// SAFETY: The namespace is valid and the output socket pointer is valid for write.
to_result(|| unsafe {
to_result(unsafe {
bindings::sock_create_kern(
ns.0.get(),
pf as _,
Expand All @@ -275,10 +275,10 @@ impl TcpListener {

// SAFETY: The type invariant guarantees that the socket is valid, and `addr` and `addrlen`
// were initialised based on valid values provided in the address enum.
to_result(|| unsafe { bindings::kernel_bind(socket, addr, addrlen as _) })?;
to_result(unsafe { bindings::kernel_bind(socket, addr, addrlen as _) })?;

// SAFETY: The socket is valid per the type invariant.
to_result(|| unsafe { bindings::kernel_listen(socket, bindings::SOMAXCONN as _) })?;
to_result(unsafe { bindings::kernel_listen(socket, bindings::SOMAXCONN as _) })?;

Ok(listener)
}
Expand All @@ -295,7 +295,7 @@ impl TcpListener {
let flags = if block { 0 } else { bindings::O_NONBLOCK };
// SAFETY: The type invariant guarantees that the socket is valid, and the output argument
// is also valid for write.
to_result(|| unsafe { bindings::kernel_accept(self.sock, &mut new, flags as _) })?;
to_result(unsafe { bindings::kernel_accept(self.sock, &mut new, flags as _) })?;
Ok(TcpStream { sock: new })
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/net/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<T: Filter> Registration<T> {

// SAFETY: `ns` has a valid reference to the namespace, and `this.hook` was just
// initialised above, so they're both valid.
to_result(|| unsafe { bindings::nf_register_net_hook(ns.0.get(), &this.hook) })?;
to_result(unsafe { bindings::nf_register_net_hook(ns.0.get(), &this.hook) })?;

this.dev = dev;
this.ns = Some(ns);
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
// - `probe()` and `remove()` are static functions.
// - `of_match_table` is either a raw pointer with static lifetime,
// as guaranteed by the [`driver::IdTable`] type, or null.
to_result(|| unsafe { bindings::__platform_driver_register(reg, module.0) })
to_result(unsafe { bindings::__platform_driver_register(reg, module.0) })
}

unsafe fn unregister(reg: *mut bindings::platform_driver) {
Expand Down
8 changes: 4 additions & 4 deletions rust/kernel/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ use crate::{bindings, cred::Credential, file::File, to_result, Result};
/// context.
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.0.get()) })
to_result(unsafe { bindings::security_binder_set_context_mgr(mgr.0.get()) })
}

/// Calls the security modules to determine if binder transactions are allowed from task `from` to
/// task `to`.
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.0.get(), to.0.get()) })
to_result(unsafe { bindings::security_binder_transaction(from.0.get(), to.0.get()) })
}

/// Calls the security modules to determine if task `from` is allowed to send binder objects
/// (owned by itself or other processes) to task `to` through a binder transaction.
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.0.get(), to.0.get()) })
to_result(unsafe { bindings::security_binder_transfer_binder(from.0.get(), to.0.get()) })
}

/// Calls the security modules to determine if task `from` is allowed to send the given file to
/// task `to` (which would get its own file descriptor) through a binder transaction.
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 {
to_result(unsafe {
bindings::security_binder_transfer_file(from.0.get(), to.0.get(), file.0.get())
})
}

0 comments on commit 156dcc5

Please sign in to comment.