From: Borislav Petkov <bp@alien8.de>
To: Steven Rostedt <rostedt@goodmis.org>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Andrew Morton <akpm@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Ingo Molnar <mingo@redhat.com>,
x86@kernel.org, Kees Cook <kees@kernel.org>,
bpf@vger.kernel.org, Tejun Heo <tj@kernel.org>,
Julia Lawall <Julia.Lawall@inria.fr>,
Nicolas Palix <nicolas.palix@imag.fr>,
cocci@inria.fr
Subject: Re: [RFC][PATCH 1/2] kthread: Add is_user_thread() and is_kernel_thread() helper functions
Date: Sat, 26 Apr 2025 14:08:46 +0300 [thread overview]
Message-ID: <26F4E8D1-4DDF-48EC-AE21-478EDF4C65C3@alien8.de> (raw)
In-Reply-To: <20250425204313.616425861@goodmis.org>
On April 25, 2025 11:41:21 PM GMT+03:00, Steven Rostedt <rostedt@goodmis.org> wrote:
>From: Steven Rostedt <rostedt@goodmis.org>
>
>In order to know if a task is a user thread or a kernel thread it is
>recommended to test the task flags for PF_KTHREAD. The old way was to
>check if the task mm pointer is NULL.
>
>It is an easy mistake to not test the flag correctly, as:
>
> if (!(task->flag & PF_KTHREAD))
>
>Is not immediately obvious that it's testing for a user thread.
>
>Add helper functions:
>
> is_user_thread()
> is_kernel_thread()
>
>that can make seeing what is being tested for much more obvious:
>
> if (is_user_thread(task))
>
>Link: https://lore.kernel.org/all/20250425133416.63d3e3b8@gandalf.local.home/
>
>Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
>---
> include/linux/sched.h | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
>diff --git a/include/linux/sched.h b/include/linux/sched.h
>index f96ac1982893..823f38b0fd3e 100644
>--- a/include/linux/sched.h
>+++ b/include/linux/sched.h
>@@ -1785,6 +1785,16 @@ static __always_inline bool is_percpu_thread(void)
> #endif
> }
>
>+static __always_inline bool is_user_thread(struct task_struct *task)
>+{
>+ return !(task->flags & PF_KTHREAD);
>+}
>+
>+static __always_inline bool is_kernel_thread(struct task_struct *task)
>+{
>+ return task->flags & PF_KTHREAD;
return !is_user_thread(task);
or the other way around.
🙂
--
Sent from a small device: formatting sucks and brevity is inevitable.
next prev parent reply other threads:[~2025-04-26 11:09 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-25 20:41 [RFC][PATCH 0/2] Add is_user_thread() and is_kernel_thread() helper functions Steven Rostedt
2025-04-25 20:41 ` [RFC][PATCH 1/2] kthread: " Steven Rostedt
2025-04-25 23:03 ` Kees Cook
2025-04-26 12:36 ` Steven Rostedt
2025-04-26 11:08 ` Borislav Petkov [this message]
2025-04-26 12:37 ` Steven Rostedt
2025-04-25 20:41 ` [RFC][PATCH 2/2] treewide: Have the task->flags & PF_KTHREAD check use the " Steven Rostedt
2025-04-25 23:09 ` Kees Cook
2025-04-26 3:22 ` Alexei Starovoitov
2025-04-28 18:34 ` Tejun Heo
2025-04-25 23:14 ` [RFC][PATCH 0/2] Add is_user_thread() and is_kernel_thread() " Andrew Morton
2025-04-26 10:41 ` Julia Lawall
2025-04-26 12:43 ` Steven Rostedt
2025-04-26 18:42 ` [PATCH] sched/core: Introduce task_*() helpers for PF_ flags Ingo Molnar
2025-04-26 18:51 ` Ingo Molnar
2025-04-26 20:06 ` Steven Rostedt
2025-04-28 12:12 ` Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=26F4E8D1-4DDF-48EC-AE21-478EDF4C65C3@alien8.de \
--to=bp@alien8.de \
--cc=Julia.Lawall@inria.fr \
--cc=akpm@linux-foundation.org \
--cc=bpf@vger.kernel.org \
--cc=cocci@inria.fr \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=nicolas.palix@imag.fr \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox