linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* question on memcg_stats_[un]lock in memcg code
@ 2025-03-14  1:11 Shakeel Butt
  2025-03-14  1:46 ` Shakeel Butt
  0 siblings, 1 reply; 3+ messages in thread
From: Shakeel Butt @ 2025-03-14  1:11 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior; +Cc: linux-mm

Hi Sebastian,

The commit be3e67b54b43 ("mm/memcg: protect per-CPU counter by disabling
preemption on PREEMPT_RT where needed.") added memcg_stats_lock() and
other variants in memcontrol.c. There usage were added in
__mod_memcg_lruvec_state, __count_memcg_events and mem_cgroup_swapout. I
am wondering why __mod_memcg_state was excluded?

thanks,
Shakeel


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

* Re: question on memcg_stats_[un]lock in memcg code
  2025-03-14  1:11 question on memcg_stats_[un]lock in memcg code Shakeel Butt
@ 2025-03-14  1:46 ` Shakeel Butt
  2025-03-14 10:12   ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 3+ messages in thread
From: Shakeel Butt @ 2025-03-14  1:46 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior; +Cc: linux-mm

On Thu, Mar 13, 2025 at 06:11:30PM -0700, Shakeel Butt wrote:
> Hi Sebastian,
> 
> The commit be3e67b54b43 ("mm/memcg: protect per-CPU counter by disabling
> preemption on PREEMPT_RT where needed.") added memcg_stats_lock() and
> other variants in memcontrol.c. There usage were added in
> __mod_memcg_lruvec_state, __count_memcg_events and mem_cgroup_swapout. I
> am wondering why __mod_memcg_state was excluded?
> 

Basically I wanted to know if the reason was that there were no direct
caller for __mod_memcg_state and something like the following patch is
fine with new direct caller:

From 6506b9463e2a96d1e9681dcaf6dece75e7050513 Mon Sep 17 00:00:00 2001
From: Shakeel Butt <shakeel.butt@linux.dev>
Date: Thu, 13 Mar 2025 18:27:41 -0700
Subject: [PATCH] memcg: use __mod_memcg_state in drain_obj_stock

For non-PREEMPT_RT kernels, drain_obj_stock() is always called with irq
disabled, so we can use __mod_memcg_state() instead of
mod_memcg_state(). For PREEMPT_RT, we need to add memcg_stats_[un]lock
in __mod_memcg_state().

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 mm/memcontrol.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 4de6acb9b8ec..8ddb9c94db7d 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -700,10 +700,12 @@ void __mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx,
 	if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx))
 		return;
 
+	memcg_stats_lock();
 	__this_cpu_add(memcg->vmstats_percpu->state[i], val);
 	val = memcg_state_val_in_pages(idx, val);
 	memcg_rstat_updated(memcg, val);
 	trace_mod_memcg_state(memcg, idx, val);
+	memcg_stats_unlock();
 }
 
 /* idx can be of type enum memcg_stat_item or node_stat_item. */
@@ -2791,7 +2793,7 @@ static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
 
 			memcg = get_mem_cgroup_from_objcg(old);
 
-			mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages);
+			__mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages);
 			memcg1_account_kmem(memcg, -nr_pages);
 			__refill_stock(memcg, nr_pages);
 
-- 
2.47.1



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

* Re: question on memcg_stats_[un]lock in memcg code
  2025-03-14  1:46 ` Shakeel Butt
@ 2025-03-14 10:12   ` Sebastian Andrzej Siewior
  0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Andrzej Siewior @ 2025-03-14 10:12 UTC (permalink / raw)
  To: Shakeel Butt; +Cc: linux-mm

On 2025-03-13 18:46:53 [-0700], Shakeel Butt wrote:
> On Thu, Mar 13, 2025 at 06:11:30PM -0700, Shakeel Butt wrote:
> > Hi Sebastian,
> > 
> > The commit be3e67b54b43 ("mm/memcg: protect per-CPU counter by disabling
> > preemption on PREEMPT_RT where needed.") added memcg_stats_lock() and
> > other variants in memcontrol.c. There usage were added in
> > __mod_memcg_lruvec_state, __count_memcg_events and mem_cgroup_swapout. I
> > am wondering why __mod_memcg_state was excluded?
> > 
> 
> Basically I wanted to know if the reason was that there were no direct
> caller for __mod_memcg_state and something like the following patch is

I guess it was omitted because the only caller is mod_memcg_state()
which disables interrupts prior calling. So this is enough.

Now if you add a user then you should add the locking like in the
suggested patch.

Sebastian


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

end of thread, other threads:[~2025-03-14 10:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-14  1:11 question on memcg_stats_[un]lock in memcg code Shakeel Butt
2025-03-14  1:46 ` Shakeel Butt
2025-03-14 10:12   ` Sebastian Andrzej Siewior

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