rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Luis Chamberlain" <mcgrof@kernel.org>,
	"Russ Weight" <russ.weight@linux.dev>,
	"Danilo Krummrich" <dakr@redhat.com>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	"Finn Behrens" <me@kloenk.dev>,
	"Valentin Obst" <kernel@valentinobst.de>
Cc: Wedson Almeida Filho <walmeida@microsoft.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 4/5] rust: map `long` to `isize` and `char` to `u8`
Date: Fri, 13 Sep 2024 22:29:24 +0100	[thread overview]
Message-ID: <20240913213041.395655-5-gary@garyguo.net> (raw)
In-Reply-To: <20240913213041.395655-1-gary@garyguo.net>

The following FFI types are replaced compared to `core::ffi`:

1. `char` type is now always mapped to `u8`, since kernel uses
   `-funsigned-char` on the C code. `core::ffi` maps it to platform
   default ABI, which can be either signed or unsigned.

2. `long` is now always mapped to `isize`. It's very common in the
   kernel to use `long` to represent a pointer-sized integer, and in
   fact `intptr_t` is a typedef of `long` in the kernel. Enforce this
   mapping rather than mapping to `i32/i64` depending on platform can
   save us a lot of unnecessary casts.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/ffi.rs             | 37 ++++++++++++++++++++++++++++++++++++-
 rust/kernel/error.rs    |  5 +----
 rust/kernel/firmware.rs |  2 +-
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/rust/ffi.rs b/rust/ffi.rs
index 1894a511ee171..814d10106b5a8 100644
--- a/rust/ffi.rs
+++ b/rust/ffi.rs
@@ -10,4 +10,39 @@
 
 #![no_std]
 
-pub use core::ffi::*;
+macro_rules! alias {
+    ($($name:ident = $ty:ty;)*) => {$(
+        #[allow(non_camel_case_types, missing_docs)]
+        pub type $name = $ty;
+
+        // Check size compatibility with libcore.
+        const _: () = assert!(
+            core::mem::size_of::<$name>() == core::mem::size_of::<core::ffi::$name>()
+        );
+    )*}
+}
+
+alias! {
+    // `core::ffi::c_char` is either i8/u8 depending on architecture. In kernel, we use
+    // `-funsigned-char` so it's always mapped to u8.
+    c_char = u8;
+
+    c_schar = i8;
+    c_uchar = u8;
+
+    c_short = i16;
+    c_ushort = u16;
+
+    c_int = i32;
+    c_uint = u32;
+
+    // In kernel, `intptr_t` is defined to be `long` in all platforms, so we can map the type to
+    // `isize`.
+    c_long = isize;
+    c_ulong = usize;
+
+    c_longlong = i64;
+    c_ulonglong = u64;
+}
+
+pub use core::ffi::c_void;
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 1090a13c2bd17..b9ad9ff25cddb 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -135,11 +135,8 @@ pub(crate) fn to_blk_status(self) -> bindings::blk_status_t {
     /// Returns the error encoded as a pointer.
     #[allow(dead_code)]
     pub(crate) fn to_ptr<T>(self) -> *mut T {
-        #[cfg_attr(target_pointer_width = "32", allow(clippy::useless_conversion))]
         // SAFETY: `self.0` is a valid error due to its invariant.
-        unsafe {
-            bindings::ERR_PTR(self.0.into()) as *mut _
-        }
+        unsafe { bindings::ERR_PTR(self.0 as _) as *mut _ }
     }
 
     /// Returns a string representing the error, if one exists.
diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
index dee5b4b18aec4..6f77945016579 100644
--- a/rust/kernel/firmware.rs
+++ b/rust/kernel/firmware.rs
@@ -12,7 +12,7 @@
 /// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`,
 /// `bindings::firmware_request_platform`, `bindings::request_firmware_direct`.
 struct FwFunc(
-    unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32,
+    unsafe extern "C" fn(*mut *const bindings::firmware, *const u8, *mut bindings::device) -> i32,
 );
 
 impl FwFunc {
-- 
2.44.1


  parent reply	other threads:[~2024-09-13 21:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-13 21:29 [PATCH 0/5] use custom FFI integer types Gary Guo
2024-09-13 21:29 ` [PATCH 1/5] rust: fix size_t in bindgen prototypes of C builtins Gary Guo
2024-09-29 21:00   ` Trevor Gross
2024-10-05 22:10     ` Gary Guo
2024-09-13 21:29 ` [PATCH 2/5] rust: map `__kernel_size_t` and friends also to usize/isize Gary Guo
2024-09-23  9:20   ` Alice Ryhl
2024-09-29 21:02   ` Trevor Gross
2024-09-13 21:29 ` [PATCH 3/5] rust: use custom FFI integer types Gary Guo
2024-09-13 21:29 ` Gary Guo [this message]
2024-09-23  9:20   ` [PATCH 4/5] rust: map `long` to `isize` and `char` to `u8` Alice Ryhl
2024-09-13 21:29 ` [PATCH 5/5] rust: cleanup unnecessary casts Gary Guo
2024-09-23  9:19   ` Alice Ryhl
2024-09-13 21:42 ` [PATCH 0/5] use custom FFI integer types Benno Lossin
     [not found]   ` <CAOcBZOS6BAJ1FTFkB3x6jdag_hL7zrLbFy7TxkvZWKxRZ_+ggA@mail.gmail.com>
2024-09-14  2:51     ` Ramon de C Valle
2024-09-14  8:52       ` Alice Ryhl
2024-09-14 23:52       ` Gary Guo
2024-09-16 17:17         ` Ramon de C Valle
2024-09-16 17:26           ` Miguel Ojeda
2024-11-11  0:14 ` Miguel Ojeda
2024-12-15 22:25   ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240913213041.395655-5-gary@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@redhat.com \
    --cc=kernel@valentinobst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=me@kloenk.dev \
    --cc=ojeda@kernel.org \
    --cc=russ.weight@linux.dev \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=walmeida@microsoft.com \
    --cc=wedsonaf@gmail.com \
    --cc=yakoyoku@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).