From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 4873D136E3B for ; Mon, 3 Jun 2024 16:05:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430734; cv=none; b=iAo6whUlV3+QsDdy66Lv9DR3+Qy7GQHARmY6ukjTCzzbJoR6ON84jFaIf7LvSVBL2geMNcwn7kaBSX+eTx1iWJyUGdwokD/NkzfdO5s4cS6FZf2VKaCZ6VlHtwh8KWdbQ3TG+PWQdc0cL5/qDkxxHRz2jfgZ972xYQ20OOHVm0w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430734; c=relaxed/simple; bh=1NRPDpmaRDB+AhvQN/BNDaJ6oGKxnyTvq19egS+D89c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i2MGNKtpmO+mCoyxj2i0ihthXFfoz+8w6Gf6iKvrtyRY2YMUPxoqygOmGAW4NLP6qk7PEnfl5/PNvSca5sImaiop6pwVzbo9sz9VSZ3/6cK4jZnCjCU9ZR5QjNz2g4UAH71q7TLmlFOGWSFIwPNWw00xY8e/B/uv6XJtQdzpiYU= 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=BeokOhRD; arc=none smtp.client-ip=209.85.219.201 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="BeokOhRD" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-df796aaa57dso4999632276.1 for ; Mon, 03 Jun 2024 09:05:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717430730; x=1718035530; 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=NweWq3SyqbU+cwUgdn2ay8imaAU/0BQPy/p7EbZYlD8=; b=BeokOhRDDTThBcpBsRXuQ7ZFPoXQTJC6mr26/PGpbtAGdiVCaSGfIj5wISMIQvuEus Cn7So3Lhg6lBMFbhMe3EkEJl+AejZn9kJIHu1vYncJEGmeZGpSRFU3bRO9XckHnQlTbM q3NK0bqNIdILeY0mCBP6B0JpjHc1c3zFiehOLdqrM/T2GEMval/Le8hYJQl/TtQ7Wx05 76bIVj1vcH3pvO2hhBGsDa0j+ovdlU29v3kjmwzZARQfdqAO3f6vx9GN/G79TfScPnCq 23ZMxjE0ynuIRVidnSDXRQHwQ4hPKXNfh9tJsPxNKdbDZG75QzFaRBDf0yNCqRDSOJTz 8fdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717430730; x=1718035530; 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=NweWq3SyqbU+cwUgdn2ay8imaAU/0BQPy/p7EbZYlD8=; b=DAcTQDKLjJYDqbmtkwWW2N54Dio5jSHbvFegaGOJ0Y2dGvBNBZIx2nh7rQLLUxzU9h WApO9op/XNw3FYoo3xBSlil/jAxzhu1/FflhCY0XGD9HbRG7gZZsixQgbM83fml6lszA 2IZzfex9DQOqSH0ABkNR9/5nQWaJHa/sdsjhq19rx142DjFpbq8of3BYEwqeK3MH+cdq 7M4KNkYRfhs706fpbaPhCtQpI4wFp9qYEsdDYBpGyq8xW/89OBi7/vyv7FcQIO4r5ZVb idvYdolK+07OGFnXjtQY0iCqzwTWs7FTQ3YylI2FwZyzU3IPsSxwERYfDTSYJrSDSLAl b5pw== X-Forwarded-Encrypted: i=1; AJvYcCU15RRBgVl1vZVHji+qYrCxES1dJewWIHGqc4Acyx7goSvoxMiUjGAu08xAR9QoZM3Z0bTiVyuTW+iAjQdSuvzrhLNK9dC0CRHHRN1w97g= X-Gm-Message-State: AOJu0YzGxOlK6Ot0ZvlDkO15O4qTUkGe93ZPRvlJLErhDKBNM9nTT7kj gy5QL6TJno/pmlXm2J0vVKPONIflt/BnjLNO8JOiY99P99xrsjOQhYfp7Q43jx31s0gMVoNuaQe 6Fx/X8F80In95/VdE6ozKZfILKA== X-Google-Smtp-Source: AGHT+IGHnoBBDF+uplOTuhsfaXzMdOsouZCQJQcVfginVN5GDE3IsdPb0SGj8uS3x0aE9ajY8sWv/aeRMLVd7zVDfAA= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:2b12:b0:dd9:2d94:cd8a with SMTP id 3f1490d57ef6-dfa73dafa73mr758560276.9.1717430730209; Mon, 03 Jun 2024 09:05:30 -0700 (PDT) Date: Mon, 03 Jun 2024 16:05:19 +0000 In-Reply-To: <20240603-b4-rbtree-v4-0-308e43d6abfc@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240603-b4-rbtree-v4-0-308e43d6abfc@google.com> X-Mailer: b4 0.12.4 Message-ID: <20240603-b4-rbtree-v4-4-308e43d6abfc@google.com> Subject: [PATCH v4 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 6ebea833e5a9..28dfa36e81c3 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 [`RBTreeIterator`] gives out mutable references to K and 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 [`RBTreeIterator`] gives out mutable references to K and 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.1.288.g0e0cd299f1-goog