public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH 0/2] Add is_user_thread() and is_kernel_thread() helper functions
@ 2025-04-25 20:41 Steven Rostedt
  2025-04-25 20:41 ` [RFC][PATCH 1/2] kthread: " Steven Rostedt
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Steven Rostedt @ 2025-04-25 20:41 UTC (permalink / raw)
  To: linux-kernel, linux-trace-kernel
  Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Andrew Morton,
	Peter Zijlstra, Linus Torvalds, Ingo Molnar, x86, Kees Cook, bpf,
	Tejun Heo, Julia Lawall, Nicolas Palix, cocci


While working on the deferred stacktrace code, Peter Zijlstra told
me to use task->flags & PF_KTHREAD instead of checking task->mm for NULL.
This seemed reasonable, but while working on it, as there were several
places that check if the task is a kernel thread and other places that
check if the task is a user space thread I found it a bit confusing
when looking at both:

	if (task->flags & PF_KTHREAD)
and
	if (!(task->flags & PF_KTHREAD))

Where I mixed them up sometimes, and checked for a user space thread when I
really wanted to check for a kernel thread. I found these mistakes before
sending out my patches, but going back and reviewing the code, I always had
to stop and spend a few unnecessary seconds making sure the check was
testing that flag correctly.

To make this a bit more obvious, I introduced two helper functions:

	is_user_thread(task)
	is_kernel_thread(task)

which simply test the flag for you. Thus, seeing:

	if (is_user_thread(task))
or
	if (is_kernel_thread(task))

it was very obvious to which test you wanted to make.

I then created a coccinelle script to change all the checks throughout the
kernel to use one of these macros.

      $ 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

It did modify the tools/sched_ext code, and I'm not sure if that's OK
or not. Does it still use the sched.h header? If so, it should be fine.
But if it is an issue, I can undo the changes to tools as well.


Steven Rostedt (2):
      kthread: Add is_user_thread() and is_kernel_thread() helper functions
      treewide: Have the task->flags & PF_KTHREAD check use the helper functions

----
 arch/arm/mm/init.c                         |  2 +-
 arch/arm64/include/asm/uaccess.h           |  2 +-
 arch/arm64/kernel/process.c                |  2 +-
 arch/arm64/kernel/proton-pack.c            |  2 +-
 arch/mips/kernel/process.c                 |  2 +-
 arch/powerpc/kernel/process.c              |  2 +-
 arch/powerpc/kernel/stacktrace.c           |  2 +-
 arch/x86/kernel/fpu/core.c                 |  2 +-
 arch/x86/kernel/process.c                  |  2 +-
 block/blk-cgroup.c                         |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h |  2 +-
 drivers/md/dm-vdo/logger.c                 |  2 +-
 drivers/tty/sysrq.c                        |  2 +-
 fs/bcachefs/clock.c                        |  2 +-
 fs/bcachefs/journal_reclaim.c              |  2 +-
 fs/bcachefs/move.c                         |  6 +++---
 fs/exec.c                                  |  2 +-
 fs/file_table.c                            |  2 +-
 fs/namespace.c                             |  2 +-
 fs/proc/array.c                            |  4 ++--
 fs/proc/base.c                             |  6 +++---
 include/linux/sched.h                      | 10 ++++++++++
 io_uring/io_uring.c                        |  2 +-
 kernel/cgroup/cgroup.c                     |  6 +++---
 kernel/cgroup/freezer.c                    |  4 ++--
 kernel/events/core.c                       |  2 +-
 kernel/exit.c                              |  2 +-
 kernel/fork.c                              |  6 +++---
 kernel/freezer.c                           |  4 ++--
 kernel/futex/pi.c                          |  2 +-
 kernel/kthread.c                           | 12 ++++++------
 kernel/livepatch/transition.c              |  2 +-
 kernel/power/process.c                     |  2 +-
 kernel/sched/core.c                        |  6 +++---
 kernel/sched/idle.c                        |  2 +-
 kernel/sched/sched.h                       |  4 ++--
 kernel/signal.c                            |  4 ++--
 kernel/stacktrace.c                        |  2 +-
 lib/is_single_threaded.c                   |  2 +-
 mm/memcontrol.c                            |  2 +-
 mm/oom_kill.c                              |  4 ++--
 mm/page_alloc.c                            |  2 +-
 mm/vmscan.c                                |  2 +-
 security/keys/request_key.c                |  2 +-
 security/smack/smack_access.c              |  2 +-
 security/smack/smack_lsm.c                 |  4 ++--
 security/tomoyo/network.c                  |  2 +-
 security/yama/yama_lsm.c                   |  2 +-
 tools/sched_ext/scx_central.bpf.c          |  2 +-
 tools/sched_ext/scx_flatcg.bpf.c           |  2 +-
 tools/sched_ext/scx_qmap.bpf.c             |  2 +-
 51 files changed, 82 insertions(+), 72 deletions(-)

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2025-04-28 18:34 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox