From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C38BF3321D7 for ; Wed, 18 Feb 2026 23:21:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771456872; cv=none; b=Y15DNDj1uTel/pQrh/E04PUP8adaU6aCwLrxdJyoCmpjIXt0XrclqSlGh8AQYC/6UUW7B+4rR8s2/OEfa98aYoIJDZnV/d5QYGmbd0HZmR9BzkTD3otTVd/4B347rzR0HKrm9FRvsV+GcVMYSM/t5U/CoCPqoOemD4rsheRZ4u4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771456872; c=relaxed/simple; bh=9QcWuXdZLQJj1M24ecLnoAjHmeagD2eNKOaxs1GTUXc=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=j766GERCEQkO1DXhSmr1nybBQEyVZQcPOPeIV426h3RUMEEOVB09t37gsd3yDqPMHdQSLXjmHl2DpCLmhGW2bzwqGlJf5nEhFVYbLJypFhUSoqXsuMhBXMCGRftUJTNEPimh/wxBy2iZhsAhDZAQgXWCe3UePFoPV3QqenQGLbo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Og6RBNUU; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Og6RBNUU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1771456871; x=1802992871; h=message-id:subject:from:to:cc:date:in-reply-to: references:content-transfer-encoding:mime-version; bh=9QcWuXdZLQJj1M24ecLnoAjHmeagD2eNKOaxs1GTUXc=; b=Og6RBNUUZWW04whW6hgEIA321YKu/2Lv7Wtmnf6pinaKLTxHzEeErE4H rXo1m6jjSFSq7zHZWXELrgXnVXoVFy25S2/+7itzR98qD4+xtqC0/xqKf qQSIQ883H9Qe/dWPoUsWLnWPjEVvY3TGJxisj5rlm9ndKAPHayO3SixNV QRTmYbaHYRsd92VdD7sPXUu+kM18pabcrgqbdUgJIjeZQjUFr537pG1o+ +89hJe4vKkkwvo6vqddFW5HSjVmmMn0tZiWbwzu0OKWb3fZyfalBorKcH nGiJRRSg77m7uFsPiScAxX/aZbpN7o/NIrtHWHuamsqRWJTgYiJGznNKI Q==; X-CSE-ConnectionGUID: yQwLsawSQwe6zuce5YUerA== X-CSE-MsgGUID: mMarhpkSTd+u7VInqmh4PQ== X-IronPort-AV: E=McAfee;i="6800,10657,11705"; a="72430886" X-IronPort-AV: E=Sophos;i="6.21,299,1763452800"; d="scan'208";a="72430886" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2026 15:21:10 -0800 X-CSE-ConnectionGUID: gUIrh46mS6uN/am03oYakg== X-CSE-MsgGUID: R6nw/d7JSc2+BptDB2vpMA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,299,1763452800"; d="scan'208";a="218871506" Received: from unknown (HELO [10.241.243.83]) ([10.241.243.83]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2026 15:21:10 -0800 Message-ID: Subject: Re: [PATCH v3 04/21] sched/cache: Make LLC id continuous From: Tim Chen To: K Prateek Nayak , "Chen, Yu C" Cc: Juri Lelli , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Madadi Vineeth Reddy , Hillf Danton , Shrikanth Hegde , Jianyong Wu , Yangyu Chen , Tingyin Duan , Vern Hao , Vern Hao , Len Brown , Aubrey Li , Zhao Liu , Chen Yu , Adam Li , Aaron Lu , Tim Chen , Josh Don , Gavin Guo , Qais Yousef , Libo Chen , linux-kernel@vger.kernel.org, Peter Zijlstra , "Gautham R . Shenoy" , Vincent Guittot , Ingo Molnar Date: Wed, 18 Feb 2026 15:21:09 -0800 In-Reply-To: References: <60a05a3f50d14a7bf3b968f62cca87893c5c552c.1770760558.git.tim.c.chen@linux.intel.com> <79755f7d-cc68-4189-b6d8-850378e54017@amd.com> <95923e36-4117-4209-97aa-a92b60f2bd49@intel.com> <6a73ac0d-1fc9-49a7-87a2-65951ab2c844@amd.com> <8df218ecf39015830856c7b1053a0f777cc1ea42.camel@linux.intel.com> <579c9039-cd66-41c3-8a72-e73d6f836ee8@amd.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.1 (3.58.1-1.fc43) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Wed, 2026-02-18 at 23:16 +0530, K Prateek Nayak wrote: > Hello Chenyu, >=20 > On 2/18/2026 8:52 PM, Chen, Yu C wrote: > > > On a separate note, if we add a dependency on SCHED_MC for SCHED_CACH= E, > > > we can simply look at cpu_coregroup_mask() and either allocate a new > > > llc_id / borrow llc id=C2=A0 in sched_cpu_activate() when CPU is onli= ned or > > > reassign them in sched_cpu_deactivate() if an entire LLC is offlined. > > >=20 > >=20 > > Prateek, may I know if you are thinking of updating every CPU's LLC id > > during its hotplug and not update all percpu LLC id in build_sched_doma= ins()? >=20 > I was still thinking of build_sched_domains() (or somewhere in the > online and offline path) where we can first simply look at > cpu_coregroup_mask() and decide if we need to traverse all CPUs and > shuffle the IDs. Pratek, How about modifying the patch like the following, stealing a lot of your code. Also added change to shrink max LLCs when the LLC with max id lost its last CPU. Thanks. Tim --- diff --git a/init/Kconfig b/init/Kconfig index 9848de949afa..4ddf54ab9cf7 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -987,6 +987,7 @@ config SCHED_CACHE bool "Cache aware load balance" default y depends on SMP + depends on SCHED_MC help When enabled, the scheduler will attempt to aggregate tasks from the same process onto a single Last Level Cache (LLC) domain when diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 48626c81ba8e..75ba4e0bfcd3 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8474,6 +8474,8 @@ int sched_cpu_deactivate(unsigned int cpu) */ synchronize_rcu(); =20 + sched_domains_free_llc_id(cpu); + sched_set_rq_offline(rq, cpu); =20 scx_rq_deactivate(rq); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 6cbc56e9adfc..04f42526e6f0 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3862,6 +3862,7 @@ static inline bool sched_cache_enabled(void) extern void sched_cache_active_set_unlocked(void); #endif extern void init_sched_mm(struct task_struct *p); +void sched_domains_free_llc_id(int cpu); =20 extern u64 avg_vruntime(struct cfs_rq *cfs_rq); extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se)= ; diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index 580fb2fbc900..5e59340ad9a9 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -18,6 +18,7 @@ void sched_domains_mutex_unlock(void) } =20 /* Protected by sched_domains_mutex: */ +static cpumask_var_t sched_domains_llc_id_allocmask; static cpumask_var_t sched_domains_tmpmask; static cpumask_var_t sched_domains_tmpmask2; static int tl_max_llcs; @@ -2590,6 +2591,57 @@ static bool topology_span_sane(const struct cpumask = *cpu_map) return true; } =20 +static int __sched_domains_alloc_llc_id(void) +{ + int lid; + + lockdep_assert_held(&sched_domains_mutex); + + lid =3D cpumask_first_zero(sched_domains_llc_id_allocmask); + if (lid >=3D tl_max_llcs) + tl_max_llcs =3D lid + 1; + + /* + * llc_id space should never grow larger than the + * possible number of CPUs in the system. + */ + if (!unlikely(WARN_ON_ONCE(lid >=3D nr_cpumask_bits))) + cpumask_set_cpu(lid, sched_domains_llc_id_allocmask); + return lid; +} + +static void __sched_domains_free_llc_id(int cpu) +{ + int i, lid; + + lockdep_assert_held(&sched_domains_mutex); + + lid =3D per_cpu(sd_llc_id, cpu); + if (lid =3D=3D -1) + return; + + per_cpu(sd_llc_id, cpu) =3D -1; + + for_each_online_cpu(i) { + /* An online CPU owns the llc_id. */ + if (per_cpu(sd_llc_id, i) =3D=3D lid) + return; + } + + cpumask_clear_cpu(lid, sched_domains_llc_id_allocmask); + + /* shrink max LLC size to save memory */ + if (lid =3D=3D tl_max_llcs - 1) + lid =3D tl_max_llcs--; +} + +void sched_domains_free_llc_id(int cpu) +{ + sched_domains_mutex_lock(); + __sched_domains_free_llc_id(cpu); + sched_domains_mutex_unlock(); +} + /* * Build sched domains for a given set of CPUs and attach the sched domain= s * to the individual CPUs @@ -2615,18 +2667,11 @@ build_sched_domains(const struct cpumask *cpu_map, = struct sched_domain_attr *att =20 /* Set up domains for CPUs specified by the cpu_map: */ for_each_cpu(i, cpu_map) { - struct sched_domain_topology_level *tl, *tl_llc =3D NULL; + struct sched_domain_topology_level *tl; int lid; =20 sd =3D NULL; for_each_sd_topology(tl) { - int flags =3D 0; - - if (tl->sd_flags) - flags =3D (*tl->sd_flags)(); - - if (flags & SD_SHARE_LLC) - tl_llc =3D tl; =20 sd =3D build_sched_domain(tl, cpu_map, attr, sd, i); =20 @@ -2642,18 +2687,19 @@ build_sched_domains(const struct cpumask *cpu_map, = struct sched_domain_attr *att if (lid =3D=3D -1) { int j; =20 + j =3D cpumask_first(cpu_coregroup_mask(i)); /* * Assign the llc_id to the CPUs that do not * have an LLC. */ - if (!tl_llc) { - per_cpu(sd_llc_id, i) =3D tl_max_llcs++; + if (j >=3D nr_cpu_ids) { + per_cpu(sd_llc_id, i) =3D __sched_domains_alloc_llc_id(); =20 continue; } =20 /* try to reuse the llc_id of its siblings */ - for_each_cpu(j, tl_llc->mask(tl_llc, i)) { + for (; j < nr_cpu_ids; j =3D cpumask_next(j, cpu_coregroup_mask(i))) { if (i =3D=3D j) continue; =20 @@ -2668,7 +2714,7 @@ build_sched_domains(const struct cpumask *cpu_map, st= ruct sched_domain_attr *att =20 /* a new LLC is detected */ if (lid =3D=3D -1) - per_cpu(sd_llc_id, i) =3D tl_max_llcs++; + per_cpu(sd_llc_id, i) =3D __sched_domains_alloc_llc_id(); } } =20 @@ -2869,6 +2915,7 @@ int __init sched_init_domains(const struct cpumask *c= pu_map) { int err; =20 + zalloc_cpumask_var(&sched_domains_llc_id_allocmask, GFP_KERNEL); zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL); zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL); zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);