Skip to content

Commit e72fdb3

Browse files
committed
rust: use the generated helper bindings
Signed-off-by: Gary Guo <[email protected]>
1 parent 0865643 commit e72fdb3

15 files changed

+52
-195
lines changed

rust/helpers.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ unsigned long rust_helper_clear_user(void __user *to, unsigned long n)
3232
return clear_user(to, n);
3333
}
3434

35-
void rust_helper_spin_lock_init(spinlock_t *lock, const char *name,
35+
void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
3636
struct lock_class_key *key)
3737
{
3838
#ifdef CONFIG_DEBUG_SPINLOCK
@@ -41,7 +41,7 @@ void rust_helper_spin_lock_init(spinlock_t *lock, const char *name,
4141
spin_lock_init(lock);
4242
#endif
4343
}
44-
EXPORT_SYMBOL_GPL(rust_helper_spin_lock_init);
44+
EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
4545

4646
void rust_helper_spin_lock(spinlock_t *lock)
4747
{
@@ -103,22 +103,23 @@ size_t rust_helper_copy_to_iter(const void *addr, size_t bytes, struct iov_iter
103103
}
104104
EXPORT_SYMBOL_GPL(rust_helper_copy_to_iter);
105105

106-
bool rust_helper_is_err(__force const void *ptr)
106+
bool rust_helper_IS_ERR(__force const void *ptr)
107107
{
108108
return IS_ERR(ptr);
109109
}
110-
EXPORT_SYMBOL_GPL(rust_helper_is_err);
110+
EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
111111

112-
long rust_helper_ptr_err(__force const void *ptr)
112+
long rust_helper_PTR_ERR(__force const void *ptr)
113113
{
114114
return PTR_ERR(ptr);
115115
}
116-
EXPORT_SYMBOL_GPL(rust_helper_ptr_err);
116+
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
117117

118118
const char *rust_helper_errname(int err)
119119
{
120120
return errname(err);
121121
}
122+
EXPORT_SYMBOL_GPL(rust_helper_errname);
122123

123124
void rust_helper_mutex_lock(struct mutex *lock)
124125
{
@@ -141,11 +142,11 @@ rust_helper_platform_set_drvdata(struct platform_device *pdev,
141142
}
142143
EXPORT_SYMBOL_GPL(rust_helper_platform_set_drvdata);
143144

144-
refcount_t rust_helper_refcount_new(void)
145+
refcount_t rust_helper_REFCOUNT_INIT(int n)
145146
{
146-
return (refcount_t)REFCOUNT_INIT(1);
147+
return (refcount_t)REFCOUNT_INIT(n);
147148
}
148-
EXPORT_SYMBOL_GPL(rust_helper_refcount_new);
149+
EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
149150

150151
void rust_helper_refcount_inc(refcount_t *r)
151152
{

rust/kernel/error.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,8 @@ impl Error {
167167

168168
impl fmt::Debug for Error {
169169
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170-
extern "C" {
171-
fn rust_helper_errname(err: c_types::c_int) -> *const c_types::c_char;
172-
}
173170
// SAFETY: FFI call.
174-
let name = unsafe { rust_helper_errname(-self.0) };
171+
let name = unsafe { bindings::errname(-self.0) };
175172

176173
if name.is_null() {
177174
// Print out number if no name can be found.
@@ -268,7 +265,7 @@ where
268265
/// ) -> c_types::c_int {
269266
/// from_kernel_result! {
270267
/// let ptr = devm_alloc(pdev)?;
271-
/// rust_helper_platform_set_drvdata(pdev, ptr);
268+
/// bindings::platform_set_drvdata(pdev, ptr);
272269
/// Ok(0)
273270
/// }
274271
/// }
@@ -312,28 +309,20 @@ macro_rules! from_kernel_result {
312309
// TODO: remove `dead_code` marker once an in-kernel client is available.
313310
#[allow(dead_code)]
314311
pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
315-
extern "C" {
316-
#[allow(improper_ctypes)]
317-
fn rust_helper_is_err(ptr: *const c_types::c_void) -> bool;
318-
319-
#[allow(improper_ctypes)]
320-
fn rust_helper_ptr_err(ptr: *const c_types::c_void) -> c_types::c_long;
321-
}
322-
323312
// CAST: casting a pointer to `*const c_types::c_void` is always valid.
324313
let const_ptr: *const c_types::c_void = ptr.cast();
325314
// SAFETY: the FFI function does not deref the pointer.
326-
if unsafe { rust_helper_is_err(const_ptr) } {
315+
if unsafe { bindings::IS_ERR(const_ptr) } {
327316
// SAFETY: the FFI function does not deref the pointer.
328-
let err = unsafe { rust_helper_ptr_err(const_ptr) };
329-
// CAST: if `rust_helper_is_err()` returns `true`,
330-
// then `rust_helper_ptr_err()` is guaranteed to return a
317+
let err = unsafe { bindings::PTR_ERR(const_ptr) };
318+
// CAST: if `IS_ERR()` returns `true`,
319+
// then `PTR_ERR()` is guaranteed to return a
331320
// negative value greater-or-equal to `-bindings::MAX_ERRNO`,
332321
// which always fits in an `i16`, as per the invariant above.
333322
// And an `i16` always fits in an `i32`. So casting `err` to
334323
// an `i32` can never overflow, and is always valid.
335324
//
336-
// SAFETY: `rust_helper_is_err()` ensures `err` is a
325+
// SAFETY: `IS_ERR()` ensures `err` is a
337326
// negative value greater-or-equal to `-bindings::MAX_ERRNO`
338327
return Err(unsafe { Error::from_kernel_errno_unchecked(err as i32) });
339328
}

rust/kernel/iov_iter.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,12 @@
55
//! C header: [`include/linux/uio.h`](../../../../include/linux/uio.h)
66
77
use crate::{
8-
bindings, c_types,
8+
bindings,
99
error::Error,
1010
io_buffer::{IoBufferReader, IoBufferWriter},
1111
Result,
1212
};
1313

14-
extern "C" {
15-
fn rust_helper_copy_to_iter(
16-
addr: *const c_types::c_void,
17-
bytes: usize,
18-
i: *mut bindings::iov_iter,
19-
) -> usize;
20-
21-
fn rust_helper_copy_from_iter(
22-
addr: *mut c_types::c_void,
23-
bytes: usize,
24-
i: *mut bindings::iov_iter,
25-
) -> usize;
26-
}
27-
2814
/// Wraps the kernel's `struct iov_iter`.
2915
///
3016
/// # Invariants
@@ -70,7 +56,7 @@ impl IoBufferWriter for IovIter {
7056
}
7157

7258
unsafe fn write_raw(&mut self, data: *const u8, len: usize) -> Result {
73-
let res = unsafe { rust_helper_copy_to_iter(data as _, len, self.ptr) };
59+
let res = unsafe { bindings::copy_to_iter(data as _, len, self.ptr) };
7460
if res != len {
7561
Err(Error::EFAULT)
7662
} else {
@@ -85,7 +71,7 @@ impl IoBufferReader for IovIter {
8571
}
8672

8773
unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result {
88-
let res = unsafe { rust_helper_copy_from_iter(out as _, len, self.ptr) };
74+
let res = unsafe { bindings::copy_from_iter(out as _, len, self.ptr) };
8975
if res != len {
9076
Err(Error::EFAULT)
9177
} else {

rust/kernel/pages.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ use crate::{
1010
};
1111
use core::{marker::PhantomData, ptr};
1212

13-
extern "C" {
14-
#[allow(improper_ctypes)]
15-
fn rust_helper_alloc_pages(
16-
gfp_mask: bindings::gfp_t,
17-
order: c_types::c_uint,
18-
) -> *mut bindings::page;
19-
20-
#[allow(improper_ctypes)]
21-
fn rust_helper_kmap(page: *mut bindings::page) -> *mut c_types::c_void;
22-
23-
#[allow(improper_ctypes)]
24-
fn rust_helper_kunmap(page: *mut bindings::page);
25-
}
26-
2713
/// A set of physical pages.
2814
///
2915
/// `Pages` holds a reference to a set of pages of order `ORDER`. Having the order as a generic
@@ -42,7 +28,7 @@ impl<const ORDER: u32> Pages<ORDER> {
4228
// TODO: Consider whether we want to allow callers to specify flags.
4329
// SAFETY: This only allocates pages. We check that it succeeds in the next statement.
4430
let pages = unsafe {
45-
rust_helper_alloc_pages(
31+
bindings::alloc_pages(
4632
bindings::GFP_KERNEL | bindings::__GFP_ZERO | bindings::__GFP_HIGHMEM,
4733
ORDER,
4834
)
@@ -141,7 +127,7 @@ impl<const ORDER: u32> Pages<ORDER> {
141127
let page = unsafe { self.pages.add(index) };
142128

143129
// SAFETY: `page` is valid based on the checks above.
144-
let ptr = unsafe { rust_helper_kmap(page) };
130+
let ptr = unsafe { bindings::kmap(page) };
145131
if ptr.is_null() {
146132
return None;
147133
}
@@ -171,6 +157,6 @@ impl Drop for PageMapping<'_> {
171157
fn drop(&mut self) {
172158
// SAFETY: An instance of `PageMapping` is created only when `kmap` succeeded for the given
173159
// page, so it is safe to unmap it here.
174-
unsafe { rust_helper_kunmap(self.page) };
160+
unsafe { bindings::kunmap(self.page) };
175161
}
176162
}

rust/kernel/platdev.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ pub struct Registration {
2929
// (it is fine for multiple threads to have a shared reference to it).
3030
unsafe impl Sync for Registration {}
3131

32-
extern "C" {
33-
#[allow(improper_ctypes)]
34-
fn rust_helper_platform_get_drvdata(
35-
pdev: *const bindings::platform_device,
36-
) -> *mut c_types::c_void;
37-
38-
#[allow(improper_ctypes)]
39-
fn rust_helper_platform_set_drvdata(
40-
pdev: *mut bindings::platform_device,
41-
data: *mut c_types::c_void,
42-
);
43-
}
44-
4532
extern "C" fn probe_callback<P: PlatformDriver>(
4633
pdev: *mut bindings::platform_device,
4734
) -> c_types::c_int {
@@ -52,7 +39,7 @@ extern "C" fn probe_callback<P: PlatformDriver>(
5239
let drv_data = drv_data.into_pointer() as *mut c_types::c_void;
5340
// SAFETY: `pdev` is guaranteed to be a valid, non-null pointer.
5441
unsafe {
55-
rust_helper_platform_set_drvdata(pdev, drv_data);
42+
bindings::platform_set_drvdata(pdev, drv_data);
5643
}
5744
Ok(0)
5845
}
@@ -65,7 +52,7 @@ extern "C" fn remove_callback<P: PlatformDriver>(
6552
// SAFETY: `pdev` is guaranteed to be a valid, non-null pointer.
6653
let device_id = unsafe { (*pdev).id };
6754
// SAFETY: `pdev` is guaranteed to be a valid, non-null pointer.
68-
let ptr = unsafe { rust_helper_platform_get_drvdata(pdev) };
55+
let ptr = unsafe { bindings::platform_get_drvdata(pdev) };
6956
// SAFETY:
7057
// - we allocated this pointer using `P::DrvData::into_pointer`,
7158
// so it is safe to turn back into a `P::DrvData`.

rust/kernel/power.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
use crate::{bindings, c_types, from_kernel_result, sync::Ref, types::PointerWrapper, Result};
88
use core::{marker::PhantomData, ops::Deref};
99

10-
extern "C" {
11-
#[allow(improper_ctypes)]
12-
fn rust_helper_dev_get_drvdata(dev: *mut bindings::device) -> *mut c_types::c_void;
13-
}
14-
1510
/// Corresponds to the kernel's `struct dev_pm_ops`.
1611
///
1712
/// It is meant to be implemented by drivers that support power-management operations.
@@ -47,7 +42,7 @@ macro_rules! pm_callback {
4742
) -> c_types::c_int {
4843
from_kernel_result! {
4944
// SAFETY: `dev` is valid as it was passed in by the C portion.
50-
let ptr = unsafe { rust_helper_dev_get_drvdata(dev) };
45+
let ptr = unsafe { bindings::dev_get_drvdata(dev) };
5146
// SAFETY: By the safety requirements of `OpsTable::build`, we know that `ptr` came
5247
// from a previous call to `T::Data::into_pointer`.
5348
let data = unsafe { T::Data::borrow(ptr) };

rust/kernel/rbtree.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ use core::{
1616
ptr::{addr_of_mut, NonNull},
1717
};
1818

19-
extern "C" {
20-
fn rust_helper_rb_link_node(
21-
node: *mut bindings::rb_node,
22-
parent: *const bindings::rb_node,
23-
rb_link: *mut *mut bindings::rb_node,
24-
);
25-
}
26-
2719
struct Node<K, V> {
2820
links: bindings::rb_node,
2921
key: K,
@@ -289,7 +281,7 @@ impl<K, V> RBTree<K, V> {
289281
// "forgot" it with `Box::into_raw`.
290282
// SAFETY: All pointers are non-null and valid (`*new_link` is null, but `new_link` is a
291283
// mutable reference).
292-
unsafe { rust_helper_rb_link_node(node_links, parent, new_link) };
284+
unsafe { bindings::rb_link_node(node_links, parent, new_link) };
293285

294286
// SAFETY: All pointers are valid. `node` has just been inserted into the tree.
295287
unsafe { bindings::rb_insert_color(node_links, &mut self.root) };

rust/kernel/security.rs

+5-28
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,13 @@
44
//!
55
//! C header: [`include/linux/security.h`](../../../../include/linux/security.h).
66
7-
use crate::{bindings, c_types, error::Error, file::File, task::Task, Result};
8-
9-
extern "C" {
10-
#[allow(improper_ctypes)]
11-
fn rust_helper_security_binder_set_context_mgr(
12-
mgr: *mut bindings::task_struct,
13-
) -> c_types::c_int;
14-
#[allow(improper_ctypes)]
15-
fn rust_helper_security_binder_transaction(
16-
from: *mut bindings::task_struct,
17-
to: *mut bindings::task_struct,
18-
) -> c_types::c_int;
19-
#[allow(improper_ctypes)]
20-
fn rust_helper_security_binder_transfer_binder(
21-
from: *mut bindings::task_struct,
22-
to: *mut bindings::task_struct,
23-
) -> c_types::c_int;
24-
#[allow(improper_ctypes)]
25-
fn rust_helper_security_binder_transfer_file(
26-
from: *mut bindings::task_struct,
27-
to: *mut bindings::task_struct,
28-
file: *mut bindings::file,
29-
) -> c_types::c_int;
30-
}
7+
use crate::{bindings, error::Error, file::File, task::Task, Result};
318

329
/// Calls the security modules to determine if the given task can become the manager of a binder
3310
/// context.
3411
pub fn binder_set_context_mgr(mgr: &Task) -> Result {
3512
// SAFETY: By the `Task` invariants, `mgr.ptr` is valid.
36-
let ret = unsafe { rust_helper_security_binder_set_context_mgr(mgr.ptr) };
13+
let ret = unsafe { bindings::security_binder_set_context_mgr(mgr.ptr) };
3714
if ret != 0 {
3815
Err(Error::from_kernel_errno(ret))
3916
} else {
@@ -45,7 +22,7 @@ pub fn binder_set_context_mgr(mgr: &Task) -> Result {
4522
/// task `to`.
4623
pub fn binder_transaction(from: &Task, to: &Task) -> Result {
4724
// SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid.
48-
let ret = unsafe { rust_helper_security_binder_transaction(from.ptr, to.ptr) };
25+
let ret = unsafe { bindings::security_binder_transaction(from.ptr, to.ptr) };
4926
if ret != 0 {
5027
Err(Error::from_kernel_errno(ret))
5128
} else {
@@ -57,7 +34,7 @@ pub fn binder_transaction(from: &Task, to: &Task) -> Result {
5734
/// (owned by itself or other processes) to task `to` through a binder transaction.
5835
pub fn binder_transfer_binder(from: &Task, to: &Task) -> Result {
5936
// SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid.
60-
let ret = unsafe { rust_helper_security_binder_transfer_binder(from.ptr, to.ptr) };
37+
let ret = unsafe { bindings::security_binder_transfer_binder(from.ptr, to.ptr) };
6138
if ret != 0 {
6239
Err(Error::from_kernel_errno(ret))
6340
} else {
@@ -70,7 +47,7 @@ pub fn binder_transfer_binder(from: &Task, to: &Task) -> Result {
7047
pub fn binder_transfer_file(from: &Task, to: &Task, file: &File) -> Result {
7148
// SAFETY: By the `Task` invariants, `from.ptr` and `to.ptr` are valid. Similarly, by the
7249
// `File` invariants, `file.ptr` is also valid.
73-
let ret = unsafe { rust_helper_security_binder_transfer_file(from.ptr, to.ptr, file.ptr) };
50+
let ret = unsafe { bindings::security_binder_transfer_file(from.ptr, to.ptr, file.ptr) };
7451
if ret != 0 {
7552
Err(Error::from_kernel_errno(ret))
7653
} else {

rust/kernel/sync/arc.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ use core::{
2727
ptr::NonNull,
2828
};
2929

30-
extern "C" {
31-
fn rust_helper_refcount_new() -> bindings::refcount_t;
32-
fn rust_helper_refcount_inc(r: *mut bindings::refcount_t);
33-
fn rust_helper_refcount_dec_and_test(r: *mut bindings::refcount_t) -> bool;
34-
}
35-
3630
/// A reference-counted pointer to an instance of `T`.
3731
///
3832
/// The reference count is incremented when new instances of [`Ref`] are created, and decremented
@@ -86,7 +80,7 @@ impl<T> Ref<T> {
8680
// INVARIANT: The refcount is initialised to a non-zero value.
8781
let mut inner = Box::try_new(RefInner {
8882
// SAFETY: Just an FFI call that returns a `refcount_t` initialised to 1.
89-
refcount: UnsafeCell::new(unsafe { rust_helper_refcount_new() }),
83+
refcount: UnsafeCell::new(unsafe { bindings::REFCOUNT_INIT(1) }),
9084
data: contents,
9185
})?;
9286

@@ -169,7 +163,7 @@ impl<T: ?Sized> Clone for Ref<T> {
169163
// INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
170164
// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
171165
// safe to increment the refcount.
172-
unsafe { rust_helper_refcount_inc(self.ptr.as_ref().refcount.get()) };
166+
unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
173167
Self {
174168
ptr: self.ptr,
175169
_p: PhantomData,
@@ -196,7 +190,7 @@ impl<T: ?Sized> Drop for Ref<T> {
196190
// INVARIANT: If the refcount reaches zero, there are no other instances of `Ref`, and
197191
// this instance is being dropped, so the broken invariant is not observable.
198192
// SAFETY: Also by the type invariant, we are allowed to decrement the refcount.
199-
let is_zero = unsafe { rust_helper_refcount_dec_and_test(refcount) };
193+
let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };
200194
if is_zero {
201195
// The count reached zero, we must free the memory.
202196
//

0 commit comments

Comments
 (0)