]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/cpu-set-util.c
Merge pull request #14592 from keszybz/simplifications
[thirdparty/systemd.git] / src / shared / cpu-set-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
618234a5 2
11c3a366
TA
3#include <errno.h>
4#include <stddef.h>
a832893f 5#include <stdio.h>
11c3a366
TA
6#include <syslog.h>
7
b5efdb8a 8#include "alloc-util.h"
6bedfcbb 9#include "cpu-set-util.h"
b070c7c0
MS
10#include "dirent-util.h"
11#include "errno-util.h"
84ac7bea 12#include "extract-word.h"
b070c7c0 13#include "fd-util.h"
11c3a366
TA
14#include "log.h"
15#include "macro.h"
0985c7c4 16#include "memory-util.h"
b070c7c0 17#include "missing_syscall.h"
93cc7779 18#include "parse-util.h"
b070c7c0 19#include "stat-util.h"
b11d6a7b 20#include "string-util.h"
b070c7c0
MS
21#include "string-table.h"
22#include "strv.h"
23#include "util.h"
618234a5 24
0985c7c4 25char* cpu_set_to_string(const CPUSet *a) {
a832893f
ZJS
26 _cleanup_free_ char *str = NULL;
27 size_t allocated = 0, len = 0;
28 int i, r;
29
0985c7c4
ZJS
30 for (i = 0; (size_t) i < a->allocated * 8; i++) {
31 if (!CPU_ISSET_S(i, a->allocated, a->set))
a832893f
ZJS
32 continue;
33
34 if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int)))
35 return NULL;
36
37 r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
38 assert_se(r > 0);
39 len += r;
40 }
41
42 return TAKE_PTR(str) ?: strdup("");
43}
44
71b28519
MS
45char *cpu_set_to_range_string(const CPUSet *set) {
46 unsigned range_start = 0, range_end;
47 _cleanup_free_ char *str = NULL;
48 size_t allocated = 0, len = 0;
49 bool in_range = false;
50 int r;
51
52 for (unsigned i = 0; i < set->allocated * 8; i++)
53 if (CPU_ISSET_S(i, set->allocated, set->set)) {
54 if (in_range)
55 range_end++;
56 else {
57 range_start = range_end = i;
58 in_range = true;
59 }
60 } else if (in_range) {
61 in_range = false;
62
63 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
64 return NULL;
65
71923237 66 if (range_end > range_start)
1f57a176
ZJS
67 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
68 else
69 r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
71b28519
MS
70 assert_se(r > 0);
71 len += r;
72 }
73
74 if (in_range) {
75 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
76 return NULL;
77
71923237 78 if (range_end > range_start)
1f57a176
ZJS
79 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
80 else
81 r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
71b28519
MS
82 assert_se(r > 0);
83 }
84
85 return TAKE_PTR(str) ?: strdup("");
86}
87
167a776d 88int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
0985c7c4
ZJS
89 size_t need;
90
91 assert(cpu_set);
92
93 need = CPU_ALLOC_SIZE(ncpus);
94 if (need > cpu_set->allocated) {
95 cpu_set_t *t;
96
97 t = realloc(cpu_set->set, need);
98 if (!t)
99 return -ENOMEM;
100
101 memzero((uint8_t*) t + cpu_set->allocated, need - cpu_set->allocated);
102
103 cpu_set->set = t;
104 cpu_set->allocated = need;
105 }
106
107 return 0;
108}
109
110static int cpu_set_add(CPUSet *cpu_set, unsigned cpu) {
111 int r;
112
113 if (cpu >= 8192)
114 /* As of kernel 5.1, CONFIG_NR_CPUS can be set to 8192 on PowerPC */
115 return -ERANGE;
116
117 r = cpu_set_realloc(cpu_set, cpu + 1);
118 if (r < 0)
119 return r;
120
121 CPU_SET_S(cpu, cpu_set->allocated, cpu_set->set);
122 return 0;
123}
124
125int cpu_set_add_all(CPUSet *a, const CPUSet *b) {
126 int r;
127
128 /* Do this backwards, so if we fail, we fail before changing anything. */
129 for (unsigned cpu_p1 = b->allocated * 8; cpu_p1 > 0; cpu_p1--)
130 if (CPU_ISSET_S(cpu_p1 - 1, b->allocated, b->set)) {
131 r = cpu_set_add(a, cpu_p1 - 1);
132 if (r < 0)
133 return r;
134 }
135
136 return 0;
137}
138
139int parse_cpu_set_full(
618234a5 140 const char *rvalue,
0985c7c4 141 CPUSet *cpu_set,
6d8a29b2 142 bool warn,
618234a5
LP
143 const char *unit,
144 const char *filename,
145 unsigned line,
146 const char *lvalue) {
147
0985c7c4 148 _cleanup_(cpu_set_reset) CPUSet c = {};
6d8a29b2 149 const char *p = rvalue;
618234a5 150
0985c7c4 151 assert(p);
618234a5
LP
152
153 for (;;) {
154 _cleanup_free_ char *word = NULL;
0985c7c4 155 unsigned cpu_lower, cpu_upper;
618234a5
LP
156 int r;
157
4ec85141 158 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_UNQUOTE);
6d8a29b2
YW
159 if (r == -ENOMEM)
160 return warn ? log_oom() : -ENOMEM;
a26662ce 161 if (r < 0)
6d8a29b2 162 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
618234a5
LP
163 if (r == 0)
164 break;
165
a26662ce
FB
166 r = parse_range(word, &cpu_lower, &cpu_upper);
167 if (r < 0)
6d8a29b2 168 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
032cf8e4 169
6d8a29b2
YW
170 if (cpu_lower > cpu_upper) {
171 if (warn)
0985c7c4
ZJS
172 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring.",
173 word, cpu_lower, cpu_upper);
174
175 /* Make sure something is allocated, to distinguish this from the empty case */
176 r = cpu_set_realloc(&c, 1);
177 if (r < 0)
178 return r;
032cf8e4
YW
179 }
180
0985c7c4
ZJS
181 for (unsigned cpu_p1 = MIN(cpu_upper, UINT_MAX-1) + 1; cpu_p1 > cpu_lower; cpu_p1--) {
182 r = cpu_set_add(&c, cpu_p1 - 1);
183 if (r < 0)
184 return warn ? log_syntax(unit, LOG_ERR, filename, line, r,
185 "Cannot add CPU %u to set: %m", cpu_p1 - 1) : r;
186 }
032cf8e4
YW
187 }
188
0985c7c4
ZJS
189 /* On success, transfer ownership to the output variable */
190 *cpu_set = c;
191 c = (CPUSet) {};
192
193 return 0;
194}
195
196int parse_cpu_set_extend(
197 const char *rvalue,
198 CPUSet *old,
199 bool warn,
200 const char *unit,
201 const char *filename,
202 unsigned line,
203 const char *lvalue) {
204
205 _cleanup_(cpu_set_reset) CPUSet cpuset = {};
206 int r;
207
208 r = parse_cpu_set_full(rvalue, &cpuset, true, unit, filename, line, lvalue);
209 if (r < 0)
210 return r;
211
212 if (!cpuset.set) {
213 /* An empty assignment resets the CPU list */
214 cpu_set_reset(old);
215 return 0;
216 }
217
218 if (!old->set) {
219 *old = cpuset;
220 cpuset = (CPUSet) {};
221 return 0;
222 }
032cf8e4 223
0985c7c4 224 return cpu_set_add_all(old, &cpuset);
032cf8e4 225}
f44b3035
ZJS
226
227int cpus_in_affinity_mask(void) {
228 size_t n = 16;
229 int r;
230
231 for (;;) {
232 cpu_set_t *c;
233
234 c = CPU_ALLOC(n);
235 if (!c)
236 return -ENOMEM;
237
238 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
239 int k;
240
241 k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c);
242 CPU_FREE(c);
243
244 if (k <= 0)
245 return -EINVAL;
246
247 return k;
248 }
249
250 r = -errno;
251 CPU_FREE(c);
252
253 if (r != -EINVAL)
254 return r;
255 if (n > SIZE_MAX/2)
256 return -ENOMEM;
257 n *= 2;
258 }
259}
c367f996
MS
260
261int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) {
262 uint8_t *out;
263
264 assert(set);
265 assert(ret);
266
267 out = new0(uint8_t, set->allocated);
268 if (!out)
269 return -ENOMEM;
270
271 for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++)
272 if (CPU_ISSET_S(cpu, set->allocated, set->set))
273 out[cpu / 8] |= 1u << (cpu % 8);
274
275 *ret = out;
276 *allocated = set->allocated;
277 return 0;
278}
279
280int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) {
281 _cleanup_(cpu_set_reset) CPUSet s = {};
282 int r;
283
284 assert(bits);
285 assert(set);
286
287 for (unsigned cpu = size * 8; cpu > 0; cpu--)
288 if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) {
289 r = cpu_set_add(&s, cpu - 1);
290 if (r < 0)
291 return r;
292 }
293
294 *set = s;
295 s = (CPUSet) {};
296 return 0;
297}
b070c7c0
MS
298
299bool numa_policy_is_valid(const NUMAPolicy *policy) {
300 assert(policy);
301
302 if (!mpol_is_valid(numa_policy_get_type(policy)))
303 return false;
304
305 if (!policy->nodes.set &&
306 !IN_SET(numa_policy_get_type(policy), MPOL_DEFAULT, MPOL_LOCAL, MPOL_PREFERRED))
307 return false;
308
309 if (policy->nodes.set &&
310 numa_policy_get_type(policy) == MPOL_PREFERRED &&
311 CPU_COUNT_S(policy->nodes.allocated, policy->nodes.set) != 1)
312 return false;
313
314 return true;
315}
316
317static int numa_policy_to_mempolicy(const NUMAPolicy *policy, unsigned long *ret_maxnode, unsigned long **ret_nodes) {
318 unsigned node, bits = 0, ulong_bits;
319 _cleanup_free_ unsigned long *out = NULL;
320
321 assert(policy);
322 assert(ret_maxnode);
323 assert(ret_nodes);
324
325 if (IN_SET(numa_policy_get_type(policy), MPOL_DEFAULT, MPOL_LOCAL) ||
326 (numa_policy_get_type(policy) == MPOL_PREFERRED && !policy->nodes.set)) {
327 *ret_nodes = NULL;
328 *ret_maxnode = 0;
329 return 0;
330 }
331
332 bits = policy->nodes.allocated * 8;
333 ulong_bits = sizeof(unsigned long) * 8;
334
335 out = new0(unsigned long, DIV_ROUND_UP(policy->nodes.allocated, sizeof(unsigned long)));
336 if (!out)
337 return -ENOMEM;
338
339 /* We don't make any assumptions about internal type libc is using to store NUMA node mask.
340 Hence we need to convert the node mask to the representation expected by set_mempolicy() */
341 for (node = 0; node < bits; node++)
342 if (CPU_ISSET_S(node, policy->nodes.allocated, policy->nodes.set))
343 out[node / ulong_bits] |= 1ul << (node % ulong_bits);
344
345 *ret_nodes = TAKE_PTR(out);
346 *ret_maxnode = bits + 1;
347 return 0;
348}
349
350int apply_numa_policy(const NUMAPolicy *policy) {
351 int r;
352 _cleanup_free_ unsigned long *nodes = NULL;
353 unsigned long maxnode;
354
355 assert(policy);
356
357 if (get_mempolicy(NULL, NULL, 0, 0, 0) < 0 && errno == ENOSYS)
358 return -EOPNOTSUPP;
359
360 if (!numa_policy_is_valid(policy))
361 return -EINVAL;
362
363 r = numa_policy_to_mempolicy(policy, &maxnode, &nodes);
364 if (r < 0)
365 return r;
366
367 r = set_mempolicy(numa_policy_get_type(policy), nodes, maxnode);
368 if (r < 0)
369 return -errno;
370
371 return 0;
372}
373
374static const char* const mpol_table[] = {
375 [MPOL_DEFAULT] = "default",
376 [MPOL_PREFERRED] = "preferred",
377 [MPOL_BIND] = "bind",
378 [MPOL_INTERLEAVE] = "interleave",
379 [MPOL_LOCAL] = "local",
380};
381
382DEFINE_STRING_TABLE_LOOKUP(mpol, int);