The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxcfs/pull/418

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
…unting

Link: https://discuss.linuxcontainers.org/t/invalid-swaptotal-in-proc-meminfo-swaptotal-0
Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com>
From c68050167a14b3ed5cc0c681f5526f10b624fbcf Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brau...@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.brau...@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;
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to