7240d2f66b3c8af059338df28bc032192abdcd20
[userspace-rcu.git] / src / compat-smp.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
6 */
7
8 #include <assert.h>
9 #include <ctype.h>
10 #include <dirent.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <pthread.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <urcu/compiler.h>
21
22 #define URCU_CPUMASK_SIZE 4096
23
24 #if defined(HAVE_SYSCONF)
25 static inline int get_num_possible_cpus_sysconf(void)
26 {
27 return sysconf(_SC_NPROCESSORS_CONF);
28 }
29 #else
30 /*
31 * On platforms without sysconf(), always return -1.
32 */
33 static inline int get_num_possible_cpus_sysconf(void)
34 {
35 return -1;
36 }
37 #endif
38
39 /*
40 * Get the highest CPU id from sysfs.
41 *
42 * Iterate on all the folders in "/sys/devices/system/cpu" that start with
43 * "cpu" followed by an integer, keep the highest CPU id encountered during
44 * this iteration and add 1 to get a number of CPUs.
45 *
46 * Returns the highest CPU id, or -1 on error.
47 */
48 static inline int _get_max_cpuid_from_sysfs(const char *path)
49 {
50 long max_cpuid = -1;
51
52 DIR *cpudir;
53 struct dirent *entry;
54
55 assert(path);
56
57 cpudir = opendir(path);
58 if (cpudir == NULL)
59 goto end;
60
61 /*
62 * Iterate on all directories named "cpu" followed by an integer.
63 */
64 while ((entry = readdir(cpudir))) {
65 if (entry->d_type == DT_DIR &&
66 strncmp(entry->d_name, "cpu", 3) == 0) {
67
68 char *endptr;
69 long cpu_id;
70
71 cpu_id = strtol(entry->d_name + 3, &endptr, 10);
72 if ((cpu_id < LONG_MAX) && (endptr != entry->d_name + 3)
73 && (*endptr == '\0')) {
74 if (cpu_id > max_cpuid)
75 max_cpuid = cpu_id;
76 }
77 }
78 }
79
80 if (closedir(cpudir))
81 perror("closedir");
82
83 /*
84 * If the max CPU id is out of bound, set it to -1 so it results in a
85 * CPU num of 0.
86 */
87 if (max_cpuid < 0 || max_cpuid > INT_MAX)
88 max_cpuid = -1;
89
90 end:
91 return max_cpuid;
92 }
93
94 static inline int get_max_cpuid_from_sysfs(void)
95 {
96 return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu");
97 }
98
99
100 /*
101 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
102 * iterate on all the folders in "/sys/devices/system/cpu" that start with
103 * "cpu" followed by an integer, keep the highest CPU id encountered during
104 * this iteration and add 1 to get a number of CPUs.
105 *
106 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
107 * return the highest one.
108 *
109 * On Linux, using the value from sysconf can be unreliable since the way it
110 * counts CPUs varies between C libraries and even between versions of the same
111 * library. If we used it directly, getcpu() could return a value greater than
112 * this sysconf, in which case the arrays indexed by processor would overflow.
113 *
114 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
115 * sysconf does not return the number of configured CPUs in the system but
116 * relies on the cpu affinity mask of the current task.
117 *
118 * Returns 0 or less on error.
119 */
120 static inline int get_num_possible_cpus_fallback(void)
121 {
122 /*
123 * Get the sysconf value as a last resort. Keep the highest number.
124 */
125 return caa_max(get_num_possible_cpus_sysconf(), get_max_cpuid_from_sysfs() + 1);
126 }
127
128 /*
129 * Get a CPU mask string from sysfs.
130 *
131 * buf: the buffer where the mask will be read.
132 * max_bytes: the maximum number of bytes to write in the buffer.
133 * path: file path to read the mask from.
134 *
135 * Returns the number of bytes read or -1 on error.
136 */
137 static inline int get_cpu_mask_from_sysfs(char *buf, size_t max_bytes, const char *path)
138 {
139 ssize_t bytes_read = 0;
140 size_t total_bytes_read = 0;
141 int fd = -1, ret = -1;
142
143 assert(path);
144
145 if (buf == NULL)
146 goto end;
147
148 fd = open(path, O_RDONLY);
149 if (fd < 0)
150 goto end;
151
152 do {
153 bytes_read = read(fd, buf + total_bytes_read,
154 max_bytes - total_bytes_read);
155
156 if (bytes_read < 0) {
157 if (errno == EINTR) {
158 continue; /* retry operation */
159 } else {
160 goto end;
161 }
162 }
163
164 total_bytes_read += bytes_read;
165 assert(total_bytes_read <= max_bytes);
166 } while (max_bytes > total_bytes_read && bytes_read > 0);
167
168 /*
169 * Make sure the mask read is a null terminated string.
170 */
171 if (total_bytes_read < max_bytes)
172 buf[total_bytes_read] = '\0';
173 else
174 buf[max_bytes - 1] = '\0';
175
176 if (total_bytes_read > INT_MAX)
177 goto end;
178
179 ret = (int) total_bytes_read;
180
181 end:
182 if (fd >= 0 && close(fd) < 0)
183 perror("close");
184
185 return ret;
186 }
187
188 /*
189 * Get the CPU possible mask string from sysfs.
190 *
191 * buf: the buffer where the mask will be read.
192 * max_bytes: the maximum number of bytes to write in the buffer.
193 *
194 * Returns the number of bytes read or -1 on error.
195 */
196 static inline int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes)
197 {
198 return get_cpu_mask_from_sysfs(buf, max_bytes,
199 "/sys/devices/system/cpu/possible");
200 }
201
202 /*
203 * Get the highest CPU id from the possible CPU mask.
204 *
205 * pmask: the mask to parse.
206 * len: the len of the mask excluding '\0'.
207 *
208 * Returns the highest CPU id from the mask or -1 on error.
209 */
210 static inline int get_max_cpuid_from_mask(const char *pmask, size_t len)
211 {
212 ssize_t i;
213 unsigned long cpu_index;
214 char *endptr;
215
216 /* We need at least one char to read */
217 if (len < 1)
218 goto error;
219
220 /* Start from the end to read the last CPU index. */
221 for (i = len - 1; i > 0; i--) {
222 /* Break when we hit the first separator. */
223 if ((pmask[i] == ',') || (pmask[i] == '-')) {
224 i++;
225 break;
226 }
227 }
228
229 cpu_index = strtoul(&pmask[i], &endptr, 10);
230
231 if ((&pmask[i] != endptr) && (cpu_index < INT_MAX))
232 return (int) cpu_index;
233
234 error:
235 return -1;
236 }
237
238 #ifdef __linux__
239 /*
240 * On Linux try sysfs first and fallback to sysconf.
241 */
242 static inline int get_possible_cpus_array_len(void)
243 {
244 int ret;
245 char buf[URCU_CPUMASK_SIZE];
246
247 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
248 ret = get_possible_cpu_mask_from_sysfs((char *) &buf, URCU_CPUMASK_SIZE);
249 if (ret <= 0)
250 goto fallback;
251
252 /* Parse the possible cpu mask, on failure fallback to sysconf. */
253 ret = get_max_cpuid_from_mask((char *) &buf, ret);
254 if (ret >= 0) {
255 /* Add 1 to convert from max cpuid to an array len. */
256 ret++;
257 goto end;
258 }
259
260 fallback:
261 /* Fallback to sysconf. */
262 ret = get_num_possible_cpus_fallback();
263
264 end:
265 return ret;
266 }
267 #else
268 /*
269 * On other platforms, only use sysconf.
270 */
271 static inline int get_possible_cpus_array_len(void)
272 {
273 return get_num_possible_cpus_sysconf();
274 }
275 #endif
This page took 0.034397 seconds and 4 git commands to generate.