[Devel] [PATCH RHEL COMMIT] proc, memcg: use memcg limits for showing oom_score inside CT

Konstantin Khorenko khorenko at virtuozzo.com
Fri Sep 24 15:04:56 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after ark-5.14
------>
commit 4c041174a44838145f27f5ba72fa09a412d280fa
Author: Andrey Ryabinin <ryabinin.a.a at gmail.com>
Date:   Fri Sep 24 15:04:56 2021 +0300

    proc,memcg: use memcg limits for showing oom_score inside CT
    
    Use memcg's limits of task to show /proc/<pid>/oom_score.
    
    Note: in vz7 we had different behavior. It showed 'oom_score'
    based on 've->memcg' limits of process reading oom_score.
    
    Now we look at memcg of <pid> process and don't care about the
    current one. It seems more correct behaviour.
    
    Signed-off-by: Andrey Ryabinin <aryabinin at virtuozzo.com>
    
    (cherry picked from vz8 commit 43e606413dfbabf139af6f31737b159b8d6d87b3)
    Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 fs/proc/base.c             |  8 +++++++-
 include/linux/memcontrol.h | 11 +++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index ff7b2776d7ae..e91b5145d92f 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -546,10 +546,16 @@ static const struct file_operations proc_lstats_operations = {
 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
 			  struct pid *pid, struct task_struct *task)
 {
-	unsigned long totalpages = totalram_pages() + total_swap_pages;
+	unsigned long totalpages;
 	unsigned long points = 0;
+	struct mem_cgroup *memcg;
 	long badness;
 
+	rcu_read_lock();
+	memcg = mem_cgroup_from_task(task);
+	totalpages = mem_cgroup_total_pages(memcg);
+	rcu_read_unlock();
+
 	badness = oom_badness(task, totalpages, NULL);
 	/*
 	 * Special case OOM_SCORE_ADJ_MIN for all others scale the
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 55313dba4847..84bacc521142 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -931,6 +931,17 @@ unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
 	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
 }
 
+static inline unsigned long mem_cgroup_total_pages(struct mem_cgroup *memcg)
+{
+	unsigned long ram, ram_swap;
+	extern long total_swap_pages;
+
+	ram = min_t(unsigned long, totalram_pages(), memcg->memory.max);
+	ram_swap = min_t(unsigned long, memcg->memsw.max, ram + total_swap_pages);
+
+	return ram_swap;
+}
+
 void mem_cgroup_handle_over_high(void);
 
 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);


More information about the Devel mailing list