linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] maple_tree: Fix status setup on restore to active
@ 2025-06-24 15:48 Liam R. Howlett
  2025-06-24 15:48 ` [PATCH 2/2] maple_tree: Add testing for restoring maple state " Liam R. Howlett
  2025-06-25  1:11 ` [PATCH 1/2] maple_tree: Fix status setup on restore " Wei Yang
  0 siblings, 2 replies; 4+ messages in thread
From: Liam R. Howlett @ 2025-06-24 15:48 UTC (permalink / raw)
  To: Andrew Morton
  Cc: maple-tree, linux-mm, linux-kernel, Wei Yang, kernel test robot,
	Liam R. Howlett

During the initial call with a maple state, an error status may be set
before a valid node is populated into the maple state node.  Subsequent
calls with the maple state may restore the state into an active state
with no node set.  This was masked by the mas_walk() always resetting
the status to ma_state and result in an extra walk in this rare
scenario.

Don't restore the state to active unless there is a value in the structs
node.  This also allows mas_walk() to be fixed to use the active state
without exposing an issue.

User visible results are marginal performance improvements when an
active state can be restored and used instead of rewalking the tree.

Stable is not Cc'ed because the existing code is stable and the
performance gains are not worth the risk.

Link: https://lore.kernel.org/all/20250611011253.19515-1-richard.weiyang@gmail.com/
Link: https://lore.kernel.org/all/20250407231354.11771-1-richard.weiyang@gmail.com/
Link: https://lore.kernel.org/all/202506191556.6bfc7b93-lkp@intel.com/
Fixes: a8091f039c1e ("maple_tree: add MAS_UNDERFLOW and MAS_OVERFLOW states")
Reported-by: Wei Yang <richard.weiyang@gmail.com>
Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202506191556.6bfc7b93-lkp@intel.com
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
 lib/maple_tree.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index 00524e55a21e0..6b0fc6ebbe363 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -4930,7 +4930,7 @@ void *mas_walk(struct ma_state *mas)
 {
 	void *entry;
 
-	if (!mas_is_active(mas) || !mas_is_start(mas))
+	if (!mas_is_active(mas) && !mas_is_start(mas))
 		mas->status = ma_start;
 retry:
 	entry = mas_state_walk(mas);
@@ -5658,6 +5658,17 @@ int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
 }
 EXPORT_SYMBOL_GPL(mas_expected_entries);
 
+static void mas_may_activate(struct ma_state *mas)
+{
+	if (!mas->node) {
+		mas->status = ma_start;
+	} else if (mas->index > mas->max || mas->index < mas->min) {
+		mas->status = ma_start;
+	} else {
+		mas->status = ma_active;
+	}
+}
+
 static bool mas_next_setup(struct ma_state *mas, unsigned long max,
 		void **entry)
 {
@@ -5681,11 +5692,11 @@ static bool mas_next_setup(struct ma_state *mas, unsigned long max,
 		break;
 	case ma_overflow:
 		/* Overflowed before, but the max changed */
-		mas->status = ma_active;
+		mas_may_activate(mas);
 		break;
 	case ma_underflow:
 		/* The user expects the mas to be one before where it is */
-		mas->status = ma_active;
+		mas_may_activate(mas);
 		*entry = mas_walk(mas);
 		if (*entry)
 			return true;
@@ -5806,11 +5817,11 @@ static bool mas_prev_setup(struct ma_state *mas, unsigned long min, void **entry
 		break;
 	case ma_underflow:
 		/* underflowed before but the min changed */
-		mas->status = ma_active;
+		mas_may_activate(mas);
 		break;
 	case ma_overflow:
 		/* User expects mas to be one after where it is */
-		mas->status = ma_active;
+		mas_may_activate(mas);
 		*entry = mas_walk(mas);
 		if (*entry)
 			return true;
@@ -5975,7 +5986,7 @@ static __always_inline bool mas_find_setup(struct ma_state *mas, unsigned long m
 			return true;
 		}
 
-		mas->status = ma_active;
+		mas_may_activate(mas);
 		*entry = mas_walk(mas);
 		if (*entry)
 			return true;
@@ -5984,7 +5995,7 @@ static __always_inline bool mas_find_setup(struct ma_state *mas, unsigned long m
 		if (unlikely(mas->last >= max))
 			return true;
 
-		mas->status = ma_active;
+		mas_may_activate(mas);
 		*entry = mas_walk(mas);
 		if (*entry)
 			return true;
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] maple_tree: Add testing for restoring maple state to active
  2025-06-24 15:48 [PATCH 1/2] maple_tree: Fix status setup on restore to active Liam R. Howlett
@ 2025-06-24 15:48 ` Liam R. Howlett
  2025-06-25  1:22   ` Wei Yang
  2025-06-25  1:11 ` [PATCH 1/2] maple_tree: Fix status setup on restore " Wei Yang
  1 sibling, 1 reply; 4+ messages in thread
From: Liam R. Howlett @ 2025-06-24 15:48 UTC (permalink / raw)
  To: Andrew Morton
  Cc: maple-tree, linux-mm, linux-kernel, Wei Yang, kernel test robot,
	Liam R. Howlett

Restoring maple status to ma_active on overflow/underflow when mas->node
was NULL could have happened in the past, but was masked by a bug in
mas_walk().  Add test cases that triggered the bug when the node was
mas->node prior to fixing the maple state setup.

Add a few extra tests around restoring the active maple status.

Link: https://lore.kernel.org/all/202506191556.6bfc7b93-lkp@intel.com/
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
 lib/test_maple_tree.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/lib/test_maple_tree.c b/lib/test_maple_tree.c
index 13e2a10d7554d..cb3936595b0d5 100644
--- a/lib/test_maple_tree.c
+++ b/lib/test_maple_tree.c
@@ -3177,6 +3177,7 @@ static noinline void __init check_state_handling(struct maple_tree *mt)
 	void *entry, *ptr = (void *) 0x1234500;
 	void *ptr2 = &ptr;
 	void *ptr3 = &ptr2;
+	unsigned long index;
 
 	/* Check MAS_ROOT First */
 	mtree_store_range(mt, 0, 0, ptr, GFP_KERNEL);
@@ -3706,6 +3707,37 @@ static noinline void __init check_state_handling(struct maple_tree *mt)
 	MT_BUG_ON(mt, mas.last != 0x1fff);
 	MT_BUG_ON(mt, !mas_is_active(&mas));
 
+	mas_unlock(&mas);
+	mtree_destroy(mt);
+
+	mt_init_flags(mt, MT_FLAGS_ALLOC_RANGE);
+	mas_lock(&mas);
+	for (int count = 0; count < 30; count++) {
+		mas_set(&mas, count);
+		mas_store_gfp(&mas, xa_mk_value(count), GFP_KERNEL);
+	}
+
+	/* Ensure mas_find works with MA_UNDERFLOW */
+	mas_set(&mas, 0);
+	entry = mas_walk(&mas);
+	mas_set(&mas, 0);
+	mas_prev(&mas, 0);
+	MT_BUG_ON(mt, mas.status != ma_underflow);
+	MT_BUG_ON(mt, mas_find(&mas, ULONG_MAX) != entry);
+
+	/* Restore active on mas_next */
+	entry = mas_next(&mas, ULONG_MAX);
+	index = mas.index;
+	mas_prev(&mas, index);
+	MT_BUG_ON(mt, mas.status != ma_underflow);
+	MT_BUG_ON(mt, mas_next(&mas, ULONG_MAX) != entry);
+
+	/* Ensure overflow -> active works */
+	mas_prev(&mas, 0);
+	mas_next(&mas, index - 1);
+	MT_BUG_ON(mt, mas.status != ma_overflow);
+	MT_BUG_ON(mt, mas_next(&mas, ULONG_MAX) != entry);
+
 	mas_unlock(&mas);
 }
 
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] maple_tree: Fix status setup on restore to active
  2025-06-24 15:48 [PATCH 1/2] maple_tree: Fix status setup on restore to active Liam R. Howlett
  2025-06-24 15:48 ` [PATCH 2/2] maple_tree: Add testing for restoring maple state " Liam R. Howlett
@ 2025-06-25  1:11 ` Wei Yang
  1 sibling, 0 replies; 4+ messages in thread
From: Wei Yang @ 2025-06-25  1:11 UTC (permalink / raw)
  To: Liam R. Howlett
  Cc: Andrew Morton, maple-tree, linux-mm, linux-kernel, Wei Yang,
	kernel test robot

On Tue, Jun 24, 2025 at 11:48:22AM -0400, Liam R. Howlett wrote:
>During the initial call with a maple state, an error status may be set
>before a valid node is populated into the maple state node.  Subsequent
>calls with the maple state may restore the state into an active state
>with no node set.  This was masked by the mas_walk() always resetting
>the status to ma_state and result in an extra walk in this rare
               ^^^

Nit

s/ma_state/ma_start/

>scenario.
>
>Don't restore the state to active unless there is a value in the structs
>node.  This also allows mas_walk() to be fixed to use the active state
>without exposing an issue.
>
>User visible results are marginal performance improvements when an
>active state can be restored and used instead of rewalking the tree.
>
>Stable is not Cc'ed because the existing code is stable and the
>performance gains are not worth the risk.
>
>Link: https://lore.kernel.org/all/20250611011253.19515-1-richard.weiyang@gmail.com/
>Link: https://lore.kernel.org/all/20250407231354.11771-1-richard.weiyang@gmail.com/
>Link: https://lore.kernel.org/all/202506191556.6bfc7b93-lkp@intel.com/
>Fixes: a8091f039c1e ("maple_tree: add MAS_UNDERFLOW and MAS_OVERFLOW states")
>Reported-by: Wei Yang <richard.weiyang@gmail.com>
>Reported-by: kernel test robot <oliver.sang@intel.com>
>Closes: https://lore.kernel.org/oe-lkp/202506191556.6bfc7b93-lkp@intel.com
>Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>

Reviewed-by: Wei Yang <richard.weiyang@gmail.com>

-- 
Wei Yang
Help you, Help me

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] maple_tree: Add testing for restoring maple state to active
  2025-06-24 15:48 ` [PATCH 2/2] maple_tree: Add testing for restoring maple state " Liam R. Howlett
@ 2025-06-25  1:22   ` Wei Yang
  0 siblings, 0 replies; 4+ messages in thread
From: Wei Yang @ 2025-06-25  1:22 UTC (permalink / raw)
  To: Liam R. Howlett
  Cc: Andrew Morton, maple-tree, linux-mm, linux-kernel, Wei Yang,
	kernel test robot

On Tue, Jun 24, 2025 at 11:48:23AM -0400, Liam R. Howlett wrote:
>Restoring maple status to ma_active on overflow/underflow when mas->node
>was NULL could have happened in the past, but was masked by a bug in
>mas_walk().  Add test cases that triggered the bug when the node was
>mas->node prior to fixing the maple state setup.
>
>Add a few extra tests around restoring the active maple status.
>
>Link: https://lore.kernel.org/all/202506191556.6bfc7b93-lkp@intel.com/
>Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>

Reviewed-by: Wei Yang <richard.weiyang@gmail.com>

-- 
Wei Yang
Help you, Help me

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-25  1:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-24 15:48 [PATCH 1/2] maple_tree: Fix status setup on restore to active Liam R. Howlett
2025-06-24 15:48 ` [PATCH 2/2] maple_tree: Add testing for restoring maple state " Liam R. Howlett
2025-06-25  1:22   ` Wei Yang
2025-06-25  1:11 ` [PATCH 1/2] maple_tree: Fix status setup on restore " Wei Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).