On Thu, 26 Jun 2025 07:48:40 -0600 Jens Axboe <ax...@kernel.dk> wrote:
> On 6/25/25 5:15 PM, Steven Rostedt wrote: > > From: Steven Rostedt <rost...@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. > > This commit message is very odd, imho, and wrong. To check if it's a > kernel thread yes you should use PF_KTHREAD, but that has nothing to do Yeah, I figured this was wrong when I saw your reply in the other thread. That's why I Cc'd you on this. [ For those interested in what that other thread is: https://lore.kernel.org/all/20250624130744.602c5...@batman.local.home/ ] > with PF_USER_WORKER. In fact, as mentioned in a previous reply, > current->mm may be non-NULL for a kthread as well, if it's done > kthread_use_mm(). > > If the current check for "is kernel thread" was using ->mm to gauge > then, then the current check was just wrong, period. Yes, but unfortunately, that was a way a task was checked to see if it was a kernel thread or not. Which was right "most of the time". But it's wrong to use that, because it can be wrong "some of the time" :-p Which brings us to this discussion. I believe Peter was under the assumption that we should not use current->mm to see if it's a user task or not, and use PF_KTHREAD instead. But for perf, a user task is something that will return back to user space, as the idea is to profile the user space stack trace. You said that PF_USER_WORKER never came from user space, so from the perf point of view, it *is* a kernel thread, and we don't want to treat it as a user space one. If we check current->mm to be a user space task, or if we check for PF_KTHREAD to be a kernel task, we are wrong in both cases when it comes to a task marked as PF_USER_WORKER. This brings up having a function like "is_kernel_thread()" (or remove the 'is' if people don't like that) that returns true if the task *only* runs in the kernel. -- Steve