linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests
@ 2025-11-01 18:19 SeongJae Park
  2025-11-01 18:19 ` [PATCH 01/22] mm/damon/tests/core-kunit: fix memory leak in damon_test_set_filters_default_reject() SeongJae Park
                   ` (21 more replies)
  0 siblings, 22 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:19 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow,
	Kefeng Wang, Sang-Heon Jeon, damon, kunit-dev, linux-kernel,
	linux-kselftest, linux-mm

DAMON kunit tests were initially written assuming those will be run on
environments that are well controlled and therefore tolerant to
transient test failures and bugs in the test code itself.  The user-mode
linux based manual run of the tests is one example of such an
environment.  And the test code was written for adding more test
coverage as fast as possible, over making those safe and reliable.

As a result, the tests resulted in having a number of bugs including
real memory leaks, theoretical unhandled memory allocation failures, and
unused memory allocations.  The allocation failures that are not handled
well are unlikely in the real world, since those allocations are too
small to fail.  But in theory, it can happen and cause inappropriate
memory access.

It is arguable if bugs in test code can really harm users.  But, anyway
bugs are bugs that need to be fixed.  Fix the bugs one by one.  Also Cc
stable@ for the fixes of memory leak and unhandled memory allocation
failures.  The unused memory allocations are only a matter of memory
efficiency, so not Cc-ing stable@.

The first patch fixes memory leaks in the test code for the DAMON core
layer.

Following fifteen, three, and one patches respectively fix unhandled
memory allocation failures in the test code for DAMON core layer,
virtual address space DAMON operation set, and DAMON sysfs interface,
one by one per test function.

Final two patches remove memory allocations that are correctly
deallocated at the end, but not really being used by any code.

SeongJae Park (22):
  mm/damon/tests/core-kunit: fix memory leak in
    damon_test_set_filters_default_reject()
  mm/damon/tests/core-kunit: handle allocation failures in
    damon_test_regions()
  mm/damon/tests/core-kunit: handle memory failure from
    damon_test_target()
  mm/damon/tests/core-kunit: handle memory alloc failure from
    damon_test_aggregate()
  mm/damon/tests/core-kunit: handle alloc failures on
    damon_test_split_at()
  mm/damon/tests/core-kunit: handle alloc failures on
    damon_test_merge_two()
  mm/damon/tests/core-kunit: handle alloc failures on
    dasmon_test_merge_regions_of()
  mm/damon/tests/core-kunit: handle alloc failures on
    damon_test_split_regions_of()
  mm/damon/tests/core-kunit: handle alloc failures in
    damon_test_ops_registration()
  mm/damon/tests/core-kunit: handle alloc failures in
    damon_test_set_regions()
  mm/damon/tests/core-kunit: handle alloc failures in
    damon_test_update_monitoring_result()
  mm/damon/tests/core-kunit: handle alloc failure on
    damon_test_set_attrs()
  mm/damon/tests/core-kunit: handle alloc failres in
    damon_test_new_filter()
  mm/damon/tests/core-kunit: handle alloc failure on
    damos_test_commit_filter()
  mm/damon/tests/core-kunit: handle alloc failures on
    damos_test_filter_out()
  mm/damon/tests/core-kunit: handle alloc failures on
    damon_test_set_filters_default_reject()
  mm/damon/tests/vaddr-kunit: handle alloc failures on
    damon_do_test_apply_three_regions()
  mm/damon/tests/vaddr-kunit: handle alloc failures in
    damon_test_split_evenly_fail()
  mm/damon/tests/vaddr-kunit: handle alloc failures on
    damon_test_split_evenly_succ()
  mm/damon/tests/sysfs-kunit: handle alloc failures on
    damon_sysfs_test_add_targets()
  mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on
    damon_test_split_at()
  mm/damon/tests/core-kunit: remove unused ctx in
    damon_test_split_regions_of()

 mm/damon/tests/core-kunit.h  | 125 ++++++++++++++++++++++++++++++++---
 mm/damon/tests/sysfs-kunit.h |  25 +++++++
 mm/damon/tests/vaddr-kunit.h |  26 +++++++-
 3 files changed, 163 insertions(+), 13 deletions(-)


base-commit: 75f0c76bb8c01fdea838a601dc3326b11177c0d8
-- 
2.47.3

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

* [PATCH 01/22] mm/damon/tests/core-kunit: fix memory leak in damon_test_set_filters_default_reject()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
@ 2025-11-01 18:19 ` SeongJae Park
  2025-11-01 18:19 ` [PATCH 02/22] mm/damon/tests/core-kunit: handle allocation failures in damon_test_regions() SeongJae Park
                   ` (20 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:19 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 16 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

Kunit test function for damos_set_filters_default_reject() allocates two
'struct damos_filter' objects and not deallocates those, so that the
memory for the two objects are leaked for every time the test runs.  Fix
this by deallocating those objects at the end of the test code.

Fixes: 094fb14913c7 ("mm/damon/tests/core-kunit: add a test for damos_set_filters_default_reject()")
Cc: <stable@vger.kernel.org> # 6.16.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 51369e35298b..69ca44f9270b 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -598,6 +598,9 @@ static void damon_test_set_filters_default_reject(struct kunit *test)
 	 */
 	KUNIT_EXPECT_EQ(test, scheme.core_filters_default_reject, false);
 	KUNIT_EXPECT_EQ(test, scheme.ops_filters_default_reject, true);
+
+	damos_free_filter(anon_filter);
+	damos_free_filter(target_filter);
 }
 
 static struct kunit_case damon_test_cases[] = {
-- 
2.47.3

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

* [PATCH 02/22] mm/damon/tests/core-kunit: handle allocation failures in damon_test_regions()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
  2025-11-01 18:19 ` [PATCH 01/22] mm/damon/tests/core-kunit: fix memory leak in damon_test_set_filters_default_reject() SeongJae Park
@ 2025-11-01 18:19 ` SeongJae Park
  2025-11-01 18:19 ` [PATCH 03/22] mm/damon/tests/core-kunit: handle memory failure from damon_test_target() SeongJae Park
                   ` (19 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:19 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_regions() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 69ca44f9270b..a2c9ee7a5de1 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -20,11 +20,17 @@ static void damon_test_regions(struct kunit *test)
 	struct damon_target *t;
 
 	r = damon_new_region(1, 2);
+	if (!r)
+		kunit_skip(test, "region alloc fail");
 	KUNIT_EXPECT_EQ(test, 1ul, r->ar.start);
 	KUNIT_EXPECT_EQ(test, 2ul, r->ar.end);
 	KUNIT_EXPECT_EQ(test, 0u, r->nr_accesses);
 
 	t = damon_new_target();
+	if (!t) {
+		damon_free_region(r);
+		kunit_skip(test, "target alloc fail");
+	}
 	KUNIT_EXPECT_EQ(test, 0u, damon_nr_regions(t));
 
 	damon_add_region(r, t);
-- 
2.47.3

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

* [PATCH 03/22] mm/damon/tests/core-kunit: handle memory failure from damon_test_target()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
  2025-11-01 18:19 ` [PATCH 01/22] mm/damon/tests/core-kunit: fix memory leak in damon_test_set_filters_default_reject() SeongJae Park
  2025-11-01 18:19 ` [PATCH 02/22] mm/damon/tests/core-kunit: handle allocation failures in damon_test_regions() SeongJae Park
@ 2025-11-01 18:19 ` SeongJae Park
  2025-11-01 18:19 ` [PATCH 04/22] mm/damon/tests/core-kunit: handle memory alloc failure from damon_test_aggregate() SeongJae Park
                   ` (18 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:19 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_target() is assuming all dynamic memory allocation in it will
succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index a2c9ee7a5de1..6e8a605277a3 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -58,7 +58,14 @@ static void damon_test_target(struct kunit *test)
 	struct damon_ctx *c = damon_new_ctx();
 	struct damon_target *t;
 
+	if (!c)
+		kunit_skip(test, "ctx alloc fail");
+
 	t = damon_new_target();
+	if (!t) {
+		damon_destroy_ctx(c);
+		kunit_skip(test, "target alloc fail");
+	}
 	KUNIT_EXPECT_EQ(test, 0u, nr_damon_targets(c));
 
 	damon_add_target(c, t);
-- 
2.47.3

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

* [PATCH 04/22] mm/damon/tests/core-kunit: handle memory alloc failure from damon_test_aggregate()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (2 preceding siblings ...)
  2025-11-01 18:19 ` [PATCH 03/22] mm/damon/tests/core-kunit: handle memory failure from damon_test_target() SeongJae Park
@ 2025-11-01 18:19 ` SeongJae Park
  2025-11-01 18:19 ` [PATCH 05/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_at() SeongJae Park
                   ` (17 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:19 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_aggregate() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 6e8a605277a3..fd1e1ecaa2c9 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -97,8 +97,15 @@ static void damon_test_aggregate(struct kunit *test)
 	struct damon_region *r;
 	int it, ir;
 
+	if (!ctx)
+		kunit_skip(test, "ctx alloc fail");
+
 	for (it = 0; it < 3; it++) {
 		t = damon_new_target();
+		if (!t) {
+			damon_destroy_ctx(ctx);
+			kunit_skip(test, "target alloc fail");
+		}
 		damon_add_target(ctx, t);
 	}
 
@@ -106,6 +113,10 @@ static void damon_test_aggregate(struct kunit *test)
 	damon_for_each_target(t, ctx) {
 		for (ir = 0; ir < 3; ir++) {
 			r = damon_new_region(saddr[it][ir], eaddr[it][ir]);
+			if (!r) {
+				damon_destroy_ctx(ctx);
+				kunit_skip(test, "region alloc fail");
+			}
 			r->nr_accesses = accesses[it][ir];
 			r->nr_accesses_bp = accesses[it][ir] * 10000;
 			damon_add_region(r, t);
-- 
2.47.3

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

* [PATCH 05/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_at()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (3 preceding siblings ...)
  2025-11-01 18:19 ` [PATCH 04/22] mm/damon/tests/core-kunit: handle memory alloc failure from damon_test_aggregate() SeongJae Park
@ 2025-11-01 18:19 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 06/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_merge_two() SeongJae Park
                   ` (16 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:19 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_split_at() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index fd1e1ecaa2c9..f5f3152cb8df 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -148,8 +148,19 @@ static void damon_test_split_at(struct kunit *test)
 	struct damon_target *t;
 	struct damon_region *r, *r_new;
 
+	if (!c)
+		kunit_skip(test, "ctx alloc fail");
 	t = damon_new_target();
+	if (!t) {
+		damon_destroy_ctx(c);
+		kunit_skip(test, "target alloc fail");
+	}
 	r = damon_new_region(0, 100);
+	if (!r) {
+		damon_destroy_ctx(c);
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
 	r->nr_accesses_bp = 420000;
 	r->nr_accesses = 42;
 	r->last_nr_accesses = 15;
-- 
2.47.3

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

* [PATCH 06/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_merge_two()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (4 preceding siblings ...)
  2025-11-01 18:19 ` [PATCH 05/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_at() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 07/22] mm/damon/tests/core-kunit: handle alloc failures on dasmon_test_merge_regions_of() SeongJae Park
                   ` (15 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_merge_two() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index f5f3152cb8df..e8219fd23318 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -188,11 +188,21 @@ static void damon_test_merge_two(struct kunit *test)
 	int i;
 
 	t = damon_new_target();
+	if (!t)
+		kunit_skip(test, "target alloc fail");
 	r = damon_new_region(0, 100);
+	if (!r) {
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
 	r->nr_accesses = 10;
 	r->nr_accesses_bp = 100000;
 	damon_add_region(r, t);
 	r2 = damon_new_region(100, 300);
+	if (!r2) {
+		damon_free_target(t);
+		kunit_skip(test, "second region alloc fail");
+	}
 	r2->nr_accesses = 20;
 	r2->nr_accesses_bp = 200000;
 	damon_add_region(r2, t);
-- 
2.47.3

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

* [PATCH 07/22] mm/damon/tests/core-kunit: handle alloc failures on dasmon_test_merge_regions_of()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (5 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 06/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_merge_two() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 08/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_regions_of() SeongJae Park
                   ` (14 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_merge_regions_of() is assuming all dynamic memory allocation
in it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index e8219fd23318..98f2a3de7cea 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -248,8 +248,14 @@ static void damon_test_merge_regions_of(struct kunit *test)
 	int i;
 
 	t = damon_new_target();
+	if (!t)
+		kunit_skip(test, "target alloc fail");
 	for (i = 0; i < ARRAY_SIZE(sa); i++) {
 		r = damon_new_region(sa[i], ea[i]);
+		if (!r) {
+			damon_free_target(t);
+			kunit_skip(test, "region alloc fail");
+		}
 		r->nr_accesses = nrs[i];
 		r->nr_accesses_bp = nrs[i] * 10000;
 		damon_add_region(r, t);
-- 
2.47.3

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

* [PATCH 08/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_regions_of()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (6 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 07/22] mm/damon/tests/core-kunit: handle alloc failures on dasmon_test_merge_regions_of() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 09/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_ops_registration() SeongJae Park
                   ` (13 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_split_regions_of() is assuming all dynamic memory allocation
in it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 98f2a3de7cea..10618cdd188e 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -278,15 +278,35 @@ static void damon_test_split_regions_of(struct kunit *test)
 	struct damon_target *t;
 	struct damon_region *r;
 
+	if (!c)
+		kunit_skip("ctx alloc fail");
 	t = damon_new_target();
+	if (!t) {
+		damon_destroy_ctx(c);
+		kunit_skip(test, "target alloc fail");
+	}
 	r = damon_new_region(0, 22);
+	if (!r) {
+		damon_destroy_ctx(c);
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
 	damon_add_region(r, t);
 	damon_split_regions_of(t, 2, DAMON_MIN_REGION);
 	KUNIT_EXPECT_LE(test, damon_nr_regions(t), 2u);
 	damon_free_target(t);
 
 	t = damon_new_target();
+	if (!t) {
+		damon_destroy_ctx(c);
+		kunit_skip(test, "second target alloc fail");
+	}
 	r = damon_new_region(0, 220);
+	if (!r) {
+		damon_destroy_ctx(c);
+		damon_free_target(t);
+		kunit_skip(test, "second region alloc fail");
+	}
 	damon_add_region(r, t);
 	damon_split_regions_of(t, 4, DAMON_MIN_REGION);
 	KUNIT_EXPECT_LE(test, damon_nr_regions(t), 4u);
-- 
2.47.3

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

* [PATCH 09/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_ops_registration()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (7 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 08/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_regions_of() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 10/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_set_regions() SeongJae Park
                   ` (12 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 19 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_ops_registration() is assuming all dynamic memory allocation
in it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 4f540f5ab4f2 ("mm/damon/core-test: add a kunit test case for ops registration")
Cc: <stable@vger.kernel.org> # 5.19.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 10618cdd188e..96c8f1269f44 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -320,6 +320,9 @@ static void damon_test_ops_registration(struct kunit *test)
 	struct damon_operations ops = {.id = DAMON_OPS_VADDR}, bak;
 	bool need_cleanup = false;
 
+	if (!c)
+		kunit_skip(test, "ctx alloc fail");
+
 	/* DAMON_OPS_VADDR is registered only if CONFIG_DAMON_VADDR is set */
 	if (!damon_is_registered_ops(DAMON_OPS_VADDR)) {
 		bak.id = DAMON_OPS_VADDR;
-- 
2.47.3

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

* [PATCH 10/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_set_regions()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (8 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 09/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_ops_registration() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 11/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_update_monitoring_result() SeongJae Park
                   ` (11 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 1 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_set_regions() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 62f409560eb2 ("mm/damon/core-test: test damon_set_regions")
Cc: <stable@vger.kernel.org> # 6.1.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 96c8f1269f44..e38c95f86a68 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -368,13 +368,26 @@ static void damon_test_ops_registration(struct kunit *test)
 static void damon_test_set_regions(struct kunit *test)
 {
 	struct damon_target *t = damon_new_target();
-	struct damon_region *r1 = damon_new_region(4, 16);
-	struct damon_region *r2 = damon_new_region(24, 32);
+	struct damon_region *r1, *r2;
 	struct damon_addr_range range = {.start = 8, .end = 28};
 	unsigned long expects[] = {8, 16, 16, 24, 24, 28};
 	int expect_idx = 0;
 	struct damon_region *r;
 
+	if (!t)
+		kunit_skip(test, "target alloc fail");
+	r1 = damon_new_region(4, 16);
+	if (!r1) {
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
+	r2 = damon_new_region(24, 32);
+	if (!r2) {
+		damon_free_target(t);
+		damon_free_region(r1);
+		kunit_skip(test, "second region alloc fail");
+	}
+
 	damon_add_region(r1, t);
 	damon_add_region(r2, t);
 	damon_set_regions(t, &range, 1, DAMON_MIN_REGION);
-- 
2.47.3

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

* [PATCH 11/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_update_monitoring_result()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (9 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 10/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_set_regions() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 12/22] mm/damon/tests/core-kunit: handle alloc failure on damon_test_set_attrs() SeongJae Park
                   ` (10 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 3 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_update_monitoring_result() is assuming all dynamic memory
allocation in it will succeed.  Those are indeed likely in the real use
cases since those allocations are too small to fail, but theoretically
those could fail.  In the case, inappropriate memory access can happen.
Fix it by appropriately cleanup pre-allocated memory and skip the
execution of the remaining tests in the failure cases.

Fixes: f4c978b6594b ("mm/damon/core-test: add a test for damon_update_monitoring_results()")
Cc: <stable@vger.kernel.org> # 6.3.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index e38c95f86a68..10c9953581ee 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -429,6 +429,9 @@ static void damon_test_update_monitoring_result(struct kunit *test)
 	struct damon_attrs new_attrs;
 	struct damon_region *r = damon_new_region(3, 7);
 
+	if (!r)
+		kunit_skip(test, "region alloc fail");
+
 	r->nr_accesses = 15;
 	r->nr_accesses_bp = 150000;
 	r->age = 20;
-- 
2.47.3

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

* [PATCH 12/22] mm/damon/tests/core-kunit: handle alloc failure on damon_test_set_attrs()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (10 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 11/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_update_monitoring_result() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 13/22] mm/damon/tests/core-kunit: handle alloc failres in damon_test_new_filter() SeongJae Park
                   ` (9 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 5 . x, Brendan Higgins, David Gow,
	Kefeng Wang, damon, kunit-dev, linux-kernel, linux-kselftest,
	linux-mm

damon_test_set_attrs() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: aa13779be6b7 ("mm/damon/core-test: add a test for damon_set_attrs()")
Cc: <stable@vger.kernel.org> # 6.5.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 10c9953581ee..b9bd69a57e62 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -465,6 +465,9 @@ static void damon_test_set_attrs(struct kunit *test)
 		.sample_interval = 5000, .aggr_interval = 100000,};
 	struct damon_attrs invalid_attrs;
 
+	if (!c)
+		kunit_skip(test, "ctx alloc fail");
+
 	KUNIT_EXPECT_EQ(test, damon_set_attrs(c, &valid_attrs), 0);
 
 	invalid_attrs = valid_attrs;
-- 
2.47.3

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

* [PATCH 13/22] mm/damon/tests/core-kunit: handle alloc failres in damon_test_new_filter()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (11 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 12/22] mm/damon/tests/core-kunit: handle alloc failure on damon_test_set_attrs() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 14/22] mm/damon/tests/core-kunit: handle alloc failure on damos_test_commit_filter() SeongJae Park
                   ` (8 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 6 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_new_filter() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 2a158e956b98 ("mm/damon/core-test: add a test for damos_new_filter()")
Cc: <stable@vger.kernel.org> # 6.6.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index b9bd69a57e62..03c7ac31db5c 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -505,6 +505,8 @@ static void damos_test_new_filter(struct kunit *test)
 	struct damos_filter *filter;
 
 	filter = damos_new_filter(DAMOS_FILTER_TYPE_ANON, true, false);
+	if (!filter)
+		kunit_skip(test, "filter alloc fail");
 	KUNIT_EXPECT_EQ(test, filter->type, DAMOS_FILTER_TYPE_ANON);
 	KUNIT_EXPECT_EQ(test, filter->matching, true);
 	KUNIT_EXPECT_PTR_EQ(test, filter->list.prev, &filter->list);
-- 
2.47.3

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

* [PATCH 14/22] mm/damon/tests/core-kunit: handle alloc failure on damos_test_commit_filter()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (12 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 13/22] mm/damon/tests/core-kunit: handle alloc failres in damon_test_new_filter() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 15/22] mm/damon/tests/core-kunit: handle alloc failures on damos_test_filter_out() SeongJae Park
                   ` (7 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 18 . x, Brendan Higgins, David Gow,
	Sang-Heon Jeon, damon, kunit-dev, linux-kernel, linux-kselftest,
	linux-mm

damon_test_commit_filter() is assuming all dynamic memory allocation in
it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: f6a4a150f1ec ("mm/damon/tests/core-kunit: add damos_commit_filter test")
Cc: <stable@vger.kernel.org> # 6.18.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 03c7ac31db5c..5af8275ffd7d 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -516,11 +516,16 @@ static void damos_test_new_filter(struct kunit *test)
 
 static void damos_test_commit_filter(struct kunit *test)
 {
-	struct damos_filter *src_filter = damos_new_filter(
-		DAMOS_FILTER_TYPE_ANON, true, true);
-	struct damos_filter *dst_filter = damos_new_filter(
-		DAMOS_FILTER_TYPE_ACTIVE, false, false);
-
+	struct damos_filter *src_filter, *dst_filter;
+
+	src_filter = damos_new_filter(DAMOS_FILTER_TYPE_ANON, true, true);
+	if (!src_filter)
+		kunit_skip(test, "src filter alloc fail");
+	dst_filter = damos_new_filter(DAMOS_FILTER_TYPE_ACTIVE, false, false);
+	if (!dst_filter) {
+		damos_destroy_filter(src_filter);
+		kunit_skip(test, "dst filter alloc fail");
+	}
 	damos_commit_filter(dst_filter, src_filter);
 	KUNIT_EXPECT_EQ(test, dst_filter->type, src_filter->type);
 	KUNIT_EXPECT_EQ(test, dst_filter->matching, src_filter->matching);
-- 
2.47.3

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

* [PATCH 15/22] mm/damon/tests/core-kunit: handle alloc failures on damos_test_filter_out()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (13 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 14/22] mm/damon/tests/core-kunit: handle alloc failure on damos_test_commit_filter() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 16/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_set_filters_default_reject() SeongJae Park
                   ` (6 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 6 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_filter_out() is assuming all dynamic memory allocation in it
will succeed.  Those are indeed likely in the real use cases since those
allocations are too small to fail, but theoretically those could fail.
In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 26713c890875 ("mm/damon/core-test: add a unit test for __damos_filter_out()")
Cc: <stable@vger.kernel.org> # 6.6.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 5af8275ffd7d..a03ae9ddd88a 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -542,11 +542,22 @@ static void damos_test_filter_out(struct kunit *test)
 	struct damos_filter *f;
 
 	f = damos_new_filter(DAMOS_FILTER_TYPE_ADDR, true, false);
+	if (!f)
+		kunit_skip(test, "filter alloc fail");
 	f->addr_range = (struct damon_addr_range){
 		.start = DAMON_MIN_REGION * 2, .end = DAMON_MIN_REGION * 6};
 
 	t = damon_new_target();
+	if (!t) {
+		damos_destroy_filter(f);
+		kunit_skip(test, "target alloc fail");
+	}
 	r = damon_new_region(DAMON_MIN_REGION * 3, DAMON_MIN_REGION * 5);
+	if (!r) {
+		damos_destroy_filter(f);
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
 	damon_add_region(r, t);
 
 	/* region in the range */
-- 
2.47.3

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

* [PATCH 16/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_set_filters_default_reject()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (14 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 15/22] mm/damon/tests/core-kunit: handle alloc failures on damos_test_filter_out() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 17/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_do_test_apply_three_regions() SeongJae Park
                   ` (5 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 16 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_set_filters_default_reject() is assuming all dynamic memory
allocation in it will succeed.  Those are indeed likely in the real use
cases since those allocations are too small to fail, but theoretically
those could fail.  In the case, inappropriate memory access can happen.
Fix it by appropriately cleanup pre-allocated memory and skip the
execution of the remaining tests in the failure cases.

Fixes: 094fb14913c7 ("mm/damon/tests/core-kunit: add a test for damos_set_filters_default_reject()")
Cc: <stable@vger.kernel.org> # 6.16.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index a03ae9ddd88a..a91d798caa70 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -659,6 +659,8 @@ static void damon_test_set_filters_default_reject(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, scheme.ops_filters_default_reject, false);
 
 	target_filter = damos_new_filter(DAMOS_FILTER_TYPE_TARGET, true, true);
+	if (!target_filter)
+		kunit_skip(test, "filter alloc fail");
 	damos_add_filter(&scheme, target_filter);
 	damos_set_filters_default_reject(&scheme);
 	/*
@@ -684,6 +686,10 @@ static void damon_test_set_filters_default_reject(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, scheme.ops_filters_default_reject, false);
 
 	anon_filter = damos_new_filter(DAMOS_FILTER_TYPE_ANON, true, true);
+	if (!anon_filter) {
+		damos_free_filter(target_filter);
+		kunit_skip(test, "anon_filter alloc fail");
+	}
 	damos_add_filter(&scheme, anon_filter);
 
 	damos_set_filters_default_reject(&scheme);
-- 
2.47.3

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

* [PATCH 17/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_do_test_apply_three_regions()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (15 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 16/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_set_filters_default_reject() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 18/22] mm/damon/tests/vaddr-kunit: handle alloc failures in damon_test_split_evenly_fail() SeongJae Park
                   ` (4 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_do_test_apply_three_regions() is assuming all dynamic memory
allocation in it will succeed.  Those are indeed likely in the real use
cases since those allocations are too small to fail, but theoretically
those could fail.  In the case, inappropriate memory access can happen.
Fix it by appropriately cleanup pre-allocated memory and skip the
execution of the remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/vaddr-kunit.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/damon/tests/vaddr-kunit.h b/mm/damon/tests/vaddr-kunit.h
index fce38dd53cf8..484223f19545 100644
--- a/mm/damon/tests/vaddr-kunit.h
+++ b/mm/damon/tests/vaddr-kunit.h
@@ -136,8 +136,14 @@ static void damon_do_test_apply_three_regions(struct kunit *test,
 	int i;
 
 	t = damon_new_target();
+	if (!t)
+		kunit_skip(test, "target alloc fail");
 	for (i = 0; i < nr_regions / 2; i++) {
 		r = damon_new_region(regions[i * 2], regions[i * 2 + 1]);
+		if (!r) {
+			damon_destroy_target(t, NULL);
+			kunit_skip(test, "region alloc fail");
+		}
 		damon_add_region(r, t);
 	}
 
-- 
2.47.3

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

* [PATCH 18/22] mm/damon/tests/vaddr-kunit: handle alloc failures in damon_test_split_evenly_fail()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (16 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 17/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_do_test_apply_three_regions() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 19/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_test_split_evenly_succ() SeongJae Park
                   ` (3 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_split_evenly_fail() is assuming all dynamic memory allocation
in it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/vaddr-kunit.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/mm/damon/tests/vaddr-kunit.h b/mm/damon/tests/vaddr-kunit.h
index 484223f19545..1b0f21c2e376 100644
--- a/mm/damon/tests/vaddr-kunit.h
+++ b/mm/damon/tests/vaddr-kunit.h
@@ -256,7 +256,16 @@ static void damon_test_split_evenly_fail(struct kunit *test,
 		unsigned long start, unsigned long end, unsigned int nr_pieces)
 {
 	struct damon_target *t = damon_new_target();
-	struct damon_region *r = damon_new_region(start, end);
+	struct damon_region *r;
+
+	if (!t)
+		kunit_skip(test, "target alloc fail");
+
+	r = damon_new_region(start, end);
+	if (!r) {
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
 
 	damon_add_region(r, t);
 	KUNIT_EXPECT_EQ(test,
-- 
2.47.3

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

* [PATCH 19/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_test_split_evenly_succ()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (17 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 18/22] mm/damon/tests/vaddr-kunit: handle alloc failures in damon_test_split_evenly_fail() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 20/22] mm/damon/tests/sysfs-kunit: handle alloc failures on damon_sysfs_test_add_targets() SeongJae Park
                   ` (2 subsequent siblings)
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 5 . 15 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_test_split_evenly_succ() is assuming all dynamic memory allocation
in it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
Cc: <stable@vger.kernel.org> # 5.15.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/vaddr-kunit.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/mm/damon/tests/vaddr-kunit.h b/mm/damon/tests/vaddr-kunit.h
index 1b0f21c2e376..30dc5459f1d2 100644
--- a/mm/damon/tests/vaddr-kunit.h
+++ b/mm/damon/tests/vaddr-kunit.h
@@ -284,10 +284,17 @@ static void damon_test_split_evenly_succ(struct kunit *test,
 	unsigned long start, unsigned long end, unsigned int nr_pieces)
 {
 	struct damon_target *t = damon_new_target();
-	struct damon_region *r = damon_new_region(start, end);
+	struct damon_region *r;
 	unsigned long expected_width = (end - start) / nr_pieces;
 	unsigned long i = 0;
 
+	if (!t)
+		kunit_skip(test, "target alloc fail");
+	r = damon_new_region(start, end);
+	if (!r) {
+		damon_free_target(t);
+		kunit_skip(test, "region alloc fail");
+	}
 	damon_add_region(r, t);
 	KUNIT_EXPECT_EQ(test,
 			damon_va_evenly_split_region(t, r, nr_pieces), 0);
-- 
2.47.3

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

* [PATCH 20/22] mm/damon/tests/sysfs-kunit: handle alloc failures on damon_sysfs_test_add_targets()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (18 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 19/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_test_split_evenly_succ() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 21/22] mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on damon_test_split_at() SeongJae Park
  2025-11-01 18:20 ` [PATCH 22/22] mm/damon/tests/core-kunit: remove unused ctx in damon_test_split_regions_of() SeongJae Park
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, # 6 . 7 . x, Brendan Higgins, David Gow, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

damon_sysfs_test_add_targets() is assuming all dynamic memory allocation
in it will succeed.  Those are indeed likely in the real use cases since
those allocations are too small to fail, but theoretically those could
fail.  In the case, inappropriate memory access can happen.  Fix it by
appropriately cleanup pre-allocated memory and skip the execution of the
remaining tests in the failure cases.

Fixes: b8ee5575f763 ("mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()")
Cc: <stable@vger.kernel.org> # 6.7.x
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/sysfs-kunit.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/mm/damon/tests/sysfs-kunit.h b/mm/damon/tests/sysfs-kunit.h
index 7b5c7b307da9..ce7218469f20 100644
--- a/mm/damon/tests/sysfs-kunit.h
+++ b/mm/damon/tests/sysfs-kunit.h
@@ -45,16 +45,41 @@ static void damon_sysfs_test_add_targets(struct kunit *test)
 	struct damon_ctx *ctx;
 
 	sysfs_targets = damon_sysfs_targets_alloc();
+	if (!sysfs_targets)
+		kunit_skip(test, "sysfs_targets alloc fail");
 	sysfs_targets->nr = 1;
 	sysfs_targets->targets_arr = kmalloc_array(1,
 			sizeof(*sysfs_targets->targets_arr), GFP_KERNEL);
+	if (!sysfs_targets->targets_arr) {
+		kfree(sysfs_targets);
+		kunit_skip(test, "targets_arr alloc fail");
+	}
 
 	sysfs_target = damon_sysfs_target_alloc();
+	if (!sysfs_target) {
+		kfree(sysfs_targets->targets_arr);
+		kfree(sysfs_targets);
+		kunit_skip(test, "sysfs_target alloc fail");
+	}
 	sysfs_target->pid = __damon_sysfs_test_get_any_pid(12, 100);
 	sysfs_target->regions = damon_sysfs_regions_alloc();
+	if (!sysfs_target->regions) {
+		kfree(sysfs_targets->targets_arr);
+		kfree(sysfs_targets);
+		kfree(sysfs_target);
+		kunit_skip(test, "sysfs_regions alloc fail");
+	}
+
 	sysfs_targets->targets_arr[0] = sysfs_target;
 
 	ctx = damon_new_ctx();
+	if (!ctx) {
+		kfree(sysfs_targets->targets_arr);
+		kfree(sysfs_targets);
+		kfree(sysfs_target);
+		kfree(sysfs_target->regions);
+		kunit_skip(test, "ctx alloc fail");
+	}
 
 	damon_sysfs_add_targets(ctx, sysfs_targets);
 	KUNIT_EXPECT_EQ(test, 1u, nr_damon_targets(ctx));
-- 
2.47.3

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

* [PATCH 21/22] mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on damon_test_split_at()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (19 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 20/22] mm/damon/tests/sysfs-kunit: handle alloc failures on damon_sysfs_test_add_targets() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  2025-11-01 18:20 ` [PATCH 22/22] mm/damon/tests/core-kunit: remove unused ctx in damon_test_split_regions_of() SeongJae Park
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, Brendan Higgins, David Gow, damon, kunit-dev,
	linux-kernel, linux-kselftest, linux-mm

damon_test_split_at() dynamically allocates a 'struct damon_ctx' object,
but it is not really being used in the code other than handling the
allocation failure and deallocating it at the end of the function.
Remove the unnecessary allocation and deallocation of the object.

Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index a91d798caa70..726f593930e7 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -144,20 +144,14 @@ static void damon_test_aggregate(struct kunit *test)
 
 static void damon_test_split_at(struct kunit *test)
 {
-	struct damon_ctx *c = damon_new_ctx();
 	struct damon_target *t;
 	struct damon_region *r, *r_new;
 
-	if (!c)
-		kunit_skip(test, "ctx alloc fail");
 	t = damon_new_target();
-	if (!t) {
-		damon_destroy_ctx(c);
+	if (!t)
 		kunit_skip(test, "target alloc fail");
-	}
 	r = damon_new_region(0, 100);
 	if (!r) {
-		damon_destroy_ctx(c);
 		damon_free_target(t);
 		kunit_skip(test, "region alloc fail");
 	}
@@ -178,7 +172,6 @@ static void damon_test_split_at(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, r->last_nr_accesses, r_new->last_nr_accesses);
 
 	damon_free_target(t);
-	damon_destroy_ctx(c);
 }
 
 static void damon_test_merge_two(struct kunit *test)
-- 
2.47.3

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

* [PATCH 22/22] mm/damon/tests/core-kunit: remove unused ctx in damon_test_split_regions_of()
  2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
                   ` (20 preceding siblings ...)
  2025-11-01 18:20 ` [PATCH 21/22] mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on damon_test_split_at() SeongJae Park
@ 2025-11-01 18:20 ` SeongJae Park
  21 siblings, 0 replies; 23+ messages in thread
From: SeongJae Park @ 2025-11-01 18:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, Brendan Higgins, David Gow, damon, kunit-dev,
	linux-kernel, linux-kselftest, linux-mm

damon_test_split_regions_of() dynamically allocates a 'struct damon_ctx'
object, but it is not really being used in the code other than handling
the allocation failure and deallocating it at the end of the function.
Remove the unnecessary allocation and deallocation of the object.

Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/tests/core-kunit.h | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 726f593930e7..96a4cd489b39 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -267,20 +267,14 @@ static void damon_test_merge_regions_of(struct kunit *test)
 
 static void damon_test_split_regions_of(struct kunit *test)
 {
-	struct damon_ctx *c = damon_new_ctx();
 	struct damon_target *t;
 	struct damon_region *r;
 
-	if (!c)
-		kunit_skip("ctx alloc fail");
 	t = damon_new_target();
-	if (!t) {
-		damon_destroy_ctx(c);
+	if (!t)
 		kunit_skip(test, "target alloc fail");
-	}
 	r = damon_new_region(0, 22);
 	if (!r) {
-		damon_destroy_ctx(c);
 		damon_free_target(t);
 		kunit_skip(test, "region alloc fail");
 	}
@@ -290,13 +284,10 @@ static void damon_test_split_regions_of(struct kunit *test)
 	damon_free_target(t);
 
 	t = damon_new_target();
-	if (!t) {
-		damon_destroy_ctx(c);
+	if (!t)
 		kunit_skip(test, "second target alloc fail");
-	}
 	r = damon_new_region(0, 220);
 	if (!r) {
-		damon_destroy_ctx(c);
 		damon_free_target(t);
 		kunit_skip(test, "second region alloc fail");
 	}
@@ -304,7 +295,6 @@ static void damon_test_split_regions_of(struct kunit *test)
 	damon_split_regions_of(t, 4, DAMON_MIN_REGION);
 	KUNIT_EXPECT_LE(test, damon_nr_regions(t), 4u);
 	damon_free_target(t);
-	damon_destroy_ctx(c);
 }
 
 static void damon_test_ops_registration(struct kunit *test)
-- 
2.47.3

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

end of thread, other threads:[~2025-11-01 18:20 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-01 18:19 [PATCH 00/22] mm/damon/tests: fix memory bugs in kunit tests SeongJae Park
2025-11-01 18:19 ` [PATCH 01/22] mm/damon/tests/core-kunit: fix memory leak in damon_test_set_filters_default_reject() SeongJae Park
2025-11-01 18:19 ` [PATCH 02/22] mm/damon/tests/core-kunit: handle allocation failures in damon_test_regions() SeongJae Park
2025-11-01 18:19 ` [PATCH 03/22] mm/damon/tests/core-kunit: handle memory failure from damon_test_target() SeongJae Park
2025-11-01 18:19 ` [PATCH 04/22] mm/damon/tests/core-kunit: handle memory alloc failure from damon_test_aggregate() SeongJae Park
2025-11-01 18:19 ` [PATCH 05/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_at() SeongJae Park
2025-11-01 18:20 ` [PATCH 06/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_merge_two() SeongJae Park
2025-11-01 18:20 ` [PATCH 07/22] mm/damon/tests/core-kunit: handle alloc failures on dasmon_test_merge_regions_of() SeongJae Park
2025-11-01 18:20 ` [PATCH 08/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_split_regions_of() SeongJae Park
2025-11-01 18:20 ` [PATCH 09/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_ops_registration() SeongJae Park
2025-11-01 18:20 ` [PATCH 10/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_set_regions() SeongJae Park
2025-11-01 18:20 ` [PATCH 11/22] mm/damon/tests/core-kunit: handle alloc failures in damon_test_update_monitoring_result() SeongJae Park
2025-11-01 18:20 ` [PATCH 12/22] mm/damon/tests/core-kunit: handle alloc failure on damon_test_set_attrs() SeongJae Park
2025-11-01 18:20 ` [PATCH 13/22] mm/damon/tests/core-kunit: handle alloc failres in damon_test_new_filter() SeongJae Park
2025-11-01 18:20 ` [PATCH 14/22] mm/damon/tests/core-kunit: handle alloc failure on damos_test_commit_filter() SeongJae Park
2025-11-01 18:20 ` [PATCH 15/22] mm/damon/tests/core-kunit: handle alloc failures on damos_test_filter_out() SeongJae Park
2025-11-01 18:20 ` [PATCH 16/22] mm/damon/tests/core-kunit: handle alloc failures on damon_test_set_filters_default_reject() SeongJae Park
2025-11-01 18:20 ` [PATCH 17/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_do_test_apply_three_regions() SeongJae Park
2025-11-01 18:20 ` [PATCH 18/22] mm/damon/tests/vaddr-kunit: handle alloc failures in damon_test_split_evenly_fail() SeongJae Park
2025-11-01 18:20 ` [PATCH 19/22] mm/damon/tests/vaddr-kunit: handle alloc failures on damon_test_split_evenly_succ() SeongJae Park
2025-11-01 18:20 ` [PATCH 20/22] mm/damon/tests/sysfs-kunit: handle alloc failures on damon_sysfs_test_add_targets() SeongJae Park
2025-11-01 18:20 ` [PATCH 21/22] mm/damon/tests/core-kunit: remove unnecessary damon_ctx variable on damon_test_split_at() SeongJae Park
2025-11-01 18:20 ` [PATCH 22/22] mm/damon/tests/core-kunit: remove unused ctx in damon_test_split_regions_of() SeongJae Park

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).