1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2024
6 #define KMSG_COMPONENT "hd"
7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 * Dynamically calculates the optimum number of high capacity COREs
12 * by considering the state the system is in. When hiperdispatch decides
13 * that a capacity update is necessary, it schedules a topology update.
14 * During topology updates the CPU capacities are always re-adjusted.
16 * There is two places where CPU capacities are being accessed within
18 * -> hiperdispatch's reoccuring work function reads CPU capacities to
19 * determine high capacity CPU count.
20 * -> during a topology update hiperdispatch's adjustment function
21 * updates CPU capacities.
22 * These two can run on different CPUs in parallel which can cause
23 * hiperdispatch to make wrong decisions. This can potentially cause
24 * some overhead by leading to extra rebuild_sched_domains() calls
25 * for correction. Access to capacities within hiperdispatch has to be
26 * serialized to prevent the overhead.
28 * Hiperdispatch decision making revolves around steal time.
29 * HD_STEAL_THRESHOLD value is taken as reference. Whenever steal time
30 * crosses the threshold value hiperdispatch falls back to giving high
31 * capacities to entitled CPUs. When steal time drops below the
32 * threshold boundary, hiperdispatch utilizes all CPUs by giving all
33 * of them high capacity.
35 * The theory behind HD_STEAL_THRESHOLD is related to the SMP thread
36 * performance. Comparing the throughput of;
37 * - single CORE, with N threads, running N tasks
38 * - N separate COREs running N tasks,
39 * using individual COREs for individual tasks yield better
40 * performance. This performance difference is roughly ~30% (can change
41 * between machine generations)
43 * Hiperdispatch tries to hint scheduler to use individual COREs for
44 * each task, as long as steal time on those COREs are less than 30%,
45 * therefore delaying the throughput loss caused by using SMP threads.
48 #include <linux/cpumask.h>
49 #include <linux/debugfs.h>
50 #include <linux/device.h>
51 #include <linux/kernel_stat.h>
52 #include <linux/kstrtox.h>
53 #include <linux/ktime.h>
54 #include <linux/sysctl.h>
55 #include <linux/types.h>
56 #include <linux/workqueue.h>
57 #include <asm/hiperdispatch.h>
58 #include <asm/setup.h>
60 #include <asm/topology.h>
62 #define CREATE_TRACE_POINTS
63 #include <asm/trace/hiperdispatch.h>
65 #define HD_DELAY_FACTOR (4)
66 #define HD_DELAY_INTERVAL (HZ / 4)
67 #define HD_STEAL_THRESHOLD 30
68 #define HD_STEAL_AVG_WEIGHT 16
70 static cpumask_t hd_vl_coremask; /* Mask containing all vertical low COREs */
71 static cpumask_t hd_vmvl_cpumask; /* Mask containing vertical medium and low CPUs */
72 static int hd_high_capacity_cores; /* Current CORE count with high capacity */
73 static int hd_entitled_cores; /* Total vertical high and medium CORE count */
74 static int hd_online_cores; /* Current online CORE count */
76 static unsigned long hd_previous_steal; /* Previous iteration's CPU steal timer total */
77 static unsigned long hd_high_time; /* Total time spent while all cpus have high capacity */
78 static unsigned long hd_low_time; /* Total time spent while vl cpus have low capacity */
79 static atomic64_t hd_adjustments; /* Total occurrence count of hiperdispatch adjustments */
81 static unsigned int hd_steal_threshold = HD_STEAL_THRESHOLD;
82 static unsigned int hd_delay_factor = HD_DELAY_FACTOR;
83 static int hd_enabled;
85 static void hd_capacity_work_fn(struct work_struct *work);
86 static DECLARE_DELAYED_WORK(hd_capacity_work, hd_capacity_work_fn);
88 static int hd_set_hiperdispatch_mode(int enable)
90 if (!MACHINE_HAS_TOPOLOGY)
92 if (hd_enabled == enable)
98 void hd_reset_state(void)
100 cpumask_clear(&hd_vl_coremask);
101 cpumask_clear(&hd_vmvl_cpumask);
102 hd_entitled_cores = 0;
106 void hd_add_core(int cpu)
108 const struct cpumask *siblings;
112 polarization = smp_cpu_get_polarization(cpu);
113 siblings = topology_sibling_cpumask(cpu);
114 switch (polarization) {
115 case POLARIZATION_VH:
118 case POLARIZATION_VM:
120 cpumask_or(&hd_vmvl_cpumask, &hd_vmvl_cpumask, siblings);
122 case POLARIZATION_VL:
123 cpumask_set_cpu(cpu, &hd_vl_coremask);
124 cpumask_or(&hd_vmvl_cpumask, &hd_vmvl_cpumask, siblings);
129 /* Serialize update and read operations of debug counters. */
130 static DEFINE_MUTEX(hd_counter_mutex);
132 static void hd_update_times(void)
138 * Check if hiperdispatch is active, if not set the prev to 0.
139 * This way it is possible to differentiate the first update iteration after
140 * enabling hiperdispatch.
142 if (hd_entitled_cores == 0 || hd_enabled == 0) {
143 prev = ktime_set(0, 0);
147 if (ktime_after(prev, 0)) {
148 if (hd_high_capacity_cores == hd_online_cores)
149 hd_high_time += ktime_ms_delta(now, prev);
151 hd_low_time += ktime_ms_delta(now, prev);
156 static void hd_update_capacities(void)
158 int cpu, upscaling_cores;
159 unsigned long capacity;
161 upscaling_cores = hd_high_capacity_cores - hd_entitled_cores;
162 capacity = upscaling_cores > 0 ? CPU_CAPACITY_HIGH : CPU_CAPACITY_LOW;
163 hd_high_capacity_cores = hd_entitled_cores;
164 for_each_cpu(cpu, &hd_vl_coremask) {
165 smp_set_core_capacity(cpu, capacity);
166 if (capacity != CPU_CAPACITY_HIGH)
168 hd_high_capacity_cores++;
170 if (upscaling_cores == 0)
171 capacity = CPU_CAPACITY_LOW;
175 void hd_disable_hiperdispatch(void)
177 cancel_delayed_work_sync(&hd_capacity_work);
178 hd_high_capacity_cores = hd_online_cores;
179 hd_previous_steal = 0;
182 int hd_enable_hiperdispatch(void)
184 mutex_lock(&hd_counter_mutex);
186 mutex_unlock(&hd_counter_mutex);
189 if (hd_entitled_cores == 0)
191 if (hd_online_cores <= hd_entitled_cores)
193 mod_delayed_work(system_wq, &hd_capacity_work, HD_DELAY_INTERVAL * hd_delay_factor);
194 hd_update_capacities();
198 static unsigned long hd_steal_avg(unsigned long new)
200 static unsigned long steal;
202 steal = (steal * (HD_STEAL_AVG_WEIGHT - 1) + new) / HD_STEAL_AVG_WEIGHT;
206 static unsigned long hd_calculate_steal_percentage(void)
208 unsigned long time_delta, steal_delta, steal, percentage;
216 for_each_cpu(cpu, &hd_vmvl_cpumask) {
217 steal += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
221 * If there is no vertical medium and low CPUs steal time
222 * is 0 as vertical high CPUs shouldn't experience steal time.
227 time_delta = ktime_to_ns(ktime_sub(now, prev));
228 if (steal > hd_previous_steal && hd_previous_steal != 0) {
229 steal_delta = (steal - hd_previous_steal) * 100 / time_delta;
230 percentage = steal_delta / cpus;
232 hd_previous_steal = steal;
237 static void hd_capacity_work_fn(struct work_struct *work)
239 unsigned long steal_percentage, new_cores;
241 mutex_lock(&smp_cpu_state_mutex);
243 * If online cores are less or equal to entitled cores hiperdispatch
244 * does not need to make any adjustments, call a topology update to
245 * disable hiperdispatch.
246 * Normally this check is handled on topology update, but during cpu
247 * unhotplug, topology and cpu mask updates are done in reverse
248 * order, causing hd_enable_hiperdispatch() to get stale data.
250 if (hd_online_cores <= hd_entitled_cores) {
251 topology_schedule_update();
252 mutex_unlock(&smp_cpu_state_mutex);
255 steal_percentage = hd_steal_avg(hd_calculate_steal_percentage());
256 if (steal_percentage < hd_steal_threshold)
257 new_cores = hd_online_cores;
259 new_cores = hd_entitled_cores;
260 if (hd_high_capacity_cores != new_cores) {
261 trace_s390_hd_rebuild_domains(hd_high_capacity_cores, new_cores);
262 hd_high_capacity_cores = new_cores;
263 atomic64_inc(&hd_adjustments);
264 topology_schedule_update();
266 trace_s390_hd_work_fn(steal_percentage, hd_entitled_cores, hd_high_capacity_cores);
267 mutex_unlock(&smp_cpu_state_mutex);
268 schedule_delayed_work(&hd_capacity_work, HD_DELAY_INTERVAL);
271 static int hiperdispatch_ctl_handler(const struct ctl_table *ctl, int write,
272 void *buffer, size_t *lenp, loff_t *ppos)
276 struct ctl_table ctl_entry = {
277 .procname = ctl->procname,
278 .data = &hiperdispatch,
279 .maxlen = sizeof(int),
280 .extra1 = SYSCTL_ZERO,
281 .extra2 = SYSCTL_ONE,
284 hiperdispatch = hd_enabled;
285 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
286 if (rc < 0 || !write)
288 mutex_lock(&smp_cpu_state_mutex);
289 if (hd_set_hiperdispatch_mode(hiperdispatch))
290 topology_schedule_update();
291 mutex_unlock(&smp_cpu_state_mutex);
295 static struct ctl_table hiperdispatch_ctl_table[] = {
297 .procname = "hiperdispatch",
299 .proc_handler = hiperdispatch_ctl_handler,
303 static ssize_t hd_steal_threshold_show(struct device *dev,
304 struct device_attribute *attr,
307 return sysfs_emit(buf, "%u\n", hd_steal_threshold);
310 static ssize_t hd_steal_threshold_store(struct device *dev,
311 struct device_attribute *attr,
318 rc = kstrtouint(buf, 0, &val);
323 hd_steal_threshold = val;
327 static DEVICE_ATTR_RW(hd_steal_threshold);
329 static ssize_t hd_delay_factor_show(struct device *dev,
330 struct device_attribute *attr,
333 return sysfs_emit(buf, "%u\n", hd_delay_factor);
336 static ssize_t hd_delay_factor_store(struct device *dev,
337 struct device_attribute *attr,
344 rc = kstrtouint(buf, 0, &val);
349 hd_delay_factor = val;
353 static DEVICE_ATTR_RW(hd_delay_factor);
355 static struct attribute *hd_attrs[] = {
356 &dev_attr_hd_steal_threshold.attr,
357 &dev_attr_hd_delay_factor.attr,
361 static const struct attribute_group hd_attr_group = {
362 .name = "hiperdispatch",
366 static int hd_greedy_time_get(void *unused, u64 *val)
368 mutex_lock(&hd_counter_mutex);
371 mutex_unlock(&hd_counter_mutex);
375 DEFINE_SIMPLE_ATTRIBUTE(hd_greedy_time_fops, hd_greedy_time_get, NULL, "%llu\n");
377 static int hd_conservative_time_get(void *unused, u64 *val)
379 mutex_lock(&hd_counter_mutex);
382 mutex_unlock(&hd_counter_mutex);
386 DEFINE_SIMPLE_ATTRIBUTE(hd_conservative_time_fops, hd_conservative_time_get, NULL, "%llu\n");
388 static int hd_adjustment_count_get(void *unused, u64 *val)
390 *val = atomic64_read(&hd_adjustments);
394 DEFINE_SIMPLE_ATTRIBUTE(hd_adjustments_fops, hd_adjustment_count_get, NULL, "%llu\n");
396 static void __init hd_create_debugfs_counters(void)
400 dir = debugfs_create_dir("hiperdispatch", arch_debugfs_dir);
401 debugfs_create_file("conservative_time_ms", 0400, dir, NULL, &hd_conservative_time_fops);
402 debugfs_create_file("greedy_time_ms", 0400, dir, NULL, &hd_greedy_time_fops);
403 debugfs_create_file("adjustment_count", 0400, dir, NULL, &hd_adjustments_fops);
406 static void __init hd_create_attributes(void)
410 dev = bus_get_dev_root(&cpu_subsys);
413 if (sysfs_create_group(&dev->kobj, &hd_attr_group))
414 pr_warn("Unable to create hiperdispatch attribute group\n");
418 static int __init hd_init(void)
420 if (IS_ENABLED(CONFIG_HIPERDISPATCH_ON)) {
421 hd_set_hiperdispatch_mode(1);
422 topology_schedule_update();
424 if (!register_sysctl("s390", hiperdispatch_ctl_table))
425 pr_warn("Failed to register s390.hiperdispatch sysctl attribute\n");
426 hd_create_debugfs_counters();
427 hd_create_attributes();
430 late_initcall(hd_init);