From: Bilal Hashim <AeeSec@126.com>
To: Miguel Ojeda <ojeda@kernel.org>,
Alice Ryhl <aliceryhl@google.com>, Boqun Feng <boqun@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Bilal Hashim <AeeSec@126.com>
Subject: [PATCH] rust: rbtree: call rb_erase() before KBox::from_raw() in remove_current()
Date: Fri, 27 Nov 2026 20:05:53 +0800 [thread overview]
Message-ID: <20261127120553.1233-1-AeeSec@126.com> (raw)
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`.
next reply other threads:[~2026-11-27 12:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 12:05 Bilal Hashim [this message]
2026-12-01 13:50 ` [PATCH] rust: rbtree: call rb_erase() before KBox::from_raw() in remove_current() Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127120553.1233-1-AeeSec@126.com \
--to=aeesec@126.com \
--cc=aliceryhl@google.com \
--cc=boqun@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.