Skip to contentSkip to conversation

damon: vaddr-test: fix a missing check on list iterator

PATCH In review Xiaomeng Tong 2 participants 3 messages Mar 27, 2022 – Mar 28, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 damon: vaddr-test: fix a missing check on list iterator

The bug is here:

	KUNIT_EXPECT_EQ(test, r->ar.start, start + i * expected_width);
	KUNIT_EXPECT_EQ(test, r->ar.end, end);

For the damon_for_each_region(), just like list_for_each_entry(), the list iterator 'drm_crtc' will point to a bogus position containing HEAD if the list is empty or no element is found. This case must be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

To fix this bug, just mov two KUNIT_EXPECT_EQ() into the loop when found.

Fixes 044cd9750fe01 ("mm/damon/vaddr-test: split a test function having >1024 bytes frame size")
---
 mm/damon/vaddr-test.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/damon/vaddr-test.h +5 -3
index 6a1b9272ea12..98b7a9f54b35 100644
@@ -281,14 +281,16 @@ static void damon_test_split_evenly_succ(struct kunit *test,
 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), nr_pieces);
 
 	damon_for_each_region(r, t) {
-		if (i == nr_pieces - 1)
+		if (i == nr_pieces - 1) {
+			KUNIT_EXPECT_EQ(test,
+				r->ar.start, start + i * expected_width);
+			KUNIT_EXPECT_EQ(test, r->ar.end, end);
 			break;
+		}
 		KUNIT_EXPECT_EQ(test,
 				r->ar.start, start + i++ * expected_width);
 		KUNIT_EXPECT_EQ(test, r->ar.end, start + i * expected_width);
 	}
-	KUNIT_EXPECT_EQ(test, r->ar.start, start + i * expected_width);
-	KUNIT_EXPECT_EQ(test, r->ar.end, end);
 	damon_free_target(t);
 }
 
Signature · 1 line
2.17.1
#1 lore raw

Hi Xiaomeng,

Quoted 9 lines · On Sun, 27 Mar 2022 16:03:45 +0800 Xiaomeng Tong <[email protected]> wrote:

The bug is here:

	KUNIT_EXPECT_EQ(test, r->ar.start, start + i * expected_width);
	KUNIT_EXPECT_EQ(test, r->ar.end, end);

For the damon_for_each_region(), just like list_for_each_entry(), the list iterator 'drm_crtc' will point to a bogus position containing HEAD if the list is empty or no element is found. This case must be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

We ensure 'damon_va_evenly_split_region()' successes before executing the loop, so the issue cannot occur. That said, I think this patch makes code better to read. Could you please resend this patch after fixing the commit message?

Quoted 2 lines

To fix this bug, just mov two KUNIT_EXPECT_EQ() into the loop

s/mov/move

Quoted 14 lines

when found.

Fixes 044cd9750fe01 ("mm/damon/vaddr-test: split a test function having >1024 bytes frame size")
---
 mm/damon/vaddr-test.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/damon/vaddr-test.h +0 -0
index 6a1b9272ea12..98b7a9f54b35 100644

mm/damon/vaddr-test.h static void damon_test_split_evenly_succ(struct kunit *test, · line 281

Quoted 1 line
mm/damon/vaddr-test.h +0 -0
@@ -281,14 +281,16 @@ static void damon_test_split_evenly_succ(struct kunit *test,
 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), nr_pieces);

As mentioned above, this will ensure the loop will not result in the bogus pointer problem.

Quoted 22 lines
mm/damon/vaddr-test.h +5 -3
 	damon_for_each_region(r, t) {
-		if (i == nr_pieces - 1)
+		if (i == nr_pieces - 1) {
+			KUNIT_EXPECT_EQ(test,
+				r->ar.start, start + i * expected_width);
+			KUNIT_EXPECT_EQ(test, r->ar.end, end);
 			break;
+		}
 		KUNIT_EXPECT_EQ(test,
 				r->ar.start, start + i++ * expected_width);
 		KUNIT_EXPECT_EQ(test, r->ar.end, start + i * expected_width);
 	}
-	KUNIT_EXPECT_EQ(test, r->ar.start, start + i * expected_width);
-	KUNIT_EXPECT_EQ(test, r->ar.end, end);
 	damon_free_target(t);
 }
 
Signature · 1 line
2.17.1

Thanks,
SJ

#2 lore raw
Quoted 12 lines · SJ
Quoted 9 lines · On Sun, 27 Mar 2022 16:03:45 +0800 Xiaomeng Tong <[email protected]> wrote:

The bug is here:

	KUNIT_EXPECT_EQ(test, r->ar.start, start + i * expected_width);
	KUNIT_EXPECT_EQ(test, r->ar.end, end);

For the damon_for_each_region(), just like list_for_each_entry(), the list iterator 'drm_crtc' will point to a bogus position containing HEAD if the list is empty or no element is found. This case must be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

Quoted 3 lines

We ensure 'damon_va_evenly_split_region()' successes before executing the loop, so the issue cannot occur. That said, I think this patch makes code better to read. Could you please resend this patch after fixing the commit message?

Yes, you should be right. I have resend this patch with the commit message changed. Please check it, thank you.

--
Xiaomeng Tong