Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] mm/damon/core: filter out damos address filter-split failed region
@ 2026-10-18 22:02 SJ Park
  2026-10-18 22:11 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: SJ Park @ 2026-10-18 22:02 UTC (permalink / raw)
  Cc: SJ Park, stable, Andrew Morton, damon, linux-kernel, linux-mm

When an address type DAMOS filter is configured, DAMOS tries to split
the region for the filter range, and apply DAMOS action to the filter-in
region.  The split operation (damon_split_region_at()) could fail from
allocation failure under extreme memory pressure.  In the case, DAMOS
action could be applied more or less than expected, ignoring the address
filter.

The issue only rarely happens under extreme memory pressure.  No crash
or leak happens.  Occasionally not applying the action to a few regions
shouldn't be a real problem.  But applying the action to a region that
the user has excluded via address filter could be a surprise depending
on the use case.  It is only theoretical.  No such real case is found
yet.

Filter out the entire region if the split is failed.  Update
damos_filter_match() signature to receive the error from split.  Also
update kunit tests for the updated signature.

The issue was discovered [1] by sashiko.

[1] https://lore.kernel.org/20260801203121.117449-1-sj@kernel.org

Fixes: ab9bda001b68 ("mm/damon/core: introduce address range type damos filter")
Cc: <stable@vger.kernel.org> # 6.6.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/core.c             | 15 ++++++++++++---
 mm/damon/tests/core-kunit.h | 11 ++++++-----
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/mm/damon/core.c b/mm/damon/core.c
index b63e60ef89901..4d34587387456 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2551,13 +2551,14 @@ static void damos_update_stat(struct damos *s,
 
 static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
 		struct damon_region *r, struct damos_filter *filter,
-		unsigned long min_region_sz)
+		unsigned long min_region_sz, int *err)
 {
 	bool matched = false;
 	struct damon_target *ti;
 	int target_idx = 0;
 	unsigned long start, end, wsum;
 
+	*err = 0;
 	switch (filter->type) {
 	case DAMOS_FILTER_TYPE_TARGET:
 		damon_for_each_target(ti, ctx) {
@@ -2588,7 +2589,9 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
 			break;
 		}
 		/* start inside the range */
-		damon_split_region_at(t, r, end - r->ar.start);
+		*err = damon_split_region_at(t, r, end - r->ar.start);
+		if (*err)
+			return false;
 		matched = true;
 		break;
 	case DAMOS_FILTER_TYPE_PROBE_HITS_WSUM:
@@ -2607,10 +2610,16 @@ static bool damos_core_filter_out(struct damon_ctx *ctx, struct damon_target *t,
 		struct damon_region *r, struct damos *s)
 {
 	struct damos_filter *filter;
+	bool matching;
+	int err;
 
 	s->core_filters_allowed = false;
 	damos_for_each_core_filter(filter, s) {
-		if (damos_filter_match(ctx, t, r, filter, ctx->min_region_sz)) {
+		matching = damos_filter_match(ctx, t, r, filter,
+				ctx->min_region_sz, &err);
+		if (err)
+			return true;
+		if (matching) {
 			if (filter->allow)
 				s->core_filters_allowed = true;
 			return !filter->allow;
diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 3fbb4e4e36fac..43e2f950f8ff4 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -1783,6 +1783,7 @@ static void damos_test_filter_out(struct kunit *test)
 	struct damon_target *t;
 	struct damon_region *r, *r2;
 	struct damos_filter *f;
+	int err;
 
 	f = damos_new_filter(DAMOS_FILTER_TYPE_ADDR, true, false);
 	if (!f)
@@ -1803,27 +1804,27 @@ static void damos_test_filter_out(struct kunit *test)
 	damon_add_region(r, t);
 
 	/* region in the range */
-	KUNIT_EXPECT_TRUE(test, damos_filter_match(NULL, t, r, f, 1));
+	KUNIT_EXPECT_TRUE(test, damos_filter_match(NULL, t, r, f, 1, &err));
 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1);
 
 	/* region before the range */
 	r->ar.start = 1;
 	r->ar.end = 2;
 	KUNIT_EXPECT_FALSE(test,
-			damos_filter_match(NULL, t, r, f, 1));
+			damos_filter_match(NULL, t, r, f, 1, &err));
 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1);
 
 	/* region after the range */
 	r->ar.start = 6;
 	r->ar.end = 8;
 	KUNIT_EXPECT_FALSE(test,
-			damos_filter_match(NULL, t, r, f, 1));
+			damos_filter_match(NULL, t, r, f, 1, &err));
 	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 1);
 
 	/* region started before the range */
 	r->ar.start = 1;
 	r->ar.end = 4;
-	KUNIT_EXPECT_FALSE(test, damos_filter_match(NULL, t, r, f, 1));
+	KUNIT_EXPECT_FALSE(test, damos_filter_match(NULL, t, r, f, 1, &err));
 	/* filter should have split the region */
 	KUNIT_EXPECT_EQ(test, r->ar.start, 1);
 	KUNIT_EXPECT_EQ(test, r->ar.end, 2);
@@ -1839,7 +1840,7 @@ static void damos_test_filter_out(struct kunit *test)
 	r->ar.start = 2;
 	r->ar.end = 8;
 	KUNIT_EXPECT_TRUE(test,
-			damos_filter_match(NULL, t, r, f, 1));
+			damos_filter_match(NULL, t, r, f, 1, &err));
 	/* filter should have split the region */
 	KUNIT_EXPECT_EQ(test, r->ar.start, 2);
 	KUNIT_EXPECT_EQ(test, r->ar.end, 6);

base-commit: dbff06b9dcfe3a45b66fcf20d0fec8c3e56f5624
-- 
2.47.3


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

* Re: [RFC PATCH] mm/damon/core: filter out damos address filter-split failed region
  2026-10-18 22:02 [RFC PATCH] mm/damon/core: filter out damos address filter-split failed region SJ Park
@ 2026-10-18 22:11 ` sashiko-bot
  2026-10-18 22:13   ` SJ Park
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-10-18 22:11 UTC (permalink / raw)
  To: SJ Park; +Cc: Andrew Morton, damon, linux-mm

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm/damon: unchecked damon_split_region_at() error in damos_filter_match()
--

commit 881e287348e01011e3ec5442e7deb6f9c112db9c
Author: SJ Park <sj@kernel.org>

mm/damon/core: filter out damos address filter-split failed region

This commit updates damos_filter_match() and damos_core_filter_out() to
propagate errors from damon_split_region_at() and filter out the region if
splitting fails during DAMOS_FILTER_TYPE_ADDR evaluation.

> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index b63e60ef89901..4d34587387456 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -2551,13 +2551,14 @@ static void damos_update_stat(struct damos *s,
>  
>  static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
>  		struct damon_region *r, struct damos_filter *filter,
> -		unsigned long min_region_sz)
> +		unsigned long min_region_sz, int *err)
>  {
>  	bool matched = false;
>  	struct damon_target *ti;
>  	int target_idx = 0;
>  	unsigned long start, end, wsum;
>  
> +	*err = 0;
>  	switch (filter->type) {
>  	case DAMOS_FILTER_TYPE_TARGET:
>  		damon_for_each_target(ti, ctx) {
> @@ -2588,7 +2589,9 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
>  			break;
>  		}
>  		/* start inside the range */
> -		damon_split_region_at(t, r, end - r->ar.start);
> +		*err = damon_split_region_at(t, r, end - r->ar.start);
> +		if (*err)
> +			return false;
>  		matched = true;
>  		break;

[Severity: Medium]
Should the earlier damon_split_region_at() call in damos_filter_match()
when r->ar.start < start also propagate its return value via *err?

mm/damon/core.c:damos_filter_match() {
	case DAMOS_FILTER_TYPE_ADDR:
		...
		/* start before the range and overlap */
		if (r->ar.start < start) {
			damon_split_region_at(t, r, start - r->ar.start);
			matched = false;
			break;
		}
}

When a region starts before the filter range and overlaps it (r->ar.start <
start and r->ar.end > start), if damon_split_region_at(t, r, start -
r->ar.start) fails with -ENOMEM, r remains unsplit across [r->ar.start,
r->ar.end) while matched is set to false and *err remains 0.

If the filter is configured to exclude [start, end) (for example,
filter->matching == true and filter->allow == false with
s->core_filters_default_reject == false, or filter->matching == false and
filter->allow == true), kdamond_apply_schemes() -> damos_apply_scheme() ->
damos_core_filter_out() sees err == 0, does not reject r, and proceeds to
call c->ops.apply_scheme() on the entire unsplit region including the
excluded address range.

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261018220253.158238-1-sj@kernel.org?part=1


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

* Re: [RFC PATCH] mm/damon/core: filter out damos address filter-split failed region
  2026-10-18 22:11 ` sashiko-bot
@ 2026-10-18 22:13   ` SJ Park
  0 siblings, 0 replies; 3+ messages in thread
From: SJ Park @ 2026-10-18 22:13 UTC (permalink / raw)
  To: sashiko-bot; +Cc: SJ Park, Andrew Morton, damon, linux-mm

On Sun, 18 Oct 2026 22:11:06 +0000 sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] mm/damon: unchecked damon_split_region_at() error in damos_filter_match()
> --
> 
> commit 881e287348e01011e3ec5442e7deb6f9c112db9c
> Author: SJ Park <sj@kernel.org>
> 
> mm/damon/core: filter out damos address filter-split failed region
> 
> This commit updates damos_filter_match() and damos_core_filter_out() to
> propagate errors from damon_split_region_at() and filter out the region if
> splitting fails during DAMOS_FILTER_TYPE_ADDR evaluation.
> 
> > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > index b63e60ef89901..4d34587387456 100644
> > --- a/mm/damon/core.c
> > +++ b/mm/damon/core.c
> > @@ -2551,13 +2551,14 @@ static void damos_update_stat(struct damos *s,
> >  
> >  static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
> >  		struct damon_region *r, struct damos_filter *filter,
> > -		unsigned long min_region_sz)
> > +		unsigned long min_region_sz, int *err)
> >  {
> >  	bool matched = false;
> >  	struct damon_target *ti;
> >  	int target_idx = 0;
> >  	unsigned long start, end, wsum;
> >  
> > +	*err = 0;
> >  	switch (filter->type) {
> >  	case DAMOS_FILTER_TYPE_TARGET:
> >  		damon_for_each_target(ti, ctx) {
> > @@ -2588,7 +2589,9 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t,
> >  			break;
> >  		}
> >  		/* start inside the range */
> > -		damon_split_region_at(t, r, end - r->ar.start);
> > +		*err = damon_split_region_at(t, r, end - r->ar.start);
> > +		if (*err)
> > +			return false;
> >  		matched = true;
> >  		break;
> 
> [Severity: Medium]
> Should the earlier damon_split_region_at() call in damos_filter_match()
> when r->ar.start < start also propagate its return value via *err?
> 
> mm/damon/core.c:damos_filter_match() {
> 	case DAMOS_FILTER_TYPE_ADDR:
> 		...
> 		/* start before the range and overlap */
> 		if (r->ar.start < start) {
> 			damon_split_region_at(t, r, start - r->ar.start);
> 			matched = false;
> 			break;
> 		}
> }
> 
> When a region starts before the filter range and overlaps it (r->ar.start <
> start and r->ar.end > start), if damon_split_region_at(t, r, start -
> r->ar.start) fails with -ENOMEM, r remains unsplit across [r->ar.start,
> r->ar.end) while matched is set to false and *err remains 0.
> 
> If the filter is configured to exclude [start, end) (for example,
> filter->matching == true and filter->allow == false with
> s->core_filters_default_reject == false, or filter->matching == false and
> filter->allow == true), kdamond_apply_schemes() -> damos_apply_scheme() ->
> damos_core_filter_out() sees err == 0, does not reject r, and proceeds to
> call c->ops.apply_scheme() on the entire unsplit region including the
> excluded address range.

Good catch.  I will fix this in the next revision.

> 
> [ ... ]
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261018220253.158238-1-sj@kernel.org?part=1


Thanks,
SJ


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

end of thread, other threads:[~2026-10-18 22:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-18 22:02 [RFC PATCH] mm/damon/core: filter out damos address filter-split failed region SJ Park
2026-10-18 22:11 ` sashiko-bot
2026-10-18 22:13   ` SJ Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox