Skip to content

Commit c22e150

Browse files
igorlordacmel
authored andcommitted
perf tools: Add helpers to use capabilities if present
Add utilities to help checking capabilities of the running procss. Make perf link with libcap, if it is available. If no libcap-dev[el], fallback to the geteuid() == 0 test used before. Committer notes: $ perf test python 18: 'import perf' in python : FAILED! $ perf test -v python Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc 18: 'import perf' in python : --- start --- test child forked, pid 23288 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: /tmp/build/perf/python/perf.so: undefined symbol: cap_get_flag test child finished with -1 ---- end ---- 'import perf' in python: FAILED! $ This happens because differently from the perf binary generated with this patch applied: $ ldd /tmp/build/perf/perf | grep libcap libcap.so.2 => /lib64/libcap.so.2 (0x00007f724a4ef000) $ The python binding isn't linking with libcap: $ ldd /tmp/build/perf/python/perf.so | grep libcap $ So add 'cap' to the 'extra_libraries' variable in tools/perf/util/setup.py, and rebuild: $ perf test python 18: 'import perf' in python : Ok $ If we explicitely disable libcap it also continues to work: $ make NO_LIBCAP=1 -C tools/perf O=/tmp/build/perf install-bin $ ldd /tmp/build/perf/perf | grep libcap $ ldd /tmp/build/perf/python/perf.so | grep libcap $ perf test python 18: 'import perf' in python : Ok $ Signed-off-by: Igor Lubashev <ilubashe@akamai.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexey Budankov <alexey.budankov@linux.intel.com> Cc: James Morris <jmorris@namei.org> Cc: Mathieu Poirier <mathieu.poirier@linaro.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Suzuki Poulouse <suzuki.poulose@arm.com> Cc: linux-arm-kernel@lists.infradead.org [ split from a larger patch ] Link: http://lkml.kernel.org/r/8a1e76cf5c7c9796d0d4d240fbaa85305298aafa.1565188228.git.ilubashe@akamai.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 74d5f3d commit c22e150

7 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎tools/perf/util/Build‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ perf-$(CONFIG_ZLIB) += zlib.o
148148
perf-$(CONFIG_LZMA) += lzma.o
149149
perf-$(CONFIG_ZSTD) += zstd.o
150150

151+
perf-$(CONFIG_LIBCAP) += cap.o
152+
151153
perf-y += demangle-java.o
152154
perf-y += demangle-rust.o
153155

‎tools/perf/util/cap.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Capability utilities
4+
*/
5+
6+
#ifdef HAVE_LIBCAP_SUPPORT
7+
8+
#include "cap.h"
9+
#include <stdbool.h>
10+
#include <sys/capability.h>
11+
12+
bool perf_cap__capable(cap_value_t cap)
13+
{
14+
cap_flag_value_t val;
15+
cap_t caps = cap_get_proc();
16+
17+
if (!caps)
18+
return false;
19+
20+
if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &val) != 0)
21+
val = CAP_CLEAR;
22+
23+
if (cap_free(caps) != 0)
24+
return false;
25+
26+
return val == CAP_SET;
27+
}
28+
29+
#endif /* HAVE_LIBCAP_SUPPORT */

‎tools/perf/util/cap.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __PERF_CAP_H
3+
#define __PERF_CAP_H
4+
5+
#include <stdbool.h>
6+
#include <linux/capability.h>
7+
#include <linux/compiler.h>
8+
9+
#ifdef HAVE_LIBCAP_SUPPORT
10+
11+
#include <sys/capability.h>
12+
13+
bool perf_cap__capable(cap_value_t cap);
14+
15+
#else
16+
17+
#include <unistd.h>
18+
#include <sys/types.h>
19+
20+
static inline bool perf_cap__capable(int cap __maybe_unused)
21+
{
22+
return geteuid() == 0;
23+
}
24+
25+
#endif /* HAVE_LIBCAP_SUPPORT */
26+
27+
#endif /* __PERF_CAP_H */

‎tools/perf/util/event.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ void cpu_map_data__synthesize(struct cpu_map_data *data, struct perf_cpu_map *m
851851
void event_attr_init(struct perf_event_attr *attr);
852852

853853
int perf_event_paranoid(void);
854+
bool perf_event_paranoid_check(int max_level);
854855

855856
extern int sysctl_perf_event_max_stack;
856857
extern int sysctl_perf_event_max_contexts_per_stack;

‎tools/perf/util/python-ext-sources‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
util/python.c
99
../lib/ctype.c
10+
util/cap.c
1011
util/evlist.c
1112
util/evsel.c
1213
util/cpumap.c

‎tools/perf/util/setup.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def finalize_options(self):
5959
extra_libraries = []
6060
if '-DHAVE_LIBNUMA_SUPPORT' in cflags:
6161
extra_libraries = [ 'numa' ]
62+
if '-DHAVE_LIBCAP_SUPPORT' in cflags:
63+
extra_libraries += [ 'cap' ]
6264

6365
perf = Extension('perf',
6466
sources = ext_sources,

‎tools/perf/util/util.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
#include <string.h>
1717
#include <errno.h>
1818
#include <limits.h>
19+
#include <linux/capability.h>
1920
#include <linux/kernel.h>
2021
#include <linux/log2.h>
2122
#include <linux/time64.h>
2223
#include <unistd.h>
24+
#include "cap.h"
2325
#include "strlist.h"
2426
#include "string2.h"
2527

@@ -403,6 +405,13 @@ int perf_event_paranoid(void)
403405

404406
return value;
405407
}
408+
409+
bool perf_event_paranoid_check(int max_level)
410+
{
411+
return perf_cap__capable(CAP_SYS_ADMIN) ||
412+
perf_event_paranoid() <= max_level;
413+
}
414+
406415
static int
407416
fetch_ubuntu_kernel_version(unsigned int *puint)
408417
{

0 commit comments

Comments
 (0)