From nobody Thu Sep 19 01:24:04 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6723D14A08D for ; Fri, 26 Jul 2024 19:57:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722023881; cv=none; b=EECueKv7iLsLl2m9GJXkUvuxQTS8DVa4k4xwvqnA6gLT4A7+qliue1UNPz1C3hTo/vuwz4Q9NFev0alhhPaQtyKZAaM/vmGUPDhbx2oj2npIzEjAw67ajzRRDNvAQPS5PHenbR3WkSvx83Wbbkw2ImbcdYNpjYrFJF9jsynP5nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722023881; c=relaxed/simple; bh=YlJB5S4znumNDllZHof+0NmmQ4v4IlYqKiGjmVkimaM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CrQ62p/tNf40CyxXUUZlr17fHdIOQzoKbwpBI+Vgjm5ozNb5nYAJpQEBd96MYzraOWNkMyehmnrGlyh0UA3hEYCyseH0suc3roWoR8Q3LHUR3hLqA37oDTqonJmPFdbuhP2rUtaNZXOdzeKUuF8z2vGNTD+lhYSGXDvFh5aPXDw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mattgilbride.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=r+iXl8TY; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mattgilbride.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="r+iXl8TY" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0353b731b8so104871276.2 for ; Fri, 26 Jul 2024 12:57:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722023878; x=1722628678; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5iuSDXVb2/pesIXeo/fDaEA7XP6L6y8oWIFuWGtUIt0=; b=r+iXl8TYPYjtfl0TFrn90R8U36ROUxqBiNu7kGOyms9AxW1v3Xb/MWyXwqCVWND6YH scux526OaJKHdyHZX4+h/xZBtSGdyIpgboGoHgmoBeWPmeYb4TA9L3BDG0DxehEu7yja dzWiykr5rd4o5FKuMmuNic1DC5qfER+WKnaYldIBFev5ZC4cwh4oX2VW2Bdb3BOr6Jlb AvClc3X3NnSzsq9aIusTZ5EP9/04BQWzRih3zvEosUUvbE8PmNwLH+/ERGbzc81D/nZo AZFjYep07BjZ3FA5m/o1G8wF2Ytq+Hh2sXYs31Wu5AfD2BegiVU2EE/TL3hKAhg48riQ 3hBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722023878; x=1722628678; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5iuSDXVb2/pesIXeo/fDaEA7XP6L6y8oWIFuWGtUIt0=; b=v609BmjYk/1c/rQ8UAcsTkoI+irxeQgVQqlfe7pNeR8RfNkZaoDKxpY8/Q6mvVIY+t ia1dm8INuu/0VDH0FCetKoNMlJ1OyOdL6hXZJJGndOXpP0DRlNKurJLgP1BxIXJgs6xS 1juWav3s1qmnO1tmqt5DMJs0GUAeJzOGDarokkTGjbJ0Gq7pl+Kdcp94XZLrJR7XSV7f ZKPYwN+Ld5WXgRKVmvFDJiExfyELLoH02NoaIMfNQcT7w8MWjkf4vmtFFD95x8v79Fc/ GD5C1SnYtqBi0uX2BGgOEgD0L2IVC7MJW4UmvrcGnTSUAUIG+QfoKhLq1KOWyqylGZ3P RAWw== X-Forwarded-Encrypted: i=1; AJvYcCUTEGSbJgbBBx8FR4nWj9fxVdOJQ/9aarZnzmWQWPd9H0qHEJLr4GAzH9u7OsRN6MM4JqlqMJqy1sFaDX/M9xxxXdBztFQt0aVcHy88 X-Gm-Message-State: AOJu0Yz1SiR2Au+mlyyqcwh2AOyg1P7LTQlk5OujrN6lwBXFu96XjNcU ytbw2jNkE0om6gS/7QVWCOYqXgtbGvCLwvx6JILvu0x8XQt/E3YpX0skaZAPhYRWw0gT8nyUAJY 52EHHoynbPfZAzBJstHgoykPodg== X-Google-Smtp-Source: AGHT+IFkRTh9HZ3eVuZO3vsYMvvsq09tsJhhns3b63+3vyk89h3guMwYDDNIp1EhFRLRfdGKKHkeMwjWQb8OsdPC6Pk= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:2b8f:b0:e03:59e2:e82 with SMTP id 3f1490d57ef6-e0b545a3f3dmr1133276.10.1722023878276; Fri, 26 Jul 2024 12:57:58 -0700 (PDT) Date: Fri, 26 Jul 2024 19:57:45 +0000 In-Reply-To: <20240726-b4-rbtree-v7-0-aee88caaf97c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240726-b4-rbtree-v7-0-aee88caaf97c@google.com> X-Mailer: b4 0.13.0 Message-ID: <20240726-b4-rbtree-v7-6-aee88caaf97c@google.com> Subject: [PATCH v7 6/6] rust: rbtree: add `RBTree::entry` From: Matt Gilbride To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Greg Kroah-Hartman , "=?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Christian Brauner Cc: Rob Landley , Davidlohr Bueso , Michel Lespinasse , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matt Gilbride Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Alice Ryhl This mirrors the entry API [1] from the Rust standard library on `RBTree`. This API can be used to access the entry at a specific key and make modifications depending on whether the key is vacant or occupied. This API is useful because it can often be used to avoid traversing the tree multiple times. This is used by binder to look up and conditionally access or insert a value, depending on whether it is there or not [2]. Link: https://doc.rust-lang.org/stable/std/collections/btree_map/enum.Entry= .html [1] Link: https://android-review.googlesource.com/c/kernel/common/+/2849906 [2] Signed-off-by: Alice Ryhl Tested-by: Alice Ryhl Signed-off-by: Matt Gilbride --- rust/kernel/rbtree.rs | 302 +++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 227 insertions(+), 75 deletions(-) diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs index 5611ae903779..f6d5162e46f5 100644 --- a/rust/kernel/rbtree.rs +++ b/rust/kernel/rbtree.rs @@ -295,12 +295,19 @@ pub fn try_create_and_insert( /// key/value pair). Returns [`None`] if a node with the same key didn= 't already exist. /// /// This function always succeeds. - pub fn insert(&mut self, RBTreeNode { node }: RBTreeNode) -> Opt= ion> { - let node =3D Box::into_raw(node); - // SAFETY: `node` is valid at least until we call `Box::from_raw`,= which only happens when - // the node is removed or replaced. - let node_links =3D unsafe { addr_of_mut!((*node).links) }; + pub fn insert(&mut self, node: RBTreeNode) -> Option> { + match self.raw_entry(&node.node.key) { + RawEntry::Occupied(entry) =3D> Some(entry.replace(node)), + RawEntry::Vacant(entry) =3D> { + entry.insert(node); + None + } + } + } =20 + fn raw_entry(&mut self, key: &K) -> RawEntry<'_, K, V> { + let raw_self: *mut RBTree =3D self; + // The returned `RawEntry` is used to call either `rb_link_node` o= r `rb_replace_node`. // The parameters of `rb_link_node` are as follows: // - `node`: A pointer to an uninitialized node being inserted. // - `parent`: A pointer to an existing node in the tree. One of i= ts child pointers must be @@ -319,62 +326,56 @@ pub fn insert(&mut self, RBTreeNode { node }: RBTreeN= ode) -> Option`. + let node =3D unsafe { container_of!(curr, Node, links) }; =20 - // We need to determine whether `node` should be the left or r= ight child of `parent`, - // so we will compare with the `key` field of `parent` a.k.a. = `this` below. - // - // SAFETY: By the type invariant of `Self`, all non-null `rb_n= ode` pointers stored in `self` - // point to the links field of `Node` objects. - let this =3D unsafe { container_of!(parent, Node, links)= }; - - // SAFETY: `this` is a non-null node so it is valid by the typ= e invariants. `node` is - // valid until the node is removed. - match unsafe { (*node).key.cmp(&(*this).key) } { - // We would like `node` to be the left child of `parent`. = Move to this child to check - // whether we can use it, or continue searching, at the ne= xt iteration. - // - // SAFETY: `parent` is a non-null node so it is valid by t= he type invariants. - Ordering::Less =3D> child_field_of_parent =3D unsafe { &mu= t (*parent).rb_left }, - // We would like `node` to be the right child of `parent`.= Move to this child to check - // whether we can use it, or continue searching, at the ne= xt iteration. - // - // SAFETY: `parent` is a non-null node so it is valid by t= he type invariants. - Ordering::Greater =3D> child_field_of_parent =3D unsafe { = &mut (*parent).rb_right }, + // SAFETY: `node` is a non-null node so it is valid by the typ= e invariants. + match key.cmp(unsafe { &(*node).key }) { + // SAFETY: `curr` is a non-null node so it is valid by the= type invariants. + Ordering::Less =3D> child_field_of_parent =3D unsafe { &mu= t (*curr).rb_left }, + // SAFETY: `curr` is a non-null node so it is valid by the= type invariants. + Ordering::Greater =3D> child_field_of_parent =3D unsafe { = &mut (*curr).rb_right }, Ordering::Equal =3D> { - // There is an existing node in the tree with this key= , and that node is - // parent. Thus, we are replacing parent with a new n= ode. - // - // INVARIANT: We are replacing an existing node with a= new one, which is valid. - // It remains valid because we "forgot" it with `Box::= into_raw`. - // SAFETY: All pointers are non-null and valid. - unsafe { bindings::rb_replace_node(parent, node_links,= &mut self.root) }; - - // INVARIANT: The node is being returned and the calle= r may free it, however, - // it was removed from the tree. So the invariants sti= ll hold. - return Some(RBTreeNode { - // SAFETY: `this` was a node in the tree, so it is= valid. - node: unsafe { Box::from_raw(this.cast_mut()) }, - }); + return RawEntry::Occupied(OccupiedEntry { + rbtree: self, + node_links: curr, + }) } } + parent =3D curr; } =20 - // INVARIANT: We are linking in a new node, which is valid. It rem= ains valid because we - // "forgot" it with `Box::into_raw`. - // SAFETY: All pointers are non-null and valid (`*child_field_of_p= arent` is null, but `child_field_of_parent` is a - // mutable reference). - unsafe { bindings::rb_link_node(node_links, parent, child_field_of= _parent) }; + RawEntry::Vacant(RawVacantEntry { + rbtree: raw_self, + parent, + child_field_of_parent, + _phantom: PhantomData, + }) + } =20 - // SAFETY: All pointers are valid. `node` has just been inserted i= nto the tree. - unsafe { bindings::rb_insert_color(node_links, &mut self.root) }; - None + /// Gets the given key's corresponding entry in the map for in-place m= anipulation. + pub fn entry(&mut self, key: K) -> Entry<'_, K, V> { + match self.raw_entry(&key) { + RawEntry::Occupied(entry) =3D> Entry::Occupied(entry), + RawEntry::Vacant(entry) =3D> Entry::Vacant(VacantEntry { raw: = entry, key }), + } + } + + /// Used for accessing the given node, if it exists. + pub fn find_mut(&mut self, key: &K) -> Option>= { + match self.raw_entry(key) { + RawEntry::Occupied(entry) =3D> Some(entry), + RawEntry::Vacant(_entry) =3D> None, + } } =20 - /// Returns a node with the given key, if one exists. - fn find(&self, key: &K) -> Option>> { + /// Returns a reference to the value corresponding to the key. + pub fn get(&self, key: &K) -> Option<&V> { let mut node =3D self.root.rb_node; while !node.is_null() { // SAFETY: By the type invariant of `Self`, all non-null `rb_n= ode` pointers stored in `self` @@ -386,47 +387,30 @@ fn find(&self, key: &K) -> Option>= > { Ordering::Less =3D> unsafe { (*node).rb_left }, // SAFETY: `node` is a non-null node so it is valid by the= type invariants. Ordering::Greater =3D> unsafe { (*node).rb_right }, - Ordering::Equal =3D> return NonNull::new(this.cast_mut()), + // SAFETY: `node` is a non-null node so it is valid by the= type invariants. + Ordering::Equal =3D> return Some(unsafe { &(*this).value }= ), } } None } =20 - /// Returns a reference to the value corresponding to the key. - pub fn get(&self, key: &K) -> Option<&V> { - // SAFETY: The `find` return value is a node in the tree, so it is= valid. - self.find(key).map(|node| unsafe { &node.as_ref().value }) - } - /// Returns a mutable reference to the value corresponding to the key. pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { - // SAFETY: The `find` return value is a node in the tree, so it is= valid. - self.find(key) - .map(|mut node| unsafe { &mut node.as_mut().value }) + self.find_mut(key).map(|node| node.into_mut()) } =20 /// Removes the node with the given key from the tree. /// /// It returns the node that was removed if one exists, or [`None`] ot= herwise. - fn remove_node(&mut self, key: &K) -> Option> { - let mut node =3D self.find(key)?; - - // SAFETY: The `find` return value is a node in the tree, so it is= valid. - unsafe { bindings::rb_erase(&mut node.as_mut().links, &mut self.ro= ot) }; - - // INVARIANT: The node is being returned and the caller may free i= t, however, it was - // removed from the tree. So the invariants still hold. - Some(RBTreeNode { - // SAFETY: The `find` return value was a node in the tree, so = it is valid. - node: unsafe { Box::from_raw(node.as_ptr()) }, - }) + pub fn remove_node(&mut self, key: &K) -> Option> { + self.find_mut(key).map(OccupiedEntry::remove_node) } =20 /// Removes the node with the given key from the tree. /// /// It returns the value that was removed if one exists, or [`None`] o= therwise. pub fn remove(&mut self, key: &K) -> Option { - self.remove_node(key).map(|node| node.node.value) + self.find_mut(key).map(OccupiedEntry::remove) } =20 /// Returns a cursor over the tree nodes based on the given key. @@ -1131,6 +1115,174 @@ unsafe impl Send for RBTreeNode {} // [`RBTreeNode`] without synchronization. unsafe impl Sync for RBTreeNode {} =20 +impl RBTreeNode { + /// Drop the key and value, but keep the allocation. + /// + /// It then becomes a reservation that can be re-initialised into a di= fferent node (i.e., with + /// a different key and/or value). + /// + /// The existing key and value are dropped in-place as part of this op= eration, that is, memory + /// may be freed (but only for the key/value; memory for the node itse= lf is kept for reuse). + pub fn into_reservation(self) -> RBTreeNodeReservation { + RBTreeNodeReservation { + node: Box::drop_contents(self.node), + } + } +} + +/// A view into a single entry in a map, which may either be vacant or occ= upied. +/// +/// This enum is constructed from the [`RBTree::entry`]. +/// +/// [`entry`]: fn@RBTree::entry +pub enum Entry<'a, K, V> { + /// This [`RBTree`] does not have a node with this key. + Vacant(VacantEntry<'a, K, V>), + /// This [`RBTree`] already has a node with this key. + Occupied(OccupiedEntry<'a, K, V>), +} + +/// Like [`Entry`], except that it doesn't have ownership of the key. +enum RawEntry<'a, K, V> { + Vacant(RawVacantEntry<'a, K, V>), + Occupied(OccupiedEntry<'a, K, V>), +} + +/// A view into a vacant entry in a [`RBTree`]. It is part of the [`Entry`= ] enum. +pub struct VacantEntry<'a, K, V> { + key: K, + raw: RawVacantEntry<'a, K, V>, +} + +/// Like [`VacantEntry`], but doesn't hold on to the key.a +/// +/// # Invariants +/// - `parent` may be null if the new node becomes the root. +/// - `child_field_of_parent` is a valid pointer to the left-child or righ= t-child of `parent`. If `parent` is +/// null, it is a pointer to the root of the [`RBTree`]. +struct RawVacantEntry<'a, K, V> { + rbtree: *mut RBTree, + /// The node that will become the parent of the new node if we insert = one. + parent: *mut bindings::rb_node, + /// This points to the left-child or right-child field of `parent`, or= `root` if `parent` is + /// null. + child_field_of_parent: *mut *mut bindings::rb_node, + _phantom: PhantomData<&'a mut RBTree>, +} + +impl<'a, K, V> RawVacantEntry<'a, K, V> { + /// Inserts the given node into the [`RBTree`] at this entry. + /// + /// The `node` must have a key such that inserting it here does not br= eak the ordering of this + /// [`RBTree`]. + fn insert(self, node: RBTreeNode) -> &'a mut V { + let node =3D Box::into_raw(node.node); + + // SAFETY: `node` is valid at least until we call `Box::from_raw`,= which only happens when + // the node is removed or replaced. + let node_links =3D unsafe { addr_of_mut!((*node).links) }; + + // INVARIANT: We are linking in a new node, which is valid. It rem= ains valid because we + // "forgot" it with `Box::into_raw`. + // SAFETY: The type invariants of `RawVacantEntry` are exactly the= safety requirements of `rb_link_node`. + unsafe { bindings::rb_link_node(node_links, self.parent, self.chil= d_field_of_parent) }; + + // SAFETY: All pointers are valid. `node` has just been inserted i= nto the tree. + unsafe { bindings::rb_insert_color(node_links, addr_of_mut!((*self= .rbtree).root)) }; + + // SAFETY: The node is valid until we remove it from the tree. + unsafe { &mut (*node).value } + } +} + +impl<'a, K, V> VacantEntry<'a, K, V> { + /// Inserts the given node into the [`RBTree`] at this entry. + pub fn insert(self, value: V, reservation: RBTreeNodeReservation= ) -> &'a mut V { + self.raw.insert(reservation.into_node(self.key, value)) + } +} + +/// A view into an occupied entry in a [`RBTree`]. It is part of the [`Ent= ry`] enum. +/// +/// # Invariants +/// - `node_links` is a valid, non-null pointer to a tree node in `self.rb= tree` +pub struct OccupiedEntry<'a, K, V> { + rbtree: &'a mut RBTree, + /// The node that this entry corresponds to. + node_links: *mut bindings::rb_node, +} + +impl<'a, K, V> OccupiedEntry<'a, K, V> { + fn node_ptr(&self) -> *mut Node { + // SAFETY: By the type invariant of `Self`, all `node_links` point= ers stored in `self` + // point to the links field of `Node` objects. + unsafe { container_of!(self.node_links, Node, links) }.cast_= mut() + } + + /// Gets a reference to the value in the entry. + pub fn get(&self) -> &V { + // SAFETY: `self.node_ptr` produces a valid pointer to a node in t= he tree. + unsafe { &(*self.node_ptr()).value } + } + + /// Gets a mutable reference to the value in the entry. + pub fn get_mut(&mut self) -> &mut V { + // SAFETY: `self.node_ptr` produces a valid pointer to a node in t= he tree. + unsafe { &mut (*self.node_ptr()).value } + } + + /// Converts the entry into a mutable reference to its value. + /// + /// If you need multiple references to the `OccupiedEntry`, see [`self= #get_mut`]. + pub fn into_mut(self) -> &'a mut V { + // SAFETY: `self.node_ptr` produces a valid pointer to a node in t= he tree. + unsafe { &mut (*self.node_ptr()).value } + } + + /// Remove this entry from the [`RBTree`]. + pub fn remove_node(self) -> RBTreeNode { + // SAFETY: The node is a node in the tree, so it is valid. + unsafe { bindings::rb_erase(self.node_links, &mut self.rbtree.root= ) }; + + // INVARIANT: The node is being returned and the caller may free i= t, however, it was + // removed from the tree. So the invariants still hold. + RBTreeNode { + // SAFETY: The node was a node in the tree, but we removed it,= so we can convert it + // back into a box. + node: unsafe { Box::from_raw(self.node_ptr()) }, + } + } + + /// Takes the value of the entry out of the map, and returns it. + pub fn remove(self) -> V { + self.remove_node().node.value + } + + /// Swap the current node for the provided node. + /// + /// The key of both nodes must be equal. + fn replace(self, node: RBTreeNode) -> RBTreeNode { + let node =3D Box::into_raw(node.node); + + // SAFETY: `node` is valid at least until we call `Box::from_raw`,= which only happens when + // the node is removed or replaced. + let new_node_links =3D unsafe { addr_of_mut!((*node).links) }; + + // SAFETY: This updates the pointers so that `new_node_links` is i= n the tree where + // `self.node_links` used to be. + unsafe { + bindings::rb_replace_node(self.node_links, new_node_links, &mu= t self.rbtree.root) + }; + + // SAFETY: + // - `self.node_ptr` produces a valid pointer to a node in the tre= e. + // - Now that we removed this entry from the tree, we can convert = the node to a box. + let old_node =3D unsafe { Box::from_raw(self.node_ptr()) }; + + RBTreeNode { node: old_node } + } +} + struct Node { links: bindings::rb_node, key: K, --=20 2.46.0.rc1.232.g9752f9e123-goog