* Re: [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro
@ 2025-03-23 10:32 Benno Lossin
0 siblings, 0 replies; 3+ messages in thread
From: Benno Lossin @ 2025-03-23 10:32 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:
> The use of `addr_of!` here is unnecessary since its immediately
> dereferenced. The main benefit of `addr_of!` is to avoid intermediate
> field loads without immediate dereferencing, so there's no benefit in
> using it here.
>
> We can achieve the same behavior by directly accessing the
> `group_leader` and `pid` fields, which is more idiomatic.
>
> Suggested-by: Benno Lossin <benno.lossin@proton.me>
> Link: https://github.com/Rust-for-Linux/linux/issues/1148
> Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
---
Cheers,
Benno
> ---
> rust/kernel/task.rs | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v5 00/17] refactor to utilize `&raw [const|mut]`
@ 2025-03-20 2:07 Antonio Hickey
2025-03-20 2:07 ` [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro Antonio Hickey
0 siblings, 1 reply; 3+ 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
This patch set enables the `raw_ref_op` feature, which became stable
in Rust 1.82.
It then replaces all occurences of `addr_of!(place)` and
`addr_of_mut!(place)` with `&raw const place` and `&raw mut place`.
Finally it adds the previous macros `addr_of!` and `addr_of_mut!` to
the disallowed macros in `.clippy.toml`.
Changes in v5:
- Fix doctest errors when compiling on the minimum Rust version (1.78)
due to not having the `raw_ref_op` feature enabled for doctests.
- Reword commit messages to more accurately describe the changes.
- Replace unsafe call to `Opaque::raw_get` with pointer casting
in workqueue.
- Fix clippy disallowed macros message for `core::ptr::addr_of`
- Link to v4: https://lore.kernel.org/all/20250316061429.817126-1-contact@antoniohickey.com/
Changes in v4:
- Fix comment typo.
- Fix clippy issues.
- Add more context and link for disallowed macros with clippy.
- Separate the patch replacing of `addr_of[_mut]!` macros with
`&raw [const|mut]` into smaller patches for each section.
(PATCH v3 2/3 -> PATCH v4 2/16 through 15/16)
- Fix email typo.
- Link to v3: https://lore.kernel.org/all/0100019597091f92-cb55b6cd-4d06-4d14-8d9c-1a1314949a00-000000@email.amazonses.com/
Changes in v3:
- Re ordered the patches, so that the patch adding the `addr_of[_mut]!`
macros to the disallowed macros in clippy is applied after replacing
all the instances of `addr_of_[_mut]` with `&raw [const|mut]`.
(moved PATCH v2 2/3 -> PATCH v3 3/3 and PATCH v2 3/3 -> PATCH v3 2/3)
- Link to v2: https://lore.kernel.org/all/0100019592c224ba-0cf38e16-2aa2-459d-99cd-09a463d616d4-000000@email.amazonses.com/
Changes in v2:
- Fix `&raw place` should be `&raw const place`
- Fix email typo
- Link to v1: https://lore.kernel.org/all/010001958dfeacb5-9039aaab-6114-494a-9f1d-f13982091169-000000@email.amazonses.com/
Suggested-by: Benno Lossin <benno.lossin@proton.me>
Link: https://github.com/Rust-for-Linux/linux/issues/1148
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
Antonio Hickey (17):
rust: enable `raw_ref_op` feature
rust: init: refactor to use `&raw [const|mut]`
rust: list: refactor to use `&raw [const|mut]`
rust: task: remove use of `addr_of!` macro
rust: faux: refactor to use `&raw [const|mut]`
rust: platform: refactor to use `&raw [const|mut]`
rust: pci: refactor to use `&raw [const|mut]`
rust: kunit: refactor to use `&raw [const|mut]`
rust: workqueue: refactor to use `&raw [const|mut]`
rust: workqueue: replace `raw_get` with pointer cast
rust: rbtree: refactor to use `&raw [const|mut]`
rust: net: phy: refactor to use `&raw [const|mut]`
rust: sync: arc: refactor to use `&raw [const|mut]`
rust: jump_label: refactor to use `&raw [const|mut]`
rust: fs: file: refactor to use `&raw [const|mut]`
rust: block: refactor to use `&raw [const|mut]`
rust: clippy: disallow `addr_of[_mut]!` macros
.clippy.toml | 4 ++++
rust/kernel/block/mq/request.rs | 4 ++--
rust/kernel/faux.rs | 4 ++--
rust/kernel/fs/file.rs | 2 +-
rust/kernel/init.rs | 8 ++++----
rust/kernel/init/macros.rs | 28 +++++++++++++-------------
rust/kernel/jump_label.rs | 4 ++--
rust/kernel/kunit.rs | 4 ++--
rust/kernel/lib.rs | 2 ++
rust/kernel/list.rs | 2 +-
rust/kernel/list/impl_list_item_mod.rs | 6 +++---
rust/kernel/net/phy.rs | 4 ++--
rust/kernel/pci.rs | 4 ++--
rust/kernel/platform.rs | 4 +---
rust/kernel/rbtree.rs | 22 ++++++++++----------
rust/kernel/sync/arc.rs | 2 +-
rust/kernel/task.rs | 4 ++--
rust/kernel/workqueue.rs | 9 +++------
scripts/Makefile.build | 4 ++--
19 files changed, 61 insertions(+), 60 deletions(-)
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro
2025-03-20 2:07 [PATCH v5 00/17] refactor to utilize `&raw [const|mut]` Antonio Hickey
@ 2025-03-20 2:07 ` Antonio Hickey
2025-03-23 19:23 ` Miguel Ojeda
0 siblings, 1 reply; 3+ 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
The use of `addr_of!` here is unnecessary since its immediately
dereferenced. The main benefit of `addr_of!` is to avoid intermediate
field loads without immediate dereferencing, so there's no benefit in
using it here.
We can achieve the same behavior by directly accessing the
`group_leader` and `pid` fields, which is more idiomatic.
Suggested-by: Benno Lossin <benno.lossin@proton.me>
Link: https://github.com/Rust-for-Linux/linux/issues/1148
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
---
rust/kernel/task.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 49012e711942..568b528e2cc4 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -257,7 +257,7 @@ pub fn as_ptr(&self) -> *mut bindings::task_struct {
pub fn group_leader(&self) -> &Task {
// SAFETY: The group leader of a task never changes after initialization, so reading this
// field is not a data race.
- let ptr = unsafe { *ptr::addr_of!((*self.as_ptr()).group_leader) };
+ let ptr = unsafe { (*self.as_ptr()).group_leader };
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
// and given that a task has a reference to its group leader, we know it must be valid for
@@ -269,7 +269,7 @@ pub fn group_leader(&self) -> &Task {
pub fn pid(&self) -> Pid {
// SAFETY: The pid of a task never changes after initialization, so reading this field is
// not a data race.
- unsafe { *ptr::addr_of!((*self.as_ptr()).pid) }
+ unsafe { (*self.as_ptr()).pid }
}
/// Returns the UID of the given task.
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro
2025-03-20 2:07 ` [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro Antonio Hickey
@ 2025-03-23 19:23 ` Miguel Ojeda
0 siblings, 0 replies; 3+ messages in thread
From: Miguel Ojeda @ 2025-03-23 19:23 UTC (permalink / raw)
To: Antonio Hickey
Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel
On Thu, Mar 20, 2025 at 3:08 AM Antonio Hickey
<contact@antoniohickey.com> wrote:
>
> Suggested-by: Benno Lossin <benno.lossin@proton.me>
> Link: https://github.com/Rust-for-Linux/linux/issues/1148
I think I would drop these two lines -- Benno definitely suggested
what triggered this series, but I think the Link does not apply to
this particular patch, and Boqun suggested this particular change in a
review anyway.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-03-23 19:24 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-23 10:32 [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro Benno Lossin
-- strict thread matches above, loose matches on Subject: below --
2025-03-20 2:07 [PATCH v5 00/17] refactor to utilize `&raw [const|mut]` Antonio Hickey
2025-03-20 2:07 ` [PATCH v5 04/17] rust: task: remove use of `addr_of!` macro Antonio Hickey
2025-03-23 19:23 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).