blob: ca0fff6272be4c1dac0dde2a1b1580195b19486b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Adrian Hunterf6edb532014-08-12 18:04:31 +03003#include <sched.h>
Yann Droneaud57480d22014-06-30 22:28:47 +02004#include "util.h"
5#include "../perf.h"
6#include "cloexec.h"
7#include "asm/bug.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00008#include "debug.h"
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -03009#include <unistd.h>
10#include <asm/unistd.h>
11#include <sys/syscall.h>
Yann Droneaud57480d22014-06-30 22:28:47 +020012
13static unsigned long flag = PERF_FLAG_FD_CLOEXEC;
14
Vinson Leee1e455f2015-03-23 12:09:16 -070015int __weak sched_getcpu(void)
16{
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -030017#ifdef __NR_getcpu
18 unsigned cpu;
19 int err = syscall(__NR_getcpu, &cpu, NULL, NULL);
20 if (!err)
21 return cpu;
22#else
Vinson Leee1e455f2015-03-23 12:09:16 -070023 errno = ENOSYS;
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -030024#endif
Vinson Leee1e455f2015-03-23 12:09:16 -070025 return -1;
26}
27
Yann Droneaud57480d22014-06-30 22:28:47 +020028static int perf_flag_probe(void)
29{
30 /* use 'safest' configuration as used in perf_evsel__fallback() */
31 struct perf_event_attr attr = {
Jiri Olsa038fa0b92014-08-03 14:10:36 +020032 .type = PERF_TYPE_SOFTWARE,
Yann Droneaud57480d22014-06-30 22:28:47 +020033 .config = PERF_COUNT_SW_CPU_CLOCK,
Adrian Huntera5b01532014-08-12 18:04:28 +030034 .exclude_kernel = 1,
Yann Droneaud57480d22014-06-30 22:28:47 +020035 };
36 int fd;
37 int err;
Adrian Hunterf6edb532014-08-12 18:04:31 +030038 int cpu;
39 pid_t pid = -1;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000040 char sbuf[STRERR_BUFSIZE];
Yann Droneaud57480d22014-06-30 22:28:47 +020041
Adrian Hunterf6edb532014-08-12 18:04:31 +030042 cpu = sched_getcpu();
43 if (cpu < 0)
44 cpu = 0;
45
Adrian Hunter48536c92015-02-24 13:20:59 +020046 /*
47 * Using -1 for the pid is a workaround to avoid gratuitous jump label
48 * changes.
49 */
Adrian Hunterf6edb532014-08-12 18:04:31 +030050 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 Droneaud57480d22014-06-30 22:28:47 +020060 err = errno;
61
62 if (fd >= 0) {
63 close(fd);
64 return 1;
65 }
66
Jiri Olsa63914ac2014-08-01 17:46:54 +020067 WARN_ONCE(err != EINVAL && err != EBUSY,
Yann Droneaud57480d22014-06-30 22:28:47 +020068 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030069 err, str_error_r(err, sbuf, sizeof(sbuf)));
Yann Droneaud57480d22014-06-30 22:28:47 +020070
71 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
Adrian Hunter48536c92015-02-24 13:20:59 +020072 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 Droneaud57480d22014-06-30 22:28:47 +020080 err = errno;
81
Adrian Hunter48536c92015-02-24 13:20:59 +020082 if (fd >= 0)
83 close(fd);
84
Jiri Olsa63914ac2014-08-01 17:46:54 +020085 if (WARN_ONCE(fd < 0 && err != EBUSY,
Yann Droneaud57480d22014-06-30 22:28:47 +020086 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030087 err, str_error_r(err, sbuf, sizeof(sbuf))))
Yann Droneaud57480d22014-06-30 22:28:47 +020088 return -1;
89
Yann Droneaud57480d22014-06-30 22:28:47 +020090 return 0;
91}
92
93unsigned 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}