From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Tim Chen <tim.c.chen@linux.intel.com>,
Chen Yu <yu.c.chen@intel.com>, Chen Yu <chen.yu@linux.dev>,
Luo Gengkun <luogengkun2@huawei.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH] sched/cache: Update nr_running_avg before testing it in task_cache_work()
Date: Thu, 3 Dec 2026 15:37:35 +0800 [thread overview]
Message-ID: <20261203073737.2470074-1-hang.zhengyang1010@gmail.com> (raw)
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
reply other threads:[~2026-12-03 7:37 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203073737.2470074-1-hang.zhengyang1010@gmail.com \
--to=hang.zhengyang1010@gmail.com \
--cc=bsegall@google.com \
--cc=chen.yu@linux.dev \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luogengkun2@huawei.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tim.c.chen@linux.intel.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yu.c.chen@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®