Skip to contentSkip to conversation

perf: get_perf_callchain return NULL for crosstask

PATCH Superseded Jordan Rome 2 participants 5 messages Nov 10, 2023 – Nov 11, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: get_perf_callchain return NULL for crosstask

Return NULL instead of returning 1 incorrect frame, which currently happens when trying to walk the user stack for any task that isn't current. Returning NULL is a better indicator that this behavior is not supported.

This issue was found using bpf_get_task_stack inside a BPF iterator ("iter/task"), which iterates over all tasks. The single address/frame in the buffer when getting user stacks for tasks that aren't current could not be symbolized (testing multiple symbolizers).

---
 kernel/events/callchain.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/events/callchain.c +3 -4
index 1273be84392c..430fa544fa80 100644
@@ -201,6 +201,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
 	}
 
 	if (user) {
+		if (crosstask)
+			return NULL;
+
 		if (!user_mode(regs)) {
 			if  (current->mm)
 				regs = task_pt_regs(current);
@@ -209,9 +212,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
 		}
 
 		if (regs) {
-			if (crosstask)
-				goto exit_put;
-
 			if (add_mark)
 				perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
 
@@ -219,7 +219,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
 		}
 	}
 
-exit_put:
 	put_callchain_entry(rctx);
 
 	return entry;
Signature · 1 line
2.39.3
#1 lore raw
Quoted 22 lines · Jordan Rome

Return NULL instead of returning 1 incorrect frame, which currently happens when trying to walk the user stack for any task that isn't current. Returning NULL is a better indicator that this behavior is not supported.

This issue was found using bpf_get_task_stack inside a BPF iterator ("iter/task"), which iterates over all tasks. The single address/frame in the buffer when getting user stacks for tasks that aren't current could not be symbolized (testing multiple symbolizers).

---
 kernel/events/callchain.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/events/callchain.c +0 -0
index 1273be84392c..430fa544fa80 100644

kernel/events/callchain.c get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, · line 205

Quoted 5 lines
kernel/events/callchain.c +2 -0
@@ -201,6 +201,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
        }
 
        if (user) {
+               if (crosstask)
+                       return NULL;

I think you need that goto exit_put here.

Quoted 25 lines
kernel/events/callchain.c +1 -5
+
                if (!user_mode(regs)) {
                        if  (current->mm)
                                regs = task_pt_regs(current);
@@ -209,9 +212,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
 
                if (regs) {
-                       if (crosstask)
-                               goto exit_put;
-
                        if (add_mark)
                                perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
 
@@ -219,7 +219,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
        }
 
-exit_put:
        put_callchain_entry(rctx);
 
        return entry;
--

2.39.3

#2 lore raw
Quoted 26 lines · Andrii Nakryiko
Quoted 24 lines · Jordan Rome

Return NULL instead of returning 1 incorrect frame, which currently happens when trying to walk the user stack for any task that isn't current. Returning NULL is a better indicator that this behavior is not supported.

This issue was found using bpf_get_task_stack inside a BPF iterator ("iter/task"), which iterates over all tasks. The single address/frame in the buffer when getting user stacks for tasks that aren't current could not be symbolized (testing multiple symbolizers).

---
 kernel/events/callchain.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/events/callchain.c +0 -0
index 1273be84392c..430fa544fa80 100644
@@ -201,6 +201,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
        }
 
Quoted 6 lines

kernel/events/callchain.c get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, · line 205

Quoted 3 lines
kernel/events/callchain.c +2 -0
        if (user) {
+               if (crosstask)
+                       return NULL;

I think you need that goto exit_put here.

Why is that? Wouldn't that be the same behavior that already exists? That being said we can probably move this check above get_callchain_entry and exit earlier.

Quoted 25 lines
Quoted 25 lines
kernel/events/callchain.c +1 -5
+
                if (!user_mode(regs)) {
                        if  (current->mm)
                                regs = task_pt_regs(current);
@@ -209,9 +212,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
 
                if (regs) {
-                       if (crosstask)
-                               goto exit_put;
-
                        if (add_mark)
                                perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
 
@@ -219,7 +219,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
        }
 
-exit_put:
        put_callchain_entry(rctx);
 
        return entry;
--

2.39.3

#3 lore raw
Quoted 36 lines · Jordan Rome
Quoted 26 lines · Andrii Nakryiko
Quoted 24 lines · Jordan Rome

Return NULL instead of returning 1 incorrect frame, which currently happens when trying to walk the user stack for any task that isn't current. Returning NULL is a better indicator that this behavior is not supported.

This issue was found using bpf_get_task_stack inside a BPF iterator ("iter/task"), which iterates over all tasks. The single address/frame in the buffer when getting user stacks for tasks that aren't current could not be symbolized (testing multiple symbolizers).

---
 kernel/events/callchain.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/events/callchain.c +0 -0
index 1273be84392c..430fa544fa80 100644
@@ -201,6 +201,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
        }
 
Quoted 6 lines

kernel/events/callchain.c get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, · line 205

Quoted 3 lines
kernel/events/callchain.c +2 -0
        if (user) {
+               if (crosstask)
+                       return NULL;

I think you need that goto exit_put here.

Quoted 3 lines

Why is that? Wouldn't that be the same behavior that already exists? That being said we can probably move this check above get_callchain_entry and exit earlier.

If I read the code right, get_callchain_entry() does expect put_callchain_entry(), which you are breaking with this return NULL.

But indeed, checking it early and bailing out might be the best solution here.

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
kernel/events/callchain.c +1 -5
+
                if (!user_mode(regs)) {
                        if  (current->mm)
                                regs = task_pt_regs(current);
@@ -209,9 +212,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
 
                if (regs) {
-                       if (crosstask)
-                               goto exit_put;
-
                        if (add_mark)
                                perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
 
@@ -219,7 +219,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
        }
 
-exit_put:
        put_callchain_entry(rctx);
 
        return entry;
--

2.39.3

#4 lore raw
Quoted 38 lines · Andrii Nakryiko
Quoted 36 lines · Jordan Rome
Quoted 32 lines · Andrii Nakryiko
Quoted 22 lines · Jordan Rome

Return NULL instead of returning 1 incorrect frame, which currently happens when trying to walk the user stack for any task that isn't current. Returning NULL is a better indicator that this behavior is not supported.

This issue was found using bpf_get_task_stack inside a BPF iterator ("iter/task"), which iterates over all tasks. The single address/frame in the buffer when getting user stacks for tasks that aren't current could not be symbolized (testing multiple symbolizers).

---
 kernel/events/callchain.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/events/callchain.c +0 -0
index 1273be84392c..430fa544fa80 100644

kernel/events/callchain.c get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, · line 205

Quoted 5 lines
kernel/events/callchain.c +2 -0
@@ -201,6 +201,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
        }
 
        if (user) {
+               if (crosstask)
+                       return NULL;

I think you need that goto exit_put here.

Quoted 9 lines
Quoted 3 lines

Why is that? Wouldn't that be the same behavior that already exists? That being said we can probably move this check above get_callchain_entry and exit earlier.

If I read the code right, get_callchain_entry() does expect put_callchain_entry(), which you are breaking with this return NULL.

But indeed, checking it early and bailing out might be the best solution here.

Sounds good. I'll move this check before get_callchain_entry.

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
kernel/events/callchain.c +1 -5
+
                if (!user_mode(regs)) {
                        if  (current->mm)
                                regs = task_pt_regs(current);
@@ -209,9 +212,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
 
                if (regs) {
-                       if (crosstask)
-                               goto exit_put;
-
                        if (add_mark)
                                perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
 
@@ -219,7 +219,6 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
                }
        }
 
-exit_put:
        put_callchain_entry(rctx);
 
        return entry;
--

2.39.3