Skip to contentSkip to conversation

rust: workqueue: replace `raw_get` with pointer cast

PATCH Benno Lossin 1 participant 1 message Mar 23, 2025 Rust

lore
Share & tools
lore

Benno Lossin

author Reviewed-by
#0 lore raw
Quoted 7 lines · Antonio Hickey

Now that Work is initialized via pin-init, the work field is always used. This allows us to replace the use of unsafe Opaque::raw_get with direct pointer casting.

One nit below, but regardless:

Quoted 9 lines
---
 rust/kernel/workqueue.rs | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
rust/kernel/workqueue.rs +0 -0
index 4e27df324d26..5cc259c1e502 100644

rust/kernel/workqueue.rs pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self · line 402

Quoted 10 lines
rust/kernel/workqueue.rs +1 -6
@@ -399,12 +399,8 @@ pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self
     /// need not be initialized.)
     #[inline]
     pub unsafe fn raw_get(ptr: *const Self) -> *mut bindings::work_struct {
-        // SAFETY: The caller promises that the pointer is aligned and not dangling.
-        //
-        // A pointer cast would also be ok due to `#[repr(transparent)]`. We use
-        // `&raw const (*ptr).work` so that the compiler does not complain that the
-        // `work` field is unused.
-        unsafe { Opaque::raw_get(&raw const (*ptr).work) }
+        // CAST: `Work` is transparent to `bindings::work_struct`.

I usually write:

    // CAST: `Work` is transparently wrapping `bindings::work_struct`.

So feel free to use that. (I find the "transparent to" a bit strange, but this isn't a huge complaint)

---
Cheers,
Benno

Quoted 4 lines
rust/kernel/workqueue.rs +1 -0
+        ptr.cast_mut().cast()
     }
 }