public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
@ 2025-11-13 14:45 Onur Özkan
  2025-11-17 12:13 ` Charalampos Mitrodimas
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Onur Özkan @ 2025-11-13 14:45 UTC (permalink / raw)
  To: rust-for-linux
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, dakr, yutaro.ono.418, charmitro,
	borys.tyran, daniel, tamird, linux-kernel, Onur Özkan

Refactors parts of the get() and cursor_lower_bound()
traversal logic to minimize the scope of unsafe blocks
and avoid duplicating same safety comments.

One of the removed comments was also misleading:

    // SAFETY: `node` is a non-null node...
    Ordering::Equal => return Some(unsafe { &(*this).value }),

as `node` should have been `this`.

No functional changes intended; this is purely a safety
improvement that reduces the amount of unsafe blocks
while keeping all invariants intact.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/rbtree.rs | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index b8fe6be6fcc4..d44f305e5161 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -384,14 +384,17 @@ pub fn get(&self, key: &K) -> Option<&V> {
             // SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
             // point to the links field of `Node<K, V>` objects.
             let this = unsafe { container_of!(node, Node<K, V>, links) };
+
             // SAFETY: `this` is a non-null node so it is valid by the type invariants.
-            node = match key.cmp(unsafe { &(*this).key }) {
-                // SAFETY: `node` is a non-null node so it is valid by the type invariants.
-                Ordering::Less => unsafe { (*node).rb_left },
-                // SAFETY: `node` is a non-null node so it is valid by the type invariants.
-                Ordering::Greater => unsafe { (*node).rb_right },
-                // SAFETY: `node` is a non-null node so it is valid by the type invariants.
-                Ordering::Equal => return Some(unsafe { &(*this).value }),
+            let this_ref = unsafe { &*this };
+
+            // SAFETY: `node` is a non-null node so it is valid by the type invariants.
+            let node_ref = unsafe { &*node };
+
+            node = match key.cmp(&this_ref.key) {
+                Ordering::Less => node_ref.rb_left,
+                Ordering::Greater => node_ref.rb_right,
+                Ordering::Equal => return Some(&this_ref.value),
             }
         }
         None
@@ -433,17 +436,17 @@ pub fn cursor_lower_bound(&mut self, key: &K) -> Option<Cursor<'_, K, V>>
             let this = unsafe { container_of!(node, Node<K, V>, links) };
             // SAFETY: `this` is a non-null node so it is valid by the type invariants.
             let this_key = unsafe { &(*this).key };
+
             // SAFETY: `node` is a non-null node so it is valid by the type invariants.
-            let left_child = unsafe { (*node).rb_left };
-            // SAFETY: `node` is a non-null node so it is valid by the type invariants.
-            let right_child = unsafe { (*node).rb_right };
+            let node_ref = unsafe { &*node };
+
             match key.cmp(this_key) {
                 Ordering::Equal => {
                     best_match = NonNull::new(this);
                     break;
                 }
                 Ordering::Greater => {
-                    node = right_child;
+                    node = node_ref.rb_right;
                 }
                 Ordering::Less => {
                     let is_better_match = match best_match {
@@ -457,7 +460,7 @@ pub fn cursor_lower_bound(&mut self, key: &K) -> Option<Cursor<'_, K, V>>
                     if is_better_match {
                         best_match = NonNull::new(this);
                     }
-                    node = left_child;
+                    node = node_ref.rb_left;
                 }
             };
         }
-- 
2.51.2


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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2025-11-13 14:45 [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs Onur Özkan
@ 2025-11-17 12:13 ` Charalampos Mitrodimas
  2026-01-18 21:03   ` Miguel Ojeda
  2026-01-14  8:42 ` Alice Ryhl
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Charalampos Mitrodimas @ 2025-11-17 12:13 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, yutaro.ono.418,
	borys.tyran, daniel, tamird, linux-kernel

Onur Özkan <work@onurozkan.dev> writes:

> Refactors parts of the get() and cursor_lower_bound()
> traversal logic to minimize the scope of unsafe blocks
> and avoid duplicating same safety comments.
>
> One of the removed comments was also misleading:
>
>     // SAFETY: `node` is a non-null node...
>     Ordering::Equal => return Some(unsafe { &(*this).value }),
>
> as `node` should have been `this`.
>
> No functional changes intended; this is purely a safety
> improvement that reduces the amount of unsafe blocks
> while keeping all invariants intact.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
>  rust/kernel/rbtree.rs | 27 +++++++++++++++------------
>  1 file changed, 15 insertions(+), 12 deletions(-)
>

This is a nice change to have if it reaches the general consensus.

Acked-By: Charalampos Mitrodimas <charmitro@posteo.net>

--
C. Mitrodimas

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2025-11-13 14:45 [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs Onur Özkan
  2025-11-17 12:13 ` Charalampos Mitrodimas
@ 2026-01-14  8:42 ` Alice Ryhl
  2026-01-18 21:02 ` Miguel Ojeda
  2026-01-19  8:21 ` Miguel Ojeda
  3 siblings, 0 replies; 9+ messages in thread
From: Alice Ryhl @ 2026-01-14  8:42 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, tmgross, dakr, yutaro.ono.418, charmitro,
	borys.tyran, daniel, tamird, linux-kernel

On Thu, Nov 13, 2025 at 3:46 PM Onur Özkan <work@onurozkan.dev> wrote:
>
> Refactors parts of the get() and cursor_lower_bound()
> traversal logic to minimize the scope of unsafe blocks
> and avoid duplicating same safety comments.
>
> One of the removed comments was also misleading:
>
>     // SAFETY: `node` is a non-null node...
>     Ordering::Equal => return Some(unsafe { &(*this).value }),
>
> as `node` should have been `this`.
>
> No functional changes intended; this is purely a safety
> improvement that reduces the amount of unsafe blocks
> while keeping all invariants intact.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

One consequence of creating a &_ to the bindings::rb_node struct means
that we assert immutability for the entire struct and not just the
rb_left/rb_right fields, but I have verified that this is ok.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2025-11-13 14:45 [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs Onur Özkan
  2025-11-17 12:13 ` Charalampos Mitrodimas
  2026-01-14  8:42 ` Alice Ryhl
@ 2026-01-18 21:02 ` Miguel Ojeda
  2026-01-18 21:27   ` Alice Ryhl
  2026-01-19  8:21 ` Miguel Ojeda
  3 siblings, 1 reply; 9+ messages in thread
From: Miguel Ojeda @ 2026-01-18 21:02 UTC (permalink / raw)
  To: Onur Özkan, aliceryhl
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, tmgross, dakr, yutaro.ono.418, charmitro,
	borys.tyran, daniel, tamird, linux-kernel

On Thu, Nov 13, 2025 at 3:46 PM Onur Özkan <work@onurozkan.dev> wrote:
>
> Refactors parts of the get() and cursor_lower_bound()

This is now `find_best_match()` given commit f56b13172382 ("rust:
rbtree: add immutable cursor").

Alice: I assume your analysis/review still applies even after Vitaly's
refactor -- if not, please shout!

Cheers,
Miguel

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2025-11-17 12:13 ` Charalampos Mitrodimas
@ 2026-01-18 21:03   ` Miguel Ojeda
  2026-01-19  8:36     ` Charalampos Mitrodimas
  0 siblings, 1 reply; 9+ messages in thread
From: Miguel Ojeda @ 2026-01-18 21:03 UTC (permalink / raw)
  To: Charalampos Mitrodimas
  Cc: Onur Özkan, rust-for-linux, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
	yutaro.ono.418, borys.tyran, daniel, tamird, linux-kernel

On Mon, Nov 17, 2025 at 1:13 PM Charalampos Mitrodimas
<charmitro@posteo.net> wrote:
>
> This is a nice change to have if it reaches the general consensus.
>
> Acked-By: Charalampos Mitrodimas <charmitro@posteo.net>

So, typically, Acked-by tags are given by maintainers of the code.
Sometimes, other stakeholders give it too:

  https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by

I will keep the tag, but if this was meant to be a Reviewed-by or
similar instead, please let me know.

(Nit: it is usually -by, not -By).

Cheers,
Miguel

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2026-01-18 21:02 ` Miguel Ojeda
@ 2026-01-18 21:27   ` Alice Ryhl
  0 siblings, 0 replies; 9+ messages in thread
From: Alice Ryhl @ 2026-01-18 21:27 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Onur Özkan, rust-for-linux, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, tmgross, dakr,
	yutaro.ono.418, charmitro, borys.tyran, daniel, tamird,
	linux-kernel

On Sun, Jan 18, 2026 at 10:02 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Thu, Nov 13, 2025 at 3:46 PM Onur Özkan <work@onurozkan.dev> wrote:
> >
> > Refactors parts of the get() and cursor_lower_bound()
>
> This is now `find_best_match()` given commit f56b13172382 ("rust:
> rbtree: add immutable cursor").
>
> Alice: I assume your analysis/review still applies even after Vitaly's
> refactor -- if not, please shout!

It's still fine, thanks.

Alice

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2025-11-13 14:45 [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs Onur Özkan
                   ` (2 preceding siblings ...)
  2026-01-18 21:02 ` Miguel Ojeda
@ 2026-01-19  8:21 ` Miguel Ojeda
  3 siblings, 0 replies; 9+ messages in thread
From: Miguel Ojeda @ 2026-01-19  8:21 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, yutaro.ono.418,
	charmitro, borys.tyran, daniel, tamird, linux-kernel

On Thu, Nov 13, 2025 at 3:46 PM Onur Özkan <work@onurozkan.dev> wrote:
>
> Refactors parts of the get() and cursor_lower_bound()
> traversal logic to minimize the scope of unsafe blocks
> and avoid duplicating same safety comments.
>
> One of the removed comments was also misleading:
>
>     // SAFETY: `node` is a non-null node...
>     Ordering::Equal => return Some(unsafe { &(*this).value }),
>
> as `node` should have been `this`.
>
> No functional changes intended; this is purely a safety
> improvement that reduces the amount of unsafe blocks
> while keeping all invariants intact.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

Applied to `rust-next` -- thanks everyone!

    [ Alice writes:

        "One consequence of creating a &_ to the bindings::rb_node struct means
        that we assert immutability for the entire struct and not just the
        rb_left/rb_right fields, but I have verified that this is ok."

          - Miguel ]

    [ Reworded title and replaced `cursor_lower_bound()` with
      `find_best_match()` in message. - Miguel ]

Cheers,
Miguel

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2026-01-18 21:03   ` Miguel Ojeda
@ 2026-01-19  8:36     ` Charalampos Mitrodimas
  2026-01-19  8:39       ` Miguel Ojeda
  0 siblings, 1 reply; 9+ messages in thread
From: Charalampos Mitrodimas @ 2026-01-19  8:36 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Onur Özkan, rust-for-linux, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
	yutaro.ono.418, borys.tyran, daniel, tamird, linux-kernel

Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> writes:

> On Mon, Nov 17, 2025 at 1:13 PM Charalampos Mitrodimas
> <charmitro@posteo.net> wrote:
>>
>> This is a nice change to have if it reaches the general consensus.
>>
>> Acked-By: Charalampos Mitrodimas <charmitro@posteo.net>
>
> So, typically, Acked-by tags are given by maintainers of the code.
> Sometimes, other stakeholders give it too:
>
>   https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by
>
> I will keep the tag, but if this was meant to be a Reviewed-by or
> similar instead, please let me know.

Hi Miguel,

I was not aware, thanks for the heads up! Assume it is a Reviewed-by or
just discard since it's invalid.

>
> (Nit: it is usually -by, not -By).

Ah yes, need to update my email client snippet configuration.

C. Mitrodimas

>
> Cheers,
> Miguel

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

* Re: [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs
  2026-01-19  8:36     ` Charalampos Mitrodimas
@ 2026-01-19  8:39       ` Miguel Ojeda
  0 siblings, 0 replies; 9+ messages in thread
From: Miguel Ojeda @ 2026-01-19  8:39 UTC (permalink / raw)
  To: Charalampos Mitrodimas
  Cc: Onur Özkan, rust-for-linux, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
	yutaro.ono.418, borys.tyran, daniel, tamird, linux-kernel

On Mon, Jan 19, 2026 at 9:36 AM Charalampos Mitrodimas
<charmitro@posteo.net> wrote:
>
> I was not aware, thanks for the heads up! Assume it is a Reviewed-by or
> just discard since it's invalid.

Ah, great, thanks for the quick reply -- updated to Reviewed-by in the
commit then.

Cheers,
Miguel

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

end of thread, other threads:[~2026-01-19  8:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-13 14:45 [PATCH v1] rbtree: reduce unsafe blocks on pointer derefs Onur Özkan
2025-11-17 12:13 ` Charalampos Mitrodimas
2026-01-18 21:03   ` Miguel Ojeda
2026-01-19  8:36     ` Charalampos Mitrodimas
2026-01-19  8:39       ` Miguel Ojeda
2026-01-14  8:42 ` Alice Ryhl
2026-01-18 21:02 ` Miguel Ojeda
2026-01-18 21:27   ` Alice Ryhl
2026-01-19  8:21 ` Miguel Ojeda

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