mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] sched/cache: Update nr_running_avg before testing it in task_cache_work()
@ 2026-12-03  7:37 Hang Zhengyang
  0 siblings, 0 replies; only message in thread
From: Hang Zhengyang @ 2026-12-03  7:37 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot
  Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, Tim Chen, Chen Yu, Chen Yu,
	Luo Gengkun, linux-kernel

task_cache_work() returns early when invalid_llc_nr() is true, and it
updates grp->nr_running_avg only at its end. invalid_llc_nr() also tests
that average, so once a burst of threads pushes it over the LLC limit,
it is never updated again. The process then loses cache-aware
scheduling until it execs or llc_aggr_tolerance is raised, even after
it goes back to a few threads.

The early return is meant for single-threaded processes. Split
invalid_llc_nr() into llc_single_threaded() and exceed_llc_nr(). Return
early only for a single thread, a zero llc_aggr_tolerance or
exceed_llc_capacity(), and test exceed_llc_nr() after the scan has
updated the average.

Processes over the limit now scan every epoch, and the limit is tested
with the current epoch included.

Fixes: 7b34bb1ca324 ("sched/cache: Skip cache-aware scheduling for single-threaded processes")
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---

Notes:
    Tested on tip/sched/core 74ce3843347b in KVM guests with debug kernels.
    A process runs 16 busy threads for 1.5 s, then keeps 2 busy. I read
    nr_running_avg and grp->cpu with gdb, two runs each:
    
      guest                   before                after
      2 x 8 cores, no SMT     avg 8, cpu -1         avg 2-3, LLC set
      2 x 8 cores, SMT2       avg 7, cpu -1         avg 4-5, LLC set
    
    Cost: on a 32-CPU guest, four hackbench -T processes over the limit
    take 0.665 s before and 0.663 s after (15 runs each), and
    task_cache_work() is below 0.01% in perf. I could not test a large
    machine.
    
    Not changed here: update_avg_scale() rounds each step toward zero, so
    near the limit the average stays on the side it came from (with SMT2,
    5 threads after a burst stay excluded). Rounding to the nearest value
    makes the decision flip often around the limit instead, so that needs
    hysteresis and a separate patch.
    
    I used Claude Code to find the issue, write the patch and run the
    tests. I reviewed the patch.

 kernel/sched/fair.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 2d9d3d686261..24cd99ed54aa 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1492,13 +1492,14 @@ static inline bool fits_llc_nr(u64 nr_threads, unsigned int nr_cpus)
 	return nr_threads * 5 < (u64)nr_cpus * 4;
 }
 
-static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p,
-			   int cpu)
+static inline bool llc_single_threaded(struct task_struct *p)
 {
-	int scale;
+	return get_nr_threads(p) <= 1;
+}
 
-	if (get_nr_threads(p) <= 1)
-		return true;
+static bool exceed_llc_nr(struct sched_cache_group *grp, int cpu)
+{
+	int scale;
 
 	/*
 	 * Scale the number of 'cores' in a LLC by llc_aggr_tolerance
@@ -1512,6 +1513,15 @@ static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p,
 			    scale * per_cpu(sd_llc_size, cpu));
 }
 
+static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p,
+			   int cpu)
+{
+	if (llc_single_threaded(p))
+		return true;
+
+	return exceed_llc_nr(grp, cpu);
+}
+
 /*
  * A task counts in nr_pref_llc_running while it is queued on its preferred
  * LLC (pref_llc_queued) and runnable (!sched_delayed), keeping the counter in
@@ -2018,8 +2028,12 @@ static void task_cache_work(struct callback_head *work)
 				     READ_ONCE(llc_epoch_period), 1)))
 		return;
 
+	/*
+	 * Don't scan for what can't be aggregated anyway. The number of
+	 * active threads is only known after the scan, see below.
+	 */
 	curr_cpu = task_cpu(p);
-	if (invalid_llc_nr(grp, p, curr_cpu) ||
+	if (llc_single_threaded(p) || !get_sched_cache_scale(1) ||
 	    exceed_llc_capacity(grp, curr_cpu)) {
 		if (READ_ONCE(grp->cpu) != -1)
 			WRITE_ONCE(grp->cpu, -1);
@@ -2095,6 +2109,14 @@ static void task_cache_work(struct callback_head *work)
 		}
 	}
 
+	update_avg_scale(&grp->nr_running_avg, nr_running);
+	if (exceed_llc_nr(grp, curr_cpu)) {
+		if (READ_ONCE(grp->cpu) != -1)
+			WRITE_ONCE(grp->cpu, -1);
+
+		return;
+	}
+
 	if (m_a_occ > (2 * curr_m_a_occ)) {
 		/*
 		 * Avoid switching sched_cache_grp->cpu too fast.
@@ -2108,8 +2130,6 @@ static void task_cache_work(struct callback_head *work)
 		 */
 		WRITE_ONCE(grp->cpu, m_a_cpu);
 	}
-
-	update_avg_scale(&grp->nr_running_avg, nr_running);
 }
 
 void init_sched_mm(struct task_struct *p)

base-commit: 74ce3843347b0fe192e9bf29f46e1a266515defa
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03  7:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  7:37 [PATCH] sched/cache: Update nr_running_avg before testing it in task_cache_work() Hang Zhengyang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®