[PATCH v5 2/2] -- DO NOT APPLY!!! -- sched/cache/debug: Add trace event and sched feature to track scan cost
From: Luo Gengkun
Date: Thu Jul 09 2026 - 08:49:01 EST
To evaluate the effectiveness of the previous patch, this debug patch re-adds
get_scan_cpumasks() and introduces two sched features as well as trace
events to facilitate testing.
Signed-off-by: Luo Gengkun <luogengkun2@xxxxxxxxxx>
---
include/trace/events/sched.h | 21 +++++++++++++++++++++
kernel/sched/fair.c | 23 ++++++++++++++++++-----
kernel/sched/features.h | 1 +
3 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h
index 535860581f15..aced624f198d 100644
--- a/include/trace/events/sched.h
+++ b/include/trace/events/sched.h
@@ -10,6 +10,27 @@
#include <linux/tracepoint.h>
#include <linux/binfmts.h>
+TRACE_EVENT(sched_cache_scan,
+
+ TP_PROTO(struct task_struct *t, int scan),
+
+ TP_ARGS(t, scan),
+
+ TP_STRUCT__entry(
+ __string( comm, t->comm )
+ __field( pid_t, pid )
+ __field( int, scan )
+ ),
+
+ TP_fast_assign(
+ __assign_str(comm);
+ __entry->pid = t->pid;
+ __entry->scan = scan;
+ ),
+
+ TP_printk("comm=%s pid=%d scan=%d", __get_str(comm), __entry->pid,
+ __entry->scan)
+);
/*
* Tracepoint for calling kthread_stop, performed to end a kthread:
*/
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 44552168fcc3..f1d24a5c5165 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1647,7 +1647,8 @@ static unsigned long fraction_mm_sched(int cpu,
guard(raw_spinlock_irqsave)(&rq->cpu_epoch_lock);
/* Skip the rq that has not been hit for a long time */
- if ((rq->cpu_epoch - pcpu_sched->epoch_timeout) > llc_epoch_affinity_timeout) {
+ if (sched_feat(SC_VISIT) &&
+ (rq->cpu_epoch - pcpu_sched->epoch_timeout) > llc_epoch_affinity_timeout) {
cpumask_clear_cpu(cpu, &mm->sc_stat.visited_cpus);
return 0;
}
@@ -1724,7 +1725,8 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
rq->cpu_runtime += delta_exec;
epoch = rq->cpu_epoch;
pcpu_sched->epoch_timeout = epoch;
- if (!cpumask_test_cpu(cpu_of(rq), &mm->sc_stat.visited_cpus))
+ if (sched_feat(SC_VISIT) &&
+ !cpumask_test_cpu(cpu_of(rq), &mm->sc_stat.visited_cpus))
cpumask_set_cpu(cpu_of(rq), &mm->sc_stat.visited_cpus);
}
@@ -1847,7 +1849,8 @@ static void task_cache_work(struct callback_head *work)
unsigned long curr_m_a_occ = 0;
struct mm_struct *mm = p->mm;
unsigned long m_a_occ = 0;
- cpumask_var_t cpus;
+ cpumask_var_t cpus, scan_cpus;
+ int scanned = 0;
WARN_ON_ONCE(work != &p->cache_work);
@@ -1878,11 +1881,15 @@ static void task_cache_work(struct callback_head *work)
if (!zalloc_cpumask_var(&cpus, GFP_KERNEL))
return;
+ if (!zalloc_cpumask_var(&scan_cpus, GFP_KERNEL))
+ return;
+
scoped_guard (cpus_read_lock) {
guard(rcu)();
get_scan_cpumasks(cpus, p);
- cpumask_and(cpus, cpus, &mm->sc_stat.visited_cpus);
+ if (sched_feat(SC_VISIT))
+ cpumask_and(cpus, cpus, &mm->sc_stat.visited_cpus);
for_each_cpu(cpu, cpus) {
/* XXX sched_cluster_active */
@@ -1893,12 +1900,17 @@ static void task_cache_work(struct callback_head *work)
if (!sd)
continue;
- for_each_cpu_and(i, sched_domain_span(sd), cpus) {
+ cpumask_copy(scan_cpus, sched_domain_span(sd));
+ if (sched_feat(SC_VISIT))
+ cpumask_and(scan_cpus, scan_cpus, cpus);
+
+ for_each_cpu(i, scan_cpus) {
cur = rcu_dereference_all(cpu_rq(i)->curr);
if (cur && !(cur->flags & (PF_EXITING | PF_KTHREAD)) &&
cur->mm == mm)
nr_running++;
+ scanned++;
occ = fraction_mm_sched(i, mm);
if (occ == 0)
continue;
@@ -1954,6 +1966,7 @@ static void task_cache_work(struct callback_head *work)
update_avg_scale(&mm->sc_stat.nr_running_avg, nr_running);
free_cpumask_var(cpus);
+ trace_sched_cache_scan(p, scanned);
}
void init_sched_mm(struct task_struct *p)
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index 8f0dee8fc475..990714ebabfd 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -142,3 +142,4 @@ SCHED_FEAT(LATENCY_WARN, false)
*/
SCHED_FEAT(NI_RANDOM, true)
SCHED_FEAT(NI_RATE, true)
+SCHED_FEAT(SC_VISIT, false)
--
2.34.1