Re: [PATCH v6 linux 1/2] sched/cache: Reduce the overhead of task_cache_work by only scan the visisted cpus
From: Chen, Yu C
Date: Fri Jul 17 2026 - 04:27:59 EST
On 7/17/2026 12:12 PM, Luo Gengkun wrote:
struct sched_cache_stat {
@@ -2398,6 +2399,7 @@ struct sched_cache_stat {
unsigned long next_scan;
unsigned long footprint;
int cpu;
+ cpumask_var_t visited_cpus;
need zalloc_cpumask_var()/free_cpumask_var() in this patch.
} ____cacheline_aligned_in_smp;
#else
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index d78467ec6ee1..ab9010b2ec49 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1585,6 +1585,7 @@ void mm_init_sched(struct mm_struct *mm,
pcpu_sched->runtime = 0;
/* a slightly stale cpu epoch is acceptible */
pcpu_sched->epoch = rq->cpu_epoch;
+ pcpu_sched->epoch_last_visit = rq->cpu_epoch;
epoch = rq->cpu_epoch;
}
@@ -1635,13 +1636,23 @@ static inline void __update_mm_sched(struct rq *rq,
}
}
-static unsigned long fraction_mm_sched(struct rq *rq,
- struct sched_cache_time *pcpu_sched)
+static unsigned long fraction_mm_sched(int cpu,
+ struct mm_struct *mm)
{
+ struct sched_cache_time *pcpu_sched =
+ per_cpu_ptr(mm->sc_stat.pcpu_sched, cpu);
+ struct rq *rq = cpu_rq(cpu);
+
guard(raw_spinlock_irqsave)(&rq->cpu_epoch_lock);
__update_mm_sched(rq, pcpu_sched);
+ /* Skip the rq that has not been hit for a long time */
+ if ((rq->cpu_epoch - pcpu_sched->epoch_last_visit) > llc_epoch_affinity_timeout) {
+ cpumask_clear_cpu(cpu, mm->sc_stat.visited_cpus);
It may be worth adding a comment in account_mm_sched() or task_cache_work()
to clarify the race condition. As Tim noted, it happens between setting and reading
visited_cpus. That said, it's a trade-off between accuracy and efficiency
- locking would fix it but at the cost of extra overhead IMO - and the update-to-date
visited_cpus could be read properly in the next invoke of task_cache_work().
thanks,
Chenyu