mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

Without the change `perf `hangs up on charaster devices. On my system it's enough to run system-wide sampler for a few seconds to get the hangup: $ perf record -a -g --call-graph=dwarf $ perf report # hung `strace` shows that hangup happens on reading on a character device `/dev/dri/renderD128` $ strace -y -f -p 2780484 strace: Process 2780484 attached pread64(101</dev/dri/renderD128>, strace: Process 2780484 detached It's call trace descends into `elfutils`: $ gdb -p 2780484 (gdb) bt #0 0x00007f5e508f04b7 in __libc_pread64 (fd=101, buf=0x7fff9df7edb0, count=0, offset=0) at ../sysdeps/unix/sysv/linux/pread64.c:25 #1 0x00007f5e52b79515 in read_file () from /<<NIX>>/elfutils-0.192/lib/libelf.so.1 #2 0x00007f5e52b25666 in libdw_open_elf () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #3 0x00007f5e52b25907 in __libdw_open_file () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #4 0x00007f5e52b120a9 in dwfl_report_elf@@ELFUTILS_0.156 () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #5 0x000000000068bf20 in __report_module (al=al@entry=0x7fff9df80010, ip=ip@entry=139803237033216, ui=ui@entry=0x5369b5e0) at util/dso.h:537 #6 0x000000000068c3d1 in report_module (ip=139803237033216, ui=0x5369b5e0) at util/unwind-libdw.c:114 #7 frame_callback (state=0x535aef10, arg=0x5369b5e0) at util/unwind-libdw.c:242 #8 0x00007f5e52b261d3 in dwfl_thread_getframes () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #9 0x00007f5e52b25bdb in get_one_thread_cb () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #10 0x00007f5e52b25faa in dwfl_getthreads () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #11 0x00007f5e52b26514 in dwfl_getthread_frames () from /<<NIX>>/elfutils-0.192/lib/libdw.so.1 #12 0x000000000068c6ce in unwind__get_entries (cb=cb@entry=0x5d4620 <unwind_entry>, arg=arg@entry=0x10cd5fa0, thread=thread@entry=0x1076a290, data=data@entry=0x7fff9df80540, max_stack=max_stack@entry=127, best_effort=best_effort@entry=false) at util/thread.h:152 #13 0x00000000005dae95 in thread__resolve_callchain_unwind (evsel=0x106006d0, thread=0x1076a290, cursor=0x10cd5fa0, sample=0x7fff9df80540, max_stack=127, symbols=true) at util/machine.c:2939 #14 thread__resolve_callchain_unwind (thread=0x1076a290, cursor=0x10cd5fa0, evsel=0x106006d0, sample=0x7fff9df80540, max_stack=127, symbols=true) at util/machine.c:2920 #15 __thread__resolve_callchain (thread=0x1076a290, cursor=0x10cd5fa0, evsel=0x106006d0, evsel@entry=0x7fff9df80440, sample=0x7fff9df80540, parent=parent@entry=0x7fff9df804a0, root_al=root_al@entry=0x7fff9df80440, max_stack=127, symbols=true) at util/machine.c:2970 #16 0x00000000005d0cb2 in thread__resolve_callchain (thread=<optimized out>, cursor=<optimized out>, evsel=0x7fff9df80440, sample=<optimized out>, parent=0x7fff9df804a0, root_al=0x7fff9df80440, max_stack=127) at util/machine.h:198 #17 sample__resolve_callchain (sample=<optimized out>, cursor=<optimized out>, parent=parent@entry=0x7fff9df804a0, evsel=evsel@entry=0x106006d0, al=al@entry=0x7fff9df80440, max_stack=max_stack@entry=127) at util/callchain.c:1127 #18 0x0000000000617e08 in hist_entry_iter__add (iter=iter@entry=0x7fff9df80480, al=al@entry=0x7fff9df80440, max_stack_depth=127, arg=arg@entry=0x7fff9df81ae0) at util/hist.c:1255 #19 0x000000000045d2d0 in process_sample_event (tool=0x7fff9df81ae0, event=<optimized out>, sample=0x7fff9df80540, evsel=0x106006d0, machine=<optimized out>) at builtin-report.c:334 #20 0x00000000005e3bb1 in perf_session__deliver_event (session=0x105ff2c0, event=0x7f5c7d735ca0, tool=0x7fff9df81ae0, file_offset=2914716832, file_path=0x105ffbf0 "perf.data") at util/session.c:1367 #21 0x00000000005e8d93 in do_flush (oe=0x105ffa50, show_progress=false) at util/ordered-events.c:245 #22 __ordered_events__flush (oe=0x105ffa50, how=OE_FLUSH__ROUND, timestamp=<optimized out>) at util/ordered-events.c:324 #23 0x00000000005e1f64 in perf_session__process_user_event (session=0x105ff2c0, event=0x7f5c7d752b18, file_offset=2914835224, file_path=0x105ffbf0 "perf.data") at util/session.c:1419 #24 0x00000000005e47c7 in reader__read_event (rd=rd@entry=0x7fff9df81260, session=session@entry=0x105ff2c0, --Type <RET> for more, q to quit, c to continue without paging-- quit prog=prog@entry=0x7fff9df81220) at util/session.c:2132 #25 0x00000000005e4b37 in reader__process_events (rd=0x7fff9df81260, session=0x105ff2c0, prog=0x7fff9df81220) at util/session.c:2181 #26 __perf_session__process_events (session=0x105ff2c0) at util/session.c:2226 #27 perf_session__process_events (session=session@entry=0x105ff2c0) at util/session.c:2390 #28 0x0000000000460add in __cmd_report (rep=0x7fff9df81ae0) at builtin-report.c:1076 #29 cmd_report (argc=<optimized out>, argv=<optimized out>) at builtin-report.c:1827 #30 0x00000000004c5a40 in run_builtin (p=p@entry=0xd8f7f8 <commands+312>, argc=argc@entry=1, argv=argv@entry=0x7fff9df844b0) at perf.c:351 #31 0x00000000004c5d63 in handle_internal_command (argc=argc@entry=1, argv=argv@entry=0x7fff9df844b0) at perf.c:404 #32 0x0000000000442de3 in run_argv (argcp=<synthetic pointer>, argv=<synthetic pointer>) at perf.c:448 #33 main (argc=<optimized out>, argv=0x7fff9df844b0) at perf.c:556 The hangup happens because nothing in` perf` or `elfutils` checks if a mapped file is easily readable. The change conservatively skips all non-regular files. Signed-off-by: Sergei Trofimovich <slyich@gmail.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Link: https://lore.kernel.org/r/20250505174419.2814857-1-slyich@gmail.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
331 lines
7.7 KiB
C
331 lines
7.7 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/compiler.h>
|
|
#include <elfutils/libdw.h>
|
|
#include <elfutils/libdwfl.h>
|
|
#include <inttypes.h>
|
|
#include <errno.h>
|
|
#include "debug.h"
|
|
#include "dso.h"
|
|
#include "unwind.h"
|
|
#include "unwind-libdw.h"
|
|
#include "machine.h"
|
|
#include "map.h"
|
|
#include "symbol.h"
|
|
#include "thread.h"
|
|
#include <linux/types.h>
|
|
#include <linux/zalloc.h>
|
|
#include "event.h"
|
|
#include "perf_regs.h"
|
|
#include "callchain.h"
|
|
#include "util/env.h"
|
|
|
|
static char *debuginfo_path;
|
|
|
|
static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,
|
|
const char *modname __maybe_unused, Dwarf_Addr base __maybe_unused,
|
|
const char *file_name, const char *debuglink_file __maybe_unused,
|
|
GElf_Word debuglink_crc __maybe_unused, char **debuginfo_file_name)
|
|
{
|
|
const struct dso *dso = *userdata;
|
|
|
|
assert(dso);
|
|
if (dso__symsrc_filename(dso) && strcmp(file_name, dso__symsrc_filename(dso)))
|
|
*debuginfo_file_name = strdup(dso__symsrc_filename(dso));
|
|
return -1;
|
|
}
|
|
|
|
static const Dwfl_Callbacks offline_callbacks = {
|
|
.find_debuginfo = __find_debuginfo,
|
|
.debuginfo_path = &debuginfo_path,
|
|
.section_address = dwfl_offline_section_address,
|
|
// .find_elf is not set as we use dwfl_report_elf() instead.
|
|
};
|
|
|
|
static int __report_module(struct addr_location *al, u64 ip,
|
|
struct unwind_info *ui)
|
|
{
|
|
Dwfl_Module *mod;
|
|
struct dso *dso = NULL;
|
|
Dwarf_Addr base;
|
|
/*
|
|
* Some callers will use al->sym, so we can't just use the
|
|
* cheaper thread__find_map() here.
|
|
*/
|
|
thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al);
|
|
|
|
if (al->map)
|
|
dso = map__dso(al->map);
|
|
|
|
if (!dso)
|
|
return 0;
|
|
|
|
/*
|
|
* The generated JIT DSO files only map the code segment without
|
|
* ELF headers. Since JIT codes used to be packed in a memory
|
|
* segment, calculating the base address using pgoff falls into
|
|
* a different code in another DSO. So just use the map->start
|
|
* directly to pick the correct one.
|
|
*/
|
|
if (!strncmp(dso__long_name(dso), "/tmp/jitted-", 12))
|
|
base = map__start(al->map);
|
|
else
|
|
base = map__start(al->map) - map__pgoff(al->map);
|
|
|
|
mod = dwfl_addrmodule(ui->dwfl, ip);
|
|
if (mod) {
|
|
Dwarf_Addr s;
|
|
|
|
dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
|
|
if (s != base)
|
|
mod = NULL;
|
|
}
|
|
|
|
if (!mod) {
|
|
char filename[PATH_MAX];
|
|
|
|
__symbol__join_symfs(filename, sizeof(filename), dso__long_name(dso));
|
|
/* Don't hang up on device files like /dev/dri/renderD128. */
|
|
if (is_regular_file(filename)) {
|
|
mod = dwfl_report_elf(ui->dwfl, dso__short_name(dso), filename, -1,
|
|
base, false);
|
|
}
|
|
}
|
|
if (!mod) {
|
|
char filename[PATH_MAX];
|
|
|
|
if (dso__build_id_filename(dso, filename, sizeof(filename), false))
|
|
mod = dwfl_report_elf(ui->dwfl, dso__short_name(dso), filename, -1,
|
|
base, false);
|
|
}
|
|
|
|
if (mod) {
|
|
void **userdatap;
|
|
|
|
dwfl_module_info(mod, &userdatap, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
*userdatap = dso;
|
|
}
|
|
|
|
return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
|
|
}
|
|
|
|
static int report_module(u64 ip, struct unwind_info *ui)
|
|
{
|
|
struct addr_location al;
|
|
int res;
|
|
|
|
addr_location__init(&al);
|
|
res = __report_module(&al, ip, ui);
|
|
addr_location__exit(&al);
|
|
return res;
|
|
}
|
|
|
|
/*
|
|
* Store all entries within entries array,
|
|
* we will process it after we finish unwind.
|
|
*/
|
|
static int entry(u64 ip, struct unwind_info *ui)
|
|
|
|
{
|
|
struct unwind_entry *e = &ui->entries[ui->idx++];
|
|
struct addr_location al;
|
|
|
|
addr_location__init(&al);
|
|
if (__report_module(&al, ip, ui)) {
|
|
addr_location__exit(&al);
|
|
return -1;
|
|
}
|
|
|
|
e->ip = ip;
|
|
e->ms.maps = al.maps;
|
|
e->ms.map = al.map;
|
|
e->ms.sym = al.sym;
|
|
|
|
pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
|
|
al.sym ? al.sym->name : "''",
|
|
ip,
|
|
al.map ? map__map_ip(al.map, ip) : (u64) 0);
|
|
addr_location__exit(&al);
|
|
return 0;
|
|
}
|
|
|
|
static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
|
|
{
|
|
/* We want only single thread to be processed. */
|
|
if (*thread_argp != NULL)
|
|
return 0;
|
|
|
|
*thread_argp = arg;
|
|
return dwfl_pid(dwfl);
|
|
}
|
|
|
|
static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
|
|
Dwarf_Word *data)
|
|
{
|
|
struct addr_location al;
|
|
ssize_t size;
|
|
struct dso *dso;
|
|
|
|
addr_location__init(&al);
|
|
if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
|
|
pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
|
|
goto out_fail;
|
|
}
|
|
dso = map__dso(al.map);
|
|
if (!dso)
|
|
goto out_fail;
|
|
|
|
size = dso__data_read_addr(dso, al.map, ui->machine, addr, (u8 *) data, sizeof(*data));
|
|
|
|
addr_location__exit(&al);
|
|
return !(size == sizeof(*data));
|
|
out_fail:
|
|
addr_location__exit(&al);
|
|
return -1;
|
|
}
|
|
|
|
static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
|
|
void *arg)
|
|
{
|
|
struct unwind_info *ui = arg;
|
|
const char *arch = perf_env__arch(ui->machine->env);
|
|
struct stack_dump *stack = &ui->sample->user_stack;
|
|
u64 start, end;
|
|
int offset;
|
|
int ret;
|
|
|
|
if (!ui->sample->user_regs)
|
|
return false;
|
|
|
|
ret = perf_reg_value(&start, ui->sample->user_regs,
|
|
perf_arch_reg_sp(arch));
|
|
if (ret)
|
|
return false;
|
|
|
|
end = start + stack->size;
|
|
|
|
/* Check overflow. */
|
|
if (addr + sizeof(Dwarf_Word) < addr)
|
|
return false;
|
|
|
|
if (addr < start || addr + sizeof(Dwarf_Word) > end) {
|
|
ret = access_dso_mem(ui, addr, result);
|
|
if (ret) {
|
|
pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
|
|
" 0x%" PRIx64 "-0x%" PRIx64 "\n",
|
|
addr, start, end);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
offset = addr - start;
|
|
*result = *(Dwarf_Word *)&stack->data[offset];
|
|
pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
|
|
addr, (unsigned long)*result, offset);
|
|
return true;
|
|
}
|
|
|
|
static const Dwfl_Thread_Callbacks callbacks = {
|
|
.next_thread = next_thread,
|
|
.memory_read = memory_read,
|
|
.set_initial_registers = libdw__arch_set_initial_registers,
|
|
};
|
|
|
|
static int
|
|
frame_callback(Dwfl_Frame *state, void *arg)
|
|
{
|
|
struct unwind_info *ui = arg;
|
|
Dwarf_Addr pc;
|
|
bool isactivation;
|
|
|
|
if (!dwfl_frame_pc(state, &pc, NULL)) {
|
|
if (!ui->best_effort)
|
|
pr_err("%s", dwfl_errmsg(-1));
|
|
return DWARF_CB_ABORT;
|
|
}
|
|
|
|
// report the module before we query for isactivation
|
|
report_module(pc, ui);
|
|
|
|
if (!dwfl_frame_pc(state, &pc, &isactivation)) {
|
|
if (!ui->best_effort)
|
|
pr_err("%s", dwfl_errmsg(-1));
|
|
return DWARF_CB_ABORT;
|
|
}
|
|
|
|
if (!isactivation)
|
|
--pc;
|
|
|
|
return entry(pc, ui) || !(--ui->max_stack) ?
|
|
DWARF_CB_ABORT : DWARF_CB_OK;
|
|
}
|
|
|
|
int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
|
|
struct thread *thread,
|
|
struct perf_sample *data,
|
|
int max_stack,
|
|
bool best_effort)
|
|
{
|
|
struct unwind_info *ui, ui_buf = {
|
|
.sample = data,
|
|
.thread = thread,
|
|
.machine = maps__machine((thread__maps(thread))),
|
|
.cb = cb,
|
|
.arg = arg,
|
|
.max_stack = max_stack,
|
|
.best_effort = best_effort
|
|
};
|
|
const char *arch = perf_env__arch(ui_buf.machine->env);
|
|
Dwarf_Word ip;
|
|
int err = -EINVAL, i;
|
|
|
|
if (!data->user_regs || !data->user_regs->regs)
|
|
return -EINVAL;
|
|
|
|
ui = zalloc(sizeof(ui_buf) + sizeof(ui_buf.entries[0]) * max_stack);
|
|
if (!ui)
|
|
return -ENOMEM;
|
|
|
|
*ui = ui_buf;
|
|
|
|
ui->dwfl = dwfl_begin(&offline_callbacks);
|
|
if (!ui->dwfl)
|
|
goto out;
|
|
|
|
err = perf_reg_value(&ip, data->user_regs, perf_arch_reg_ip(arch));
|
|
if (err)
|
|
goto out;
|
|
|
|
err = report_module(ip, ui);
|
|
if (err)
|
|
goto out;
|
|
|
|
err = !dwfl_attach_state(ui->dwfl, EM_NONE, thread__tid(thread), &callbacks, ui);
|
|
if (err)
|
|
goto out;
|
|
|
|
err = dwfl_getthread_frames(ui->dwfl, thread__tid(thread), frame_callback, ui);
|
|
|
|
if (err && ui->max_stack != max_stack)
|
|
err = 0;
|
|
|
|
/*
|
|
* Display what we got based on the order setup.
|
|
*/
|
|
for (i = 0; i < ui->idx && !err; i++) {
|
|
int j = i;
|
|
|
|
if (callchain_param.order == ORDER_CALLER)
|
|
j = ui->idx - i - 1;
|
|
|
|
err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
|
|
}
|
|
|
|
out:
|
|
if (err)
|
|
pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
|
|
|
|
dwfl_end(ui->dwfl);
|
|
free(ui);
|
|
return 0;
|
|
}
|