Skip to content

Commit dc6a87f

Browse files
Valentin Schneidergregkh
authored andcommitted
sched: Make the idle task quack like a per-CPU kthread
[ Upstream commit 00b89fe ] For all intents and purposes, the idle task is a per-CPU kthread. It isn't created via the same route as other pcpu kthreads however, and as a result it is missing a few bells and whistles: it fails kthread_is_per_cpu() and it doesn't have PF_NO_SETAFFINITY set. Fix the former by giving the idle task a kthread struct along with the KTHREAD_IS_PER_CPU flag. This requires some extra iffery as init_idle() call be called more than once on the same idle task. Signed-off-by: Valentin Schneider <valentin.schneider@arm.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20210510151024.2448573-2-valentin.schneider@arm.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b03f44f commit dc6a87f

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

‎include/linux/kthread.h‎

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

36+
void set_kthread_struct(struct task_struct *p);
37+
3638
void kthread_set_per_cpu(struct task_struct *k, int cpu);
3739
bool kthread_is_per_cpu(struct task_struct *k);
3840

‎kernel/kthread.c‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ enum KTHREAD_BITS {
6868
KTHREAD_SHOULD_PARK,
6969
};
7070

71-
static inline void set_kthread_struct(void *kthread)
72-
{
73-
/*
74-
* We abuse ->set_child_tid to avoid the new member and because it
75-
* can't be wrongly copied by copy_process(). We also rely on fact
76-
* that the caller can't exec, so PF_KTHREAD can't be cleared.
77-
*/
78-
current->set_child_tid = (__force void __user *)kthread;
79-
}
80-
8171
static inline struct kthread *to_kthread(struct task_struct *k)
8272
{
8373
WARN_ON(!(k->flags & PF_KTHREAD));
@@ -103,6 +93,22 @@ static inline struct kthread *__to_kthread(struct task_struct *p)
10393
return kthread;
10494
}
10595

96+
void set_kthread_struct(struct task_struct *p)
97+
{
98+
struct kthread *kthread;
99+
100+
if (__to_kthread(p))
101+
return;
102+
103+
kthread = kzalloc(sizeof(*kthread), GFP_KERNEL);
104+
/*
105+
* We abuse ->set_child_tid to avoid the new member and because it
106+
* can't be wrongly copied by copy_process(). We also rely on fact
107+
* that the caller can't exec, so PF_KTHREAD can't be cleared.
108+
*/
109+
p->set_child_tid = (__force void __user *)kthread;
110+
}
111+
106112
void free_kthread_struct(struct task_struct *k)
107113
{
108114
struct kthread *kthread;
@@ -272,8 +278,8 @@ static int kthread(void *_create)
272278
struct kthread *self;
273279
int ret;
274280

275-
self = kzalloc(sizeof(*self), GFP_KERNEL);
276-
set_kthread_struct(self);
281+
set_kthread_struct(current);
282+
self = to_kthread(current);
277283

278284
/* If user was SIGKILLed, I release the structure. */
279285
done = xchg(&create->done, NULL);

‎kernel/sched/core.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7440,12 +7440,25 @@ void __init init_idle(struct task_struct *idle, int cpu)
74407440

74417441
__sched_fork(0, idle);
74427442

7443+
/*
7444+
* The idle task doesn't need the kthread struct to function, but it
7445+
* is dressed up as a per-CPU kthread and thus needs to play the part
7446+
* if we want to avoid special-casing it in code that deals with per-CPU
7447+
* kthreads.
7448+
*/
7449+
set_kthread_struct(idle);
7450+
74437451
raw_spin_lock_irqsave(&idle->pi_lock, flags);
74447452
raw_spin_lock(&rq->lock);
74457453

74467454
idle->state = TASK_RUNNING;
74477455
idle->se.exec_start = sched_clock();
7448-
idle->flags |= PF_IDLE;
7456+
/*
7457+
* PF_KTHREAD should already be set at this point; regardless, make it
7458+
* look like a proper per-CPU kthread.
7459+
*/
7460+
idle->flags |= PF_IDLE | PF_KTHREAD | PF_NO_SETAFFINITY;
7461+
kthread_set_per_cpu(idle, cpu);
74497462

74507463
scs_task_reset(idle);
74517464
kasan_unpoison_task_stack(idle);
@@ -7662,12 +7675,8 @@ static void balance_push(struct rq *rq)
76627675
/*
76637676
* Both the cpu-hotplug and stop task are in this case and are
76647677
* required to complete the hotplug process.
7665-
*
7666-
* XXX: the idle task does not match kthread_is_per_cpu() due to
7667-
* histerical raisins.
76687678
*/
7669-
if (rq->idle == push_task ||
7670-
kthread_is_per_cpu(push_task) ||
7679+
if (kthread_is_per_cpu(push_task) ||
76717680
is_migration_disabled(push_task)) {
76727681

76737682
/*

0 commit comments

Comments
 (0)