Skip to content

Commit ac687e6

Browse files
author
Peter Zijlstra
committed
kthread: Extract KTHREAD_IS_PER_CPU
There is a need to distinguish geniune per-cpu kthreads from kthreads that happen to have a single CPU affinity. Geniune per-cpu kthreads are kthreads that are CPU affine for correctness, these will obviously have PF_KTHREAD set, but must also have PF_NO_SETAFFINITY set, lest userspace modify their affinity and ruins things. However, these two things are not sufficient, PF_NO_SETAFFINITY is also set on other tasks that have their affinities controlled through other means, like for instance workqueues. Therefore another bit is needed; it turns out kthread_create_per_cpu() already has such a bit: KTHREAD_IS_PER_CPU, which is used to make kthread_park()/kthread_unpark() work correctly. Expose this flag and remove the implicit setting of it from kthread_create_on_cpu(); the io_uring usage of it seems dubious at best. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Valentin Schneider <[email protected]> Tested-by: Valentin Schneider <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 22f667c commit ac687e6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

include/linux/kthread.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
3333
unsigned int cpu,
3434
const char *namefmt);
3535

36+
void kthread_set_per_cpu(struct task_struct *k, int cpu);
37+
bool kthread_is_per_cpu(struct task_struct *k);
38+
3639
/**
3740
* kthread_run - create and wake a thread.
3841
* @threadfn: the function to run until signal_pending(current).

kernel/kthread.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,36 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
493493
return p;
494494
kthread_bind(p, cpu);
495495
/* CPU hotplug need to bind once again when unparking the thread. */
496-
set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
497496
to_kthread(p)->cpu = cpu;
498497
return p;
499498
}
500499

500+
void kthread_set_per_cpu(struct task_struct *k, int cpu)
501+
{
502+
struct kthread *kthread = to_kthread(k);
503+
if (!kthread)
504+
return;
505+
506+
WARN_ON_ONCE(!(k->flags & PF_NO_SETAFFINITY));
507+
508+
if (cpu < 0) {
509+
clear_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
510+
return;
511+
}
512+
513+
kthread->cpu = cpu;
514+
set_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
515+
}
516+
517+
bool kthread_is_per_cpu(struct task_struct *k)
518+
{
519+
struct kthread *kthread = to_kthread(k);
520+
if (!kthread)
521+
return false;
522+
523+
return test_bit(KTHREAD_IS_PER_CPU, &kthread->flags);
524+
}
525+
501526
/**
502527
* kthread_unpark - unpark a thread created by kthread_create().
503528
* @k: thread created by kthread_create().

kernel/smpboot.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ __smpboot_create_thread(struct smp_hotplug_thread *ht, unsigned int cpu)
188188
kfree(td);
189189
return PTR_ERR(tsk);
190190
}
191+
kthread_set_per_cpu(tsk, cpu);
191192
/*
192193
* Park the thread so that it could start right on the CPU
193194
* when it is available.

0 commit comments

Comments
 (0)