All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	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, 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 2/2] treewide: Have the task->flags & PF_KTHREAD check use the helper functions
Date: Fri, 25 Apr 2025 16:09:08 -0700	[thread overview]
Message-ID: <202504251558.AA50716@keescook> (raw)
In-Reply-To: <20250425204313.784243618@goodmis.org>

On Fri, Apr 25, 2025 at 04:41:22PM -0400, Steven Rostedt wrote:
> From: Steven Rostedt <rostedt@goodmis.org>
> 
> Getting the check if a task is a kernel thread or a user thread can be
> error prone as it's not easy to see the difference.
> 
> 	if (!(task->flags & PF_KTHREAD))
> 
> Is not immediately obvious that it's checking for a user thread.
> 
> 	if (is_user_thread(task))
> 
> Is much easier to review, as it is obvious that it is checking if the task
> is a user thread.
> 
> Using a coccinelle script, convert these checks over to using either
> is_user_thread() or is_kernel_thread().
> 
>   $ cat kthread.cocci
>   @@
>   identifier task;
>   @@
>   -	!(task->flags & PF_KTHREAD)
>   +	is_user_thread(task)
>   @@
>   identifier task;
>   @@
>   -	(task->flags & PF_KTHREAD) == 0
>   +	is_user_thread(task)
>   @@
>   identifier task;
>   @@
>   -	(task->flags & PF_KTHREAD) != 0
>   +	is_kernel_thread(task)
>   @@
>   identifier task;
>   @@
>   -	task->flags & PF_KTHREAD
>   +	is_kernel_thread(task)
> 
>   $ spatch --dir --include-headers kthread.cocci . > /tmp/t.patch
>   $ patch -p1 < /tmp/t.patch
> 
> Make sure to undo the conversion of the helper functions themselves!
> 
>   $ git show include/linux/sched.h | patch -p1 -R

FYI, the "file in" test can be helpful. I use it to exclude tools and
samples regularly, and *I think* it would work for excluding individual
files too:

@name_of_rule depends !(file in "tools") && !(file in "samples")@

I've been collecting random notes like this here:

https://github.com/kees/kernel-tools/tree/trunk/coccinelle

>  tools/sched_ext/scx_central.bpf.c          |  2 +-
>  tools/sched_ext/scx_flatcg.bpf.c           |  2 +-
>  tools/sched_ext/scx_qmap.bpf.c             |  2 +-

I think these are fine. The Makefile is pulling in standard kbuild
Makefiles, so I think the correct include directories (outside of
tools/) are being used.

But yeah, easy mechanical change and a readability improvement. :)

Reviewed-by: Kees Cook <kees@kernel.org>

-- 
Kees Cook

  reply	other threads:[~2025-04-25 23:09 UTC|newest]

Thread overview: 18+ 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
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 [this message]
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 10:41     ` [cocci] " 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=202504251558.AA50716@keescook \
    --to=kees@kernel.org \
    --cc=Julia.Lawall@inria.fr \
    --cc=akpm@linux-foundation.org \
    --cc=bpf@vger.kernel.org \
    --cc=cocci@inria.fr \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.