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 v3 28/35] maple_tree: Add mas_next_range() and mas_find_range() interfaces
Date: Fri, 12 May 2023 14:20:29 -0400 [thread overview]
Message-ID: <20230512182036.359030-29-Liam.Howlett@oracle.com> (raw)
In-Reply-To: <20230512182036.359030-1-Liam.Howlett@oracle.com>
Some users of the maple tree may want to move to the next range in the
tree, even if it stores a NULL. This family of function provides that
functionality by advancing one slot at a time and returning the result,
while mas_contiguous() will iterate over the range and stop on
encountering the first NULL.
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
include/linux/maple_tree.h | 15 ++++
lib/maple_tree.c | 172 ++++++++++++++++++++++++++-----------
2 files changed, 137 insertions(+), 50 deletions(-)
diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h
index ed92abf4c1fb5..a4cd8f891a090 100644
--- a/include/linux/maple_tree.h
+++ b/include/linux/maple_tree.h
@@ -455,6 +455,7 @@ void *mas_erase(struct ma_state *mas);
int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp);
void mas_store_prealloc(struct ma_state *mas, void *entry);
void *mas_find(struct ma_state *mas, unsigned long max);
+void *mas_find_range(struct ma_state *mas, unsigned long max);
void *mas_find_rev(struct ma_state *mas, unsigned long min);
int mas_preallocate(struct ma_state *mas, gfp_t gfp);
bool mas_is_err(struct ma_state *mas);
@@ -467,6 +468,7 @@ int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries);
void *mas_prev(struct ma_state *mas, unsigned long min);
void *mas_next(struct ma_state *mas, unsigned long max);
+void *mas_next_range(struct ma_state *mas, unsigned long max);
int mas_empty_area(struct ma_state *mas, unsigned long min, unsigned long max,
unsigned long size);
@@ -528,6 +530,19 @@ static inline void mas_reset(struct ma_state *mas)
#define mas_for_each(__mas, __entry, __max) \
while (((__entry) = mas_find((__mas), (__max))) != NULL)
+/**
+ * mas_contiguous() - Iterate over a contiguous range of the maple tree.
+ * @__mas: Maple Tree operation state (maple_state)
+ * @__entry: Entry retrieved from the tree
+ * @__max: maximum index to retrieve from the tree
+ *
+ * When returned, mas->index and mas->last will hold the entire range of the
+ * entry. The loop will terminate on the first NULL encountered.
+ *
+ * Note: may return the zero entry.
+ */
+#define mas_contiguous(__mas, __entry, __max) \
+ while (((__entry) = mas_find_range((__mas), (__max))) != NULL)
/**
* mas_set_range() - Set up Maple Tree operation state for a different index.
diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index 860f82e161661..10b9471a80cdb 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -4792,13 +4792,10 @@ static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty)
*/
static inline void *mas_next_entry(struct ma_state *mas, unsigned long limit)
{
- void *entry = NULL;
-
if (mas->last >= limit)
return NULL;
- entry = mas_next_slot(mas, limit, false);
- return entry;
+ return mas_next_slot(mas, limit, false);
}
/*
@@ -5885,18 +5882,8 @@ int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
}
EXPORT_SYMBOL_GPL(mas_expected_entries);
-/**
- * mas_next() - Get the next entry.
- * @mas: The maple state
- * @max: The maximum index to check.
- *
- * Returns the next entry after @mas->index.
- * Must hold rcu_read_lock or the write lock.
- * Can return the zero entry.
- *
- * Return: The next entry or %NULL
- */
-void *mas_next(struct ma_state *mas, unsigned long max)
+static inline bool mas_next_setup(struct ma_state *mas, unsigned long max,
+ void **entry)
{
bool was_none = mas_is_none(mas);
@@ -5904,24 +5891,71 @@ void *mas_next(struct ma_state *mas, unsigned long max)
mas->node = MAS_START;
if (mas_is_start(mas))
- mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
+ *entry = mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
if (mas_is_ptr(mas)) {
+ *entry = NULL;
if (was_none && mas->index == 0) {
mas->index = mas->last = 0;
- return mas_root(mas);
+ return true;
}
mas->index = 1;
mas->last = ULONG_MAX;
mas->node = MAS_NONE;
- return NULL;
+ return true;
}
- /* Retries on dead nodes handled by mas_next_entry */
- return mas_next_entry(mas, max);
+ if (mas_is_none(mas))
+ return true;
+ return false;
+}
+
+/**
+ * mas_next() - Get the next entry.
+ * @mas: The maple state
+ * @max: The maximum index to check.
+ *
+ * Returns the next entry after @mas->index.
+ * Must hold rcu_read_lock or the write lock.
+ * Can return the zero entry.
+ *
+ * Return: The next entry or %NULL
+ */
+void *mas_next(struct ma_state *mas, unsigned long max)
+{
+ void *entry = NULL;
+
+ if (mas_next_setup(mas, max, &entry))
+ return entry;
+
+ /* Retries on dead nodes handled by mas_next_slot */
+ return mas_next_slot(mas, max, false);
}
EXPORT_SYMBOL_GPL(mas_next);
+/**
+ * mas_next_range() - Advance the maple state to the next range
+ * @mas: The maple state
+ * @max: The maximum index to check.
+ *
+ * Sets @mas->index and @mas->last to the range.
+ * Must hold rcu_read_lock or the write lock.
+ * Can return the zero entry.
+ *
+ * Return: The next entry or %NULL
+ */
+void *mas_next_range(struct ma_state *mas, unsigned long max)
+{
+ void *entry = NULL;
+
+ if (mas_next_setup(mas, max, &entry))
+ return entry;
+
+ /* Retries on dead nodes handled by mas_next_slot */
+ return mas_next_slot(mas, max, true);
+}
+EXPORT_SYMBOL_GPL(mas_next_range);
+
/**
* mt_next() - get the next value in the maple tree
* @mt: The maple tree
@@ -6031,49 +6065,41 @@ void mas_pause(struct ma_state *mas)
EXPORT_SYMBOL_GPL(mas_pause);
/**
- * mas_find() - On the first call, find the entry at or after mas->index up to
- * %max. Otherwise, find the entry after mas->index.
+ * mas_find_setup() - Internal function to set up mas_find*().
* @mas: The maple state
- * @max: The maximum value to check.
- *
- * Must hold rcu_read_lock or the write lock.
- * If an entry exists, last and index are updated accordingly.
- * May set @mas->node to MAS_NONE.
+ * @max: The maximum index
+ * @entry: Pointer to the entry
*
- * Return: The entry or %NULL.
+ * Returns: True if entry is the answer, false otherwise.
*/
-void *mas_find(struct ma_state *mas, unsigned long max)
+static inline bool mas_find_setup(struct ma_state *mas, unsigned long max,
+ void **entry)
{
+ *entry = NULL;
+
if (unlikely(mas_is_none(mas))) {
if (unlikely(mas->last >= max))
- return NULL;
+ return true;
mas->index = mas->last;
mas->node = MAS_START;
- }
-
- if (unlikely(mas_is_paused(mas))) {
+ } else if (unlikely(mas_is_paused(mas))) {
if (unlikely(mas->last >= max))
- return NULL;
+ return true;
mas->node = MAS_START;
mas->index = ++mas->last;
- }
-
-
- if (unlikely(mas_is_ptr(mas)))
+ } else if (unlikely(mas_is_ptr(mas)))
goto ptr_out_of_range;
if (unlikely(mas_is_start(mas))) {
/* First run or continue */
- void *entry;
-
if (mas->index > max)
- return NULL;
+ return true;
- entry = mas_walk(mas);
- if (entry)
- return entry;
+ *entry = mas_walk(mas);
+ if (*entry)
+ return true;
}
@@ -6081,23 +6107,69 @@ void *mas_find(struct ma_state *mas, unsigned long max)
if (unlikely(mas_is_ptr(mas)))
goto ptr_out_of_range;
- return NULL;
+ return true;
}
if (mas->index == max)
- return NULL;
+ return true;
- /* Retries on dead nodes handled by mas_next_slot */
- return mas_next_slot(mas, max, false);
+ return false;
ptr_out_of_range:
mas->node = MAS_NONE;
mas->index = 1;
mas->last = ULONG_MAX;
- return NULL;
+ return true;
+}
+
+/**
+ * mas_find() - On the first call, find the entry at or after mas->index up to
+ * %max. Otherwise, find the entry after mas->index.
+ * @mas: The maple state
+ * @max: The maximum value to check.
+ *
+ * Must hold rcu_read_lock or the write lock.
+ * If an entry exists, last and index are updated accordingly.
+ * May set @mas->node to MAS_NONE.
+ *
+ * Return: The entry or %NULL.
+ */
+void *mas_find(struct ma_state *mas, unsigned long max)
+{
+ void *entry = NULL;
+
+ if (mas_find_setup(mas, max, &entry))
+ return entry;
+
+ /* Retries on dead nodes handled by mas_next_slot */
+ return mas_next_slot(mas, max, false);
}
EXPORT_SYMBOL_GPL(mas_find);
+/**
+ * mas_find_range() - On the first call, find the entry at or after
+ * mas->index up to %max. Otherwise, advance to the next slot mas->index.
+ * @mas: The maple state
+ * @max: The maximum value to check.
+ *
+ * Must hold rcu_read_lock or the write lock.
+ * If an entry exists, last and index are updated accordingly.
+ * May set @mas->node to MAS_NONE.
+ *
+ * Return: The entry or %NULL.
+ */
+void *mas_find_range(struct ma_state *mas, unsigned long max)
+{
+ void *entry;
+
+ if (mas_find_setup(mas, max, &entry))
+ return entry;
+
+ /* Retries on dead nodes handled by mas_next_slot */
+ return mas_next_slot(mas, max, true);
+}
+EXPORT_SYMBOL_GPL(mas_find_range);
+
/**
* mas_find_rev: On the first call, find the first non-null entry at or below
* mas->index down to %min. Otherwise find the first non-null entry below
--
2.39.2
next prev parent reply other threads:[~2023-05-12 18:22 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-12 18:20 [PATCH v3 00/35] Maple tree mas_{next,prev}_range() and cleanup Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 01/35] maple_tree: Fix static analyser cppcheck issue Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 02/35] maple_tree: Clean up mas_parent_enum() and rename to mas_parent_type() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 03/35] maple_tree: Avoid unnecessary ascending Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 04/35] maple_tree: Clean up mas_dfs_postorder() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 05/35] maple_tree: Add format option to mt_dump() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 06/35] maple_tree: Add debug BUG_ON and WARN_ON variants Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 07/35] maple_tree: Convert BUG_ON() to MT_BUG_ON() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 08/35] maple_tree: Change RCU checks to WARN_ON() instead of BUG_ON() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 09/35] maple_tree: Convert debug code to use MT_WARN_ON() and MAS_WARN_ON() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 10/35] maple_tree: Use MAS_BUG_ON() when setting a leaf node as a parent Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 11/35] maple_tree: Use MAS_BUG_ON() in mas_set_height() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 12/35] maple_tree: Use MAS_BUG_ON() from mas_topiary_range() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 13/35] maple_tree: Use MAS_WR_BUG_ON() in mas_store_prealloc() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 14/35] maple_tree: Use MAS_BUG_ON() prior to calling mas_meta_gap() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 15/35] maple_tree: Return error on mte_pivots() out of range Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 16/35] maple_tree: Make test code work without debug enabled Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 17/35] mm: Update validate_mm() to use vma iterator Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 18/35] mm: Update vma_iter_store() to use MAS_WARN_ON() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 19/35] maple_tree: Add __init and __exit to test module Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 20/35] maple_tree: Remove unnecessary check from mas_destroy() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 21/35] maple_tree: mas_start() reset depth on dead node Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 22/35] mm/mmap: Change do_vmi_align_munmap() for maple tree iterator changes Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 23/35] maple_tree: Try harder to keep active node after mas_next() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 24/35] maple_tree: Try harder to keep active node with mas_prev() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 25/35] maple_tree: Revise limit checks in mas_empty_area{_rev}() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 26/35] maple_tree: Fix testing mas_empty_area() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 27/35] maple_tree: Introduce mas_next_slot() interface Liam R. Howlett
2023-05-12 18:20 ` Liam R. Howlett [this message]
2023-05-12 23:08 ` [PATCH v3 28/35] maple_tree: Add mas_next_range() and mas_find_range() interfaces Andrew Morton
2023-05-15 20:26 ` Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 29/35] maple_tree: Relocate mas_rewalk() and mas_rewalk_if_dead() Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 30/35] maple_tree: Introduce mas_prev_slot() interface Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 31/35] maple_tree: Add mas_prev_range() and mas_find_range_rev interface Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 32/35] maple_tree: Clear up index and last setting in single entry tree Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 33/35] maple_tree: Update testing code for mas_{next,prev,walk} Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 34/35] mm: Add vma_iter_{next,prev}_range() to vma iterator Liam R. Howlett
2023-05-12 18:20 ` [PATCH v3 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=20230512182036.359030-29-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