Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>
Link: https://patch.msgid.link/20260105-redefine-compat_ptr_ioctl-v1-1-25edb3d91acc@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alice Ryhl and committed by
Greg Kroah-Hartman
68aabb29 b0e930a6

+15 -3
+1 -2
drivers/android/binder/rust_binder_main.rs
··· 322 322 owner: THIS_MODULE.as_ptr(), 323 323 poll: Some(rust_binder_poll), 324 324 unlocked_ioctl: Some(rust_binder_ioctl), 325 - #[cfg(CONFIG_COMPAT)] 326 - compat_ioctl: Some(bindings::compat_ptr_ioctl), 325 + compat_ioctl: bindings::compat_ptr_ioctl, 327 326 mmap: Some(rust_binder_mmap), 328 327 open: Some(rust_binder_open), 329 328 release: Some(rust_binder_release),
+13
rust/bindings/lib.rs
··· 67 67 } 68 68 69 69 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 + };
+1 -1
rust/kernel/miscdevice.rs
··· 410 410 compat_ioctl: if T::HAS_COMPAT_IOCTL { 411 411 Some(Self::compat_ioctl) 412 412 } else if T::HAS_IOCTL { 413 - Some(bindings::compat_ptr_ioctl) 413 + bindings::compat_ptr_ioctl 414 414 } else { 415 415 None 416 416 },