linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Liam R. Howlett" <Liam.Howlett@oracle.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: maple-tree@lists.infradead.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>
Subject: [PATCH v4 23/35] maple_tree: Try harder to keep active node after mas_next()
Date: Thu, 18 May 2023 10:55:32 -0400	[thread overview]
Message-ID: <20230518145544.1722059-24-Liam.Howlett@oracle.com> (raw)
In-Reply-To: <20230518145544.1722059-1-Liam.Howlett@oracle.com>

Clean up the mas_next() call to try and keep a node reference when
possible.  This will avoid re-walking the tree in most cases.

Also clean up the single entry tree handling to ensure index/last are
consistent with what one would expect. (returning NULL with limit of
1-oo).

Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
 lib/maple_tree.c | 89 +++++++++++++++++++++++++-----------------------
 1 file changed, 47 insertions(+), 42 deletions(-)

diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index e233f41ed4da..09142af08214 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -4726,33 +4726,25 @@ static inline void *mas_next_nentry(struct ma_state *mas,
 		if (ma_dead_node(node))
 			return NULL;
 
+		mas->last = pivot;
 		if (entry)
-			goto found;
+			return entry;
 
 		if (pivot >= max)
 			return NULL;
 
+		if (pivot >= mas->max)
+			return NULL;
+
 		mas->index = pivot + 1;
 		mas->offset++;
 	}
 
-	if (mas->index > mas->max) {
-		mas->index = mas->last;
-		return NULL;
-	}
-
-	pivot = mas_safe_pivot(mas, pivots, mas->offset, type);
+	pivot = mas_logical_pivot(mas, pivots, mas->offset, type);
 	entry = mas_slot(mas, slots, mas->offset);
 	if (ma_dead_node(node))
 		return NULL;
 
-	if (!pivot)
-		return NULL;
-
-	if (!entry)
-		return NULL;
-
-found:
 	mas->last = pivot;
 	return entry;
 }
@@ -4781,21 +4773,15 @@ static inline void mas_rewalk(struct ma_state *mas, unsigned long index)
 static inline void *mas_next_entry(struct ma_state *mas, unsigned long limit)
 {
 	void *entry = NULL;
-	struct maple_enode *prev_node;
 	struct maple_node *node;
-	unsigned char offset;
 	unsigned long last;
 	enum maple_type mt;
 
-	if (mas->index > limit) {
-		mas->index = mas->last = limit;
-		mas_pause(mas);
+	if (mas->last >= limit)
 		return NULL;
-	}
+
 	last = mas->last;
 retry:
-	offset = mas->offset;
-	prev_node = mas->node;
 	node = mas_mn(mas);
 	mt = mte_node_type(mas->node);
 	mas->offset++;
@@ -4814,12 +4800,10 @@ static inline void *mas_next_entry(struct ma_state *mas, unsigned long limit)
 		if (likely(entry))
 			return entry;
 
-		if (unlikely((mas->index > limit)))
-			break;
+		if (unlikely((mas->last >= limit)))
+			return NULL;
 
 next_node:
-		prev_node = mas->node;
-		offset = mas->offset;
 		if (unlikely(mas_next_node(mas, node, limit))) {
 			mas_rewalk(mas, last);
 			goto retry;
@@ -4829,9 +4813,6 @@ static inline void *mas_next_entry(struct ma_state *mas, unsigned long limit)
 		mt = mte_node_type(mas->node);
 	}
 
-	mas->index = mas->last = limit;
-	mas->offset = offset;
-	mas->node = prev_node;
 	return NULL;
 }
 
@@ -5919,6 +5900,8 @@ EXPORT_SYMBOL_GPL(mas_expected_entries);
  */
 void *mas_next(struct ma_state *mas, unsigned long max)
 {
+	bool was_none = mas_is_none(mas);
+
 	if (mas_is_none(mas) || mas_is_paused(mas))
 		mas->node = MAS_START;
 
@@ -5926,16 +5909,16 @@ void *mas_next(struct ma_state *mas, unsigned long max)
 		mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
 
 	if (mas_is_ptr(mas)) {
-		if (!mas->index) {
-			mas->index = 1;
-			mas->last = ULONG_MAX;
+		if (was_none && mas->index == 0) {
+			mas->index = mas->last = 0;
+			return mas_root(mas);
 		}
+		mas->index = 1;
+		mas->last = ULONG_MAX;
+		mas->node = MAS_NONE;
 		return NULL;
 	}
 
-	if (mas->last == ULONG_MAX)
-		return NULL;
-
 	/* Retries on dead nodes handled by mas_next_entry */
 	return mas_next_entry(mas, max);
 }
@@ -6059,17 +6042,25 @@ EXPORT_SYMBOL_GPL(mas_pause);
  */
 void *mas_find(struct ma_state *mas, unsigned long max)
 {
+	if (unlikely(mas_is_none(mas))) {
+		if (unlikely(mas->last >= max))
+			return NULL;
+
+		mas->index = mas->last;
+		mas->node = MAS_START;
+	}
+
 	if (unlikely(mas_is_paused(mas))) {
-		if (unlikely(mas->last == ULONG_MAX)) {
-			mas->node = MAS_NONE;
+		if (unlikely(mas->last >= max))
 			return NULL;
-		}
+
 		mas->node = MAS_START;
 		mas->index = ++mas->last;
 	}
 
-	if (unlikely(mas_is_none(mas)))
-		mas->node = MAS_START;
+
+	if (unlikely(mas_is_ptr(mas)))
+		goto ptr_out_of_range;
 
 	if (unlikely(mas_is_start(mas))) {
 		/* First run or continue */
@@ -6081,13 +6072,27 @@ void *mas_find(struct ma_state *mas, unsigned long max)
 		entry = mas_walk(mas);
 		if (entry)
 			return entry;
+
 	}
 
-	if (unlikely(!mas_searchable(mas)))
+	if (unlikely(!mas_searchable(mas))) {
+		if (unlikely(mas_is_ptr(mas)))
+			goto ptr_out_of_range;
+
+		return NULL;
+	}
+
+	if (mas->index == max)
 		return NULL;
 
 	/* Retries on dead nodes handled by mas_next_entry */
 	return mas_next_entry(mas, max);
+
+ptr_out_of_range:
+	mas->node = MAS_NONE;
+	mas->index = 1;
+	mas->last = ULONG_MAX;
+	return NULL;
 }
 EXPORT_SYMBOL_GPL(mas_find);
 
@@ -6518,7 +6523,7 @@ void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max)
 	if (entry)
 		goto unlock;
 
-	while (mas_searchable(&mas) && (mas.index < max)) {
+	while (mas_searchable(&mas) && (mas.last < max)) {
 		entry = mas_next_entry(&mas, max);
 		if (likely(entry && !xa_is_zero(entry)))
 			break;
-- 
2.39.2



  parent reply	other threads:[~2023-05-18 14:56 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18 14:55 [PATCH v4 00/35] Maple tree mas_{next,prev}_range() and cleanup Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 01/35] maple_tree: Fix static analyser cppcheck issue Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 02/35] maple_tree: Clean up mas_parent_enum() and rename to mas_parent_type() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 03/35] maple_tree: Avoid unnecessary ascending Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 04/35] maple_tree: Clean up mas_dfs_postorder() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 05/35] maple_tree: Add format option to mt_dump() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 06/35] maple_tree: Add debug BUG_ON and WARN_ON variants Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 07/35] maple_tree: Convert BUG_ON() to MT_BUG_ON() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 08/35] maple_tree: Change RCU checks to WARN_ON() instead of BUG_ON() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 09/35] maple_tree: Convert debug code to use MT_WARN_ON() and MAS_WARN_ON() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 10/35] maple_tree: Use MAS_BUG_ON() when setting a leaf node as a parent Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 11/35] maple_tree: Use MAS_BUG_ON() in mas_set_height() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 12/35] maple_tree: Use MAS_BUG_ON() from mas_topiary_range() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 13/35] maple_tree: Use MAS_WR_BUG_ON() in mas_store_prealloc() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 14/35] maple_tree: Use MAS_BUG_ON() prior to calling mas_meta_gap() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 15/35] maple_tree: Return error on mte_pivots() out of range Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 16/35] maple_tree: Make test code work without debug enabled Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 17/35] mm: Update validate_mm() to use vma iterator Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 18/35] mm: Update vma_iter_store() to use MAS_WARN_ON() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 19/35] maple_tree: Add __init and __exit to test module Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 20/35] maple_tree: Remove unnecessary check from mas_destroy() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 21/35] maple_tree: mas_start() reset depth on dead node Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 22/35] mm/mmap: Change do_vmi_align_munmap() for maple tree iterator changes Liam R. Howlett
2023-05-18 14:55 ` Liam R. Howlett [this message]
2023-05-18 14:55 ` [PATCH v4 24/35] maple_tree: Try harder to keep active node with mas_prev() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 25/35] maple_tree: Revise limit checks in mas_empty_area{_rev}() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 26/35] maple_tree: Fix testing mas_empty_area() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 27/35] maple_tree: Introduce mas_next_slot() interface Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 28/35] maple_tree: Add mas_next_range() and mas_find_range() interfaces Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 29/35] maple_tree: Relocate mas_rewalk() and mas_rewalk_if_dead() Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 30/35] maple_tree: Introduce mas_prev_slot() interface Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 31/35] maple_tree: Add mas_prev_range() and mas_find_range_rev interface Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 32/35] maple_tree: Clear up index and last setting in single entry tree Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 33/35] maple_tree: Update testing code for mas_{next,prev,walk} Liam R. Howlett
2023-07-02 18:20   ` Geert Uytterhoeven
2023-07-04 15:11     ` Peng Zhang
2023-07-04 15:22       ` Liam R. Howlett
2023-07-07 19:28         ` Liam R. Howlett
2023-07-10 15:03           ` Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 34/35] mm: Add vma_iter_{next,prev}_range() to vma iterator Liam R. Howlett
2023-05-18 14:55 ` [PATCH v4 35/35] mm: Avoid rewalk in mmap_region Liam R. Howlett

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=20230518145544.1722059-24-Liam.Howlett@oracle.com \
    --to=liam.howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maple-tree@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox