sched/cache: Skip kernel threads for cache aware scheduling to rubustify the code

Kernel thread should not be covered by cache aware scheduling as
it borrows the statistics from the user space thread. Filter the
kernel thread in account_mm_sched().

In theory a kernel thread does not have any valid
cache group, so !grp should gate the kernel thread.
Add the PF_KTHREAD check explicitly here for safety
reasons, to guard against future modifications and
to pair with task_tick_cache().

Fixes: df0d984759 ("sched/cache: Introduce infrastructure for cache-aware load balancing")
Signed-off-by: Chen Yu <yu.c.chen@intel.com>
Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: <stable@kernel.org> # 7.2.x
Link: https://patch.msgid.link/058f0c6ea7b991c177a17de347fa3157f25489a7.1790035273.git.tim.c.chen@linux.intel.com
This commit is contained in:
Chen Yu 2026-09-21 17:37:26 -07:00 committed by Ingo Molnar
parent b636fef85b
commit 65efcccddc

View File

@ -1907,8 +1907,14 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
/*
* init_task, kthreads and user thread created
* by user_mode_thread() don't have a cache group.
* In theory a kernel thread does not have any valid
* cache group, because sched_cache_fork() is not
* invoked for a kernel thread - !grp should gate the
* kernel thread. Use the PF_KTHREAD check explicitly
* here for safety reasons, to guard against future
* modifications and to pair with task_tick_cache().
*/
if (!grp || !grp->pcpu_sched)
if (p->flags & PF_KTHREAD || !grp || !grp->pcpu_sched)
return;
pcpu_sched = per_cpu_ptr(grp->pcpu_sched, cpu_of(rq));