[lxc-devel] [lxcfs/master] proc_fuse: cap swap to global values

brauner on Github lxc-bot at linuxcontainers.org
Tue Jun 9 09:30:41 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 380 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200609/90ea1569/attachment.bin>
-------------- next part --------------
From c3708fa6db4fc91b487a899a84023fecec751787 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 9 Jun 2020 11:29:25 +0200
Subject: [PATCH] proc_fuse: cap swap to global values

Closes: #412.
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/proc_fuse.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 62f9665..6a3ed66 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -1020,7 +1020,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	bool wants_swap = opts && !opts->swap_off;
 	struct file_info *d = INTTYPE_TO_PTR(fi->fh);
 	uint64_t memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
-		 hosttotal = 0, swtotal = 0;
+		 hosttotal = 0, swfree = 0, swtotal = 0;
 	struct memory_stat mstat = {};
 	size_t linelen = 0, total_len = 0;
 	char *cache = d->buf;
@@ -1071,11 +1071,21 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 		if (ret >= 0)
 			ret = cgroup_ops->get_memory_swap_current(cgroup_ops, cgroup, &memswusage_str);
 		if (ret >= 0) {
+			struct sysinfo info;
+
 			memswlimit = get_min_memlimit(cgroup, true);
 			memswlimit = memswlimit / 1024;
 
 			if (safe_uint64(memswusage_str, &memswusage, 10) < 0)
 				lxcfs_error("Failed to convert memswusage %s", memswusage_str);
+
+			ret = sysinfo(&info);
+			if (!ret) {
+				if (info.totalswap < memswlimit)
+					memswlimit = info.totalswap;
+				swfree = info.freeswap;
+			}
+
 			memswusage = memswusage / 1024;
 			swtotal = memswlimit;
 		}
@@ -1112,14 +1122,10 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			printme = lbuf;
 		} else if (startswith(line, "SwapTotal:")) {
 			if (memswlimit > 0 && wants_swap) {
-				uint64_t hostswtotal = 0;
-
-				sscanf(line + STRLITERALLEN("SwapTotal:"), "%" PRIu64, &hostswtotal);
-
-				/* Don't advertise more SWAP than the total memory allowed. */
-				if (hostswtotal < swtotal)
-					swtotal = hostswtotal;
-
+				/*
+				 * Swap total has already been calculated or
+				 * capped to the system limit above.
+				 */
 				snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " kB\n", swtotal);
 			} else {
 				snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " kB\n", (uint64_t)0);
@@ -1127,11 +1133,15 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:")) {
 			if (memswlimit > 0 && wants_swap) {
-				uint64_t swfree = 0;
 				uint64_t swusage = 0;
 
 				swusage = memswusage - memusage;
-				swfree = swtotal - swusage;
+				/*
+				 * Free swap has already been capped to the
+				 * system limit above.
+				 */
+				if (swusage <= swtotal)
+					swfree = swtotal - swusage;
 
 				snprintf(lbuf, 100, "SwapFree:       %8" PRIu64 " kB\n", swfree);
 			} else {


More information about the lxc-devel mailing list