[lxc-devel] [lxcfs/master] fix /sys/devices/system/cpu/online in cpuset not correct bug

yinhongbo on Github lxc-bot at linuxcontainers.org
Fri Sep 6 07:42:20 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1048 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190906/034654dc/attachment.bin>
-------------- next part --------------
From 1e14fd04716dede2e6be6965f0e46fcc2033a0d5 Mon Sep 17 00:00:00 2001
From: Hongbo Yin <yinhongbo at bytedance.com>
Date: Fri, 6 Sep 2019 15:35:58 +0800
Subject: [PATCH] fix /sys/devices/system/cpu/online in cpuset not correct bug

Signed-off-by: Hongbo Yin <yinhongbo at bytedance.com>
---
 bindings.h   |  1 +
 cpuset.c     | 19 +++++++++++++++++++
 sysfs_fuse.c |  6 ++++++
 3 files changed, 26 insertions(+)

diff --git a/bindings.h b/bindings.h
index fdda207..250bbac 100644
--- a/bindings.h
+++ b/bindings.h
@@ -82,5 +82,6 @@ extern char *get_cpuset(const char *cg);
 extern bool use_cpuview(const char *cg);
 extern int max_cpu_count(const char *cg);
 extern void do_release_file_info(struct fuse_file_info *fi);
+extern int cpu_number_in_cpuset(const char *cpuset);
 
 #endif /* __LXCFS_BINDINGS_H */
diff --git a/cpuset.c b/cpuset.c
index 0ff98da..a2d22d4 100644
--- a/cpuset.c
+++ b/cpuset.c
@@ -43,3 +43,22 @@ bool cpu_in_cpuset(int cpu, const char *cpuset)
 	return false;
 }
 
+/*
+ * get cpu number in cpuset
+ */
+int cpu_number_in_cpuset(const char *cpuset)
+{
+	const char *c;
+	int cpu_number = 0;
+
+	for (c = cpuset; c; c = cpuset_nexttok(c)) {
+		int a, b, ret;
+
+		ret = cpuset_getrange(c, &a, &b);
+		if (ret == 1)
+			cpu_number++;
+		else if (ret == 2)
+			cpu_number +=  a > b ? a - b + 1 : b - a + 1;
+	}
+	return cpu_number;
+}
diff --git a/sysfs_fuse.c b/sysfs_fuse.c
index 32a59b7..dc535d7 100644
--- a/sysfs_fuse.c
+++ b/sysfs_fuse.c
@@ -46,6 +46,7 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size,
 	bool use_view;
 
 	int max_cpus = 0;
+	int max_cpus_in_cpuset = 0;
 	pid_t initpid;
 	ssize_t total_len = 0;
 
@@ -77,6 +78,11 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size,
 	if (use_view)
 		max_cpus = max_cpu_count(cg);
 
+	max_cpus_in_cpuset = cpu_number_in_cpuset(cpuset);
+	// use min value in cpu quota and cpuset
+	if (max_cpus_in_cpuset > 0)
+		max_cpus = max_cpus_in_cpuset > max_cpus ? max_cpus : max_cpus_in_cpuset;
+
 	if (max_cpus == 0)
 		return read_file("/sys/devices/system/cpu/online", buf, size, d);
 	if (max_cpus > 1)


More information about the lxc-devel mailing list