Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Adrian Hunter | f6edb53 | 2014-08-12 18:04:31 +0300 | [diff] [blame] | 3 | #include <sched.h> |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 4 | #include "util.h" |
| 5 | #include "../perf.h" |
| 6 | #include "cloexec.h" |
| 7 | #include "asm/bug.h" |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 8 | #include "debug.h" |
Arnaldo Carvalho de Melo | c7007e9 | 2016-07-12 10:29:31 -0300 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | #include <asm/unistd.h> |
| 11 | #include <sys/syscall.h> |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 12 | |
| 13 | static unsigned long flag = PERF_FLAG_FD_CLOEXEC; |
| 14 | |
Vinson Lee | e1e455f | 2015-03-23 12:09:16 -0700 | [diff] [blame] | 15 | int __weak sched_getcpu(void) |
| 16 | { |
Arnaldo Carvalho de Melo | c7007e9 | 2016-07-12 10:29:31 -0300 | [diff] [blame] | 17 | #ifdef __NR_getcpu |
| 18 | unsigned cpu; |
| 19 | int err = syscall(__NR_getcpu, &cpu, NULL, NULL); |
| 20 | if (!err) |
| 21 | return cpu; |
| 22 | #else |
Vinson Lee | e1e455f | 2015-03-23 12:09:16 -0700 | [diff] [blame] | 23 | errno = ENOSYS; |
Arnaldo Carvalho de Melo | c7007e9 | 2016-07-12 10:29:31 -0300 | [diff] [blame] | 24 | #endif |
Vinson Lee | e1e455f | 2015-03-23 12:09:16 -0700 | [diff] [blame] | 25 | return -1; |
| 26 | } |
| 27 | |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 28 | static int perf_flag_probe(void) |
| 29 | { |
| 30 | /* use 'safest' configuration as used in perf_evsel__fallback() */ |
| 31 | struct perf_event_attr attr = { |
Jiri Olsa | 038fa0b9 | 2014-08-03 14:10:36 +0200 | [diff] [blame] | 32 | .type = PERF_TYPE_SOFTWARE, |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 33 | .config = PERF_COUNT_SW_CPU_CLOCK, |
Adrian Hunter | a5b0153 | 2014-08-12 18:04:28 +0300 | [diff] [blame] | 34 | .exclude_kernel = 1, |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 35 | }; |
| 36 | int fd; |
| 37 | int err; |
Adrian Hunter | f6edb53 | 2014-08-12 18:04:31 +0300 | [diff] [blame] | 38 | int cpu; |
| 39 | pid_t pid = -1; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 40 | char sbuf[STRERR_BUFSIZE]; |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 41 | |
Adrian Hunter | f6edb53 | 2014-08-12 18:04:31 +0300 | [diff] [blame] | 42 | cpu = sched_getcpu(); |
| 43 | if (cpu < 0) |
| 44 | cpu = 0; |
| 45 | |
Adrian Hunter | 48536c9 | 2015-02-24 13:20:59 +0200 | [diff] [blame] | 46 | /* |
| 47 | * Using -1 for the pid is a workaround to avoid gratuitous jump label |
| 48 | * changes. |
| 49 | */ |
Adrian Hunter | f6edb53 | 2014-08-12 18:04:31 +0300 | [diff] [blame] | 50 | while (1) { |
| 51 | /* check cloexec flag */ |
| 52 | fd = sys_perf_event_open(&attr, pid, cpu, -1, |
| 53 | PERF_FLAG_FD_CLOEXEC); |
| 54 | if (fd < 0 && pid == -1 && errno == EACCES) { |
| 55 | pid = 0; |
| 56 | continue; |
| 57 | } |
| 58 | break; |
| 59 | } |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 60 | err = errno; |
| 61 | |
| 62 | if (fd >= 0) { |
| 63 | close(fd); |
| 64 | return 1; |
| 65 | } |
| 66 | |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 67 | WARN_ONCE(err != EINVAL && err != EBUSY, |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 68 | "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 69 | err, str_error_r(err, sbuf, sizeof(sbuf))); |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 70 | |
| 71 | /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */ |
Adrian Hunter | 48536c9 | 2015-02-24 13:20:59 +0200 | [diff] [blame] | 72 | while (1) { |
| 73 | fd = sys_perf_event_open(&attr, pid, cpu, -1, 0); |
| 74 | if (fd < 0 && pid == -1 && errno == EACCES) { |
| 75 | pid = 0; |
| 76 | continue; |
| 77 | } |
| 78 | break; |
| 79 | } |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 80 | err = errno; |
| 81 | |
Adrian Hunter | 48536c9 | 2015-02-24 13:20:59 +0200 | [diff] [blame] | 82 | if (fd >= 0) |
| 83 | close(fd); |
| 84 | |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 85 | if (WARN_ONCE(fd < 0 && err != EBUSY, |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 86 | "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 87 | err, str_error_r(err, sbuf, sizeof(sbuf)))) |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 88 | return -1; |
| 89 | |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | unsigned long perf_event_open_cloexec_flag(void) |
| 94 | { |
| 95 | static bool probed; |
| 96 | |
| 97 | if (!probed) { |
| 98 | if (perf_flag_probe() <= 0) |
| 99 | flag = 0; |
| 100 | probed = true; |
| 101 | } |
| 102 | |
| 103 | return flag; |
| 104 | } |