Skip to content

Commit a4a52ea

Browse files
committed
rust: kernel: use core::ffi instead of kernel::c_types
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 1f7051e commit a4a52ea

23 files changed

+138
-142
lines changed

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/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))

rust/kernel/file.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! [`include/linux/file.h`](../../../../include/linux/file.h)
77
88
use crate::{
9-
bindings, c_types,
9+
bindings,
1010
cred::Credential,
1111
error::{code::*, from_kernel_result, Error, Result},
1212
io_buffer::{IoBufferReader, IoBufferWriter},
@@ -216,7 +216,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
216216
unsafe extern "C" fn open_callback(
217217
inode: *mut bindings::inode,
218218
file: *mut bindings::file,
219-
) -> c_types::c_int {
219+
) -> core::ffi::c_int {
220220
from_kernel_result! {
221221
// SAFETY: `A::convert` must return a valid non-null pointer that
222222
// should point to data in the inode or file that lives longer
@@ -233,19 +233,19 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
233233
// for implementers of the file operations (no other C code accesses
234234
// it), so we know that there are no concurrent threads/CPUs accessing
235235
// it (it's not visible to any other Rust code).
236-
unsafe { (*file).private_data = ptr as *mut c_types::c_void };
236+
unsafe { (*file).private_data = ptr as *mut core::ffi::c_void };
237237
Ok(0)
238238
}
239239
}
240240

241241
unsafe extern "C" fn read_callback(
242242
file: *mut bindings::file,
243-
buf: *mut c_types::c_char,
244-
len: c_types::c_size_t,
243+
buf: *mut core::ffi::c_char,
244+
len: core::ffi::c_size_t,
245245
offset: *mut bindings::loff_t,
246-
) -> c_types::c_ssize_t {
246+
) -> core::ffi::c_ssize_t {
247247
from_kernel_result! {
248-
let mut data = unsafe { UserSlicePtr::new(buf as *mut c_types::c_void, len).writer() };
248+
let mut data = unsafe { UserSlicePtr::new(buf as *mut core::ffi::c_void, len).writer() };
249249
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
250250
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
251251
// `release` callback, which the C API guarantees that will be called only when all
@@ -288,12 +288,12 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
288288

289289
unsafe extern "C" fn write_callback(
290290
file: *mut bindings::file,
291-
buf: *const c_types::c_char,
292-
len: c_types::c_size_t,
291+
buf: *const core::ffi::c_char,
292+
len: core::ffi::c_size_t,
293293
offset: *mut bindings::loff_t,
294-
) -> c_types::c_ssize_t {
294+
) -> core::ffi::c_ssize_t {
295295
from_kernel_result! {
296-
let mut data = unsafe { UserSlicePtr::new(buf as *mut c_types::c_void, len).reader() };
296+
let mut data = unsafe { UserSlicePtr::new(buf as *mut core::ffi::c_void, len).reader() };
297297
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
298298
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
299299
// `release` callback, which the C API guarantees that will be called only when all
@@ -337,7 +337,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
337337
unsafe extern "C" fn release_callback(
338338
_inode: *mut bindings::inode,
339339
file: *mut bindings::file,
340-
) -> c_types::c_int {
340+
) -> core::ffi::c_int {
341341
let ptr = mem::replace(unsafe { &mut (*file).private_data }, ptr::null_mut());
342342
T::release(unsafe { T::Data::from_pointer(ptr as _) }, unsafe {
343343
File::from_ptr(file)
@@ -348,7 +348,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
348348
unsafe extern "C" fn llseek_callback(
349349
file: *mut bindings::file,
350350
offset: bindings::loff_t,
351-
whence: c_types::c_int,
351+
whence: core::ffi::c_int,
352352
) -> bindings::loff_t {
353353
from_kernel_result! {
354354
let off = match whence as u32 {
@@ -370,9 +370,9 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
370370

371371
unsafe extern "C" fn unlocked_ioctl_callback(
372372
file: *mut bindings::file,
373-
cmd: c_types::c_uint,
374-
arg: c_types::c_ulong,
375-
) -> c_types::c_long {
373+
cmd: core::ffi::c_uint,
374+
arg: core::ffi::c_ulong,
375+
) -> core::ffi::c_long {
376376
from_kernel_result! {
377377
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
378378
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
@@ -388,9 +388,9 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
388388

389389
unsafe extern "C" fn compat_ioctl_callback(
390390
file: *mut bindings::file,
391-
cmd: c_types::c_uint,
392-
arg: c_types::c_ulong,
393-
) -> c_types::c_long {
391+
cmd: core::ffi::c_uint,
392+
arg: core::ffi::c_ulong,
393+
) -> core::ffi::c_long {
394394
from_kernel_result! {
395395
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
396396
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
@@ -407,7 +407,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
407407
unsafe extern "C" fn mmap_callback(
408408
file: *mut bindings::file,
409409
vma: *mut bindings::vm_area_struct,
410-
) -> c_types::c_int {
410+
) -> core::ffi::c_int {
411411
from_kernel_result! {
412412
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
413413
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
@@ -431,8 +431,8 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
431431
file: *mut bindings::file,
432432
start: bindings::loff_t,
433433
end: bindings::loff_t,
434-
datasync: c_types::c_int,
435-
) -> c_types::c_int {
434+
datasync: core::ffi::c_int,
435+
) -> core::ffi::c_int {
436436
from_kernel_result! {
437437
let start = start.try_into()?;
438438
let end = end.try_into()?;

rust/kernel/gpio.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! C header: [`include/linux/gpio/driver.h`](../../../../include/linux/gpio/driver.h)
66
77
use crate::{
8-
bindings, c_types, device, error::code::*, error::from_kernel_result, types::PointerWrapper,
9-
Error, Result,
8+
bindings, device, error::code::*, error::from_kernel_result, types::PointerWrapper, Error,
9+
Result,
1010
};
1111
use core::{
1212
cell::UnsafeCell,
@@ -249,8 +249,8 @@ impl<T: Chip> Drop for Registration<T> {
249249

250250
unsafe extern "C" fn get_direction_callback<T: Chip>(
251251
gc: *mut bindings::gpio_chip,
252-
offset: c_types::c_uint,
253-
) -> c_types::c_int {
252+
offset: core::ffi::c_uint,
253+
) -> core::ffi::c_int {
254254
from_kernel_result! {
255255
// SAFETY: The value stored as chip data was returned by `into_pointer` during registration.
256256
let data = unsafe { T::Data::borrow(bindings::gpiochip_get_data(gc)) };
@@ -260,8 +260,8 @@ unsafe extern "C" fn get_direction_callback<T: Chip>(
260260

261261
unsafe extern "C" fn direction_input_callback<T: Chip>(
262262
gc: *mut bindings::gpio_chip,
263-
offset: c_types::c_uint,
264-
) -> c_types::c_int {
263+
offset: core::ffi::c_uint,
264+
) -> core::ffi::c_int {
265265
from_kernel_result! {
266266
// SAFETY: The value stored as chip data was returned by `into_pointer` during registration.
267267
let data = unsafe { T::Data::borrow(bindings::gpiochip_get_data(gc)) };
@@ -272,9 +272,9 @@ unsafe extern "C" fn direction_input_callback<T: Chip>(
272272

273273
unsafe extern "C" fn direction_output_callback<T: Chip>(
274274
gc: *mut bindings::gpio_chip,
275-
offset: c_types::c_uint,
276-
value: c_types::c_int,
277-
) -> c_types::c_int {
275+
offset: core::ffi::c_uint,
276+
value: core::ffi::c_int,
277+
) -> core::ffi::c_int {
278278
from_kernel_result! {
279279
// SAFETY: The value stored as chip data was returned by `into_pointer` during registration.
280280
let data = unsafe { T::Data::borrow(bindings::gpiochip_get_data(gc)) };
@@ -285,8 +285,8 @@ unsafe extern "C" fn direction_output_callback<T: Chip>(
285285

286286
unsafe extern "C" fn get_callback<T: Chip>(
287287
gc: *mut bindings::gpio_chip,
288-
offset: c_types::c_uint,
289-
) -> c_types::c_int {
288+
offset: core::ffi::c_uint,
289+
) -> core::ffi::c_int {
290290
from_kernel_result! {
291291
// SAFETY: The value stored as chip data was returned by `into_pointer` during registration.
292292
let data = unsafe { T::Data::borrow(bindings::gpiochip_get_data(gc)) };
@@ -297,8 +297,8 @@ unsafe extern "C" fn get_callback<T: Chip>(
297297

298298
unsafe extern "C" fn set_callback<T: Chip>(
299299
gc: *mut bindings::gpio_chip,
300-
offset: c_types::c_uint,
301-
value: c_types::c_int,
300+
offset: core::ffi::c_uint,
301+
value: core::ffi::c_int,
302302
) {
303303
// SAFETY: The value stored as chip data was returned by `into_pointer` during registration.
304304
let data = unsafe { T::Data::borrow(bindings::gpiochip_get_data(gc)) };

0 commit comments

Comments
 (0)