lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <202504251558.AA50716@keescook>
Date: Fri, 25 Apr 2025 16:09:08 -0700
From: Kees Cook <kees@...nel.org>
To: Steven Rostedt <rostedt@...dmis.org>
Cc: linux-kernel@...r.kernel.org, linux-trace-kernel@...r.kernel.org,
	Masami Hiramatsu <mhiramat@...nel.org>,
	Mark Rutland <mark.rutland@....com>,
	Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
	Andrew Morton <akpm@...ux-foundation.org>,
	Peter Zijlstra <peterz@...radead.org>,
	Linus Torvalds <torvalds@...ux-foundation.org>,
	Ingo Molnar <mingo@...hat.com>, x86@...nel.org, bpf@...r.kernel.org,
	Tejun Heo <tj@...nel.org>, Julia Lawall <Julia.Lawall@...ia.fr>,
	Nicolas Palix <nicolas.palix@...g.fr>, cocci@...ia.fr
Subject: Re: [RFC][PATCH 2/2] treewide: Have the task->flags & PF_KTHREAD
 check use the helper functions

On Fri, Apr 25, 2025 at 04:41:22PM -0400, Steven Rostedt wrote:
> From: Steven Rostedt <rostedt@...dmis.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@...nel.org>

-- 
Kees Cook

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ