[lxc-devel] [lxcfs/master] 2016 08 21/fixrace

hallyn on Github lxc-bot at linuxcontainers.org
Mon Aug 22 01:11:11 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 302 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160822/7d07b0da/attachment.bin>
-------------- next part --------------
From 56ee748c3fd89752a6262471eb49d3b670bb3c09 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge at hallyn.com>
Date: Sun, 21 Aug 2016 13:26:42 -0500
Subject: [PATCH 1/3] pam: fix race in cgroup creation

If we find that a cgroup already exists, we should

1. remove all the cgroups which we've created so far
2. set existed to true
3. return failure

The caller should then detect that existed == true,
and re-try with the next index.

Signed-off-by: Serge Hallyn <serge at hallyn.com>
---
 pam/pam_cgfs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/pam/pam_cgfs.c b/pam/pam_cgfs.c
index 08f0694..24510e1 100644
--- a/pam/pam_cgfs.c
+++ b/pam/pam_cgfs.c
@@ -585,7 +585,7 @@ static bool cgfs_create_forone(struct controller *c, uid_t uid, gid_t gid, const
 #if DEBUG
 			fprintf(stderr, "%s existed\n", path);
 #endif
-			return true;
+			return false;
 		}
 
 		bool pass = mkdir_p(c->mount_path, path);
@@ -794,16 +794,16 @@ static int handle_login(const char *user)
 			return PAM_SESSION_ERR;
 		}
 
+		existed = false;
 		if (!cgfs_create(cg, uid, gid, &existed)) {
+			if (existed) {
+				idx++;
+				continue;
+			}
 			mysyslog(LOG_ERR, "Failed to create a cgroup for user %s\n", user);
 			return PAM_SESSION_ERR;
 		}
 
-		if (existed == 1) {
-			idx++;
-			continue;
-		}
-
 		if (!cgfs_enter(cg, false)) {
 			mysyslog(LOG_ERR, "Failed to enter user cgroup %s for user %s\n", cg, user);
 			return PAM_SESSION_ERR;

From 942dcb2e02c825efa6d5ec5359776cce1f5e4b4c Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge at hallyn.com>
Date: Sun, 21 Aug 2016 15:05:31 -0500
Subject: [PATCH 2/3] meminfo: don't show negative swapfree

Also commonize some of the mem{,sw} free/used calculations.

Closes #115

Signed-off-by: Serge Hallyn <serge at hallyn.com>
---
 bindings.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/bindings.c b/bindings.c
index 2fb4acf..c310650 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3069,7 +3069,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 		*memswlimit_str = NULL, *memswusage_str = NULL,
 		*memswlimit_default_str = NULL, *memswusage_default_str = NULL;
 	unsigned long memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
-		cached = 0, hosttotal = 0;
+		cached = 0, hosttotal = 0, swaptotal, swapusage, swapfree, memfree;
 	char *line = NULL;
 	size_t linelen = 0, total_len = 0, rv = 0;
 	char *cache = d->buf;
@@ -3127,6 +3127,10 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	memusage = strtoul(memusage_str, NULL, 10);
 	memlimit /= 1024;
 	memusage /= 1024;
+	memfree = memlimit - memusage;
+	swaptotal = memswlimit - memlimit;
+	swapusage = memswusage - memusage;
+	swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0;
 
 	get_mem_cached(memstat_str, &cached);
 
@@ -3146,17 +3150,16 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			snprintf(lbuf, 100, "MemTotal:       %8lu kB\n", memlimit);
 			printme = lbuf;
 		} else if (startswith(line, "MemFree:")) {
-			snprintf(lbuf, 100, "MemFree:        %8lu kB\n", memlimit - memusage);
+			snprintf(lbuf, 100, "MemFree:        %8lu kB\n", memfree);
 			printme = lbuf;
 		} else if (startswith(line, "MemAvailable:")) {
-			snprintf(lbuf, 100, "MemAvailable:   %8lu kB\n", memlimit - memusage);
+			snprintf(lbuf, 100, "MemAvailable:   %8lu kB\n", memfree);
 			printme = lbuf;
 		} else if (startswith(line, "SwapTotal:") && memswlimit > 0) {
-			snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", memswlimit - memlimit);
+			snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", swaptotal);
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0) {
-			snprintf(lbuf, 100, "SwapFree:       %8lu kB\n",
-				(memswlimit - memlimit) - (memswusage - memusage));
+			snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", swapfree);
 			printme = lbuf;
 		} else if (startswith(line, "Slab:")) {
 			snprintf(lbuf, 100, "Slab:        %8lu kB\n", 0UL);
@@ -3472,6 +3475,8 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
 		char cpu_char[10]; /* That's a lot of cores */
 		char *c;
 
+		if (strlen(line) == 0)
+			continue;
 		if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) {
 			/* not a ^cpuN line containing a number N, just print it */
 			l = snprintf(cache, cache_size, "%s", line);

From 43ef690ec8fb0c64c2ed65e887657cca8839ff1a Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge at hallyn.com>
Date: Sun, 21 Aug 2016 20:07:52 -0500
Subject: [PATCH 3/3] fix up

---
 bindings.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/bindings.c b/bindings.c
index c310650..9bac10a 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3069,7 +3069,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 		*memswlimit_str = NULL, *memswusage_str = NULL,
 		*memswlimit_default_str = NULL, *memswusage_default_str = NULL;
 	unsigned long memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
-		cached = 0, hosttotal = 0, swaptotal, swapusage, swapfree, memfree;
+		cached = 0, hosttotal = 0;
 	char *line = NULL;
 	size_t linelen = 0, total_len = 0, rv = 0;
 	char *cache = d->buf;
@@ -3127,10 +3127,6 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	memusage = strtoul(memusage_str, NULL, 10);
 	memlimit /= 1024;
 	memusage /= 1024;
-	memfree = memlimit - memusage;
-	swaptotal = memswlimit - memlimit;
-	swapusage = memswusage - memusage;
-	swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0;
 
 	get_mem_cached(memstat_str, &cached);
 
@@ -3150,15 +3146,18 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			snprintf(lbuf, 100, "MemTotal:       %8lu kB\n", memlimit);
 			printme = lbuf;
 		} else if (startswith(line, "MemFree:")) {
-			snprintf(lbuf, 100, "MemFree:        %8lu kB\n", memfree);
+			snprintf(lbuf, 100, "MemFree:        %8lu kB\n", memlimit - memusage);
 			printme = lbuf;
 		} else if (startswith(line, "MemAvailable:")) {
-			snprintf(lbuf, 100, "MemAvailable:   %8lu kB\n", memfree);
+			snprintf(lbuf, 100, "MemAvailable:   %8lu kB\n", memlimit - memusage);
 			printme = lbuf;
 		} else if (startswith(line, "SwapTotal:") && memswlimit > 0) {
-			snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", swaptotal);
+			snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", memswlimit - memlimit);
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0) {
+			unsigned long swaptotal = memswlimit - memlimit,
+					swapusage = memswusage - memusage,
+					swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0;
 			snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", swapfree);
 			printme = lbuf;
 		} else if (startswith(line, "Slab:")) {


More information about the lxc-devel mailing list