Skip to contentSkip to conversation

sched/fair: Dynamic asym priority support

PATCH Applied K Prateek Nayak 4 participants 12 messages Apr 9, 2025 – Apr 16, 2025

Subsystems (3)
lore
Share & tools
lore

K Prateek Nayak

author reviewer
#0 lore raw

v2 cover sched/fair: Dynamic asym priority support

A subset of AMD Processors which support Preferred Core rankings can have these rankings change at runtime to bias the load balancing towards CPUs with higher frequency / larger cache.

In the current implementation, the CPU with the highest asym priority - "asym_prefer_cpu" is cached in the sched_group struct when building the sched domain hierarchy.

Previous approach in [1] to uncache the "asym_prefer_cpu" and compute it during load balancing was not popular as it not only lost the benefits of caching but also added more overhead in update_sg_lb_stats().

At OSPM'25, Vincent suggested retaining "asym_prefer_cpu" but updating it dynamically when the asym priority changes without needing to rebuild the entire sched domain hierarchy.

Introduce sched_update_asym_prefer_cpu() which traverses the local hierarchy on priority change and recomputes the "asym_prefer_cpu". Since sched_group for !SD_OVERLAP domains are shared by all the CPUs in sched_group_span(sg) (see get_group() in kernel/sched/topology.c), updating the "asym_prefer_cpu" in the groups of the local hierarchy ensures all the CPUs in the group see the updated value.

Groups of SD_OVERLAP domains can be supported too but this involves moving "asym_prefer_cpu" to "sg->sgc" which adds another level of indirection. Since there isn't a use case currently where both SD_OVERLAP and SD_ASYM_PACKING is set for the same sched domain, v2 keeps things simple only extends dynamic updates to groups of !SD_OVERLAP domains. If this future looking enablement is required, please do let me know.

Printing the "asym_prefer_cpu" for the local group in debugfs has not only proved useful to debug this series but has also helped uncover other unrelated issues like [2] which is why I've retained it for inclusion.

This series is based on:

git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git sched/core

at commit 6432e163ba1b ("sched/isolation: Make use of more than one housekeeping cpu")

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
---
Changelog:

v1..v2:

o New approach that introduces sched_update_asym_prefer_cpu() to update the "asym_prefer_cpu" dynamically on ranking change without rebuilding the sched domain hierarchy.
---
K Prateek Nayak (4):
sched/fair: Use READ_ONCE() to read sg->asym_prefer_cpu sched/topology: Introduce sched_update_asym_prefer_cpu() cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change sched/debug: Print the local group's asym_prefer_cpu

 drivers/cpufreq/amd-pstate.c   |  4 ++-
 include/linux/sched/topology.h |  6 ++++
 kernel/sched/debug.c           |  4 +++
 kernel/sched/fair.c            |  5 +--
 kernel/sched/topology.c        | 58 ++++++++++++++++++++++++++++++++++
 5 files changed, 74 insertions(+), 3 deletions(-)

base-commit: 6432e163ba1b7d80b5876792ce53e511f041ab91

Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v2 1/4 sched/fair: Use READ_ONCE() to read sg->asym_prefer_cpu

Subsequent commits add the support to dynamically update the sched_group struct's "asym_prefer_cpu" member from a remote CPU. Use READ_ONCE() when reading the "sg->asym_prefer_cpu" to ensure load balancer always reads the latest value.

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
index 0c19459c8042..5e1bd9e8464c 100644
@@ -10251,7 +10251,7 @@ sched_group_asym(struct lb_env *env, struct sg_lb_stats *sgs, struct sched_group
 	    (sgs->group_weight - sgs->idle_cpus != 1))
 		return false;
 
-	return sched_asym(env->sd, env->dst_cpu, group->asym_prefer_cpu);
+	return sched_asym(env->sd, env->dst_cpu, READ_ONCE(group->asym_prefer_cpu));
 }
 
 /* One group has more than one SMT CPU while the other group does not */
@@ -10488,7 +10488,8 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
 	case group_asym_packing:
 		/* Prefer to move from lowest priority CPU's work */
-		return sched_asym_prefer(sds->busiest->asym_prefer_cpu, sg->asym_prefer_cpu);
+		return sched_asym_prefer(READ_ONCE(sds->busiest->asym_prefer_cpu),
+					 READ_ONCE(sg->asym_prefer_cpu));
 
 	case group_misfit_task:
 		/*
Signature · 1 line
2.34.1
#2 lore raw

[tip: sched/core] sched/fair: Use READ_ONCE() to read sg->asym_prefer_cpu

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     872aa4de18889be63317a8c0f2de71a3a01e487c
Gitweb:        https://git.kernel.org/tip/872aa4de18889be63317a8c0f2de71a3a01e487c
Author:        K Prateek Nayak <[email protected]>
AuthorDate:    Wed, 09 Apr 2025 05:34:43 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 16 Apr 2025 21:09:11 +02:00

sched/fair: Use READ_ONCE() to read sg->asym_prefer_cpu

Subsequent commits add the support to dynamically update the sched_group struct's "asym_prefer_cpu" member from a remote CPU. Use READ_ONCE() when reading the "sg->asym_prefer_cpu" to ensure load balancer always reads the latest value.

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
index 0c19459..5e1bd9e 100644
@@ -10251,7 +10251,7 @@ sched_group_asym(struct lb_env *env, struct sg_lb_stats *sgs, struct sched_group
 	    (sgs->group_weight - sgs->idle_cpus != 1))
 		return false;
 
-	return sched_asym(env->sd, env->dst_cpu, group->asym_prefer_cpu);
+	return sched_asym(env->sd, env->dst_cpu, READ_ONCE(group->asym_prefer_cpu));
 }
 
 /* One group has more than one SMT CPU while the other group does not */
@@ -10488,7 +10488,8 @@ static bool update_sd_pick_busiest(struct lb_env *env,
 
 	case group_asym_packing:
 		/* Prefer to move from lowest priority CPU's work */
-		return sched_asym_prefer(sds->busiest->asym_prefer_cpu, sg->asym_prefer_cpu);
+		return sched_asym_prefer(READ_ONCE(sds->busiest->asym_prefer_cpu),
+					 READ_ONCE(sg->asym_prefer_cpu));
 
 	case group_misfit_task:
 		/*
author reviewer
#3 lore raw

PATCH v2 2/4 sched/topology: Introduce sched_update_asym_prefer_cpu()

In reply to K Prateek Nayak

A subset of AMD Processors supporting Preferred Core Rankings also feature the ability to dynamically switch these rankings at runtime to bias load balancing towards or away from the LLC domain with larger cache.

To support dynamically updating "sg->asym_prefer_cpu" without needing to rebuild the sched domain, introduce sched_update_asym_prefer_cpu() which recomutes the "asym_prefer_cpu" when the core-ranking of a CPU changes.

sched_update_asym_prefer_cpu() swaps the "sg->asym_prefer_cpu" with the CPU whose ranking has changed if the new ranking is greater than that of the "asym_prefer_cpu". If CPU whose ranking has changed is the current "asym_prefer_cpu", it scans the CPUs of the sched groups to find the new "asym_prefer_cpu" and sets it accordingly.

get_group() for non-overlapping sched domains returns the sched group for the first CPU in the sched_group_span() which ensures all CPUs in the group see the updated value of "asym_prefer_cpu".

Overlapping groups are allocated differently and will require moving the "asym_prefer_cpu" to "sg->sgc" but since the current implementations do not set "SD_ASYM_PACKING" at NUMA domains, skip additional indirection and place a SCHED_WARN_ON() to alert any future users.

---
 include/linux/sched/topology.h |  6 ++++
 kernel/sched/topology.c        | 58 ++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
include/linux/sched/topology.h +6 -0
index 7b4301b7235f..198bb5cc1774 100644
@@ -195,6 +195,8 @@ struct sched_domain_topology_level {
 };
 
 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
+extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
+
 
 # define SD_INIT_NAME(type)		.name = #type
 
@@ -223,6 +225,10 @@ static inline bool cpus_share_resources(int this_cpu, int that_cpu)
 	return true;
 }
 
+static inline void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio)
+{
+}
+
 #endif	/* !CONFIG_SMP */
 
 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
kernel/sched/topology.c +58 -0

64 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

[tip: sched/core] sched/topology: Introduce sched_update_asym_prefer_cpu()

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     0e3f6c3696424fa90d6f512779d617a05a1cf031
Gitweb:        https://git.kernel.org/tip/0e3f6c3696424fa90d6f512779d617a05a1cf031
Author:        K Prateek Nayak <[email protected]>
AuthorDate:    Wed, 09 Apr 2025 05:34:44 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 16 Apr 2025 21:09:11 +02:00

sched/topology: Introduce sched_update_asym_prefer_cpu()

A subset of AMD Processors supporting Preferred Core Rankings also feature the ability to dynamically switch these rankings at runtime to bias load balancing towards or away from the LLC domain with larger cache.

To support dynamically updating "sg->asym_prefer_cpu" without needing to rebuild the sched domain, introduce sched_update_asym_prefer_cpu() which recomutes the "asym_prefer_cpu" when the core-ranking of a CPU changes.

sched_update_asym_prefer_cpu() swaps the "sg->asym_prefer_cpu" with the CPU whose ranking has changed if the new ranking is greater than that of the "asym_prefer_cpu". If CPU whose ranking has changed is the current "asym_prefer_cpu", it scans the CPUs of the sched groups to find the new "asym_prefer_cpu" and sets it accordingly.

get_group() for non-overlapping sched domains returns the sched group for the first CPU in the sched_group_span() which ensures all CPUs in the group see the updated value of "asym_prefer_cpu".

Overlapping groups are allocated differently and will require moving the "asym_prefer_cpu" to "sg->sgc" but since the current implementations do not set "SD_ASYM_PACKING" at NUMA domains, skip additional indirection and place a SCHED_WARN_ON() to alert any future users.

---
 include/linux/sched/topology.h |  6 +++-
 kernel/sched/topology.c        | 58 +++++++++++++++++++++++++++++++++-
 2 files changed, 64 insertions(+)
include/linux/sched/topology.h +6 -0
index 7b4301b..198bb5c 100644
@@ -195,6 +195,8 @@ struct sched_domain_topology_level {
 };
 
 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
+extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
+
 
 # define SD_INIT_NAME(type)		.name = #type
 
@@ -223,6 +225,10 @@ static inline bool cpus_share_resources(int this_cpu, int that_cpu)
 	return true;
 }
 
+static inline void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio)
+{
+}
+
 #endif	/* !CONFIG_SMP */
 
 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
kernel/sched/topology.c +58 -0

64 lines not on this page · raw message

author reviewer
#5 lore raw

PATCH v2 3/4 cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change

In reply to K Prateek Nayak

A subset of AMD systems supporting Preferred Core rankings can have their rankings changed dynamically at runtime. Update the "sg->asym_prefer_cpu" across the local hierarchy of CPU when the preferred core ranking changes.

---
 drivers/cpufreq/amd-pstate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cpufreq/amd-pstate.c +3 -1
index 6789eed1bb5b..8796217ccc60 100644
@@ -844,8 +844,10 @@ static void amd_pstate_update_limits(unsigned int cpu)
 	if (highest_perf_changed) {
 		WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
 
-		if (cur_high < CPPC_MAX_PERF)
+		if (cur_high < CPPC_MAX_PERF) {
 			sched_set_itmt_core_prio((int)cur_high, cpu);
+			sched_update_asym_prefer_cpu(cpu, prev_high, cur_high);
+		}
 	}
 }
 
Signature · 1 line
2.34.1
maintainer Acked-by on v2 3/4
#6 lore raw

Re: [PATCH v2 3/4] cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change

Quoted 6 lines · K Prateek Nayak

A subset of AMD systems supporting Preferred Core rankings can have their rankings changed dynamically at runtime. Update the "sg->asym_prefer_cpu" across the local hierarchy of CPU when the preferred core ranking changes.

Quoted 20 lines
---
  drivers/cpufreq/amd-pstate.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cpufreq/amd-pstate.c +3 -1
index 6789eed1bb5b..8796217ccc60 100644
@@ -844,8 +844,10 @@ static void amd_pstate_update_limits(unsigned int cpu)
  	if (highest_perf_changed) {
  		WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
 
-		if (cur_high < CPPC_MAX_PERF)
+		if (cur_high < CPPC_MAX_PERF) {
  			sched_set_itmt_core_prio((int)cur_high, cpu);
+			sched_update_asym_prefer_cpu(cpu, prev_high, cur_high);
+		}
  	}
  }
 
#7 lore raw

[tip: sched/core] cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change

In reply to K Prateek Nayak
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     8157fbc907452aa5674df2de23c1c7305c907006
Gitweb:        https://git.kernel.org/tip/8157fbc907452aa5674df2de23c1c7305c907006
Author:        K Prateek Nayak <[email protected]>
AuthorDate:    Wed, 09 Apr 2025 05:34:45 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 16 Apr 2025 21:09:11 +02:00

cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change

A subset of AMD systems supporting Preferred Core rankings can have their rankings changed dynamically at runtime. Update the "sg->asym_prefer_cpu" across the local hierarchy of CPU when the preferred core ranking changes.

---
 drivers/cpufreq/amd-pstate.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cpufreq/amd-pstate.c +3 -1
index 6789eed..8796217 100644
@@ -844,8 +844,10 @@ static void amd_pstate_update_limits(unsigned int cpu)
 	if (highest_perf_changed) {
 		WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
 
-		if (cur_high < CPPC_MAX_PERF)
+		if (cur_high < CPPC_MAX_PERF) {
 			sched_set_itmt_core_prio((int)cur_high, cpu);
+			sched_update_asym_prefer_cpu(cpu, prev_high, cur_high);
+		}
 	}
 }
 
author reviewer
#8 lore raw

PATCH v2 4/4 sched/debug: Print the local group's asym_prefer_cpu

In reply to K Prateek Nayak

Add a file to read local group's "asym_prefer_cpu" from debugfs. This information was useful when debugging issues where "asym_prefer_cpu" was incorrectly set to a CPU with a lower asym priority.

---
 kernel/sched/debug.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/sched/debug.c +4 -0
index 56ae54e0ce6a..557246880a7e 100644
@@ -588,6 +588,10 @@ static void register_sd(struct sched_domain *sd, struct dentry *parent)
 	debugfs_create_file("flags", 0444, parent, &sd->flags, &sd_flags_fops);
 	debugfs_create_file("groups_flags", 0444, parent, &sd->groups->flags, &sd_flags_fops);
 	debugfs_create_u32("level", 0444, parent, (u32 *)&sd->level);
+
+	if (sd->flags & SD_ASYM_PACKING)
+		debugfs_create_u32("group_asym_prefer_cpu", 0444, parent,
+				   (u32 *)&sd->groups->asym_prefer_cpu);
 }
 
 void update_sched_domain_debugfs(void)
Signature · 1 line
2.34.1
#9 lore raw

[tip: sched/core] sched/debug: Print the local group's asym_prefer_cpu

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     44671e21e3463f36f6c6e4b691216f60e85840e4
Gitweb:        https://git.kernel.org/tip/44671e21e3463f36f6c6e4b691216f60e85840e4
Author:        K Prateek Nayak <[email protected]>
AuthorDate:    Wed, 09 Apr 2025 05:34:46 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 16 Apr 2025 21:09:11 +02:00

sched/debug: Print the local group's asym_prefer_cpu

Add a file to read local group's "asym_prefer_cpu" from debugfs. This information was useful when debugging issues where "asym_prefer_cpu" was incorrectly set to a CPU with a lower asym priority.

---
 kernel/sched/debug.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/sched/debug.c +4 -0
index 56ae54e..5572468 100644
@@ -588,6 +588,10 @@ static void register_sd(struct sched_domain *sd, struct dentry *parent)
 	debugfs_create_file("flags", 0444, parent, &sd->flags, &sd_flags_fops);
 	debugfs_create_file("groups_flags", 0444, parent, &sd->groups->flags, &sd_flags_fops);
 	debugfs_create_u32("level", 0444, parent, (u32 *)&sd->level);
+
+	if (sd->flags & SD_ASYM_PACKING)
+		debugfs_create_u32("group_asym_prefer_cpu", 0444, parent,
+				   (u32 *)&sd->groups->asym_prefer_cpu);
 }
 
 void update_sched_domain_debugfs(void)
maintainer
#10 lore raw
In reply to K Prateek Nayak
Quoted 6 lines · K Prateek Nayak

K Prateek Nayak (4):
sched/fair: Use READ_ONCE() to read sg->asym_prefer_cpu sched/topology: Introduce sched_update_asym_prefer_cpu() cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change sched/debug: Print the local group's asym_prefer_cpu

Quoted 6 lines
 drivers/cpufreq/amd-pstate.c   |  4 ++-
 include/linux/sched/topology.h |  6 ++++
 kernel/sched/debug.c           |  4 +++
 kernel/sched/fair.c            |  5 +--
 kernel/sched/topology.c        | 58 ++++++++++++++++++++++++++++++++++
 5 files changed, 74 insertions(+), 3 deletions(-)

This seems reasonable. I'll queue it up, and unless someone (robot or real person) objects, we'll get it merged :-)

author reviewer
#11 lore raw

Hello Peter,

Quoted 14 lines · Peter Zijlstra
Quoted 6 lines · K Prateek Nayak

K Prateek Nayak (4):
sched/fair: Use READ_ONCE() to read sg->asym_prefer_cpu sched/topology: Introduce sched_update_asym_prefer_cpu() cpufreq/amd-pstate: Update asym_prefer_cpu when core rankings change sched/debug: Print the local group's asym_prefer_cpu

Quoted 6 lines
  drivers/cpufreq/amd-pstate.c   |  4 ++-
  include/linux/sched/topology.h |  6 ++++
  kernel/sched/debug.c           |  4 +++
  kernel/sched/fair.c            |  5 +--
  kernel/sched/topology.c        | 58 ++++++++++++++++++++++++++++++++++
  5 files changed, 74 insertions(+), 3 deletions(-)
Quoted 2 lines

This seems reasonable. I'll queue it up, and unless someone (robot or real person) objects, we'll get it merged :-)

Thank you! I'll be ready with a fire extinguisher but hopefully I won't need it :)

Signature · 2 lines
Thanks and Regards,
Prateek