mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luo Gengkun <luogengkun@huaweicloud.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Chen Yu <yu.c.chen@intel.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>
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>,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 2/7] sched/cache: Add thread count to cache group
Date: Thu, 26 Nov 2026 13:48:23 +0000	[thread overview]
Message-ID: <4d976ab4e82b43f7733b3a47729e66aff3d32e58.1795691926.git.luogengkun2@huawei.com> (raw)
In-Reply-To: <cover.1795691926.git.luogengkun2@huawei.com>

Every cache group belongs to an mm today, so get_nr_threads() is a
reasonable stand-in for the group's member count.  Later patches in
this series will introduce cache groups that no mm owns, and
get_nr_threads() cannot tell whether such a group is valid.

So add an atomic nr_threads counter to struct sched_cache_group and
maintain it through fork, exec, exit and failed-fork cleanup.
invalid_llc_nr() switches to grp->nr_threads, an exact count of the
tasks that reference the group.

Also add a WARN_ON_ONCE in sched_cache_group_free_rcu() so any leak is
caught when the group is finally released: a group being freed has no
members left, so a non-zero count means an attach/detach pairing bug.

Signed-off-by: Luo Gengkun <luogengkun@huaweicloud.com>
---
 include/linux/sched.h      |  1 +
 kernel/sched/cache_sched.c | 59 ++++++++++++++++++++++++++++++++------
 kernel/sched/cache_sched.h |  1 +
 kernel/sched/fair.c        | 14 ++++-----
 4 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index d35ae49a991f..3073ed421b58 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2415,6 +2415,7 @@ struct sched_cache_group {
 	unsigned long footprint;
 	int cpu;
 	refcount_t refcnt;
+	atomic_t nr_threads;
 	struct rcu_head rcu;
 } ____cacheline_aligned_in_smp;
 
diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
index c20ed59ba45b..e1562b6af0bc 100644
--- a/kernel/sched/cache_sched.c
+++ b/kernel/sched/cache_sched.c
@@ -20,6 +20,7 @@ static void sched_cache_group_free_rcu(struct rcu_head *rcu)
 	struct sched_cache_group *grp =
 		container_of(rcu, struct sched_cache_group, rcu);
 
+	WARN_ON_ONCE(atomic_read(&grp->nr_threads));
 	free_percpu(grp->pcpu_sched);
 	kfree(grp);
 }
@@ -54,26 +55,60 @@ struct sched_cache_group *task_cache_group_get(struct task_struct *p)
 	return sched_cache_group_get(rcu_dereference(p->sched_cache_grp));
 }
 
+/*
+ * Become a member of @grp and take one reference on it.  The reference
+ * returned is the caller's: publishing @grp into a task's sched_cache_grp
+ * hands it to the field, and every other outcome gives it back through
+ * sched_cache_group_detach().
+ *
+ * Returns NULL, having taken nothing, when @grp is NULL or already being
+ * released.
+ */
+static struct sched_cache_group *
+sched_cache_group_attach(struct sched_cache_group *grp) __must_check;
+
+static struct sched_cache_group *
+sched_cache_group_attach(struct sched_cache_group *grp)
+{
+	if (!grp || !refcount_inc_not_zero(&grp->refcnt))
+		return NULL;
+
+	atomic_inc(&grp->nr_threads);
+	return grp;
+}
+
+void sched_cache_group_detach(struct sched_cache_group *grp)
+{
+	if (grp) {
+		atomic_dec(&grp->nr_threads);
+		sched_cache_group_put(grp);
+	}
+}
+
 void sched_cache_fork(struct task_struct *p)
 {
 	/*
 	 * The child takes its own reference on the mm's cache group, separate
-	 * from the reference held by the mm. @p is not yet visible to readers,
-	 * so a plain initializing store is enough.
+	 * from the reference held by the mm.  Attaching takes that reference
+	 * and the membership in one step, and the store publishes both: @p is
+	 * not yet visible to readers, so a plain initializing store is enough.
 	 */
 	RCU_INIT_POINTER(p->sched_cache_grp,
-			 sched_cache_group_get(p->mm->sched_cache_grp));
+			 sched_cache_group_attach(p->mm->sched_cache_grp));
 }
 
 void sched_cache_fork_cleanup(struct task_struct *p)
 {
+	struct sched_cache_group *grp;
+
 	/*
 	 * A fork that fails after sched_cache_fork() never reaches exit_mm(),
 	 * so drop the reference here. @p never became visible, so there are no
 	 * concurrent readers and the reference we hold keeps the group alive.
 	 */
-	sched_cache_group_put(rcu_access_pointer(p->sched_cache_grp));
+	grp = rcu_access_pointer(p->sched_cache_grp);
 	RCU_INIT_POINTER(p->sched_cache_grp, NULL);
+	sched_cache_group_detach(grp);
 }
 
 void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
@@ -81,11 +116,16 @@ void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
 	struct sched_cache_group *old;
 
 	/*
-	 * Acquire the new reference before publishing the pointer, then drop
-	 * the old one. @p is current and the only writer of its own pointer.
+	 * Attaching takes the new group's reference and its membership before
+	 * the store publishes it, so a reader that finds it in the field sees
+	 * a group @p is already counted in.  The store then hands over that
+	 * reference and the old group is detached, which drops both its
+	 * membership and the reference it was published with.  @p is current
+	 * and the only writer of its own pointer.
 	 */
-	old = sched_cache_replace_grp(p, sched_cache_group_get(mm->sched_cache_grp));
-	sched_cache_group_put(old);
+	old = sched_cache_replace_grp(p,
+			sched_cache_group_attach(mm->sched_cache_grp));
+	sched_cache_group_detach(old);
 }
 
 void sched_cache_exit_mm(struct task_struct *p)
@@ -105,7 +145,7 @@ void sched_cache_exit_mm(struct task_struct *p)
 		WRITE_ONCE(grp->footprint, fp - sub);
 	}
 #endif
-	sched_cache_group_put(grp);
+	sched_cache_group_detach(grp);
 }
 
 static void sched_cache_group_init(struct sched_cache_group *grp,
@@ -131,6 +171,7 @@ static void sched_cache_group_init(struct sched_cache_group *grp,
 	grp->nr_running_avg = 0;
 	grp->footprint = 0;
 	refcount_set(&grp->refcnt, 1);
+	atomic_set(&grp->nr_threads, 0);
 	/*
 	 * The update to grp->pcpu_sched should not be reordered
 	 * before initialization to grp's other fields, in case
diff --git a/kernel/sched/cache_sched.h b/kernel/sched/cache_sched.h
index 4d0febe5fee1..c73595ad6a9a 100644
--- a/kernel/sched/cache_sched.h
+++ b/kernel/sched/cache_sched.h
@@ -13,6 +13,7 @@
 struct sched_cache_group *
 sched_cache_alloc_group(struct sched_cache_time __percpu *_pcpu_sched);
 void sched_cache_group_put(struct sched_cache_group *grp);
+void sched_cache_group_detach(struct sched_cache_group *grp);
 
 #endif /* CONFIG_SCHED_CACHE */
 
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 29c6be227ef8..f5610cbc76ec 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1527,17 +1527,17 @@ static bool exceed_llc_capacity(struct sched_cache_group *grp, int cpu)
 	return false;
 }
 
-static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p,
-			   int cpu)
+static bool invalid_llc_nr(struct sched_cache_group *grp, int cpu)
 {
 	int scale;
 
-	if (get_nr_threads(p) <= 1)
+	/* Skip if the cache group has at most one member. */
+	if (atomic_read(&grp->nr_threads) <= 1)
 		return true;
 
 	/*
 	 * Scale the number of 'cores' in a LLC by llc_aggr_tolerance
-	 * and compare it to the task's active threads.
+	 * and compare it to the group's active threads.
 	 */
 	scale = get_sched_cache_scale(1);
 	if (scale == INT_MAX)
@@ -1802,7 +1802,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
 	 * its preferred state.
 	 */
 	if ((long)(epoch - READ_ONCE(grp->epoch)) > llc_epoch_affinity_timeout ||
-	    invalid_llc_nr(grp, p, cpu_of(rq)) ||
+	    invalid_llc_nr(grp, cpu_of(rq)) ||
 	    exceed_llc_capacity(grp, cpu_of(rq))) {
 		if (READ_ONCE(grp->cpu) != -1)
 			WRITE_ONCE(grp->cpu, -1);
@@ -1946,7 +1946,7 @@ static void task_cache_work(struct callback_head *work)
 		return;
 
 	curr_cpu = task_cpu(p);
-	if (invalid_llc_nr(grp, p, curr_cpu) ||
+	if (invalid_llc_nr(grp, curr_cpu) ||
 	    exceed_llc_capacity(grp, curr_cpu)) {
 		if (READ_ONCE(grp->cpu) != -1)
 			WRITE_ONCE(grp->cpu, -1);
@@ -10830,7 +10830,7 @@ static enum llc_mig can_migrate_llc_task(struct lb_env *env,
 		return mig_unrestricted;
 
 	/* skip cache aware load balance for too many threads */
-	if (invalid_llc_nr(grp, p, dst_cpu) ||
+	if (invalid_llc_nr(grp, dst_cpu) ||
 	    exceed_llc_capacity(grp, dst_cpu)) {
 		if (READ_ONCE(grp->cpu) != -1)
 			WRITE_ONCE(grp->cpu, -1);
-- 
2.34.1


  parent reply	other threads:[~2026-11-26 13:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 13:48 [RFC PATCH 0/7] sched/cache: Introduce per-task affinity group via BPF detector Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 1/7] sched/cache: Move the cache group lifecycle to cache_sched.c Luo Gengkun
2026-11-26 13:48 ` Luo Gengkun [this message]
2026-11-26 13:48 ` [RFC PATCH 3/7] sched/cache: Introduce the task affinity group Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 4/7] sched/cache: Converge task accounting to the affinity root Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 5/7] sched/cache: Add the per-CPU hint queue Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 6/7] sched/cache: Add BPF-facing hint submitters Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 7/7] samples/bpf: Add pipe-based cache hint example Luo Gengkun

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=4d976ab4e82b43f7733b3a47729e66aff3d32e58.1795691926.git.luogengkun2@huawei.com \
    --to=luogengkun@huaweicloud.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --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®