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