Skip to content

Commit d2b8246

Browse files
authored
Merge branch 'rust' into macros
2 parents 36bb99a + b3e8654 commit d2b8246

15 files changed

+394
-25
lines changed

rust/helpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ void rust_helper___INIT_WORK_WITH_KEY(struct work_struct *work,
627627
}
628628
EXPORT_SYMBOL_GPL(rust_helper___INIT_WORK_WITH_KEY);
629629

630+
struct dentry *rust_helper_dget(struct dentry *dentry)
631+
{
632+
return dget(dentry);
633+
}
634+
EXPORT_SYMBOL_GPL(rust_helper_dget);
635+
630636
/*
631637
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
632638
* as the Rust `usize` type, so we can use it in contexts where Rust

rust/kernel/amba.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
9595
}
9696
// SAFETY: By the safety requirements of this function, `reg` is valid and fully
9797
// initialised.
98-
to_result(|| unsafe { bindings::amba_driver_register(reg) })
98+
to_result(unsafe { bindings::amba_driver_register(reg) })
9999
}
100100

101101
unsafe fn unregister(reg: *mut bindings::amba_driver) {

rust/kernel/clk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Clk {
3535
/// This function should not be called in atomic context.
3636
pub fn prepare_enable(self) -> Result<EnabledClk> {
3737
// SAFETY: The pointer is valid by the type invariant.
38-
to_result(|| unsafe { bindings::clk_prepare_enable(self.0) })?;
38+
to_result(unsafe { bindings::clk_prepare_enable(self.0) })?;
3939
Ok(EnabledClk(self))
4040
}
4141
}

rust/kernel/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,9 @@ pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
551551
Ok(ptr)
552552
}
553553

554-
/// Calls a kernel function that returns an integer error code on failure and converts the result
555-
/// to a [`Result`].
556-
pub fn to_result(func: impl FnOnce() -> core::ffi::c_int) -> Result {
557-
let err = func();
554+
/// Converts an integer as returned by a C kernel function to an error if it's negative, and
555+
/// `Ok(())` otherwise.
556+
pub fn to_result(err: core::ffi::c_int) -> Result {
558557
if err < 0 {
559558
Err(Error::from_kernel_errno(err))
560559
} else {

rust/kernel/fs.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! File systems.
4+
//!
5+
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h)
6+
7+
use crate::{bindings, AlwaysRefCounted};
8+
use core::{cell::UnsafeCell, ptr};
9+
10+
/// Wraps the kernel's `struct inode`.
11+
///
12+
/// # Invariants
13+
///
14+
/// Instances of this type are always ref-counted, that is, a call to `ihold` ensures that the
15+
/// allocation remains valid at least until the matching call to `iput`.
16+
#[repr(transparent)]
17+
pub struct INode(pub(crate) UnsafeCell<bindings::inode>);
18+
19+
// SAFETY: The type invariants guarantee that `INode` is always ref-counted.
20+
unsafe impl AlwaysRefCounted for INode {
21+
fn inc_ref(&self) {
22+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
23+
unsafe { bindings::ihold(self.0.get()) };
24+
}
25+
26+
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
27+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
28+
unsafe { bindings::iput(obj.cast().as_ptr()) }
29+
}
30+
}
31+
32+
/// Wraps the kernel's `struct dentry`.
33+
///
34+
/// # Invariants
35+
///
36+
/// Instances of this type are always ref-counted, that is, a call to `dget` ensures that the
37+
/// allocation remains valid at least until the matching call to `dput`.
38+
#[repr(transparent)]
39+
pub struct DEntry(pub(crate) UnsafeCell<bindings::dentry>);
40+
41+
// SAFETY: The type invariants guarantee that `DEntry` is always ref-counted.
42+
unsafe impl AlwaysRefCounted for DEntry {
43+
fn inc_ref(&self) {
44+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
45+
unsafe { bindings::dget(self.0.get()) };
46+
}
47+
48+
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
49+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
50+
unsafe { bindings::dput(obj.cast().as_ptr()) }
51+
}
52+
}

rust/kernel/hwrng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<T: Operations> Registration<T> {
105105
);
106106

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

110110
this.registered = true;
111111
this.name = Some(name);

0 commit comments

Comments
 (0)