]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/cloexec.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / cloexec.c
CommitLineData
a43783ae 1#include <errno.h>
f6edb53c 2#include <sched.h>
57480d2c
YD
3#include "util.h"
4#include "../perf.h"
5#include "cloexec.h"
6#include "asm/bug.h"
6e81c74c 7#include "debug.h"
c7007e98
ACM
8#include <unistd.h>
9#include <asm/unistd.h>
10#include <sys/syscall.h>
57480d2c
YD
11
12static unsigned long flag = PERF_FLAG_FD_CLOEXEC;
13
e1e455f4
VL
14int __weak sched_getcpu(void)
15{
c7007e98
ACM
16#ifdef __NR_getcpu
17 unsigned cpu;
18 int err = syscall(__NR_getcpu, &cpu, NULL, NULL);
19 if (!err)
20 return cpu;
21#else
e1e455f4 22 errno = ENOSYS;
c7007e98 23#endif
e1e455f4
VL
24 return -1;
25}
26
57480d2c
YD
27static int perf_flag_probe(void)
28{
29 /* use 'safest' configuration as used in perf_evsel__fallback() */
30 struct perf_event_attr attr = {
038fa0b9 31 .type = PERF_TYPE_SOFTWARE,
57480d2c 32 .config = PERF_COUNT_SW_CPU_CLOCK,
a5b0153c 33 .exclude_kernel = 1,
57480d2c
YD
34 };
35 int fd;
36 int err;
f6edb53c
AH
37 int cpu;
38 pid_t pid = -1;
6e81c74c 39 char sbuf[STRERR_BUFSIZE];
57480d2c 40
f6edb53c
AH
41 cpu = sched_getcpu();
42 if (cpu < 0)
43 cpu = 0;
44
48536c91
AH
45 /*
46 * Using -1 for the pid is a workaround to avoid gratuitous jump label
47 * changes.
48 */
f6edb53c
AH
49 while (1) {
50 /* check cloexec flag */
51 fd = sys_perf_event_open(&attr, pid, cpu, -1,
52 PERF_FLAG_FD_CLOEXEC);
53 if (fd < 0 && pid == -1 && errno == EACCES) {
54 pid = 0;
55 continue;
56 }
57 break;
58 }
57480d2c
YD
59 err = errno;
60
61 if (fd >= 0) {
62 close(fd);
63 return 1;
64 }
65
63914aca 66 WARN_ONCE(err != EINVAL && err != EBUSY,
57480d2c 67 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
c8b5f2c9 68 err, str_error_r(err, sbuf, sizeof(sbuf)));
57480d2c
YD
69
70 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
48536c91
AH
71 while (1) {
72 fd = sys_perf_event_open(&attr, pid, cpu, -1, 0);
73 if (fd < 0 && pid == -1 && errno == EACCES) {
74 pid = 0;
75 continue;
76 }
77 break;
78 }
57480d2c
YD
79 err = errno;
80
48536c91
AH
81 if (fd >= 0)
82 close(fd);
83
63914aca 84 if (WARN_ONCE(fd < 0 && err != EBUSY,
57480d2c 85 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
c8b5f2c9 86 err, str_error_r(err, sbuf, sizeof(sbuf))))
57480d2c
YD
87 return -1;
88
57480d2c
YD
89 return 0;
90}
91
92unsigned long perf_event_open_cloexec_flag(void)
93{
94 static bool probed;
95
96 if (!probed) {
97 if (perf_flag_probe() <= 0)
98 flag = 0;
99 probed = true;
100 }
101
102 return flag;
103}