Compare
78436f3882fb8d848c823bf8cf754988202c5ca5..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
5 files changed,
+31
−29
- arch/x86/events/intel/uncore_snbep.c
- include/linux/sched/user.h
- kernel/events/core.c
- kernel/sched/core.c
- kernel/sched/fair.c
modified arch/x86/events/intel/uncore_snbep.c
| @@ -474,9 +474,6 @@ | |||
| 474 | 474 | /* DMR */ | |
| 475 | 475 | #define DMR_IMH1_HIOP_MMIO_BASE 0x1ffff6ae7000 | |
| 476 | 476 | #define DMR_HIOP_MMIO_SIZE 0x8000 | |
| 477 | - | #define DMR_CXLCM_PMON_CTL_INVERT BIT(21) | |
| 478 | - | #define DMR_CXLCM_PMON_RAW_EVENT_MASK (GENERIC_PMON_RAW_EVENT_MASK | \ | |
| 479 | - | DMR_CXLCM_PMON_CTL_INVERT) | |
| 480 | 477 | #define DMR_CXLCM_EVENT_MASK_EXT 0xf | |
| 481 | 478 | #define DMR_HAMVF_EVENT_MASK_EXT 0xffffffff | |
| 482 | 479 | #define DMR_PCIE4_EVENT_MASK_EXT 0xffffff | |
| @@ -6699,7 +6696,7 @@ static struct event_constraint dmr_uncore_cxlcm_constraints[] = { | |||
| 6699 | 6696 | ||
| 6700 | 6697 | static struct intel_uncore_type dmr_uncore_cxlcm = { | |
| 6701 | 6698 | .name = "cxlcm", | |
| 6702 | - | .event_mask = DMR_CXLCM_PMON_RAW_EVENT_MASK, | |
| 6699 | + | .event_mask = GENERIC_PMON_RAW_EVENT_MASK, | |
| 6703 | 6700 | .event_mask_ext = DMR_CXLCM_EVENT_MASK_EXT, | |
| 6704 | 6701 | .constraints = dmr_uncore_cxlcm_constraints, | |
| 6705 | 6702 | .format_group = &dmr_cxlcm_uncore_format_group, | |
modified include/linux/sched/user.h
| @@ -23,13 +23,11 @@ struct user_struct { | |||
| 23 | 23 | struct hlist_node uidhash_node; | |
| 24 | 24 | kuid_t uid; | |
| 25 | 25 | ||
| 26 | - | #if defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \ | |
| 26 | + | #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL) || \ | |
| 27 | + | defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \ | |
| 27 | 28 | defined(CONFIG_VFIO_PCI_ZDEV_KVM) || IS_ENABLED(CONFIG_IOMMUFD) || \ | |
| 28 | 29 | defined(CONFIG_SECRETMEM) | |
| 29 | - | atomic_long_t locked_vm; /* pinned pages charged to RLIMIT_MEMLOCK */ | |
| 30 | - | #endif | |
| 31 | - | #ifdef CONFIG_PERF_EVENTS | |
| 32 | - | atomic_long_t perf_mlock; /* perf buffer pages within perf_event_mlock_kb */ | |
| 30 | + | atomic_long_t locked_vm; | |
| 33 | 31 | #endif | |
| 34 | 32 | #ifdef CONFIG_WATCH_QUEUE | |
| 35 | 33 | atomic_t nr_watches; /* The number of watches this user currently has */ | |
modified kernel/events/core.c
| @@ -7059,7 +7059,7 @@ static void perf_mmap_close(struct vm_area_struct *vma) | |||
| 7059 | 7059 | perf_pmu_output_stop(event); | |
| 7060 | 7060 | ||
| 7061 | 7061 | /* now it's safe to free the pages */ | |
| 7062 | - | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->perf_mlock); | |
| 7062 | + | atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); | |
| 7063 | 7063 | atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); | |
| 7064 | 7064 | ||
| 7065 | 7065 | /* this has to be the last one */ | |
| @@ -7242,11 +7242,11 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, | |||
| 7242 | 7242 | /* Increase the limit linearly with more CPUs */ | |
| 7243 | 7243 | user_lock_limit *= num_online_cpus(); | |
| 7244 | 7244 | ||
| 7245 | - | user_locked = atomic_long_read(&user->perf_mlock); | |
| 7245 | + | user_locked = atomic_long_read(&user->locked_vm); | |
| 7246 | 7246 | ||
| 7247 | 7247 | /* | |
| 7248 | 7248 | * sysctl_perf_event_mlock may have changed, so that | |
| 7249 | - | * user->perf_mlock > user_lock_limit | |
| 7249 | + | * user->locked_vm > user_lock_limit | |
| 7250 | 7250 | */ | |
| 7251 | 7251 | if (user_locked > user_lock_limit) | |
| 7252 | 7252 | user_locked = user_lock_limit; | |
| @@ -7254,7 +7254,7 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, | |||
| 7254 | 7254 | ||
| 7255 | 7255 | if (user_locked > user_lock_limit) { | |
| 7256 | 7256 | /* | |
| 7257 | - | * charge perf_mlock until it hits user_lock_limit; | |
| 7257 | + | * charge locked_vm until it hits user_lock_limit; | |
| 7258 | 7258 | * charge the rest from pinned_vm | |
| 7259 | 7259 | */ | |
| 7260 | 7260 | *extra = user_locked - user_lock_limit; | |
| @@ -7272,7 +7272,7 @@ static void perf_mmap_account(struct vm_area_struct *vma, long user_extra, long | |||
| 7272 | 7272 | { | |
| 7273 | 7273 | struct user_struct *user = current_user(); | |
| 7274 | 7274 | ||
| 7275 | - | atomic_long_add(user_extra, &user->perf_mlock); | |
| 7275 | + | atomic_long_add(user_extra, &user->locked_vm); | |
| 7276 | 7276 | atomic64_add(extra, &vma->vm_mm->pinned_vm); | |
| 7277 | 7277 | } | |
| 7278 | 7278 | ||
| @@ -7281,7 +7281,7 @@ static void perf_mmap_unaccount(struct vm_area_struct *vma, struct perf_buffer * | |||
| 7281 | 7281 | struct user_struct *user = rb->mmap_user; | |
| 7282 | 7282 | ||
| 7283 | 7283 | atomic_long_sub((perf_data_size(rb) >> PAGE_SHIFT) + 1 - rb->mmap_locked, | |
| 7284 | - | &user->perf_mlock); | |
| 7284 | + | &user->locked_vm); | |
| 7285 | 7285 | atomic64_sub(rb->mmap_locked, &vma->vm_mm->pinned_vm); | |
| 7286 | 7286 | } | |
| 7287 | 7287 | ||
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 | |