From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 1F69316D9C1 for ; Thu, 11 Jul 2024 16:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720714877; cv=none; b=tp+YhZ9WYameK7I1PP+EadCPqJBHjxTegzRhbOkoq+3HDJHJGJdaHPD1lVIyiv87uSg1EOy5X6l5W7+c9FRLrH5FuuNOsNcNEM8qvmhxdZv+Xl6C/toOWzzwMeTV7B0bVaAWBS+bqW/uybp2ymuzOGn+WsEnQmXTSpkqk6n1m78= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720714877; c=relaxed/simple; bh=UE9mgp099aUNg4/OMZK1ryyrI3EjKrNjK3up6VJV0i4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ryO3zBOouAFZ2Hfv5BjariaU/NaSV7SLAiqPmYVve9d2MIX7xP9jY6pbzjzuV9TXDWY2U+ACaFzHHomAWcOL3bMTBW1y7z+OOfbNNiWZS3IdN/k/x+LgnUUBUOS+a8ggMzEtrjM44cMnMYkkqXSsBIy3ZtElS9DMHWb95Mbv49E= 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=HXqbXepg; arc=none smtp.client-ip=209.85.128.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="HXqbXepg" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6590a57788aso16251987b3.2 for ; Thu, 11 Jul 2024 09:21:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720714875; x=1721319675; 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=IuQBQL3mOIq+/Or7stk+SJ0IfbuxCEtq092dP7M+fuE=; b=HXqbXepgGw8qLd9m2arYaiP6xmU2bA9jygH6DMJb1XsgZtKDWsWF9wbn9P3or7Dhbi 9jIej20/IjVdYNdJUAf7Ezdv53wprXg/kKH5n+ot7T3IrDRjVQ9uywlxRfwiQfS+1AK8 7A8oH5Fvkto1RaZ19tgZLSdZrwlA3g6tKeJxtJJ0mltmjOKzYvlQ411L752SsuyM4uvP pFBdcyjxntvSnn+MJjxVgH+iGMZg3LmdBhl3P/jtBwRenIsaaipLomJtdTF/cBvpQQ4q LiFkEfNjd6lC/Kc6YR4gBuTejcWnR/0Ap2G8RioW0h6kPj9brIL75zSAlVc6SX1GbJP1 LdPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720714875; x=1721319675; 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=IuQBQL3mOIq+/Or7stk+SJ0IfbuxCEtq092dP7M+fuE=; b=iS9jZCzYqQdtRvjqTNQo5US1ItTXxT9ZYGJaXhGrgy0eB+Ya5dJRkqOrsyzYNLuF0q jb/v+vRuRSsJ744jrOK07XQYZKnpzxJUEiCOA8qDkaU89YMYeg1uEH08T6jtAnJXz0Uh /U4t4uRpG+AF7lQldGb+fRApHeGp07Zifkn7Fiq0vWZ6Mtk3dV62S9364/elaWjTH7hn 7Aktx9JEp7Vq0CnH+t/WnbaQOX5DqeBIpN8/HsLsRLMrEbj9KzvQNW0NY/PRVKSxyeie 9/RISHsy2EUTBvW1kSfQlNIXDOx4B+znSsBya8zcd/wGtiXw6Z3j5pJ8WTVREsy6fZlb I/Qg== X-Forwarded-Encrypted: i=1; AJvYcCX2I/vYslTssdpzlus/jSZoGLQXMf4itksw7ppES6QXuAy9/Pm+xntYadvsGgFJPslM4XUZ4kBQ5nczezSliWDP8eJUgF++Kecq9hq84Sg= X-Gm-Message-State: AOJu0YzjcyJEroXkcxKwY+tNtDqKmF8X2rLH8tQYUHgsvhTL5QbirAO4 6yk3glSlhs2vts6rzagGeaaUZ5Iy95fY0LEpSLzY4lgT3oQiw+peiFeaKnLKz0heddElKXRrPF4 AAMqsdwDbg4989mAlnRe6X+fDkw== X-Google-Smtp-Source: AGHT+IE65F2pRwuaq/0Em8AhjcXkK8INPwJeZy/oIKK2fZVRByc43twSHu2CJlJFCC5vcrSDY4ZxLO/Z6J3ZRh9nk2Y= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:690c:380e:b0:62d:a29:537e with SMTP id 00721157ae682-658ef53b41dmr1994907b3.4.1720714875055; Thu, 11 Jul 2024 09:21:15 -0700 (PDT) Date: Thu, 11 Jul 2024 16:21:00 +0000 In-Reply-To: <20240711-b4-rbtree-v6-0-14bef1a8cdba@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240711-b4-rbtree-v6-0-14bef1a8cdba@google.com> X-Mailer: b4 0.13.0 Message-ID: <20240711-b4-rbtree-v6-4-14bef1a8cdba@google.com> Subject: [PATCH v6 4/6] rust: rbtree: add mutable iterator 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" From: Wedson Almeida Filho Add mutable Iterator implementation for `RBTree`, allowing iteration over (key, value) pairs in key order. Only values are mutable, as mutating keys implies modifying a node's position in the tree. Mutable iteration is used by the binder driver during shutdown to clean up the tree maintained by the "range allocator" [1]. Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-6-08ba9197f637@google.com/ [1] Signed-off-by: Wedson Almeida Filho Signed-off-by: Matt Gilbride Reviewed-by: Alice Ryhl Tested-by: Alice Ryhl --- rust/kernel/rbtree.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 12 deletions(-) diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs index 40db6d3e732b..64e0f2328879 100644 --- a/rust/kernel/rbtree.rs +++ b/rust/kernel/rbtree.rs @@ -197,8 +197,26 @@ pub fn iter(&self) -> Iter<'_, K, V> { // INVARIANT: `bindings::rb_first` returns a valid pointer to a tree node given a valid pointer to a tree root. Iter { _tree: PhantomData, - // SAFETY: `self.root` is a valid pointer to the tree root. - next: unsafe { bindings::rb_first(&self.root) }, + iter_raw: IterRaw { + // SAFETY: by the invariants, all pointers are valid. + next: unsafe { bindings::rb_first(&self.root) }, + _phantom: PhantomData, + }, + } + } + + /// Returns a mutable iterator over the tree nodes, sorted by key. + pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { + IterMut { + _tree: PhantomData, + // INVARIANT: + // - `self.root` is a valid pointer to a tree root. + // - `bindings::rb_first` produces a valid pointer to a node given `root` is valid. + iter_raw: IterRaw { + // SAFETY: by the invariants, all pointers are valid. + next: unsafe { bindings::rb_first(&self.root) }, + _phantom: PhantomData, + }, } } @@ -211,6 +229,11 @@ pub fn keys(&self) -> impl Iterator { pub fn values(&self) -> impl Iterator { self.iter().map(|(_, v)| v) } + + /// Returns a mutable iterator over the values of the nodes in the tree, sorted by key. + pub fn values_mut(&mut self) -> impl Iterator { + self.iter_mut().map(|(_, v)| v) + } } impl RBTree @@ -414,13 +437,9 @@ fn into_iter(self) -> Self::IntoIter { /// An iterator over the nodes of a [`RBTree`]. /// /// Instances are created by calling [`RBTree::iter`]. -/// -/// # Invariants -/// - `self.next` is a valid pointer. -/// - `self.next` points to a node stored inside of a valid `RBTree`. pub struct Iter<'a, K, V> { _tree: PhantomData<&'a RBTree>, - next: *mut bindings::rb_node, + iter_raw: IterRaw, } // SAFETY: The [`Iter`] gives out immutable references to K and V, so it has the same @@ -434,21 +453,76 @@ unsafe impl<'a, K: Sync, V: Sync> Sync for Iter<'a, K, V> {} impl<'a, K, V> Iterator for Iter<'a, K, V> { type Item = (&'a K, &'a V); + fn next(&mut self) -> Option { + self.iter_raw.next().map(|(k, v)| + // SAFETY: Due to `self._tree`, `k` and `v` are valid for the lifetime of `'a`. + unsafe { (&*k, &*v) }) + } +} + +impl<'a, K, V> IntoIterator for &'a mut RBTree { + type Item = (&'a K, &'a mut V); + type IntoIter = IterMut<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + +/// A mutable iterator over the nodes of a [`RBTree`]. +/// +/// Instances are created by calling [`RBTree::iter_mut`]. +pub struct IterMut<'a, K, V> { + _tree: PhantomData<&'a mut RBTree>, + iter_raw: IterRaw, +} + +// SAFETY: The [`IterMut`] gives out immutable references to K and mutable references to V, so it has the same +// thread safety requirements as mutable references. +unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {} + +// SAFETY: The [`IterMut`] gives out immutable references to K and mutable references to V, so it has the same +// thread safety requirements as mutable references. +unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {} + +impl<'a, K, V> Iterator for IterMut<'a, K, V> { + type Item = (&'a K, &'a mut V); + + fn next(&mut self) -> Option { + self.iter_raw.next().map(|(k, v)| + // SAFETY: Due to `&mut self`, we have exclusive access to `k` and `v`, for the lifetime of `'a`. + unsafe { (&*k, &mut *v) }) + } +} + +/// A raw iterator over the nodes of a [`RBTree`]. +/// +/// # Invariants +/// - `self.next` is a valid pointer. +/// - `self.next` points to a node stored inside of a valid `RBTree`. +struct IterRaw { + next: *mut bindings::rb_node, + _phantom: PhantomData (K, V)>, +} + +impl Iterator for IterRaw { + type Item = (*mut K, *mut V); + fn next(&mut self) -> Option { if self.next.is_null() { return None; } - // SAFETY: By the type invariant of `Iter`, `self.next` is a valid node in an `RBTree`, + // SAFETY: By the type invariant of `IterRaw`, `self.next` is a valid node in an `RBTree`, // and by the type invariant of `RBTree`, all nodes point to the links field of `Node` objects. - let cur = unsafe { container_of!(self.next, Node, links) }; + let cur: *mut Node = + unsafe { container_of!(self.next, Node, links) }.cast_mut(); // SAFETY: `self.next` is a valid tree node by the type invariants. self.next = unsafe { bindings::rb_next(self.next) }; - // SAFETY: By the same reasoning above, it is safe to dereference the node. Additionally, - // it is ok to return a reference to members because the iterator must outlive it. - Some(unsafe { (&(*cur).key, &(*cur).value) }) + // SAFETY: By the same reasoning above, it is safe to dereference the node. + Some(unsafe { (addr_of_mut!((*cur).key), addr_of_mut!((*cur).value)) }) } } -- 2.45.2.803.g4e1b14247a-goog