[tip:perf/core] perf machine: Synthesize and process mmap events for x86 PTI entry trampolines

2018-05-23 Thread tip-bot for Adrian Hunter
Commit-ID:  a8ce99b0ee9ad32debad0a9f28d21451ba237cc1
Gitweb: https://git.kernel.org/tip/a8ce99b0ee9ad32debad0a9f28d21451ba237cc1
Author: Adrian Hunter 
AuthorDate: Tue, 22 May 2018 13:54:37 +0300
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Wed, 23 May 2018 10:26:39 -0300

perf machine: Synthesize and process mmap events for x86 PTI entry trampolines

Like the kernel text, the location of x86 PTI entry trampolines must be
recorded in the perf.data file. Like the kernel, synthesize a mmap event
for that, and add processing for it.

Signed-off-by: Adrian Hunter 
Cc: Alexander Shishkin 
Cc: Andi Kleen 
Cc: Andy Lutomirski 
Cc: Dave Hansen 
Cc: H. Peter Anvin 
Cc: Jiri Olsa 
Cc: Joerg Roedel 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Cc: x...@kernel.org
Link: 
http://lkml.kernel.org/r/1526986485-6562-10-git-send-email-adrian.hun...@intel.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/arch/x86/util/Build   |  1 +
 tools/perf/arch/x86/util/event.c | 76 
 tools/perf/util/event.c  | 34 ++
 tools/perf/util/event.h  |  8 +
 tools/perf/util/machine.c| 28 +++
 5 files changed, 140 insertions(+), 7 deletions(-)

diff --git a/tools/perf/arch/x86/util/Build b/tools/perf/arch/x86/util/Build
index aa1ce5f6cc00..844b8f335532 100644
--- a/tools/perf/arch/x86/util/Build
+++ b/tools/perf/arch/x86/util/Build
@@ -5,6 +5,7 @@ libperf-y += kvm-stat.o
 libperf-y += perf_regs.o
 libperf-y += group.o
 libperf-y += machine.o
+libperf-y += event.o
 
 libperf-$(CONFIG_DWARF) += dwarf-regs.o
 libperf-$(CONFIG_BPF_PROLOGUE) += dwarf-regs.o
diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/event.c
new file mode 100644
index ..675a0213044d
--- /dev/null
+++ b/tools/perf/arch/x86/util/event.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+#include 
+#include 
+
+#include "../../util/machine.h"
+#include "../../util/tool.h"
+#include "../../util/map.h"
+#include "../../util/util.h"
+#include "../../util/debug.h"
+
+#if defined(__x86_64__)
+
+int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
+  perf_event__handler_t process,
+  struct machine *machine)
+{
+   int rc = 0;
+   struct map *pos;
+   struct map_groups *kmaps = >kmaps;
+   struct maps *maps = >maps;
+   union perf_event *event = zalloc(sizeof(event->mmap) +
+machine->id_hdr_size);
+
+   if (!event) {
+   pr_debug("Not enough memory synthesizing mmap event "
+"for extra kernel maps\n");
+   return -1;
+   }
+
+   for (pos = maps__first(maps); pos; pos = map__next(pos)) {
+   struct kmap *kmap;
+   size_t size;
+
+   if (!__map__is_extra_kernel_map(pos))
+   continue;
+
+   kmap = map__kmap(pos);
+
+   size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
+  PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
+  machine->id_hdr_size;
+
+   memset(event, 0, size);
+
+   event->mmap.header.type = PERF_RECORD_MMAP;
+
+   /*
+* kernel uses 0 for user space maps, see kernel/perf_event.c
+* __perf_event_mmap
+*/
+   if (machine__is_host(machine))
+   event->header.misc = PERF_RECORD_MISC_KERNEL;
+   else
+   event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
+
+   event->mmap.header.size = size;
+
+   event->mmap.start = pos->start;
+   event->mmap.len   = pos->end - pos->start;
+   event->mmap.pgoff = pos->pgoff;
+   event->mmap.pid   = machine->pid;
+
+   strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
+
+   if (perf_tool__process_synth_event(tool, event, machine,
+  process) != 0) {
+   rc = -1;
+   break;
+   }
+   }
+
+   free(event);
+   return rc;
+}
+
+#endif
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index aafa9878465f..0c8ecf0c78a4 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -88,10 +88,10 @@ static const char *perf_ns__name(unsigned int id)
return perf_ns__names[id];
 }
 
-static int perf_tool__process_synth_event(struct perf_tool *tool,
- union perf_event *event,
-

[tip:perf/core] perf machine: Synthesize and process mmap events for x86 PTI entry trampolines

2018-05-23 Thread tip-bot for Adrian Hunter
Commit-ID:  a8ce99b0ee9ad32debad0a9f28d21451ba237cc1
Gitweb: https://git.kernel.org/tip/a8ce99b0ee9ad32debad0a9f28d21451ba237cc1
Author: Adrian Hunter 
AuthorDate: Tue, 22 May 2018 13:54:37 +0300
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Wed, 23 May 2018 10:26:39 -0300

perf machine: Synthesize and process mmap events for x86 PTI entry trampolines

Like the kernel text, the location of x86 PTI entry trampolines must be
recorded in the perf.data file. Like the kernel, synthesize a mmap event
for that, and add processing for it.

Signed-off-by: Adrian Hunter 
Cc: Alexander Shishkin 
Cc: Andi Kleen 
Cc: Andy Lutomirski 
Cc: Dave Hansen 
Cc: H. Peter Anvin 
Cc: Jiri Olsa 
Cc: Joerg Roedel 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Cc: x...@kernel.org
Link: 
http://lkml.kernel.org/r/1526986485-6562-10-git-send-email-adrian.hun...@intel.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/arch/x86/util/Build   |  1 +
 tools/perf/arch/x86/util/event.c | 76 
 tools/perf/util/event.c  | 34 ++
 tools/perf/util/event.h  |  8 +
 tools/perf/util/machine.c| 28 +++
 5 files changed, 140 insertions(+), 7 deletions(-)

diff --git a/tools/perf/arch/x86/util/Build b/tools/perf/arch/x86/util/Build
index aa1ce5f6cc00..844b8f335532 100644
--- a/tools/perf/arch/x86/util/Build
+++ b/tools/perf/arch/x86/util/Build
@@ -5,6 +5,7 @@ libperf-y += kvm-stat.o
 libperf-y += perf_regs.o
 libperf-y += group.o
 libperf-y += machine.o
+libperf-y += event.o
 
 libperf-$(CONFIG_DWARF) += dwarf-regs.o
 libperf-$(CONFIG_BPF_PROLOGUE) += dwarf-regs.o
diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/event.c
new file mode 100644
index ..675a0213044d
--- /dev/null
+++ b/tools/perf/arch/x86/util/event.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+#include 
+#include 
+
+#include "../../util/machine.h"
+#include "../../util/tool.h"
+#include "../../util/map.h"
+#include "../../util/util.h"
+#include "../../util/debug.h"
+
+#if defined(__x86_64__)
+
+int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
+  perf_event__handler_t process,
+  struct machine *machine)
+{
+   int rc = 0;
+   struct map *pos;
+   struct map_groups *kmaps = >kmaps;
+   struct maps *maps = >maps;
+   union perf_event *event = zalloc(sizeof(event->mmap) +
+machine->id_hdr_size);
+
+   if (!event) {
+   pr_debug("Not enough memory synthesizing mmap event "
+"for extra kernel maps\n");
+   return -1;
+   }
+
+   for (pos = maps__first(maps); pos; pos = map__next(pos)) {
+   struct kmap *kmap;
+   size_t size;
+
+   if (!__map__is_extra_kernel_map(pos))
+   continue;
+
+   kmap = map__kmap(pos);
+
+   size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
+  PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
+  machine->id_hdr_size;
+
+   memset(event, 0, size);
+
+   event->mmap.header.type = PERF_RECORD_MMAP;
+
+   /*
+* kernel uses 0 for user space maps, see kernel/perf_event.c
+* __perf_event_mmap
+*/
+   if (machine__is_host(machine))
+   event->header.misc = PERF_RECORD_MISC_KERNEL;
+   else
+   event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
+
+   event->mmap.header.size = size;
+
+   event->mmap.start = pos->start;
+   event->mmap.len   = pos->end - pos->start;
+   event->mmap.pgoff = pos->pgoff;
+   event->mmap.pid   = machine->pid;
+
+   strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
+
+   if (perf_tool__process_synth_event(tool, event, machine,
+  process) != 0) {
+   rc = -1;
+   break;
+   }
+   }
+
+   free(event);
+   return rc;
+}
+
+#endif
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index aafa9878465f..0c8ecf0c78a4 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -88,10 +88,10 @@ static const char *perf_ns__name(unsigned int id)
return perf_ns__names[id];
 }
 
-static int perf_tool__process_synth_event(struct perf_tool *tool,
- union perf_event *event,
- struct machine *machine,
- perf_event__handler_t process)
+int perf_tool__process_synth_event(struct perf_tool *tool,
+  union perf_event *event,
+  struct