Skip to contentSkip to conversation

rust: cpufreq: Ensure C ABI compatibility in all unsafe

PATCH Applied Abhinav Ananthu 3 participants 6 messages Jun 13, 2025 – Jun 19, 2025 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rust: cpufreq: Ensure C ABI compatibility in all unsafe

Update all unsafe extern "C" callback functions in the cpufreq module to use kernel::ffi types (c_int, c_uint, etc.) instead of Rust-native types like i32, u32, or usize.

This change ensures that all Rust callbacks have signatures that are ABI-compatible with their corresponding C counterparts, which is critical for FFI correctness and safety.

---
 rust/kernel/cpufreq.rs | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
rust/kernel/cpufreq.rs +13 -13
index 11b03e9d7e89..481a6d2dc362 100644
@@ -1207,8 +1207,8 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn target_callback(
         ptr: *mut bindings::cpufreq_policy,
-        target_freq: u32,
-        relation: u32,
+        target_freq: c_uint,
+        relation: c_uint,
     ) -> kernel::ffi::c_int {
         from_result(|| {
             // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
@@ -1226,7 +1226,7 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn target_index_callback(
         ptr: *mut bindings::cpufreq_policy,
-        index: u32,
+        index: c_uint,
     ) -> kernel::ffi::c_int {
         from_result(|| {
             // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
@@ -1249,7 +1249,7 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn fast_switch_callback(
         ptr: *mut bindings::cpufreq_policy,
-        target_freq: u32,
+        target_freq: c_uint,
     ) -> kernel::ffi::c_uint {
         // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
         // lifetime of `policy`.
@@ -1263,10 +1263,10 @@ impl<T: Driver> Registration<T> {
     ///
     /// - This function may only be called from the cpufreq C infrastructure.
     unsafe extern "C" fn adjust_perf_callback(
-        cpu: u32,
-        min_perf: usize,
-        target_perf: usize,
-        capacity: usize,
+        cpu: c_uint,
+        min_perf: c_ulong,
+        target_perf: c_ulong,
+        capacity: c_ulong,
     ) {
         // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
         let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) };
@@ -1284,7 +1284,7 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn get_intermediate_callback(
         ptr: *mut bindings::cpufreq_policy,
-        index: u32,
+        index: c_uint,
     ) -> kernel::ffi::c_uint {
         // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
         // lifetime of `policy`.
@@ -1305,7 +1305,7 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn target_intermediate_callback(
         ptr: *mut bindings::cpufreq_policy,
-        index: u32,
+        index: c_uint,
     ) -> kernel::ffi::c_int {
         from_result(|| {
             // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
@@ -1325,7 +1325,7 @@ impl<T: Driver> Registration<T> {
     /// # Safety
     ///
     /// - This function may only be called from the cpufreq C infrastructure.
-    unsafe extern "C" fn get_callback(cpu: u32) -> kernel::ffi::c_uint {
+    unsafe extern "C" fn get_callback(cpu: c_uint) -> kernel::ffi::c_uint {
         // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
         let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) };
 
@@ -1351,7 +1351,7 @@ impl<T: Driver> Registration<T> {
     ///
     /// - This function may only be called from the cpufreq C infrastructure.
     /// - The pointer arguments must be valid pointers.
-    unsafe extern "C" fn bios_limit_callback(cpu: i32, limit: *mut u32) -> kernel::ffi::c_int {
+    unsafe extern "C" fn bios_limit_callback(cpu: c_int, limit: *mut c_uint) -> kernel::ffi::c_int {
         // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
         let cpu_id = unsafe { CpuId::from_i32_unchecked(cpu) };
 
@@ -1371,7 +1371,7 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn set_boost_callback(
         ptr: *mut bindings::cpufreq_policy,
-        state: i32,
+        state: c_int,
     ) -> kernel::ffi::c_int {
         from_result(|| {
             // SAFETY: The `ptr` is guaranteed to be valid by the contract with the C code for the
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 20 lines · Abhinav Ananthu

Update all unsafe extern "C" callback functions in the cpufreq module to use kernel::ffi types (c_int, c_uint, etc.) instead of Rust-native types like i32, u32, or usize.

This change ensures that all Rust callbacks have signatures that are ABI-compatible with their corresponding C counterparts, which is critical for FFI correctness and safety.

---
 rust/kernel/cpufreq.rs | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
rust/kernel/cpufreq.rs +0 -0
index 11b03e9d7e89..481a6d2dc362 100644

rust/kernel/cpufreq.rs impl<T: Driver> Registration<T> { · line 1211

Quoted 7 lines
rust/kernel/cpufreq.rs +2 -2
@@ -1207,8 +1207,8 @@ impl<T: Driver> Registration<T> {
     /// - The pointer arguments must be valid pointers.
     unsafe extern "C" fn target_callback(
         ptr: *mut bindings::cpufreq_policy,
-        target_freq: u32,
-        relation: u32,
+        target_freq: c_uint,
+        relation: c_uint,

I think the one in prelude points to core::ffi::* instead, while we want to use kernel::ffi::* ?

Miguel ?

Also why does prelude use ::ffi::* instead of kernel::ffi::* ? I was always a bit confused about it.

Signature · 1 line
viresh
#2 lore raw
Quoted 5 lines · Viresh Kumar

I think the one in prelude points to core::ffi::* instead, while we want to use kernel::ffi::* ?

Miguel ?

The one in the kernel prelude points to the kernel ones (the core ones should not be used anymore).

Quoted 2 lines

Also why does prelude use ::ffi::* instead of kernel::ffi::* ? I was always a bit confused about it.

It is because we define them in a crate called ffi, not in the kernel one; and we also re-export it in kernel.

In any case, from now on, one should just refer to them directly as c_* (please use the kernel prelude for that) -- please see:

https://docs.kernel.org/rust/coding-guidelines.html#c-ffi-types

I hope that helps!

Cheers,
Miguel

maintainer Applied
#4 lore raw
In reply to Abhinav Ananthu
Quoted 8 lines · Abhinav Ananthu

Update all unsafe extern "C" callback functions in the cpufreq module to use kernel::ffi types (c_int, c_uint, etc.) instead of Rust-native types like i32, u32, or usize.

This change ensures that all Rust callbacks have signatures that are ABI-compatible with their corresponding C counterparts, which is critical for FFI correctness and safety.

Quoted 6 lines
---
 rust/kernel/cpufreq.rs | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

Applied. Thanks.

There are few users of kernel::ffi::* in cpufreq.rs and opp.rs, if you want to convert them as well.

Signature · 1 line
viresh

A message that is not in the archive (sent off-list or not yet received): CAGR7w80V_Atc_DuF1RGOFHbvXd1V2j8mMbN3yg+Q6oytiJwNbw@mail.gmail.com

maintainer
#6 lore raw
Quoted 2 lines · Abhinav ananthu

sure , should I make two different patches for opp.rs and cpufreq.rs or make the changes in a single patch?

Two patches.

Signature · 1 line
viresh