Skip to content

Commit 6912318

Browse files
mikeyozbenh
authored andcommitted
powerpc/perf: Fix setting of "to" addresses for BHRB
Currently we only set the "to" address in the branch stack when the CPU explicitly gives us a value. Unfortunately it only does this for XL form branches (eg blr, bctr, bctar) and not I and B form branches (eg b, bc). Fortunately if we read the instruction from memory we can extract the offset of a branch and calculate the target address. This adds a function power_pmu_bhrb_to() to calculate the target/to address of the corresponding I and B form branches. It handles branches in both user and kernel spaces. It also plumbs this into the perf brhb reading code. Signed-off-by: Michael Neuling <mikey@neuling.org> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
1 parent 506e70d commit 6912318

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

‎arch/powerpc/perf/core-book3s.c‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
#include <linux/perf_event.h>
1414
#include <linux/percpu.h>
1515
#include <linux/hardirq.h>
16+
#include <linux/uaccess.h>
1617
#include <asm/reg.h>
1718
#include <asm/pmc.h>
1819
#include <asm/machdep.h>
1920
#include <asm/firmware.h>
2021
#include <asm/ptrace.h>
22+
#include <asm/code-patching.h>
2123

2224
#define BHRB_MAX_ENTRIES 32
2325
#define BHRB_TARGET 0x0000000000000002
@@ -362,6 +364,32 @@ void power_pmu_flush_branch_stack(void)
362364
if (ppmu->bhrb_nr)
363365
power_pmu_bhrb_reset();
364366
}
367+
/* Calculate the to address for a branch */
368+
static __u64 power_pmu_bhrb_to(u64 addr)
369+
{
370+
unsigned int instr;
371+
int ret;
372+
__u64 target;
373+
374+
if (is_kernel_addr(addr))
375+
return branch_target((unsigned int *)addr);
376+
377+
/* Userspace: need copy instruction here then translate it */
378+
pagefault_disable();
379+
ret = __get_user_inatomic(instr, (unsigned int __user *)addr);
380+
if (ret) {
381+
pagefault_enable();
382+
return 0;
383+
}
384+
pagefault_enable();
385+
386+
target = branch_target(&instr);
387+
if ((!target) || (instr & BRANCH_ABSOLUTE))
388+
return target;
389+
390+
/* Translate relative branch target from kernel to user address */
391+
return target - (unsigned long)&instr + addr;
392+
}
365393

366394
/* Processing BHRB entries */
367395
void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
@@ -426,7 +454,8 @@ void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
426454
/* Branches to immediate field
427455
(ie I or B form) */
428456
cpuhw->bhrb_entries[u_index].from = addr;
429-
cpuhw->bhrb_entries[u_index].to = 0;
457+
cpuhw->bhrb_entries[u_index].to =
458+
power_pmu_bhrb_to(addr);
430459
cpuhw->bhrb_entries[u_index].mispred = pred;
431460
cpuhw->bhrb_entries[u_index].predicted = ~pred;
432461
}

0 commit comments

Comments
 (0)