21255d
From 1a822dbe19ab6634ffb2c0d3ce92b27b503e1612 Mon Sep 17 00:00:00 2001
21255d
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
21255d
Date: Mon, 17 Feb 2020 13:50:31 +0100
21255d
Subject: [PATCH] core: add support for setting CPUAffinity= to special "numa"
21255d
 value
21255d
21255d
systemd will automatically derive CPU affinity mask from NUMA node
21255d
mask.
21255d
21255d
Fixes #13248
21255d
21255d
(cherry picked from commit e2b2fb7f566d13a3de61952b5356cd4d2eaee917)
21255d
21255d
Resolves: #1740657
21255d
---
21255d
 man/systemd.exec.xml                 |  9 +++---
21255d
 src/basic/cpu-set-util.c             | 43 ++++++++++++++++++++++++--
21255d
 src/basic/cpu-set-util.h             |  1 +
21255d
 src/core/dbus-execute.c              | 30 +++++++++++++++++-
21255d
 src/core/execute.c                   | 46 ++++++++++++++++++++++++++--
21255d
 src/core/execute.h                   |  3 ++
21255d
 src/core/load-fragment.c             | 14 ++++++++-
21255d
 src/shared/bus-unit-util.c           |  9 ++++++
21255d
 src/test/test-cpu-set-util.c         |  6 ++--
21255d
 test/TEST-36-NUMAPOLICY/testsuite.sh | 18 +++++++++++
21255d
 10 files changed, 166 insertions(+), 13 deletions(-)
21255d
21255d
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
21255d
index e2a5ede968..696438c4ef 100644
21255d
--- a/man/systemd.exec.xml
21255d
+++ b/man/systemd.exec.xml
21255d
@@ -706,10 +706,11 @@ CapabilityBoundingSet=~CAP_B CAP_C</programlisting>
21255d
         <term><varname>CPUAffinity=</varname></term>
21255d
 
21255d
         <listitem><para>Controls the CPU affinity of the executed processes. Takes a list of CPU indices or ranges
21255d
-        separated by either whitespace or commas. CPU ranges are specified by the lower and upper CPU indices separated
21255d
-        by a dash. This option may be specified more than once, in which case the specified CPU affinity masks are
21255d
-        merged. If the empty string is assigned, the mask is reset, all assignments prior to this will have no
21255d
-        effect. See
21255d
+        separated by either whitespace or commas. Alternatively, takes a special "numa" value in which case systemd
21255d
+        automatically derives allowed CPU range based on the value of <varname>NUMAMask=</varname> option. CPU ranges
21255d
+        are specified by the lower and upper CPU indices separated by a dash. This option may be specified more than
21255d
+        once, in which case the specified CPU affinity masks are merged. If the empty string is assigned, the mask
21255d
+        is reset, all assignments prior to this will have no effect. See
21255d
         <citerefentry><refentrytitle>sched_setaffinity</refentrytitle><manvolnum>2</manvolnum></citerefentry> for
21255d
         details.</para></listitem>
21255d
       </varlistentry>
21255d
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
21255d
index 51752ad1a6..1922c95864 100644
21255d
--- a/src/basic/cpu-set-util.c
21255d
+++ b/src/basic/cpu-set-util.c
21255d
@@ -12,12 +12,14 @@
21255d
 #include "cpu-set-util.h"
21255d
 #include "dirent-util.h"
21255d
 #include "extract-word.h"
21255d
+#include "fileio.h"
21255d
 #include "fd-util.h"
21255d
 #include "log.h"
21255d
 #include "macro.h"
21255d
 #include "missing.h"
21255d
 #include "parse-util.h"
21255d
 #include "stat-util.h"
21255d
+#include "stdio-util.h"
21255d
 #include "string-util.h"
21255d
 #include "string-table.h"
21255d
 #include "strv.h"
21255d
@@ -179,7 +181,7 @@ int cpu_set_add_all(CPUSet *a, const CPUSet *b) {
21255d
                                 return r;
21255d
                 }
21255d
 
21255d
-        return 0;
21255d
+        return 1;
21255d
 }
21255d
 
21255d
 int parse_cpu_set_full(
21255d
@@ -264,7 +266,7 @@ int parse_cpu_set_extend(
21255d
         if (!old->set) {
21255d
                 *old = cpuset;
21255d
                 cpuset = (CPUSet) {};
21255d
-                return 0;
21255d
+                return 1;
21255d
         }
21255d
 
21255d
         return cpu_set_add_all(old, &cpuset);
21255d
@@ -417,6 +419,43 @@ int apply_numa_policy(const NUMAPolicy *policy) {
21255d
         return 0;
21255d
 }
21255d
 
21255d
+int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) {
21255d
+        int r;
21255d
+        size_t i;
21255d
+        _cleanup_(cpu_set_reset) CPUSet s = {};
21255d
+
21255d
+        assert(policy);
21255d
+        assert(ret);
21255d
+
21255d
+        for (i = 0; i < policy->nodes.allocated * 8; i++) {
21255d
+                _cleanup_free_ char *l = NULL;
21255d
+                char p[STRLEN("/sys/devices/system/node/node//cpulist") + DECIMAL_STR_MAX(size_t) + 1];
21255d
+                _cleanup_(cpu_set_reset) CPUSet part = {};
21255d
+
21255d
+                if (!CPU_ISSET_S(i, policy->nodes.allocated, policy->nodes.set))
21255d
+                        continue;
21255d
+
21255d
+                xsprintf(p, "/sys/devices/system/node/node%zu/cpulist", i);
21255d
+
21255d
+                r = read_one_line_file(p, &l);
21255d
+                if (r < 0)
21255d
+                        return r;
21255d
+
21255d
+                r = parse_cpu_set(l, &part);
21255d
+                if (r < 0)
21255d
+                        return r;
21255d
+
21255d
+                r = cpu_set_add_all(&s, &part);
21255d
+                if (r < 0)
21255d
+                        return r;
21255d
+        }
21255d
+
21255d
+        *ret = s;
21255d
+        s = (CPUSet) {};
21255d
+
21255d
+        return 0;
21255d
+}
21255d
+
21255d
 static const char* const mpol_table[] = {
21255d
         [MPOL_DEFAULT]    = "default",
21255d
         [MPOL_PREFERRED]  = "preferred",
21255d
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
21255d
index 8519a9b6c8..795be807af 100644
21255d
--- a/src/basic/cpu-set-util.h
21255d
+++ b/src/basic/cpu-set-util.h
21255d
@@ -78,6 +78,7 @@ static inline void numa_policy_reset(NUMAPolicy *p) {
21255d
 }
21255d
 
21255d
 int apply_numa_policy(const NUMAPolicy *policy);
21255d
+int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret);
21255d
 
21255d
 const char* mpol_to_string(int i) _const_;
21255d
 int mpol_from_string(const char *s) _pure_;
21255d
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
21255d
index d5acca384f..0fe4c14e48 100644
21255d
--- a/src/core/dbus-execute.c
21255d
+++ b/src/core/dbus-execute.c
21255d
@@ -58,6 +58,8 @@ static BUS_DEFINE_PROPERTY_GET2(property_get_ioprio_priority, "i", ExecContext,
21255d
 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_string, "s", NULL);
21255d
 static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_level, "i", int, LOG_PRI);
21255d
 static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_facility, "i", int, LOG_FAC);
21255d
+static BUS_DEFINE_PROPERTY_GET(property_get_cpu_affinity_from_numa, "b", ExecContext, exec_context_get_cpu_affinity_from_numa);
21255d
+
21255d
 
21255d
 static int property_get_environment_files(
21255d
                 sd_bus *bus,
21255d
@@ -215,6 +217,7 @@ static int property_get_cpu_affinity(
21255d
                 sd_bus_error *error) {
21255d
 
21255d
         ExecContext *c = userdata;
21255d
+        _cleanup_(cpu_set_reset) CPUSet s = {};
21255d
         _cleanup_free_ uint8_t *array = NULL;
21255d
         size_t allocated;
21255d
 
21255d
@@ -222,7 +225,16 @@ static int property_get_cpu_affinity(
21255d
         assert(reply);
21255d
         assert(c);
21255d
 
21255d
-        (void) cpu_set_to_dbus(&c->cpu_set, &array, &allocated);
21255d
+        if (c->cpu_affinity_from_numa) {
21255d
+                int r;
21255d
+
21255d
+                r = numa_to_cpu_set(&c->numa_policy, &s);
21255d
+                if (r < 0)
21255d
+                        return r;
21255d
+        }
21255d
+
21255d
+        (void) cpu_set_to_dbus(c->cpu_affinity_from_numa ? &s : &c->cpu_set,  &array, &allocated);
21255d
+
21255d
         return sd_bus_message_append_array(reply, 'y', array, allocated);
21255d
 }
21255d
 
21255d
@@ -743,6 +755,7 @@ const sd_bus_vtable bus_exec_vtable[] = {
21255d
         SD_BUS_PROPERTY("CPUSchedulingPolicy", "i", property_get_cpu_sched_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
         SD_BUS_PROPERTY("CPUSchedulingPriority", "i", property_get_cpu_sched_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
         SD_BUS_PROPERTY("CPUAffinity", "ay", property_get_cpu_affinity, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
+        SD_BUS_PROPERTY("CPUAffinityFromNUMA", "b", property_get_cpu_affinity_from_numa, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
         SD_BUS_PROPERTY("NUMAPolicy", "i", property_get_numa_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
         SD_BUS_PROPERTY("NUMAMask", "ay", property_get_numa_mask, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
         SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
21255d
@@ -1639,6 +1652,20 @@ int bus_exec_context_set_transient_property(
21255d
 
21255d
                 return 1;
21255d
 
21255d
+        } else if (streq(name, "CPUAffinityFromNUMA")) {
21255d
+                int q;
21255d
+
21255d
+                r = sd_bus_message_read_basic(message, 'b', &q);
21255d
+                if (r < 0)
21255d
+                        return r;
21255d
+
21255d
+                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
21255d
+                        c->cpu_affinity_from_numa = q;
21255d
+                        unit_write_settingf(u, flags, name, "%s=%s", "CPUAffinity", "numa");
21255d
+                }
21255d
+
21255d
+                return 1;
21255d
+
21255d
         } else if (streq(name, "NUMAPolicy")) {
21255d
                 int32_t type;
21255d
 
21255d
@@ -1653,6 +1680,7 @@ int bus_exec_context_set_transient_property(
21255d
                         c->numa_policy.type = type;
21255d
 
21255d
                 return 1;
21255d
+
21255d
         } else if (streq(name, "IOSchedulingClass")) {
21255d
                 int32_t q;
21255d
 
21255d
diff --git a/src/core/execute.c b/src/core/execute.c
21255d
index 3c54ac1110..d528d08830 100644
21255d
--- a/src/core/execute.c
21255d
+++ b/src/core/execute.c
21255d
@@ -2750,6 +2750,33 @@ static int compile_suggested_paths(const ExecContext *c, const ExecParameters *p
21255d
 
21255d
 static char *exec_command_line(char **argv);
21255d
 
21255d
+static int exec_context_cpu_affinity_from_numa(const ExecContext *c, CPUSet *ret) {
21255d
+        _cleanup_(cpu_set_reset) CPUSet s = {};
21255d
+        int r;
21255d
+
21255d
+        assert(c);
21255d
+        assert(ret);
21255d
+
21255d
+        if (!c->numa_policy.nodes.set) {
21255d
+                log_debug("Can't derive CPU affinity mask from NUMA mask because NUMA mask is not set, ignoring");
21255d
+                return 0;
21255d
+        }
21255d
+
21255d
+        r = numa_to_cpu_set(&c->numa_policy, &s);
21255d
+        if (r < 0)
21255d
+                return r;
21255d
+
21255d
+        cpu_set_reset(ret);
21255d
+
21255d
+        return cpu_set_add_all(ret, &s);
21255d
+}
21255d
+
21255d
+bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c) {
21255d
+        assert(c);
21255d
+
21255d
+        return c->cpu_affinity_from_numa;
21255d
+}
21255d
+
21255d
 static int exec_child(
21255d
                 Unit *unit,
21255d
                 const ExecCommand *command,
21255d
@@ -3012,11 +3039,26 @@ static int exec_child(
21255d
                 }
21255d
         }
21255d
 
21255d
-        if (context->cpu_set.set)
21255d
-                if (sched_setaffinity(0, context->cpu_set.allocated, context->cpu_set.set) < 0) {
21255d
+        if (context->cpu_affinity_from_numa || context->cpu_set.set) {
21255d
+                _cleanup_(cpu_set_reset) CPUSet converted_cpu_set = {};
21255d
+                const CPUSet *cpu_set;
21255d
+
21255d
+                if (context->cpu_affinity_from_numa) {
21255d
+                        r = exec_context_cpu_affinity_from_numa(context, &converted_cpu_set);
21255d
+                        if (r < 0) {
21255d
+                                *exit_status = EXIT_CPUAFFINITY;
21255d
+                                return log_unit_error_errno(unit, r, "Failed to derive CPU affinity mask from NUMA mask: %m");
21255d
+                        }
21255d
+
21255d
+                        cpu_set = &converted_cpu_set;
21255d
+                } else
21255d
+                        cpu_set = &context->cpu_set;
21255d
+
21255d
+                if (sched_setaffinity(0, cpu_set->allocated, cpu_set->set) < 0) {
21255d
                         *exit_status = EXIT_CPUAFFINITY;
21255d
                         return log_unit_error_errno(unit, errno, "Failed to set up CPU affinity: %m");
21255d
                 }
21255d
+        }
21255d
 
21255d
         if (mpol_is_valid(numa_policy_get_type(&context->numa_policy))) {
21255d
                 r = apply_numa_policy(&context->numa_policy);
21255d
diff --git a/src/core/execute.h b/src/core/execute.h
21255d
index 86c1cee84c..62c6229621 100644
21255d
--- a/src/core/execute.h
21255d
+++ b/src/core/execute.h
21255d
@@ -152,6 +152,7 @@ struct ExecContext {
21255d
 
21255d
         CPUSet cpu_set;
21255d
         NUMAPolicy numa_policy;
21255d
+        bool cpu_affinity_from_numa;
21255d
 
21255d
         ExecInput std_input;
21255d
         ExecOutput std_output;
21255d
@@ -375,6 +376,8 @@ int exec_runtime_deserialize_compat(Unit *u, const char *key, const char *value,
21255d
 void exec_runtime_deserialize_one(Manager *m, const char *value, FDSet *fds);
21255d
 void exec_runtime_vacuum(Manager *m);
21255d
 
21255d
+bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
21255d
+
21255d
 const char* exec_output_to_string(ExecOutput i) _const_;
21255d
 ExecOutput exec_output_from_string(const char *s) _pure_;
21255d
 
21255d
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
21255d
index 33fdb82754..740401a582 100644
21255d
--- a/src/core/load-fragment.c
21255d
+++ b/src/core/load-fragment.c
21255d
@@ -1251,13 +1251,25 @@ int config_parse_exec_cpu_affinity(const char *unit,
21255d
                                    void *userdata) {
21255d
 
21255d
         ExecContext *c = data;
21255d
+        int r;
21255d
 
21255d
         assert(filename);
21255d
         assert(lvalue);
21255d
         assert(rvalue);
21255d
         assert(data);
21255d
 
21255d
-        return parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
21255d
+        if (streq(rvalue, "numa")) {
21255d
+                c->cpu_affinity_from_numa = true;
21255d
+                cpu_set_reset(&c->cpu_set);
21255d
+
21255d
+                return 0;
21255d
+        }
21255d
+
21255d
+        r = parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
21255d
+        if (r >= 0)
21255d
+                c->cpu_affinity_from_numa = false;
21255d
+
21255d
+        return r;
21255d
 }
21255d
 
21255d
 int config_parse_capability_set(
21255d
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
21255d
index 7029aa5615..daa2c2dce5 100644
21255d
--- a/src/shared/bus-unit-util.c
21255d
+++ b/src/shared/bus-unit-util.c
21255d
@@ -26,6 +26,8 @@
21255d
 #include "securebits-util.h"
21255d
 #include "signal-util.h"
21255d
 #include "socket-protocol-list.h"
21255d
+#include "socket-util.h"
21255d
+#include "stdio-util.h"
21255d
 #include "string-util.h"
21255d
 #include "syslog-util.h"
21255d
 #include "terminal-util.h"
21255d
@@ -997,6 +999,13 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
21255d
                 _cleanup_free_ uint8_t *array = NULL;
21255d
                 size_t allocated;
21255d
 
21255d
+                if (eq && streq(eq, "numa")) {
21255d
+                        r = sd_bus_message_append(m, "(sv)", "CPUAffinityFromNUMA", "b", true);
21255d
+                        if (r < 0)
21255d
+                                return bus_log_create_error(r);
21255d
+                        return r;
21255d
+                }
21255d
+
21255d
                 r = parse_cpu_set(eq, &cpuset);
21255d
                 if (r < 0)
21255d
                         return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
21255d
diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c
21255d
index 136eaca82d..1b7be5df4e 100644
21255d
--- a/src/test/test-cpu-set-util.c
21255d
+++ b/src/test/test-cpu-set-util.c
21255d
@@ -218,12 +218,12 @@ static void test_parse_cpu_set_extend(void) {
21255d
 
21255d
         log_info("/* %s */", __func__);
21255d
 
21255d
-        assert_se(parse_cpu_set_extend("1 3", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
21255d
+        assert_se(parse_cpu_set_extend("1 3", &c, true, NULL, "fake", 1, "CPUAffinity") == 1);
21255d
         assert_se(CPU_COUNT_S(c.allocated, c.set) == 2);
21255d
         assert_se(s1 = cpu_set_to_string(&c);;
21255d
         log_info("cpu_set_to_string: %s", s1);
21255d
 
21255d
-        assert_se(parse_cpu_set_extend("4", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
21255d
+        assert_se(parse_cpu_set_extend("4", &c, true, NULL, "fake", 1, "CPUAffinity") == 1);
21255d
         assert_se(CPU_COUNT_S(c.allocated, c.set) == 3);
21255d
         assert_se(s2 = cpu_set_to_string(&c);;
21255d
         log_info("cpu_set_to_string: %s", s2);
21255d
@@ -240,7 +240,7 @@ static void test_cpu_set_to_from_dbus(void) {
21255d
 
21255d
         log_info("/* %s */", __func__);
21255d
 
21255d
-        assert_se(parse_cpu_set_extend("1 3 8 100-200", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
21255d
+        assert_se(parse_cpu_set_extend("1 3 8 100-200", &c, true, NULL, "fake", 1, "CPUAffinity") == 1);
21255d
         assert_se(s = cpu_set_to_string(&c);;
21255d
         log_info("cpu_set_to_string: %s", s);
21255d
         assert_se(CPU_COUNT_S(c.allocated, c.set) == 104);
21255d
diff --git a/test/TEST-36-NUMAPOLICY/testsuite.sh b/test/TEST-36-NUMAPOLICY/testsuite.sh
21255d
index bffac4ffe6..7ccaa5b412 100755
21255d
--- a/test/TEST-36-NUMAPOLICY/testsuite.sh
21255d
+++ b/test/TEST-36-NUMAPOLICY/testsuite.sh
21255d
@@ -279,6 +279,18 @@ else
21255d
     # Maks must be ignored
21255d
     grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
21255d
 
21255d
+    echo "Unit file CPUAffinity=NUMA support"
21255d
+    writeTestUnitNUMAPolicy "bind" "0"
21255d
+    echo "CPUAffinity=numa" >> $testUnitNUMAConf
21255d
+    systemctl daemon-reload
21255d
+    systemctl start $testUnit
21255d
+    systemctlCheckNUMAProperties $testUnit "bind" "0"
21255d
+    pid=$(systemctl show --value -p MainPID $testUnit)
21255d
+    cpulist=$(cat /sys/devices/system/node/node0/cpulist)
21255d
+    affinity_systemd=$(systemctl show --value -p CPUAffinity $testUnit)
21255d
+    [ $cpulist = $affinity_systemd ]
21255d
+    pid1StopUnit $testUnit
21255d
+
21255d
     echo "systemd-run NUMAPolicy support"
21255d
     runUnit='numa-systemd-run-test.service'
21255d
 
21255d
@@ -309,6 +321,12 @@ else
21255d
     systemd-run -p NUMAPolicy=local -p NUMAMask=0 --unit $runUnit sleep 1000
21255d
     systemctlCheckNUMAProperties $runUnit "local" ""
21255d
     pid1StopUnit $runUnit
21255d
+
21255d
+    systemd-run -p NUMAPolicy=local -p NUMAMask=0 -p CPUAffinity=numa --unit $runUnit sleep 1000
21255d
+    systemctlCheckNUMAProperties $runUnit "local" ""
21255d
+    systemctl cat $runUnit | grep -q 'CPUAffinity=numa'
21255d
+    pid1StopUnit $runUnit
21255d
+
21255d
 fi
21255d
 
21255d
 # Cleanup