1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <syslog.h>
7 
8 #include "alloc-util.h"
9 #include "cpu-set-util.h"
10 #include "dirent-util.h"
11 #include "errno-util.h"
12 #include "extract-word.h"
13 #include "fd-util.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "memory-util.h"
17 #include "parse-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "util.h"
22 
cpu_set_to_string(const CPUSet * a)23 char* cpu_set_to_string(const CPUSet *a) {
24         _cleanup_free_ char *str = NULL;
25         size_t len = 0;
26         int i, r;
27 
28         for (i = 0; (size_t) i < a->allocated * 8; i++) {
29                 if (!CPU_ISSET_S(i, a->allocated, a->set))
30                         continue;
31 
32                 if (!GREEDY_REALLOC(str, len + 1 + DECIMAL_STR_MAX(int)))
33                         return NULL;
34 
35                 r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
36                 assert_se(r > 0);
37                 len += r;
38         }
39 
40         return TAKE_PTR(str) ?: strdup("");
41 }
42 
cpu_set_to_range_string(const CPUSet * set)43 char *cpu_set_to_range_string(const CPUSet *set) {
44         unsigned range_start = 0, range_end;
45         _cleanup_free_ char *str = NULL;
46         bool in_range = false;
47         size_t len = 0;
48         int r;
49 
50         for (unsigned i = 0; i < set->allocated * 8; i++)
51                 if (CPU_ISSET_S(i, set->allocated, set->set)) {
52                         if (in_range)
53                                 range_end++;
54                         else {
55                                 range_start = range_end = i;
56                                 in_range = true;
57                         }
58                 } else if (in_range) {
59                         in_range = false;
60 
61                         if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
62                                 return NULL;
63 
64                         if (range_end > range_start)
65                                 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
66                         else
67                                 r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
68                         assert_se(r > 0);
69                         len += r;
70                 }
71 
72         if (in_range) {
73                 if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(int)))
74                         return NULL;
75 
76                 if (range_end > range_start)
77                         r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
78                 else
79                         r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
80                 assert_se(r > 0);
81         }
82 
83         return TAKE_PTR(str) ?: strdup("");
84 }
85 
cpu_set_realloc(CPUSet * cpu_set,unsigned ncpus)86 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
87         size_t need;
88 
89         assert(cpu_set);
90 
91         need = CPU_ALLOC_SIZE(ncpus);
92         if (need > cpu_set->allocated) {
93                 cpu_set_t *t;
94 
95                 t = realloc(cpu_set->set, need);
96                 if (!t)
97                         return -ENOMEM;
98 
99                 memzero((uint8_t*) t + cpu_set->allocated, need - cpu_set->allocated);
100 
101                 cpu_set->set = t;
102                 cpu_set->allocated = need;
103         }
104 
105         return 0;
106 }
107 
cpu_set_add(CPUSet * cpu_set,unsigned cpu)108 int cpu_set_add(CPUSet *cpu_set, unsigned cpu) {
109         int r;
110 
111         if (cpu >= 8192)
112                 /* As of kernel 5.1, CONFIG_NR_CPUS can be set to 8192 on PowerPC */
113                 return -ERANGE;
114 
115         r = cpu_set_realloc(cpu_set, cpu + 1);
116         if (r < 0)
117                 return r;
118 
119         CPU_SET_S(cpu, cpu_set->allocated, cpu_set->set);
120         return 0;
121 }
122 
cpu_set_add_all(CPUSet * a,const CPUSet * b)123 int cpu_set_add_all(CPUSet *a, const CPUSet *b) {
124         int r;
125 
126         /* Do this backwards, so if we fail, we fail before changing anything. */
127         for (unsigned cpu_p1 = b->allocated * 8; cpu_p1 > 0; cpu_p1--)
128                 if (CPU_ISSET_S(cpu_p1 - 1, b->allocated, b->set)) {
129                         r = cpu_set_add(a, cpu_p1 - 1);
130                         if (r < 0)
131                                 return r;
132                 }
133 
134         return 1;
135 }
136 
parse_cpu_set_full(const char * rvalue,CPUSet * cpu_set,bool warn,const char * unit,const char * filename,unsigned line,const char * lvalue)137 int parse_cpu_set_full(
138                 const char *rvalue,
139                 CPUSet *cpu_set,
140                 bool warn,
141                 const char *unit,
142                 const char *filename,
143                 unsigned line,
144                 const char *lvalue) {
145 
146         _cleanup_(cpu_set_reset) CPUSet c = {};
147         const char *p = rvalue;
148 
149         assert(p);
150 
151         for (;;) {
152                 _cleanup_free_ char *word = NULL;
153                 unsigned cpu_lower, cpu_upper;
154                 int r;
155 
156                 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_UNQUOTE);
157                 if (r == -ENOMEM)
158                         return warn ? log_oom() : -ENOMEM;
159                 if (r < 0)
160                         return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
161                 if (r == 0)
162                         break;
163 
164                 r = parse_range(word, &cpu_lower, &cpu_upper);
165                 if (r < 0)
166                         return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
167 
168                 if (cpu_lower > cpu_upper) {
169                         if (warn)
170                                 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring.",
171                                            word, cpu_lower, cpu_upper);
172 
173                         /* Make sure something is allocated, to distinguish this from the empty case */
174                         r = cpu_set_realloc(&c, 1);
175                         if (r < 0)
176                                 return r;
177                 }
178 
179                 for (unsigned cpu_p1 = MIN(cpu_upper, UINT_MAX-1) + 1; cpu_p1 > cpu_lower; cpu_p1--) {
180                         r = cpu_set_add(&c, cpu_p1 - 1);
181                         if (r < 0)
182                                 return warn ? log_syntax(unit, LOG_ERR, filename, line, r,
183                                                          "Cannot add CPU %u to set: %m", cpu_p1 - 1) : r;
184                 }
185         }
186 
187         /* On success, transfer ownership to the output variable */
188         *cpu_set = c;
189         c = (CPUSet) {};
190 
191         return 0;
192 }
193 
parse_cpu_set_extend(const char * rvalue,CPUSet * old,bool warn,const char * unit,const char * filename,unsigned line,const char * lvalue)194 int parse_cpu_set_extend(
195                 const char *rvalue,
196                 CPUSet *old,
197                 bool warn,
198                 const char *unit,
199                 const char *filename,
200                 unsigned line,
201                 const char *lvalue) {
202 
203         _cleanup_(cpu_set_reset) CPUSet cpuset = {};
204         int r;
205 
206         r = parse_cpu_set_full(rvalue, &cpuset, true, unit, filename, line, lvalue);
207         if (r < 0)
208                 return r;
209 
210         if (!cpuset.set) {
211                 /* An empty assignment resets the CPU list */
212                 cpu_set_reset(old);
213                 return 0;
214         }
215 
216         if (!old->set) {
217                 *old = cpuset;
218                 cpuset = (CPUSet) {};
219                 return 1;
220         }
221 
222         return cpu_set_add_all(old, &cpuset);
223 }
224 
cpus_in_affinity_mask(void)225 int cpus_in_affinity_mask(void) {
226         size_t n = 16;
227         int r;
228 
229         for (;;) {
230                 cpu_set_t *c;
231 
232                 c = CPU_ALLOC(n);
233                 if (!c)
234                         return -ENOMEM;
235 
236                 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
237                         int k;
238 
239                         k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c);
240                         CPU_FREE(c);
241 
242                         if (k <= 0)
243                                 return -EINVAL;
244 
245                         return k;
246                 }
247 
248                 r = -errno;
249                 CPU_FREE(c);
250 
251                 if (r != -EINVAL)
252                         return r;
253                 if (n > SIZE_MAX/2)
254                         return -ENOMEM;
255                 n *= 2;
256         }
257 }
258 
cpu_set_to_dbus(const CPUSet * set,uint8_t ** ret,size_t * allocated)259 int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) {
260         uint8_t *out;
261 
262         assert(set);
263         assert(ret);
264 
265         out = new0(uint8_t, set->allocated);
266         if (!out)
267                 return -ENOMEM;
268 
269         for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++)
270                 if (CPU_ISSET_S(cpu, set->allocated, set->set))
271                         out[cpu / 8] |= 1u << (cpu % 8);
272 
273         *ret = out;
274         *allocated = set->allocated;
275         return 0;
276 }
277 
cpu_set_from_dbus(const uint8_t * bits,size_t size,CPUSet * set)278 int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) {
279         _cleanup_(cpu_set_reset) CPUSet s = {};
280         int r;
281 
282         assert(bits);
283         assert(set);
284 
285         for (unsigned cpu = size * 8; cpu > 0; cpu--)
286                 if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) {
287                         r = cpu_set_add(&s, cpu - 1);
288                         if (r < 0)
289                                 return r;
290                 }
291 
292         *set = s;
293         s = (CPUSet) {};
294         return 0;
295 }
296