summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Lubashev <ilubashe@akamai.com>2019-08-07 10:44:14 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2019-08-14 09:48:39 -0400
commitc22e150e3afa6f8db2300bd510e4ac26bbee1bf3 (patch)
tree62da5a7e092c75a43660d2075d626663d3aa3767
parent74d5f3d06f707eb5f7e1908ad88954bde02000ce (diff)
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>
-rw-r--r--tools/perf/util/Build2
-rw-r--r--tools/perf/util/cap.c29
-rw-r--r--tools/perf/util/cap.h27
-rw-r--r--tools/perf/util/event.h1
-rw-r--r--tools/perf/util/python-ext-sources1
-rw-r--r--tools/perf/util/setup.py2
-rw-r--r--tools/perf/util/util.c9
7 files changed, 71 insertions, 0 deletions
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 7abf05131889..7cda749059a9 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -148,6 +148,8 @@ perf-$(CONFIG_ZLIB) += zlib.o
148perf-$(CONFIG_LZMA) += lzma.o 148perf-$(CONFIG_LZMA) += lzma.o
149perf-$(CONFIG_ZSTD) += zstd.o 149perf-$(CONFIG_ZSTD) += zstd.o
150 150
151perf-$(CONFIG_LIBCAP) += cap.o
152
151perf-y += demangle-java.o 153perf-y += demangle-java.o
152perf-y += demangle-rust.o 154perf-y += demangle-rust.o
153 155
diff --git a/tools/perf/util/cap.c b/tools/perf/util/cap.c
new file mode 100644
index 000000000000..c3ba841bbf37
--- /dev/null
+++ b/tools/perf/util/cap.c
@@ -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
12bool 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 */
diff --git a/tools/perf/util/cap.h b/tools/perf/util/cap.h
new file mode 100644
index 000000000000..10af94e473da
--- /dev/null
+++ b/tools/perf/util/cap.h
@@ -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
13bool perf_cap__capable(cap_value_t cap);
14
15#else
16
17#include <unistd.h>
18#include <sys/types.h>
19
20static 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 */
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 70841d115349..0e164e8ae28d 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -851,6 +851,7 @@ void cpu_map_data__synthesize(struct cpu_map_data *data, struct perf_cpu_map *m
851void event_attr_init(struct perf_event_attr *attr); 851void event_attr_init(struct perf_event_attr *attr);
852 852
853int perf_event_paranoid(void); 853int perf_event_paranoid(void);
854bool perf_event_paranoid_check(int max_level);
854 855
855extern int sysctl_perf_event_max_stack; 856extern int sysctl_perf_event_max_stack;
856extern int sysctl_perf_event_max_contexts_per_stack; 857extern int sysctl_perf_event_max_contexts_per_stack;
diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources
index 235bd9803390..c6dd478956f1 100644
--- a/tools/perf/util/python-ext-sources
+++ b/tools/perf/util/python-ext-sources
@@ -7,6 +7,7 @@
7 7
8util/python.c 8util/python.c
9../lib/ctype.c 9../lib/ctype.c
10util/cap.c
10util/evlist.c 11util/evlist.c
11util/evsel.c 12util/evsel.c
12util/cpumap.c 13util/cpumap.c
diff --git a/tools/perf/util/setup.py b/tools/perf/util/setup.py
index d48f9cd58964..aa344a163eaf 100644
--- a/tools/perf/util/setup.py
+++ b/tools/perf/util/setup.py
@@ -59,6 +59,8 @@ ext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources))
59extra_libraries = [] 59extra_libraries = []
60if '-DHAVE_LIBNUMA_SUPPORT' in cflags: 60if '-DHAVE_LIBNUMA_SUPPORT' in cflags:
61 extra_libraries = [ 'numa' ] 61 extra_libraries = [ 'numa' ]
62if '-DHAVE_LIBCAP_SUPPORT' in cflags:
63 extra_libraries += [ 'cap' ]
62 64
63perf = Extension('perf', 65perf = Extension('perf',
64 sources = ext_sources, 66 sources = ext_sources,
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 9c3c97697387..6fd130a5d8f2 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -16,10 +16,12 @@
16#include <string.h> 16#include <string.h>
17#include <errno.h> 17#include <errno.h>
18#include <limits.h> 18#include <limits.h>
19#include <linux/capability.h>
19#include <linux/kernel.h> 20#include <linux/kernel.h>
20#include <linux/log2.h> 21#include <linux/log2.h>
21#include <linux/time64.h> 22#include <linux/time64.h>
22#include <unistd.h> 23#include <unistd.h>
24#include "cap.h"
23#include "strlist.h" 25#include "strlist.h"
24#include "string2.h" 26#include "string2.h"
25 27
@@ -403,6 +405,13 @@ int perf_event_paranoid(void)
403 405
404 return value; 406 return value;
405} 407}
408
409bool perf_event_paranoid_check(int max_level)
410{
411 return perf_cap__capable(CAP_SYS_ADMIN) ||
412 perf_event_paranoid() <= max_level;
413}
414
406static int 415static int
407fetch_ubuntu_kernel_version(unsigned int *puint) 416fetch_ubuntu_kernel_version(unsigned int *puint)
408{ 417{