Skip to content

Commit 3202e35

Browse files
Ravi Bangoriampe
authored andcommitted
powerpc/perf: Fix MMCRA corruption by bhrb_filter
Consider a scenario where user creates two events: 1st event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY; fd = perf_event_open(attr, 0, 1, -1, 0); This sets cpuhw->bhrb_filter to 0 and returns valid fd. 2nd event: attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; attr.branch_sample_type = PERF_SAMPLE_BRANCH_CALL; fd = perf_event_open(attr, 0, 1, -1, 0); It overrides cpuhw->bhrb_filter to -1 and returns with error. Now if power_pmu_enable() gets called by any path other than power_pmu_add(), ppmu->config_bhrb(-1) will set MMCRA to -1. Fixes: 3925f46 ("powerpc/perf: Enable branch stack sampling framework") Cc: stable@vger.kernel.org # v3.10+ Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com> Reviewed-by: Madhavan Srinivasan <maddy@linux.vnet.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent b59bd35 commit 3202e35

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

arch/powerpc/perf/core-book3s.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,7 @@ static int power_pmu_event_init(struct perf_event *event)
18501850
int n;
18511851
int err;
18521852
struct cpu_hw_events *cpuhw;
1853+
u64 bhrb_filter;
18531854

18541855
if (!ppmu)
18551856
return -ENOENT;
@@ -1955,13 +1956,14 @@ static int power_pmu_event_init(struct perf_event *event)
19551956
err = power_check_constraints(cpuhw, events, cflags, n + 1);
19561957

19571958
if (has_branch_stack(event)) {
1958-
cpuhw->bhrb_filter = ppmu->bhrb_filter_map(
1959+
bhrb_filter = ppmu->bhrb_filter_map(
19591960
event->attr.branch_sample_type);
19601961

1961-
if (cpuhw->bhrb_filter == -1) {
1962+
if (bhrb_filter == -1) {
19621963
put_cpu_var(cpu_hw_events);
19631964
return -EOPNOTSUPP;
19641965
}
1966+
cpuhw->bhrb_filter = bhrb_filter;
19651967
}
19661968

19671969
put_cpu_var(cpu_hw_events);

arch/powerpc/perf/power8-pmu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ enum {
2929
#define POWER8_MMCRA_IFM1 0x0000000040000000UL
3030
#define POWER8_MMCRA_IFM2 0x0000000080000000UL
3131
#define POWER8_MMCRA_IFM3 0x00000000C0000000UL
32+
#define POWER8_MMCRA_BHRB_MASK 0x00000000C0000000UL
3233

3334
/*
3435
* Raw event encoding for PowerISA v2.07 (Power8):
@@ -243,6 +244,8 @@ static u64 power8_bhrb_filter_map(u64 branch_sample_type)
243244

244245
static void power8_config_bhrb(u64 pmu_bhrb_filter)
245246
{
247+
pmu_bhrb_filter &= POWER8_MMCRA_BHRB_MASK;
248+
246249
/* Enable BHRB filter in PMU */
247250
mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
248251
}

arch/powerpc/perf/power9-pmu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ enum {
9292
#define POWER9_MMCRA_IFM1 0x0000000040000000UL
9393
#define POWER9_MMCRA_IFM2 0x0000000080000000UL
9494
#define POWER9_MMCRA_IFM3 0x00000000C0000000UL
95+
#define POWER9_MMCRA_BHRB_MASK 0x00000000C0000000UL
9596

9697
/* Nasty Power9 specific hack */
9798
#define PVR_POWER9_CUMULUS 0x00002000
@@ -300,6 +301,8 @@ static u64 power9_bhrb_filter_map(u64 branch_sample_type)
300301

301302
static void power9_config_bhrb(u64 pmu_bhrb_filter)
302303
{
304+
pmu_bhrb_filter &= POWER9_MMCRA_BHRB_MASK;
305+
303306
/* Enable BHRB filter in PMU */
304307
mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
305308
}

0 commit comments

Comments
 (0)