1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
19 #include <sys/types.h>
20 #include <rseq/compiler.h>
24 #define __max(a,b) ((a)>(b)?(a):(b))
26 #define RSEQ_CPUMASK_SIZE 4096
28 static int possible_cpus_array_len_cache
;
31 int _get_max_cpuid_from_sysfs(const char *path
)
40 cpudir
= opendir(path
);
45 * Iterate on all directories named "cpu" followed by an integer.
47 while ((entry
= readdir(cpudir
))) {
48 if (entry
->d_type
== DT_DIR
&&
49 strncmp(entry
->d_name
, "cpu", 3) == 0) {
54 cpu_id
= strtol(entry
->d_name
+ 3, &endptr
, 10);
55 if ((cpu_id
< LONG_MAX
) && (endptr
!= entry
->d_name
+ 3)
56 && (*endptr
== '\0')) {
57 if (cpu_id
> max_cpuid
)
67 * If the max CPU id is out of bound, set it to -1 so it results in a
70 if (max_cpuid
< 0 || max_cpuid
> INT_MAX
)
78 * Get the highest CPU id from sysfs.
80 * Iterate on all the folders in "/sys/devices/system/cpu" that start with
81 * "cpu" followed by an integer, keep the highest CPU id encountered during
82 * this iteration and add 1 to get a number of CPUs.
84 * Returns the highest CPU id, or -1 on error.
87 int get_max_cpuid_from_sysfs(void)
89 return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu");
93 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
94 * iterate on all the folders in "/sys/devices/system/cpu" that start with
95 * "cpu" followed by an integer, keep the highest CPU id encountered during
96 * this iteration and add 1 to get a number of CPUs.
98 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
99 * return the highest one.
101 * On Linux, using the value from sysconf can be unreliable since the way it
102 * counts CPUs varies between C libraries and even between versions of the same
103 * library. If we used it directly, getcpu() could return a value greater than
104 * this sysconf, in which case the arrays indexed by processor would overflow.
106 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
107 * sysconf does not return the number of configured CPUs in the system but
108 * relies on the cpu affinity mask of the current task.
110 * Returns 0 or less on error.
113 int get_num_possible_cpus_fallback(void)
116 * Get the sysconf value as a last resort. Keep the highest number.
118 return __max(sysconf(_SC_NPROCESSORS_CONF
), get_max_cpuid_from_sysfs() + 1);
122 * Get a CPU mask string from sysfs.
124 * buf: the buffer where the mask will be read.
125 * max_bytes: the maximum number of bytes to write in the buffer.
126 * path: file path to read the mask from.
128 * Returns the number of bytes read or -1 on error.
131 int get_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
, const char *path
)
133 ssize_t bytes_read
= 0;
134 size_t total_bytes_read
= 0;
135 int fd
= -1, ret
= -1;
142 fd
= open(path
, O_RDONLY
);
147 bytes_read
= read(fd
, buf
+ total_bytes_read
,
148 max_bytes
- total_bytes_read
);
150 if (bytes_read
< 0) {
151 if (errno
== EINTR
) {
152 continue; /* retry operation */
158 total_bytes_read
+= bytes_read
;
159 assert(total_bytes_read
<= max_bytes
);
160 } while (max_bytes
> total_bytes_read
&& bytes_read
> 0);
163 * Make sure the mask read is a null terminated string.
165 if (total_bytes_read
< max_bytes
)
166 buf
[total_bytes_read
] = '\0';
168 buf
[max_bytes
- 1] = '\0';
170 if (total_bytes_read
> INT_MAX
)
172 ret
= (int) total_bytes_read
;
174 if (fd
>= 0 && close(fd
) < 0)
180 * Get the CPU possible mask string from sysfs.
182 * buf: the buffer where the mask will be read.
183 * max_bytes: the maximum number of bytes to write in the buffer.
185 * Returns the number of bytes read or -1 on error.
188 int get_possible_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
)
190 return get_cpu_mask_from_sysfs(buf
, max_bytes
,
191 "/sys/devices/system/cpu/possible");
195 * Get the highest CPU id from a CPU mask.
197 * pmask: the mask to parse.
198 * len: the len of the mask excluding '\0'.
200 * Returns the highest CPU id from the mask or -1 on error.
203 int get_max_cpuid_from_mask(const char *pmask
, size_t len
)
206 unsigned long cpu_index
;
209 /* We need at least one char to read */
213 /* Start from the end to read the last CPU index. */
214 for (i
= len
- 1; i
> 0; i
--) {
215 /* Break when we hit the first separator. */
216 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
222 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
224 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
225 return (int) cpu_index
;
231 static void update_possible_cpus_array_len_cache(void)
233 char buf
[RSEQ_CPUMASK_SIZE
];
236 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
237 ret
= get_possible_cpu_mask_from_sysfs((char *) &buf
, RSEQ_CPUMASK_SIZE
);
241 /* Parse the possible cpu mask, on failure fallback to sysconf. */
242 ret
= get_max_cpuid_from_mask((char *) &buf
, ret
);
244 /* Add 1 to convert from max cpuid to an array len. */
250 /* Fallback to sysconf. */
251 ret
= get_num_possible_cpus_fallback();
254 /* If all methods failed, don't store the value. */
258 possible_cpus_array_len_cache
= ret
;
262 * Returns the length of an array that could contain a per-CPU element for each
263 * possible CPU id for the lifetime of the process.
265 * We currently assume CPU ids are contiguous up the maximum CPU id.
267 * If the cache is not yet initialized, get the value from
268 * "/sys/devices/system/cpu/possible" or fallback to sysconf and cache it.
270 * If all methods fail, don't populate the cache and return 0.
272 int get_possible_cpus_array_len(void)
274 if (rseq_unlikely(!possible_cpus_array_len_cache
))
275 update_possible_cpus_array_len_cache();
277 return possible_cpus_array_len_cache
;