Skip to contentSkip to conversation

Various perf patches

PATCH In review Peter Zijlstra 9 participants 42 messages May 3, 2013 – May 22, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Various perf patches

Some perf patches fixing things reported by Andi.

#1 lore raw

PATCH 1/3 perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Errata BV98 states that all MEM_*_RETIRED events corrupt the counter value of the SMT sibling's counters. Blacklist these events

---
 arch/x86/kernel/cpu/perf_event_intel.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel.c +9 -4
@@ -128,10 +128,15 @@ static struct event_constraint intel_ivb
 	INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
 	INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
 	INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
-	INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
-	INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
-	INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
-	INTEL_EVENT_CONSTRAINT(0xd3, 0xf), /*  MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
+	/*
+	 * Errata BV98 -- MEM_*_RETIRED events can leak between counters of SMT
+	 * siblings; disable these events because they can corrupt unrelated
+	 * counters.
+	 */
+	INTEL_EVENT_CONSTRAINT(0xd0, 0x0), /* MEM_UOPS_RETIRED.* */
+	INTEL_EVENT_CONSTRAINT(0xd1, 0x0), /* MEM_LOAD_UOPS_RETIRED.* */
+	INTEL_EVENT_CONSTRAINT(0xd2, 0x0), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
+	INTEL_EVENT_CONSTRAINT(0xd3, 0x0), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
 	EVENT_CONSTRAINT_END
 };
 
#2 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Quoted 2 lines · Peter Zijlstra

Errata BV98 states that all MEM_*_RETIRED events corrupt the counter value of the SMT sibling's counters. Blacklist these events

I disagree with this patch. This is just overkill and not needed at all.

-Andi

maintainer
#3 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Quoted 6 lines · Andi Kleen
Quoted 2 lines · Peter Zijlstra

Errata BV98 states that all MEM_*_RETIRED events corrupt the counter value of the SMT sibling's counters. Blacklist these events

I disagree with this patch. This is just overkill and not needed at all.

Then give me a patch that both completely describes the problem and ensures isolation so that no counters get corrupted (and isn't too invasive).

So far you've failed on both counts.

#4 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Hi,

Based on our testing, it appears the corruption occurs only when the MEM_* events are used and only on the sibling counter. In other words, if HT0 has MEM_* in cntr0, then HT1 cntr0 cannot be used, otherwise whatever is there may get corrupted. So I think we could enhance Andi's initial patch to handle this case instead of blacklist those events. They are very important events.

Quoted 14 lines · Peter Zijlstra
Quoted 6 lines · Andi Kleen
Quoted 2 lines · Peter Zijlstra

Errata BV98 states that all MEM_*_RETIRED events corrupt the counter value of the SMT sibling's counters. Blacklist these events

I disagree with this patch. This is just overkill and not needed at all.

Then give me a patch that both completely describes the problem and ensures isolation so that no counters get corrupted (and isn't too invasive).

So far you've failed on both counts.

maintainer
#5 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Quoted 7 lines · Stephane Eranian

Hi,

Based on our testing, it appears the corruption occurs only when the MEM_* events are used and only on the sibling counter. In other words, if HT0 has MEM_* in cntr0, then HT1 cntr0 cannot be used, otherwise whatever is there may get corrupted.

Ah, great. That is the least horrid case :-)

Quoted 3 lines

So I think we could enhance Andi's initial patch
to handle this case instead of blacklist those events. They are very important events.

Will you take a stab at it? I suppose you'll have to make each counter have a shared resource and have the mem_*_retired events mark the resource taken.

Also, did you test what happens when you turn SMT off in the BIOS so you get double the amount of counters; do we then leak into CNTn+4 or is all well again?

#6 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Stephane Eranian

Hi,

Based on our testing, it appears the corruption occurs only when the MEM_* events are used and only on the sibling counter. In other words, if HT0 has MEM_* in cntr0, then HT1 cntr0 cannot be used, otherwise whatever is there may get corrupted.

Ah, great. That is the least horrid case :-)

Yeah.

Quoted 8 lines
Quoted 3 lines

So I think we could enhance Andi's initial patch
to handle this case instead of blacklist those events. They are very important events.

Will you take a stab at it? I suppose you'll have to make each counter have a shared resource and have the mem_*_retired events mark the resource taken.

Yes, I will try to come up with a patch to force multiplexing when those events are used.

The difficulty is that we cannot use the shared_regs infrastructure because the constraint is quite different. Here we need to say that if a mem event uses cnt0 on one thread, then nothing can be using cnt0 on the other thread. The current shared_regs infrastructure does not work on the counter level. So we need to invent something else. It cannot even be extended. We need to schedule the mem event, then look at the assignment and exclude all the counters used by the mem event from the other thread. Alternatively, we can only schedule mem event on counters which are not currently used by the sibling thread. We need to peek at the used counters cross HT threads. Not so easy to do.

Quoted 4 lines

Also, did you test what happens when you turn SMT off in the BIOS so you get double the amount of counters; do we then leak into CNTn+4 or is all well again?

I have not tried that yet. Hopefully, it won't exhibit the problem.

#7 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Quoted 5 lines

The difficulty is that we cannot use the shared_regs infrastructure because the constraint is quite different. Here we need to say that if a mem event uses cnt0 on one thread, then nothing can be using cnt0 on the other thread. The current shared_regs infrastructure does not work on the counter level. So we need to invent something else. It cannot even be extended.

Just need a extra reg per counter, that is only triggered with that event.

-Andi

#8 lore raw

Re: [PATCH 1/3] perf, x86: Blacklist all MEM_*_RETIRED events for IVB

Quoted 8 lines · Andi Kleen
Quoted 5 lines

The difficulty is that we cannot use the shared_regs infrastructure because the constraint is quite different. Here we need to say that if a mem event uses cnt0 on one thread, then nothing can be using cnt0 on the other thread. The current shared_regs infrastructure does not work on the counter level. So we need to invent something else. It cannot even be extended.

Just need a extra reg per counter, that is only triggered with that event.

Yes, it would be per counter and not per-event. But the check has to happen after the assignment and each time we release the counter.

Quoted 1 line

-Andi

#9 lore raw

[tip:perf/urgent] perf/x86: Blacklist all MEM_*_RETIRED events for Ivy Bridge

In reply to Peter Zijlstra
perf/x86: Blacklist all MEM_*_RETIRED events for Ivy Bridge
Commit-ID:  741a698f420c34c458294a6accecfbad702a7c52
Gitweb:     http://git.kernel.org/tip/741a698f420c34c458294a6accecfbad702a7c52
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 3 May 2013 14:11:23 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 4 May 2013 08:37:46 +0200

perf/x86: Blacklist all MEM_*_RETIRED events for Ivy Bridge

Errata BV98 states that all MEM_*_RETIRED events corrupt the counter value of the SMT sibling's counters. Blacklist these events

---
 arch/x86/kernel/cpu/perf_event_intel.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel.c +9 -4
index cc45deb..4a0a462 100644
@@ -125,10 +125,15 @@ static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
 	INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
 	INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
 	INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
-	INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
-	INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
-	INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
-	INTEL_EVENT_CONSTRAINT(0xd3, 0xf), /*  MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
+	/*
+	 * Errata BV98 -- MEM_*_RETIRED events can leak between counters of SMT
+	 * siblings; disable these events because they can corrupt unrelated
+	 * counters.
+	 */
+	INTEL_EVENT_CONSTRAINT(0xd0, 0x0), /* MEM_UOPS_RETIRED.* */
+	INTEL_EVENT_CONSTRAINT(0xd1, 0x0), /* MEM_LOAD_UOPS_RETIRED.* */
+	INTEL_EVENT_CONSTRAINT(0xd2, 0x0), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
+	INTEL_EVENT_CONSTRAINT(0xd3, 0x0), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
 	EVENT_CONSTRAINT_END
 };
 
#10 lore raw

PATCH 2/3 perf, x86, lbr: Fix LBR filter

In reply to Peter Zijlstra

The LBR 'from' adddress is under full userspace control; ensure we validate it before reading from it.

Note: is_module_text_address() can potentially be quite expensive;
for those running into that either stop using modules as all sane people do or optimize it using an RCU backed rb-tree.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +12 -2
@@ -442,8 +442,18 @@ static int branch_type(unsigned long fro
 			return X86_BR_NONE;
 
 		addr = buf;
-	} else
-		addr = (void *)from;
+	} else {
+		/*
+		 * The LBR logs any address in the IP, even if the IP just
+		 * faulted. This means userspace can control the from address.
+		 * Ensure we don't blindy read any address by validating it is
+		 * a known text address.
+		 */
+		if (kernel_text_address(from))
+			addr = (void *)from;
+		else
+			return X86_BR_NONE;
+	}
 
 	/*
 	 * decoder needs to know the ABI especially
#11 lore raw

Re: [PATCH 2/3] perf, x86, lbr: Fix LBR filter

arch/x86/kernel/cpu/perf_event_intel_lbr.c static int branch_type(unsigned long fro · line 452

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +8 -0
+	} else {
+		/*
+		 * The LBR logs any address in the IP, even if the IP just
+		 * faulted. This means userspace can control the from address.
+		 * Ensure we don't blindy read any address by validating it is
+		 * a known text address.
+		 */
+		if (kernel_text_address(from))

Sorry doing it this way is just incredible expensive and dumb.

After all the tool is called "perf" and not "non perf"

-Andi

#12 lore raw

Re: [PATCH 2/3] perf, x86, lbr: Fix LBR filter

Quoted 10 lines · Andi Kleen

arch/x86/kernel/cpu/perf_event_intel_lbr.c static int branch_type(unsigned long fro · line 452

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +8 -0
+	} else {
+		/*
+		 * The LBR logs any address in the IP, even if the IP just
+		 * faulted. This means userspace can control the from address.
+		 * Ensure we don't blindy read any address by validating it is
+		 * a known text address.
+		 */
+		if (kernel_text_address(from))

Sorry doing it this way is just incredible expensive and dumb.

If anyone using this feature notices the __module_address() overhead then a 'module addresses RCU rbtree' could be added, which should solve the overhead impact.

In any case Peter's patch fixes the bug without regressing the feature as it is implemented today. Do you have a better solution that does not break the ABI? The solution you proposed before regresses existing functionality.

Thanks,

Ingo

#13 lore raw

[tip:perf/urgent] perf/x86/intel/lbr: Fix LBR filter

In reply to Peter Zijlstra
perf/x86/intel/lbr: Fix LBR filter
Commit-ID:  6e15eb3ba6c0249c9e8c783517d131b47db995ca
Gitweb:     http://git.kernel.org/tip/6e15eb3ba6c0249c9e8c783517d131b47db995ca
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 3 May 2013 14:11:24 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 4 May 2013 08:37:47 +0200

perf/x86/intel/lbr: Fix LBR filter

The LBR 'from' adddress is under full userspace control; ensure we validate it before reading from it.

Note: is_module_text_address() can potentially be quite expensive; for those running into that with high overhead in modules optimize it using an RCU backed rb-tree.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +12 -2
index da02e9c..de341d4 100644
@@ -442,8 +442,18 @@ static int branch_type(unsigned long from, unsigned long to)
 			return X86_BR_NONE;
 
 		addr = buf;
-	} else
-		addr = (void *)from;
+	} else {
+		/*
+		 * The LBR logs any address in the IP, even if the IP just
+		 * faulted. This means userspace can control the from address.
+		 * Ensure we don't blindy read any address by validating it is
+		 * a known text address.
+		 */
+		if (kernel_text_address(from))
+			addr = (void *)from;
+		else
+			return X86_BR_NONE;
+	}
 
 	/*
 	 * decoder needs to know the ABI especially
#14 lore raw

PATCH 3/3 perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;
 		mask |= X86_BR_KERNEL;
+	}
 
 	/* we ignore BRANCH_HV here */
 
Acked-by on v1 3/3
#15 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 3 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

Cc Andi Kleen <[email protected]>
Quoted 17 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;
 		mask |= X86_BR_KERNEL;
+	}
 
 	/* we ignore BRANCH_HV here */
 
Signature · 1 line
[email protected] -- Speaking for myself only
#16 lore raw

[tip:perf/urgent] perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Peter Zijlstra
perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL
Commit-ID:  0f5c78b5f33ce940034743e5f9485fc81ad75b0f
Gitweb:     http://git.kernel.org/tip/0f5c78b5f33ce940034743e5f9485fc81ad75b0f
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 3 May 2013 14:11:25 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 4 May 2013 08:37:48 +0200

perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
index de341d4..0e92871 100644
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;
 		mask |= X86_BR_KERNEL;
+	}
 
 	/* we ignore BRANCH_HV here */
 
maintainer
#17 lore raw

Re: [tip:perf/urgent] perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 28 lines · tip-bot for Peter Zijlstra
Commit-ID:  0f5c78b5f33ce940034743e5f9485fc81ad75b0f
Gitweb:     http://git.kernel.org/tip/0f5c78b5f33ce940034743e5f9485fc81ad75b0f
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 3 May 2013 14:11:25 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 4 May 2013 08:37:48 +0200

perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +0 -0
index de341d4..0e92871 100644

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event) · line 323

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +3 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;

It is probably not too late to amend this patch and remove the "-EACCES":

arch/x86/kernel/cpu/perf_event_intel_lbr.c: In function ‘intel_pmu_setup_sw_lbr_filter’:
arch/x86/kernel/cpu/perf_event_intel_lbr.c:323:4: warning: ‘return’ with a value, in function returning void [enabled by default]

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#18 lore raw

Re: [tip:perf/urgent] perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 39 lines · Borislav Petkov
Quoted 28 lines · tip-bot for Peter Zijlstra
Commit-ID:  0f5c78b5f33ce940034743e5f9485fc81ad75b0f
Gitweb:     http://git.kernel.org/tip/0f5c78b5f33ce940034743e5f9485fc81ad75b0f
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 3 May 2013 14:11:25 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sat, 4 May 2013 08:37:48 +0200

perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +0 -0
index de341d4..0e92871 100644

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event) · line 323

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +3 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;

It is probably not too late to amend this patch and remove the "-EACCES":

Quoted 2 lines

arch/x86/kernel/cpu/perf_event_intel_lbr.c: In function ???intel_pmu_setup_sw_lbr_filter???:
arch/x86/kernel/cpu/perf_event_intel_lbr.c:323:4: warning: ???return??? with a value, in function returning void [enabled by default]

Well, the better fix is to propagate that error condition down instead of ignoring it.

Since it was the head commit for a tree others don't typically pull and base development work off I amended it.

Thanks,

Ingo

maintainer
#19 lore raw

Re: [tip:perf/urgent] perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Borislav Petkov
Quoted 11 lines

Review static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event) · line 323

Quoted 8 lines
(quoted diff) +3 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;

It is probably not too late to amend this patch and remove the "-EACCES":

Quoted 2 lines

arch/x86/kernel/cpu/perf_event_intel_lbr.c: In function ‘intel_pmu_setup_sw_lbr_filter’:
arch/x86/kernel/cpu/perf_event_intel_lbr.c:323:4: warning: ‘return’ with a value, in function returning void [enabled by default]

Oh urgh, looks like I forgot a refresh before posting..

This one actually compiles a defconfig bzImage.

---
Subject: perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL
From: Peter Zijlstra <[email protected]>
Date: Fri May 03 14:07:49 CEST 2013

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +11 -4
@@ -310,7 +310,7 @@ void intel_pmu_lbr_read(void)
  * - in case there is no HW filter
  * - in case the HW filter has errata or limitations
  */
-static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
+static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 {
 	u64 br_type = event->attr.branch_sample_type;
 	int mask = 0;
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;
 		mask |= X86_BR_KERNEL;
+	}
 
 	/* we ignore BRANCH_HV here */
 
@@ -339,6 +342,8 @@ static void intel_pmu_setup_sw_lbr_filte
 	 * be used by fixup code for some CPU
 	 */
 	event->hw.branch_reg.reg = mask;
+
+	return 0;
 }
 
 /*
@@ -375,7 +380,7 @@ static int intel_pmu_setup_hw_lbr_filter
 
 int intel_pmu_setup_lbr_filter(struct perf_event *event)
 {
-	int ret = 0;
+	int ret;
 
 	/*
 	 * no LBR on this PMU
@@ -386,7 +391,9 @@ int intel_pmu_setup_lbr_filter(struct pe
 	/*
 	 * setup SW LBR filter
 	 */
-	intel_pmu_setup_sw_lbr_filter(event);
+	ret = intel_pmu_setup_sw_lbr_filter(event);
+	if (ret)
+		return ret;
 
 	/*
 	 * setup HW LBR filter, if any
#20 lore raw

Re: [tip:perf/urgent] perf/x86/intel/lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 76 lines · Peter Zijlstra
Quoted 11 lines

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event) · line 323

Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +3 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;

It is probably not too late to amend this patch and remove the "-EACCES":

Quoted 2 lines

arch/x86/kernel/cpu/perf_event_intel_lbr.c: In function ???intel_pmu_setup_sw_lbr_filter???:
arch/x86/kernel/cpu/perf_event_intel_lbr.c:323:4: warning: ???return??? with a value, in function returning void [enabled by default]

Oh urgh, looks like I forgot a refresh before posting..

This one actually compiles a defconfig bzImage.

---
Subject: perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL
From: Peter Zijlstra <[email protected]>
Date: Fri May 03 14:07:49 CEST 2013

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +8 -3
@@ -310,7 +310,7 @@ void intel_pmu_lbr_read(void)
  * - in case there is no HW filter
  * - in case the HW filter has errata or limitations
  */
-static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
+static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
 {
 	u64 br_type = event->attr.branch_sample_type;
 	int mask = 0;
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
 	if (br_type & PERF_SAMPLE_BRANCH_USER)
 		mask |= X86_BR_USER;
 
-	if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+	if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+			return -EACCES;
 		mask |= X86_BR_KERNEL;
+	}
 
 	/* we ignore BRANCH_HV here */
 
@@ -339,6 +342,8 @@ static void intel_pmu_setup_sw_lbr_filte
 	 * be used by fixup code for some CPU
 	 */
 	event->hw.branch_reg.reg = mask;
+
+	return 0;
 }
 
 /*
@@ -375,7 +380,7 @@ static int intel_pmu_setup_hw_lbr_filter
 
 int intel_pmu_setup_lbr_filter(struct perf_event *event)
 {
-	int ret = 0;
+	int ret;
 
 	/*
 	 * no LBR on this PMU

arch/x86/kernel/cpu/perf_event_intel_lbr.c int intel_pmu_setup_lbr_filter(struct pe · line 399

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +3 -1
@@ -386,7 +391,9 @@ int intel_pmu_setup_lbr_filter(struct pe
 	/*
 	 * setup SW LBR filter
 	 */
-	intel_pmu_setup_sw_lbr_filter(event);
+	ret = intel_pmu_setup_sw_lbr_filter(event);
+	if (ret)
+		return ret;
 
 	/*
 	 * setup HW LBR filter, if any

That looks pretty close to what I did as well.

Thanks,

Ingo

#21 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

When using LBR priv level filtering, the filter applies to the branch target only.

maintainer
#22 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

Ah, indeed. I'll try and whip up a patch.

maintainer
#23 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Stephane Eranian
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 12 +++++++++---
 include/linux/perf_event.h                 | 10 +++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +9 -3
index d978353..f44d635 100644
@@ -585,17 +585,23 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
 
 		/* if type does not correspond, then discard */
 		if (type == X86_BR_NONE || (br_sel & type) != type) {
-			cpuc->lbr_entries[i].from = 0;
+			cpuc->lbr_entries[i].__delete = 1;
 			compress = true;
 		}
+
+		/* hide kernel addresses if we're not privileged  */
+		if (!(br_sel & X86_BR_KERNEL) && kernel_ip(from)) {
+			cpuc->lbr_entries[i].from = -1L;
+			cpuc->lbr_entries[i].invalid_from = 1;
+		}
 	}
 
 	if (!compress)
 		return;
 
-	/* remove all entries with from=0 */
+	/* remove all entries with __delete */
 	for (i = 0; i < cpuc->lbr_stack.nr; ) {
-		if (!cpuc->lbr_entries[i].from) {
+		if (cpuc->lbr_entries[i].__delete) {
 			j = i;
 			while (++j < cpuc->lbr_stack.nr)
 				cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
include/linux/perf_event.h +7 -3
index f463a46..7acf1c9 100644
@@ -77,9 +77,13 @@ struct perf_raw_record {
 struct perf_branch_entry {
 	__u64	from;
 	__u64	to;
-	__u64	mispred:1,  /* target mispredicted */
-		predicted:1,/* target predicted */
-		reserved:62;
+	__u64	mispred:1,	/* target mispredicted		*/
+		predicted:1,	/* target predicted		*/
+		invalid_to:1,	/* @to isn't to be trusted	*/
+		invalid_from:1, /* @from isn't to be trusted	*/
+		reserved:59,
+		__delete:1;	/* Implementation; userspace should
+				   always see a 0 		*/
 };
 
 /*
maintainer
#24 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 2 lines · Peter Zijlstra

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

The asymmetry is unfortunate, but I think its more useful to have the from kernel branch target than it is not to have it. This way you at least know there was a kernel entry/exit and where you've continued.

Without either branch to kernel and branch from kernel entries you'd be wondering WTF happend to your control flow.

Quoted 58 lines
---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 12 +++++++++---
 include/linux/perf_event.h                 | 10 +++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +9 -3
index d978353..f44d635 100644
@@ -585,17 +585,23 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
 
 		/* if type does not correspond, then discard */
 		if (type == X86_BR_NONE || (br_sel & type) != type) {
-			cpuc->lbr_entries[i].from = 0;
+			cpuc->lbr_entries[i].__delete = 1;
 			compress = true;
 		}
+
+		/* hide kernel addresses if we're not privileged  */
+		if (!(br_sel & X86_BR_KERNEL) && kernel_ip(from)) {
+			cpuc->lbr_entries[i].from = -1L;
+			cpuc->lbr_entries[i].invalid_from = 1;
+		}
 	}
 
 	if (!compress)
 		return;
 
-	/* remove all entries with from=0 */
+	/* remove all entries with __delete */
 	for (i = 0; i < cpuc->lbr_stack.nr; ) {
-		if (!cpuc->lbr_entries[i].from) {
+		if (cpuc->lbr_entries[i].__delete) {
 			j = i;
 			while (++j < cpuc->lbr_stack.nr)
 				cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
include/linux/perf_event.h +7 -3
index f463a46..7acf1c9 100644
@@ -77,9 +77,13 @@ struct perf_raw_record {
 struct perf_branch_entry {
 	__u64	from;
 	__u64	to;
-	__u64	mispred:1,  /* target mispredicted */
-		predicted:1,/* target predicted */
-		reserved:62;
+	__u64	mispred:1,	/* target mispredicted		*/
+		predicted:1,	/* target predicted		*/
+		invalid_to:1,	/* @to isn't to be trusted	*/
+		invalid_from:1, /* @from isn't to be trusted	*/
+		reserved:59,
+		__delete:1;	/* Implementation; userspace should
+				   always see a 0 		*/
 };
 
 /*
#25 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Peter Zijlstra
Quoted 77 lines · Peter Zijlstra
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c | 12 +++++++++---
 include/linux/perf_event.h                 | 10 +++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_lbr.c +9 -3
index d978353..f44d635 100644
@@ -585,17 +585,23 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
 
 		/* if type does not correspond, then discard */
 		if (type == X86_BR_NONE || (br_sel & type) != type) {
-			cpuc->lbr_entries[i].from = 0;
+			cpuc->lbr_entries[i].__delete = 1;
 			compress = true;
 		}
+
+		/* hide kernel addresses if we're not privileged  */
+		if (!(br_sel & X86_BR_KERNEL) && kernel_ip(from)) {
+			cpuc->lbr_entries[i].from = -1L;
+			cpuc->lbr_entries[i].invalid_from = 1;
+		}
 	}
 
 	if (!compress)
 		return;
 
-	/* remove all entries with from=0 */
+	/* remove all entries with __delete */
 	for (i = 0; i < cpuc->lbr_stack.nr; ) {
-		if (!cpuc->lbr_entries[i].from) {
+		if (cpuc->lbr_entries[i].__delete) {
 			j = i;
 			while (++j < cpuc->lbr_stack.nr)
 				cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
include/linux/perf_event.h +0 -0
index f463a46..7acf1c9 100644

include/linux/perf_event.h struct perf_raw_record { · line 83

Quoted 10 lines
include/linux/perf_event.h +4 -3
@@ -77,9 +77,13 @@ struct perf_raw_record {
 struct perf_branch_entry {
 	__u64	from;
 	__u64	to;
-	__u64	mispred:1,  /* target mispredicted */
-		predicted:1,/* target predicted */
-		reserved:62;
+	__u64	mispred:1,	/* target mispredicted		*/
+		predicted:1,	/* target predicted		*/
+		invalid_to:1,	/* @to isn't to be trusted	*/
+		invalid_from:1, /* @from isn't to be trusted	*/

Thanks Peter. One possible issue...

When the kernel has to read the branch from memory, there is no way for it to know that it's the same one that the HW actually executed. Hence there's a possibility that the to address is invalid but we can't tell for sure.

I'm happy to just ignore that and mark calculated to address as valid, unless you think it would be worthwhile extra information to pass onto the user?

If we wanted this extra fidelity we could add a possibly_invalid_to:1 flag to your patch but I'm not sure it's worth it to be honest.

mikey

#26 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Peter Zijlstra
Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 5 lines
Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Mikey

include/uapi/linux/perf_event.h +2 -1
index fb104e5..891c769 100644
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
 	PERF_SAMPLE_BRANCH_ANY_CALL	= 1U << 4, /* any call branch */
 	PERF_SAMPLE_BRANCH_ANY_RETURN	= 1U << 5, /* any return branch */
 	PERF_SAMPLE_BRANCH_IND_CALL	= 1U << 6, /* indirect calls */
+	PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 
-	PERF_SAMPLE_BRANCH_MAX		= 1U << 7, /* non-ABI */
+	PERF_SAMPLE_BRANCH_MAX		= 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +1 -0
index cdf58ec..5b0b89d 100644
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
 	BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
 	BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
 	BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+	BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),
 	BRANCH_END
 };
 
maintainer
#27 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 5 lines · Michael Neuling

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

I don't see an immediate problem with that except that we on x86 need to implement that in the software filter. Stephane do you see any fundamental issue with that?

Quoted 28 lines
include/uapi/linux/perf_event.h +2 -1
index fb104e5..891c769 100644
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
 	PERF_SAMPLE_BRANCH_ANY_CALL	= 1U << 4, /* any call branch */
 	PERF_SAMPLE_BRANCH_ANY_RETURN	= 1U << 5, /* any return branch */
 	PERF_SAMPLE_BRANCH_IND_CALL	= 1U << 6, /* indirect calls */
+	PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 
-	PERF_SAMPLE_BRANCH_MAX		= 1U << 7, /* non-ABI */
+	PERF_SAMPLE_BRANCH_MAX		= 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +1 -0
index cdf58ec..5b0b89d 100644
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
 	BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
 	BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
 	BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+	BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),
 	BRANCH_END
 };
 
#28 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Michael Neuling

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

I don't see an immediate problem with that except that we on x86 need to implement that in the software filter. Stephane do you see any fundamental issue with that?

On X86, the LBR cannot filter on conditional in HW. Thus as Peter said, it would have to be done in SW. I did not add that because I think those branches are not necessarily useful for tools.

Quoted 28 lines
Quoted 28 lines
include/uapi/linux/perf_event.h +2 -1
index fb104e5..891c769 100644
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
      PERF_SAMPLE_BRANCH_ANY_CALL     = 1U << 4, /* any call branch */
      PERF_SAMPLE_BRANCH_ANY_RETURN   = 1U << 5, /* any return branch */
      PERF_SAMPLE_BRANCH_IND_CALL     = 1U << 6, /* indirect calls */
+     PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 
-     PERF_SAMPLE_BRANCH_MAX          = 1U << 7, /* non-ABI */
+     PERF_SAMPLE_BRANCH_MAX          = 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +1 -0
index cdf58ec..5b0b89d 100644
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
      BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
      BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
      BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+     BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),
      BRANCH_END
 };
 
maintainer
#29 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 8 lines · Stephane Eranian
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Michael Neuling

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Quoted 7 lines
Quoted 4 lines

I don't see an immediate problem with that except that we on x86 need to implement that in the software filter. Stephane do you see any fundamental issue with that?

On X86, the LBR cannot filter on conditional in HW. Thus as Peter said, it would have to be done in SW. I did not add that because I think those branches are not necessarily useful for tools.

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

Also, it looks like its trivial to add to x86, you seem to have already done all the hard work by having X86_BR_JCC.

The only missing piece would be:

arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -0
@@ -337,6 +337,10 @@ static int intel_pmu_setup_sw_lbr_filter
 
 	if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
 		mask |= X86_BR_IND_CALL;
+
+	if (br_type & PERF_SAMPLE_BRANCH_CONDITIONAL)
+		mask |= X86_BR_JCC;
+
 	/*
 	 * stash actual user request into reg, it may
 	 * be used by fixup code for some CPU
#30 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Stephane Eranian
Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Michael Neuling

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

I don't see an immediate problem with that except that we on x86 need to implement that in the software filter. Stephane do you see any fundamental issue with that?

Quoted 8 lines
Quoted 3 lines

On X86, the LBR cannot filter on conditional in HW. Thus as Peter said, it would have to be done in SW. I did not add that because I think those branches are not necessarily useful for tools.

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

You could mispredict the target address of a computed goto. You'd know it was taken but not know target address until later in the pipeline.

On this, the POWER8 branch history buffer tells us two things about the prediction status.
1) if the branch was predicted taken/not taken correctly
2) if the target address was predicted correctly or not (for computed gotos only)
So we'd actually like more prediction bits too :-D

Quoted 3 lines

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

I don't think we can mispredict a non-conditional non-computed but I'll have to check with the HW folks.

Mikey

Quoted 20 lines

Also, it looks like its trivial to add to x86, you seem to have already done all the hard work by having X86_BR_JCC.

The only missing piece would be:

arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -0
@@ -337,6 +337,10 @@ static int intel_pmu_setup_sw_lbr_filter
 
 	if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
 		mask |= X86_BR_IND_CALL;
+
+	if (br_type & PERF_SAMPLE_BRANCH_CONDITIONAL)
+		mask |= X86_BR_JCC;
+
 	/*
 	 * stash actual user request into reg, it may
 	 * be used by fixup code for some CPU
maintainer
#31 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 9 lines · Michael Neuling
Quoted 4 lines · Peter Zijlstra

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

You could mispredict the target address of a computed goto. You'd know it was taken but not know target address until later in the pipeline.

Oh right, computed targets could indeed be mis predicted. I was more thinking about jumps with immediate values.

Quoted 6 lines

On this, the POWER8 branch history buffer tells us two things about the prediction status.
1) if the branch was predicted taken/not taken correctly
2) if the target address was predicted correctly or not (for computed gotos only)
So we'd actually like more prediction bits too :-D

So if I understand this right, 1) maps to the predicted flags we have; 2) would be new stuff?

We don't really have anything like that on x86, but I suppose if you make the thing optional and present a 'useful' use-case implemented in userspace code we could take it :-)

Quoted 6 lines
Quoted 3 lines

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

I don't think we can mispredict a non-conditional non-computed but I'll have to check with the HW folks.

I was mostly wondering about the use-case for the conditional filter. Stephane didn't think it useful, clearly your hardware guys thought different :-)

#32 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 32 lines · Peter Zijlstra
Quoted 9 lines · Michael Neuling
Quoted 4 lines · Peter Zijlstra

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

You could mispredict the target address of a computed goto. You'd know it was taken but not know target address until later in the pipeline.

Oh right, computed targets could indeed be mis predicted. I was more thinking about jumps with immediate values.

Quoted 6 lines

On this, the POWER8 branch history buffer tells us two things about the prediction status.
1) if the branch was predicted taken/not taken correctly
2) if the target address was predicted correctly or not (for computed gotos only)
So we'd actually like more prediction bits too :-D

So if I understand this right, 1) maps to the predicted flags we have; 2) would be new stuff?

We don't really have anything like that on x86, but I suppose if you make the thing optional and present a 'useful' use-case implemented in userspace code we could take it :-)

Quoted 4 lines
Quoted 3 lines

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

Quoted 7 lines
Quoted 2 lines

I don't think we can mispredict a non-conditional non-computed but I'll have to check with the HW folks.

I was mostly wondering about the use-case for the conditional filter. Stephane didn't think it useful, clearly your hardware guys thought different :-)

From my experience talking with compiler people, they care about ALL

the branches
and not the conditional so much. They use LBR to do basic block profiling.

#33 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 36 lines · Stephane Eranian
Quoted 35 lines · Peter Zijlstra
Quoted 9 lines · Michael Neuling
Quoted 4 lines · Peter Zijlstra

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

You could mispredict the target address of a computed goto. You'd know it was taken but not know target address until later in the pipeline.

Oh right, computed targets could indeed be mis predicted. I was more thinking about jumps with immediate values.

Quoted 6 lines

On this, the POWER8 branch history buffer tells us two things about the prediction status.
1) if the branch was predicted taken/not taken correctly
2) if the target address was predicted correctly or not (for computed gotos only)
So we'd actually like more prediction bits too :-D

So if I understand this right, 1) maps to the predicted flags we have; 2) would be new stuff?

We don't really have anything like that on x86, but I suppose if you make the thing optional and present a 'useful' use-case implemented in userspace code we could take it :-)

Quoted 6 lines
Quoted 3 lines

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

I don't think we can mispredict a non-conditional non-computed but I'll have to check with the HW folks.

Quoted 6 lines
Quoted 2 lines

I was mostly wondering about the use-case for the conditional filter. Stephane didn't think it useful, clearly your hardware guys thought different :-)

From my experience talking with compiler people, they care about ALL the branches and not the conditional so much. They use LBR to do basic block profiling.

OK. I don't have a good handle on what's useful for compilers or JITs right now. I'm just plumbing through what's possible.

Mikey

#34 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 41 lines · Michael Neuling
Quoted 39 lines · Stephane Eranian
Quoted 37 lines · Peter Zijlstra
Quoted 9 lines · Michael Neuling
Quoted 4 lines · Peter Zijlstra

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

You could mispredict the target address of a computed goto. You'd know it was taken but not know target address until later in the pipeline.

Oh right, computed targets could indeed be mis predicted. I was more thinking about jumps with immediate values.

Quoted 6 lines

On this, the POWER8 branch history buffer tells us two things about the prediction status.
1) if the branch was predicted taken/not taken correctly
2) if the target address was predicted correctly or not (for computed gotos only)
So we'd actually like more prediction bits too :-D

So if I understand this right, 1) maps to the predicted flags we have; 2) would be new stuff?

We don't really have anything like that on x86, but I suppose if you make the thing optional and present a 'useful' use-case implemented in userspace code we could take it :-)

Quoted 6 lines
Quoted 3 lines

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

I don't think we can mispredict a non-conditional non-computed but I'll have to check with the HW folks.

I was mostly wondering about the use-case for the conditional filter. Stephane didn't think it useful, clearly your hardware guys thought different :-)

Quoted 7 lines
Quoted 3 lines

From my experience talking with compiler people, they care about ALL the branches and not the conditional so much. They use LBR to do basic block profiling.

OK. I don't have a good handle on what's useful for compilers or JITs right now. I'm just plumbing through what's possible.

I understand. It is okay to extend the interface.

#35 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Peter Zijlstra
Quoted 22 lines · Peter Zijlstra
Quoted 8 lines · Stephane Eranian
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Michael Neuling

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Quoted 7 lines
Quoted 4 lines

I don't see an immediate problem with that except that we on x86 need to implement that in the software filter. Stephane do you see any fundamental issue with that?

On X86, the LBR cannot filter on conditional in HW. Thus as Peter said, it would have to be done in SW. I did not add that because I think those branches are not necessarily useful for tools.

Wouldn't it be mostly conditional branches that are the primary control flow and can get predicted wrong? I mean, I'm sure someone will miss-predict an unconditional branch but its not like we care about people with such afflictions do we?

Quoted 8 lines

Anyway, since PPC people thought it worth baking into hardware, presumably they have a compelling use case. Mikey could you see if you can retrieve that from someone in the know? It might be interesting.

Also, it looks like its trivial to add to x86, you seem to have already done all the hard work by having X86_BR_JCC.

The only missing piece would be:

Peter,

Can we add your signed-off-by on this?

We are cleaning up our series for conditional branches and would like to add this as part of the post.

Mikey

Quoted 15 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -0
@@ -337,6 +337,10 @@ static int intel_pmu_setup_sw_lbr_filter
 
 	if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
 		mask |= X86_BR_IND_CALL;
+
+	if (br_type & PERF_SAMPLE_BRANCH_CONDITIONAL)
+		mask |= X86_BR_JCC;
+
 	/*
 	 * stash actual user request into reg, it may
 	 * be used by fixup code for some CPU
maintainer
#36 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 6 lines · Michael Neuling

Peter Zijlstra <[email protected]> wrote:

Can we add your signed-off-by on this?

We are cleaning up our series for conditional branches and would like to add this as part of the post.

Sure, but its completely untested.. I was hoping Stephane would say somnething about it since he wrote all that magic ;-)

But yeah, feel free to add my SoB.

#37 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 11 lines · Peter Zijlstra
Quoted 6 lines · Michael Neuling

Peter Zijlstra <[email protected]> wrote:

Can we add your signed-off-by on this?

We are cleaning up our series for conditional branches and would like to add this as part of the post.

Sure, but its completely untested.. I was hoping Stephane would say somnething about it since he wrote all that magic ;-)

Let me take a look at it.

Quoted 1 line

But yeah, feel free to add my SoB.

#38 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

In reply to Michael Neuling
Quoted 50 lines · Michael Neuling
Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 5 lines
Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Mikey

include/uapi/linux/perf_event.h +0 -0
index fb104e5..891c769 100644

include/uapi/linux/perf_event.h enum perf_branch_sample_type { · line 161

Quoted 5 lines
include/uapi/linux/perf_event.h +1 -0
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
        PERF_SAMPLE_BRANCH_ANY_CALL     = 1U << 4, /* any call branch */
        PERF_SAMPLE_BRANCH_ANY_RETURN   = 1U << 5, /* any return branch */
        PERF_SAMPLE_BRANCH_IND_CALL     = 1U << 6, /* indirect calls */
+       PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 

I would use PERF_SAMPLE_BRANCH_COND here.

Quoted 10 lines
include/uapi/linux/perf_event.h +1 -1
-       PERF_SAMPLE_BRANCH_MAX          = 1U << 7, /* non-ABI */
+       PERF_SAMPLE_BRANCH_MAX          = 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +0 -0
index cdf58ec..5b0b89d 100644

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 679

Quoted 4 lines
tools/perf/builtin-record.c +1 -0
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
        BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
        BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
        BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+       BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),

use "cond"

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 682

Quoted 3 lines
tools/perf/builtin-record.c +0 -0
        BRANCH_END
 };
 

And if you do this, you also need to update the x86 perf_event_intel_lbr.c mapping
tables to fill out the entries for PERF_SAMPLE_BRANCH_COND:

[PERF_SAMPLE_BRANCH_COND] = LBR_JCC,

And you also need to update intel_pmu_setup_sw_lbr_filter() to handle the conversion to x86 instructions:

if (br_type & PERF_SAMPLE_BRANCH_COND)
mask |= X86_BR_JCC;

You also need to update the perf-record.txt documentation to list cond as a possible
branch filter.

#39 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 88 lines · Stephane Eranian
Quoted 50 lines · Michael Neuling
Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 5 lines
Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Mikey

include/uapi/linux/perf_event.h +0 -0
index fb104e5..891c769 100644

include/uapi/linux/perf_event.h enum perf_branch_sample_type { · line 161

Quoted 5 lines
include/uapi/linux/perf_event.h +1 -0
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
        PERF_SAMPLE_BRANCH_ANY_CALL     = 1U << 4, /* any call branch */
        PERF_SAMPLE_BRANCH_ANY_RETURN   = 1U << 5, /* any return branch */
        PERF_SAMPLE_BRANCH_IND_CALL     = 1U << 6, /* indirect calls */
+       PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 

I would use PERF_SAMPLE_BRANCH_COND here.

Quoted 10 lines
include/uapi/linux/perf_event.h +1 -1
-       PERF_SAMPLE_BRANCH_MAX          = 1U << 7, /* non-ABI */
+       PERF_SAMPLE_BRANCH_MAX          = 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +0 -0
index cdf58ec..5b0b89d 100644

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 679

Quoted 4 lines
tools/perf/builtin-record.c +1 -0
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
        BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
        BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
        BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+       BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),

use "cond"

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 682

Quoted 3 lines
tools/perf/builtin-record.c +0 -0
        BRANCH_END
 };
 

And if you do this, you also need to update the x86 perf_event_intel_lbr.c mapping
tables to fill out the entries for PERF_SAMPLE_BRANCH_COND:

[PERF_SAMPLE_BRANCH_COND] = LBR_JCC,

And you also need to update intel_pmu_setup_sw_lbr_filter() to handle the conversion to x86 instructions:

Quoted 7 lines

if (br_type & PERF_SAMPLE_BRANCH_COND)
mask |= X86_BR_JCC;

You also need to update the perf-record.txt documentation to list cond as a possible
branch filter.

Hey Stephane,

I have incorporated all the review comments into the patch series
https://lkml.org/lkml/2013/5/22/51.

Regards
Anshuman

#40 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Hi,

Quoted 93 lines · Anshuman Khandual
Quoted 92 lines · Stephane Eranian
Quoted 50 lines · Michael Neuling
Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 5 lines
Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Mikey

include/uapi/linux/perf_event.h +0 -0
index fb104e5..891c769 100644

include/uapi/linux/perf_event.h enum perf_branch_sample_type { · line 161

Quoted 5 lines
include/uapi/linux/perf_event.h +1 -0
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
        PERF_SAMPLE_BRANCH_ANY_CALL     = 1U << 4, /* any call branch */
        PERF_SAMPLE_BRANCH_ANY_RETURN   = 1U << 5, /* any return branch */
        PERF_SAMPLE_BRANCH_IND_CALL     = 1U << 6, /* indirect calls */
+       PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 

I would use PERF_SAMPLE_BRANCH_COND here.

Quoted 10 lines
include/uapi/linux/perf_event.h +1 -1
-       PERF_SAMPLE_BRANCH_MAX          = 1U << 7, /* non-ABI */
+       PERF_SAMPLE_BRANCH_MAX          = 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +0 -0
index cdf58ec..5b0b89d 100644

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 679

Quoted 4 lines
tools/perf/builtin-record.c +1 -0
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
        BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
        BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
        BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+       BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),

use "cond"

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 682

Quoted 3 lines
tools/perf/builtin-record.c +0 -0
        BRANCH_END
 };
 

And if you do this, you also need to update the x86 perf_event_intel_lbr.c mapping
tables to fill out the entries for PERF_SAMPLE_BRANCH_COND:

[PERF_SAMPLE_BRANCH_COND] = LBR_JCC,

And you also need to update intel_pmu_setup_sw_lbr_filter() to handle the conversion to x86 instructions:

if (br_type & PERF_SAMPLE_BRANCH_COND)
mask |= X86_BR_JCC;

Quoted 9 lines
Quoted 3 lines

You also need to update the perf-record.txt documentation to list cond as a possible
branch filter.

Hey Stephane,

I have incorporated all the review comments into the patch series
https://lkml.org/lkml/2013/5/22/51.

I don't see how you can compile Patch 3/5:

+ BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_CONDITIONAL),

Needs to be PERF_SAMPLE_BRANCH_COND.

#41 lore raw

Re: [PATCH 3/3] perf, x86, lbr: Demand proper privileges for PERF_SAMPLE_BRANCH_KERNEL

Quoted 105 lines · Stephane Eranian

Hi,

Quoted 99 lines · Anshuman Khandual
Quoted 88 lines · Stephane Eranian
Quoted 50 lines · Michael Neuling
Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra

We should always have proper privileges when requesting kernel data.

---
 arch/x86/kernel/cpu/perf_event_intel_lbr.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

arch/x86/kernel/cpu/perf_event_intel_lbr.c static void intel_pmu_setup_sw_lbr_filte · line 326

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_lbr.c +4 -1
@@ -318,8 +318,11 @@ static void intel_pmu_setup_sw_lbr_filte
        if (br_type & PERF_SAMPLE_BRANCH_USER)
                mask |= X86_BR_USER;
 
-       if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
+       if (br_type & PERF_SAMPLE_BRANCH_KERNEL) {
+               if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+                       return -EACCES;
                mask |= X86_BR_KERNEL;
+       }
 

This will prevent regular users from capturing kernel -> kernel branches. But it won't prevent users from getting kernel -> user branches. Thus some kernel address will still be captured. I guess they could be eliminated by the sw_filter.

Quoted 5 lines
Quoted 2 lines

When using LBR priv level filtering, the filter applies to the branch target only.

How about something like the below? It also adds the branch flags Mikey wanted for PowerPC.

Peter,

BTW PowerPC also has the ability to filter on conditional branches. Any chance we could add something like the follow to perf also?

Mikey

include/uapi/linux/perf_event.h +0 -0
index fb104e5..891c769 100644

include/uapi/linux/perf_event.h enum perf_branch_sample_type { · line 161

Quoted 5 lines
include/uapi/linux/perf_event.h +1 -0
@@ -157,8 +157,9 @@ enum perf_branch_sample_type {
        PERF_SAMPLE_BRANCH_ANY_CALL     = 1U << 4, /* any call branch */
        PERF_SAMPLE_BRANCH_ANY_RETURN   = 1U << 5, /* any return branch */
        PERF_SAMPLE_BRANCH_IND_CALL     = 1U << 6, /* indirect calls */
+       PERF_SAMPLE_BRANCH_CONDITIONAL  = 1U << 7, /* conditional branches */
 

I would use PERF_SAMPLE_BRANCH_COND here.

Quoted 10 lines
include/uapi/linux/perf_event.h +1 -1
-       PERF_SAMPLE_BRANCH_MAX          = 1U << 7, /* non-ABI */
+       PERF_SAMPLE_BRANCH_MAX          = 1U << 8, /* non-ABI */
 };
 
 #define PERF_SAMPLE_BRANCH_PLM_ALL \
tools/perf/builtin-record.c +0 -0
index cdf58ec..5b0b89d 100644

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 679

Quoted 4 lines
tools/perf/builtin-record.c +1 -0
@@ -676,6 +676,7 @@ static const struct branch_mode branch_modes[] = {
        BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
        BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
        BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
+       BRANCH_OPT("cnd", PERF_SAMPLE_BRANCH_CONDITIONAL),

use "cond"

tools/perf/builtin-record.c static const struct branch_mode branch_modes[] = { · line 682

Quoted 3 lines
tools/perf/builtin-record.c +0 -0
        BRANCH_END
 };
 

And if you do this, you also need to update the x86 perf_event_intel_lbr.c mapping
tables to fill out the entries for PERF_SAMPLE_BRANCH_COND:

[PERF_SAMPLE_BRANCH_COND] = LBR_JCC,

And you also need to update intel_pmu_setup_sw_lbr_filter() to handle the conversion to x86 instructions:

Quoted 7 lines

if (br_type & PERF_SAMPLE_BRANCH_COND)
mask |= X86_BR_JCC;

You also need to update the perf-record.txt documentation to list cond as a possible
branch filter.

Hey Stephane,

Quoted 9 lines
Quoted 3 lines

I have incorporated all the review comments into the patch series
https://lkml.org/lkml/2013/5/22/51.

I don't see how you can compile Patch 3/5:

+ BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_CONDITIONAL),

Needs to be PERF_SAMPLE_BRANCH_COND.

Ahh, sorry missed it, will fix it.