Skip to content

Commit c810c92

Browse files
authored
Merge pull request #800 from ojeda/core_ffi
Take advantage of `feature(core_ffi_c)` and `feature(c_size_t)`
2 parents 4297971 + fdfa476 commit c810c92

29 files changed

+149
-273
lines changed

rust/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ bindgen_c_flags_final = $(bindgen_c_flags_lto)
297297
quiet_cmd_bindgen = BINDGEN $@
298298
cmd_bindgen = \
299299
$(BINDGEN) $< $(bindgen_target_flags) \
300-
--use-core --with-derive-default --ctypes-prefix c_types \
300+
--use-core --with-derive-default --ctypes-prefix core::ffi \
301301
--no-debug '.*' \
302302
--size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
303303
$(bindgen_target_cflags) $(bindgen_target_extra)

rust/kernel/allocator.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::alloc::{GlobalAlloc, Layout};
66
use core::ptr;
77

88
use crate::bindings;
9-
use crate::c_types;
109

1110
struct KernelAllocator;
1211

@@ -19,7 +18,7 @@ unsafe impl GlobalAlloc for KernelAllocator {
1918

2019
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
2120
unsafe {
22-
bindings::kfree(ptr as *const c_types::c_void);
21+
bindings::kfree(ptr as *const core::ffi::c_void);
2322
}
2423
}
2524
}
@@ -39,14 +38,14 @@ fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
3938

4039
#[no_mangle]
4140
fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
42-
unsafe { bindings::kfree(ptr as *const c_types::c_void) };
41+
unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
4342
}
4443

4544
#[no_mangle]
4645
fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
4746
unsafe {
4847
bindings::krealloc(
49-
ptr as *const c_types::c_void,
48+
ptr as *const core::ffi::c_void,
5049
new_size,
5150
bindings::GFP_KERNEL,
5251
) as *mut u8

rust/kernel/amba.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! C header: [`include/linux/amba/bus.h`](../../../../include/linux/amba/bus.h)
66
77
use crate::{
8-
bindings, c_types, device, driver, error::from_kernel_result, io_mem::Resource, power,
9-
str::CStr, to_result, types::PointerWrapper, Result, ThisModule,
8+
bindings, device, driver, error::from_kernel_result, io_mem::Resource, power, str::CStr,
9+
to_result, types::PointerWrapper, Result, ThisModule,
1010
};
1111

1212
/// A registration of an amba driver.
@@ -108,7 +108,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
108108
unsafe extern "C" fn probe_callback<T: Driver>(
109109
adev: *mut bindings::amba_device,
110110
aid: *const bindings::amba_id,
111-
) -> c_types::c_int {
111+
) -> core::ffi::c_int {
112112
from_kernel_result! {
113113
// SAFETY: `adev` is valid by the contract with the C code. `dev` is alive only for the
114114
// duration of this call, so it is guaranteed to remain alive for the lifetime of `dev`.

rust/kernel/bindings.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ mod bindings_raw {
2222
// Use glob import here to expose all helpers.
2323
// Symbols defined within the module will take precedence to the glob import.
2424
pub use super::bindings_helper::*;
25-
use crate::c_types;
2625
include!(concat!(env!("OBJTREE"), "/rust/bindings_generated.rs"));
2726
}
2827

@@ -33,7 +32,6 @@ mod bindings_raw {
3332
mod bindings_helper {
3433
// Import the generated bindings for types.
3534
use super::bindings_raw::*;
36-
use crate::c_types;
3735
include!(concat!(
3836
env!("OBJTREE"),
3937
"/rust/bindings_helpers_generated.rs"

rust/kernel/c_types.rs

Lines changed: 0 additions & 119 deletions
This file was deleted.

rust/kernel/chrdev.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use core::marker::PhantomPinned;
1414
use core::pin::Pin;
1515

1616
use crate::bindings;
17-
use crate::c_types;
1817
use crate::error::{code::*, Error, Result};
1918
use crate::file;
2019
use crate::str::CStr;
@@ -53,7 +52,7 @@ impl Cdev {
5352
Ok(Self(cdev))
5453
}
5554

56-
fn add(&mut self, dev: bindings::dev_t, count: c_types::c_uint) -> Result {
55+
fn add(&mut self, dev: bindings::dev_t, count: core::ffi::c_uint) -> Result {
5756
// SAFETY: According to the type invariants:
5857
// - [`self.0`] can be safely passed to [`bindings::cdev_add`].
5958
// - [`(*self.0).ops`] will live at least as long as [`self.0`].

rust/kernel/device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::{
2121
};
2222

2323
#[cfg(CONFIG_PRINTK)]
24-
use crate::{c_str, c_types};
24+
use crate::c_str;
2525

2626
/// A raw device.
2727
///
@@ -149,10 +149,10 @@ pub unsafe trait RawDevice {
149149
#[cfg(CONFIG_PRINTK)]
150150
unsafe {
151151
bindings::_dev_printk(
152-
klevel as *const _ as *const c_types::c_char,
152+
klevel as *const _ as *const core::ffi::c_char,
153153
self.raw_device(),
154154
c_str!("%pA").as_char_ptr(),
155-
&msg as *const _ as *const c_types::c_void,
155+
&msg as *const _ as *const core::ffi::c_void,
156156
)
157157
};
158158
}

rust/kernel/error.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//!
55
//! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
66
7+
use crate::bindings;
78
use crate::str::CStr;
8-
use crate::{bindings, c_types};
99
use alloc::{
1010
alloc::{AllocError, LayoutError},
1111
collections::TryReserveError,
@@ -315,14 +315,14 @@ pub mod code {
315315
///
316316
/// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`).
317317
#[derive(Clone, Copy, PartialEq, Eq)]
318-
pub struct Error(c_types::c_int);
318+
pub struct Error(core::ffi::c_int);
319319

320320
impl Error {
321321
/// Creates an [`Error`] from a kernel error code.
322322
///
323323
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
324324
/// be returned in such a case.
325-
pub(crate) fn from_kernel_errno(errno: c_types::c_int) -> Error {
325+
pub(crate) fn from_kernel_errno(errno: core::ffi::c_int) -> Error {
326326
if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
327327
// TODO: Make it a `WARN_ONCE` once available.
328328
crate::pr_warn!(
@@ -342,14 +342,14 @@ impl Error {
342342
/// # Safety
343343
///
344344
/// `errno` must be within error code range (i.e. `>= -MAX_ERRNO && < 0`).
345-
pub(crate) unsafe fn from_kernel_errno_unchecked(errno: c_types::c_int) -> Error {
345+
pub(crate) unsafe fn from_kernel_errno_unchecked(errno: core::ffi::c_int) -> Error {
346346
// INVARIANT: The contract ensures the type invariant
347347
// will hold.
348348
Error(errno)
349349
}
350350

351351
/// Returns the kernel error code.
352-
pub fn to_kernel_errno(self) -> c_types::c_int {
352+
pub fn to_kernel_errno(self) -> core::ffi::c_int {
353353
self.0
354354
}
355355

@@ -482,11 +482,10 @@ where
482482
///
483483
/// ```ignore
484484
/// # use kernel::from_kernel_result;
485-
/// # use kernel::c_types;
486485
/// # use kernel::bindings;
487486
/// unsafe extern "C" fn probe_callback(
488487
/// pdev: *mut bindings::platform_device,
489-
/// ) -> c_types::c_int {
488+
/// ) -> core::ffi::c_int {
490489
/// from_kernel_result! {
491490
/// let ptr = devm_alloc(pdev)?;
492491
/// bindings::platform_set_drvdata(pdev, ptr);
@@ -515,12 +514,11 @@ pub(crate) use from_kernel_result;
515514
///
516515
/// ```ignore
517516
/// # use kernel::from_kernel_err_ptr;
518-
/// # use kernel::c_types;
519517
/// # use kernel::bindings;
520518
/// fn devm_platform_ioremap_resource(
521519
/// pdev: &mut PlatformDevice,
522520
/// index: u32,
523-
/// ) -> Result<*mut c_types::c_void> {
521+
/// ) -> Result<*mut core::ffi::c_void> {
524522
/// // SAFETY: FFI call.
525523
/// unsafe {
526524
/// from_kernel_err_ptr(bindings::devm_platform_ioremap_resource(
@@ -533,8 +531,8 @@ pub(crate) use from_kernel_result;
533531
// TODO: Remove `dead_code` marker once an in-kernel client is available.
534532
#[allow(dead_code)]
535533
pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
536-
// CAST: Casting a pointer to `*const c_types::c_void` is always valid.
537-
let const_ptr: *const c_types::c_void = ptr.cast();
534+
// CAST: Casting a pointer to `*const core::ffi::c_void` is always valid.
535+
let const_ptr: *const core::ffi::c_void = ptr.cast();
538536
// SAFETY: The FFI function does not deref the pointer.
539537
if unsafe { bindings::IS_ERR(const_ptr) } {
540538
// SAFETY: The FFI function does not deref the pointer.
@@ -555,7 +553,7 @@ pub(crate) fn from_kernel_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
555553

556554
/// Calls a kernel function that returns an integer error code on failure and converts the result
557555
/// to a [`Result`].
558-
pub fn to_result(func: impl FnOnce() -> c_types::c_int) -> Result {
556+
pub fn to_result(func: impl FnOnce() -> core::ffi::c_int) -> Result {
559557
let err = func();
560558
if err < 0 {
561559
Err(Error::from_kernel_errno(err))

0 commit comments

Comments
 (0)