The memsw counter in cgroup-v2 is reused by swap counter and does not
include memory usage, we should use it directly instead of subtracting
memory from memsw.

While on it let's reorder loading memused, memtotal, swapused and
swaptotal before doing any calculations or corrections on them, and also
fix comment style.

https://virtuozzo.atlassian.net/browse/VSTOR-105819
Fixes: 6992eead25249 ("ve/proc: virtualize /proc/meminfo in a Container")
Signed-off-by: Pavel Tikhomirov <[email protected]>

Feature: procfs: virtualize /proc/meminfo
---
 mm/show_mem.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/mm/show_mem.c b/mm/show_mem.c
index be4985e672dc4..0453e3c635eb2 100644
--- a/mm/show_mem.c
+++ b/mm/show_mem.c
@@ -106,13 +106,23 @@ void si_meminfo_ve(struct sysinfo *si, struct ve_struct 
*ve)
 
        memtotal = READ_ONCE(memcg->memory.max);
        memused = page_counter_read(&memcg->memory);
-       si->totalram = memtotal;
 
-       swaptotal = READ_ONCE(memcg->memsw.max) - memtotal;
-       swapused = page_counter_read(&memcg->memsw) - memused;
+       if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
+               swaptotal = READ_ONCE(memcg->swap.max);
+               swapused = page_counter_read(&memcg->swap);
+       } else {
+               swaptotal = READ_ONCE(memcg->memsw.max) - memtotal;
+               swapused = page_counter_read(&memcg->memsw) - memused;
+       }
+
+       si->totalram = memtotal;
        si->totalswap = swaptotal;
-       /* Due to global reclaim, memory.memsw.usage can be greater than
-        * (memory.memsw.max - memory.max). */
+
+       /*
+        * Due to global reclaim, memory.memsw.usage can be greater than
+        * (memory.memsw.max - memory.max), and we show this "excess" swap
+        * usage as if it's memory usage.
+        */
        if (swaptotal >= swapused) {
                si->freeswap = swaptotal - swapused;
        } else {
-- 
2.51.0

_______________________________________________
Devel mailing list
[email protected]
https://lists.openvz.org/mailman/listinfo/devel

Reply via email to