Skip to contentSkip to conversation

kthread detection cleanup

PATCH In review Mark Rutland 8 participants 25 messages Aug 14, 2019 – Aug 21, 2019

Subsystems (46)
lore
Share & tools
lore

Mark Rutland

author maintainer
#0 lore raw

cover kthread detection cleanup

A reasonable amount of kernel code looks at task_struct::mm to determine whether a thread is a kthread or a real user task. This isn't quite right, since kthreads can have a non-NULL mm when calling use_mm().

The correct way to check whether a task is a kthread is to check whether PF_KTHREAD is set in task_struct::flags, but doing so is a bit unwieldy.

To make this a bit nicer, this series adds a new is_kthread(tsk) helper, converts existing code to make use of it, and fixes up a number of erroneous checks of current->mm. Hopefully this will push people to use the right approach in future.

I'm sure there are other instances in the kernel tree where we don't check this correctly. In this series I'm just trying to fix the instances I'm reasonably confident are incorrect.

This series is based on v5.3-rc3, and I've pushed it out to my sched/kthread-cleanup branch on kernel.org [1,2].

Thanks,
Mark.

[1] git://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git sched/kthread-cleanup
[2] https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/log/?h=sched/kthread-cleanup

Mark Rutland (9):
sched/core: add is_kthread() helper
sched: treewide: use is_kthread()
kmemleak: correctly check for kthreads
arm/perf: correctly check for kthreads
arm64: correctly check for ktrheads
sparc/perf: correctly check for kthreads
x86/lbr: correctly check for kthreads
x86/fpu: correctly check for kthreads
samples/kretprobe: correctly check for kthreads

 arch/alpha/kernel/process.c         |  2 +-
 arch/arc/kernel/process.c           |  2 +-
 arch/arm/kernel/perf_callchain.c    |  2 +-
 arch/arm/kernel/process.c           |  2 +-
 arch/arm/mm/init.c                  |  2 +-
 arch/arm64/kernel/process.c         |  6 +++---
 arch/c6x/kernel/process.c           |  2 +-
 arch/csky/kernel/process.c          |  2 +-
 arch/h8300/kernel/process.c         |  2 +-
 arch/hexagon/kernel/process.c       |  2 +-
 arch/ia64/kernel/process.c          |  2 +-
 arch/m68k/kernel/process.c          |  2 +-
 arch/microblaze/kernel/process.c    |  2 +-
 arch/mips/kernel/process.c          |  4 ++--
 arch/nds32/kernel/process.c         |  4 ++--
 arch/nios2/kernel/process.c         |  2 +-
 arch/openrisc/kernel/process.c      |  2 +-
 arch/parisc/kernel/process.c        |  2 +-
 arch/powerpc/kernel/process.c       |  2 +-
 arch/riscv/kernel/process.c         |  2 +-
 arch/s390/kernel/process.c          |  2 +-
 arch/sh/kernel/process_32.c         |  2 +-
 arch/sh/kernel/process_64.c         |  2 +-
 arch/sparc/kernel/perf_event.c      |  2 +-
 arch/sparc/kernel/process_32.c      |  2 +-
 arch/sparc/kernel/process_64.c      |  2 +-
 arch/um/kernel/process.c            |  2 +-
 arch/unicore32/kernel/process.c     |  2 +-
 arch/x86/events/intel/lbr.c         |  2 +-
 arch/x86/include/asm/fpu/internal.h |  2 +-
 arch/x86/kernel/fpu/core.c          |  2 +-
 arch/x86/kernel/process_32.c        |  2 +-
 arch/x86/kernel/process_64.c        |  2 +-
 arch/xtensa/kernel/process.c        |  2 +-
 block/blk-cgroup.c                  |  2 +-
 drivers/tty/sysrq.c                 |  2 +-
 fs/coredump.c                       |  2 +-
 fs/file_table.c                     |  4 ++--
 fs/namespace.c                      |  2 +-
 fs/proc/base.c                      |  4 ++--
 include/linux/cgroup.h              |  2 +-
 include/linux/sched.h               | 13 ++++++++++++-
 kernel/cgroup/freezer.c             |  4 ++--
 kernel/events/core.c                |  2 +-
 kernel/exit.c                       |  2 +-
 kernel/fork.c                       |  6 +++---
 kernel/freezer.c                    |  4 ++--
 kernel/futex.c                      |  2 +-
 kernel/kthread.c                    |  6 +++---
 kernel/livepatch/transition.c       |  2 +-
 kernel/ptrace.c                     |  2 +-
 kernel/sched/core.c                 |  8 ++++----
 kernel/sched/idle.c                 |  2 +-
 kernel/sched/wait.c                 |  2 +-
 kernel/signal.c                     |  2 +-
 kernel/stacktrace.c                 |  2 +-
 lib/is_single_threaded.c            |  2 +-
 mm/kmemleak.c                       |  6 +++---
 mm/memcontrol.c                     |  2 +-
 mm/oom_kill.c                       |  4 ++--
 mm/page_alloc.c                     |  2 +-
 mm/vmacache.c                       |  2 +-
 mm/vmscan.c                         |  2 +-
 samples/kprobes/kretprobe_example.c |  2 +-
 security/smack/smack_access.c       |  2 +-
 security/smack/smack_lsm.c          |  4 ++--
 security/yama/yama_lsm.c            |  2 +-
 67 files changed, 97 insertions(+), 86 deletions(-)
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 1/9 sched/core: add is_kthread() helper

Code checking whether a task is a kthread isn't very consistent. Some code correctly tests task->flags & PF_THREAD, while other code checks task->mm (which can be true for a kthread which calls use_mm()).

So that we can clean this up and keep the code easy to follow, let's add an obvious helper function to test whether a task is a kthread. Subsequent patches will use this as part of cleaning up and correcting open-coded tests.

At the same time, let's fix up the kerneldoc for is_idle_task() for consistency with the new helper, using true/false rather than 0/1, given the functions return bool.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/sched.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
include/linux/sched.h +12 -1
index 9f51932bd543..b7e96409d75f 100644
@@ -1621,13 +1621,24 @@ extern struct task_struct *idle_task(int cpu);
  * is_idle_task - is the specified task an idle task?
  * @p: the task in question.
  *
- * Return: 1 if @p is an idle task. 0 otherwise.
+ * Return: true if @p is an idle task, false otherwise.
  */
 static inline bool is_idle_task(const struct task_struct *p)
 {
 	return !!(p->flags & PF_IDLE);
 }
 
+/**
+ * is_kthread - is the specified task a kthread
+ * @p: the task in question.
+ *
+ * Return: true if @p is a kthread, false otherwise.
+ */
+static inline bool is_kthread(const struct task_struct *p)
+{
+	return !!(p->flags & PF_KTHREAD);
+}
+
 extern struct task_struct *curr_task(int cpu);
 extern void ia64_set_curr_task(int cpu, struct task_struct *p);
 
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH 1/9] sched/core: add is_kthread() helper

Hi Mark,

Quoted 9 lines · Mark Rutland

Code checking whether a task is a kthread isn't very consistent. Some code correctly tests task->flags & PF_THREAD, while other code checks task->mm (which can be true for a kthread which calls use_mm()).

So that we can clean this up and keep the code easy to follow, let's add an obvious helper function to test whether a task is a kthread. Subsequent patches will use this as part of cleaning up and correcting open-coded tests.

Quoted 5 lines

At the same time, let's fix up the kerneldoc for is_idle_task() for consistency with the new helper, using true/false rather than 0/1, given the functions return bool.

Thanks for your patch!

Quoted 13 lines
include/linux/sched.h +1 -1
@@ -1621,13 +1621,24 @@ extern struct task_struct *idle_task(int cpu);
  * is_idle_task - is the specified task an idle task?
  * @p: the task in question.
  *
- * Return: 1 if @p is an idle task. 0 otherwise.
+ * Return: true if @p is an idle task, false otherwise.
  */
 static inline bool is_idle_task(const struct task_struct *p)
 {
        return !!(p->flags & PF_IDLE);
 }

include/linux/sched.h extern struct task_struct *idle_task(int cpu); · line 1639

Quoted 10 lines
include/linux/sched.h +9 -0
 
+/**
+ * is_kthread - is the specified task a kthread
+ * @p: the task in question.
+ *
+ * Return: true if @p is a kthread, false otherwise.
+ */
+static inline bool is_kthread(const struct task_struct *p)
+{
+       return !!(p->flags & PF_KTHREAD);

The !! is not really needed.
Probably you followed is_idle_task() above (where it's also not needed).

include/linux/sched.h extern struct task_struct *idle_task(int cpu); · line 1641

Quoted 2 lines
include/linux/sched.h +2 -0
+}
+

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#3 lore raw

Re: [PATCH 1/9] sched/core: add is_kthread() helper

Quoted 34 lines · Geert Uytterhoeven

Hi Mark,

Quoted 9 lines · Mark Rutland

Code checking whether a task is a kthread isn't very consistent. Some code correctly tests task->flags & PF_THREAD, while other code checks task->mm (which can be true for a kthread which calls use_mm()).

So that we can clean this up and keep the code easy to follow, let's add an obvious helper function to test whether a task is a kthread. Subsequent patches will use this as part of cleaning up and correcting open-coded tests.

Quoted 5 lines

At the same time, let's fix up the kerneldoc for is_idle_task() for consistency with the new helper, using true/false rather than 0/1, given the functions return bool.

Thanks for your patch!

Quoted 14 lines
include/linux/sched.h +1 -1
@@ -1621,13 +1621,24 @@ extern struct task_struct *idle_task(int cpu);
  * is_idle_task - is the specified task an idle task?
  * @p: the task in question.
  *
- * Return: 1 if @p is an idle task. 0 otherwise.
+ * Return: true if @p is an idle task, false otherwise.
  */
 static inline bool is_idle_task(const struct task_struct *p)
 {
        return !!(p->flags & PF_IDLE);
 }
 
Quoted 12 lines

include/linux/sched.h extern struct task_struct *idle_task(int cpu); · line 1639

Quoted 9 lines
include/linux/sched.h +9 -0
+/**
+ * is_kthread - is the specified task a kthread
+ * @p: the task in question.
+ *
+ * Return: true if @p is a kthread, false otherwise.
+ */
+static inline bool is_kthread(const struct task_struct *p)
+{
+       return !!(p->flags & PF_KTHREAD);

The !! is not really needed.
Probably you followed is_idle_task() above (where it's also not needed).

Indeed! I'm aware of the implicit bool conversion, but kept that for consistency.

Peter, Ingo, do you have a preference?

Thanks,
Mark.

#4 lore raw

Re: [PATCH 1/9] sched/core: add is_kthread() helper

Quoted 48 lines · Mark Rutland
Quoted 34 lines · Geert Uytterhoeven

Hi Mark,

Quoted 9 lines · Mark Rutland

Code checking whether a task is a kthread isn't very consistent. Some code correctly tests task->flags & PF_THREAD, while other code checks task->mm (which can be true for a kthread which calls use_mm()).

So that we can clean this up and keep the code easy to follow, let's add an obvious helper function to test whether a task is a kthread. Subsequent patches will use this as part of cleaning up and correcting open-coded tests.

Quoted 5 lines

At the same time, let's fix up the kerneldoc for is_idle_task() for consistency with the new helper, using true/false rather than 0/1, given the functions return bool.

Thanks for your patch!

Quoted 14 lines
include/linux/sched.h +1 -1
@@ -1621,13 +1621,24 @@ extern struct task_struct *idle_task(int cpu);
  * is_idle_task - is the specified task an idle task?
  * @p: the task in question.
  *
- * Return: 1 if @p is an idle task. 0 otherwise.
+ * Return: true if @p is an idle task, false otherwise.
  */
 static inline bool is_idle_task(const struct task_struct *p)
 {
        return !!(p->flags & PF_IDLE);
 }
 
Quoted 12 lines

include/linux/sched.h extern struct task_struct *idle_task(int cpu); · line 1639

Quoted 9 lines
include/linux/sched.h +9 -0
+/**
+ * is_kthread - is the specified task a kthread
+ * @p: the task in question.
+ *
+ * Return: true if @p is a kthread, false otherwise.
+ */
+static inline bool is_kthread(const struct task_struct *p)
+{
+       return !!(p->flags & PF_KTHREAD);

The !! is not really needed.
Probably you followed is_idle_task() above (where it's also not needed).

Quoted 4 lines

Indeed! I'm aware of the implicit bool conversion, but kept that for consistency.

Peter, Ingo, do you have a preference?

So the !! pattern is useful where the return value is an integer (i.e. there's a risk of non-bool use) - but the return value is an explicit bool here, so !! is IMO an entirely superfluous obfuscation.

Should probably be fixed for is_idle_task() as well?

Thanks,

Ingo

maintainer
#5 lore raw

Re: [PATCH 1/9] sched/core: add is_kthread() helper

Quoted 11 lines · Ingo Molnar
Quoted 10 lines · Mark Rutland
Quoted 8 lines · Geert Uytterhoeven

include/linux/sched.h extern struct task_struct *idle_task(int cpu); · line 1639

Quoted 3 lines · Mark Rutland
include/linux/sched.h +3 -0
+static inline bool is_kthread(const struct task_struct *p)
+{
+       return !!(p->flags & PF_KTHREAD);

The !! is not really needed.
Probably you followed is_idle_task() above (where it's also not needed).

Quoted 8 lines
Quoted 4 lines

Indeed! I'm aware of the implicit bool conversion, but kept that for consistency.

Peter, Ingo, do you have a preference?

So the !! pattern is useful where the return value is an integer (i.e. there's a risk of non-bool use) - but the return value is an explicit bool here, so !! is IMO an entirely superfluous obfuscation.

Yeah, no real preference, for giggles, (_Bool) also seems to work.

author maintainer
#6 lore raw

PATCH 2/9 sched: treewide: use is_kthread()

In reply to Mark Rutland

Now that we have is_kthread(), let's convert existing open-coded checks of the form:

task->flags & PF_KTHREAD

... over to the new helper, which makes things a little easier to read, and sets a consistent example for new code to follow.

Generated with coccinelle:

----
virtual patch

@ depends on patch @
expression E;
@@

- (E->flags & PF_KTHREAD)
+ is_kthread(E)
----

... though this didn't pick up the instance in <linux/cgroup.h>, which I fixed up manually.

Instances checking multiple PF_* flags at ocne are left as-is for now.

There should be no functional change as a result of this patch.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/alpha/kernel/process.c      | 2 +-
 arch/arc/kernel/process.c        | 2 +-
 arch/arm/kernel/process.c        | 2 +-
 arch/arm/mm/init.c               | 2 +-
 arch/arm64/kernel/process.c      | 4 ++--
 arch/c6x/kernel/process.c        | 2 +-
 arch/csky/kernel/process.c       | 2 +-
 arch/h8300/kernel/process.c      | 2 +-
 arch/hexagon/kernel/process.c    | 2 +-
 arch/ia64/kernel/process.c       | 2 +-
 arch/m68k/kernel/process.c       | 2 +-
 arch/microblaze/kernel/process.c | 2 +-
 arch/mips/kernel/process.c       | 4 ++--
 arch/nds32/kernel/process.c      | 4 ++--
 arch/nios2/kernel/process.c      | 2 +-
 arch/openrisc/kernel/process.c   | 2 +-
 arch/parisc/kernel/process.c     | 2 +-
 arch/powerpc/kernel/process.c    | 2 +-
 arch/riscv/kernel/process.c      | 2 +-
 arch/s390/kernel/process.c       | 2 +-
 arch/sh/kernel/process_32.c      | 2 +-
 arch/sh/kernel/process_64.c      | 2 +-
 arch/sparc/kernel/process_32.c   | 2 +-
 arch/sparc/kernel/process_64.c   | 2 +-
 arch/um/kernel/process.c         | 2 +-
 arch/unicore32/kernel/process.c  | 2 +-
 arch/x86/kernel/fpu/core.c       | 2 +-
 arch/x86/kernel/process_32.c     | 2 +-
 arch/x86/kernel/process_64.c     | 2 +-
 arch/xtensa/kernel/process.c     | 2 +-
 block/blk-cgroup.c               | 2 +-
 drivers/tty/sysrq.c              | 2 +-
 fs/coredump.c                    | 2 +-
 fs/file_table.c                  | 4 ++--
 fs/namespace.c                   | 2 +-
 fs/proc/base.c                   | 4 ++--
 include/linux/cgroup.h           | 2 +-
 kernel/cgroup/freezer.c          | 4 ++--
 kernel/events/core.c             | 2 +-
 kernel/exit.c                    | 2 +-
 kernel/fork.c                    | 6 +++---
 kernel/freezer.c                 | 4 ++--
 kernel/futex.c                   | 2 +-
 kernel/kthread.c                 | 6 +++---
 kernel/livepatch/transition.c    | 2 +-
 kernel/ptrace.c                  | 2 +-
 kernel/sched/core.c              | 8 ++++----
 kernel/sched/idle.c              | 2 +-
 kernel/sched/wait.c              | 2 +-
 kernel/signal.c                  | 2 +-
 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/vmacache.c                    | 2 +-
 mm/vmscan.c                      | 2 +-
 security/smack/smack_access.c    | 2 +-
 security/smack/smack_lsm.c       | 4 ++--
 security/yama/yama_lsm.c         | 2 +-
 60 files changed, 76 insertions(+), 76 deletions(-)
arch/alpha/kernel/process.c +1 -1
index 48b81d015d8a..e99ec5fab586 100644
@@ -251,7 +251,7 @@ copy_thread(unsigned long clone_flags, unsigned long usp,
 	childti->pcb.ksp = (unsigned long) childstack;
 	childti->pcb.flags = 1;	/* set FEN, clear everything else */
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(childstack, 0,
 			sizeof(struct switch_stack) + sizeof(struct pt_regs));
arch/arc/kernel/process.c +1 -1
index e1889ce3faf9..f4cf6e9311cd 100644
@@ -200,7 +200,7 @@ int copy_thread(unsigned long clone_flags,
 	childksp[0] = 0;			/* fp */
 	childksp[1] = (unsigned long)ret_from_fork; /* blink */
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(c_regs, 0, sizeof(struct pt_regs));
 
 		c_callee->r13 = kthread_arg;
arch/arm/kernel/process.c +1 -1
index f934a6739fc0..899ead9b1336 100644
@@ -242,7 +242,7 @@ copy_thread(unsigned long clone_flags, unsigned long stack_start,
 	thread->cpu_domain = get_domain();
 #endif
 
-	if (likely(!(p->flags & PF_KTHREAD))) {
+	if (likely(!is_kthread(p))) {
 		*childregs = *current_pt_regs();
 		childregs->ARM_r0 = 0;
 		if (stack_start)
arch/arm/mm/init.c +1 -1
index 16d373d587c4..0b568802d400 100644
@@ -625,7 +625,7 @@ static void update_sections_early(struct section_perm perms[], int n)
 	struct task_struct *t, *s;
 
 	for_each_process(t) {
-		if (t->flags & PF_KTHREAD)
+		if (is_kthread(t))
 			continue;
 		for_each_thread(t, s)
 			set_section_perms(perms, n, true, s->mm);
arch/arm64/kernel/process.c +2 -2
index f674f28df663..288012687c29 100644
@@ -367,7 +367,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
 	 */
 	fpsimd_flush_task_state(p);
 
-	if (likely(!(p->flags & PF_KTHREAD))) {
+	if (likely(!is_kthread(p))) {
 		*childregs = *current_pt_regs();
 		childregs->regs[0] = 0;
 
@@ -454,7 +454,7 @@ static void ssbs_thread_switch(struct task_struct *next)
 	 * Nothing to do for kernel threads, but 'regs' may be junk
 	 * (e.g. idle task) so check the flags and bail early.
 	 */
-	if (unlikely(next->flags & PF_KTHREAD))
+	if (unlikely(is_kthread(next)))
 		return;
 
 	/* If the mitigation is enabled, then we leave SSBS clear. */
arch/c6x/kernel/process.c +1 -1
index cb9c8b63cddd..942bc45b7395 100644
@@ -112,7 +112,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 
 	childregs = task_pt_regs(p);
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* case of  __kernel_thread: we return to supervisor space */
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->sp = (unsigned long)(childregs + 1);
arch/csky/kernel/process.c +1 -1
index e555740c0be5..671b061ee785 100644
@@ -52,7 +52,7 @@ int copy_thread(unsigned long clone_flags,
 	/* setup ksp for switch_to !!! */
 	p->thread.ksp = (unsigned long)childstack;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childstack->r15 = (unsigned long) ret_from_kernel_thread;
 		childstack->r8 = kthread_arg;
arch/h8300/kernel/process.c +1 -1
index e35cdf092e07..e9beda6d5760 100644
@@ -114,7 +114,7 @@ int copy_thread(unsigned long clone_flags,
 
 	childregs = (struct pt_regs *) (THREAD_SIZE + task_stack_page(p)) - 1;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->retpc = (unsigned long) ret_from_kernel_thread;
 		childregs->er4 = topstk; /* arg */
arch/hexagon/kernel/process.c +1 -1
index ac07f5f4b76b..1f9e6787d07d 100644
@@ -73,7 +73,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 						    sizeof(*ss));
 	ss->lr = (unsigned long)ret_from_fork;
 	p->thread.switch_sp = ss;
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		/* r24 <- fn, r25 <- arg */
 		ss->r24 = usp;
arch/ia64/kernel/process.c +1 -1
index 968b5f33e725..3d75db83b333 100644
@@ -376,7 +376,7 @@ copy_thread(unsigned long clone_flags,
 
 	ia64_drop_fpu(p);	/* don't pick up stale state from a CPU's fph */
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		if (unlikely(!user_stack_base)) {
 			/* fork_idle() called us */
 			return 0;
arch/m68k/kernel/process.c +1 -1
index 4e77a06735c1..1adacbf17513 100644
@@ -138,7 +138,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 	 */
 	p->thread.fs = get_fs().seg;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(frame, 0, sizeof(struct fork_frame));
 		frame->regs.sr = PS_S;
arch/microblaze/kernel/process.c +1 -1
index 6527ec22f158..e5f5d4c04152 100644
@@ -60,7 +60,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 	struct pt_regs *childregs = task_pt_regs(p);
 	struct thread_info *ti = task_thread_info(p);
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* if we're creating a new kernel thread then just zeroing all
 		 * the registers. That's OK for a brand new thread.*/
 		memset(childregs, 0, sizeof(struct pt_regs));
arch/mips/kernel/process.c +2 -2
index 339870ed92f7..8a5a2216558e 100644
@@ -87,7 +87,7 @@ void exit_thread(struct task_struct *tsk)
 	 * User threads may have allocated a delay slot emulation frame.
 	 * If so, clean up that allocation.
 	 */
-	if (!(current->flags & PF_KTHREAD))
+	if (!is_kthread(current))
 		dsemul_thread_cleanup(tsk);
 }
 
@@ -132,7 +132,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long usp,
 	/*  Put the stack after the struct pt_regs.  */
 	childksp = (unsigned long) childregs;
 	p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		unsigned long status = p->thread.cp0_status;
 		memset(childregs, 0, sizeof(struct pt_regs));
arch/nds32/kernel/process.c +2 -2
index 9712fd474f2c..5d88ca5d3621 100644
@@ -156,7 +156,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
 
 	memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		/* kernel thread fn */
 		p->thread.cpu_context.r6 = stack_start;
@@ -207,7 +207,7 @@ struct task_struct *_switch_fpu(struct task_struct *prev, struct task_struct *ne
 #if !IS_ENABLED(CONFIG_LAZY_FPU)
 	unlazy_fpu(prev);
 #endif
-	if (!(next->flags & PF_KTHREAD))
+	if (!is_kthread(next))
 		clear_fpu(task_pt_regs(next));
 	return prev;
 }
arch/nios2/kernel/process.c +1 -1
index 509e7855e8dc..f13eb8c23fc8 100644
@@ -109,7 +109,7 @@ int copy_thread(unsigned long clone_flags,
 	struct switch_stack *childstack =
 		((struct switch_stack *)childregs) - 1;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childstack, 0,
 			sizeof(struct switch_stack) + sizeof(struct pt_regs));
 
arch/openrisc/kernel/process.c +1 -1
index b06f84f6676f..cf79967202dd 100644
@@ -168,7 +168,7 @@ copy_thread(unsigned long clone_flags, unsigned long usp,
 	sp -= sizeof(struct pt_regs);
 	kregs = (struct pt_regs *)sp;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(kregs, 0, sizeof(struct pt_regs));
 		kregs->gpr[20] = usp; /* fn, kernel thread */
 		kregs->gpr[22] = arg;
arch/parisc/kernel/process.c +1 -1
index ecc5c2771208..774ebf61323e 100644
@@ -220,7 +220,7 @@ copy_thread(unsigned long clone_flags, unsigned long usp,
 	extern void * const ret_from_kernel_thread;
 	extern void * const child_return;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(cregs, 0, sizeof(struct pt_regs));
 		if (!usp) /* idle thread */
arch/powerpc/kernel/process.c +1 -1
index 8fc4de0d22b4..411c7e8046eb 100644
@@ -1615,7 +1615,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 	/* Copy registers */
 	sp -= sizeof(struct pt_regs);
 	childregs = (struct pt_regs *) sp;
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->gpr[1] = sp + sizeof(struct pt_regs);
arch/riscv/kernel/process.c +1 -1
index f23794bd1e90..08b6dfc6dc54 100644
@@ -96,7 +96,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 	struct pt_regs *childregs = task_pt_regs(p);
 
 	/* p->thread holds context to be restored by __switch_to() */
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* Kernel thread */
 		const register unsigned long gp __asm__ ("gp");
 		memset(childregs, 0, sizeof(struct pt_regs));
arch/s390/kernel/process.c +1 -1
index 63873aa6693f..4802a4014f15 100644
@@ -113,7 +113,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long new_stackp,
 	frame->sf.gprs[9] = (unsigned long) frame;
 
 	/* Store access registers to kernel stack of new process. */
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(&frame->childregs, 0, sizeof(struct pt_regs));
 		frame->childregs.psw.mask = PSW_KERNEL_BITS | PSW_MASK_DAT |
arch/sh/kernel/process_32.c +1 -1
index a094633874c3..8cd8f2025bf0 100644
@@ -137,7 +137,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 
 	childregs = task_pt_regs(p);
 	p->thread.sp = (unsigned long) childregs;
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		p->thread.pc = (unsigned long) ret_from_kernel_thread;
 		childregs->regs[4] = arg;
arch/sh/kernel/process_64.c +1 -1
index c2844a2e18cd..9c9c6b1efc17 100644
@@ -389,7 +389,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 	childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
 	p->thread.sp = (unsigned long) childregs;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(childregs, 0, sizeof(struct pt_regs));
 		childregs->regs[2] = (unsigned long)arg;
 		childregs->regs[3] = (unsigned long)usp;
arch/sparc/kernel/process_32.c +1 -1
index 26cca65e9246..0c9ec7c8b8ad 100644
@@ -338,7 +338,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
 	ti->ksp = (unsigned long) new_stack;
 	p->thread.kregs = childregs;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		extern int nwindows;
 		unsigned long psr;
 		memset(new_stack, 0, STACKFRAME_SZ + TRACEREG_SZ);
arch/sparc/kernel/process_64.c +1 -1
index 4282116e28e7..e98db3ba97c1 100644
@@ -632,7 +632,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
 				       sizeof(struct sparc_stackf));
 	t->fpsaved[0] = 0;
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		memset(child_trap_frame, 0, child_stack_sz);
 		__thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP] = 
 			(current_pt_regs()->tstate + 1) & TSTATE_CWP;
arch/um/kernel/process.c +1 -1
index 67c0d1a860e9..58d26a08719d 100644
@@ -157,7 +157,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
 		unsigned long arg, struct task_struct * p)
 {
 	void (*handler)(void);
-	int kthread = current->flags & PF_KTHREAD;
+	int kthread = is_kthread(current);
 	int ret = 0;
 
 	p->thread = (struct thread_struct) INIT_THREAD;
arch/unicore32/kernel/process.c +1 -1
index b4fd3a604a18..a48ee8b317b9 100644
@@ -228,7 +228,7 @@ copy_thread(unsigned long clone_flags, unsigned long stack_start,
 
 	memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
 	thread->cpu_context.sp = (unsigned long)childregs;
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		thread->cpu_context.pc = (unsigned long)ret_from_kernel_thread;
 		thread->cpu_context.r4 = stack_start;
 		thread->cpu_context.r5 = stk_sz;
arch/x86/kernel/fpu/core.c +1 -1
index 12c70840980e..52a7ac55f89a 100644
@@ -91,7 +91,7 @@ void kernel_fpu_begin(void)
 
 	this_cpu_write(in_kernel_fpu, true);
 
-	if (!(current->flags & PF_KTHREAD) &&
+	if (!is_kthread(current) &&
 	    !test_thread_flag(TIF_NEED_FPU_LOAD)) {
 		set_thread_flag(TIF_NEED_FPU_LOAD);
 		/*
arch/x86/kernel/process_32.c +1 -1
index b8ceec4974fe..8263c37fac2b 100644
@@ -134,7 +134,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
 	p->thread.sp0 = (unsigned long) (childregs+1);
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(childregs, 0, sizeof(struct pt_regs));
 		frame->bx = sp;		/* function */
arch/x86/kernel/process_64.c +1 -1
index af64519b2695..85375eb3cff1 100644
@@ -397,7 +397,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
 	savesegment(ds, p->thread.ds);
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		/* kernel thread */
 		memset(childregs, 0, sizeof(struct pt_regs));
 		frame->bx = sp;		/* function */
arch/xtensa/kernel/process.c +1 -1
index db278a9e80c7..1dc0c67a55b3 100644
@@ -217,7 +217,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp_thread_fn,
 
 	p->thread.sp = (unsigned long)childregs;
 
-	if (!(p->flags & PF_KTHREAD)) {
+	if (!is_kthread(p)) {
 		struct pt_regs *regs = current_pt_regs();
 		unsigned long usp = usp_thread_fn ?
 			usp_thread_fn : regs->areg[1];
block/blk-cgroup.c +1 -1
index 55a7dc227dfb..44ea9f371e60 100644
@@ -1739,7 +1739,7 @@ void blkcg_maybe_throttle_current(void)
  */
 void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay)
 {
-	if (unlikely(current->flags & PF_KTHREAD))
+	if (unlikely(is_kthread(current)))
 		return;
 
 	if (!blk_get_queue(q))
drivers/tty/sysrq.c +1 -1
index 573b2055173c..3101dba0281d 100644
@@ -336,7 +336,7 @@ static void send_sig_all(int sig)
 
 	read_lock(&tasklist_lock);
 	for_each_process(p) {
-		if (p->flags & PF_KTHREAD)
+		if (is_kthread(p))
 			continue;
 		if (is_global_init(p))
 			continue;
fs/coredump.c +1 -1
index b1ea7dfbd149..03ecf67d783e 100644
@@ -412,7 +412,7 @@ static int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
 	for_each_process(g) {
 		if (g == tsk->group_leader)
 			continue;
-		if (g->flags & PF_KTHREAD)
+		if (is_kthread(g))
 			continue;
 
 		for_each_thread(g, p) {
fs/file_table.c +2 -2
index b07b53f24ff5..ce793b771a18 100644
@@ -335,7 +335,7 @@ void fput_many(struct file *file, unsigned int refs)
 	if (atomic_long_sub_and_test(refs, &file->f_count)) {
 		struct task_struct *task = current;
 
-		if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
+		if (likely(!in_interrupt() && !is_kthread(task))) {
 			init_task_work(&file->f_u.fu_rcuhead, ____fput);
 			if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
 				return;
@@ -368,7 +368,7 @@ void __fput_sync(struct file *file)
 {
 	if (atomic_long_dec_and_test(&file->f_count)) {
 		struct task_struct *task = current;
-		BUG_ON(!(task->flags & PF_KTHREAD));
+		BUG_ON(!is_kthread(task));
 		__fput(file);
 	}
 }
fs/namespace.c +1 -1
index d28d30b13043..7af913a0aee2 100644
@@ -1173,7 +1173,7 @@ static void mntput_no_expire(struct mount *mnt)
 
 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
 		struct task_struct *task = current;
-		if (likely(!(task->flags & PF_KTHREAD))) {
+		if (likely(!is_kthread(task))) {
 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
 			if (!task_work_add(task, &mnt->mnt_rcu, true))
 				return;
fs/proc/base.c +2 -2
index ebea9501afb8..0036d241ba31 100644
@@ -1098,7 +1098,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 				continue;
 
 			/* do not touch kernel threads or the global init */
-			if (p->flags & PF_KTHREAD || is_global_init(p))
+			if (is_kthread(p) || is_global_init(p))
 				continue;
 
 			task_lock(p);
@@ -1695,7 +1695,7 @@ void task_dump_owner(struct task_struct *task, umode_t mode,
 	kuid_t uid;
 	kgid_t gid;
 
-	if (unlikely(task->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(task))) {
 		*ruid = GLOBAL_ROOT_UID;
 		*rgid = GLOBAL_ROOT_GID;
 		return;
include/linux/cgroup.h +1 -1
index f6b048902d6c..96f47db6281a 100644
@@ -907,7 +907,7 @@ static inline bool cgroup_task_freeze(struct task_struct *task)
 {
 	bool ret;
 
-	if (task->flags & PF_KTHREAD)
+	if (is_kthread(task))
 		return false;
 
 	rcu_read_lock();
kernel/cgroup/freezer.c +2 -2
index 8cf010680678..eb55085f7b97 100644
@@ -199,7 +199,7 @@ static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze)
 		 * Ignore kernel threads here. Freezing cgroups containing
 		 * kthreads isn't supported.
 		 */
-		if (task->flags & PF_KTHREAD)
+		if (is_kthread(task))
 			continue;
 		cgroup_freeze_task(task, freeze);
 	}
@@ -227,7 +227,7 @@ void cgroup_freezer_migrate_task(struct task_struct *task,
 	/*
 	 * Kernel threads are not supposed to be frozen at all.
 	 */
-	if (task->flags & PF_KTHREAD)
+	if (is_kthread(task))
 		return;
 
 	/*
kernel/events/core.c +1 -1
index 0463c1151bae..7cb753cf32cd 100644
@@ -5951,7 +5951,7 @@ static void perf_sample_regs_user(struct perf_regs *regs_user,
 	if (user_mode(regs)) {
 		regs_user->abi = perf_reg_abi(current);
 		regs_user->regs = regs;
-	} else if (!(current->flags & PF_KTHREAD)) {
+	} else if (!is_kthread(current)) {
 		perf_get_regs_user(regs_user, regs, regs_user_copy);
 	} else {
 		regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
kernel/exit.c +1 -1
index 5b4a5dcce8f8..e53b291735d4 100644
@@ -448,7 +448,7 @@ void mm_update_next_owner(struct mm_struct *mm)
 	 * Search through everything else, we should not get here often.
 	 */
 	for_each_process(g) {
-		if (g->flags & PF_KTHREAD)
+		if (is_kthread(g))
 			continue;
 		for_each_thread(g, c) {
 			if (c->mm == mm)
kernel/fork.c +3 -3
index 2852d0e76ea3..d2e401d04971 100644
@@ -459,7 +459,7 @@ void free_task(struct task_struct *tsk)
 	ftrace_graph_exit_task(tsk);
 	put_seccomp_filter(tsk);
 	arch_release_task_struct(tsk);
-	if (tsk->flags & PF_KTHREAD)
+	if (is_kthread(tsk))
 		free_kthread_struct(tsk);
 	free_task_struct(tsk);
 }
@@ -1167,7 +1167,7 @@ struct file *get_task_exe_file(struct task_struct *task)
 	task_lock(task);
 	mm = task->mm;
 	if (mm) {
-		if (!(task->flags & PF_KTHREAD))
+		if (!is_kthread(task))
 			exe_file = get_mm_exe_file(mm);
 	}
 	task_unlock(task);
@@ -1191,7 +1191,7 @@ struct mm_struct *get_task_mm(struct task_struct *task)
 	task_lock(task);
 	mm = task->mm;
 	if (mm) {
-		if (task->flags & PF_KTHREAD)
+		if (is_kthread(task))
 			mm = NULL;
 		else
 			mmget(mm);
kernel/freezer.c +2 -2
index c0738424bb43..d1c2855cfde4 100644
@@ -51,7 +51,7 @@ bool freezing_slow_path(struct task_struct *p)
 	if (pm_nosig_freezing || cgroup_freezing(p))
 		return true;
 
-	if (pm_freezing && !(p->flags & PF_KTHREAD))
+	if (pm_freezing && !is_kthread(p))
 		return true;
 
 	return false;
@@ -140,7 +140,7 @@ bool freeze_task(struct task_struct *p)
 		return false;
 	}
 
-	if (!(p->flags & PF_KTHREAD))
+	if (!is_kthread(p))
 		fake_signal_wake_up(p);
 	else
 		wake_up_state(p, TASK_INTERRUPTIBLE);
kernel/futex.c +1 -1
index 6d50728ef2e7..3b100d17aa9d 100644
@@ -1249,7 +1249,7 @@ static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key,
 	if (!p)
 		return handle_exit_race(uaddr, uval, NULL);
 
-	if (unlikely(p->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(p))) {
 		put_task_struct(p);
 		return -EPERM;
 	}
kernel/kthread.c +3 -3
index 621467c33fef..2c505d2e228c 100644
@@ -72,7 +72,7 @@ static inline void set_kthread_struct(void *kthread)
 
 static inline struct kthread *to_kthread(struct task_struct *k)
 {
-	WARN_ON(!(k->flags & PF_KTHREAD));
+	WARN_ON(!is_kthread(k));
 	return (__force void *)k->set_child_tid;
 }
 
@@ -1205,7 +1205,7 @@ void kthread_associate_blkcg(struct cgroup_subsys_state *css)
 {
 	struct kthread *kthread;
 
-	if (!(current->flags & PF_KTHREAD))
+	if (!is_kthread(current))
 		return;
 	kthread = to_kthread(current);
 	if (!kthread)
@@ -1231,7 +1231,7 @@ struct cgroup_subsys_state *kthread_blkcg(void)
 {
 	struct kthread *kthread;
 
-	if (current->flags & PF_KTHREAD) {
+	if (is_kthread(current)) {
 		kthread = to_kthread(current);
 		if (kthread)
 			return kthread->blkcg_css;
kernel/livepatch/transition.c +1 -1
index cdf318d86dd6..3091d4eae48f 100644
@@ -358,7 +358,7 @@ static void klp_send_signals(void)
 		 * Meanwhile the task could migrate itself and the action
 		 * would be meaningless. It is not serious though.
 		 */
-		if (task->flags & PF_KTHREAD) {
+		if (is_kthread(task)) {
 			/*
 			 * Wake up a kthread which sleeps interruptedly and
 			 * still has not been migrated.
kernel/ptrace.c +1 -1
index cb9ddcc08119..025ed158a1d4 100644
@@ -376,7 +376,7 @@ static int ptrace_attach(struct task_struct *task, long request,
 	audit_ptrace(task);
 
 	retval = -EPERM;
-	if (unlikely(task->flags & PF_KTHREAD))
+	if (unlikely(is_kthread(task)))
 		goto out;
 	if (same_thread_group(task, current))
 		goto out;
kernel/sched/core.c +4 -4
index 2b037f195473..4c4a0494c39b 100644
@@ -1323,7 +1323,7 @@ void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
 
 static inline bool is_per_cpu_kthread(struct task_struct *p)
 {
-	if (!(p->flags & PF_KTHREAD))
+	if (!is_kthread(p))
 		return false;
 
 	if (p->nr_cpus_allowed != 1)
@@ -1518,7 +1518,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
 	rq = task_rq_lock(p, &rf);
 	update_rq_clock(rq);
 
-	if (p->flags & PF_KTHREAD) {
+	if (is_kthread(p)) {
 		/*
 		 * Kernel threads are allowed on online && !active CPUs
 		 */
@@ -1544,7 +1544,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
 
 	do_set_cpus_allowed(p, new_mask);
 
-	if (p->flags & PF_KTHREAD) {
+	if (is_kthread(p)) {
 		/*
 		 * For kernel threads that do indeed end up on online &&
 		 * !active we want to ensure they are strict per-CPU threads.
@@ -6649,7 +6649,7 @@ void normalize_rt_tasks(void)
 		/*
 		 * Only normalize user tasks:
 		 */
-		if (p->flags & PF_KTHREAD)
+		if (is_kthread(p))
 			continue;
 
 		p->se.exec_start = 0;
kernel/sched/idle.c +1 -1
index 80940939b733..dbf38ba93991 100644
@@ -321,7 +321,7 @@ void play_idle(unsigned long duration_ms)
 	 */
 	WARN_ON_ONCE(current->policy != SCHED_FIFO);
 	WARN_ON_ONCE(current->nr_cpus_allowed != 1);
-	WARN_ON_ONCE(!(current->flags & PF_KTHREAD));
+	WARN_ON_ONCE(!is_kthread(current));
 	WARN_ON_ONCE(!(current->flags & PF_NO_SETAFFINITY));
 	WARN_ON_ONCE(!duration_ms);
 
kernel/sched/wait.c +1 -1
index c1e566a114ca..a68429685d2b 100644
@@ -380,7 +380,7 @@ EXPORT_SYMBOL(autoremove_wake_function);
 
 static inline bool is_kthread_should_stop(void)
 {
-	return (current->flags & PF_KTHREAD) && kthread_should_stop();
+	return is_kthread(current) && kthread_should_stop();
 }
 
 /*
kernel/signal.c +1 -1
index e667be6907d7..a6e7ea47ae47 100644
@@ -1085,7 +1085,7 @@ static int __send_signal(int sig, struct kernel_siginfo *info, struct task_struc
 	/*
 	 * Skip useless siginfo allocation for SIGKILL and kernel threads.
 	 */
-	if ((sig == SIGKILL) || (t->flags & PF_KTHREAD))
+	if ((sig == SIGKILL) || is_kthread(t))
 		goto out_set;
 
 	/*
kernel/stacktrace.c +1 -1
index f5440abb7532..bb05fac6e786 100644
@@ -229,7 +229,7 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
 	mm_segment_t fs;
 
 	/* Trace user stack if not a kernel thread */
-	if (current->flags & PF_KTHREAD)
+	if (is_kthread(current))
 		return 0;
 
 	fs = get_fs();
lib/is_single_threaded.c +1 -1
index 8c98b20bfc41..bbb3fa4c01a4 100644
@@ -28,7 +28,7 @@ bool current_is_single_threaded(void)
 	ret = false;
 	rcu_read_lock();
 	for_each_process(p) {
-		if (unlikely(p->flags & PF_KTHREAD))
+		if (unlikely(is_kthread(p)))
 			continue;
 		if (unlikely(p == task->group_leader))
 			continue;
mm/memcontrol.c +1 -1
index cdbb7a84cb6e..a3965ca6037d 100644
@@ -2668,7 +2668,7 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (in_interrupt() || !current->mm || is_kthread(current))
 		return true;
 	return false;
 }
mm/oom_kill.c +2 -2
index eda2e2a0bdc6..06dde52d7abb 100644
@@ -162,7 +162,7 @@ static bool oom_unkillable_task(struct task_struct *p)
 {
 	if (is_global_init(p))
 		return true;
-	if (p->flags & PF_KTHREAD)
+	if (is_kthread(p))
 		return true;
 	return false;
 }
@@ -919,7 +919,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
 		 * No use_mm() user needs to read from the userspace so we are
 		 * ok to reap it.
 		 */
-		if (unlikely(p->flags & PF_KTHREAD))
+		if (unlikely(is_kthread(p)))
 			continue;
 		do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
 	}
mm/page_alloc.c +1 -1
index 272c6de1bf4e..4863f9812b90 100644
@@ -831,7 +831,7 @@ static inline struct capture_control *task_capc(struct zone *zone)
 	struct capture_control *capc = current->capture_control;
 
 	return capc &&
-		!(current->flags & PF_KTHREAD) &&
+		!is_kthread(current) &&
 		!capc->page &&
 		capc->cc->zone == zone &&
 		capc->cc->direct_compaction ? capc : NULL;
mm/vmacache.c +1 -1
index cdc32a3b02fa..153ae62b2276 100644
@@ -30,7 +30,7 @@
  */
 static inline bool vmacache_valid_mm(struct mm_struct *mm)
 {
-	return current->mm == mm && !(current->flags & PF_KTHREAD);
+	return current->mm == mm && !is_kthread(current);
 }
 
 void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
mm/vmscan.c +1 -1
index dbdc46a84f63..16defb76cb2b 100644
@@ -3110,7 +3110,7 @@ static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
 	 * committing a transaction where throttling it could forcing other
 	 * processes to block on log_wait_commit().
 	 */
-	if (current->flags & PF_KTHREAD)
+	if (is_kthread(current))
 		goto out;
 
 	/*
security/smack/smack_access.c +1 -1
index f1c93a7be9ec..1ec30f956852 100644
@@ -671,7 +671,7 @@ bool smack_privileged(int cap)
 	/*
 	 * All kernel tasks are privileged
 	 */
-	if (unlikely(current->flags & PF_KTHREAD))
+	if (unlikely(is_kthread(current)))
 		return true;
 
 	return smack_privileged_cred(cap, current_cred());
security/smack/smack_lsm.c +2 -2
index 4c5e5a438f8b..182debb9f114 100644
@@ -2257,7 +2257,7 @@ static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
 	/*
 	 * Sockets created by kernel threads receive web label.
 	 */
-	if (unlikely(current->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(current))) {
 		ssp->smk_in = &smack_known_web;
 		ssp->smk_out = &smack_known_web;
 	} else {
@@ -2761,7 +2761,7 @@ static int smack_socket_post_create(struct socket *sock, int family,
 	/*
 	 * Sockets created by kernel threads receive web label.
 	 */
-	if (unlikely(current->flags & PF_KTHREAD)) {
+	if (unlikely(is_kthread(current))) {
 		ssp = sock->sk->sk_security;
 		ssp->smk_in = &smack_known_web;
 		ssp->smk_out = &smack_known_web;
security/yama/yama_lsm.c +1 -1
index 94dc346370b1..99a8bd60beb0 100644
@@ -79,7 +79,7 @@ static void report_access(const char *access, struct task_struct *target,
 
 	assert_spin_locked(&target->alloc_lock); /* for target->comm */
 
-	if (current->flags & PF_KTHREAD) {
+	if (is_kthread(current)) {
 		/* I don't think kthreads call task_work_run() before exiting.
 		 * Imagine angry ranting about procfs here.
 		 */
Signature · 1 line
2.11.0
maintainer Acked-by on v1 2/9
#7 lore raw

Re: [PATCH 2/9] sched: treewide: use is_kthread()

Hi Mark,

Quoted 24 lines · Mark Rutland

Now that we have is_kthread(), let's convert existing open-coded checks of the form:

task->flags & PF_KTHREAD

... over to the new helper, which makes things a little easier to read, and sets a consistent example for new code to follow.

Generated with coccinelle:

----
virtual patch

@ depends on patch @
expression E;
@@

- (E->flags & PF_KTHREAD)
+ is_kthread(E)
----

... though this didn't pick up the instance in <linux/cgroup.h>, which I fixed up manually.

Quoted 7 lines

Instances checking multiple PF_* flags at ocne are left as-is for now.

There should be no functional change as a result of this patch.

 arch/m68k/kernel/process.c       | 2 +-

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#9 lore raw

Re: [PATCH 2/9] sched: treewide: use is_kthread()

Quoted 7 lines · Sebastian Andrzej Siewior

On 2019-08-14 11:41:24 [+0100], Mark Rutland wrote:
…

Quoted 1 line

Instances checking multiple PF_* flags at ocne are left as-is for now.

s@ocne@once@

Whoops; I'll fix that typo now...

Thanks,
Mark

Reviewed-by on v1 2/9
#10 lore raw

Re: [PATCH 2/9] sched: treewide: use is_kthread()

In reply to Mark Rutland
Quoted 8 lines · Mark Rutland

Now that we have is_kthread(), let's convert existing open-coded checks of the form:

task->flags & PF_KTHREAD

... over to the new helper, which makes things a little easier to read, and sets a consistent example for new code to follow.

[...]

Quoted 3 lines
 kernel/sched/core.c              | 8 ++++----
 kernel/sched/idle.c              | 2 +-
 kernel/sched/wait.c              | 2 +-

Looks all sane there for me.

author maintainer
#11 lore raw

PATCH 3/9 kmemleak: correctly check for kthreads

In reply to Mark Rutland

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc Catalin Marinas <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 mm/kmemleak.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/kmemleak.c +3 -3
index 6e9e8cca663e..42ea3c14b577 100644
@@ -1290,10 +1290,10 @@ static int scan_should_stop(void)
 	 * This function may be called from either process or kthread context,
 	 * hence the need to check for both stop conditions.
 	 */
-	if (current->mm)
-		return signal_pending(current);
-	else
+	if (is_kthread(current))
 		return kthread_should_stop();
+	else
+		return signal_pending(current);
 
 	return 0;
 }
Signature · 1 line
2.11.0
maintainer Acked-by on v1 3/9
#12 lore raw

Re: [PATCH 3/9] kmemleak: correctly check for kthreads

Quoted 7 lines · Mark Rutland

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Quoted 7 lines
Cc Catalin Marinas <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 mm/kmemleak.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 4 lines

index 6e9e8cca663e..42ea3c14b577 100644

mm/kmemleak.c static int scan_should_stop(void) · line 1296

Quoted 10 lines
mm/kmemleak.c +3 -3
@@ -1290,10 +1290,10 @@ static int scan_should_stop(void)
 	 * This function may be called from either process or kthread context,
 	 * hence the need to check for both stop conditions.
 	 */
-	if (current->mm)
-		return signal_pending(current);
-	else
+	if (is_kthread(current))
 		return kthread_should_stop();
+	else
+		return signal_pending(current);

While you are at it, you can drop the 'else' as well (occasionally checkpatch complains about 'else' after return).

Thanks.

Signature · 1 line
Catalin
author maintainer
#13 lore raw

PATCH 4/9 arm/perf: correctly check for kthreads

In reply to Mark Rutland

The arm perf_callchain_user() functions looks at current->mm, apparently to determine whether the thread is a kthread without any valid user context.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc Will Deacon <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/arm/kernel/perf_callchain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/perf_callchain.c +1 -1
index 3b69a76d341e..5c920f2c46a8 100644
@@ -71,7 +71,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs
 
 	perf_callchain_store(entry, regs->ARM_pc);
 
-	if (!current->mm)
+	if (is_kthread(current))
 		return;
 
 	tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Signature · 1 line
2.11.0
author maintainer
#14 lore raw

PATCH 5/9 arm64: correctly check for ktrheads

In reply to Mark Rutland

Since commit:

6eb6c80187c55b7f ("arm64: kernel thread don't need to save fpsimd context.")

... we skip saving the fpsimd state for kernel threads in arch_dup_task_struct(). We determine whether current is a kthread by looking at current->mm.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc Catalin Marinas <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/process.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/process.c +1 -1
index 288012687c29..82b0a7a257cd 100644
@@ -335,7 +335,7 @@ void arch_release_task_struct(struct task_struct *tsk)
  */
 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 {
-	if (current->mm)
+	if (!is_kthread(current))
 		fpsimd_preserve_current_state();
 	*dst = *src;
 
Signature · 1 line
2.11.0
maintainer Acked-by on v1 5/9
#15 lore raw

Re: [PATCH 5/9] arm64: correctly check for ktrheads

Quoted 13 lines · Mark Rutland

Since commit:

6eb6c80187c55b7f ("arm64: kernel thread don't need to save fpsimd context.")

... we skip saving the fpsimd state for kernel threads in arch_dup_task_struct(). We determine whether current is a kthread by looking at current->mm.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

Quoted 7 lines

For consistency, let's use is_kthread() here.

Cc Catalin Marinas <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Will Deacon <[email protected]>
author maintainer
#16 lore raw

PATCH 6/9 sparc/perf: correctly check for kthreads

In reply to Mark Rutland

The sparc perf_callchain_user() functions looks at current->mm, apparently to determine whether the thread is a kthread without any valid user context.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc David S. Miller <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/sparc/kernel/perf_event.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sparc/kernel/perf_event.c +1 -1
index a58ae9c42803..ef7b1a03bea9 100644
@@ -1858,7 +1858,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs
 
 	perf_callchain_store(entry, regs->tpc);
 
-	if (!current->mm)
+	if (is_kthread(current))
 		return;
 
 	flushw_user();
Signature · 1 line
2.11.0
maintainer Acked-by on v1 6/9
#17 lore raw

Re: [PATCH 6/9] sparc/perf: correctly check for kthreads

From: Mark Rutland <[email protected]>
Date: Wed, 14 Aug 2019 11:41:28 +0100

Quoted 4 lines

The sparc perf_callchain_user() functions looks at current->mm, apparently to determine whether the thread is a kthread without any valid user context.

Quoted 8 lines

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

author maintainer
#18 lore raw

PATCH 7/9 x86/lbr: correctly check for kthreads

In reply to Mark Rutland

The x86 lbr branch_type() functions looks at current->mm, apparently to determine whether the thread is a kthread without any valid user context.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc Andi Kleen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Kan Liang <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/events/intel/lbr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +1 -1
index 6f814a27416b..ed2b7e57a249 100644
@@ -857,7 +857,7 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
 		 * can happen if measuring at the user level only
 		 * and we interrupt in a kernel thread, e.g., idle.
 		 */
-		if (!current->mm)
+		if (is_kthread(current))
 			return X86_BR_NONE;
 
 		/* may fail if text not present */
Signature · 1 line
2.11.0
author maintainer
#19 lore raw

PATCH 8/9 x86/fpu: correctly check for kthreads

In reply to Mark Rutland

Per commit:

0cecca9d03c964ab ("x86/fpu: Eager switch PKRU state")

... switch_fpu_state() is trying to distinguish user threads from kthreads, such that kthreads consistently use init_pkru_value. It does do by looking at current->mm.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc Borislav Petkov <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Sebastian Andrzej Siewior <[email protected]>
---
 arch/x86/include/asm/fpu/internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/fpu/internal.h +1 -1
index 4c95c365058a..ed3d85fa7c67 100644
@@ -607,7 +607,7 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
 	 * PKRU state is switched eagerly because it needs to be valid before we
 	 * return to userland e.g. for a copy_to_user() operation.
 	 */
-	if (current->mm) {
+	if (!is_kthread(current)) {
 		pk = get_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
 		if (pk)
 			pkru_val = pk->pkru;
Signature · 1 line
2.11.0
Acked-by on v1 8/9
#20 lore raw

Re: [PATCH 8/9] x86/fpu: correctly check for kthreads

Quoted 4 lines · Mark Rutland

Per commit:

0cecca9d03c964ab ("x86/fpu: Eager switch PKRU state")

Quoted 8 lines

... switch_fpu_state() is trying to distinguish user threads from kthreads, such that kthreads consistently use init_pkru_value. It does do by looking at current->mm.

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

I think this was missed in commit.

	8d3289f2fa1e0 ("x86/fpu: Don't use current->mm to check for a kthread")

A kthread with use_mm() would load here non-existing FPU state.

Sebastian

author maintainer
#21 lore raw

Re: [PATCH 8/9] x86/fpu: correctly check for kthreads

Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Mark Rutland

Per commit:

0cecca9d03c964ab ("x86/fpu: Eager switch PKRU state")

... switch_fpu_state() is trying to distinguish user threads from kthreads, such that kthreads consistently use init_pkru_value. It does do by looking at current->mm.

Quoted 7 lines
Quoted 4 lines

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

I think this was missed in commit.

	8d3289f2fa1e0 ("x86/fpu: Don't use current->mm to check for a kthread")

Yup, though if it's a bug it's been a bug since commit:

0cecca9d03c964ab ("x86/fpu: Eager switch PKRU state")

... which I guess the fixes tag would have to mention.

Quoted 4 lines

A kthread with use_mm() would load here non-existing FPU state.

Given the above, should I add the fixes tag (for 0cecca9d03c964ab)?

Thanks,
Mark.

#22 lore raw

Re: [PATCH 8/9] x86/fpu: correctly check for kthreads

Quoted 7 lines · Mark Rutland

I think this was missed in commit.

Quoted 1 line
	8d3289f2fa1e0 ("x86/fpu: Don't use current->mm to check for a kthread")

Yup, though if it's a bug it's been a bug since commit:

0cecca9d03c964ab ("x86/fpu: Eager switch PKRU state")

Quoted 8 lines

... which I guess the fixes tag would have to mention.

Quoted 4 lines

A kthread with use_mm() would load here non-existing FPU state.

Given the above, should I add the fixes tag (for 0cecca9d03c964ab)?

Buh. Either that or hch's commit (8d3289f2fa1e0 ("x86/fpu: Don't use current->mm to check for a kthread").
That will trigger a stable backport (for 5.2) asking what to do about is_kthread() so please leave a hint to use the PF_ flag like hch did.

Now that I think about it, even if we end up in kthread with use_mm() then its FPU state is non-existent which means get_xsave_addr() returns NULL. This is okay / expected but it triggers the WARN_ONCE().

Quoted 2 lines

Thanks,
Mark.

Sebastian

author maintainer
#23 lore raw

PATCH 9/9 samples/kretprobe: correctly check for kthreads

In reply to Mark Rutland

In general, a non-NULL current->mm doesn't imply that current is a kthread, as kthreads can install an mm via use_mm(), and so it's preferable to use is_kthread() to determine whether a thread is a kthread.

For consistency, let's use is_kthread() here.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
---
 samples/kprobes/kretprobe_example.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/kprobes/kretprobe_example.c +1 -1
index 186315ca88b3..d6e0bf196f0e 100644
@@ -41,7 +41,7 @@ static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
 {
 	struct my_data *data;
 
-	if (!current->mm)
+	if (is_kthread(current))
 		return 1;	/* Skip kernel threads */
 
 	data = (struct my_data *)ri->data;
Signature · 1 line
2.11.0
#24 lore raw
In reply to Mark Rutland
Quoted 7 lines · Mark Rutland

A reasonable amount of kernel code looks at task_struct::mm to determine whether a thread is a kthread or a real user task. This isn't quite right, since kthreads can have a non-NULL mm when calling use_mm().

The correct way to check whether a task is a kthread is to check whether PF_KTHREAD is set in task_struct::flags, but doing so is a bit unwieldy.

Quoted 9 lines

To make this a bit nicer, this series adds a new is_kthread(tsk) helper, converts existing code to make use of it, and fixes up a number of erroneous checks of current->mm. Hopefully this will push people to use the right approach in future.

I'm sure there are other instances in the kernel tree where we don't check this correctly. In this series I'm just trying to fix the instances I'm reasonably confident are incorrect.

I've only found one extra location in kernel/sched/ that seems like it wants to use is_kthread(), included below. I can send it out separately after this gets merged.

-----8<-----

kernel/sched/core.c +3 -4
index 4c4a0494c39b..0956972f6ea7 100644
@@ -1964,11 +1964,10 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
 out:
 	if (state != cpuset) {
 		/*
-		 * Don't tell them about moving exiting tasks or
-		 * kernel threads (both mm NULL), since they never
-		 * leave kernel.
+		 * Don't tell them about moving exiting tasks (NULL mm) or
+		 * kernel threads since they never leave kernel.
 		 */
-		if (p->mm && printk_ratelimit()) {
+		if ((!is_kthread(p) && p->mm) && printk_ratelimit()) {
 			printk_deferred("process %d (%s) no longer affine to cpu%d\n",
 					task_pid_nr(p), p->comm, cpu);
 		}

----->8-----

Regarding sched/fair.c, I am quite convinced all of the p->mm checks are genuine (as in they don't care if it's a kthread or not) - those are all about NUMA balancing. Maybe Rik/Mel can correct me if I'm wrong.

Cheers,
Valentin