From nobody Mon Feb 9 12:24:30 2026 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 B663F17333A for ; Sat, 27 Jul 2024 20:30:57 +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=1722112259; cv=none; b=SDXHs8tJlMkXl+2KyM+HBYw99NeaEN9O1RB7pBbZ6LXG3snGmCUy0H2pk0dv1wwhxPoF0c5PEam2fIeczCY/XrHGK7dzVrDTAvzHZqetlFU3ZRTdxsYxFVGVgHe0cqurdeIpYQCT+hdiNSMZMDs3oCF/8+4A+uwbXfd8oE/gKQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722112259; c=relaxed/simple; bh=JBNM3bVLmXUXX+PRWRAwYHRy0p6zuHJ8e6RIemD+NYk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NQJ4kEE0cQ5+g7U8ecAU4cQyDF9SUB6Vc8cfA8t3bg9M0rE9Cgr2xT11HV0BF8bk23HQY4emOutZ3N333IuCIP6xFaSouW99Z1BTpZV4X6u7CJ6tZjIvJxcSQbjD8eKrjmmMc7l0Jj9QPXH8wMOLzbvEjk7OyNbppUk1mD5ltko= 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=mE4PoEqK; 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="mE4PoEqK" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0353b731b8so1679268276.2 for ; Sat, 27 Jul 2024 13:30:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722112257; x=1722717057; 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=AkoIbyLEqhRuUBxBjLNxqzkpGpAMBfb6LrU8pnYzXCM=; b=mE4PoEqK/nDjuutZeDJe9Wv6frqUUD5z5vL4oXx/rBpusA6FTb67H9RJAlTExsYbM1 VCO8ik11CakFIsYVZKjsGm31Q+Ph6T/5AOVp75ylGTURiRPI4OGRfLtwGMHKrSSqVLF8 T0f/pQ05WVIRMjKAPTOORaM7M51u+xRioc+vDJr+Pb07O/xgah5cT6rRC+885mgiuLf7 XTJCt+XvYpQcdeR65+gZ9JPXM6Vzdhyj1z4CtCVWVb3Si2bYl5doGBkFKQEiDkXOs85m AQdftJYcZPlRcksUw2vKdXjM4itk0c6j/u3ZL0WaaeHz9oV/773s/m3KZvWOuuFT8MSA Ex2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722112257; x=1722717057; 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=AkoIbyLEqhRuUBxBjLNxqzkpGpAMBfb6LrU8pnYzXCM=; b=tmPnw57yJmcVgkO36llrQDu/+PsyaZTT3fQ7Df2TTJOM909Sj6aYVth1owFz1Ur3qL JuRZdrJlYruAdXoO1QGnbhymo5+SAOqay8fXXAJPfcDFdQ2LmQC3+KQ6GDFnudaNzTaR 2yN5aiKmEqaTLum51/LnhB/xyJ1hHVCSgAFY+PgRACO9HF4K//lUdkZg1QFt4t+k2ODP MKWUtx8JeieUj4rxeFsehR47HJhrNEX9i8PZItKsfHOLYHXjxzhG33SXZ1nYXc1l05Fc +flOg14McPiIqKS2ToAIkq7aALxQtb8hPWmgtSEaYL5thIqhuLZJN2oMn//SvqjDzaMq JF4w== X-Forwarded-Encrypted: i=1; AJvYcCXdO3V62q5JgcMoUJ7Qw6EqrkaJdlAhuw4f9dRVP43UdN8JsxnCcpXWbvDILxNCJUPmlwHd8ylL08TW7im4PZNOizMk7YeXj3cjfaNz X-Gm-Message-State: AOJu0YzKaYrGAu8N8LZpVPHFvv1al70SHPfaoXbCXWOoxv+H0Vo10QYT deIAwKvGxcDBYe3t2OOimOq2vmNpaLOxiCEN1f1ibocxLdE5YBXoPctpLoMvatDfiMQnln0JV69 FcTfbCTnva+EZFd4c2kKGBEXEsw== X-Google-Smtp-Source: AGHT+IFKD2QpBLGb1gAoGa8hNUQs6UI3tA4LINLXQVfGYc+EwLsPEOsO7gKmmusYfTi3C9vZ5m6O/YAV08PC++q+kl8= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:1028:b0:e0b:1049:f928 with SMTP id 3f1490d57ef6-e0b54559206mr6023276.8.1722112256695; Sat, 27 Jul 2024 13:30:56 -0700 (PDT) Date: Sat, 27 Jul 2024 20:30:48 +0000 In-Reply-To: <20240727-b4-rbtree-v8-0-951600ada434@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240727-b4-rbtree-v8-0-951600ada434@google.com> X-Mailer: b4 0.13.0 Message-ID: <20240727-b4-rbtree-v8-3-951600ada434@google.com> Subject: [PATCH v8 3/6] rust: rbtree: add 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" Content-Transfer-Encoding: quoted-printable From: Wedson Almeida Filho - Add Iterator implementation for `RBTree`, allowing iteration over (key, value) pairs in key order. - Add individual `keys()` and `values()` functions to iterate over keys or values alone. - Update doctests to use iteration instead of explicitly getting items. Iteration is needed by the binder driver to enumerate all values in a tree for oneway spam detection [1]. Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-17-08b= a9197f637@google.com/ [1] Signed-off-by: Wedson Almeida Filho Reviewed-by: Alice Ryhl Tested-by: Alice Ryhl Signed-off-by: Matt Gilbride Reviewed-by: Benno Lossin --- rust/kernel/rbtree.rs | 130 +++++++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 112 insertions(+), 18 deletions(-) diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs index 74c53e4d5c00..d10074e4ac58 100644 --- a/rust/kernel/rbtree.rs +++ b/rust/kernel/rbtree.rs @@ -47,14 +47,30 @@ /// assert_eq!(tree.get(&30).unwrap(), &300); /// } /// +/// // Iterate over the nodes we just inserted. +/// { +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&10, &100)); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert_eq!(iter.next().unwrap(), (&30, &300)); +/// assert!(iter.next().is_none()); +/// } +/// +/// // Print all elements. +/// for (key, value) in &tree { +/// pr_info!("{} =3D {}\n", key, value); +/// } +/// /// // Replace one of the elements. /// tree.try_create_and_insert(10, 1000, flags::GFP_KERNEL)?; /// /// // Check that the tree reflects the replacement. /// { -/// assert_eq!(tree.get(&10).unwrap(), &1000); -/// assert_eq!(tree.get(&20).unwrap(), &200); -/// assert_eq!(tree.get(&30).unwrap(), &300); +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&10, &1000)); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert_eq!(iter.next().unwrap(), (&30, &300)); +/// assert!(iter.next().is_none()); /// } /// /// // Change the value of one of the elements. @@ -62,9 +78,11 @@ /// /// // Check that the tree reflects the update. /// { -/// assert_eq!(tree.get(&10).unwrap(), &1000); -/// assert_eq!(tree.get(&20).unwrap(), &200); -/// assert_eq!(tree.get(&30).unwrap(), &3000); +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&10, &1000)); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert_eq!(iter.next().unwrap(), (&30, &3000)); +/// assert!(iter.next().is_none()); /// } /// /// // Remove an element. @@ -72,9 +90,10 @@ /// /// // Check that the tree reflects the removal. /// { -/// assert_eq!(tree.get(&10), None); -/// assert_eq!(tree.get(&20).unwrap(), &200); -/// assert_eq!(tree.get(&30).unwrap(), &3000); +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert_eq!(iter.next().unwrap(), (&30, &3000)); +/// assert!(iter.next().is_none()); /// } /// /// # Ok::<(), Error>(()) @@ -114,9 +133,11 @@ /// /// // Check the nodes we just inserted. /// { -/// assert_eq!(tree.get(&10).unwrap(), &100); -/// assert_eq!(tree.get(&20).unwrap(), &200); -/// assert_eq!(tree.get(&30).unwrap(), &300); +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&10, &100)); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert_eq!(iter.next().unwrap(), (&30, &300)); +/// assert!(iter.next().is_none()); /// } /// /// // Remove a node, getting back ownership of it. @@ -124,9 +145,10 @@ /// /// // Check that the tree reflects the removal. /// { -/// assert_eq!(tree.get(&10).unwrap(), &100); -/// assert_eq!(tree.get(&20).unwrap(), &200); -/// assert_eq!(tree.get(&30), None); +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&10, &100)); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert!(iter.next().is_none()); /// } /// /// // Create a preallocated reservation that we can re-use later. @@ -138,9 +160,11 @@ /// /// // Check that the tree reflect the new insertion. /// { -/// assert_eq!(tree.get(&10).unwrap(), &100); -/// assert_eq!(tree.get(&15).unwrap(), &150); -/// assert_eq!(tree.get(&20).unwrap(), &200); +/// let mut iter =3D tree.iter(); +/// assert_eq!(iter.next().unwrap(), (&10, &100)); +/// assert_eq!(iter.next().unwrap(), (&15, &150)); +/// assert_eq!(iter.next().unwrap(), (&20, &200)); +/// assert!(iter.next().is_none()); /// } /// /// # Ok::<(), Error>(()) @@ -167,6 +191,26 @@ pub fn new() -> Self { _p: PhantomData, } } + + /// Returns an iterator over the tree nodes, sorted by key. + pub fn iter(&self) -> Iter<'_, K, V> { + // INVARIANT: `bindings::rb_first` returns a valid pointer to a tr= ee 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) }, + } + } + + /// Returns an iterator over the keys of the nodes in the tree, in sor= ted order. + pub fn keys(&self) -> impl Iterator { + self.iter().map(|(k, _)| k) + } + + /// Returns an iterator over the values of the nodes in the tree, sort= ed by key. + pub fn values(&self) -> impl Iterator { + self.iter().map(|(_, v)| v) + } } =20 impl RBTree @@ -358,6 +402,56 @@ fn drop(&mut self) { } } =20 +impl<'a, K, V> IntoIterator for &'a RBTree { + type Item =3D (&'a K, &'a V); + type IntoIter =3D Iter<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +/// 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, +} + +// SAFETY: The [`Iter`] gives out immutable references to K and V, so it h= as the same +// thread safety requirements as immutable references. +unsafe impl<'a, K: Sync, V: Sync> Send for Iter<'a, K, V> {} + +// SAFETY: The [`Iter`] gives out immutable references to K and V, so it h= as the same +// thread safety requirements as immutable references. +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 =3D (&'a K, &'a 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`, + // and by the type invariant of `RBTree`, all nodes point to the l= inks field of `Node` objects. + let cur =3D unsafe { container_of!(self.next, Node, links) }; + + // SAFETY: `self.next` is a valid tree node by the type invariants. + self.next =3D 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) }) + } +} + /// A memory reservation for a red-black tree node. /// /// --=20 2.46.0.rc1.232.g9752f9e123-goog