valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0281-shared-cpu-set-util-make-transfer-of-cpu_set_t-over-.patch

Brian Stinson 2593d8
From daa0243fda679c8af723648b8b1e501fc55b0ada Mon Sep 17 00:00:00 2001
Brian Stinson 2593d8
From: Michal Sekletar <msekleta@redhat.com>
Brian Stinson 2593d8
Date: Wed, 22 May 2019 13:55:49 +0200
Brian Stinson 2593d8
Subject: [PATCH] shared/cpu-set-util: make transfer of cpu_set_t over bus
Brian Stinson 2593d8
 endian safe
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit c367f996f5f091a63f812f0140b304c649be77fc)
Brian Stinson 2593d8
Brian Stinson 2593d8
Related: #1734787
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/basic/cpu-set-util.c   | 38 ++++++++++++++++++++++++++++++++++++++
Brian Stinson 2593d8
 src/basic/cpu-set-util.h   |  3 +++
Brian Stinson 2593d8
 src/core/dbus-execute.c    |  6 +++++-
Brian Stinson 2593d8
 src/shared/bus-unit-util.c |  8 +++++++-
Brian Stinson 2593d8
 4 files changed, 53 insertions(+), 2 deletions(-)
Brian Stinson 2593d8
Brian Stinson 2593d8
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
Brian Stinson 2593d8
index c297eab032..74e35e57dd 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.c
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.c
Brian Stinson 2593d8
@@ -209,3 +209,41 @@ int cpus_in_affinity_mask(void) {
Brian Stinson 2593d8
                 n *= 2;
Brian Stinson 2593d8
         }
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) {
Brian Stinson 2593d8
+        uint8_t *out;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        assert(set);
Brian Stinson 2593d8
+        assert(ret);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        out = new0(uint8_t, set->allocated);
Brian Stinson 2593d8
+        if (!out)
Brian Stinson 2593d8
+                return -ENOMEM;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++)
Brian Stinson 2593d8
+                if (CPU_ISSET_S(cpu, set->allocated, set->set))
Brian Stinson 2593d8
+                        out[cpu / 8] |= 1u << (cpu % 8);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        *ret = out;
Brian Stinson 2593d8
+        *allocated = set->allocated;
Brian Stinson 2593d8
+        return 0;
Brian Stinson 2593d8
+}
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) {
Brian Stinson 2593d8
+        _cleanup_(cpu_set_reset) CPUSet s = {};
Brian Stinson 2593d8
+        int r;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        assert(bits);
Brian Stinson 2593d8
+        assert(set);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        for (unsigned cpu = size * 8; cpu > 0; cpu--)
Brian Stinson 2593d8
+                if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) {
Brian Stinson 2593d8
+                        r = cpu_set_add(&s, cpu - 1);
Brian Stinson 2593d8
+                        if (r < 0)
Brian Stinson 2593d8
+                                return r;
Brian Stinson 2593d8
+                }
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        *set = s;
Brian Stinson 2593d8
+        s = (CPUSet) {};
Brian Stinson 2593d8
+        return 0;
Brian Stinson 2593d8
+}
Brian Stinson 2593d8
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
index 68a73bf9f7..415c6ca295 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.h
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
@@ -46,4 +46,7 @@ static inline int parse_cpu_set(const char *rvalue, CPUSet *cpu_set){
Brian Stinson 2593d8
         return parse_cpu_set_full(rvalue, cpu_set, false, NULL, NULL, 0, NULL);
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated);
Brian Stinson 2593d8
+int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
 int cpus_in_affinity_mask(void);
Brian Stinson 2593d8
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
Brian Stinson 2593d8
index 08946627e3..50ea71a281 100644
Brian Stinson 2593d8
--- a/src/core/dbus-execute.c
Brian Stinson 2593d8
+++ b/src/core/dbus-execute.c
Brian Stinson 2593d8
@@ -1553,18 +1553,22 @@ int bus_exec_context_set_transient_property(
Brian Stinson 2593d8
         if (streq(name, "CPUAffinity")) {
Brian Stinson 2593d8
                 const void *a;
Brian Stinson 2593d8
                 size_t n;
Brian Stinson 2593d8
+                _cleanup_(cpu_set_reset) CPUSet set = {};
Brian Stinson 2593d8
 
Brian Stinson 2593d8
                 r = sd_bus_message_read_array(message, 'y', &a, &n);
Brian Stinson 2593d8
                 if (r < 0)
Brian Stinson 2593d8
                         return r;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+                r = cpu_set_from_dbus(a, n, &set);
Brian Stinson 2593d8
+                if (r < 0)
Brian Stinson 2593d8
+                        return r;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
Brian Stinson 2593d8
                         if (n == 0) {
Brian Stinson 2593d8
                                 cpu_set_reset(&c->cpu_set);
Brian Stinson 2593d8
                                 unit_write_settingf(u, flags, name, "%s=", name);
Brian Stinson 2593d8
                         } else {
Brian Stinson 2593d8
                                 _cleanup_free_ char *str = NULL;
Brian Stinson 2593d8
-                                const CPUSet set = {(cpu_set_t*) a, n};
Brian Stinson 2593d8
 
Brian Stinson 2593d8
                                 str = cpu_set_to_string(&set);
Brian Stinson 2593d8
                                 if (!str)
Brian Stinson 2593d8
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
Brian Stinson 2593d8
index 75b4aace84..ec8732c226 100644
Brian Stinson 2593d8
--- a/src/shared/bus-unit-util.c
Brian Stinson 2593d8
+++ b/src/shared/bus-unit-util.c
Brian Stinson 2593d8
@@ -933,12 +933,18 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         if (streq(field, "CPUAffinity")) {
Brian Stinson 2593d8
                 _cleanup_(cpu_set_reset) CPUSet cpuset = {};
Brian Stinson 2593d8
+                _cleanup_free_ uint8_t *array = NULL;
Brian Stinson 2593d8
+                size_t allocated;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
                 r = parse_cpu_set(eq, &cpuset);
Brian Stinson 2593d8
                 if (r < 0)
Brian Stinson 2593d8
                         return log_error_errno(r, "Failed to parse %s value: %s", field, eq);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-                return bus_append_byte_array(m, field, cpuset.set, cpuset.allocated);
Brian Stinson 2593d8
+                r = cpu_set_to_dbus(&cpuset, &array, &allocated);
Brian Stinson 2593d8
+                if (r < 0)
Brian Stinson 2593d8
+                        return log_error_errno(r, "Failed to serialize CPUAffinity: %m");
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                return bus_append_byte_array(m, field, array, allocated);
Brian Stinson 2593d8
         }
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         if (STR_IN_SET(field, "RestrictAddressFamilies", "SystemCallFilter")) {