From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796283464; b=wchDxQn3Ubj/YaH6sseU3F1Jd+gKyWs2Ontnk4uYcX9PMQSYGKX5CIl5u9WG0mGzsouU dnSVWW+e7245WsYxpI1hGeSaDe+nZD/teDC/Aw3Sp4Vp59fVd9Uv/rtgs4xLCKj7XDpZW 4S/L6RJlEq/v7NoDkywS89i68C9/q848ouDoxACgHm4fOeAqdDOjJPAlqZHmgfPOFVini Mc5G1trogvRzIkVIVWWWJkNI0W/vkiimwfY5Qjb+JjeR3a5gZloO2MwbkYdu+MJtnFlH8 L06fDDZsIHjV2aa4t1MDXjr7jU+kxuVbO/igTgr4/hSSP5Betf0vg7u3J149DURhOqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796283464; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=jvqR0LrC/6jS9ibs9UyOcE4N+Mqs0pihKu5g4ACJz9c=; b=UpDMqooXaNkCN9mDfI2rMuMRt626aODGwU4WxNHcofGnf9wsm9lPKsWbr3PpRSy1l68S fuvr+bM/xsBDK78/afMYRQC4cPRy9T+p7RwR5wdCYVd637QaHkXOeD/CLr4AIBuJAJZPu rq1gZcgCbW4zGT33t9unShlnQlx6wRrE8gAANcYL+BN3ViBFZpZoee3jA79qaA4CaTc9N b/Nwq9xHCx4EYN8ZEzebYAn0Fw3NQcRBoxYeWP28QOfbjeEt6LXKIVaBMCKx7fH5+m/WV VkU7JEoIiCBOaK/n0fNmRcItcOVkoPI8Im+pgahny/s9nlgr3g9B9r3njEdtMsg61og== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Db+PzwAF; arc=none smtp.remote-ip=209.85.216.53 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 263E7606A322 for ; Thu, 3 Dec 2026 07:37:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 263E7606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Db+PzwAF Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3b0e0e65adbso6258584a91.3 for ; Wed, 02 Dec 2026 23:37:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796283463; x=1796888263; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=jvqR0LrC/6jS9ibs9UyOcE4N+Mqs0pihKu5g4ACJz9c=; b=Db+PzwAFqUt5uJxeVGn7bXD1IFufJRthnm/QzWtv7/33UitpDQ04TKwe1aELARLS4L dz+gr9ruqSFFZx/2EhidlGLo8y/MStJ9NoxirNL8YHpX42kOECCut+aMVJ9ChtK8YpCo iAwbDGNXe7SyXSVW7Bz2IRtvWnmbKfHy7r33pZOnUG3kGRxVAbPzsK7kJ/R03CLFO+qS WvVGZNzZZCkMWNZg0ujRJUOK99yO4B3veVREBNmQWhu5tUcxBeRYNkWJS35iLkLDdkmz /fPAp3ap3HnPYeUdZqytKUIjJxj0pZhhaelwlgb2zA4cYlR4f1p6uMyGxOxt+0R+e3Vx Ojrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796283463; x=1796888263; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jvqR0LrC/6jS9ibs9UyOcE4N+Mqs0pihKu5g4ACJz9c=; b=JMy1i3ydC+RU5pK5Up/wgdBGIeBhjoYpum9W6seHGsyVbMPGcEtU/zyPAJDHWfMXJx fwQsVLWZiSaE1BSwK2G90bX3P0Yi4qisZ9XSsDGNGdGJ6qCUTc7YX620fGhzzuaCqJCH 2MwXJIrMl0jnhm1TJhL8SM+TGXAON6apntQ2cCOF7LzKrHcmPHrA/BCDmzZfeMXsU3um /3V9nSa2G1jdJ2cJrWAAzXLfEO6c/08RFJZO4VbVgLeTfa3KE4c3G8jJCkz5fM73IbUL k+0HFn7tKWifc+gzJe6h/o5HOum31voKi6gOcC1oFjiHu25orXQatqw8bcNi/njiTltt WQ9A== X-Forwarded-Encrypted: i=1; AL+hErHLA3G8sKgLbdFo1hieE0v6RJqsyDreHYlcKNs3riBYUdBoxCd1JfKfHK5dqL+Fr35pEPBekhBCj3TmvPQ=@vger.kernel.org X-Gm-Message-State: APuU4Z9fBg4PGkD6ddMKzGivafmEyzbIkJY89msAKiavqiszANSP3+nP 7LvUCf697lpQwODH4piDhVqiyXRC0JiTV59ZhLUKF8NueCu3JGR90cdZ X-Gm-Gg: AZFdTZclnWJ5ebldCqgg8KbM0+jFSCpAgHuLFR2Z3NJ87VJrpPzKmrjlc3vZfV5pC71 5JQrQwB805j3gBDX1rxXKKSVsxFilkJneq9tohw9yM6MINf9IPgmyta4+1iqGGJSTWHX6AfCxT2 sUvpYBOxluebvRcuFSiDXOQJaOYkDGYz+8gJfXW/Xy0fkYOYqBV9TvMgkbE9zGWdC7IGnZxpJNN iiw6AYjOZkwIXSlqOnKGDQNxkS2Sv4vHd0hGxDj8S8OkKFUYW/zRWabxB42r8o1S9NWmBHfr3G/ LbV7ns+iF2J+IKJjiATIh68W20ovp5DnzWYISqJ1CAt3wOxz9zW+Z2NQUvTdQRwedI6jccs/CvZ uYa2G41zK+zO12KpGajHvjhCKq38meFLWOKKdJg/tfrxtcd21UgM/K3+FujyWx8e/RubR4HUqNm CR8AdnbmqNMl2CjrD6WdhUE5Snkx1e1lQnq2xxEEDwnvt55ylwSKGIowVAGiOiwEUUsQ7H/Hwqv JYDErr6RIFoNq0VnffvZegePL0b4708K/XKztLOsoJOIQjErZgb4+KJBKxFp243dRidn74KJxAw QU2uiiIQn4LGhvzzRvN0kn0yCA4hRD42D251WT/8p3F1pzTx6g9MhthHgUfdKj8k9PVazg/qsyv xzVyk/rNF X-Received: by 2002:a17:90b:3d8d:b0:3b3:b750:d2ed with SMTP id 98e67ed59e1d1-3b41c7b56c9mr44905447a91.8.1796283463026; Wed, 02 Dec 2026 23:37:43 -0800 (PST) Received: from thinkpad.tail59a646.ts.net ([112.199.224.229]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b704cfesm26231047a91.1.2026.12.02.23.37.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 23:37:42 -0800 (PST) From: Hang Zhengyang 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@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 Message-ID: <20261203073737.2470074-1-hang.zhengyang1010@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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