summaryrefslogtreecommitdiffstats
path: root/mm/interval_tree.c
diff options
context:
space:
mode:
authorTim Chen <tim.c.chen@linux.intel.com>2026-09-21 17:37:22 -0700
committerIngo Molnar <mingo@kernel.org>2026-09-22 10:49:53 +0200
commit0d6526f82c3cdefcca47f73f5fc08dc6f335eac6 (patch)
tree176dc2d685debcfb082c151ee94f2425ac96b364 /mm/interval_tree.c
downloadlinux-stable-0d6526f82c3cdefcca47f73f5fc08dc6f335eac6.tar.gz
linux-stable-0d6526f82c3cdefcca47f73f5fc08dc6f335eac6.zip
sched/cache: Keep nr_pref_llc_running in the runnable domain, to fix LLC mis-scheduling buggrafted
alb_break_llc() decides whether to break LLC preference during active load balance. It does so by testing that every runnable fair task on the source rq prefers its LLC: env->src_rq->nr_pref_llc_running == env->src_rq->cfs.h_nr_runnable But the two counters cover different sets. nr_pref_llc_running is updated in account_llc_enqueue()/account_llc_dequeue(), next to cfs_rq->nr_queued, so it follows queued tasks. h_nr_runnable is updated in set_delayed()/ clear_delayed() and drops delay-dequeued tasks. So under DELAY_DEQUEUE, a preferring task that goes to sleep stays counted in nr_pref_llc_running while h_nr_runnable falls. The equality then breaks, alb_break_llc() returns false, and active balance is free to pull a task off its preferred LLC. Active balance only moves runnable tasks, and this is the only LLC check it consults: once the stopper runs, LBF_ACTIVE_LB skips the per-task test in can_migrate_task(). The runnable set is the one we want. Fix it on the counter side. A task should be counted in nr_pref_llc_running exactly while it is both queued on its preferred LLC (pref_llc_queued) and runnable (!sched_delayed). Define that membership once in task_pref_llc_runnable(), and adjust the counter only through pref_llc_running_inc()/pref_llc_running_dec() from the four sites that change either input: account_llc_enqueue(), account_llc_dequeue(), set_delayed() and clear_delayed(). Gating every update on the same predicate keeps the delay, wake and dequeue paths from double-counting or underflowing; see the comments at those sites for the ordering. nr_llc_running and sd->llc_counts are not touched and stay on queued semantics. Fixes: 714059f79ff0 ("sched/cache: Handle moving single tasks to/from their preferred LLC") Closes: https://lore.kernel.org/lkml/20260827135000.735138-1-zhanxusheng@xiaomi.com/ Reported-by: Zhan Xusheng <zhanxusheng@xiaomi.com> Suggested-by: Chen Yu <yu.c.chen@intel.com> Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Kayra Cizmeci <kayracizmeci@gmail.com> Cc: <stable@kernel.org> # v7.2.x Link: https://patch.msgid.link/06af61afedac32e6477f57feb4d658f6c411c3af.1790035273.git.tim.c.chen@linux.intel.com
Diffstat (limited to 'mm/interval_tree.c')
-rw-r--r--mm/interval_tree.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/mm/interval_tree.c b/mm/interval_tree.c
new file mode 100644
index 000000000..7bbbf15cf
--- /dev/null
+++ b/mm/interval_tree.c
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * mm/interval_tree.c - interval tree for address_space->i_mmap and
+ * anon_vma->rb_root
+ *
+ * Copyright (C) 2012, Michel Lespinasse <walken@google.com>
+ */
+
+#include <linux/mm.h>
+#include <linux/fs.h>
+#include <linux/rmap.h>
+#include <linux/interval_tree_generic.h>
+
+/* File-backed interval tree (address_space->i_mmap) */
+
+INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.rb,
+ pgoff_t, shared.rb_subtree_last,
+ vma_start_pgoff, vma_last_pgoff, static,
+ __mapping_rmap_tree)
+
+void mapping_rmap_tree_insert(struct vm_area_struct *vma,
+ struct address_space *mapping)
+{
+ __mapping_rmap_tree_insert(vma, &mapping->i_mmap);
+}
+
+/* Insert vma immediately after prev in the interval tree */
+void mapping_rmap_tree_insert_after(struct vm_area_struct *vma,
+ struct vm_area_struct *prev,
+ struct address_space *mapping)
+{
+ struct rb_node **link;
+ struct vm_area_struct *parent;
+ const pgoff_t pgoff_last = vma_last_pgoff(vma);
+
+ VM_WARN_ON_ONCE_VMA(vma_start_pgoff(vma) != vma_start_pgoff(prev), vma);
+
+ if (!prev->shared.rb.rb_right) {
+ parent = prev;
+ link = &prev->shared.rb.rb_right;
+ } else {
+ parent = rb_entry(prev->shared.rb.rb_right,
+ struct vm_area_struct, shared.rb);
+ if (parent->shared.rb_subtree_last < pgoff_last)
+ parent->shared.rb_subtree_last = pgoff_last;
+ while (parent->shared.rb.rb_left) {
+ parent = rb_entry(parent->shared.rb.rb_left,
+ struct vm_area_struct, shared.rb);
+ if (parent->shared.rb_subtree_last < pgoff_last)
+ parent->shared.rb_subtree_last = pgoff_last;
+ }
+ link = &parent->shared.rb.rb_left;
+ }
+
+ vma->shared.rb_subtree_last = pgoff_last;
+ rb_link_node(&vma->shared.rb, &parent->shared.rb, link);
+ rb_insert_augmented(&vma->shared.rb, &mapping->i_mmap.rb_root,
+ &__mapping_rmap_tree_augment);
+}
+
+void mapping_rmap_tree_remove(struct vm_area_struct *vma,
+ struct address_space *mapping)
+{
+ __mapping_rmap_tree_remove(vma, &mapping->i_mmap);
+}
+
+struct vm_area_struct *
+mapping_rmap_tree_iter_first(struct address_space *mapping,
+ pgoff_t pgoff_start, pgoff_t pgoff_last)
+{
+ return __mapping_rmap_tree_iter_first(&mapping->i_mmap,
+ pgoff_start, pgoff_last);
+}
+
+struct vm_area_struct *
+mapping_rmap_tree_iter_next(struct vm_area_struct *vma,
+ pgoff_t pgoff_start, pgoff_t pgoff_last)
+{
+ return __mapping_rmap_tree_iter_next(vma, pgoff_start, pgoff_last);
+}
+
+/* Anonymous interval tree (anon_vma->rb_root) */
+
+static pgoff_t avc_start_pgoff(struct anon_vma_chain *avc)
+{
+ return vma_start_anon_pgoff(avc->vma);
+}
+
+static pgoff_t avc_last_pgoff(struct anon_vma_chain *avc)
+{
+ return vma_last_anon_pgoff(avc->vma);
+}
+
+INTERVAL_TREE_DEFINE(struct anon_vma_chain, rb, pgoff_t, rb_subtree_last,
+ avc_start_pgoff, avc_last_pgoff,
+ static, __anon_rmap_tree)
+
+void anon_rmap_tree_insert(struct anon_vma_chain *avc,
+ struct anon_vma *anon_vma)
+{
+#ifdef CONFIG_DEBUG_VM_RB
+ avc->cached_vma_start = avc_start_pgoff(avc);
+ avc->cached_vma_last = avc_last_pgoff(avc);
+#endif
+ __anon_rmap_tree_insert(avc, &anon_vma->rb_root);
+}
+
+void anon_rmap_tree_remove(struct anon_vma_chain *avc,
+ struct anon_vma *anon_vma)
+{
+ __anon_rmap_tree_remove(avc, &anon_vma->rb_root);
+}
+
+struct anon_vma_chain *
+anon_rmap_tree_iter_first(struct anon_vma *anon_vma,
+ pgoff_t pgoff_start, pgoff_t pgoff_last)
+{
+ return __anon_rmap_tree_iter_first(&anon_vma->rb_root,
+ pgoff_start, pgoff_last);
+}
+
+struct anon_vma_chain *
+anon_rmap_tree_iter_next(struct anon_vma_chain *avc,
+ pgoff_t pgoff_start, pgoff_t pgoff_last)
+{
+ return __anon_rmap_tree_iter_next(avc, pgoff_start, pgoff_last);
+}
+
+#ifdef CONFIG_DEBUG_VM_RB
+void anon_rmap_tree_verify(struct anon_vma_chain *avc)
+{
+ WARN_ON_ONCE(avc->cached_vma_start != avc_start_pgoff(avc));
+ WARN_ON_ONCE(avc->cached_vma_last != avc_last_pgoff(avc));
+}
+#endif