-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf tools: Enable close-on-exec flag on perf file descriptor
In commit a21b0b3 ('perf: Introduce a flag to enable close-on-exec in perf_event_open()'), flag PERF_FLAG_FD_CLOEXEC was added to perf_event_open(2) syscall to allows userspace to atomically enable close-on-exec behavor when creating the file descriptor. This patch makes perf tools use the new flag if supported by the kernel, so that the event file descriptors got automatically closed if perf tool exec a sub-command. Signed-off-by: Yann Droneaud <ydroneaud@opteya.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/n/1404160127-7475-1-git-send-email-ydroneaud@opteya.com Signed-off-by: Jiri Olsa <jolsa@kernel.org>
- Loading branch information
Yann Droneaud
authored and
Jiri Olsa
committed
Jul 18, 2014
1 parent
ec6dbcb
commit 57480d2
Showing
11 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "util.h" | ||
#include "../perf.h" | ||
#include "cloexec.h" | ||
#include "asm/bug.h" | ||
|
||
static unsigned long flag = PERF_FLAG_FD_CLOEXEC; | ||
|
||
static int perf_flag_probe(void) | ||
{ | ||
/* use 'safest' configuration as used in perf_evsel__fallback() */ | ||
struct perf_event_attr attr = { | ||
.type = PERF_COUNT_SW_CPU_CLOCK, | ||
.config = PERF_COUNT_SW_CPU_CLOCK, | ||
}; | ||
int fd; | ||
int err; | ||
|
||
/* check cloexec flag */ | ||
fd = sys_perf_event_open(&attr, 0, -1, -1, | ||
PERF_FLAG_FD_CLOEXEC); | ||
err = errno; | ||
|
||
if (fd >= 0) { | ||
close(fd); | ||
return 1; | ||
} | ||
|
||
WARN_ONCE(err != EINVAL, | ||
"perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n", | ||
err, strerror(err)); | ||
|
||
/* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */ | ||
fd = sys_perf_event_open(&attr, 0, -1, -1, 0); | ||
err = errno; | ||
|
||
if (WARN_ONCE(fd < 0, | ||
"perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n", | ||
err, strerror(err))) | ||
return -1; | ||
|
||
close(fd); | ||
|
||
return 0; | ||
} | ||
|
||
unsigned long perf_event_open_cloexec_flag(void) | ||
{ | ||
static bool probed; | ||
|
||
if (!probed) { | ||
if (perf_flag_probe() <= 0) | ||
flag = 0; | ||
probed = true; | ||
} | ||
|
||
return flag; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef __PERF_CLOEXEC_H | ||
#define __PERF_CLOEXEC_H | ||
|
||
unsigned long perf_event_open_cloexec_flag(void); | ||
|
||
#endif /* __PERF_CLOEXEC_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters