Cache aware scheduling enhancements
Subsystems (2)
v4 cover Cache aware scheduling enhancements
This patch set contains cache-aware scheduling enhancements and bug fixes on top of Peter's sched/cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/cache
Patches 1 to 6 resolve the over-aggregation issue, which is the remaining part of v4 that has not yet been merged into sched/cache. Patches 7 to 15 fix bugs reported by Sashiko (online and local).
Compared with cache-aware v4, the major change in the first part is storing the LLC effective size in the per-CPU bottom sched_domain. This allows checking whether a task's memory footprint exceeds the threshold by fetching the value directly from the corresponding sched_domain, instead of recalculating it every time. Besides, the NUMA balance page-fault statistics is used instead of RSS to estimate the working set. We also picked up Jianyong's optimization patch to reduce CPU scan overhead. However, if NUMA balancing is not enabled we will not have this working set estimate. Perhaps using RSS will be apprpriate for such scenario.
Gengkun's CPU scan optimization is not
included for now and will be revisited after further tuning.
Most patches in the second part address race conditions. Each patch fixes one independent issue to facilitate easier review.
Test results show that the current version keeps the same performance as v4 for workloads and platforms we tested.
Future plans are to introduce fine-grained control of using cache aware scheduling on specific tasks after the load-balance-based cache-aware scheduling is merged:
- Look into task tagging (e.g. with schedqos framework, cgroup) for non process based tasks grouping to LLC.
- Evaluate fast cache-aware aggregation in the wakeup path.
I will be on sabbatical from mid May to mid June. Chen Yu will still be following up these patches.
Thanks.
Tim
Chen Yu (15):
sched/cache: Disable cache aware scheduling for processes with high
thread counts
sched/cache: Skip cache-aware scheduling for single-threaded processes sched/cache: Calculate the LLC size and store it in sched_domain sched/cache: Avoid cache-aware scheduling for memory-heavy processes sched/cache: Add user control to adjust the aggressiveness of
cache-aware scheduling
sched/cache: Fix rcu warning when accessing sd_llc domain sched/cache: Fix potential NULL mm pointer access sched/cache: Annotate lockless accesses to mm->sc_stat.cpu sched/cache: Fix unpaired account_llc_enqueue/dequeue sched/cache: Fix checking active load balance by only considering the
CFS task
sched/cache: Fix race condition during sched domain rebuild sched/cache: Fix cache aware scheduling enabling for multi LLCs system sched/cache: Fix has_multi_llcs iff at least one partition has
multiple LLCs
sched/cache: Fix possible overflow when invalidating the preferred CPU sched/cache: Fix stale preferred_llc for a new task
Jianyong Wu (1):
sched/cache: Allow only 1 thread of the process to calculate the LLC
occupancy
drivers/base/cacheinfo.c | 23 +++ include/linux/cacheinfo.h | 1 + include/linux/sched.h | 5 + include/linux/sched/topology.h | 7 + init/init_task.c | 1 + kernel/exit.c | 29 ++++ kernel/sched/debug.c | 14 +- kernel/sched/fair.c | 256 +++++++++++++++++++++++++++++---- kernel/sched/sched.h | 7 +- kernel/sched/topology.c | 240 +++++++++++++++++++++++++------ 10 files changed, 509 insertions(+), 74 deletions(-)
Signature · 1 line
2.32.0
PATCH v4 1/16 sched/cache: Allow only 1 thread of the process to calculate the LLC occupancy
From: Jianyong Wu <[email protected]>
Scanning online CPUs to calculate the occupancy might be time-consuming. Only allow 1 thread of the process to scan the CPUs at the same time, which is similar to what NUMA balance does in task_numa_work().
--- include/linux/sched.h | 1 + kernel/sched/fair.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
include/linux/sched.h +1 -0
@@ -2423,6 +2423,7 @@ struct sched_cache_stat {struct sched_cache_time __percpu *pcpu_sched;raw_spinlock_t lock;unsigned long epoch; +unsigned long next_scan;int cpu; }____cacheline_aligned_in_smp ;
kernel/sched/fair.c +11 -0
@@ -1451,6 +1451,7 @@ void mm_init_sched(struct mm_struct *mm,raw_spin_lock_init (&mm->sc_stat.lock); mm->sc_stat.epoch = epoch; mm->sc_stat.cpu =-1 ; + mm->sc_stat.next_scan = jiffies;/* * The update to mm->sc_stat should not be reordered
@@ -1661,6 +1662,7 @@ static void get_scan_cpumasks(cpumask_var_t cpus, struct task_struct *p)static void task_cache_work (struct callback_head *work) { +unsigned long next_scan, now = jiffies;struct task_struct *p = current;struct mm_struct *mm = p->mm;unsigned long m_a_occ =0 ;
@@ -1675,6 +1677,15 @@ static void task_cache_work(struct callback_head *work)if (p->flags &PF_EXITING )return ; + next_scan =READ_ONCE (mm->sc_stat.next_scan); +if (time_before (now, next_scan)) +return ; + +/* only 1 thread is allowed to scan */ +if (!try_cmpxchg (&mm->sc_stat.next_scan, &next_scan, + now +EPOCH_PERIOD )) +return ; +if (!zalloc_cpumask_var (&cpus,GFP_KERNEL ))return ;
Signature · 1 line
2.32.0
PATCH v4 2/16 sched/cache: Disable cache aware scheduling for processes with high thread counts
In reply to Tim ChenFrom: Chen Yu <[email protected]>
A performance regression was observed by Prateek when running hackbench with many threads per process (high fd count). To avoid this, processes with a large number of active threads are excluded from cache-aware scheduling.
With sched_cache enabled, record the number of active threads in each process during the periodic task_cache_work(). While iterating over CPUs, if the currently running task belongs to the same process as the task that launched task_cache_work(), increment the active thread count.
If the number of active threads within the process exceeds the number of Cores (divided by the SMT number) in the LLC, do not enable cache-aware scheduling. However, on systems with a smaller number of CPUs within 1 LLC, like Power10/Power11 with SMT4 and an LLC size of 4, this check effectively disables cache-aware scheduling for any process. One possible solution suggested by Peter is to use an LLC-mask instead of a single LLC value for preference. Once there are a 'few' LLCs as preference, this constraint becomes a little easier. It could be an enhancement in the future.
For users who wish to perform task aggregation regardless, a debugfs knob is provided for tuning in a subsequent change.
--- include/linux/sched.h | 1 + kernel/sched/fair.c | 48 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-)
include/linux/sched.h +1 -0
@@ -2423,6 +2423,7 @@ struct sched_cache_stat {struct sched_cache_time __percpu *pcpu_sched;raw_spinlock_t lock;unsigned long epoch; +u64 nr_running_avg;unsigned long next_scan;int cpu; }____cacheline_aligned_in_smp ;
kernel/sched/fair.c +43 -5
95 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH v4 3/16 sched/cache: Skip cache-aware scheduling for single-threaded processes
In reply to Tim ChenFrom: Chen Yu <[email protected]>
For a single thread, the current wakeup path tends to place it on the same LLC where it was previously running with cache-hot data. There is no need to enable cache-aware scheduling for single-threaded processes for the following reasons:
1. Cache-aware scheduling primarily benefits multi-threaded processes where threads share data. Single-threaded processes typically have no inter-thread data sharing and thus gain little.
2. Enabling it incurs the additional overhead of tracking the thread's residency in the LLCs.
3. Bypassing single-threaded processes avoids excessive concentration of such tasks on a single LLC.
Nevertheless, this check can be omitted if users explicitly provide hints for such single-threaded workloads where different processes have shared memory, e.g., via prctl() or other interfaces to be added in the future.
--- kernel/sched/fair.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/sched/fair.c +16 -4
@@ -1384,8 +1384,12 @@ static int llc_id(int cpu)return per_cpu (sd_llc_id, cpu); } -static bool invalid_llc_nr (struct mm_struct *mm,int cpu) +static bool invalid_llc_nr (struct mm_struct *mm,struct task_struct *p, +int cpu) { +if (get_nr_threads (p) <=1 ) +return true; +return !fits_capacity ((mm->sc_stat.nr_running_avg * cpu_smt_num_threads),per_cpu (sd_llc_size, cpu)); }
@@ -1581,7 +1585,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)* its preferred state. */ if (epoch -READ_ONCE (mm->sc_stat.epoch) >EPOCH_LLC_AFFINITY_TIMEOUT || -invalid_llc_nr (mm,cpu_of (rq))) { +invalid_llc_nr (mm, p,cpu_of (rq))) {if (mm->sc_stat.cpu !=-1 ) mm->sc_stat.cpu =-1 ; }
@@ -1687,9 +1691,9 @@ static inline void update_avg_scale(u64 *avg, u64 sample)static void task_cache_work (struct callback_head *work) { +int cpu, m_a_cpu =-1 , nr_running =0 , curr_cpu;unsigned long next_scan, now = jiffies;struct task_struct *p = current, *cur; -int cpu, m_a_cpu =-1 , nr_running =0 ;unsigned long curr_m_a_occ =0 ;struct mm_struct *mm = p->mm;unsigned long m_a_occ =0 ;
@@ -1711,6 +1715,14 @@ static void task_cache_work(struct callback_head *work) now + EPOCH_PERIOD))return ; + curr_cpu =task_cpu (p); +if (invalid_llc_nr (mm, p, curr_cpu)) { +if (mm->sc_stat.cpu !=-1 ) + mm->sc_stat.cpu =-1 ; + +return ; + } +if (!zalloc_cpumask_var (&cpus,GFP_KERNEL ))return ;
@@ -10326,7 +10338,7 @@ static enum llc_mig can_migrate_llc_task(int src_cpu, int dst_cpu,return mig_unrestricted;/* skip cache aware load balance for too many threads */ -if (invalid_llc_nr (mm, dst_cpu)) { +if (invalid_llc_nr (mm, p, dst_cpu)) {if (mm->sc_stat.cpu !=-1 ) mm->sc_stat.cpu =-1 ;return mig_unrestricted;
Signature · 1 line
2.32.0
PATCH v4 4/16 sched/cache: Calculate the LLC size and store it in sched_domain
In reply to Tim ChenFrom: Chen Yu <[email protected]>
Cache aware scheduling needs to know the LLC size that a process can use, so as to avoid memory-intensive tasks from being over-aggregated on a single LLC.
Introduce a preparation patch to add get_effective_llc_bytes() to get the LLC size that a CPU can use. The function can be further enhanced by subtracting the LLC cache ways reserved by resctrl (CAT in Intel RDT, etc).
--- drivers/base/cacheinfo.c | 23 ++++++++ include/linux/cacheinfo.h | 1 + include/linux/sched/topology.h | 7 +++ kernel/sched/topology.c | 98 ++++++++++++++++++++++++++++++++-- 4 files changed, 126 insertions(+), 3 deletions(-)
drivers/base/cacheinfo.c +23 -0
@@ -17,6 +17,7 @@#include <linux/init.h> #include <linux/of.h> #include <linux/sched.h> +#include <linux/sched/topology.h> #include <linux/slab.h> #include <linux/smp.h> #include <linux/sysfs.h>
@@ -68,6 +69,24 @@ bool last_level_cache_is_valid(unsigned int cpu) } +/* +* Get the cacheinfo of the LLC associated with @cpu. +* Derived from update_per_cpu_data_slice_size_cpu(). +*/ +struct cacheinfo *get_cpu_cacheinfo_llc (unsigned int cpu) +{ +struct cacheinfo *llc; + +if (!last_level_cache_is_valid (cpu)) +return NULL ; + + llc =per_cpu_cacheinfo_idx (cpu,cache_leaves (cpu) -1 ); +if (llc->type !=CACHE_TYPE_DATA && llc->type !=CACHE_TYPE_UNIFIED ) +return NULL ; + +return llc; +} +bool last_level_cache_is_shared (unsigned int cpu_x,unsigned int cpu_y) {struct cacheinfo *llc_x, *llc_y;
@@ -1018,6 +1037,7 @@ static int cacheinfo_cpu_online(unsigned int cpu) goto err;if (cpu_map_shared_cache (true, cpu, &cpu_map))update_per_cpu_data_slice_size (true, cpu, cpu_map); +sched_update_llc_bytes (cpu);return 0 ; err:free_cache_attributes (cpu);
@@ -1036,6 +1056,9 @@ static int cacheinfo_cpu_pre_down(unsigned int cpu)free_cache_attributes (cpu);if (nr_shared >1 )update_per_cpu_data_slice_size (false, cpu, cpu_map); + +sched_update_llc_bytes (cpu); +return 0 ; }
include/linux/cacheinfo.h +1 -0
@@ -89,6 +89,7 @@ int populate_cache_leaves(unsigned int cpu);int cache_setup_acpi (unsigned int cpu);bool last_level_cache_is_valid (unsigned int cpu);bool last_level_cache_is_shared (unsigned int cpu_x,unsigned int cpu_y); +struct cacheinfo *get_cpu_cacheinfo_llc (unsigned int cpu);int fetch_cache_info (unsigned int cpu);int detect_cache_attributes (unsigned int cpu);#ifndef CONFIG_ACPI_PPTT
include/linux/sched/topology.h +7 -0
@@ -106,6 +106,7 @@ struct sched_domain {#ifdef CONFIG_SCHED_CACHE unsigned int llc_max; unsigned int *llc_counts __counted_by_ptr(llc_max); +unsigned long llc_bytes;#endif #ifdef CONFIG_SCHEDSTATS
@@ -265,4 +266,10 @@ static inline int task_node(const struct task_struct *p) return cpu_to_node(task_cpu(p)); } +#ifdef CONFIG_SCHED_CACHE +extern void sched_update_llc_bytes (unsigned int cpu); +#else +static inline void sched_update_llc_bytes (unsigned int cpu) { } +#endif +#endif /* _LINUX_SCHED_TOPOLOGY_H */
kernel/sched/topology.c +95 -3
134 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH v4 5/16 sched/cache: Avoid cache-aware scheduling for memory-heavy processes
In reply to Tim ChenFrom: Chen Yu <[email protected]>
Prateek and Tingyin reported that memory-intensive workloads (such as stream) can saturate memory bandwidth and caches on the preferred LLC when sched_cache aggregates too many threads.
To mitigate this, estimate a process's memory footprint by comparing its NUMA balancing fault statistics to the size of the LLC. If the footprint exceeds the LLC size, skip cache-aware scheduling.
Note that footprint is only an approximation of the memory footprint, since the kernel lacks suitable metrics to estimate the real working set. If a user-provided hint is available in the future, it would be more accurate. A later patch will allow users to provide a hint to adjust this threshold.
--- include/linux/sched.h | 1 + kernel/exit.c | 29 ++++++++++++++++++++ kernel/sched/fair.c | 62 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 89 insertions(+), 3 deletions(-)
include/linux/sched.h +1 -0
@@ -2425,6 +2425,7 @@ struct sched_cache_stat {unsigned long epoch;u64 nr_running_avg;unsigned long next_scan; +unsigned long footprint;int cpu; }____cacheline_aligned_in_smp ;
kernel/exit.c +29 -0
@@ -543,6 +543,32 @@ void mm_update_next_owner(struct mm_struct *mm) }#endif /* CONFIG_MEMCG */ +#if defined(CONFIG_SCHED_CACHE ) && defined(CONFIG_NUMA_BALANCING ) +/* +* Subtract the memory footprint of the current task from +* mm. +*/ +static void exit_mm_sched_cache (struct mm_struct *mm) +{ +unsigned long fp, sub; + +if (!current->total_numa_faults) +return ; +/* +* No lock protection due to performance considerations. +* Make sure mm->sc_stat.footprint does not become +* negative. +*/ + fp =READ_ONCE (mm->sc_stat.footprint); + sub =min (fp, current->total_numa_faults); +WRITE_ONCE (mm->sc_stat.footprint, fp - sub); +} +#else +static inline void exit_mm_sched_cache (struct mm_struct *mm) +{ +} +#endif /* CONFIG_SCHED_CACHE CONFIG_NUMA_BALANCING */ +/* * Turn us into a lazy TLB process if we * aren't already..
@@ -554,6 +580,9 @@ static void exit_mm(void)exit_mm_release (current, mm);if (!mm)return ; + +exit_mm_sched_cache (mm); +mmap_read_lock (mm);mmgrab_lazy_tlb (mm);BUG_ON (mm != current->active_mm);
kernel/sched/fair.c +59 -3
104 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH v4 6/16 sched/cache: Add user control to adjust the aggressiveness of cache-aware scheduling
In reply to Tim ChenFrom: Chen Yu <[email protected]>
Introduce a set of debugfs knobs to control how aggressively the cache aware scheduling does the task aggregation.
(1) aggr_tolerance
With sched_cache enabled, the scheduler uses a process's footprint as a proxy for its LLC footprint to determine if aggregating tasks on the preferred LLC could cause cache contention. If the footprint exceeds the LLC size, aggregation is skipped. Since the kernel cannot efficiently track per-task cache usage (resctrl is user-space only), userspace can provide a more accurate hint.
Introduce /sys/kernel/debug/sched/llc_balancing/aggr_tolerance to let users control how strictly footprint limits aggregation. Values range from 0 to 100:
- 0: Cache-aware scheduling is disabled.
- 1: Strict; tasks with footprint larger than LLC size are skipped.
- >=100: Aggressive; tasks are aggregated regardless of footprint.
For example, with a 32MB L3 cache:
- aggr_tolerance=1 -> tasks with footprint > 32MB are skipped.
- aggr_tolerance=99 -> tasks with footprint > 784GB are skipped
(784GB = (1 + (99 - 1) * 256) * 32MB).
Similarly, /sys/kernel/debug/sched/llc_balancing/aggr_tolerance also controls how strictly the number of active threads is considered when doing cache aware load balance. The number of SMTs is also considered. High SMT counts reduce the aggregation capacity, preventing excessive task aggregation on SMT-heavy systems like Power10/Power11.
Yangyu suggested introducing separate aggregation controls for the number of active threads and memory footprint checks. Since there are plans to add per-process/task group controls, fine-grained tunables are deferred to that implementation.
(2) epoch_period, epoch_affinity_timeout, imb_pct, overaggr_pct are also turned into tunables.
--- kernel/sched/debug.c | 10 +++++++ kernel/sched/fair.c | 68 ++++++++++++++++++++++++++++++++++++++------ kernel/sched/sched.h | 5 ++++ 3 files changed, 75 insertions(+), 8 deletions(-)
kernel/sched/debug.c +10 -0
@@ -670,6 +670,16 @@ static __init int sched_init_debug(void) llc =debugfs_create_dir ("llc_balancing" , debugfs_sched);debugfs_create_file ("enabled" ,0644 , llc,NULL , &sched_cache_enable_fops); +debugfs_create_u32 ("aggr_tolerance" ,0644 , llc, + &llc_aggr_tolerance); +debugfs_create_u32 ("epoch_period" ,0644 , llc, + &llc_epoch_period); +debugfs_create_u32 ("epoch_affinity_timeout" ,0644 , llc, + &llc_epoch_affinity_timeout); +debugfs_create_u32 ("overaggr_pct" ,0644 , llc, + &llc_overaggr_pct); +debugfs_create_u32 ("imb_pct" ,0644 , llc, + &llc_imb_pct);#endif debugfs_create_file ("debug" ,0444 , debugfs_sched,NULL , &sched_debug_fops);
kernel/sched/fair.c +60 -8
136 lines not on this page · raw message
kernel/sched/sched.h +5 -0
@@ -4072,6 +4072,11 @@ static inline void mm_cid_switch_to(struct task_struct *prev, struct task_structDECLARE_STATIC_KEY_FALSE (sched_cache_present);DECLARE_STATIC_KEY_FALSE (sched_cache_active);extern int sysctl_sched_cache_user; +extern unsigned int llc_aggr_tolerance; +extern unsigned int llc_epoch_period; +extern unsigned int llc_epoch_affinity_timeout; +extern unsigned int llc_imb_pct; +extern unsigned int llc_overaggr_pct;static inline bool sched_cache_enabled (void ) {
Signature · 1 line
2.32.0
PATCH v4 7/16 sched/cache: Fix rcu warning when accessing sd_llc domain
In reply to Tim ChenFrom: Chen Yu <[email protected]>
rcu_dereference_all() should be used to access the sd_llc domain under RCU protection.
This bug was reported by sashiko.
--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
@@ -1814,7 +1814,7 @@ static void task_cache_work(struct callback_head *work)for_each_cpu (cpu, cpus) {/* XXX sched_cluster_active */ -struct sched_domain *sd =per_cpu (sd_llc, cpu); +struct sched_domain *sd =rcu_dereference_all (per_cpu (sd_llc, cpu));unsigned long occ, m_occ =0 , a_occ =0 ;int m_cpu =-1 , i;
Signature · 1 line
2.32.0
PATCH v4 8/16 sched/cache: Fix potential NULL mm pointer access
In reply to Tim ChenFrom: Chen Yu <[email protected]>
A concurrent task exit might cause a NULL pointer dereference in account_mm_sched(). Use the locally cached mm pointer instead, since the active_mm reference guarantees the structure remains allocated. Meanwhile, skip the kernel thread because it has nothing to do with cache aware scheduling.
This bug was reported by sashiko and Vern.
--- kernel/sched/fair.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
@@ -1649,7 +1649,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)if (!mm || !mm->sc_stat.pcpu_sched)return ; - pcpu_sched =per_cpu_ptr (p->mm->sc_stat.pcpu_sched,cpu_of (rq)); + pcpu_sched =per_cpu_ptr (mm->sc_stat.pcpu_sched,cpu_of (rq)); scoped_guard (raw_spinlock, &rq->cpu_epoch_lock) {__update_mm_sched (rq, pcpu_sched);
@@ -1689,7 +1689,8 @@ static void task_tick_cache(struct rq *rq, struct task_struct *p)if (!sched_cache_enabled ())return ; -if (!mm || !mm->sc_stat.pcpu_sched) +if (!mm || p->flags &PF_KTHREAD || + !mm->sc_stat.pcpu_sched)return ; epoch = rq->cpu_epoch;
Signature · 1 line
2.32.0
PATCH v4 9/16 sched/cache: Annotate lockless accesses to mm->sc_stat.cpu
In reply to Tim ChenFrom: Chen Yu <[email protected]>
mm->sc_stat.cpu is written by task_cache_work() and could be read locklessly by several functions on other CPUs. Use READ_ONCE and WRITE_ONCE on mm->sc_stat.cpu access and write to prevent inconsistent values from compiler optimizations when there are multiple accesses.
For example in get_pref_llc(), if the writer updated the field between two compiler-generated loads, the validation (e.g., cpu != -1) and subsequent use (e.g., llc_id(cpu)) could operate on different values, allowing a negative CPU ID to be used as an index.
Leave plain write in mm_init_sched(), where the mm is not yet visible to other CPUs.
This bug was reported by sashiko.
--- kernel/sched/fair.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-)
kernel/sched/fair.c +15 -14
@@ -1598,13 +1598,14 @@ static unsigned long fraction_mm_sched(struct rq *rq,static int get_pref_llc (struct task_struct *p,struct mm_struct *mm) { -int mm_sched_llc =-1 ; +int mm_sched_llc =-1 , mm_sched_cpu;if (!mm)return -1 ; -if (mm->sc_stat.cpu !=-1 ) { - mm_sched_llc =llc_id (mm->sc_stat.cpu); + mm_sched_cpu =READ_ONCE (mm->sc_stat.cpu); +if (mm_sched_cpu !=-1 ) { + mm_sched_llc =llc_id (mm_sched_cpu);#ifdef CONFIG_NUMA_BALANCING /*
@@ -1619,7 +1620,7 @@ static int get_pref_llc(struct task_struct *p, struct mm_struct *mm)*/ if (static_branch_likely (&sched_numa_balancing) && p->numa_preferred_nid >=0 && -cpu_to_node (mm->sc_stat.cpu) != p->numa_preferred_nid) +cpu_to_node (mm_sched_cpu) != p->numa_preferred_nid) mm_sched_llc =-1 ;#endif }
@@ -1665,8 +1666,8 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)if (epoch -READ_ONCE (mm->sc_stat.epoch) > llc_epoch_affinity_timeout ||invalid_llc_nr (mm, p,cpu_of (rq)) ||exceed_llc_capacity (mm,cpu_of (rq))) { -if (mm->sc_stat.cpu !=-1 ) - mm->sc_stat.cpu =-1 ; +if (READ_ONCE (mm->sc_stat.cpu) !=-1 ) +WRITE_ONCE (mm->sc_stat.cpu,-1 ); } mm_sched_llc =get_pref_llc (p, mm);
@@ -1714,7 +1715,7 @@ static void get_scan_cpumasks(cpumask_var_t cpus, struct task_struct *p)if (!static_branch_likely (&sched_numa_balancing)) goto out; - cpu = p->mm->sc_stat.cpu; + cpu =READ_ONCE (p->mm->sc_stat.cpu);if (cpu !=-1 ) nid =cpu_to_node (cpu); curr_cpu =task_cpu (p);
@@ -1799,8 +1800,8 @@ static void task_cache_work(struct callback_head *work) curr_cpu =task_cpu (p);if (invalid_llc_nr (mm, p, curr_cpu) ||exceed_llc_capacity (mm, curr_cpu)) { -if (mm->sc_stat.cpu !=-1 ) - mm->sc_stat.cpu =-1 ; +if (READ_ONCE (mm->sc_stat.cpu) !=-1 ) +WRITE_ONCE (mm->sc_stat.cpu,-1 );return ; }
@@ -1857,7 +1858,7 @@ static void task_cache_work(struct callback_head *work) m_a_cpu = m_cpu; } -if (llc_id (cpu) ==llc_id (mm->sc_stat.cpu)) +if (llc_id (cpu) ==llc_id (READ_ONCE (mm->sc_stat.cpu))) curr_m_a_occ = a_occ;cpumask_andnot (cpus, cpus,sched_domain_span (sd));
@@ -1875,7 +1876,7 @@ static void task_cache_work(struct callback_head *work)* 3. 2X is chosen based on test results, as it delivers * the optimal performance gain so far. */ - mm->sc_stat.cpu = m_a_cpu; +WRITE_ONCE (mm->sc_stat.cpu, m_a_cpu); }update_avg_scale (&mm->sc_stat.nr_running_avg, nr_running);
@@ -10441,15 +10442,15 @@ static enum llc_mig can_migrate_llc_task(int src_cpu, int dst_cpu,if (!mm)return mig_unrestricted; - cpu = mm->sc_stat.cpu; + cpu =READ_ONCE (mm->sc_stat.cpu);if (cpu <0 ||cpus_share_cache (src_cpu, dst_cpu))return mig_unrestricted;/* skip cache aware load balance for too many threads */ if (invalid_llc_nr (mm, p, dst_cpu) ||exceed_llc_capacity (mm, dst_cpu)) { -if (mm->sc_stat.cpu !=-1 ) - mm->sc_stat.cpu =-1 ; +if (READ_ONCE (mm->sc_stat.cpu) !=-1 ) +WRITE_ONCE (mm->sc_stat.cpu,-1 );return mig_unrestricted; }
Signature · 1 line
2.32.0
PATCH v4 10/16 sched/cache: Fix unpaired account_llc_enqueue/dequeue
In reply to Tim ChenFrom: Chen Yu <[email protected]>
There is a race condition that, after a task is enqueued on a runqueue, task_llc(p) may change due to CPU hotplug, because the llc_id is dynamically allocated and adjusted at runtime.
Therefore, checking task_llc(p) to determine whether the task is being dequeued from its preferred LLC is unreliable and can cause inconsistent values.
To fix this problem, record whether p is enqueued on its preferred LLC, in order to pair with account_llc_dequeue() to maintain a consistent nr_pref_llc_running per runqueue.
This bug was reported by sashiko, and the solution was once suggested by Prateek.
--- include/linux/sched.h | 2 ++ init/init_task.c | 1 + kernel/sched/fair.c | 31 ++++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-)
include/linux/sched.h +2 -0
@@ -1410,6 +1410,8 @@ struct task_struct {#ifdef CONFIG_SCHED_CACHE struct callback_head cache_work;int preferred_llc; +/* 1: task was enqueued to its preferred LLC, 0 otherwise */ +int pref_llc_queued;#endif struct rseq_data rseq;
init/init_task.c +1 -0
@@ -217,6 +217,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {#endif #ifdef CONFIG_SCHED_CACHE .preferred_llc = -1, + .pref_llc_queued = 0,#endif #if defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS ) .kasan_depth = 1,
kernel/sched/fair.c +28 -3
@@ -1472,15 +1472,32 @@ static bool invalid_llc_nr(struct mm_struct *mm, struct task_struct *p,static void account_llc_enqueue (struct rq *rq,struct task_struct *p) { +int pref_llc, pref_llc_queued;struct sched_domain *sd; -int pref_llc; pref_llc = p->preferred_llc;if (pref_llc <0 )return ; + pref_llc_queued = (pref_llc ==task_llc (p)); rq->nr_llc_running++; - rq->nr_pref_llc_running += (pref_llc ==task_llc (p)); + rq->nr_pref_llc_running += pref_llc_queued; + +/* +* Record whether p is enqueued on its preferred +* LLC, in order to pair with account_llc_dequeue() +* to maintain a consistent nr_pref_llc_running per +* runqueue. +* This is necessary because a race condition exists: +* after a task is enqueued on a runqueue, task_llc(p) +* may change due to CPU hotplug. Therefore, checking +* task_llc(p) to determine whether the task is being +* dequeued from its preferred LLC is unreliable and +* can cause inconsistent values - checking the +* p->pref_llc_queued in account_llc_dequeue() would +* be reliable. +*/ + p->pref_llc_queued = pref_llc_queued; sd =rcu_dereference_all (rq->sd);if (sd && (unsigned int )pref_llc < sd->llc_max)
@@ -1497,7 +1514,15 @@ static void account_llc_dequeue(struct rq *rq, struct task_struct *p)return ; rq->nr_llc_running--; - rq->nr_pref_llc_running -= (pref_llc ==task_llc (p)); +if (p->pref_llc_queued) { + rq->nr_pref_llc_running--; +/* +* Update the status in case +* other logic might query +* this. +*/ + p->pref_llc_queued =0 ; + } sd =rcu_dereference_all (rq->sd);if (sd && (unsigned int )pref_llc < sd->llc_max) {
Signature · 1 line
2.32.0
PATCH v4 11/16 sched/cache: Fix checking active load balance by only considering the CFS task
In reply to Tim ChenFrom: Chen Yu <[email protected]>
The currently running task cur may not be a CFS task, such as an RT or Deadline task. For non-CFS tasks, the task_util(cur) utilization average is not maintained, so this might pass a stale or meaningless value to can_migrate_llc().
Check if the task is CFS before getting its task_util().
This bug was reported by sashiko.
--- kernel/sched/fair.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
@@ -10509,7 +10509,8 @@ alb_break_llc(struct lb_env *env)/* * All tasks prefer to stay on their current CPU. * Do not pull a task from its preferred CPU if: -* 1. It is the only task running there(not too imbalance); OR +* 1. It is the only task running and does not exceed +* imbalance allowance; OR * 2. Migrating it away from its preferred LLC would violate * the cache-aware scheduling policy. */
@@ -10522,7 +10523,7 @@ alb_break_llc(struct lb_env *env)return true; cur =rcu_dereference_all (env->src_rq->curr); -if (cur) +if (cur && cur->sched_class == &fair_sched_class) util =task_util (cur); if (can_migrate_llc(env->src_cpu, env->dst_cpu,
Signature · 1 line
2.32.0
PATCH v4 12/16 sched/cache: Fix race condition during sched domain rebuild
In reply to Tim ChenFrom: Chen Yu <[email protected]>
sched_cache_active_set_unlocked() checks hardware support without locks:
static void sched_cache_active_set(bool locked)
{
/* hardware does not support */
if (!static_branch_likely(&sched_cache_present)) {
_sched_cache_active_set(false, locked);
return;
}
...If build_sched_domains() runs concurrently during CPU hotplug, it can disable sched_cache_present under sched_domains_mutex and the CPU hotplug lock. If a debugfs write thread evaluates sched_cache_present as true right before that, and then blocks or gets preempted, it might proceed to enable sched_cache_active after the hardware support has been marked as absent. Make it safer by acquiring cpus_read_lock() and sched_domains_mutex_lock() when the user changes sched_cache_active via debugfs.
This bug was reported by sashiko.
--- kernel/sched/debug.c | 4 +++- kernel/sched/sched.h | 2 +- kernel/sched/topology.c | 42 +++++++++++++++-------------------------- 3 files changed, 19 insertions(+), 29 deletions(-)
kernel/sched/debug.c +3 -1
@@ -224,7 +224,9 @@ sched_cache_enable_write(struct file *filp, const char __user *ubuf, sysctl_sched_cache_user = val; -sched_cache_active_set_unlocked (); +sched_cache_active_set (); + + *ppos += cnt;return cnt; }
kernel/sched/sched.h +1 -1
@@ -4083,7 +4083,7 @@ static inline bool sched_cache_enabled(void) return static_branch_unlikely(&sched_cache_active); } -extern void sched_cache_active_set_unlocked (void ); +extern void sched_cache_active_set (void );#endif
kernel/sched/topology.c +15 -27
76 lines not on this page · raw message
Signature · 1 line
2.32.0
PATCH v4 13/16 sched/cache: Fix cache aware scheduling enabling for multi LLCs system
In reply to Tim ChenFrom: Chen Yu <[email protected]>
If there are multiple LLCs in the system, cache aware scheduling should be enabled. However, there is a corner case where, if there is a single NUMA node and a single LLC per node, cache aware scheduling will be turned on in the current implementation - because at this moment, the parent domain has not yet been degenerated, and it is possible that the current domain has the same cpu span as its parent. There is no need to turn cache aware scheduling on in this scenario.
Fix it by iterating the parent domains to find a domain that is a superset of the current sd_llc, so that later, after the duplicated parent domains have been degenerated, cache aware scheduling will take effect.
For example, the expected behavior would be:
2 sockets, 1 LLC per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 1 socket, 2 LLCs per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 2 sockets, 2 LLCs per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 1 socket, 1 LLC per socket: MC span=0-3, PKG span=0-3, has_multi_llcs=false
This bug was reported by sashiko.
multi LLCs NUMA node")
--- kernel/sched/topology.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-)
kernel/sched/topology.c +36 -3
@@ -1007,6 +1007,37 @@ static bool alloc_sd_llc(const struct cpumask *cpu_map, }#endif +/* +* Return true if @sd belongs to an LLC group whose enclosing +* partition spans more than one LLC. @sd must be the topmost +* SD_SHARE_LLC domain. +* +* Any duplicated parent domains with the same span as @sd are +* skipped: before cpu_attach_domain() degeneration these still +* exist, after degeneration the loop is a no-op. This makes the +* helper usable both during sched domain build and against an +* already-attached domain tree. +* +* Note: For systems with a single LLC per node, cache-aware +* scheduling is still enabled when multiple nodes exist. +* However, NUMA balancing decisions take precedence over +* cache-aware scheduling. Conversely, if there is only one +* LLC per partition, cache-aware scheduling should be disabled. +*/ +static bool sd_in_multi_llcs (struct sched_domain *sd) +{ +struct sched_domain *sdp = sd->parent; + +/* it does not make sense to aggregate to 1 CPU */ +if (sd->span_weight ==1 ) +return false; + +while (sdp && sdp->span_weight == sd->span_weight) + sdp = sdp->parent; + +return !!sdp; +} +/* * Return the canonical balance CPU for this group, this is the first CPU * of this group that's also in the balance mask.
@@ -3016,9 +3047,11 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att* NUMA imbalance stats for the hierarchy. */ if (sd->parent) { -if (IS_ENABLED (CONFIG_NUMA )) -adjust_numa_imbalance (sd); - has_multi_llcs = true; +if (IS_ENABLED (CONFIG_NUMA )) +adjust_numa_imbalance (sd); + +if (sd_in_multi_llcs (sd)) + has_multi_llcs = true; } } }
Signature · 1 line
2.32.0
PATCH v4 14/16 sched/cache: Fix has_multi_llcs iff at least one partition has multiple LLCs
In reply to Tim ChenFrom: Chen Yu <[email protected]>
sched_cache_present is a global static key, but build_sched_domains() is called per partition from the "Build new domains" loop in partition_sched_domains_locked(). Each call unconditionally sets the key based solely on the has_multi_llcs local variable for that partition.
The call to the last partition set the value even when there are previous partitions with multiple LLCs.
If partition A (multi-LLC) is built first, the key is enabled. Then when partition B (single-LLC) is built, the key is disabled. The multi-LLC partition A is still active but the key is now off.
Fix it by doing a similar thing as sched_energy_present: check the multi-LLCs during the iteration over all the partitions rather than checking it on a single partition.
This bug was reported by sashiko.
multi LLCs NUMA node")
--- kernel/sched/topology.c | 69 +++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 16 deletions(-)
kernel/sched/topology.c +53 -16
@@ -950,6 +950,7 @@ static void _sched_cache_active_set(void) } } +/* used by debugfs */ void sched_cache_active_set (void ) {cpus_read_lock ();
@@ -999,12 +1000,27 @@ void sched_update_llc_bytes(unsigned int cpu) unlock: sched_domains_mutex_unlock(); } + +static void sched_cache_set (bool has_multi_llcs) +{ +/* +* TBD: check before writing to it. sched domain rebuild +* is not in the critical path, leave as-is for now. +*/ +if (has_multi_llcs) +static_branch_enable_cpuslocked (&sched_cache_present); +else +static_branch_disable_cpuslocked (&sched_cache_present); + +_sched_cache_active_set (); +}#else static bool alloc_sd_llc (const struct cpumask *cpu_map,struct s_data *d) {return false; } +static inline void sched_cache_set (bool has_multi_llcs) { }#endif /*
@@ -2949,7 +2965,8 @@ void sched_domains_free_llc_id(int cpu)* to the individual CPUs */ static int -build_sched_domains (const struct cpumask *cpu_map,struct sched_domain_attr *attr) +build_sched_domains (const struct cpumask *cpu_map,struct sched_domain_attr *attr, +bool *multi_llcs) {enum s_alloc alloc_state = sa_none;bool has_multi_llcs = false;
@@ -3093,18 +3110,7 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att ret =0 ; error: -#ifdef CONFIG_SCHED_CACHE -/* -* TBD: check before writing to it. sched domain rebuild -* is not in the critical path, leave as-is for now. -*/ -if (!ret && has_multi_llcs) -static_branch_enable_cpuslocked (&sched_cache_present); -else -static_branch_disable_cpuslocked (&sched_cache_present); - -_sched_cache_active_set (); -#endif + *multi_llcs = has_multi_llcs;__free_domain_allocs (&d, alloc_state, cpu_map);return ret;
@@ -3167,6 +3173,7 @@ void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)*/ int __init sched_init_domains (const struct cpumask *cpu_map) { +bool multi_llcs;int err;zalloc_cpumask_var (&sched_domains_llc_id_allocmask,GFP_KERNEL );
@@ -3181,7 +3188,9 @@ int __init sched_init_domains(const struct cpumask *cpu_map)if (!doms_cur) doms_cur = &fallback_doms;cpumask_and (doms_cur[0 ], cpu_map,housekeeping_cpumask (HK_TYPE_DOMAIN )); - err =build_sched_domains (doms_cur[0 ],NULL ); + err =build_sched_domains (doms_cur[0 ],NULL , &multi_llcs); +if (!err) +sched_cache_set (multi_llcs);return err; }
@@ -3254,6 +3263,7 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new struct sched_domain_attr *dattr_new) {bool __maybe_unused has_eas = false; +bool has_multi_llcs = false, multi_llcs;int i, j, n;int new_topology;
@@ -3303,14 +3313,41 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_newfor (i =0 ; i < ndoms_new; i++) {for (j =0 ; j < n && !new_topology; j++) {if (cpumask_equal (doms_new[i], doms_cur[j]) && -dattrs_equal (dattr_new, i, dattr_cur, j)) +dattrs_equal (dattr_new, i, dattr_cur, j)) { +/* +* Reused partition has to be taken care +* of here, because there could be a corner +* case that if the reused partition is skipped +* and only new partition is considered, an +* incorrect has_multi_llcs would be set. For +* example: +* If the only multi-LLC partition is reused +* and a new single-LLC partition is built, +* sched_cache_set(false) disables cache-aware +* scheduling globally despite the reused +* multi-LLC partition still being active. +*/ +struct sched_domain *sd; +int cpu =cpumask_first (doms_cur[j]); + +guard (rcu)(); + sd =rcu_dereference (cpu_rq (cpu)->sd); +while (sd && sd->parent && (sd->parent->flags &SD_SHARE_LLC )) + sd = sd->parent; +if (sd && (sd->flags &SD_SHARE_LLC ) && sd->parent && +sd_in_multi_llcs (sd)) + has_multi_llcs = true; goto match2; + } }/* No match - add a new doms_new */ -build_sched_domains (doms_new[i], dattr_new ? dattr_new + i :NULL ); +build_sched_domains (doms_new[i], dattr_new ? dattr_new + i :NULL , + &multi_llcs); + has_multi_llcs |= multi_llcs; match2: ; } +sched_cache_set (has_multi_llcs);#if defined(CONFIG_ENERGY_MODEL ) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL )/* Build perf domains: */
Signature · 1 line
2.32.0
PATCH v4 15/16 sched/cache: Fix possible overflow when invalidating the preferred CPU
In reply to Tim ChenFrom: Chen Yu <[email protected]>
epoch comes from the local rq->cpu_epoch, but mm->sc_stat.epoch is written by task_tick_cache() running on any CPU - potentially a different CPU whose rq->cpu_epoch is further ahead. The unsigned underflow wraps to a huge number, so the condition fires incorrectly.
Fix this by converting the result to long.
--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
@@ -1688,7 +1688,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)* If this process hasn't hit task_cache_work() for a while invalidate * its preferred state. */ -if (epoch -READ_ONCE (mm->sc_stat.epoch) > llc_epoch_affinity_timeout || +if ((long )(epoch -READ_ONCE (mm->sc_stat.epoch)) > (long )llc_epoch_affinity_timeout ||invalid_llc_nr (mm, p,cpu_of (rq)) ||exceed_llc_capacity (mm,cpu_of (rq))) {if (READ_ONCE (mm->sc_stat.cpu) !=-1 )
Signature · 1 line
2.32.0
PATCH v4 16/16 sched/cache: Fix stale preferred_llc for a new task
In reply to Tim ChenFrom: Chen Yu <[email protected]>
On fork without CLONE_VM, the child gets a new mm, the parent's preferred_llc value is stale for the child.
Fix this by resetting the task's preferred_llc to -1.
This bug was reported by sashiko.
--- kernel/sched/fair.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/sched/fair.c +5 -0
@@ -1914,6 +1914,11 @@ void init_sched_mm(struct task_struct *p)init_task_work (work, task_cache_work); work->next = work; +/* +* Reset new task's preference to avoid +* polluting account_llc_enqueue(). +*/ + p->preferred_llc = -1; }#else /* CONFIG_SCHED_CACHE */
Signature · 1 line
2.32.0
Peter Zijlstra
maintainerRe: [Patch v4 12/16] sched/cache: Fix race condition during sched domain rebuild
In reply to Tim ChenReview
Quoted 8 lines · Tim Chen
/*
* Enable/disable cache aware scheduling according to
* user input and the presence of hardware support.
+ * Expected to be protected bycpus_read_lock()and
+ *sched_domains_mutex_lock()
*/(quoted diff) +1
-1-static void sched_cache_active_set(bool locked) +static void _sched_cache_active_set(void)
Like so?
kernel/sched/topology.c +3 -2
@@ -920,11 +920,12 @@ static bool alloc_sd_llc(const struct cp/* * Enable/disable cache aware scheduling according to * user input and the presence of hardware support. -* Expected to be protected by cpus_read_lock() and -* sched_domains_mutex_lock() */ static void _sched_cache_active_set (void ) { +lockdep_assert_cpus_held (); +lockdep_assert_held (&sched_domains_mutex); +/* hardware does not support */ if (!static_branch_likely (&sched_cache_present)) {static_branch_disable_cpuslocked (&sched_cache_active);
Peter Zijlstra
maintainerRe: [Patch v4 13/16] sched/cache: Fix cache aware scheduling enabling for multi LLCs system
In reply to Tim ChenQuoted 22 lines · Tim Chen
From: Chen Yu <[email protected]>
If there are multiple LLCs in the system, cache aware scheduling should be enabled. However, there is a corner case where, if there is a single NUMA node and a single LLC per node, cache aware scheduling will be turned on in the current implementation - because at this moment, the parent domain has not yet been degenerated, and it is possible that the current domain has the same cpu span as its parent. There is no need to turn cache aware scheduling on in this scenario.
Fix it by iterating the parent domains to find a domain that is a superset of the current
sd_llc, so that later, after the duplicated parent domains have been degenerated, cache aware scheduling will take effect.For example, the expected behavior would be:
2 sockets, 1 LLC per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 1 socket, 2 LLCs per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 2 sockets, 2 LLCs per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 1 socket, 1 LLC per socket: MC span=0-3, PKG span=0-3, has_multi_llcs=false
Quoted 4 lines
This bug was reported by sashiko.
Fixes d59f4fd1d303 ("sched/cache: Enable cache aware scheduling formulti LLCs NUMA node")
FWIW, this and the next patch have this Fixes: line mangled -- I fixed it. But please figure out what did that so it doesn't happen again.
Peter Zijlstra
maintainerRe: [Patch v4 15/16] sched/cache: Fix possible overflow when invalidating the preferred CPU
In reply to Tim ChenQuoted 22 lines · Tim Chen
From: Chen Yu <[email protected]>
epoch comes from the local
rq->cpu_epoch, butmm->sc_stat.epochis written bytask_tick_cache()running on any CPU - potentially a different CPU whoserq->cpu_epochis further ahead. The unsigned underflow wraps to a huge number, so the condition fires incorrectly.Fix this by converting the result to long.
Fixes df0d98475954 ("sched/cache: Introduce infrastructure for cache-aware load balancing")Signed-off-by Chen Yu <[email protected]>Co-developed-by Tim Chen <[email protected]>Signed-off-by Tim Chen <[email protected]>--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/fair.c +0
-0
kernel/sched/fair.c void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec) · line 1691
Quoted 5 lines
kernel/sched/fair.c +1
-1@@ -1688,7 +1688,7 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)* If this process hasn't hit task_cache_work() for a while invalidate * its preferred state. */ - if (epoch - READ_ONCE(mm->sc_stat.epoch) > llc_epoch_affinity_timeout || + if ((long)(epoch - READ_ONCE(mm->sc_stat.epoch)) > (long)llc_epoch_affinity_timeout ||
I think your fixes is wrong; afaict this was broken by patch 6, before that llc_epoch_affinity_timeout was EPOCH_LLC_AFFINITY_TIMEOUT, which is a literal 5, and thus a signed type.
Anyway, a single (long) cast should be sufficient, the other side will get promoted along IIRC.
Re: [Patch v4 12/16] sched/cache: Fix race condition during sched domain rebuild
In reply to Peter ZijlstraQuoted 12 lines · Peter Zijlstra
Review
Quoted 8 lines · Tim Chen
/*
* Enable/disable cache aware scheduling according to
* user input and the presence of hardware support.
+ * Expected to be protected bycpus_read_lock()and
+ *sched_domains_mutex_lock()
*/(quoted diff) +1
-1-static void sched_cache_active_set(bool locked) +static void _sched_cache_active_set(void)Like so?
Yes, I think this is OK. One case is that the _sched_cache_active_set() might be called via sched_init_smp() -> sched_init_domains() -> build_sched_domains() where only sched_domains_mutex was hold. But since it is during boot up thus system_state < SYSTEM_RUNNING and lockdep_assert_cpus_held() will not trigger the assert.
thanks,
Chenyu
Quoted 17 lines
kernel/sched/topology.c +3
-2@@ -920,11 +920,12 @@ static bool alloc_sd_llc(const struct cp/* * Enable/disable cache aware scheduling according to * user input and the presence of hardware support. -* Expected to be protected by cpus_read_lock() and -* sched_domains_mutex_lock() */ static void _sched_cache_active_set (void ) { +lockdep_assert_cpus_held (); +lockdep_assert_held (&sched_domains_mutex); +/* hardware does not support */ if (!static_branch_likely (&sched_cache_present)) {static_branch_disable_cpuslocked (&sched_cache_active);
Re: [Patch v4 13/16] sched/cache: Fix cache aware scheduling enabling for multi LLCs system
In reply to Peter ZijlstraQuoted 23 lines · Peter Zijlstra
Quoted 22 lines · Tim Chen
From: Chen Yu <[email protected]>
If there are multiple LLCs in the system, cache aware scheduling should be enabled. However, there is a corner case where, if there is a single NUMA node and a single LLC per node, cache aware scheduling will be turned on in the current implementation - because at this moment, the parent domain has not yet been degenerated, and it is possible that the current domain has the same cpu span as its parent. There is no need to turn cache aware scheduling on in this scenario.
Fix it by iterating the parent domains to find a domain that is a superset of the current
sd_llc, so that later, after the duplicated parent domains have been degenerated, cache aware scheduling will take effect.For example, the expected behavior would be:
2 sockets, 1 LLC per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 1 socket, 2 LLCs per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 2 sockets, 2 LLCs per socket: MC span=0-3, PKG span=0-7, has_multi_llcs=true 1 socket, 1 LLC per socket: MC span=0-3, PKG span=0-3, has_multi_llcs=false
Quoted 7 lines
Quoted 4 lines
This bug was reported by sashiko.
Fixes d59f4fd1d303 ("sched/cache: Enable cache aware scheduling formulti LLCs NUMA node")
FWIW, this and the next patch have this Fixes: line mangled -- I fixed it. But please figure out what did that so it doesn't happen again.
Thanks for fixing it. We will pay attention to this in the next version.
thanks,
Chenyu
K Prateek Nayak
reviewerHello Tim, Chenyu,
Quoted 3 lines · Tim Chen
This patch set contains cache-aware scheduling enhancements and bug fixes on top of Peter's sched/cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/cache
I took the latest queue:sched/core for a spin before and after the sched/cache merge and everything is now looking fine.
I've temporary lost access to my usual test machine so I could only grab the microbenchmark data but they are mostly positive to unaffected as expected. I'll update if I see anything funky with longer running benchmarks if and when I get a chance.
Following is the data from a dual socket Zen4c system (2 x 128C/256T) with 32 LLCs in total:
o Kernels:
tip: queue:sched/core at commit dd29c017aed6 ("sched/rt: Have
RT_PUSH_IPI be default off for non PREEMPT_RT")
sched-cache: queue:sched/core at commit a26d9208c137 ("Merge branch
'sched/cache'")
o Benchmark results
==================================================================
Test : hackbench
Units : Normalized time in seconds
Interpretation: Lower is better
Statistic : AMean
==================================================================
Case: tip[pct imp](CV) sched_cache[pct imp](CV)
1-groups 1.00 [ -0.00]( 9.66) 0.92 [ 8.04](14.93) 2-groups 1.00 [ -0.00]( 9.22) 0.88 [ 11.96](12.53) 4-groups 1.00 [ -0.00]( 2.14) 0.99 [ 0.93]( 1.55) 8-groups 1.00 [ -0.00]( 2.80) 1.00 [ 0.22]( 3.96) 16-groups 1.00 [ -0.00]( 5.54) 1.00 [ -0.49]( 2.76)
==================================================================
Test : tbench
Units : Normalized throughput
Interpretation: Higher is better
Statistic : AMean
==================================================================
Clients: tip[pct imp](CV) sched_cache[pct imp](CV)
1 1.00 [ 0.00]( 0.03) 1.00 [ 0.30]( 0.29)
2 1.00 [ 0.00]( 0.32) 1.00 [ -0.45]( 1.86)
4 1.00 [ 0.00]( 0.34) 1.00 [ 0.38]( 0.14)
8 1.00 [ 0.00]( 0.24) 1.01 [ 0.56]( 0.34)
16 1.00 [ 0.00]( 0.45) 1.00 [ 0.12]( 0.05)
32 1.00 [ 0.00]( 0.58) 1.01 [ 1.27]( 0.58)
64 1.00 [ 0.00]( 0.81) 1.01 [ 1.32]( 0.16)
128 1.00 [ 0.00]( 0.53) 1.03 [ 3.27]( 1.15)
256 1.00 [ 0.00]( 0.30) 1.02 [ 2.14]( 0.64)
512 1.00 [ 0.00]( 3.73) 1.01 [ 1.00]( 2.73)
1024 1.00 [ 0.00]( 0.23) 0.99 [ -0.53]( 0.29)
2048 1.00 [ 0.00]( 0.14) 0.99 [ -0.73]( 0.37) ==================================================================
Test : stream-10
Units : Normalized Bandwidth, MB/s
Interpretation: Higher is better
Statistic : HMean
==================================================================
Test: tip[pct imp](CV) sched_cache[pct imp](CV)
Copy 1.00 [ 0.00]( 0.66) 1.00 [ 0.04]( 0.43)
Scale 1.00 [ 0.00]( 0.89) 1.00 [ 0.17]( 0.70)
Add 1.00 [ 0.00]( 0.73) 1.00 [ 0.08]( 0.73)
Triad 1.00 [ 0.00]( 0.70) 1.00 [ 0.04]( 0.75) ==================================================================
Test : stream-100
Units : Normalized Bandwidth, MB/s
Interpretation: Higher is better
Statistic : HMean
==================================================================
Test: tip[pct imp](CV) sched_cache[pct imp](CV)
Copy 1.00 [ 0.00]( 0.32) 1.00 [ -0.25]( 1.49)
Scale 1.00 [ 0.00]( 0.26) 0.99 [ -0.50]( 1.56)
Add 1.00 [ 0.00]( 0.29) 0.99 [ -0.69]( 1.22)
Triad 1.00 [ 0.00]( 0.27) 0.99 [ -0.71]( 1.24) ==================================================================
Test : netperf
Units : Normalized Througput
Interpretation: Higher is better
Statistic : AMean
==================================================================
Clients: tip[pct imp](CV) sched_cache[pct imp](CV)
1-clients 1.00 [ 0.00]( 0.10) 1.00 [ -0.08]( 0.13)
2-clients 1.00 [ 0.00]( 0.29) 1.00 [ -0.01]( 0.16)
4-clients 1.00 [ 0.00]( 0.36) 1.00 [ -0.25]( 0.21)
8-clients 1.00 [ 0.00]( 0.32) 1.00 [ -0.28]( 0.16)
16-clients 1.00 [ 0.00]( 0.24) 1.00 [ -0.38]( 0.24)
32-clients 1.00 [ 0.00]( 0.42) 1.00 [ -0.46]( 0.49)
64-clients 1.00 [ 0.00]( 0.94) 1.00 [ -0.40]( 0.65)
128-clients 1.00 [ 0.00]( 1.10) 1.00 [ -0.08]( 0.89)
256-clients 1.00 [ 0.00]( 1.06) 1.00 [ -0.10]( 0.97)
512-clients 1.00 [ 0.00]( 4.68) 0.98 [ -1.56]( 4.53)
768-clients 1.00 [ 0.00](34.35) 0.98 [ -2.03](32.96)
1024-clients 1.00 [ 0.00](42.76) 0.98 [ -1.74](43.29) ==================================================================
Test : schbench
Units : Normalized 99th percentile latency in us
Interpretation: Lower is better
Statistic : Median
==================================================================
#workers: tip[pct imp](CV) sched_cache[pct imp](CV)
1 1.00 [ -0.00](18.94) 0.39 [ 61.36]( 8.81)
2 1.00 [ -0.00]( 1.67) 0.91 [ 8.57](12.48)
4 1.00 [ -0.00]( 9.79) 0.70 [ 29.73](11.76)
8 1.00 [ -0.00]( 2.27) 0.82 [ 18.18]( 6.19)
16 1.00 [ -0.00]( 0.00) 0.98 [ 1.79]( 1.82)
32 1.00 [ -0.00]( 1.92) 1.00 [ -0.00]( 0.72)
64 1.00 [ -0.00]( 1.19) 1.02 [ -1.56]( 0.77)
128 1.00 [ -0.00]( 0.67) 1.00 [ -0.00]( 0.44)
256 1.00 [ -0.00]( 0.46) 1.01 [ -0.88]( 1.08)
512 1.00 [ -0.00]( 0.33) 0.97 [ 2.64]( 2.07)
768 1.00 [ -0.00]( 4.69) 1.02 [ -1.55]( 2.51)
1024 1.00 [ -0.00]( 2.71) 1.05 [ -4.72]( 1.36) ==================================================================
Test : new-schbench-requests-per-second
Units : Normalized Requests per second
Interpretation: Higher is better
Statistic : Median
==================================================================
#workers: tip[pct imp](CV) sched_cache[pct imp](CV)
1 1.00 [ 0.00]( 0.15) 0.99 [ -0.59]( 0.15)
2 1.00 [ 0.00]( 0.00) 0.99 [ -0.59]( 0.15)
4 1.00 [ 0.00]( 0.00) 1.00 [ -0.29]( 0.15)
8 1.00 [ 0.00]( 0.15) 1.00 [ 0.00]( 0.00)
16 1.00 [ 0.00]( 0.15) 1.00 [ 0.00]( 0.00)
32 1.00 [ 0.00]( 0.15) 1.00 [ -0.29]( 0.00)
64 1.00 [ 0.00]( 0.00) 1.00 [ 0.00]( 0.00)
128 1.00 [ 0.00](12.53) 0.99 [ -0.59](13.81)
256 1.00 [ 0.00]( 0.15) 1.00 [ -0.28]( 0.51)
512 1.00 [ 0.00]( 0.84) 1.01 [ 0.75]( 1.02)
768 1.00 [ 0.00]( 2.05) 1.01 [ 1.18]( 1.25)
1024 1.00 [ 0.00]( 2.90) 0.98 [ -1.62]( 1.25) ==================================================================
Test : new-schbench-wakeup-latency
Units : Normalized 99th percentile latency in us
Interpretation: Lower is better
Statistic : Median
==================================================================
#workers: tip[pct imp](CV) sched_cache[pct imp](CV)
1 1.00 [ -0.00](12.99) 1.33 [-33.33](31.03)
2 1.00 [ -0.00]( 4.08) 0.77 [ 23.08]( 5.34)
4 1.00 [ -0.00]( 0.00) 0.82 [ 18.18]( 5.53)
8 1.00 [ -0.00]( 0.00) 0.91 [ 9.09]( 0.00)
16 1.00 [ -0.00]( 4.56) 1.00 [ -0.00]( 4.84)
32 1.00 [ -0.00]( 0.00) 0.91 [ 9.09]( 0.00)
64 1.00 [ -0.00]( 5.00) 1.00 [ -0.00]( 5.00)
128 1.00 [ -0.00]( 7.45) 1.17 [-16.67](18.75)
256 1.00 [ -0.00]( 2.70) 1.02 [ -2.49]( 5.07)
512 1.00 [ -0.00]( 0.00) 1.00 [ -0.00]( 0.00)
768 1.00 [ -0.00]( 1.66) 1.02 [ -2.44]( 1.30)
1024 1.00 [ -0.00]( 3.32) 1.01 [ -1.19]( 1.92) ==================================================================
Test : new-schbench-request-latency
Units : Normalized 99th percentile latency in us
Interpretation: Lower is better
Statistic : Median
==================================================================
#workers: tip[pct imp](CV) sched_cache[pct imp](CV)
1 1.00 [ -0.00]( 0.14) 1.01 [ -0.80]( 0.41)
2 1.00 [ -0.00]( 0.14) 1.02 [ -1.60]( 0.27)
4 1.00 [ -0.00]( 0.00) 1.01 [ -1.07]( 0.68)
8 1.00 [ -0.00]( 0.14) 1.01 [ -0.80]( 0.00)
16 1.00 [ -0.00]( 1.49) 0.98 [ 1.82]( 0.00)
32 1.00 [ -0.00]( 0.89) 0.99 [ 0.53]( 0.27)
64 1.00 [ -0.00]( 1.43) 1.00 [ -0.26]( 1.22)
128 1.00 [ -0.00]( 2.78) 1.01 [ -0.89]( 3.06)
256 1.00 [ -0.00]( 0.13) 1.00 [ -0.00]( 0.13)
512 1.00 [ -0.00]( 6.72) 1.07 [ -6.59]( 8.20)
768 1.00 [ -0.00]( 3.42) 1.05 [ -4.61]( 2.67)
1024 1.00 [ -0.00]( 4.37) 0.99 [ 1.43]( 2.40)---
Thanks a ton! And sorry for not having been the most responsive on the latest iterations.
Signature · 2 lines
Thanks and Regards, Prateek
Hi Prateek,
Quoted 15 lines · K Prateek Nayak
Hello Tim, Chenyu,
Quoted 3 lines · Tim Chen
This patch set contains cache-aware scheduling enhancements and bug fixes on top of Peter's sched/cache branch:
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=sched/cacheI took the latest queue:sched/core for a spin before and after the sched/cache merge and everything is now looking fine.
I've temporary lost access to my usual test machine so I could only grab the microbenchmark data but they are mostly positive to unaffected as expected. I'll update if I see anything funky with longer running benchmarks if and when I get a chance.
[ ... ]
Quoted 4 lines
Thanks a ton! And sorry for not having been the most responsive on the latest iterations.
Thanks very much for your help with this series!
I have kicked off the server-side tests based on sched/core. I suppose cache-aware scheduling can still leverage sd_llc_share after applying Peter’s patch and yours, allowing the sched domain share data for SD_SHARE_LLC and SD_ASYM_CPUCAPACITY to coexist.
Additionally, since sched/core includes optimizations for selecting SMT cores during asymmetric-capacity wakeups, I have also set up big.LITTLE systems for testing. One testbed features P-cores with SMT enabled while E-cores run without SMT support. I am simply curious to see how the benchmark data turns out.
thanks,
Chenyu