Re: [syzbot] WARNING in kthread_is_per_cpu

From: Peter Zijlstra
Date: Mon Apr 19 2021 - 14:47:10 EST


On Mon, Apr 19, 2021 at 12:31:22PM +0100, Valentin Schneider wrote:

> if ((p->flags & PF_KTHREAD) && kthread_is_per_cpu(p))
> `\
> to_kthread(p);
> `\
> WARN_ON(!(p->flags & PF_KTHREAD));
>
> ... Huh?

Something like so perhaps?

diff --git a/kernel/kthread.c b/kernel/kthread.c
index 1578973c5740..eeba40df61ac 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -78,6 +78,14 @@ static inline void set_kthread_struct(void *kthread)
current->set_child_tid = (__force void __user *)kthread;
}

+static inline struct kthread *__to_kthread(struct task_struct *k)
+{
+ void *kthread = (__force void *)k->set_child_tid;
+ if (kthread && !(k->flags & PF_KTHREAD))
+ kthread = NULL;
+ return kthread;
+}
+
static inline struct kthread *to_kthread(struct task_struct *k)
{
WARN_ON(!(k->flags & PF_KTHREAD));
@@ -516,7 +524,7 @@ void kthread_set_per_cpu(struct task_struct *k, int cpu)

bool kthread_is_per_cpu(struct task_struct *k)
{
- struct kthread *kthread = to_kthread(k);
+ struct kthread *kthread = __to_kthread(k);
if (!kthread)
return false;

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 3384ea74cad4..dc6311bd6986 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -7658,7 +7658,7 @@ static void balance_push(struct rq *rq)
* histerical raisins.
*/
if (rq->idle == push_task ||
- ((push_task->flags & PF_KTHREAD) && kthread_is_per_cpu(push_task)) ||
+ kthread_is_per_cpu(push_task) ||
is_migration_disabled(push_task)) {

/*