.. | .. |
---|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
---|
2 | | -#include "../perf.h" |
---|
3 | 2 | #include "util.h" |
---|
4 | 3 | #include "debug.h" |
---|
| 4 | +#include "event.h" |
---|
5 | 5 | #include <api/fs/fs.h> |
---|
6 | | -#include <sys/mman.h> |
---|
7 | 6 | #include <sys/stat.h> |
---|
8 | 7 | #include <sys/utsname.h> |
---|
9 | 8 | #include <dirent.h> |
---|
.. | .. |
---|
15 | 14 | #include <string.h> |
---|
16 | 15 | #include <errno.h> |
---|
17 | 16 | #include <limits.h> |
---|
| 17 | +#include <linux/capability.h> |
---|
18 | 18 | #include <linux/kernel.h> |
---|
19 | 19 | #include <linux/log2.h> |
---|
20 | 20 | #include <linux/time64.h> |
---|
21 | 21 | #include <unistd.h> |
---|
| 22 | +#include "cap.h" |
---|
22 | 23 | #include "strlist.h" |
---|
| 24 | +#include "string2.h" |
---|
23 | 25 | |
---|
24 | 26 | /* |
---|
25 | 27 | * XXX We need to find a better place for these things... |
---|
.. | .. |
---|
37 | 39 | perf_singlethreaded = false; |
---|
38 | 40 | } |
---|
39 | 41 | |
---|
40 | | -unsigned int page_size; |
---|
41 | | - |
---|
42 | | -#ifdef _SC_LEVEL1_DCACHE_LINESIZE |
---|
43 | | -#define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE) |
---|
44 | | -#else |
---|
45 | | -static void cache_line_size(int *cacheline_sizep) |
---|
46 | | -{ |
---|
47 | | - if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep)) |
---|
48 | | - pr_debug("cannot determine cache line size"); |
---|
49 | | -} |
---|
50 | | -#endif |
---|
51 | | - |
---|
52 | | -int cacheline_size(void) |
---|
53 | | -{ |
---|
54 | | - static int size; |
---|
55 | | - |
---|
56 | | - if (!size) |
---|
57 | | - cache_line_size(&size); |
---|
58 | | - |
---|
59 | | - return size; |
---|
60 | | -} |
---|
61 | | - |
---|
62 | 42 | int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH; |
---|
63 | 43 | int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK; |
---|
64 | 44 | |
---|
.. | .. |
---|
73 | 53 | sysctl_perf_event_max_contexts_per_stack = value; |
---|
74 | 54 | |
---|
75 | 55 | return sysctl_perf_event_max_stack; |
---|
| 56 | +} |
---|
| 57 | + |
---|
| 58 | +bool sysctl__nmi_watchdog_enabled(void) |
---|
| 59 | +{ |
---|
| 60 | + static bool cached; |
---|
| 61 | + static bool nmi_watchdog; |
---|
| 62 | + int value; |
---|
| 63 | + |
---|
| 64 | + if (cached) |
---|
| 65 | + return nmi_watchdog; |
---|
| 66 | + |
---|
| 67 | + if (sysctl__read_int("kernel/nmi_watchdog", &value) < 0) |
---|
| 68 | + return false; |
---|
| 69 | + |
---|
| 70 | + nmi_watchdog = (value > 0) ? true : false; |
---|
| 71 | + cached = true; |
---|
| 72 | + |
---|
| 73 | + return nmi_watchdog; |
---|
76 | 74 | } |
---|
77 | 75 | |
---|
78 | 76 | bool test_attr__enabled; |
---|
.. | .. |
---|
116 | 114 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
---|
117 | 115 | } |
---|
118 | 116 | |
---|
119 | | -int rm_rf(const char *path) |
---|
| 117 | +static bool match_pat(char *file, const char **pat) |
---|
| 118 | +{ |
---|
| 119 | + int i = 0; |
---|
| 120 | + |
---|
| 121 | + if (!pat) |
---|
| 122 | + return true; |
---|
| 123 | + |
---|
| 124 | + while (pat[i]) { |
---|
| 125 | + if (strglobmatch(file, pat[i])) |
---|
| 126 | + return true; |
---|
| 127 | + |
---|
| 128 | + i++; |
---|
| 129 | + } |
---|
| 130 | + |
---|
| 131 | + return false; |
---|
| 132 | +} |
---|
| 133 | + |
---|
| 134 | +/* |
---|
| 135 | + * The depth specify how deep the removal will go. |
---|
| 136 | + * 0 - will remove only files under the 'path' directory |
---|
| 137 | + * 1 .. x - will dive in x-level deep under the 'path' directory |
---|
| 138 | + * |
---|
| 139 | + * If specified the pat is array of string patterns ended with NULL, |
---|
| 140 | + * which are checked upon every file/directory found. Only matching |
---|
| 141 | + * ones are removed. |
---|
| 142 | + * |
---|
| 143 | + * The function returns: |
---|
| 144 | + * 0 on success |
---|
| 145 | + * -1 on removal failure with errno set |
---|
| 146 | + * -2 on pattern failure |
---|
| 147 | + */ |
---|
| 148 | +static int rm_rf_depth_pat(const char *path, int depth, const char **pat) |
---|
120 | 149 | { |
---|
121 | 150 | DIR *dir; |
---|
122 | | - int ret = 0; |
---|
| 151 | + int ret; |
---|
123 | 152 | struct dirent *d; |
---|
124 | 153 | char namebuf[PATH_MAX]; |
---|
| 154 | + struct stat statbuf; |
---|
125 | 155 | |
---|
126 | | - dir = opendir(path); |
---|
127 | | - if (dir == NULL) |
---|
| 156 | + /* Do not fail if there's no file. */ |
---|
| 157 | + ret = lstat(path, &statbuf); |
---|
| 158 | + if (ret) |
---|
128 | 159 | return 0; |
---|
129 | 160 | |
---|
| 161 | + /* Try to remove any file we get. */ |
---|
| 162 | + if (!(statbuf.st_mode & S_IFDIR)) |
---|
| 163 | + return unlink(path); |
---|
| 164 | + |
---|
| 165 | + /* We have directory in path. */ |
---|
| 166 | + dir = opendir(path); |
---|
| 167 | + if (dir == NULL) |
---|
| 168 | + return -1; |
---|
| 169 | + |
---|
130 | 170 | while ((d = readdir(dir)) != NULL && !ret) { |
---|
131 | | - struct stat statbuf; |
---|
132 | 171 | |
---|
133 | 172 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
---|
134 | 173 | continue; |
---|
| 174 | + |
---|
| 175 | + if (!match_pat(d->d_name, pat)) { |
---|
| 176 | + ret = -2; |
---|
| 177 | + break; |
---|
| 178 | + } |
---|
135 | 179 | |
---|
136 | 180 | scnprintf(namebuf, sizeof(namebuf), "%s/%s", |
---|
137 | 181 | path, d->d_name); |
---|
.. | .. |
---|
144 | 188 | } |
---|
145 | 189 | |
---|
146 | 190 | if (S_ISDIR(statbuf.st_mode)) |
---|
147 | | - ret = rm_rf(namebuf); |
---|
| 191 | + ret = depth ? rm_rf_depth_pat(namebuf, depth - 1, pat) : 0; |
---|
148 | 192 | else |
---|
149 | 193 | ret = unlink(namebuf); |
---|
150 | 194 | } |
---|
.. | .. |
---|
154 | 198 | return ret; |
---|
155 | 199 | |
---|
156 | 200 | return rmdir(path); |
---|
| 201 | +} |
---|
| 202 | + |
---|
| 203 | +static int rm_rf_kcore_dir(const char *path) |
---|
| 204 | +{ |
---|
| 205 | + char kcore_dir_path[PATH_MAX]; |
---|
| 206 | + const char *pat[] = { |
---|
| 207 | + "kcore", |
---|
| 208 | + "kallsyms", |
---|
| 209 | + "modules", |
---|
| 210 | + NULL, |
---|
| 211 | + }; |
---|
| 212 | + |
---|
| 213 | + snprintf(kcore_dir_path, sizeof(kcore_dir_path), "%s/kcore_dir", path); |
---|
| 214 | + |
---|
| 215 | + return rm_rf_depth_pat(kcore_dir_path, 0, pat); |
---|
| 216 | +} |
---|
| 217 | + |
---|
| 218 | +int rm_rf_perf_data(const char *path) |
---|
| 219 | +{ |
---|
| 220 | + const char *pat[] = { |
---|
| 221 | + "data", |
---|
| 222 | + "data.*", |
---|
| 223 | + NULL, |
---|
| 224 | + }; |
---|
| 225 | + |
---|
| 226 | + rm_rf_kcore_dir(path); |
---|
| 227 | + |
---|
| 228 | + return rm_rf_depth_pat(path, 0, pat); |
---|
| 229 | +} |
---|
| 230 | + |
---|
| 231 | +int rm_rf(const char *path) |
---|
| 232 | +{ |
---|
| 233 | + return rm_rf_depth_pat(path, INT_MAX, NULL); |
---|
157 | 234 | } |
---|
158 | 235 | |
---|
159 | 236 | /* A filter which removes dot files */ |
---|
.. | .. |
---|
190 | 267 | return list; |
---|
191 | 268 | } |
---|
192 | 269 | |
---|
193 | | -static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi) |
---|
194 | | -{ |
---|
195 | | - int err = -1; |
---|
196 | | - char *line = NULL; |
---|
197 | | - size_t n; |
---|
198 | | - FILE *from_fp, *to_fp; |
---|
199 | | - struct nscookie nsc; |
---|
200 | | - |
---|
201 | | - nsinfo__mountns_enter(nsi, &nsc); |
---|
202 | | - from_fp = fopen(from, "r"); |
---|
203 | | - nsinfo__mountns_exit(&nsc); |
---|
204 | | - if (from_fp == NULL) |
---|
205 | | - goto out; |
---|
206 | | - |
---|
207 | | - to_fp = fopen(to, "w"); |
---|
208 | | - if (to_fp == NULL) |
---|
209 | | - goto out_fclose_from; |
---|
210 | | - |
---|
211 | | - while (getline(&line, &n, from_fp) > 0) |
---|
212 | | - if (fputs(line, to_fp) == EOF) |
---|
213 | | - goto out_fclose_to; |
---|
214 | | - err = 0; |
---|
215 | | -out_fclose_to: |
---|
216 | | - fclose(to_fp); |
---|
217 | | - free(line); |
---|
218 | | -out_fclose_from: |
---|
219 | | - fclose(from_fp); |
---|
220 | | -out: |
---|
221 | | - return err; |
---|
222 | | -} |
---|
223 | | - |
---|
224 | | -static int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size) |
---|
225 | | -{ |
---|
226 | | - void *ptr; |
---|
227 | | - loff_t pgoff; |
---|
228 | | - |
---|
229 | | - pgoff = off_in & ~(page_size - 1); |
---|
230 | | - off_in -= pgoff; |
---|
231 | | - |
---|
232 | | - ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff); |
---|
233 | | - if (ptr == MAP_FAILED) |
---|
234 | | - return -1; |
---|
235 | | - |
---|
236 | | - while (size) { |
---|
237 | | - ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out); |
---|
238 | | - if (ret < 0 && errno == EINTR) |
---|
239 | | - continue; |
---|
240 | | - if (ret <= 0) |
---|
241 | | - break; |
---|
242 | | - |
---|
243 | | - size -= ret; |
---|
244 | | - off_in += ret; |
---|
245 | | - off_out += ret; |
---|
246 | | - } |
---|
247 | | - munmap(ptr, off_in + size); |
---|
248 | | - |
---|
249 | | - return size ? -1 : 0; |
---|
250 | | -} |
---|
251 | | - |
---|
252 | | -static int copyfile_mode_ns(const char *from, const char *to, mode_t mode, |
---|
253 | | - struct nsinfo *nsi) |
---|
254 | | -{ |
---|
255 | | - int fromfd, tofd; |
---|
256 | | - struct stat st; |
---|
257 | | - int err; |
---|
258 | | - char *tmp = NULL, *ptr = NULL; |
---|
259 | | - struct nscookie nsc; |
---|
260 | | - |
---|
261 | | - nsinfo__mountns_enter(nsi, &nsc); |
---|
262 | | - err = stat(from, &st); |
---|
263 | | - nsinfo__mountns_exit(&nsc); |
---|
264 | | - if (err) |
---|
265 | | - goto out; |
---|
266 | | - err = -1; |
---|
267 | | - |
---|
268 | | - /* extra 'x' at the end is to reserve space for '.' */ |
---|
269 | | - if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { |
---|
270 | | - tmp = NULL; |
---|
271 | | - goto out; |
---|
272 | | - } |
---|
273 | | - ptr = strrchr(tmp, '/'); |
---|
274 | | - if (!ptr) |
---|
275 | | - goto out; |
---|
276 | | - ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1); |
---|
277 | | - *ptr = '.'; |
---|
278 | | - |
---|
279 | | - tofd = mkstemp(tmp); |
---|
280 | | - if (tofd < 0) |
---|
281 | | - goto out; |
---|
282 | | - |
---|
283 | | - if (fchmod(tofd, mode)) |
---|
284 | | - goto out_close_to; |
---|
285 | | - |
---|
286 | | - if (st.st_size == 0) { /* /proc? do it slowly... */ |
---|
287 | | - err = slow_copyfile(from, tmp, nsi); |
---|
288 | | - goto out_close_to; |
---|
289 | | - } |
---|
290 | | - |
---|
291 | | - nsinfo__mountns_enter(nsi, &nsc); |
---|
292 | | - fromfd = open(from, O_RDONLY); |
---|
293 | | - nsinfo__mountns_exit(&nsc); |
---|
294 | | - if (fromfd < 0) |
---|
295 | | - goto out_close_to; |
---|
296 | | - |
---|
297 | | - err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); |
---|
298 | | - |
---|
299 | | - close(fromfd); |
---|
300 | | -out_close_to: |
---|
301 | | - close(tofd); |
---|
302 | | - if (!err) |
---|
303 | | - err = link(tmp, to); |
---|
304 | | - unlink(tmp); |
---|
305 | | -out: |
---|
306 | | - free(tmp); |
---|
307 | | - return err; |
---|
308 | | -} |
---|
309 | | - |
---|
310 | | -int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi) |
---|
311 | | -{ |
---|
312 | | - return copyfile_mode_ns(from, to, 0755, nsi); |
---|
313 | | -} |
---|
314 | | - |
---|
315 | | -int copyfile_mode(const char *from, const char *to, mode_t mode) |
---|
316 | | -{ |
---|
317 | | - return copyfile_mode_ns(from, to, mode, NULL); |
---|
318 | | -} |
---|
319 | | - |
---|
320 | | -int copyfile(const char *from, const char *to) |
---|
321 | | -{ |
---|
322 | | - return copyfile_mode(from, to, 0755); |
---|
323 | | -} |
---|
324 | | - |
---|
325 | | -static ssize_t ion(bool is_read, int fd, void *buf, size_t n) |
---|
326 | | -{ |
---|
327 | | - void *buf_start = buf; |
---|
328 | | - size_t left = n; |
---|
329 | | - |
---|
330 | | - while (left) { |
---|
331 | | - /* buf must be treated as const if !is_read. */ |
---|
332 | | - ssize_t ret = is_read ? read(fd, buf, left) : |
---|
333 | | - write(fd, buf, left); |
---|
334 | | - |
---|
335 | | - if (ret < 0 && errno == EINTR) |
---|
336 | | - continue; |
---|
337 | | - if (ret <= 0) |
---|
338 | | - return ret; |
---|
339 | | - |
---|
340 | | - left -= ret; |
---|
341 | | - buf += ret; |
---|
342 | | - } |
---|
343 | | - |
---|
344 | | - BUG_ON((size_t)(buf - buf_start) != n); |
---|
345 | | - return n; |
---|
346 | | -} |
---|
347 | | - |
---|
348 | | -/* |
---|
349 | | - * Read exactly 'n' bytes or return an error. |
---|
350 | | - */ |
---|
351 | | -ssize_t readn(int fd, void *buf, size_t n) |
---|
352 | | -{ |
---|
353 | | - return ion(true, fd, buf, n); |
---|
354 | | -} |
---|
355 | | - |
---|
356 | | -/* |
---|
357 | | - * Write exactly 'n' bytes or return an error. |
---|
358 | | - */ |
---|
359 | | -ssize_t writen(int fd, const void *buf, size_t n) |
---|
360 | | -{ |
---|
361 | | - /* ion does not modify buf. */ |
---|
362 | | - return ion(false, fd, (void *)buf, n); |
---|
363 | | -} |
---|
364 | | - |
---|
365 | 270 | size_t hex_width(u64 v) |
---|
366 | 271 | { |
---|
367 | 272 | size_t n = 1; |
---|
.. | .. |
---|
370 | 275 | ++n; |
---|
371 | 276 | |
---|
372 | 277 | return n; |
---|
373 | | -} |
---|
374 | | - |
---|
375 | | -/* |
---|
376 | | - * While we find nice hex chars, build a long_val. |
---|
377 | | - * Return number of chars processed. |
---|
378 | | - */ |
---|
379 | | -int hex2u64(const char *ptr, u64 *long_val) |
---|
380 | | -{ |
---|
381 | | - char *p; |
---|
382 | | - |
---|
383 | | - *long_val = strtoull(ptr, &p, 16); |
---|
384 | | - |
---|
385 | | - return p - ptr; |
---|
386 | 278 | } |
---|
387 | 279 | |
---|
388 | 280 | int perf_event_paranoid(void) |
---|
.. | .. |
---|
394 | 286 | |
---|
395 | 287 | return value; |
---|
396 | 288 | } |
---|
| 289 | + |
---|
| 290 | +bool perf_event_paranoid_check(int max_level) |
---|
| 291 | +{ |
---|
| 292 | + return perf_cap__capable(CAP_SYS_ADMIN) || |
---|
| 293 | + perf_cap__capable(CAP_PERFMON) || |
---|
| 294 | + perf_event_paranoid() <= max_level; |
---|
| 295 | +} |
---|
| 296 | + |
---|
397 | 297 | static int |
---|
398 | 298 | fetch_ubuntu_kernel_version(unsigned int *puint) |
---|
399 | 299 | { |
---|
.. | .. |
---|
479 | 379 | return 0; |
---|
480 | 380 | } |
---|
481 | 381 | |
---|
482 | | -const char *perf_tip(const char *dirpath) |
---|
| 382 | +int perf_tip(char **strp, const char *dirpath) |
---|
483 | 383 | { |
---|
484 | 384 | struct strlist *tips; |
---|
485 | 385 | struct str_node *node; |
---|
486 | | - char *tip = NULL; |
---|
487 | 386 | struct strlist_config conf = { |
---|
488 | 387 | .dirname = dirpath, |
---|
489 | 388 | .file_only = true, |
---|
490 | 389 | }; |
---|
| 390 | + int ret = 0; |
---|
491 | 391 | |
---|
| 392 | + *strp = NULL; |
---|
492 | 393 | tips = strlist__new("tips.txt", &conf); |
---|
493 | 394 | if (tips == NULL) |
---|
494 | | - return errno == ENOENT ? NULL : |
---|
495 | | - "Tip: check path of tips.txt or get more memory! ;-p"; |
---|
| 395 | + return -errno; |
---|
496 | 396 | |
---|
497 | 397 | if (strlist__nr_entries(tips) == 0) |
---|
498 | 398 | goto out; |
---|
499 | 399 | |
---|
500 | 400 | node = strlist__entry(tips, random() % strlist__nr_entries(tips)); |
---|
501 | | - if (asprintf(&tip, "Tip: %s", node->s) < 0) |
---|
502 | | - tip = (char *)"Tip: get more memory! ;-)"; |
---|
| 401 | + if (asprintf(strp, "Tip: %s", node->s) < 0) |
---|
| 402 | + ret = -ENOMEM; |
---|
503 | 403 | |
---|
504 | 404 | out: |
---|
505 | 405 | strlist__delete(tips); |
---|
506 | 406 | |
---|
507 | | - return tip; |
---|
| 407 | + return ret; |
---|
| 408 | +} |
---|
| 409 | + |
---|
| 410 | +char *perf_exe(char *buf, int len) |
---|
| 411 | +{ |
---|
| 412 | + int n = readlink("/proc/self/exe", buf, len); |
---|
| 413 | + if (n > 0) { |
---|
| 414 | + buf[n] = 0; |
---|
| 415 | + return buf; |
---|
| 416 | + } |
---|
| 417 | + return strcpy(buf, "perf"); |
---|
508 | 418 | } |
---|