[lxc-devel] [PATCH lxcfs] Fix busted swap usage

Nehal J Wani nehaljw.kkd1 at gmail.com
Fri Jan 8 17:16:14 UTC 2016


When no limit is specified using lxc.cgroup.memory.memsw.limit_in_bytes,
overflow occurs while calculating Swap{Total,Free}. Commit a2de34b tried
to fix this, but introduced another bug, wherein if
memory.memsw.limit_in_bytes >= memory.limit_in_bytes, then Swap{Total,Free}
are not shown as expected.

This patch assumes that if memory.memsw.limit_in_bytes is not set, then
/sys/fs/cgroup/memory/lxc/${CT_NAME}/memory.memsw.limit_in_bytes will have
the default value = LONG_MAX based on:
https://github.com/torvalds/linux/blob/v4.3/mm/memcontrol.c#L4037
https://github.com/torvalds/linux/blob/v4.3/mm/memcontrol.c#L2893
https://github.com/torvalds/linux/blob/v4.3/include/linux/page_counter.h#L21

Fixes #60

Signed-off-by: Nehal J Wani <nehaljw.kkd1 at gmail.com>
---
 lxcfs.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/lxcfs.c b/lxcfs.c
index abcbf3a..d387285 100644
--- a/lxcfs.c
+++ b/lxcfs.c
@@ -32,6 +32,7 @@
 #include <sys/socket.h>
 #include <sys/mount.h>
 #include <wait.h>
+#include <limits.h>
 
 #ifdef FORTRAVIS
 #define GLIB_DISABLE_DEPRECATION_WARNINGS
@@ -1960,10 +1961,6 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	if (!cgfs_get_value("memory", cg, "memory.stat", &memstat_str))
 		goto err;
 
-	memusage = strtoul(memusage_str, NULL, 10);
-	memlimit /= 1024;
-	memusage /= 1024;
-
 	// Following values are allowed to fail, because swapaccount might be turned
 	// off for current kernel
 	if(cgfs_get_value("memory", cg, "memory.memsw.limit_in_bytes", &memswlimit_str) &&
@@ -1971,15 +1968,17 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	{
 		memswlimit = strtoul(memswlimit_str, NULL, 10);
 		memswusage = strtoul(memswusage_str, NULL, 10);
-		memswlimit /= 1024;
-		memswusage /= 1024;
-		if (memswlimit >= memlimit)
-			memswlimit = 0;
-		if (memswusage >= memlimit)
-			memswusage = 0;
-
+		/* In case memswlimit is unlimited; i.e. not set explicitely,
+		 * the default value: RES_LIMIT is set, which is equal to
+		 * PAGE_COUNTER_MAX * PAGE_SIZE, which, in most cases, is
+		 * equal to LONG_MAX */
+		memswlimit = memswlimit == LONG_MAX ? 0 : memswlimit / 1024;
 	}
-	
+
+	memusage = strtoul(memusage_str, NULL, 10);
+	memlimit /= 1024;
+	memusage /= 1024;
+
 	get_mem_cached(memstat_str, &cached);
 
 	f = fopen("/proc/meminfo", "r");
-- 
2.5.0



More information about the lxc-devel mailing list