[lxc-devel] [lxcfs/master] proc_fuse: provide host values when kernel does not support swap acco…

brauner on Github lxc-bot at linuxcontainers.org
Wed Jun 24 11:14:30 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 469 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200624/c79c15e7/attachment.bin>
-------------- next part --------------
From c68050167a14b3ed5cc0c681f5526f10b624fbcf Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 24 Jun 2020 13:13:25 +0200
Subject: [PATCH] proc_fuse: provide host values when kernel does not support
 swap accounting

Link: https://discuss.linuxcontainers.org/t/invalid-swaptotal-in-proc-meminfo-swaptotal-0
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/bindings.c       | 12 ++++++++++++
 src/bindings.h       |  1 +
 src/cgroups/cgfsng.c | 31 +++++++++++++++++++++++++++++++
 src/cgroups/cgroup.h |  1 +
 src/proc_fuse.c      |  4 ++--
 5 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/bindings.c b/src/bindings.c
index 28da74b..8dc885d 100644
--- a/src/bindings.c
+++ b/src/bindings.c
@@ -50,6 +50,7 @@
 #include "utils.h"
 
 static bool can_use_pidfd;
+static bool can_use_swap;
 
 static volatile sig_atomic_t reload_successful;
 
@@ -58,6 +59,11 @@ bool liblxcfs_functional(void)
 	return reload_successful != 0;
 }
 
+bool liblxcfs_can_use_swap(void)
+{
+	return can_use_swap;
+}
+
 /* Define pivot_root() if missing from the C library */
 #ifndef HAVE_PIVOT_ROOT
 static int pivot_root(const char *new_root, const char *put_old)
@@ -880,6 +886,12 @@ static void __attribute__((constructor)) lxcfs_init(void)
 		lxcfs_info("Kernel supports pidfds");
 	}
 
+	can_use_swap = cgroup_ops->can_use_swap(cgroup_ops);
+	if (can_use_swap)
+		lxcfs_info("Kernel supports swap accounting");
+	else
+		lxcfs_info("Kernel does not support swap accounting");
+
 	lxcfs_info("api_extensions:");
 	for (i = 0; i < nr_api_extensions; i++)
 		lxcfs_info("- %s", api_extensions[i]);
diff --git a/src/bindings.h b/src/bindings.h
index 28543dc..4ab4f72 100644
--- a/src/bindings.h
+++ b/src/bindings.h
@@ -91,6 +91,7 @@ extern pid_t lookup_initpid_in_store(pid_t qpid);
 extern void prune_init_slice(char *cg);
 extern bool supports_pidfd(void);
 extern bool liblxcfs_functional(void);
+extern bool liblxcfs_can_use_swap(void);
 
 static inline int install_signal_handler(int signo,
 					 void (*handler)(int, siginfo_t *, void *))
diff --git a/src/cgroups/cgfsng.c b/src/cgroups/cgfsng.c
index 89f7cab..cf891b3 100644
--- a/src/cgroups/cgfsng.c
+++ b/src/cgroups/cgfsng.c
@@ -619,6 +619,36 @@ static int cgfsng_get_memory_swap_max(struct cgroup_ops *ops,
 	return cgfsng_get_memory(ops, cgroup, "memory.swap.max", value);
 }
 
+static bool cgfsng_can_use_swap(struct cgroup_ops *ops)
+{
+	bool has_swap = false;
+	struct hierarchy *h;
+
+	h = ops->get_hierarchy(ops, "memory");
+	if (!h)
+		return false;
+
+	if (is_unified_hierarchy(h)) {
+		if (faccessat(h->fd, "memory.swap.max", F_OK, 0))
+			return false;
+
+		if (faccessat(h->fd, "memory.swap.current", F_OK, 0))
+			return false;
+
+		has_swap = true;
+	} else {
+		if (faccessat(h->fd, "memory.memsw.limit_in_bytes", F_OK, 0))
+			return false;
+
+		if (faccessat(h->fd, "memory.memsw.usage_in_bytes", F_OK, 0))
+			return false;
+
+		has_swap = true;
+	}
+
+	return has_swap;
+}
+
 static int cgfsng_get_memory_stats(struct cgroup_ops *ops, const char *cgroup,
 				   char **value)
 {
@@ -977,6 +1007,7 @@ struct cgroup_ops *cgfsng_ops_init(void)
 	cgfsng_ops->get_memory_swap_max = cgfsng_get_memory_swap_max;
 	cgfsng_ops->get_memory_current = cgfsng_get_memory_current;
 	cgfsng_ops->get_memory_swap_current = cgfsng_get_memory_swap_current;
+	cgfsng_ops->can_use_swap = cgfsng_can_use_swap;
 
 	/* cpuset */
 	cgfsng_ops->get_cpuset_cpus = cgfsng_get_cpuset_cpus;
diff --git a/src/cgroups/cgroup.h b/src/cgroups/cgroup.h
index e8327fd..ad4a3ae 100644
--- a/src/cgroups/cgroup.h
+++ b/src/cgroups/cgroup.h
@@ -153,6 +153,7 @@ struct cgroup_ops {
 			      char **value);
 	int (*get_memory_swap_max)(struct cgroup_ops *ops, const char *cgroup,
 				   char **value);
+	bool (*can_use_swap)(struct cgroup_ops *ops);
 
 	/* cpuset */
 	int (*get_cpuset_cpus)(struct cgroup_ops *ops, const char *cgroup,
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index c78b9e8..21bed1e 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -248,7 +248,7 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
 		       *memswusage_str = NULL;
 	struct fuse_context *fc = fuse_get_context();
 	struct lxcfs_opts *opts = (struct lxcfs_opts *)fuse_get_context()->private_data;
-	bool wants_swap = opts && !opts->swap_off;
+	bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap();
 	struct file_info *d = INTTYPE_TO_PTR(fi->fh);
 	uint64_t memswlimit = 0, memlimit = 0, memusage = 0, memswusage = 0,
 		 swtotal = 0, swfree = 0, swusage = 0;
@@ -1021,7 +1021,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	__do_fclose FILE *f = NULL;
 	struct fuse_context *fc = fuse_get_context();
 	struct lxcfs_opts *opts = (struct lxcfs_opts *)fuse_get_context()->private_data;
-	bool wants_swap = opts && !opts->swap_off, host_swap = false;
+	bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap(), host_swap = false;
 	struct file_info *d = INTTYPE_TO_PTR(fi->fh);
 	uint64_t memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
 		 hosttotal = 0, swfree = 0, swusage = 0, swtotal = 0;


More information about the lxc-devel mailing list