[Devel] [PATCH RHEL8 COMMIT] mm/memcg: reclaim memory.cache.limit_in_bytes from background

Konstantin Khorenko khorenko at virtuozzo.com
Fri Jul 30 20:23:38 MSK 2021


The commit is pushed to "branch-rh8-4.18.0-305.3.1.vz8.7.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-305.3.1.vz8.7.1
------>
commit a98497e597a2b0adbcce10e5ee82d8cc3dacee13
Author: Andrey Ryabinin <aryabinin at virtuozzo.com>
Date:   Thu Jul 29 18:20:47 2021 +0300

    mm/memcg: reclaim memory.cache.limit_in_bytes from background
    
    Reclaiming memory above memory.cache.limit_in_bytes always in direct
    reclaim mode adds to much of a cost for vstorage. Instead of direct
    reclaim allow to overflow memory.cache.limit_in_bytes but launch
    the reclaim in background task.
    
    https://pmc.acronis.com/browse/VSTOR-24395
    https://jira.sw.ru/browse/PSBM-94761
    Signed-off-by: Andrey Ryabinin <aryabinin at virtuozzo.com>
    
    (cherry picked from commit c7235680e58c0d7d792e8f47264ef233d2752b0b)
    
    see ms 1a3e1f40 ("mm: memcontrol: decouple reference counting from page accounting")
    
    https://jira.sw.ru/browse/PSBM-131957
    
    Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn at virtuozzo.com>
---
 mm/memcontrol.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 2e79ede5801b..4a4a82c55225 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2538,12 +2538,16 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg,
 	unsigned long nr_reclaimed = 0;
 
 	do {
-		if (page_counter_read(&memcg->memory) <=
-		    READ_ONCE(memcg->memory.high))
-			continue;
-		memcg_memory_event(memcg, MEMCG_HIGH);
-		nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
-							     gfp_mask, true);
+		if (page_counter_read(&memcg->memory) >
+		    READ_ONCE(memcg->memory.high)) {
+			memcg_memory_event(memcg, MEMCG_HIGH);
+			nr_reclaimed += try_to_free_mem_cgroup_pages(memcg,
+						nr_pages, gfp_mask, true);
+		}
+
+		if (page_counter_read(&memcg->cache) > memcg->cache.max)
+			nr_reclaimed += try_to_free_mem_cgroup_pages(memcg,
+						nr_pages, gfp_mask, false);
 	} while ((memcg = parent_mem_cgroup(memcg)) &&
 		 !mem_cgroup_is_root(memcg));
 
@@ -2825,12 +2829,7 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, bool kmem_charge
 			goto charge;
 		}
 
-		if (cache_charge && !page_counter_try_charge(
-				&memcg->cache, nr_pages, &counter)) {
-			refill_stock(memcg, nr_pages);
-			goto charge;
-		}
-		return 0;
+		goto done;
 	}
 
 charge:
@@ -2855,19 +2854,6 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, bool kmem_charge
 		}
 	}
 
-	if (!mem_over_limit && cache_charge) {
-		if (page_counter_try_charge(&memcg->cache, nr_pages, &counter))
-			goto done_restock;
-
-		may_swap = false;
-		mem_over_limit = mem_cgroup_from_counter(counter, cache);
-		page_counter_uncharge(&memcg->memory, batch);
-		if (do_memsw_account())
-			page_counter_uncharge(&memcg->memsw, batch);
-		if (kmem_charge)
-			page_counter_uncharge(&memcg->kmem, nr_pages);
-	}
-
 	if (!mem_over_limit)
 		goto done_restock;
 
@@ -3003,6 +2989,9 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, bool kmem_charge
 done_restock:
 	if (batch > nr_pages)
 		refill_stock(memcg, batch - nr_pages);
+done:
+	if (cache_charge)
+		page_counter_charge(&memcg->cache, nr_pages);
 
 	/*
 	 * If the hierarchy is above the normal consumption range, schedule
@@ -3043,7 +3032,11 @@ static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, bool kmem_charge
 			current->memcg_nr_pages_over_high += batch;
 			set_notify_resume(current);
 			break;
+		} else if (page_counter_read(&memcg->cache) > memcg->cache.max) {
+			if (!work_pending(&memcg->high_work))
+				schedule_work(&memcg->high_work);
 		}
+
 	} while ((memcg = parent_mem_cgroup(memcg)));
 
 	return 0;


More information about the Devel mailing list