mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Re: [PATCH v5 10/17] rust: workqueue: replace `raw_get` with pointer cast
@ 2025-03-23 10:38 Benno Lossin
  0 siblings, 0 replies; 2+ messages in thread
From: Benno Lossin @ 2025-03-23 10:38 UTC (permalink / raw)
  To: Antonio Hickey, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: rust-for-linux, linux-kernel

On Thu Mar 20, 2025 at 3:07 AM CET, Antonio Hickey wrote:
> 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.
>
> Suggested-by: Boqun Feng <boqun.feng@gmail.com>
> Link: https://lore.kernel.org/all/20250316061429.817126-1-contact@antoniohickey.com/T/#mc7a4757e8c132f84228b728c7d123d73841501d6
> Signed-off-by: Antonio Hickey <contact@antoniohickey.com>

One nit below, but regardless:

Reviewed-by: Benno Lossin <benno.lossin@proton.me>

> ---
>  rust/kernel/workqueue.rs | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
> index 4e27df324d26..5cc259c1e502 100644
> --- a/rust/kernel/workqueue.rs
> +++ b/rust/kernel/workqueue.rs
> @@ -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

> +        ptr.cast_mut().cast()
>      }
>  }
>  



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH v5 10/17] rust: workqueue: replace `raw_get` with pointer cast
       [not found] <20250320020740.1631171-1-contact@antoniohickey.com>
@ 2025-03-20  2:07 ` Antonio Hickey
  0 siblings, 0 replies; 2+ messages in thread
From: Antonio Hickey @ 2025-03-20  2:07 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: Antonio Hickey, rust-for-linux, linux-kernel

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.

Suggested-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/all/20250316061429.817126-1-contact@antoniohickey.com/T/#mc7a4757e8c132f84228b728c7d123d73841501d6
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
---
 rust/kernel/workqueue.rs | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index 4e27df324d26..5cc259c1e502 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -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`.
+        ptr.cast_mut().cast()
     }
 }
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-23 10:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-23 10:38 [PATCH v5 10/17] rust: workqueue: replace `raw_get` with pointer cast Benno Lossin
     [not found] <20250320020740.1631171-1-contact@antoniohickey.com>
2025-03-20  2:07 ` Antonio Hickey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®