[tip: sched/core] sched/cache: Allow only 1 thread of the process to calculate the LLC occupancy

tip-bot2 for Jianyong Wu posted 1 patch 4 days, 14 hours ago
include/linux/sched.h |  1 +
kernel/sched/fair.c   | 11 +++++++++++
2 files changed, 12 insertions(+)
[tip: sched/core] sched/cache: Allow only 1 thread of the process to calculate the LLC occupancy
Posted by tip-bot2 for Jianyong Wu 4 days, 14 hours ago
The following commit has been merged into the sched/core branch of tip:

Commit-ID:     a2b4cf39d9d333bfeb9262dbaafe3d24d405a5c0
Gitweb:        https://git.kernel.org/tip/a2b4cf39d9d333bfeb9262dbaafe3d24d405a5c0
Author:        Jianyong Wu <wujianyong@hygon.cn>
AuthorDate:    Wed, 13 May 2026 13:39:12 -07:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Mon, 18 May 2026 21:33:14 +02:00

sched/cache: Allow only 1 thread of the process to calculate the LLC occupancy

Scanning online CPUs to calculate the occupancy might be
time-consuming. Only allow 1 thread of the process to scan
the CPUs at the same time, which is similar to what
NUMA balance does in task_numa_work().

Signed-off-by: Jianyong Wu <wujianyong@hygon.cn>
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>
Link: https://patch.msgid.link/5672b52e588b855b01e5a1a17822f7c6c7237a3d.1778703694.git.tim.c.chen@linux.intel.com
---
 include/linux/sched.h |  1 +
 kernel/sched/fair.c   | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index d201048..6d883f1 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2423,6 +2423,7 @@ struct sched_cache_stat {
 	struct sched_cache_time __percpu *pcpu_sched;
 	raw_spinlock_t lock;
 	unsigned long epoch;
+	unsigned long next_scan;
 	int cpu;
 } ____cacheline_aligned_in_smp;
 
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 5f22e5a..a759ea6 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1451,6 +1451,7 @@ void mm_init_sched(struct mm_struct *mm,
 	raw_spin_lock_init(&mm->sc_stat.lock);
 	mm->sc_stat.epoch = epoch;
 	mm->sc_stat.cpu = -1;
+	mm->sc_stat.next_scan = jiffies;
 
 	/*
 	 * The update to mm->sc_stat should not be reordered
@@ -1661,6 +1662,7 @@ out:
 
 static void task_cache_work(struct callback_head *work)
 {
+	unsigned long next_scan, now = jiffies;
 	struct task_struct *p = current;
 	struct mm_struct *mm = p->mm;
 	unsigned long m_a_occ = 0;
@@ -1675,6 +1677,15 @@ static void task_cache_work(struct callback_head *work)
 	if (p->flags & PF_EXITING)
 		return;
 
+	next_scan = READ_ONCE(mm->sc_stat.next_scan);
+	if (time_before(now, next_scan))
+		return;
+
+	/* only 1 thread is allowed to scan */
+	if (!try_cmpxchg(&mm->sc_stat.next_scan, &next_scan,
+			 now + EPOCH_PERIOD))
+		return;
+
 	if (!zalloc_cpumask_var(&cpus, GFP_KERNEL))
 		return;