[PATCH] memcg: multi-memcg percpu charge cache - fix 2
From: Shakeel Butt
Date: Fri Apr 25 2025 - 16:10:43 EST
Simplify refill_stock by avoiding goto and doing the operations inline
and make sure the given memcg is not cached multiple times.
Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
mm/memcontrol.c | 27 +++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 997e2da5d2ca..9dfdbb2fcccc 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1907,7 +1907,8 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
struct mem_cgroup *cached;
uint8_t stock_pages;
unsigned long flags;
- bool evict = true;
+ bool success = false;
+ int empty_slot = -1;
int i;
/*
@@ -1931,26 +1932,28 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
stock = this_cpu_ptr(&memcg_stock);
for (i = 0; i < NR_MEMCG_STOCK; ++i) {
-again:
cached = READ_ONCE(stock->cached[i]);
- if (!cached) {
- css_get(&memcg->css);
- WRITE_ONCE(stock->cached[i], memcg);
- }
- if (!cached || memcg == READ_ONCE(stock->cached[i])) {
+ if (!cached && empty_slot == -1)
+ empty_slot = i;
+ if (memcg == READ_ONCE(stock->cached[i])) {
stock_pages = READ_ONCE(stock->nr_pages[i]) + nr_pages;
WRITE_ONCE(stock->nr_pages[i], stock_pages);
if (stock_pages > MEMCG_CHARGE_BATCH)
drain_stock(stock, i);
- evict = false;
+ success = true;
break;
}
}
- if (evict) {
- i = get_random_u32_below(NR_MEMCG_STOCK);
- drain_stock(stock, i);
- goto again;
+ if (!success) {
+ i = empty_slot;
+ if (i == -1) {
+ i = get_random_u32_below(NR_MEMCG_STOCK);
+ drain_stock(stock, i);
+ }
+ css_get(&memcg->css);
+ WRITE_ONCE(stock->cached[i], memcg);
+ WRITE_ONCE(stock->nr_pages[i], stock_pages);
}
local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
--
2.47.1