Skip to content

Commit 7e9e7a9

Browse files
DarksonnBennoLossin
authored andcommitted
rust: redefine bindings::compat_ptr_ioctl in Rust
There is currently an inconsistency between C and Rust, which is that when Rust requires cfg(CONFIG_COMPAT) on compat_ioctl when using the compat_ptr_ioctl symbol because '#define compat_ptr_ioctl NULL' does not get translated to anything by bindgen. But it's not *just* a matter of translating the '#define' into Rust when CONFIG_COMPAT=n. This is because when CONFIG_COMPAT=y, the type of compat_ptr_ioctl is a non-nullable function pointer, and to seamlessly use it regardless of the config, we need a nullable function pointer. I think it's important to do something about this; I've seen the mistake of accidentally forgetting '#[cfg(CONFIG_COMPAT)]' when compat_ptr_ioctl is used multiple times now. This explicitly declares 'bindings::compat_ptr_ioctl' as an Option that is always defined but might be None. This matches C, but isn't ideal: it modifies the bindings crate. But I'm not sure if there's a better way to do it. If we just redefine in kernel/, then people may still use the one in bindings::, since that is where you would normally find it. I am open to suggestions. Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net>
1 parent c473d7a commit 7e9e7a9

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

drivers/android/binder/rust_binder_main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ pub static rust_binder_fops: AssertSync<kernel::bindings::file_operations> = {
314314
owner: THIS_MODULE.as_ptr(),
315315
poll: Some(rust_binder_poll),
316316
unlocked_ioctl: Some(rust_binder_ioctl),
317-
#[cfg(CONFIG_COMPAT)]
318-
compat_ioctl: Some(bindings::compat_ptr_ioctl),
317+
compat_ioctl: bindings::compat_ptr_ioctl,
319318
mmap: Some(rust_binder_mmap),
320319
open: Some(rust_binder_open),
321320
release: Some(rust_binder_release),

rust/bindings/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ mod bindings_helper {
6767
}
6868

6969
pub use bindings_raw::*;
70+
71+
pub const compat_ptr_ioctl: Option<
72+
unsafe extern "C" fn(*mut file, ffi::c_uint, ffi::c_ulong) -> ffi::c_long,
73+
> = {
74+
#[cfg(CONFIG_COMPAT)]
75+
{
76+
Some(bindings_raw::compat_ptr_ioctl)
77+
}
78+
#[cfg(not(CONFIG_COMPAT))]
79+
{
80+
None
81+
}
82+
};

rust/kernel/miscdevice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
411411
compat_ioctl: if T::HAS_COMPAT_IOCTL {
412412
Some(Self::compat_ioctl)
413413
} else if T::HAS_IOCTL {
414-
Some(bindings::compat_ptr_ioctl)
414+
bindings::compat_ptr_ioctl
415415
} else {
416416
None
417417
},

0 commit comments

Comments
 (0)