Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

s390/perf: implement perf_callchain_user()

Daan De Meyer and Neal Gompa reported that s390 does not support perf user
stack unwinding.

This was never implemented since this requires user space to be compiled
with the -mbackchain compile option, which until now no distribution
did. However this is going to change with Fedora. Therefore provide a
perf_callchain_user() implementation.

Note that due to the way s390 sets up stack frames the provided call chains
can contain invalid values. This is especially true for the first stack
frame, where it is not possible to tell if the return address has been
written to the stack already or not.

Reported-by: Daan De Meyer <daan.j.demeyer@gmail.com>
Reported-by: Neal Gompa <ngompa@fedoraproject.org>
Closes: https://lore.kernel.org/all/CAO8sHcn3+_qrnvp0580aK7jN0Wion5F7KYeBAa4MnCY4mqABPA@mail.gmail.com/
Link: https://lore.kernel.org/all/20231030123558.10816-A-hca@linux.ibm.com
Reviewed-by: Neal Gompa <ngompa@fedoraproject.org>
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>

authored by

Heiko Carstens and committed by
Vasily Gorbik
504b73d0 e14aec23

+48
+7
arch/s390/include/asm/stacktrace.h
··· 6 6 #include <linux/ptrace.h> 7 7 #include <asm/switch_to.h> 8 8 9 + struct stack_frame_user { 10 + unsigned long back_chain; 11 + unsigned long empty1[5]; 12 + unsigned long gprs[10]; 13 + unsigned long empty2[4]; 14 + }; 15 + 9 16 enum stack_type { 10 17 STACK_TYPE_UNKNOWN, 11 18 STACK_TYPE_TASK,
+41
arch/s390/kernel/perf_event.c
··· 15 15 #include <linux/export.h> 16 16 #include <linux/seq_file.h> 17 17 #include <linux/spinlock.h> 18 + #include <linux/uaccess.h> 19 + #include <linux/compat.h> 18 20 #include <linux/sysfs.h> 21 + #include <asm/stacktrace.h> 19 22 #include <asm/irq.h> 20 23 #include <asm/cpu_mf.h> 21 24 #include <asm/lowcore.h> ··· 213 210 if (!addr || perf_callchain_store(entry, addr)) 214 211 return; 215 212 } 213 + } 214 + 215 + void perf_callchain_user(struct perf_callchain_entry_ctx *entry, 216 + struct pt_regs *regs) 217 + { 218 + struct stack_frame_user __user *sf; 219 + unsigned long ip, sp; 220 + bool first = true; 221 + 222 + if (is_compat_task()) 223 + return; 224 + perf_callchain_store(entry, instruction_pointer(regs)); 225 + sf = (void __user *)user_stack_pointer(regs); 226 + pagefault_disable(); 227 + while (entry->nr < entry->max_stack) { 228 + if (__get_user(sp, &sf->back_chain)) 229 + break; 230 + if (__get_user(ip, &sf->gprs[8])) 231 + break; 232 + if (ip & 0x1) { 233 + /* 234 + * If the instruction address is invalid, and this 235 + * is the first stack frame, assume r14 has not 236 + * been written to the stack yet. Otherwise exit. 237 + */ 238 + if (first && !(regs->gprs[14] & 0x1)) 239 + ip = regs->gprs[14]; 240 + else 241 + break; 242 + } 243 + perf_callchain_store(entry, ip); 244 + /* Sanity check: ABI requires SP to be aligned 8 bytes. */ 245 + if (!sp || sp & 0x7) 246 + break; 247 + sf = (void __user *)sp; 248 + first = false; 249 + } 250 + pagefault_enable(); 216 251 } 217 252 218 253 /* Perf definitions for PMU event attributes in sysfs */