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>,
Vernon Yang <vernon2gm@gmail.com>
Subject: [PATCH v4 31/35] maple_tree: Add mas_prev_range() and mas_find_range_rev interface
Date: Thu, 18 May 2023 10:55:40 -0400 [thread overview]
Message-ID: <20230518145544.1722059-32-Liam.Howlett@oracle.com> (raw)
In-Reply-To: <20230518145544.1722059-1-Liam.Howlett@oracle.com>
Some users of the maple tree may want to move to the previous range
regardless of the value stored there. Add this interface as well as the
'find' variant to support walking to the first value, then iterating
over the previous ranges.
Cc: Vernon Yang <vernon2gm@gmail.com>
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
include/linux/maple_tree.h | 2 +
lib/maple_tree.c | 161 ++++++++++++++++++++++++++++---------
2 files changed, 124 insertions(+), 39 deletions(-)
diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h
index 9d040043858a..541675229568 100644
--- a/include/linux/maple_tree.h
+++ b/include/linux/maple_tree.h
@@ -457,6 +457,7 @@ 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);
+void *mas_find_range_rev(struct ma_state *mas, unsigned long max);
int mas_preallocate(struct ma_state *mas, gfp_t gfp);
bool mas_is_err(struct ma_state *mas);
@@ -467,6 +468,7 @@ void mas_destroy(struct ma_state *mas);
int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries);
void *mas_prev(struct ma_state *mas, unsigned long min);
+void *mas_prev_range(struct ma_state *mas, unsigned long max);
void *mas_next(struct ma_state *mas, unsigned long max);
void *mas_next_range(struct ma_state *mas, unsigned long max);
diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index f0e9ce5b0515..fe481d4e5e6a 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -5924,18 +5924,8 @@ void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max)
}
EXPORT_SYMBOL_GPL(mt_next);
-/**
- * mas_prev() - Get the previous entry
- * @mas: The maple state
- * @min: The minimum value to check.
- *
- * Must hold rcu_read_lock or the write lock.
- * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not
- * searchable nodes.
- *
- * Return: the previous value or %NULL.
- */
-void *mas_prev(struct ma_state *mas, unsigned long min)
+static inline bool mas_prev_setup(struct ma_state *mas, unsigned long min,
+ void **entry)
{
if (mas->index <= min)
goto none;
@@ -5953,7 +5943,8 @@ void *mas_prev(struct ma_state *mas, unsigned long min)
if (!mas->index)
goto none;
mas->index = mas->last = 0;
- return mas_root(mas);
+ *entry = mas_root(mas);
+ return true;
}
if (mas_is_none(mas)) {
@@ -5961,18 +5952,64 @@ void *mas_prev(struct ma_state *mas, unsigned long min)
/* Walked to out-of-range pointer? */
mas->index = mas->last = 0;
mas->node = MAS_ROOT;
- return mas_root(mas);
+ *entry = mas_root(mas);
+ return true;
}
- return NULL;
+ return true;
}
- return mas_prev_slot(mas, min, false);
+
+ return false;
none:
mas->node = MAS_NONE;
- return NULL;
+ return true;
+}
+
+/**
+ * mas_prev() - Get the previous entry
+ * @mas: The maple state
+ * @min: The minimum value to check.
+ *
+ * Must hold rcu_read_lock or the write lock.
+ * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not
+ * searchable nodes.
+ *
+ * Return: the previous value or %NULL.
+ */
+void *mas_prev(struct ma_state *mas, unsigned long min)
+{
+ void *entry = NULL;
+
+ if (mas_prev_setup(mas, min, &entry))
+ return entry;
+
+ return mas_prev_slot(mas, min, false);
}
EXPORT_SYMBOL_GPL(mas_prev);
+/**
+ * mas_prev_range() - Advance to the previous range
+ * @mas: The maple state
+ * @min: The minimum value to check.
+ *
+ * Sets @mas->index and @mas->last to the range.
+ * Must hold rcu_read_lock or the write lock.
+ * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not
+ * searchable nodes.
+ *
+ * Return: the previous value or %NULL.
+ */
+void *mas_prev_range(struct ma_state *mas, unsigned long min)
+{
+ void *entry = NULL;
+
+ if (mas_prev_setup(mas, min, &entry))
+ return entry;
+
+ return mas_prev_slot(mas, min, true);
+}
+EXPORT_SYMBOL_GPL(mas_prev_range);
+
/**
* mt_prev() - get the previous value in the maple tree
* @mt: The maple tree
@@ -6119,20 +6156,18 @@ void *mas_find_range(struct ma_state *mas, unsigned long max)
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
- * mas->index down to %min.
+ * mas_find_rev_setup() - Internal function to set up mas_find_*_rev()
* @mas: The maple state
- * @min: The minimum 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.
+ * @min: The minimum index
+ * @entry: Pointer to the entry
*
- * Return: The entry or %NULL.
+ * Returns: True if entry is the answer, false otherwise.
*/
-void *mas_find_rev(struct ma_state *mas, unsigned long min)
+static inline bool mas_find_rev_setup(struct ma_state *mas, unsigned long min,
+ void **entry)
{
+ *entry = NULL;
+
if (unlikely(mas_is_none(mas))) {
if (mas->index <= min)
goto none;
@@ -6144,7 +6179,7 @@ void *mas_find_rev(struct ma_state *mas, unsigned long min)
if (unlikely(mas_is_paused(mas))) {
if (unlikely(mas->index <= min)) {
mas->node = MAS_NONE;
- return NULL;
+ return true;
}
mas->node = MAS_START;
mas->last = --mas->index;
@@ -6152,14 +6187,12 @@ void *mas_find_rev(struct ma_state *mas, unsigned long min)
if (unlikely(mas_is_start(mas))) {
/* First run or continue */
- void *entry;
-
if (mas->index < min)
- return NULL;
+ return true;
- entry = mas_walk(mas);
- if (entry)
- return entry;
+ *entry = mas_walk(mas);
+ if (*entry)
+ return true;
}
if (unlikely(!mas_searchable(mas))) {
@@ -6173,22 +6206,72 @@ void *mas_find_rev(struct ma_state *mas, unsigned long min)
*/
mas->last = mas->index = 0;
mas->node = MAS_ROOT;
- return mas_root(mas);
+ *entry = mas_root(mas);
+ return true;
}
}
if (mas->index < min)
- return NULL;
+ return true;
- /* Retries on dead nodes handled by mas_prev_slot */
- return mas_prev_slot(mas, min, false);
+ return false;
none:
mas->node = MAS_NONE;
- return NULL;
+ return true;
+}
+
+/**
+ * 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
+ * mas->index down to %min.
+ * @mas: The maple state
+ * @min: The minimum 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_rev(struct ma_state *mas, unsigned long min)
+{
+ void *entry;
+
+ if (mas_find_rev_setup(mas, min, &entry))
+ return entry;
+
+ /* Retries on dead nodes handled by mas_prev_slot */
+ return mas_prev_slot(mas, min, false);
+
}
EXPORT_SYMBOL_GPL(mas_find_rev);
+/**
+ * mas_find_range_rev: On the first call, find the first non-null entry at or
+ * below mas->index down to %min. Otherwise advance to the previous slot after
+ * mas->index down to %min.
+ * @mas: The maple state
+ * @min: The minimum 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_rev(struct ma_state *mas, unsigned long min)
+{
+ void *entry;
+
+ if (mas_find_rev_setup(mas, min, &entry))
+ return entry;
+
+ /* Retries on dead nodes handled by mas_prev_slot */
+ return mas_prev_slot(mas, min, true);
+}
+EXPORT_SYMBOL_GPL(mas_find_range_rev);
+
/**
* mas_erase() - Find the range in which index resides and erase the entire
* range.
--
2.39.2
next prev parent reply other threads:[~2023-05-18 14:57 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 ` [PATCH v4 23/35] maple_tree: Try harder to keep active node after mas_next() Liam R. Howlett
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 ` Liam R. Howlett [this message]
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-32-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 \
--cc=vernon2gm@gmail.com \
/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