[PATCH 6/6] rust: hrtimer: Make HrTimer repr(transparent)
Andreas Hindborg <[email protected]>
| Newsgroups | org.kernel.vger.rust-for-linux,org.freedesktop.lists.dri-devel,org.freedesktop.lists.intel-gfx,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
From: FUJITA Tomonori <[email protected]> HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound. Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there. repr(transparent) cannot be combined with repr(C), so drop the latter. Suggested-by: Miguel Ojeda <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Signed-off-by: FUJITA Tomonori <[email protected]> Link: https://msgid.link/[email protected] Signed-off-by: Andreas Hindborg <[email protected]> --- rust/kernel/time/hrtimer.rs | 6 +++++- rust/kernel/time/hrtimer/arc.rs | 2 +- rust/kernel/time/hrtimer/pin.rs | 2 +- rust/kernel/time/hrtimer/pin_mut.rs | 2 +- rust/kernel/time/hrtimer/tbox.rs | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 2a9abc9f5d8c..ab7c568b8855 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -427,8 +427,12 @@ /// # Invariants /// /// * `self.timer` is initialized by `bindings::hrtimer_setup_ext`. +// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a +// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of +// this type sits inside `Opaque`. Being transparent rejects a second field at compile time, +// but it does not enforce that the remaining field stays `Opaque`. #[pin_data] -#[repr(C)] +#[repr(transparent)] pub struct HrTimer<T> { #[pin] timer: Opaque<bindings::hrtimer>, diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc.rs index 8a9fcb5c69e6..46ccff9e0024 100644 --- a/rust/kernel/time/hrtimer/arc.rs +++ b/rust/kernel/time/hrtimer/arc.rs @@ -84,7 +84,7 @@ impl<T> RawHrTimerCallback for Arc<T> expires: bindings::ktime_t, fwd: *mut bindings::hrtimer_forward_args, ) -> bindings::hrtimer_restart { - // `HrTimer` is `repr(C)` + // `HrTimer` is `repr(transparent)` let timer_ptr = ptr.cast::<super::HrTimer<T>>(); // SAFETY: By C API contract `ptr` is the pointer we passed when diff --git a/rust/kernel/time/hrtimer/pin.rs b/rust/kernel/time/hrtimer/pin.rs index d1143f278f31..5fd374fdc480 100644 --- a/rust/kernel/time/hrtimer/pin.rs +++ b/rust/kernel/time/hrtimer/pin.rs @@ -87,7 +87,7 @@ impl<'a, T> RawHrTimerCallback for Pin<&'a T> expires: bindings::ktime_t, fwd: *mut bindings::hrtimer_forward_args, ) -> bindings::hrtimer_restart { - // `HrTimer` is `repr(C)` + // `HrTimer` is `repr(transparent)` let timer_ptr = ptr.cast::<HrTimer<T>>(); // SAFETY: By the safety requirement of this function, `timer_ptr` diff --git a/rust/kernel/time/hrtimer/pin_mut.rs b/rust/kernel/time/hrtimer/pin_mut.rs index 04f9d8cbddcd..2bba3c41d6e9 100644 --- a/rust/kernel/time/hrtimer/pin_mut.rs +++ b/rust/kernel/time/hrtimer/pin_mut.rs @@ -91,7 +91,7 @@ impl<'a, T> RawHrTimerCallback for Pin<&'a mut T> expires: bindings::ktime_t, fwd: *mut bindings::hrtimer_forward_args, ) -> bindings::hrtimer_restart { - // `HrTimer` is `repr(C)` + // `HrTimer` is `repr(transparent)` let timer_ptr = ptr.cast::<HrTimer<T>>(); // SAFETY: By the safety requirement of this function, `timer_ptr` diff --git a/rust/kernel/time/hrtimer/tbox.rs b/rust/kernel/time/hrtimer/tbox.rs index c7f86909e21b..399ad7677043 100644 --- a/rust/kernel/time/hrtimer/tbox.rs +++ b/rust/kernel/time/hrtimer/tbox.rs @@ -107,7 +107,7 @@ impl<T, A> RawHrTimerCallback for Pin<Box<T, A>> expires: bindings::ktime_t, fwd: *mut bindings::hrtimer_forward_args, ) -> bindings::hrtimer_restart { - // `HrTimer` is `repr(C)` + // `HrTimer` is `repr(transparent)` let timer_ptr = ptr.cast::<super::HrTimer<T>>(); // SAFETY: By C API contract `ptr` is the pointer we passed when -- 2.51.2