Compare
0fa993581aaa58a34ae3dcebebe0fbf593fcb441..2f93bcb3f9aaf6ceee1aec0a97d4fe386d48e781
sched/fair: Avoid overflow in place_entity()
place_entity() inflates an entity's virtual lag with
lag * (load + weight) / load
Commit 4823725d9d1d ("sched/fair: Increase weight bits for avg_vruntime")
replaced the scaled-down weights used in this calculation with
avg_vruntime_weight(), removing the arithmetic headroom provided by
scale_load_down().
The multiplication can overflow s64 before the division even when the
final quotient is representable. A task's hierarchical effective weight
can be as small as 2 after avg_vruntime_weight() scaling under group
scheduling, while entity_lag() bounds |vlag| by
calc_delta_fair(cfs_rq_max_slice(cfs_rq) + TICK_NSEC, se)
For example, with HZ=1000, NICE_0_LOAD=1048576, and the minimum scaled
hierarchical weight scale_load(2)=2048, this bound is 891289600. With 114
maximum-weight entities, the runqueue load is 10361604096. The
intermediate product is:
891289600 * (10361604096 + 2)
= 9235189971864780800
> S64_MAX
The signed overflow corrupts the entity's placement.
Rewrite
lag * (load + weight) / load
as
lag + lag * weight / load
The two expressions are equivalent, but the latter avoids multiplying the
virtual lag by the total runqueue weight. entity_lag() bounds vlag against
the entity's effective weight, so the remaining lag * weight product stays
bounded. rescale_entity() preserves this scale across h_load changes and
already relies on the same product when rescaling vlag.
Keep the original zero-load warning semantics by multiplying lag by weight
after replacing the divisor with one.
Fixes: 4823725d9d1d ("sched/fair: Increase weight bits for avg_vruntime")
Signed-off-by: Hui Su <sh_def@163.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260928124754.1692077-1-sh_def@163.com
Hui Su <sh_def@163.com> · 2026-09-28 12:47 UTC · committed by Peter Zijlstra
7 files changed,
+30
−28
- kernel/sched/core.c
- kernel/sched/fair.c
- kernel/trace/Makefile
- kernel/trace/ring_buffer.c
- kernel/trace/trace.c
- kernel/trace/trace_events_hist.c
- kernel/trace/undefsyms_base.c
modified kernel/sched/core.c
| @@ -11327,17 +11327,16 @@ void sched_change_end(struct sched_change_ctx *ctx) | |||
| 11327 | 11327 | if (p->sched_class->switched_to) | |
| 11328 | 11328 | p->sched_class->switched_to(rq, p); | |
| 11329 | 11329 | ||
| 11330 | + | /* | |
| 11331 | + | * Check if the task can preempt the current in case the class | |
| 11332 | + | * was upgraded. Note that none of the switch*_from() methods | |
| 11333 | + | * know the new class and none of the switch*_to() methods know | |
| 11334 | + | * the old class so we must resched here. | |
| 11335 | + | */ | |
| 11336 | + | if (ctx->queued) | |
| 11337 | + | wakeup_preempt(rq, p, 0); | |
| 11338 | + | ||
| 11330 | 11339 | if (ctx->running) { | |
| 11331 | - | /* | |
| 11332 | - | * If this was a class promotion; let the old class | |
| 11333 | - | * know it got preempted. Note that none of the | |
| 11334 | - | * switch*_from() methods know the new class and none | |
| 11335 | - | * of the switch*_to() methods know the old class. | |
| 11336 | - | */ | |
| 11337 | - | if (sched_class_above(p->sched_class, ctx->class)) { | |
| 11338 | - | rq->next_class->wakeup_preempt(rq, p, 0); | |
| 11339 | - | rq->next_class = p->sched_class; | |
| 11340 | - | } | |
| 11341 | 11340 | /* | |
| 11342 | 11341 | * If this was a degradation in class; make sure to | |
| 11343 | 11342 | * reschedule. | |
modified kernel/sched/fair.c
| @@ -6463,12 +6463,20 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) | |||
| 6463 | 6463 | load = cfs_rq->sum_weight; | |
| 6464 | 6464 | if (curr && curr->on_rq) | |
| 6465 | 6465 | load += avg_vruntime_weight(cfs_rq, curr->h_load.weight); | |
| 6466 | - | ||
| 6467 | - | weight = avg_vruntime_weight(cfs_rq, se->h_load.weight); | |
| 6468 | - | lag *= load + weight; | |
| 6469 | 6466 | if (WARN_ON_ONCE(!load)) | |
| 6470 | 6467 | load = 1; | |
| 6471 | - | lag = div64_long(lag, load); | |
| 6468 | + | ||
| 6469 | + | weight = avg_vruntime_weight(cfs_rq, se->h_load.weight); | |
| 6470 | + | ||
| 6471 | + | /* | |
| 6472 | + | * Rewrite, because the product (W + w_i)*vl'_i might | |
| 6473 | + | * overflow s64: | |
| 6474 | + | * | |
| 6475 | + | * vl_i = (W + w_i)*vl'_i / W | |
| 6476 | + | * = (W*vl'_i + w_i*vl'_i) / W | |
| 6477 | + | * = vl'_i + w_i*vl'_i/W | |
| 6478 | + | */ | |
| 6479 | + | lag += div64_long(lag * weight, load); | |
| 6472 | 6480 | ||
| 6473 | 6481 | /* | |
| 6474 | 6482 | * A heavy entity (relative to the tree) will pull the | |
modified kernel/trace/Makefile
| @@ -146,12 +146,11 @@ KASAN_SANITIZE_undefsyms_base.o := y | |||
| 146 | 146 | ||
| 147 | 147 | UNDEFINED_ALLOWLIST = __asan __gcov __kasan __kcsan __hwasan __sancov __sanitizer __tsan __ubsan __msan \ | |
| 148 | 148 | __aeabi_unwind_cpp __s390_indirect_jump __x86_indirect_thunk simple_ring_buffer \ | |
| 149 | - | current_thread_info_reg \ | |
| 150 | - | $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '$$2 {print $$2}') | |
| 149 | + | $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '{print $$2}') | |
| 151 | 150 | ||
| 152 | 151 | quiet_cmd_check_undefined = NM $< | |
| 153 | 152 | cmd_check_undefined = \ | |
| 154 | - | undefsyms=$$($(NM) -u $< | awk '$$2 {print $$2}' | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \ | |
| 153 | + | undefsyms=$$($(NM) -u $< | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \ | |
| 155 | 154 | if [ -n "$$undefsyms" ]; then \ | |
| 156 | 155 | echo "Unexpected symbols in $<:" >&2; \ | |
| 157 | 156 | echo "$$undefsyms" >&2; \ | |
modified kernel/trace/ring_buffer.c
| @@ -2380,9 +2380,6 @@ int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, in | |||
| 2380 | 2380 | struct seq_file *m; | |
| 2381 | 2381 | int ret; | |
| 2382 | 2382 | ||
| 2383 | - | if (!cpumask_test_cpu(cpu, buffer->cpumask)) | |
| 2384 | - | return -ENODEV; | |
| 2385 | - | ||
| 2386 | 2383 | ret = seq_open(file, &rb_meta_seq_ops); | |
| 2387 | 2384 | if (ret) | |
| 2388 | 2385 | return ret; | |
| @@ -3630,8 +3627,10 @@ static void rb_inc_iter(struct ring_buffer_iter *iter) | |||
| 3630 | 3627 | /* Return the index into the sub-buffers for a given sub-buffer */ | |
| 3631 | 3628 | static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf) | |
| 3632 | 3629 | { | |
| 3633 | - | void *subbuf_array = rb_subbufs_from_meta(meta); | |
| 3630 | + | void *subbuf_array; | |
| 3634 | 3631 | ||
| 3632 | + | subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs; | |
| 3633 | + | subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size); | |
| 3635 | 3634 | return (subbuf - subbuf_array) / meta->subbuf_size; | |
| 3636 | 3635 | } | |
| 3637 | 3636 | ||
modified kernel/trace/trace.c
| @@ -5881,7 +5881,7 @@ static int tracing_buffer_meta_open(struct inode *inode, struct file *filp) | |||
| 5881 | 5881 | ||
| 5882 | 5882 | ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu); | |
| 5883 | 5883 | if (ret < 0) | |
| 5884 | - | trace_array_put(tr); | |
| 5884 | + | __trace_array_put(tr); | |
| 5885 | 5885 | return ret; | |
| 5886 | 5886 | } | |
| 5887 | 5887 | ||
modified kernel/trace/trace_events_hist.c
| @@ -289,7 +289,7 @@ static u64 hist_field_log2(struct hist_field *hist_field, | |||
| 289 | 289 | ||
| 290 | 290 | u64 val = hist_fn_call(operand, elt, buffer, rbe, event); | |
| 291 | 291 | ||
| 292 | - | return order_base_2(val); | |
| 292 | + | return (u64) ilog2(roundup_pow_of_two(val)); | |
| 293 | 293 | } | |
| 294 | 294 | ||
| 295 | 295 | static u64 hist_field_bucket(struct hist_field *hist_field, | |
| @@ -3090,7 +3090,7 @@ create_field_var_hist(struct hist_trigger_data *target_hist_data, | |||
| 3090 | 3090 | key_field = hist_data->fields[i]; | |
| 3091 | 3091 | if (!first) | |
| 3092 | 3092 | seq_buf_putc(&s, ','); | |
| 3093 | - | expr_field_str(key_field, &s); | |
| 3093 | + | seq_buf_puts(&s, key_field->field->name); | |
| 3094 | 3094 | first = false; | |
| 3095 | 3095 | } | |
| 3096 | 3096 | ||
modified kernel/trace/undefsyms_base.c
| @@ -9,7 +9,6 @@ | |||
| 9 | 9 | ||
| 10 | 10 | #include <linux/atomic.h> | |
| 11 | 11 | #include <linux/string.h> | |
| 12 | - | #include <asm/local.h> | |
| 13 | 12 | #include <asm/page.h> | |
| 14 | 13 | ||
| 15 | 14 | void undefsyms_base(void *p, int n); | |
| @@ -19,13 +18,11 @@ static char page[PAGE_SIZE] __aligned(PAGE_SIZE); | |||
| 19 | 18 | void undefsyms_base(void *p, int n) | |
| 20 | 19 | { | |
| 21 | 20 | char buffer[256] = { 0 }; | |
| 22 | - | u32 u = 0; | |
| 23 | 21 | ||
| 22 | + | u32 u = 0; | |
| 24 | 23 | memset((char * volatile)page, 8, PAGE_SIZE); | |
| 25 | 24 | memset((char * volatile)buffer, 8, sizeof(buffer)); | |
| 26 | 25 | memcpy((void * volatile)p, buffer, sizeof(buffer)); | |
| 27 | 26 | cmpxchg((u32 * volatile)&u, 0, 8); | |
| 28 | - | cmpxchg((unsigned long *)p, 0, 8); | |
| 29 | - | local_set((local_t *)p, 8); | |
| 30 | 27 | WARN_ON(n == 0xdeadbeef); | |
| 31 | 28 | } | |