Skip to contentSkip to conversation

tools/perf: Fix segfault during perf record --off-cpu when debuginfo is not enabled

PATCH Applied Athira Rajeev 3 participants 6 messages Dec 23, 2024 – Jan 14, 2025 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/perf: Fix segfault during perf record --off-cpu when debuginfo is not enabled

When kernel is built without debuginfo, running perf record with --off-cpu results in segfault as below:

./perf record --off-cpu -e dummy sleep 1
libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find '.BTF' ELF section in /lib/modules/6.13.0-rc3+/build/vmlinux
libbpf: failed to find valid kernel BTF
Segmentation fault (core dumped)

The backtrace pointed to:

   #0  0x00000000100fb17c in btf.type_cnt ()
   #1  0x00000000100fc1a8 in btf_find_by_name_kind ()
   #2  0x00000000100fc38c in btf.find_by_name_kind ()
   #3  0x00000000102ee3ac in off_cpu_prepare ()
   #4  0x000000001002f78c in cmd_record ()
   #5  0x00000000100aee78 in run_builtin ()
   #6  0x00000000100af3e4 in handle_internal_command ()
   #7  0x000000001001004c in main ()

Code sequence is:
static void check_sched_switch_args(void)

   {
        struct btf *btf = btf__load_vmlinux_btf();
        const struct btf_type *t1, *t2, *t3;
        u32 type_id;

        type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
                                         BTF_KIND_TYPEDEF);

btf__load_vmlinux_btf fails when CONFIG_DEBUG_INFO_BTF is not enabled. Here bpf__find_by_name_kind calls btf__type_cnt with NULL btf value and results in segfault. To fix this, add a check to see if btf is not NULL before invoking bpf__find_by_name_kind

---
 tools/perf/util/bpf_off_cpu.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/bpf_off_cpu.c +5 -0
index a590a8ac1f9d..4269b41d1771 100644
@@ -100,6 +100,11 @@ static void check_sched_switch_args(void)
 	const struct btf_type *t1, *t2, *t3;
 	u32 type_id;
 
+	if (!btf) {
+		pr_debug("Missing btf, check if CONFIG_DEBUG_INFO_BTF is enabled\n");
+		goto cleanup;
+	}
+
 	type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
 					 BTF_KIND_TYPEDEF);
 	if ((s32)type_id < 0)
Signature · 1 line
2.43.5
#1 lore raw
Quoted 49 lines · Athira Rajeev

When kernel is built without debuginfo, running perf record with --off-cpu results in segfault as below:

./perf record --off-cpu -e dummy sleep 1
libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find '.BTF' ELF section in /lib/modules/6.13.0-rc3+/build/vmlinux
libbpf: failed to find valid kernel BTF
Segmentation fault (core dumped)

The backtrace pointed to:

  #0  0x00000000100fb17c in btf.type_cnt ()
  #1  0x00000000100fc1a8 in btf_find_by_name_kind ()
  #2  0x00000000100fc38c in btf.find_by_name_kind ()
  #3  0x00000000102ee3ac in off_cpu_prepare ()
  #4  0x000000001002f78c in cmd_record ()
  #5  0x00000000100aee78 in run_builtin ()
  #6  0x00000000100af3e4 in handle_internal_command ()
  #7  0x000000001001004c in main ()

Code sequence is:
static void check_sched_switch_args(void)

  {
       struct btf *btf = btf__load_vmlinux_btf();
       const struct btf_type *t1, *t2, *t3;
       u32 type_id;

       type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
                                        BTF_KIND_TYPEDEF);

btf__load_vmlinux_btf fails when CONFIG_DEBUG_INFO_BTF is not enabled. Here bpf__find_by_name_kind calls btf__type_cnt with NULL btf value and results in segfault. To fix this, add a check to see if btf is not NULL before invoking bpf__find_by_name_kind

---
tools/perf/util/bpf_off_cpu.c | 5 +++++

1 file changed, 5 insertions(+)
tools/perf/util/bpf_off_cpu.c +0 -0
index a590a8ac1f9d..4269b41d1771 100644

tools/perf/util/bpf_off_cpu.c static void check_sched_switch_args(void) · line 107

Quoted 8 lines
tools/perf/util/bpf_off_cpu.c +5 -0
@@ -100,6 +100,11 @@ static void check_sched_switch_args(void)
 const struct btf_type *t1, *t2, *t3;
 u32 type_id;
 
+ if (!btf) {
+ pr_debug("Missing btf, check if CONFIG_DEBUG_INFO_BTF is enabled\n");
+ goto cleanup;
+ }
+

Hi,

Looking for review comments on this

Athira

Quoted 6 lines
tools/perf/util/bpf_off_cpu.c +0 -0
 type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
 BTF_KIND_TYPEDEF);
 if ((s32)type_id < 0)
Signature · 1 line
2.43.5
maintainer Reviewed-by
#2 lore raw
Quoted 34 lines · Athira Rajeev
Quoted 34 lines · Athira Rajeev

When kernel is built without debuginfo, running perf record with --off-cpu results in segfault as below:

./perf record --off-cpu -e dummy sleep 1
libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find '.BTF' ELF section in /lib/modules/6.13.0-rc3+/build/vmlinux
libbpf: failed to find valid kernel BTF
Segmentation fault (core dumped)

The backtrace pointed to:

  #0  0x00000000100fb17c in btf.type_cnt ()
  #1  0x00000000100fc1a8 in btf_find_by_name_kind ()
  #2  0x00000000100fc38c in btf.find_by_name_kind ()
  #3  0x00000000102ee3ac in off_cpu_prepare ()
  #4  0x000000001002f78c in cmd_record ()
  #5  0x00000000100aee78 in run_builtin ()
  #6  0x00000000100af3e4 in handle_internal_command ()
  #7  0x000000001001004c in main ()

Code sequence is:
static void check_sched_switch_args(void)

  {
       struct btf *btf = btf__load_vmlinux_btf();
       const struct btf_type *t1, *t2, *t3;
       u32 type_id;

       type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
                                        BTF_KIND_TYPEDEF);
Quoted 6 lines
Quoted 6 lines

btf__load_vmlinux_btf fails when CONFIG_DEBUG_INFO_BTF is not enabled. Here bpf__find_by_name_kind calls btf__type_cnt with NULL btf value and results in segfault. To fix this, add a check to see if btf is not NULL before invoking bpf__find_by_name_kind

Thanks,
Namhyung

Quoted 30 lines
Quoted 9 lines

---
tools/perf/util/bpf_off_cpu.c | 5 +++++

1 file changed, 5 insertions(+)
tools/perf/util/bpf_off_cpu.c +0 -0
index a590a8ac1f9d..4269b41d1771 100644

tools/perf/util/bpf_off_cpu.c static void check_sched_switch_args(void) · line 107

Quoted 8 lines
tools/perf/util/bpf_off_cpu.c +5 -0
@@ -100,6 +100,11 @@ static void check_sched_switch_args(void)
 const struct btf_type *t1, *t2, *t3;
 u32 type_id;
 
+ if (!btf) {
+ pr_debug("Missing btf, check if CONFIG_DEBUG_INFO_BTF is enabled\n");
+ goto cleanup;
+ }
+

Hi,

Looking for review comments on this

Athira

Quoted 6 lines
tools/perf/util/bpf_off_cpu.c +0 -0
 type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
 BTF_KIND_TYPEDEF);
 if ((s32)type_id < 0)
Signature · 1 line
2.43.5
#3 lore raw
Quoted 44 lines · Namhyung Kim
Quoted 39 lines · Athira Rajeev
Quoted 39 lines · Athira Rajeev

When kernel is built without debuginfo, running perf record with --off-cpu results in segfault as below:

./perf record --off-cpu -e dummy sleep 1
libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find '.BTF' ELF section in /lib/modules/6.13.0-rc3+/build/vmlinux
libbpf: failed to find valid kernel BTF
Segmentation fault (core dumped)

The backtrace pointed to:

 #0  0x00000000100fb17c in btf.type_cnt ()
 #1  0x00000000100fc1a8 in btf_find_by_name_kind ()
 #2  0x00000000100fc38c in btf.find_by_name_kind ()
 #3  0x00000000102ee3ac in off_cpu_prepare ()
 #4  0x000000001002f78c in cmd_record ()
 #5  0x00000000100aee78 in run_builtin ()
 #6  0x00000000100af3e4 in handle_internal_command ()
 #7  0x000000001001004c in main ()

Code sequence is:
static void check_sched_switch_args(void)

 {
      struct btf *btf = btf__load_vmlinux_btf();
      const struct btf_type *t1, *t2, *t3;
      u32 type_id;

      type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
                                       BTF_KIND_TYPEDEF);

btf__load_vmlinux_btf fails when CONFIG_DEBUG_INFO_BTF is not enabled. Here bpf__find_by_name_kind calls btf__type_cnt with NULL btf value and results in segfault. To fix this, add a check to see if btf is not NULL before invoking bpf__find_by_name_kind

Quoted 7 lines
Quoted 1 line
Quoted 1 line

Thanks,
Namhyung

Thanks Namhyung for reviewing the patch

Athira

Quoted 28 lines
Quoted 28 lines
Quoted 9 lines

---
tools/perf/util/bpf_off_cpu.c | 5 +++++

1 file changed, 5 insertions(+)
tools/perf/util/bpf_off_cpu.c +0 -0
index a590a8ac1f9d..4269b41d1771 100644

tools/perf/util/bpf_off_cpu.c static void check_sched_switch_args(void) · line 107

Quoted 8 lines
tools/perf/util/bpf_off_cpu.c +5 -0
@@ -100,6 +100,11 @@ static void check_sched_switch_args(void)
 const struct btf_type *t1, *t2, *t3;
 u32 type_id;
 
+ if (!btf) {
+ pr_debug("Missing btf, check if CONFIG_DEBUG_INFO_BTF is enabled\n");
+ goto cleanup;
+ }
+

Hi,

Looking for review comments on this

Athira

Quoted 5 lines
tools/perf/util/bpf_off_cpu.c +0 -0
 type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
 BTF_KIND_TYPEDEF);
 if ((s32)type_id < 0)
Signature · 1 line
2.43.5
maintainer Applied
#4 lore raw
In reply to Namhyung Kim
Quoted 35 lines · Namhyung Kim
Quoted 34 lines · Athira Rajeev
Quoted 34 lines · Athira Rajeev

When kernel is built without debuginfo, running perf record with --off-cpu results in segfault as below:

./perf record --off-cpu -e dummy sleep 1
libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find '.BTF' ELF section in /lib/modules/6.13.0-rc3+/build/vmlinux
libbpf: failed to find valid kernel BTF
Segmentation fault (core dumped)

The backtrace pointed to:

  #0  0x00000000100fb17c in btf.type_cnt ()
  #1  0x00000000100fc1a8 in btf_find_by_name_kind ()
  #2  0x00000000100fc38c in btf.find_by_name_kind ()
  #3  0x00000000102ee3ac in off_cpu_prepare ()
  #4  0x000000001002f78c in cmd_record ()
  #5  0x00000000100aee78 in run_builtin ()
  #6  0x00000000100af3e4 in handle_internal_command ()
  #7  0x000000001001004c in main ()

Code sequence is:
static void check_sched_switch_args(void)

  {
       struct btf *btf = btf__load_vmlinux_btf();
       const struct btf_type *t1, *t2, *t3;
       u32 type_id;

       type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
                                        BTF_KIND_TYPEDEF);
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

btf__load_vmlinux_btf fails when CONFIG_DEBUG_INFO_BTF is not enabled. Here bpf__find_by_name_kind calls btf__type_cnt with NULL btf value and results in segfault. To fix this, add a check to see if btf is not NULL before invoking bpf__find_by_name_kind

Thanks, applied to perf-tools-next,

- Arnaldo

#5 lore raw
Quoted 45 lines · Arnaldo Carvalho de Melo
Quoted 40 lines · Namhyung Kim
Quoted 39 lines · Athira Rajeev
Quoted 39 lines · Athira Rajeev

When kernel is built without debuginfo, running perf record with --off-cpu results in segfault as below:

./perf record --off-cpu -e dummy sleep 1
libbpf: kernel BTF is missing at '/sys/kernel/btf/vmlinux', was CONFIG_DEBUG_INFO_BTF enabled?
libbpf: failed to find '.BTF' ELF section in /lib/modules/6.13.0-rc3+/build/vmlinux
libbpf: failed to find valid kernel BTF
Segmentation fault (core dumped)

The backtrace pointed to:

 #0  0x00000000100fb17c in btf.type_cnt ()
 #1  0x00000000100fc1a8 in btf_find_by_name_kind ()
 #2  0x00000000100fc38c in btf.find_by_name_kind ()
 #3  0x00000000102ee3ac in off_cpu_prepare ()
 #4  0x000000001002f78c in cmd_record ()
 #5  0x00000000100aee78 in run_builtin ()
 #6  0x00000000100af3e4 in handle_internal_command ()
 #7  0x000000001001004c in main ()

Code sequence is:
static void check_sched_switch_args(void)

 {
      struct btf *btf = btf__load_vmlinux_btf();
      const struct btf_type *t1, *t2, *t3;
      u32 type_id;

      type_id = btf__find_by_name_kind(btf, "btf_trace_sched_switch",
                                       BTF_KIND_TYPEDEF);

btf__load_vmlinux_btf fails when CONFIG_DEBUG_INFO_BTF is not enabled. Here bpf__find_by_name_kind calls btf__type_cnt with NULL btf value and results in segfault. To fix this, add a check to see if btf is not NULL before invoking bpf__find_by_name_kind

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

Thanks, applied to perf-tools-next,

- Arnaldo

Thanks Namhyung and Arnaldo

Athira.