From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (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 DD2F686353 for ; Fri, 20 Feb 2026 19:24:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771615454; cv=none; b=SUsyf5oGnwkrbP+4QXYvHs/ROCk55Bp8NKFTR4AJS9WvN/XDs843jwt+aDRkBgonPQY9dzztH1D8QLrLNgDq0+/NxvLz8XRmXISj0rhvlR5bcsos0Lp9NzIEOKO91jn5CdGFn7lvCzZUjtp65m3Lu7PN/SWxw9jVu8Tqb74oN3g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771615454; c=relaxed/simple; bh=PRjIE6scFlJk6UEyQeIhNg07qeOdhV/DFVZDx4OPVQk=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=sEqlUyAqHIjbXgegMHKk0LvJZzSt/iv3BNDXmlY/zNmoanCsiOwJIYPRNnJhJAs0LydVflxVublnj5DXkIPS10SlzGpPGZzFbXiSZt9LrL8LTYWLPgVIOq37E5Ejqau6B0noU+A/01ocLCufC6aQHMl6WpTetDJKAbTgXggyUSw= 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=LTehDWMV; arc=none smtp.client-ip=198.175.65.18 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="LTehDWMV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1771615453; x=1803151453; h=message-id:subject:from:to:cc:date:in-reply-to: references:content-transfer-encoding:mime-version; bh=PRjIE6scFlJk6UEyQeIhNg07qeOdhV/DFVZDx4OPVQk=; b=LTehDWMVecUNC/80fpTApmFu8HPkMJgemSrWjUR4nEzDlCsQaip8repv ARIhUQNHXV3Xk17VUVaZOtb7sUvSI+Ksn+OCjSKCH/rqNCQXo3XUBzcTs mrWOur3HE0h45i8k9rRU6xNKHRMKnQ3AIcMFMFBTschDCGoAQEPWv3zAX zaFfa2RD1I5/Q6B/00uKCOE1bdPz1CbcS2nS3kQOv5lFXMT+xHaoiiHdZ i8aya8gxgOU1foQUn/0sKbauPGfJb1RGybhulbjRvZ212pbOgLMH+OF01 dsl4Ho5V6qwGNLVH/aTjGP1hcAoBIawccuNkTR0uK27wfdVl0dhLHPmoD Q==; X-CSE-ConnectionGUID: kyab7FXcQouaV26B3aZ8kw== X-CSE-MsgGUID: R11hZqcXSXurYO8HGC5p/A== X-IronPort-AV: E=McAfee;i="6800,10657,11707"; a="72756386" X-IronPort-AV: E=Sophos;i="6.21,302,1763452800"; d="scan'208";a="72756386" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Feb 2026 11:24:13 -0800 X-CSE-ConnectionGUID: Gq3mxdQGSzafI/QniVYVgQ== X-CSE-MsgGUID: w7nRbpqaSuiqPWYHKTBwVw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,302,1763452800"; d="scan'208";a="215069478" Received: from schen9-mobl4.amr.corp.intel.com (HELO [10.125.110.105]) ([10.125.110.105]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Feb 2026 11:24:12 -0800 Message-ID: Subject: Re: [PATCH v3 04/21] sched/cache: Make LLC id continuous From: Tim Chen To: "Chen, Yu C" , Peter Zijlstra Cc: Ingo Molnar , "Gautham R . Shenoy" , Vincent Guittot , 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, K Prateek Nayak Date: Fri, 20 Feb 2026 11:24:11 -0800 In-Reply-To: References: <60a05a3f50d14a7bf3b968f62cca87893c5c552c.1770760558.git.tim.c.chen@linux.intel.com> <79755f7d-cc68-4189-b6d8-850378e54017@amd.com> <20260219154007.GI1395266@noisy.programming.kicks-ass.net> <2ffbceaa-f9ab-461a-8324-3f500fcf8786@intel.com> <20260220160300.GH2995752@noisy.programming.kicks-ass.net> 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 Sat, 2026-02-21 at 00:10 +0800, Chen, Yu C wrote: > On 2/21/2026 12:03 AM, Peter Zijlstra wrote: > > On Fri, Feb 20, 2026 at 11:53:31PM +0800, Chen, Yu C wrote: > > > Hi Peter, > > >=20 > > > On 2/19/2026 11:40 PM, Peter Zijlstra wrote: > > > > On Mon, Feb 16, 2026 at 01:14:20PM +0530, K Prateek Nayak wrote: > > >=20 > > > [ ... ] > > >=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++; > > > >=20 > > > > Urgh,. should we not rather track the max lid? > > > >=20 > > >=20 > > > Do you mean we should not always increment the max lid, > > > but instead decrease it when an llc_id is released? > > > I think Tim has adjusted the code to shrink tl_max_llcs > > > when an llc_id is released: > > > https://lore.kernel.org/all/acc7a5c96e8235bf11af640798ce1b60bcaa8196.= camel@linux.intel.com/ > >=20 > > You can only shrink when the max lid is released. Since lid is an array > > index, something like max_lid =3D weight(mask) would be terribly broken= . > >=20 > > But what I was getting at is that the code as presented there is rather > > non-obvious. Yes, if the lid is higher, it cannot be more than one > > higher than the previous value, but something like: > >=20 > > lid =3D cpumask_first_zero(); > > BUG_ON(lid >=3D nr_cpu_ids); > > max_lid =3D max(max_lid, lid); > >=20 > > Is way simpler to follow since it doesn't have that hidden assumption. > >=20 > > Then, if you want to allow shrinking, then the clear side could do > > something like: > >=20 > > __cpumask_clear(lid, mask); > > if (lid =3D=3D max_lid) > > max_lid =3D cpumask_last(mask); > >=20 > > or something like that. > >=20 >=20 > Got it, we will adjust the code accordingly. >=20 How about modifying this patch like the following: Thanks. Tim --- diff --git a/init/Kconfig b/init/Kconfig index f4b2649f8401..da405c00e9e3 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -994,6 +994,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 c464e370576f..e34b5842caa4 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8372,6 +8372,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 f4785f84b1f1..3096adc13074 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3932,6 +3932,13 @@ static inline bool sched_cache_enabled(void) =20 extern void sched_cache_active_set_unlocked(void); #endif + +#ifdef CONFIG_SMP +void sched_domains_free_llc_id(int cpu); +#else /* !CONFIG_SMP: */ +static inline void sched_domains_free_llc_id(int cpu) { } +#endif /* !CONFIG_SMP */ + extern void init_sched_mm(struct task_struct *p); =20 extern u64 avg_vruntime(struct cfs_rq *cfs_rq); diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index e86dea1b9e86..f3bc6636170f 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; @@ -2660,6 +2661,61 @@ static bool topology_span_sane(const struct cpumask = *cpu_map) return true; } =20 +#ifdef CONFIG_SMP +static int __sched_domains_alloc_llc_id(void) +{ + int lid, max_lid; + + lockdep_assert_held(&sched_domains_mutex); + + lid =3D cpumask_first_zero(sched_domains_llc_id_allocmask); + /* + * llc_id space should never grow larger than the + * possible number of CPUs in the system. + */ + BUG_ON(lid >=3D nr_cpu_ids); + max_lid =3D cpumask_last(sched_domains_llc_id_allocmask); + /* size is one more than max index */ + tl_max_llcs =3D max(lid, max_lid) + 1; + + return lid; +} + +static void __sched_domains_free_llc_id(int cpu) +{ + int i, lid, last_lid; + + lockdep_assert_held(&sched_domains_mutex); + + lid =3D per_cpu(sd_llc_id, cpu); + if (lid =3D=3D -1) + return; + + BUG_ON(lid >=3D nr_cpu_ids); + per_cpu(sd_llc_id, cpu) =3D -1; + + for_each_cpu(i, cpu_coregroup_mask(cpu)) { + /* 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); + + last_lid =3D cpumask_last(sched_domains_llc_id_allocmask); + /* shrink max LLC size to save memory */ + if (last_lid < tl_max_llcs - 1) + tl_max_llcs =3D last_lid + 1; +} + +void sched_domains_free_llc_id(int cpu) +{ + sched_domains_mutex_lock(); + __sched_domains_free_llc_id(cpu); + sched_domains_mutex_unlock(); +} +#endif /* CONFIG_SMP */ + /* * Build sched domains for a given set of CPUs and attach the sched domain= s * to the individual CPUs @@ -2685,18 +2741,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 @@ -2708,22 +2757,14 @@ build_sched_domains(const struct cpumask *cpu_map, = struct sched_domain_attr *att break; } =20 +#ifdef CONFIG_SMP lid =3D per_cpu(sd_llc_id, i); if (lid =3D=3D -1) { int j; =20 - /* - * 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++; - - continue; - } - + j =3D cpumask_first(cpu_coregroup_mask(i)); /* 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 @@ -2738,8 +2779,9 @@ 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(); } +#endif /* CONFIG_SMP */ } =20 if (WARN_ON(!topology_span_sane(cpu_map))) @@ -2939,6 +2981,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);