Skip to content

Commit 0cdda9f

Browse files
author
Mamatha Inamdar
committed
arch/powerpc/perf: Check the instruction type before creating sample with perf_mem_data_src
JIRA: https://issues.redhat.com/browse/RHEL-80602 commit 2ffb26a Author: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Date: Tue Jan 21 18:46:20 2025 +0530 arch/powerpc/perf: Check the instruction type before creating sample with perf_mem_data_src perf mem report aborts as below sometimes (during some corner case) in powerpc: # ./perf mem report 1>out *** stack smashing detected ***: terminated Aborted (core dumped) The backtrace is as below: __pthread_kill_implementation () raise () abort () __libc_message __fortify_fail __stack_chk_fail hist_entry.lvl_snprintf __sort__hpp_entry __hist_entry__snprintf hists.fprintf cmd_report cmd_mem Snippet of code which triggers the issue from tools/perf/util/sort.c static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf, size_t size, unsigned int width) { char out[64]; perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info); return repsep_snprintf(bf, size, "%-*s", width, out); } The value of "out" is filled from perf_mem_data_src value. Debugging this further showed that for some corner cases, the value of "data_src" was pointing to wrong value. This resulted in bigger size of string and causing stack check fail. The perf mem data source values are captured in the sample via isa207_get_mem_data_src function. The initial check is to fetch the type of sampled instruction. If the type of instruction is not valid (not a load/store instruction), the function returns. Since 'commit e16fd7f ("perf: Use sample_flags for data_src")', data_src field is not initialized by the perf_sample_data_init() function. If the PMU driver doesn't set the data_src value to zero if type is not valid, this will result in uninitailised value for data_src. The uninitailised value of data_src resulted in stack check fail followed by abort for "perf mem report". When requesting for data source information in the sample, the instruction type is expected to be load or store instruction. In ISA v3.0, due to hardware limitation, there are corner cases where the instruction type other than load or store is observed. In ISA v3.0 and before values "0" and "7" are considered reserved. In ISA v3.1, value "7" has been used to indicate "larx/stcx". Drop the sample if instruction type has reserved values for this field with a ISA version check. Initialize data_src to zero in isa207_get_mem_data_src if the instruction type is not load/store. Reported-by: Disha Goel <disgoel@linux.vnet.ibm.com> Signed-off-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com> Link: https://patch.msgid.link/20250121131621.39054-1-atrajeev@linux.vnet.ibm.com Signed-off-by: Mamatha Inamdar <minamdar@redhat.com>
1 parent 15b5887 commit 0cdda9f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

arch/powerpc/perf/core-book3s.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,10 @@ static struct pmu power_pmu = {
22122212
#define PERF_SAMPLE_ADDR_TYPE (PERF_SAMPLE_ADDR | \
22132213
PERF_SAMPLE_PHYS_ADDR | \
22142214
PERF_SAMPLE_DATA_PAGE_SIZE)
2215+
2216+
#define SIER_TYPE_SHIFT 15
2217+
#define SIER_TYPE_MASK (0x7ull << SIER_TYPE_SHIFT)
2218+
22152219
/*
22162220
* A counter has overflowed; update its count and record
22172221
* things if requested. Note that interrupts are hard-disabled
@@ -2280,6 +2284,22 @@ static void record_and_restart(struct perf_event *event, unsigned long val,
22802284
is_kernel_addr(mfspr(SPRN_SIAR)))
22812285
record = 0;
22822286

2287+
/*
2288+
* SIER[46-48] presents instruction type of the sampled instruction.
2289+
* In ISA v3.0 and before values "0" and "7" are considered reserved.
2290+
* In ISA v3.1, value "7" has been used to indicate "larx/stcx".
2291+
* Drop the sample if "type" has reserved values for this field with a
2292+
* ISA version check.
2293+
*/
2294+
if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC &&
2295+
ppmu->get_mem_data_src) {
2296+
val = (regs->dar & SIER_TYPE_MASK) >> SIER_TYPE_SHIFT;
2297+
if (val == 0 || (val == 7 && !cpu_has_feature(CPU_FTR_ARCH_31))) {
2298+
record = 0;
2299+
atomic64_inc(&event->lost_samples);
2300+
}
2301+
}
2302+
22832303
/*
22842304
* Finally record data if requested.
22852305
*/

arch/powerpc/perf/isa207-common.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,10 @@ void isa207_get_mem_data_src(union perf_mem_data_src *dsrc, u32 flags,
321321

322322
sier = mfspr(SPRN_SIER);
323323
val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
324-
if (val != 1 && val != 2 && !(val == 7 && cpu_has_feature(CPU_FTR_ARCH_31)))
324+
if (val != 1 && val != 2 && !(val == 7 && cpu_has_feature(CPU_FTR_ARCH_31))) {
325+
dsrc->val = 0;
325326
return;
327+
}
326328

327329
idx = (sier & ISA207_SIER_LDST_MASK) >> ISA207_SIER_LDST_SHIFT;
328330
sub_idx = (sier & ISA207_SIER_DATA_SRC_MASK) >> ISA207_SIER_DATA_SRC_SHIFT;

0 commit comments

Comments
 (0)