linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] memcg: damon: get memcg reference before access
@ 2025-12-24  3:45 Shakeel Butt
  2025-12-24  5:21 ` SeongJae Park
  0 siblings, 1 reply; 4+ messages in thread
From: Shakeel Butt @ 2025-12-24  3:45 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, damon, linux-mm, cgroups, linux-kernel, Meta kernel team

The commit b74a120bcf507 ("mm/damon/core: implement
DAMOS_QUOTA_NODE_MEMCG_USED_BP") added accesses to memcg structure
without getting reference to it. This is unsafe. Let's get the reference
before accessing the memcg.

Fixes: b74a120bcf507 ("mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_USED_BP")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 mm/damon/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/mm/damon/core.c b/mm/damon/core.c
index 4ad5f290d382..89982e0229f0 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2051,13 +2051,15 @@ static unsigned long damos_get_node_memcg_used_bp(
 
 	rcu_read_lock();
 	memcg = mem_cgroup_from_id(goal->memcg_id);
-	rcu_read_unlock();
-	if (!memcg) {
+	if (!memcg || !mem_cgroup_tryget(memcg)) {
+		rcu_read_unlock();
 		if (goal->metric == DAMOS_QUOTA_NODE_MEMCG_USED_BP)
 			return 0;
 		else	/* DAMOS_QUOTA_NODE_MEMCG_FREE_BP */
 			return 10000;
 	}
+	rcu_read_unlock();
+
 	mem_cgroup_flush_stats(memcg);
 	lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(goal->nid));
 	used_pages = lruvec_page_state(lruvec, NR_ACTIVE_ANON);
@@ -2065,6 +2067,8 @@ static unsigned long damos_get_node_memcg_used_bp(
 	used_pages += lruvec_page_state(lruvec, NR_ACTIVE_FILE);
 	used_pages += lruvec_page_state(lruvec, NR_INACTIVE_FILE);
 
+	mem_cgroup_put(memcg);
+
 	si_meminfo_node(&i, goal->nid);
 	if (goal->metric == DAMOS_QUOTA_NODE_MEMCG_USED_BP)
 		numerator = used_pages;
-- 
2.47.3



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

* Re: [PATCH] memcg: damon: get memcg reference before access
  2025-12-24  3:45 [PATCH] memcg: damon: get memcg reference before access Shakeel Butt
@ 2025-12-24  5:21 ` SeongJae Park
  2025-12-24  7:51   ` Shakeel Butt
  0 siblings, 1 reply; 4+ messages in thread
From: SeongJae Park @ 2025-12-24  5:21 UTC (permalink / raw)
  To: Shakeel Butt
  Cc: SeongJae Park, Andrew Morton, damon, linux-mm, cgroups,
	linux-kernel, Meta kernel team

On Tue, 23 Dec 2025 19:45:27 -0800 Shakeel Butt <shakeel.butt@linux.dev> wrote:

> The commit b74a120bcf507 ("mm/damon/core: implement
> DAMOS_QUOTA_NODE_MEMCG_USED_BP") added accesses to memcg structure
> without getting reference to it. This is unsafe. Let's get the reference
> before accessing the memcg.

Thank you for catching and fixing this!

Nit.  On the subject, could we use 'mm/damon/core:' prefix instead of 'memcg:
damon:' for keeping the file's commit log subjects consistent?

> 
> Fixes: b74a120bcf507 ("mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_USED_BP")

I was firsty thinking we might need to Cc: stable@.  But I realized the broken
commit has merged into 6.19-rc1.  So Cc: stable@ is not needed.

> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>

Other than the tirivial subject prefix inconsistency, looks good to me.

Reviewed-by: SeongJae Park <sj@kernel.org>

> ---
>  mm/damon/core.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 4ad5f290d382..89982e0229f0 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -2051,13 +2051,15 @@ static unsigned long damos_get_node_memcg_used_bp(
>  
>  	rcu_read_lock();
>  	memcg = mem_cgroup_from_id(goal->memcg_id);
> -	rcu_read_unlock();
> -	if (!memcg) {
> +	if (!memcg || !mem_cgroup_tryget(memcg)) {

For this part, I was thinking '!memcg' part seems not technically needed
because mem_cgroup_tryget() does the check.  But I think that's just trivial,
so this also looks good to me.

> +		rcu_read_unlock();
>  		if (goal->metric == DAMOS_QUOTA_NODE_MEMCG_USED_BP)
>  			return 0;
>  		else	/* DAMOS_QUOTA_NODE_MEMCG_FREE_BP */
>  			return 10000;
>  	}
> +	rcu_read_unlock();
> +
>  	mem_cgroup_flush_stats(memcg);
>  	lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(goal->nid));
>  	used_pages = lruvec_page_state(lruvec, NR_ACTIVE_ANON);
> @@ -2065,6 +2067,8 @@ static unsigned long damos_get_node_memcg_used_bp(
>  	used_pages += lruvec_page_state(lruvec, NR_ACTIVE_FILE);
>  	used_pages += lruvec_page_state(lruvec, NR_INACTIVE_FILE);
>  
> +	mem_cgroup_put(memcg);
> +
>  	si_meminfo_node(&i, goal->nid);
>  	if (goal->metric == DAMOS_QUOTA_NODE_MEMCG_USED_BP)
>  		numerator = used_pages;
> -- 
> 2.47.3


Thanks,
SJ

[...]


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

* Re: [PATCH] memcg: damon: get memcg reference before access
  2025-12-24  5:21 ` SeongJae Park
@ 2025-12-24  7:51   ` Shakeel Butt
  2025-12-24 15:37     ` SeongJae Park
  0 siblings, 1 reply; 4+ messages in thread
From: Shakeel Butt @ 2025-12-24  7:51 UTC (permalink / raw)
  To: SeongJae Park
  Cc: Andrew Morton, damon, linux-mm, cgroups, linux-kernel, Meta kernel team

On Tue, Dec 23, 2025 at 09:21:47PM -0800, SeongJae Park wrote:
> On Tue, 23 Dec 2025 19:45:27 -0800 Shakeel Butt <shakeel.butt@linux.dev> wrote:
> 
> > The commit b74a120bcf507 ("mm/damon/core: implement
> > DAMOS_QUOTA_NODE_MEMCG_USED_BP") added accesses to memcg structure
> > without getting reference to it. This is unsafe. Let's get the reference
> > before accessing the memcg.
> 
> Thank you for catching and fixing this!
> 
> Nit.  On the subject, could we use 'mm/damon/core:' prefix instead of 'memcg:
> damon:' for keeping the file's commit log subjects consistent?

Done.

> 
> > 
> > Fixes: b74a120bcf507 ("mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_USED_BP")
> 
> I was firsty thinking we might need to Cc: stable@.  But I realized the broken
> commit has merged into 6.19-rc1.  So Cc: stable@ is not needed.
> 
> > Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
> 
> Other than the tirivial subject prefix inconsistency, looks good to me.
> 
> Reviewed-by: SeongJae Park <sj@kernel.org>
>

Thanks.

> > ---
> >  mm/damon/core.c | 8 ++++++--
> >  1 file changed, 6 insertions(+), 2 deletions(-)
> > 
> > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > index 4ad5f290d382..89982e0229f0 100644
> > --- a/mm/damon/core.c
> > +++ b/mm/damon/core.c
> > @@ -2051,13 +2051,15 @@ static unsigned long damos_get_node_memcg_used_bp(
> >  
> >  	rcu_read_lock();
> >  	memcg = mem_cgroup_from_id(goal->memcg_id);
> > -	rcu_read_unlock();
> > -	if (!memcg) {
> > +	if (!memcg || !mem_cgroup_tryget(memcg)) {
> 
> For this part, I was thinking '!memcg' part seems not technically needed
> because mem_cgroup_tryget() does the check.  But I think that's just trivial,
> so this also looks good to me.
> 

Hmm !memcg check inside mem_cgroup_tryget() is a weird one. It makes
mem_cgroup_tryget() to return true for NULL parameter. We can not use
mem_cgroup_tryget() as is alone here.


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

* Re: [PATCH] memcg: damon: get memcg reference before access
  2025-12-24  7:51   ` Shakeel Butt
@ 2025-12-24 15:37     ` SeongJae Park
  0 siblings, 0 replies; 4+ messages in thread
From: SeongJae Park @ 2025-12-24 15:37 UTC (permalink / raw)
  To: Shakeel Butt
  Cc: SeongJae Park, Andrew Morton, damon, linux-mm, cgroups,
	linux-kernel, Meta kernel team

On Tue, 23 Dec 2025 23:51:55 -0800 Shakeel Butt <shakeel.butt@linux.dev> wrote:

> On Tue, Dec 23, 2025 at 09:21:47PM -0800, SeongJae Park wrote:
> > On Tue, 23 Dec 2025 19:45:27 -0800 Shakeel Butt <shakeel.butt@linux.dev> wrote:
[...]
> > > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > > index 4ad5f290d382..89982e0229f0 100644
> > > --- a/mm/damon/core.c
> > > +++ b/mm/damon/core.c
> > > @@ -2051,13 +2051,15 @@ static unsigned long damos_get_node_memcg_used_bp(
> > >  
> > >  	rcu_read_lock();
> > >  	memcg = mem_cgroup_from_id(goal->memcg_id);
> > > -	rcu_read_unlock();
> > > -	if (!memcg) {
> > > +	if (!memcg || !mem_cgroup_tryget(memcg)) {
> > 
> > For this part, I was thinking '!memcg' part seems not technically needed
> > because mem_cgroup_tryget() does the check.  But I think that's just trivial,
> > so this also looks good to me.
> > 
> 
> Hmm !memcg check inside mem_cgroup_tryget() is a weird one. It makes
> mem_cgroup_tryget() to return true for NULL parameter. We can not use
> mem_cgroup_tryget() as is alone here.

Oh, you're right.  I should have read it more carefully.  Thank you for
enlightening me :)


Thanks,
SJ

[...]


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

end of thread, other threads:[~2025-12-24 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-24  3:45 [PATCH] memcg: damon: get memcg reference before access Shakeel Butt
2025-12-24  5:21 ` SeongJae Park
2025-12-24  7:51   ` Shakeel Butt
2025-12-24 15:37     ` SeongJae Park

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