Skip to contentSkip to conversation

memcontrol: Wait for draining of remote stocks to avoid OOM when charging

RFC PATCH In review Michal Koutný 2 participants 3 messages May 30, 2025 – Jun 12, 2025

Subsystems (3)
lore
Share & tools
lore

Michal Koutný

author maintainer
#0 lore raw

RFC PATCH 1/1 memcontrol: Wait for draining of remote stocks to avoid OOM when charging

The LTP memcontrol03.c checks behavior of memory.min protection under relatively tight conditions -- there is 2MiB margin for allocating task below test's memory.max.

MEMCG_CHARGE_BATCH might be over-charged to page_counters temporarily but this alone should not lead to OOM because this overcharged amount is retrieved by draining stock. Or is it?

I suspect this may cause troubles when there is >MEMCG_CHARGE_BATCH charge preceded by a small charge:

  try_charge_memcg(memcg, ..., 1);
    // counter->usage += 64
    // local stock = 63
    // no OOM but counter->usage > counter->max
  // running on different CPU
  try_charge_memcg(memcg, ..., 65);
    // 4M in stock + 148M new charge, only 150M w/out hard protection to reclaim
    try_to_free_mem_cgroup_pages
      if (cpu == curcpu)
        drain_local_stock // this would be ok
      else
        schedule_work_on(cpu, &stock->work); // this is asynchronous
      // charging+(no more)reclaim is retried MAX_RECLAIM_RETRIES = 16 times
      // if other cpu stock aren't flushed by now, this may cause OOM

This effect is pronounced on machines with 64k page size where it makes MEMCG_CHARGE_BATCH worth whopping 4MiB (per CPU).

Prevent the premature OOM by waiting for stock flushing (even) from remote CPUs.

---
 mm/memcontrol-v1.h |  2 +-
 mm/memcontrol.c    | 15 ++++++++++-----
 2 files changed, 11 insertions(+), 6 deletions(-)

My reason(s) for RFC:
1) I'm not sure if there isn't a simpler way than flushing stocks over all CPUs (also the guard with gfpflags_allow_blocking() is there only for explicitness, in case the code was moved over).
2) It requires specific scheduling over CPUs, so it may not be so common and severe in practice.

mm/memcontrol-v1.h +1 -1
index 6358464bb4160..3e57645d0c175 100644
@@ -24,7 +24,7 @@
 
 unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap);
 
-void drain_all_stock(struct mem_cgroup *root_memcg);
+void drain_all_stock(struct mem_cgroup *root_memcg, bool sync);
 
 unsigned long memcg_events(struct mem_cgroup *memcg, int event);
 unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item);
mm/memcontrol.c +10 -5
index 2d4d65f25fecd..ddf905baab12d 100644
@@ -1911,7 +1911,7 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  * Drains all per-CPU charge caches for given root_memcg resp. subtree
  * of the hierarchy under it.
  */
-void drain_all_stock(struct mem_cgroup *root_memcg)
+void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
 {
 	int cpu, curcpu;
 
@@ -1948,6 +1948,11 @@ void drain_all_stock(struct mem_cgroup *root_memcg)
 				schedule_work_on(cpu, &stock->work);
 		}
 	}
+	if (sync)
+		for_each_online_cpu(cpu) {
+			struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
+			flush_work(&stock->work);
+		}
 	migrate_enable();
 	mutex_unlock(&percpu_charge_mutex);
 }
@@ -2307,7 +2312,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 		goto retry;
 
 	if (!drained) {
-		drain_all_stock(mem_over_limit);
+		drain_all_stock(mem_over_limit, gfpflags_allow_blocking(gfp_mask));
 		drained = true;
 		goto retry;
 	}
@@ -3773,7 +3778,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
 	wb_memcg_offline(memcg);
 	lru_gen_offline_memcg(memcg);
 
-	drain_all_stock(memcg);
+	drain_all_stock(memcg, false);
 
 	mem_cgroup_id_put(memcg);
 }
@@ -4205,7 +4210,7 @@ static ssize_t memory_high_write(struct kernfs_open_file *of,
 			break;
 
 		if (!drained) {
-			drain_all_stock(memcg);
+			drain_all_stock(memcg, false);
 			drained = true;
 			continue;
 		}
@@ -4253,7 +4258,7 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 			break;
 
 		if (!drained) {
-			drain_all_stock(memcg);
+			drain_all_stock(memcg, false);
 			drained = true;
 			continue;
 		}

base-commit: 0ff41df1cb268fc69e703a08a57ee14ae967d0ca

Signature · 1 line
2.49.0
#1 lore raw
kernel test robot noticed the following build errors:

Hi Michal,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on 0ff41df1cb268fc69e703a08a57ee14ae967d0ca]

url:    https://github.com/intel-lab-lkp/linux/commits/Michal-Koutn/memcontrol-Wait-for-draining-of-remote-stocks-to-avoid-OOM-when-charging/20250530-231950
base:   0ff41df1cb268fc69e703a08a57ee14ae967d0ca
patch link:    https://lore.kernel.org/r/20250530151858.672391-1-mkoutny%40suse.com
patch subject: [RFC PATCH] memcontrol: Wait for draining of remote stocks to avoid OOM when charging
config: sh-randconfig-002-20250530 (https://download.01.org/0day-ci/archive/20250531/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250531/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

mm/memcontrol-v1.c: In function 'mem_cgroup_resize_max':

>> mm/memcontrol-v1.c:1475:25: error: too few arguments to function 'drain_all_stock'; expected 2, have 1
    1475 |                         drain_all_stock(memcg);
         |                         ^~~~~~~~~~~~~~~
   In file included from mm/memcontrol-v1.c:17:
   mm/memcontrol-v1.h:27:6: note: declared here
      27 | void drain_all_stock(struct mem_cgroup *root_memcg, bool sync);
         |      ^~~~~~~~~~~~~~~
   mm/memcontrol-v1.c: In function 'mem_cgroup_force_empty':
   mm/memcontrol-v1.c:1505:9: error: too few arguments to function 'drain_all_stock'; expected 2, have 1
    1505 |         drain_all_stock(memcg);
         |         ^~~~~~~~~~~~~~~
   mm/memcontrol-v1.h:27:6: note: declared here
      27 | void drain_all_stock(struct mem_cgroup *root_memcg, bool sync);
         |      ^~~~~~~~~~~~~~~

vim +/drain_all_stock +1475 mm/memcontrol-v1.c

ea1e879631ffdf Roman Gushchin 2024-06-24  1438  
ea1e879631ffdf Roman Gushchin 2024-06-24  1439  static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
ea1e879631ffdf Roman Gushchin 2024-06-24  1440  				 unsigned long max, bool memsw)
ea1e879631ffdf Roman Gushchin 2024-06-24  1441  {
ea1e879631ffdf Roman Gushchin 2024-06-24  1442  	bool enlarge = false;
ea1e879631ffdf Roman Gushchin 2024-06-24  1443  	bool drained = false;
ea1e879631ffdf Roman Gushchin 2024-06-24  1444  	int ret;
ea1e879631ffdf Roman Gushchin 2024-06-24  1445  	bool limits_invariant;
ea1e879631ffdf Roman Gushchin 2024-06-24  1446  	struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
ea1e879631ffdf Roman Gushchin 2024-06-24  1447  
ea1e879631ffdf Roman Gushchin 2024-06-24  1448  	do {
ea1e879631ffdf Roman Gushchin 2024-06-24  1449  		if (signal_pending(current)) {
ea1e879631ffdf Roman Gushchin 2024-06-24  1450  			ret = -EINTR;
ea1e879631ffdf Roman Gushchin 2024-06-24  1451  			break;
ea1e879631ffdf Roman Gushchin 2024-06-24  1452  		}
ea1e879631ffdf Roman Gushchin 2024-06-24  1453  
ea1e879631ffdf Roman Gushchin 2024-06-24  1454  		mutex_lock(&memcg_max_mutex);
ea1e879631ffdf Roman Gushchin 2024-06-24  1455  		/*
ea1e879631ffdf Roman Gushchin 2024-06-24  1456  		 * Make sure that the new limit (memsw or memory limit) doesn't
ea1e879631ffdf Roman Gushchin 2024-06-24  1457  		 * break our basic invariant rule memory.max <= memsw.max.
ea1e879631ffdf Roman Gushchin 2024-06-24  1458  		 */
ea1e879631ffdf Roman Gushchin 2024-06-24  1459  		limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
ea1e879631ffdf Roman Gushchin 2024-06-24  1460  					   max <= memcg->memsw.max;
ea1e879631ffdf Roman Gushchin 2024-06-24  1461  		if (!limits_invariant) {
ea1e879631ffdf Roman Gushchin 2024-06-24  1462  			mutex_unlock(&memcg_max_mutex);
ea1e879631ffdf Roman Gushchin 2024-06-24  1463  			ret = -EINVAL;
ea1e879631ffdf Roman Gushchin 2024-06-24  1464  			break;
ea1e879631ffdf Roman Gushchin 2024-06-24  1465  		}
ea1e879631ffdf Roman Gushchin 2024-06-24  1466  		if (max > counter->max)
ea1e879631ffdf Roman Gushchin 2024-06-24  1467  			enlarge = true;
ea1e879631ffdf Roman Gushchin 2024-06-24  1468  		ret = page_counter_set_max(counter, max);
ea1e879631ffdf Roman Gushchin 2024-06-24  1469  		mutex_unlock(&memcg_max_mutex);
ea1e879631ffdf Roman Gushchin 2024-06-24  1470  
ea1e879631ffdf Roman Gushchin 2024-06-24  1471  		if (!ret)
ea1e879631ffdf Roman Gushchin 2024-06-24  1472  			break;
ea1e879631ffdf Roman Gushchin 2024-06-24  1473  
ea1e879631ffdf Roman Gushchin 2024-06-24  1474  		if (!drained) {
ea1e879631ffdf Roman Gushchin 2024-06-24 @1475  			drain_all_stock(memcg);
ea1e879631ffdf Roman Gushchin 2024-06-24  1476  			drained = true;
ea1e879631ffdf Roman Gushchin 2024-06-24  1477  			continue;
ea1e879631ffdf Roman Gushchin 2024-06-24  1478  		}
ea1e879631ffdf Roman Gushchin 2024-06-24  1479  
ea1e879631ffdf Roman Gushchin 2024-06-24  1480  		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
68cd9050d871e4 Dan Schatzberg 2024-01-03  1481  				memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP, NULL)) {
ea1e879631ffdf Roman Gushchin 2024-06-24  1482  			ret = -EBUSY;
ea1e879631ffdf Roman Gushchin 2024-06-24  1483  			break;
ea1e879631ffdf Roman Gushchin 2024-06-24  1484  		}
ea1e879631ffdf Roman Gushchin 2024-06-24  1485  	} while (true);
ea1e879631ffdf Roman Gushchin 2024-06-24  1486  
ea1e879631ffdf Roman Gushchin 2024-06-24  1487  	if (!ret && enlarge)
ea1e879631ffdf Roman Gushchin 2024-06-24  1488  		memcg1_oom_recover(memcg);
ea1e879631ffdf Roman Gushchin 2024-06-24  1489  
ea1e879631ffdf Roman Gushchin 2024-06-24  1490  	return ret;
ea1e879631ffdf Roman Gushchin 2024-06-24  1491  }
ea1e879631ffdf Roman Gushchin 2024-06-24  1492  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#2 lore raw
In reply to Michal Koutný
Quoted 2 lines · Michal Koutný

2) It requires specific scheduling over CPUs, so it may not be so common and severe in practice.

This means in practice, there'd be likely a _different_ running memcg on other CPUs and that would implicitly flush those stocks. I'm concluding there's no big issue to fix.

Michal