Skip to content

Commit 83bc9c3

Browse files
committed
perf trace beauty: Beautify pkey_{alloc,free,mprotect} arguments
Reuse 'mprotect' beautifiers for 'pkey_mprotect'. System wide tracing pkey_alloc, pkey_free and pkey_mprotect calls, with backtraces: # perf trace -e pkey_alloc,pkey_mprotect,pkey_free --max-stack=5 0.000 ( 0.011 ms): pkey/7818 pkey_alloc(init_val: DISABLE_ACCESS|DISABLE_WRITE) = -1 EINVAL Invalid argument syscall (/usr/lib64/libc-2.25.so) pkey_alloc (/home/acme/c/pkey) 0.022 ( 0.003 ms): pkey/7818 pkey_mprotect(start: 0x7f28c3890000, len: 4096, prot: READ|WRITE, pkey: -1) = 0 syscall (/usr/lib64/libc-2.25.so) pkey_mprotect (/home/acme/c/pkey) 0.030 ( 0.002 ms): pkey/7818 pkey_free(pkey: -1 ) = -1 EINVAL Invalid argument syscall (/usr/lib64/libc-2.25.so) pkey_free (/home/acme/c/pkey) The tools/include/uapi/asm-generic/mman-common.h file is used to find the access rights defines for the pkey_alloc syscall second argument. Since we have the detector of changes for the tools/include header files versus its kernel origin (include/uapi/asm-generic/mman-common.h), we'll get whatever new flag appears for that argument automatically. This method should be used in other cases where it is easy to generate those flags tables because the header has properly namespaced defines like PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE. Cc: Adrian Hunter <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Dave Hansen <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent a2105f8 commit 83bc9c3

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

tools/perf/Makefile.perf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ export INSTALL SHELL_PATH
387387

388388
SHELL = $(SHELL_PATH)
389389

390-
beauty_ioctl_outdir := $(OUTPUT)trace/beauty/generated/ioctl
390+
beauty_outdir := $(OUTPUT)trace/beauty/generated
391+
beauty_ioctl_outdir := $(beauty_outdir)/ioctl
391392
drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
392393
drm_hdr_dir := $(srctree)/tools/include/uapi/drm
393394
drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
@@ -398,6 +399,13 @@ _dummy := $(shell [ -d '$(beauty_ioctl_outdir)' ] || mkdir -p '$(beauty_ioctl_ou
398399
$(drm_ioctl_array): $(drm_hdr_dir)/drm.h $(drm_hdr_dir)/i915_drm.h $(drm_ioctl_tbl)
399400
$(Q)$(SHELL) '$(drm_ioctl_tbl)' $(drm_hdr_dir) > $@
400401

402+
pkey_alloc_access_rights_array := $(beauty_outdir)/pkey_alloc_access_rights_array.c
403+
asm_generic_hdr_dir := $(srctree)/tools/include/uapi/asm-generic/
404+
pkey_alloc_access_rights_tbl := $(srctree)/tools/perf/trace/beauty/pkey_alloc_access_rights.sh
405+
406+
$(pkey_alloc_access_rights_array): $(asm_generic_hdr_dir)/mman-common.h $(pkey_alloc_access_rights_tbl)
407+
$(Q)$(SHELL) '$(pkey_alloc_access_rights_tbl)' $(asm_generic_hdr_dir) > $@
408+
401409
sndrv_ctl_ioctl_array := $(beauty_ioctl_outdir)/sndrv_ctl_ioctl_array.c
402410
sndrv_ctl_hdr_dir := $(srctree)/tools/include/uapi/sound
403411
sndrv_ctl_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/sndrv_ctl_ioctl.sh
@@ -528,6 +536,7 @@ __build-dir = $(subst $(OUTPUT),,$(dir $@))
528536
build-dir = $(if $(__build-dir),$(__build-dir),.)
529537

530538
prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioctl_array) \
539+
$(pkey_alloc_access_rights_array) \
531540
$(sndrv_pcm_ioctl_array) \
532541
$(sndrv_ctl_ioctl_array) \
533542
$(kvm_ioctl_array) \
@@ -803,6 +812,7 @@ clean:: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clea
803812
$(OUTPUT)tests/llvm-src-{base,kbuild,prologue,relocation}.c \
804813
$(OUTPUT)pmu-events/pmu-events.c \
805814
$(OUTPUT)$(drm_ioctl_array) \
815+
$(OUTPUT)$(pkey_alloc_access_rights_array) \
806816
$(OUTPUT)$(sndrv_ctl_ioctl_array) \
807817
$(OUTPUT)$(sndrv_pcm_ioctl_array) \
808818
$(OUTPUT)$(kvm_ioctl_array) \

tools/perf/builtin-trace.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ static struct syscall_fmt {
693693
[4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
694694
{ .name = "pipe2",
695695
.arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
696+
{ .name = "pkey_alloc",
697+
.arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
698+
{ .name = "pkey_free",
699+
.arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
700+
{ .name = "pkey_mprotect",
701+
.arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
702+
[2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
703+
[3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
696704
{ .name = "poll", .timeout = true, },
697705
{ .name = "ppoll", .timeout = true, },
698706
{ .name = "pread", .alias = "pread64", },

tools/perf/trace/beauty/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ libperf-y += fcntl.o
33
ifeq ($(SRCARCH),$(filter $(SRCARCH),x86))
44
libperf-y += ioctl.o
55
endif
6+
libperf-y += pkey_alloc.o
67
libperf-y += statx.o

tools/perf/trace/beauty/beauty.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ size_t syscall_arg__scnprintf_fcntl_arg(char *bf, size_t size, struct syscall_ar
7878
size_t syscall_arg__scnprintf_ioctl_cmd(char *bf, size_t size, struct syscall_arg *arg);
7979
#define SCA_IOCTL_CMD syscall_arg__scnprintf_ioctl_cmd
8080

81+
size_t syscall_arg__scnprintf_pkey_alloc_access_rights(char *bf, size_t size, struct syscall_arg *arg);
82+
#define SCA_PKEY_ALLOC_ACCESS_RIGHTS syscall_arg__scnprintf_pkey_alloc_access_rights
83+
8184
size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size, struct syscall_arg *arg);
8285
#define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
8386

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* trace/beauty/pkey_alloc.c
3+
*
4+
* Copyright (C) 2017, Red Hat Inc, Arnaldo Carvalho de Melo <[email protected]>
5+
*
6+
* Released under the GPL v2. (and only v2, not any later version)
7+
*/
8+
9+
#include "trace/beauty/beauty.h"
10+
#include <linux/kernel.h>
11+
#include <linux/log2.h>
12+
13+
static size_t pkey_alloc__scnprintf_access_rights(int access_rights, char *bf, size_t size)
14+
{
15+
int i, printed = 0;
16+
17+
#include "trace/beauty/generated/pkey_alloc_access_rights_array.c"
18+
static DEFINE_STRARRAY(pkey_alloc_access_rights);
19+
20+
if (access_rights == 0) {
21+
const char *s = strarray__pkey_alloc_access_rights.entries[0];
22+
if (s)
23+
return scnprintf(bf, size, "%s", s);
24+
return scnprintf(bf, size, "%d", 0);
25+
}
26+
27+
for (i = 1; i < strarray__pkey_alloc_access_rights.nr_entries; ++i) {
28+
int bit = 1 << (i - 1);
29+
30+
if (!(access_rights & bit))
31+
continue;
32+
33+
if (printed != 0)
34+
printed += scnprintf(bf + printed, size - printed, "|");
35+
36+
if (strarray__pkey_alloc_access_rights.entries[i] != NULL)
37+
printed += scnprintf(bf + printed, size - printed, "%s", strarray__pkey_alloc_access_rights.entries[i]);
38+
else
39+
printed += scnprintf(bf + printed, size - printed, "0x%#", bit);
40+
}
41+
42+
return printed;
43+
}
44+
45+
size_t syscall_arg__scnprintf_pkey_alloc_access_rights(char *bf, size_t size, struct syscall_arg *arg)
46+
{
47+
unsigned long cmd = arg->val;
48+
49+
return pkey_alloc__scnprintf_access_rights(cmd, bf, size);
50+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
header_dir=$1
4+
5+
printf "static const char *pkey_alloc_access_rights[] = {\n"
6+
regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+PKEY_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*'
7+
egrep $regex ${header_dir}/mman-common.h | \
8+
sed -r "s/$regex/\2 \2 \1/g" | \
9+
sort | xargs printf "\t[%s ? (ilog2(%s) + 1) : 0] = \"%s\",\n"
10+
printf "};\n"

0 commit comments

Comments
 (0)