Skip to contentSkip to conversation

mm/damon/tests/vaddr-kunit: init maple tree without MT_FLAGS_LOCK_EXTERN

PATCH Applied SeongJae Park 3 participants 5 messages Sep 4, 2024

Subsystems (3)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

PATCH v2 1/1 mm/damon/tests/vaddr-kunit: init maple tree without MT_FLAGS_LOCK_EXTERN

damon_test_three_regions_in_vmas() initializes a maple tree with MM_MT_FLAGS. The flags contains MT_FLAGS_LOCK_EXTERN, which means mt_lock of the maple tree will not be used. And therefore the maple tree initialization code skips initialization of the mt_lock. However, __link_vmas(), which adds vmas for test to the maple tree, uses the mt_lock. In other words, the uninitialized spinlock is used. The problem becomes clear when spinlock debugging is turned on, since it reports spinlock bad magic bug.

Fix the issue by excluding MT_FLAGS_LOCK_EXTERN from the maple tree initialization flags. Note that we don't use empty flags to make it further similar to the usage of mm maple tree, and to be prepared for possible future changes, as suggested by Liam.

Fixes d0cf3dd47f0d ("damon: convert __damon_va_three_regions to use the VMA iterator")

---
Changes from v1
(https://lore.kernel.org/[email protected])
- Keep lock usage and update the initialization flags (Liam)
- Fix a typo: s/celar/clear/ (Guenter)

 mm/damon/tests/vaddr-kunit.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/damon/tests/vaddr-kunit.h +1 -1
index 83626483f82b..a339d117150f 100644
@@ -77,7 +77,7 @@ static void damon_test_three_regions_in_vmas(struct kunit *test)
 		(struct vm_area_struct) {.vm_start = 307, .vm_end = 330},
 	};
 
-	mt_init_flags(&mm.mm_mt, MM_MT_FLAGS);
+	mt_init_flags(&mm.mm_mt, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU);
 	if (__link_vmas(&mm.mm_mt, vmas, ARRAY_SIZE(vmas)))
 		kunit_skip(test, "Failed to create VMA tree");
 
Signature · 1 line
2.39.2
#1 lore raw
Quoted 19 lines · SeongJae Park

damon_test_three_regions_in_vmas() initializes a maple tree with MM_MT_FLAGS. The flags contains MT_FLAGS_LOCK_EXTERN, which means mt_lock of the maple tree will not be used. And therefore the maple tree initialization code skips initialization of the mt_lock. However, __link_vmas(), which adds vmas for test to the maple tree, uses the mt_lock. In other words, the uninitialized spinlock is used. The problem becomes clear when spinlock debugging is turned on, since it reports spinlock bad magic bug.

Fix the issue by excluding MT_FLAGS_LOCK_EXTERN from the maple tree initialization flags. Note that we don't use empty flags to make it further similar to the usage of mm maple tree, and to be prepared for possible future changes, as suggested by Liam.

Fixes d0cf3dd47f0d ("damon: convert __damon_va_three_regions to use the VMA iterator")
Quoted 4 lines

---
Changes from v1
(https://lore.kernel.org/[email protected])
- Keep lock usage and update the initialization flags (Liam)

Not sure I understand how this is better. Is the resulting rcu warning considered to be irrelevant or a separate problem ?

Thanks,
Guenter

Quoted 18 lines

- Fix a typo: s/celar/clear/ (Guenter)

  mm/damon/tests/vaddr-kunit.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/damon/tests/vaddr-kunit.h +1 -1
index 83626483f82b..a339d117150f 100644
@@ -77,7 +77,7 @@ static void damon_test_three_regions_in_vmas(struct kunit *test)
  		(struct vm_area_struct) {.vm_start = 307, .vm_end = 330},
  	};
 
-	mt_init_flags(&mm.mm_mt, MM_MT_FLAGS);
+	mt_init_flags(&mm.mm_mt, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU);
  	if (__link_vmas(&mm.mm_mt, vmas, ARRAY_SIZE(vmas)))
  		kunit_skip(test, "Failed to create VMA tree");
 
author maintainer
#2 lore raw
Quoted 25 lines · On Wed, 4 Sep 2024 11:41:28 -0700 Guenter Roeck <[email protected]> wrote:
Quoted 19 lines · SeongJae Park

damon_test_three_regions_in_vmas() initializes a maple tree with MM_MT_FLAGS. The flags contains MT_FLAGS_LOCK_EXTERN, which means mt_lock of the maple tree will not be used. And therefore the maple tree initialization code skips initialization of the mt_lock. However, __link_vmas(), which adds vmas for test to the maple tree, uses the mt_lock. In other words, the uninitialized spinlock is used. The problem becomes clear when spinlock debugging is turned on, since it reports spinlock bad magic bug.

Fix the issue by excluding MT_FLAGS_LOCK_EXTERN from the maple tree initialization flags. Note that we don't use empty flags to make it further similar to the usage of mm maple tree, and to be prepared for possible future changes, as suggested by Liam.

Fixes d0cf3dd47f0d ("damon: convert __damon_va_three_regions to use the VMA iterator")
Quoted 4 lines

---
Changes from v1
(https://lore.kernel.org/[email protected])
- Keep lock usage and update the initialization flags (Liam)

Quoted 2 lines

Not sure I understand how this is better. Is the resulting rcu warning considered to be irrelevant or a separate problem ?

I believe it's a separate potential problem (warning) that deserves a look. Meanwhile, this patch is fixing an issue that is definitely a bug, so better to fix right now, imho.

Thanks,
SJ

[...]

#3 lore raw
Quoted 27 lines · SeongJae Park
Quoted 25 lines · On Wed, 4 Sep 2024 11:41:28 -0700 Guenter Roeck <[email protected]> wrote:
Quoted 23 lines · SeongJae Park

damon_test_three_regions_in_vmas() initializes a maple tree with MM_MT_FLAGS. The flags contains MT_FLAGS_LOCK_EXTERN, which means mt_lock of the maple tree will not be used. And therefore the maple tree initialization code skips initialization of the mt_lock. However, __link_vmas(), which adds vmas for test to the maple tree, uses the mt_lock. In other words, the uninitialized spinlock is used. The problem becomes clear when spinlock debugging is turned on, since it reports spinlock bad magic bug.

Fix the issue by excluding MT_FLAGS_LOCK_EXTERN from the maple tree initialization flags. Note that we don't use empty flags to make it further similar to the usage of mm maple tree, and to be prepared for possible future changes, as suggested by Liam.

Fixes d0cf3dd47f0d ("damon: convert __damon_va_three_regions to use the VMA iterator")

---
Changes from v1
(https://lore.kernel.org/[email protected])
- Keep lock usage and update the initialization flags (Liam)

Quoted 7 lines
Quoted 2 lines

Not sure I understand how this is better. Is the resulting rcu warning considered to be irrelevant or a separate problem ?

I believe it's a separate potential problem (warning) that deserves a look. Meanwhile, this patch is fixing an issue that is definitely a bug, so better to fix right now, imho.

Whenever you walk the tree, you need to hold the write lock or the rcu read lock. I believe that __damon_va_three_regions() needs to have the rcu_read_lock()/ruc_read_unlock() around the for_each_vma() loop.

I sent a patch to the old thread that I suspect will fix it - but I don't see the issue so it is hard to say for sure.

Thanks,
Liam

Tested-by on v2 1/1
#4 lore raw
In reply to SeongJae Park
Quoted 14 lines · SeongJae Park

damon_test_three_regions_in_vmas() initializes a maple tree with MM_MT_FLAGS. The flags contains MT_FLAGS_LOCK_EXTERN, which means mt_lock of the maple tree will not be used. And therefore the maple tree initialization code skips initialization of the mt_lock. However, __link_vmas(), which adds vmas for test to the maple tree, uses the mt_lock. In other words, the uninitialized spinlock is used. The problem becomes clear when spinlock debugging is turned on, since it reports spinlock bad magic bug.

Fix the issue by excluding MT_FLAGS_LOCK_EXTERN from the maple tree initialization flags. Note that we don't use empty flags to make it further similar to the usage of mm maple tree, and to be prepared for possible future changes, as suggested by Liam.

Quoted 5 lines
Fixes d0cf3dd47f0d ("damon: convert __damon_va_three_regions to use the VMA iterator")
Quoted 25 lines

---
Changes from v1
(https://lore.kernel.org/[email protected])
- Keep lock usage and update the initialization flags (Liam)
- Fix a typo: s/celar/clear/ (Guenter)

 mm/damon/tests/vaddr-kunit.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/damon/tests/vaddr-kunit.h +1 -1
index 83626483f82b..a339d117150f 100644
@@ -77,7 +77,7 @@ static void damon_test_three_regions_in_vmas(struct kunit *test)
 		(struct vm_area_struct) {.vm_start = 307, .vm_end = 330},
 	};
 
-	mt_init_flags(&mm.mm_mt, MM_MT_FLAGS);
+	mt_init_flags(&mm.mm_mt, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU);
 	if (__link_vmas(&mm.mm_mt, vmas, ARRAY_SIZE(vmas)))
 		kunit_skip(test, "Failed to create VMA tree");
 
Signature · 1 line
2.39.2