1 // SPDX-License-Identifier: GPL-2.0
10 #include <asm/unistd.h>
11 #include <sys/syscall.h>
13 static unsigned long flag
= PERF_FLAG_FD_CLOEXEC
;
15 int __weak
sched_getcpu(void)
19 int err
= syscall(__NR_getcpu
, &cpu
, NULL
, NULL
);
28 static int perf_flag_probe(void)
30 /* use 'safest' configuration as used in perf_evsel__fallback() */
31 struct perf_event_attr attr
= {
32 .type
= PERF_TYPE_SOFTWARE
,
33 .config
= PERF_COUNT_SW_CPU_CLOCK
,
40 char sbuf
[STRERR_BUFSIZE
];
47 * Using -1 for the pid is a workaround to avoid gratuitous jump label
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
) {
67 WARN_ONCE(err
!= EINVAL
&& err
!= EBUSY
,
68 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
69 err
, str_error_r(err
, sbuf
, sizeof(sbuf
)));
71 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
73 fd
= sys_perf_event_open(&attr
, pid
, cpu
, -1, 0);
74 if (fd
< 0 && pid
== -1 && errno
== EACCES
) {
85 if (WARN_ONCE(fd
< 0 && err
!= EBUSY
,
86 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
87 err
, str_error_r(err
, sbuf
, sizeof(sbuf
))))
93 unsigned long perf_event_open_cloexec_flag(void)
98 if (perf_flag_probe() <= 0)