52b84b
From 9fe3b9c7165afeedcf9f31959c436bcec233bb4d Mon Sep 17 00:00:00 2001
4d52c6
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
4d52c6
Date: Tue, 14 Apr 2020 16:16:45 +0200
4d52c6
Subject: [PATCH] basic: use comma as separator in cpuset cgroup cpu ranges
4d52c6
4d52c6
This is a workaround for
4d52c6
https://bugzilla.redhat.com/show_bug.cgi?id=1819152 and should be
4d52c6
reverted in RHEL-8.3.
4d52c6
4d52c6
RHEL-only
4d52c6
52b84b
Related: #1818054
4d52c6
---
4d52c6
 src/basic/cpu-set-util.c | 45 ++++++++++++++++++++++++++++++++++++++++
4d52c6
 src/basic/cpu-set-util.h |  1 +
4d52c6
 src/core/cgroup.c        |  2 +-
4d52c6
 3 files changed, 47 insertions(+), 1 deletion(-)
4d52c6
4d52c6
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
4d52c6
index 36cb017ae7..51752ad1a6 100644
4d52c6
--- a/src/basic/cpu-set-util.c
4d52c6
+++ b/src/basic/cpu-set-util.c
4d52c6
@@ -86,6 +86,51 @@ char *cpu_set_to_range_string(const CPUSet *set) {
4d52c6
         return TAKE_PTR(str) ?: strdup("");
4d52c6
 }
4d52c6
 
4d52c6
+/* XXX(msekleta): this is the workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1819152, remove in 8.3 */
4d52c6
+char *cpu_set_to_range_string_kernel(const CPUSet *set) {
4d52c6
+        unsigned range_start = 0, range_end;
4d52c6
+        _cleanup_free_ char *str = NULL;
4d52c6
+        size_t allocated = 0, len = 0;
4d52c6
+        bool in_range = false;
4d52c6
+        int r;
4d52c6
+
4d52c6
+        for (unsigned i = 0; i < set->allocated * 8; i++)
4d52c6
+                if (CPU_ISSET_S(i, set->allocated, set->set)) {
4d52c6
+                        if (in_range)
4d52c6
+                                range_end++;
4d52c6
+                        else {
4d52c6
+                                range_start = range_end = i;
4d52c6
+                                in_range = true;
4d52c6
+                        }
4d52c6
+                } else if (in_range) {
4d52c6
+                        in_range = false;
4d52c6
+
4d52c6
+                        if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
4d52c6
+                                return NULL;
4d52c6
+
4d52c6
+                        if (range_end > range_start)
4d52c6
+                                r = sprintf(str + len, len > 0 ? ",%d-%d" : "%d-%d", range_start, range_end);
4d52c6
+                        else
4d52c6
+                                r = sprintf(str + len, len > 0 ? ",%d" : "%d", range_start);
4d52c6
+                        assert_se(r > 0);
4d52c6
+                        len += r;
4d52c6
+                }
4d52c6
+
4d52c6
+        if (in_range) {
4d52c6
+                if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
4d52c6
+                        return NULL;
4d52c6
+
4d52c6
+                if (range_end > range_start)
4d52c6
+                        r = sprintf(str + len, len > 0 ? ",%d-%d" : "%d-%d", range_start, range_end);
4d52c6
+                else
4d52c6
+                        r = sprintf(str + len, len > 0 ? ",%d" : "%d", range_start);
4d52c6
+                assert_se(r > 0);
4d52c6
+        }
4d52c6
+
4d52c6
+        return TAKE_PTR(str) ?: strdup("");
4d52c6
+}
4d52c6
+
4d52c6
+
4d52c6
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
4d52c6
         size_t need;
4d52c6
 
4d52c6
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
4d52c6
index 295028cb54..8519a9b6c8 100644
4d52c6
--- a/src/basic/cpu-set-util.h
4d52c6
+++ b/src/basic/cpu-set-util.h
4d52c6
@@ -27,6 +27,7 @@ int cpu_set_add_all(CPUSet *a, const CPUSet *b);
4d52c6
 
4d52c6
 char* cpu_set_to_string(const CPUSet *a);
4d52c6
 char *cpu_set_to_range_string(const CPUSet *a);
4d52c6
+char *cpu_set_to_range_string_kernel(const CPUSet *a);
4d52c6
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus);
4d52c6
 
4d52c6
 int parse_cpu_set_full(
4d52c6
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
4d52c6
index 3f7665b755..9e4c3c7dac 100644
4d52c6
--- a/src/core/cgroup.c
4d52c6
+++ b/src/core/cgroup.c
4d52c6
@@ -557,7 +557,7 @@ static void cgroup_apply_unified_cpuset(Unit *u, CPUSet cpus, const char *name)
4d52c6
         _cleanup_free_ char *buf = NULL;
4d52c6
         int r;
4d52c6
 
4d52c6
-        buf = cpu_set_to_range_string(&cpus);
4d52c6
+        buf = cpu_set_to_range_string_kernel(&cpus);
4d52c6
         if (!buf)
4d52c6
             return;
4d52c6