* [PATCH] rust: rbtree: call rb_erase() before KBox::from_raw() in remove_current()
@ 2026-11-27 12:05 Bilal Hashim
2026-12-01 13:50 ` Alice Ryhl
0 siblings, 1 reply; 2+ messages in thread
From: Bilal Hashim @ 2026-11-27 12:05 UTC (permalink / raw)
To: Miguel Ojeda, Alice Ryhl, Boqun Feng
Cc: rust-for-linux, linux-kernel, Bilal Hashim
In CursorMut::remove_current(), KBox::from_raw(this) is currently called
before bindings::rb_erase(&mut (*this).links, ...). Constructing and
moving a KBox asserts exclusive (noalias) ownership over the allocation,
which invalidates the raw pointer `this` and aliases with the subsequent
bindings::rb_erase() call that mutates the rb_node links in C.
Move bindings::rb_erase() before KBox::from_raw(this), matching the order
already used in CursorMut::remove_neighbor().
Tested with LLVM=-22 and Rust KUnit doctests in QEMU (372/372 passed,
including all 11 rbtree doctests).
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -918,15 +918,15 @@
pub fn remove_current(self) -> (Option<Self>, RBTreeNode<K, V>) {
let prev = self.get_neighbor_raw(Direction::Prev);
let next = self.get_neighbor_raw(Direction::Next);
+ // SAFETY: The reference to the tree used to create the cursor outlives the cursor, so
+ // the tree cannot change. By the tree invariant, all nodes are valid.
+ unsafe { bindings::rb_erase(self.current.as_ptr(), addr_of_mut!(self.tree.root)) };
// 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!(self.current.as_ptr(), Node<K, V>, links) };
// SAFETY: `this` is valid by the type invariants as described above.
let node = unsafe { KBox::from_raw(this) };
let node = RBTreeNode { node };
- // SAFETY: The reference to the tree used to create the cursor outlives the cursor, so
- // the tree cannot change. By the tree invariant, all nodes are valid.
- unsafe { bindings::rb_erase(&mut (*this).links, addr_of_mut!(self.tree.root)) };
// INVARIANT:
// - `current` is a valid node in the [`RBTree`] pointed to by `self.tree`.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] rust: rbtree: call rb_erase() before KBox::from_raw() in remove_current()
2026-11-27 12:05 [PATCH] rust: rbtree: call rb_erase() before KBox::from_raw() in remove_current() Bilal Hashim
@ 2026-12-01 13:50 ` Alice Ryhl
0 siblings, 0 replies; 2+ messages in thread
From: Alice Ryhl @ 2026-12-01 13:50 UTC (permalink / raw)
To: Bilal Hashim
Cc: Alice Ryhl, Miguel Ojeda, Boqun Feng, rust-for-linux, linux-kernel
On Fri, Nov 27, 2026 at 08:05:53PM +0800, Bilal Hashim wrote:
> In CursorMut::remove_current(), KBox::from_raw(this) is currently called
> before bindings::rb_erase(&mut (*this).links, ...). Constructing and
> moving a KBox asserts exclusive (noalias) ownership over the allocation,
> which invalidates the raw pointer `this` and aliases with the subsequent
> bindings::rb_erase() call that mutates the rb_node links in C.
>
> Move bindings::rb_erase() before KBox::from_raw(this), matching the order
> already used in CursorMut::remove_neighbor().
>
> Tested with LLVM=-22 and Rust KUnit doctests in QEMU (372/372 passed,
> including all 11 rbtree doctests).
>
> Signed-off-by: Bilal Hashim <AeeSec@126.com>
Note that `KBox` doesn't actually emit `noalias` today, but it is of
course still semantically wrong.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Alice
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 13:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 12:05 [PATCH] rust: rbtree: call rb_erase() before KBox::from_raw() in remove_current() Bilal Hashim
2026-12-01 13:50 ` Alice Ryhl
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®