[PATCH v6 11/18] perf: Use current->flags & PF_KTHREAD instead of current->mm == NULL

Steven Rostedt posted 18 patches 7 months, 3 weeks ago
There is a newer version of this series
[PATCH v6 11/18] perf: Use current->flags & PF_KTHREAD instead of current->mm == NULL
Posted by Steven Rostedt 7 months, 3 weeks ago
From: Steven Rostedt <rostedt@goodmis.org>

To determine if a task is a kernel thread or not, it is more reliable to
use (current->flags & PF_KTHREAD) than to rely on current->mm being NULL.
That is because some kernel tasks (io_uring helpers) may have a mm field.

Link: https://lore.kernel.org/linux-trace-kernel/20250424163607.GE18306@noisy.programming.kicks-ass.net/

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/events/callchain.c | 6 +++---
 kernel/events/core.c      | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c
index abf258913ab6..cda145dc11bd 100644
--- a/kernel/events/callchain.c
+++ b/kernel/events/callchain.c
@@ -246,10 +246,10 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, bool user,
 
 	if (user) {
 		if (!user_mode(regs)) {
-			if  (current->mm)
-				regs = task_pt_regs(current);
-			else
+			if (current->flags & PF_KTHREAD)
 				regs = NULL;
+			else
+				regs = task_pt_regs(current);
 		}
 
 		if (regs) {
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 67581babe9ba..430dd158b1ee 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7989,7 +7989,7 @@ static u64 perf_virt_to_phys(u64 virt)
 		 * Try IRQ-safe get_user_page_fast_only first.
 		 * If failed, leave phys_addr as 0.
 		 */
-		if (current->mm != NULL) {
+		if (!(current->flags & PF_KTHREAD)) {
 			struct page *p;
 
 			pagefault_disable();
-- 
2.47.2
Re: [PATCH v6 11/18] perf: Use current->flags & PF_KTHREAD instead of current->mm == NULL
Posted by Josh Poimboeuf 7 months, 3 weeks ago
On Fri, Apr 25, 2025 at 10:54:33AM -0400, Steven Rostedt wrote:
> From: Steven Rostedt <rostedt@goodmis.org>
> 
> To determine if a task is a kernel thread or not, it is more reliable to
> use (current->flags & PF_KTHREAD) than to rely on current->mm being NULL.
> That is because some kernel tasks (io_uring helpers) may have a mm field.

There's a current->mm check in the user space unwinder (patch 1) which
should probably also be converted.

-- 
Josh
Re: [PATCH v6 11/18] perf: Use current->flags & PF_KTHREAD instead of current->mm == NULL
Posted by Steven Rostedt 7 months, 3 weeks ago
On Fri, 25 Apr 2025 08:09:55 -0700
Josh Poimboeuf <jpoimboe@kernel.org> wrote:

> On Fri, Apr 25, 2025 at 10:54:33AM -0400, Steven Rostedt wrote:
> > From: Steven Rostedt <rostedt@goodmis.org>
> > 
> > To determine if a task is a kernel thread or not, it is more reliable to
> > use (current->flags & PF_KTHREAD) than to rely on current->mm being NULL.
> > That is because some kernel tasks (io_uring helpers) may have a mm field.  
> 
> There's a current->mm check in the user space unwinder (patch 1) which
> should probably also be converted.

Bah! OK.

I also plan on creating a couple of macros and running a coccinelle script
to replace all instances in the kernel of:

  !(task->flags & PF_KTHREAD) into is_user_thread(task)

and

  (task->flags & PF_KTHREAD) into is_kernel_thread(task)

As I've already screwed up missing the '!' (or adding it) a couple of
times. But caught it before posting the patches.

-- Steve