[PATCH v1 8/8] perf/x86: Add event owner check when PEBS output to Intel PT

From: Luwei Kang
Date: Sun Oct 27 2019 - 07:12:51 EST

For PEBS output to Intel PT, a Intel PT event should be the group
leader of an PEBS counter event in host. For Intel PT
virtualization enabling in KVM guest, the PT facilities will be
passthrough to guest and do not allocate PT event from host perf
event framework. This is different with PMU virtualization.

Intel new hardware feature that can make PEBS enabled in KVM guest
by output PEBS records to Intel PT buffer. KVM need to allocate
a event counter for this PEBS event without Intel PT event leader.

This patch add event owner check for PEBS output to PT event that
only non-kernel event need group leader(PT).

Signed-off-by: Luwei Kang <luwei.kang@xxxxxxxxx>
arch/x86/events/core.c | 3 ++-
include/linux/perf_event.h | 1 +
kernel/events/core.c | 2 +-
3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 7b21455..214041a 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -1014,7 +1014,8 @@ static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader,
* away, the group was broken down and this singleton event
* can't schedule any more.
- if (is_pebs_pt(leader) && !leader->aux_event)
+ if (is_pebs_pt(leader) && !leader->aux_event &&
+ !is_kernel_event(leader))
return -EINVAL;

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 61448c1..22ef4b0 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -928,6 +928,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
extern u64 perf_event_read_value(struct perf_event *event,
u64 *enabled, u64 *running);

+extern bool is_kernel_event(struct perf_event *event);

struct perf_sample_data {
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 9ec0b0b..00f943b 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -166,7 +166,7 @@ static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,

#define TASK_TOMBSTONE ((void *)-1L)

-static bool is_kernel_event(struct perf_event *event)
+bool is_kernel_event(struct perf_event *event)
return READ_ONCE(event->owner) == TASK_TOMBSTONE;