]>
Commit | Line | Data |
---|---|---|
f6edb53c | 1 | #include <sched.h> |
57480d2c YD |
2 | #include "util.h" |
3 | #include "../perf.h" | |
4 | #include "cloexec.h" | |
5 | #include "asm/bug.h" | |
6e81c74c | 6 | #include "debug.h" |
57480d2c YD |
7 | |
8 | static unsigned long flag = PERF_FLAG_FD_CLOEXEC; | |
9 | ||
10 | static int perf_flag_probe(void) | |
11 | { | |
12 | /* use 'safest' configuration as used in perf_evsel__fallback() */ | |
13 | struct perf_event_attr attr = { | |
038fa0b9 | 14 | .type = PERF_TYPE_SOFTWARE, |
57480d2c | 15 | .config = PERF_COUNT_SW_CPU_CLOCK, |
a5b0153c | 16 | .exclude_kernel = 1, |
57480d2c YD |
17 | }; |
18 | int fd; | |
19 | int err; | |
f6edb53c AH |
20 | int cpu; |
21 | pid_t pid = -1; | |
6e81c74c | 22 | char sbuf[STRERR_BUFSIZE]; |
57480d2c | 23 | |
f6edb53c AH |
24 | cpu = sched_getcpu(); |
25 | if (cpu < 0) | |
26 | cpu = 0; | |
27 | ||
48536c91 AH |
28 | /* |
29 | * Using -1 for the pid is a workaround to avoid gratuitous jump label | |
30 | * changes. | |
31 | */ | |
f6edb53c AH |
32 | while (1) { |
33 | /* check cloexec flag */ | |
34 | fd = sys_perf_event_open(&attr, pid, cpu, -1, | |
35 | PERF_FLAG_FD_CLOEXEC); | |
36 | if (fd < 0 && pid == -1 && errno == EACCES) { | |
37 | pid = 0; | |
38 | continue; | |
39 | } | |
40 | break; | |
41 | } | |
57480d2c YD |
42 | err = errno; |
43 | ||
44 | if (fd >= 0) { | |
45 | close(fd); | |
46 | return 1; | |
47 | } | |
48 | ||
63914aca | 49 | WARN_ONCE(err != EINVAL && err != EBUSY, |
57480d2c | 50 | "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n", |
6e81c74c | 51 | err, strerror_r(err, sbuf, sizeof(sbuf))); |
57480d2c YD |
52 | |
53 | /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */ | |
48536c91 AH |
54 | while (1) { |
55 | fd = sys_perf_event_open(&attr, pid, cpu, -1, 0); | |
56 | if (fd < 0 && pid == -1 && errno == EACCES) { | |
57 | pid = 0; | |
58 | continue; | |
59 | } | |
60 | break; | |
61 | } | |
57480d2c YD |
62 | err = errno; |
63 | ||
48536c91 AH |
64 | if (fd >= 0) |
65 | close(fd); | |
66 | ||
63914aca | 67 | if (WARN_ONCE(fd < 0 && err != EBUSY, |
57480d2c | 68 | "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n", |
6e81c74c | 69 | err, strerror_r(err, sbuf, sizeof(sbuf)))) |
57480d2c YD |
70 | return -1; |
71 | ||
57480d2c YD |
72 | return 0; |
73 | } | |
74 | ||
75 | unsigned long perf_event_open_cloexec_flag(void) | |
76 | { | |
77 | static bool probed; | |
78 | ||
79 | if (!probed) { | |
80 | if (perf_flag_probe() <= 0) | |
81 | flag = 0; | |
82 | probed = true; | |
83 | } | |
84 | ||
85 | return flag; | |
86 | } |