Skip to content

Commit 5444b8b

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

17 files changed

+67
-226
lines changed

rust/helpers.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <linux/security.h>
1414
#include <asm/io.h>
1515

16-
void rust_helper_BUG(void)
16+
__noreturn void rust_helper_BUG(void)
1717
{
1818
BUG();
1919
}
@@ -91,16 +91,16 @@ void rust_helper_writeq(u64 value, volatile void __iomem *addr)
9191
EXPORT_SYMBOL_GPL(rust_helper_writeq);
9292
#endif
9393

94-
void rust_helper_spin_lock_init(spinlock_t *lock, const char *name,
95-
struct lock_class_key *key)
94+
void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
95+
struct lock_class_key *key)
9696
{
9797
#ifdef CONFIG_DEBUG_SPINLOCK
9898
__spin_lock_init(lock, name, key);
9999
#else
100100
spin_lock_init(lock);
101101
#endif
102102
}
103-
EXPORT_SYMBOL_GPL(rust_helper_spin_lock_init);
103+
EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
104104

105105
void rust_helper_spin_lock(spinlock_t *lock)
106106
{
@@ -162,22 +162,23 @@ size_t rust_helper_copy_to_iter(const void *addr, size_t bytes, struct iov_iter
162162
}
163163
EXPORT_SYMBOL_GPL(rust_helper_copy_to_iter);
164164

165-
bool rust_helper_is_err(__force const void *ptr)
165+
bool rust_helper_IS_ERR(__force const void *ptr)
166166
{
167167
return IS_ERR(ptr);
168168
}
169-
EXPORT_SYMBOL_GPL(rust_helper_is_err);
169+
EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
170170

171-
long rust_helper_ptr_err(__force const void *ptr)
171+
long rust_helper_PTR_ERR(__force const void *ptr)
172172
{
173173
return PTR_ERR(ptr);
174174
}
175-
EXPORT_SYMBOL_GPL(rust_helper_ptr_err);
175+
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
176176

177177
const char *rust_helper_errname(int err)
178178
{
179179
return errname(err);
180180
}
181+
EXPORT_SYMBOL_GPL(rust_helper_errname);
181182

182183
void rust_helper_mutex_lock(struct mutex *lock)
183184
{
@@ -200,11 +201,11 @@ rust_helper_platform_set_drvdata(struct platform_device *pdev,
200201
}
201202
EXPORT_SYMBOL_GPL(rust_helper_platform_set_drvdata);
202203

203-
refcount_t rust_helper_refcount_new(void)
204+
refcount_t rust_helper_REFCOUNT_INIT(int n)
204205
{
205-
return (refcount_t)REFCOUNT_INIT(1);
206+
return (refcount_t)REFCOUNT_INIT(n);
206207
}
207-
EXPORT_SYMBOL_GPL(rust_helper_refcount_new);
208+
EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
208209

209210
void rust_helper_refcount_inc(refcount_t *r)
210211
{

rust/kernel/error.rs

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

352352
impl fmt::Debug for Error {
353353
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
354-
extern "C" {
355-
fn rust_helper_errname(err: c_types::c_int) -> *const c_types::c_char;
356-
}
357354
// SAFETY: FFI call.
358-
let name = unsafe { rust_helper_errname(-self.0) };
355+
let name = unsafe { bindings::errname(-self.0) };
359356

360357
if name.is_null() {
361358
// Print out number if no name can be found.
@@ -452,7 +449,7 @@ where
452449
/// ) -> c_types::c_int {
453450
/// from_kernel_result! {
454451
/// let ptr = devm_alloc(pdev)?;
455-
/// rust_helper_platform_set_drvdata(pdev, ptr);
452+
/// bindings::platform_set_drvdata(pdev, ptr);
456453
/// Ok(0)
457454
/// }
458455
/// }
@@ -496,28 +493,20 @@ macro_rules! from_kernel_result {
496493
// TODO: remove `dead_code` marker once an in-kernel client is available.
497494
#[allow(dead_code)]
498495
pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
499-
extern "C" {
500-
#[allow(improper_ctypes)]
501-
fn rust_helper_is_err(ptr: *const c_types::c_void) -> bool;
502-
503-
#[allow(improper_ctypes)]
504-
fn rust_helper_ptr_err(ptr: *const c_types::c_void) -> c_types::c_long;
505-
}
506-
507496
// CAST: casting a pointer to `*const c_types::c_void` is always valid.
508497
let const_ptr: *const c_types::c_void = ptr.cast();
509498
// SAFETY: the FFI function does not deref the pointer.
510-
if unsafe { rust_helper_is_err(const_ptr) } {
499+
if unsafe { bindings::IS_ERR(const_ptr) } {
511500
// SAFETY: the FFI function does not deref the pointer.
512-
let err = unsafe { rust_helper_ptr_err(const_ptr) };
513-
// CAST: if `rust_helper_is_err()` returns `true`,
514-
// then `rust_helper_ptr_err()` is guaranteed to return a
501+
let err = unsafe { bindings::PTR_ERR(const_ptr) };
502+
// CAST: if `IS_ERR()` returns `true`,
503+
// then `PTR_ERR()` is guaranteed to return a
515504
// negative value greater-or-equal to `-bindings::MAX_ERRNO`,
516505
// which always fits in an `i16`, as per the invariant above.
517506
// And an `i16` always fits in an `i32`. So casting `err` to
518507
// an `i32` can never overflow, and is always valid.
519508
//
520-
// SAFETY: `rust_helper_is_err()` ensures `err` is a
509+
// SAFETY: `IS_ERR()` ensures `err` is a
521510
// negative value greater-or-equal to `-bindings::MAX_ERRNO`
522511
return Err(unsafe { Error::from_kernel_errno_unchecked(err as i32) });
523512
}

rust/kernel/io_mem.rs

+6-25
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,9 @@
44
//!
55
//! C header: [`include/asm-generic/io.h`](../../../../include/asm-generic/io.h)
66
7-
use crate::{bindings, c_types, Error, Result};
7+
use crate::{bindings, Error, Result};
88
use core::convert::TryInto;
99

10-
extern "C" {
11-
fn rust_helper_ioremap(
12-
offset: bindings::resource_size_t,
13-
size: c_types::c_ulong,
14-
) -> *mut c_types::c_void;
15-
16-
fn rust_helper_readb(addr: *const c_types::c_void) -> u8;
17-
fn rust_helper_readw(addr: *const c_types::c_void) -> u16;
18-
fn rust_helper_readl(addr: *const c_types::c_void) -> u32;
19-
fn rust_helper_readq(addr: *const c_types::c_void) -> u64;
20-
21-
fn rust_helper_writeb(value: u8, addr: *mut c_types::c_void);
22-
fn rust_helper_writew(value: u16, addr: *mut c_types::c_void);
23-
fn rust_helper_writel(value: u32, addr: *mut c_types::c_void);
24-
fn rust_helper_writeq(value: u64, addr: *mut c_types::c_void);
25-
}
26-
2710
/// Represents a memory resource.
2811
pub struct Resource {
2912
offset: bindings::resource_size_t,
@@ -88,7 +71,7 @@ macro_rules! define_read {
8871
// SAFETY: The type invariants guarantee that `ptr` is a valid pointer. The check above
8972
// guarantees that the code won't build if `offset` makes the read go out of bounds
9073
// (including the type size).
91-
unsafe { concat_idents!(rust_helper_, $name)(ptr as _) }
74+
unsafe { bindings::$name(ptr as _) }
9275
}
9376

9477
/// Reads IO data from the given offset.
@@ -102,7 +85,7 @@ macro_rules! define_read {
10285
// SAFETY: The type invariants guarantee that `ptr` is a valid pointer. The check above
10386
// returns an error if `offset` would make the read go out of bounds (including the
10487
// type size).
105-
Ok(unsafe { concat_idents!(rust_helper_, $name)(ptr as _) })
88+
Ok(unsafe { bindings::$name(ptr as _) })
10689
}
10790
};
10891
}
@@ -118,7 +101,7 @@ macro_rules! define_write {
118101
// SAFETY: The type invariants guarantee that `ptr` is a valid pointer. The check above
119102
// guarantees that the code won't link if `offset` makes the write go out of bounds
120103
// (including the type size).
121-
unsafe { concat_idents!(rust_helper_, $name)(value, ptr as _) }
104+
unsafe { bindings::$name(value, ptr as _) }
122105
}
123106

124107
/// Writes IO data to the given offset.
@@ -132,7 +115,7 @@ macro_rules! define_write {
132115
// SAFETY: The type invariants guarantee that `ptr` is a valid pointer. The check above
133116
// returns an error if `offset` would make the write go out of bounds (including the
134117
// type size).
135-
unsafe { concat_idents!(rust_helper_, $name)(value, ptr as _) };
118+
unsafe { bindings::$name(value, ptr as _) };
136119
Ok(())
137120
}
138121
};
@@ -165,9 +148,7 @@ impl<const SIZE: usize> IoMem<SIZE> {
165148

166149
// Try to map the resource.
167150
// SAFETY: Just mapping the memory range.
168-
// TODO: Remove `into` call below (and disabling of clippy warning) once #465 is fixed.
169-
#[allow(clippy::complexity)]
170-
let addr = unsafe { rust_helper_ioremap(res.offset, res.size.into()) };
151+
let addr = unsafe { bindings::ioremap(res.offset, res.size as _) };
171152
if addr.is_null() {
172153
Err(Error::ENOMEM)
173154
} else {

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/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,11 @@ macro_rules! container_of {
232232
#[cfg(not(any(testlib, test)))]
233233
#[panic_handler]
234234
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
235-
extern "C" {
236-
fn rust_helper_BUG() -> !;
237-
}
238235
pr_emerg!("{}\n", info);
239236
// SAFETY: FFI call.
240-
unsafe { rust_helper_BUG() };
237+
unsafe { bindings::BUG() };
238+
// Bindgen currently does not recognize `__noreturn` so `BUG` returns `()`
239+
// instead of `!`.
240+
// https://github.com/rust-lang/rust-bindgen/issues/2094
241+
loop {}
241242
}

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) };

0 commit comments

Comments
 (0)