From nobody Sat Jun 13 23:24:30 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B8E52FD681; Tue, 5 May 2026 10:50:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777978213; cv=none; b=bc56NpO0M2yh7xhZsx82SC1c6nq8q3mxgVIPy17GKA6+kqBGy0z8SCfHrVfkcP/8I5ErIbwklSoWMBGThVPsOExxbve88eFEM3n7Ii3mFQLzQ/vm8MTQnD+IL5XCFJ2drABHfQIc/e+62Uwdif/Mam6NDar59JAtl96R14msQiY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777978213; c=relaxed/simple; bh=VSGp6HhvkBMGcpSJWkB4/xGXgYUiNfFKwf9izu70XUM=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=jZgOMd4xpl0tOApHNnU/OThB9sJt7EHIfaebl1cPP8cLHlX8+nqJ4GStv7zAFoFoFe/9GjR2Kxtf5dTy0pngYv+IBM7lR2QHkaMPyNU28ILt9WRgKV/14KvaHhYJdz7Wrz5kx0o0GXbC5ATq4DuOuRzmje9KBqxMSmeQDNt0heY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=po1lhvxt; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=09uuZKbR; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="po1lhvxt"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="09uuZKbR" Date: Tue, 05 May 2026 10:50:09 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1777978210; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kOUhYcPmyGrs7f4ubkodyIxdm3J3rGSN6v55wlkV+cE=; b=po1lhvxtCEmm50GH+aW6O1D2gxWVKU38t6ni+pvgmvyncaDx7uFqK3SjED0adUdVQ6dNKH +pUrC1nkJxmQyxp7ygXgAhh0FYe6gXgw1Rxn0WKK978oO6aH6/0El6DfWdTlbkmV1bFeUr g+9ZTYBsj72AqQE1y1EQo+XYGkkA1kloBdPy3OUYib2uKXaQ9fTMMbBtqguhwLNmYAQv8G HhzwCoyFOZMUGgmB9aXH7Cj2fEPyXzudDInHi7yW1QmS6VBLPM/U4KF3FIwjmIOWlfF37M tFRXGk7gV8q6pwelL304bIrkowYpECNoei1BP7DTDV3wg9PbnlZa+YMbAYEPgw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1777978210; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kOUhYcPmyGrs7f4ubkodyIxdm3J3rGSN6v55wlkV+cE=; b=09uuZKbRhqNVEbCKKucSFl0SL2KnXsbhGHOR5/m3jGbqcT5smyTT++9NEVTjbDOz38NuEM m02I3u7ko8uNv8Bg== From: "tip-bot2 for Zhan Xusheng" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/urgent] sched/fair: Fix overflow in vruntime_eligible() Cc: Zhan Xusheng , Zhan Xusheng , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20260505103155.GN3102924@noisy.programming.kicks-ass.net> References: <20260505103155.GN3102924@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <177797820901.424702.5106811085659998133.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the sched/urgent branch of tip: Commit-ID: 416dd8fc352b6027210b42de5889ee280e4bc40c Gitweb: https://git.kernel.org/tip/416dd8fc352b6027210b42de5889ee280= e4bc40c Author: Zhan Xusheng AuthorDate: Fri, 01 May 2026 12:40:06 +02:00 Committer: Peter Zijlstra CommitterDate: Tue, 05 May 2026 12:44:24 +02:00 sched/fair: Fix overflow in vruntime_eligible() Zhan Xusheng reported running into sporadic a s64 mult overflow in vruntime_eligible(). When constructing a worst case scenario: If you have cgroups, then you can have an entity of weight 2 (per calc_group_shares()), and its vlag should then be bounded by: (slice+TICK_N= SEC) * NICE_0_LOAD, which is around 44 bits as per the comment on entity_key(). The other extreme is 100*NICE_0_LOAD, thus you get: {key, weight}[] :=3D { puny: { (slice + TICK_NSEC) * NICE_0_LOAD, 2 }, max: { 0, 100*NICE_0_LOAD }, } The avg_vruntime() would end up being very close to 0 (which is zero_vruntime), so no real help making that more accurate. vruntime_eligible(puny) ends up with: avg =3D 2 * puny.key (+ 0) load =3D 2 + 100 * NICE_0_LOAD avg >=3D puny.key * load And that is: (slice + TICK_NSEC) * NICE_0_LOAD * NICE_0_LOAD * 100, which w= ill overflow s64. Zhan suggested using __builtin_mul_overflow(), however after staring at compiler output for various architectures using godbolt, it seems that usin= g an __int128 multiplication often results in better code. Specifically, a number of architectures already compute the __int128 produc= t to determine the overflow. Eg. arm64 already has the 'smulh' instruction used.= By explicitly doing an __int128 multiply, it will emit the 'mul; smulh' patter= n, which modern cores can fuse (armv8-a clang-22.1.0). x86_64 has less branches (no OF handling). Since Linux has ARCH_SUPPORTS_INT128 to gate __int128 usage, also provide t= he __builtin_mul_overflow() variant as a fallback. [peterz: Changelog and __int128 bits] Fixes: 556146ce5e94 ("sched/fair: Avoid overflow in enqueue_entity()") Reported-by: Zhan Xusheng Closes: https://patch.msgid.link/20260415145742.10359-1-zhanxusheng%40xiaom= i.com Signed-off-by: Zhan Xusheng Signed-off-by: Peter Zijlstra (Intel) Link: https://patch.msgid.link/20260505103155.GN3102924%40noisy.programming= .kicks-ass.net --- kernel/sched/fair.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 7289658..b91c8b2 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -882,11 +882,11 @@ bool update_entity_lag(struct cfs_rq *cfs_rq, struct = sched_entity *se) * * lag_i >=3D 0 -> V >=3D v_i * - * \Sum (v_i - v)*w_i - * V =3D ------------------ + v + * \Sum (v_i - v0)*w_i + * V =3D ------------------- + v0 * \Sum w_i * - * lag_i >=3D 0 -> \Sum (v_i - v)*w_i >=3D (v_i - v)*(\Sum w_i) + * lag_i >=3D 0 -> \Sum (v_i - v0)*w_i >=3D (v_i - v0)*(\Sum w_i) * * Note: using 'avg_vruntime() > se->vruntime' is inaccurate due * to the loss in precision caused by the division. @@ -894,7 +894,7 @@ bool update_entity_lag(struct cfs_rq *cfs_rq, struct sc= hed_entity *se) static int vruntime_eligible(struct cfs_rq *cfs_rq, u64 vruntime) { struct sched_entity *curr =3D cfs_rq->curr; - s64 avg =3D cfs_rq->sum_w_vruntime; + s64 key, avg =3D cfs_rq->sum_w_vruntime; long load =3D cfs_rq->sum_weight; =20 if (curr && curr->on_rq) { @@ -904,7 +904,36 @@ static int vruntime_eligible(struct cfs_rq *cfs_rq, u6= 4 vruntime) load +=3D weight; } =20 - return avg >=3D vruntime_op(vruntime, "-", cfs_rq->zero_vruntime) * load; + key =3D vruntime_op(vruntime, "-", cfs_rq->zero_vruntime); + + /* + * The worst case term for @key includes 'NSEC_TICK * NICE_0_LOAD' + * and @load obviously includes NICE_0_LOAD. NSEC_TICK is around 24 + * bits, while NICE_0_LOAD is 20 on 64bit and 10 otherwise. + * + * This gives that on 64bit the product will be at least 64bit which + * overflows s64, while on 32bit it will only be 44bits and should fit + * comfortably. + */ +#ifdef CONFIG_64BIT +#ifdef CONFIG_ARCH_SUPPORTS_INT128 + /* This often results in simpler code than __builtin_mul_overflow(). */ + return avg >=3D (__int128)key * load; +#else + s64 rhs; + /* + * On overflow, the sign of key tells us the correct answer: a large + * positive key means vruntime >> V, so not eligible; a large negative + * key means vruntime << V, so eligible. + */ + if (check_mul_overflow(key, load, &rhs)) + return key <=3D 0; + + return avg >=3D rhs; +#endif +#else /* 32bit */ + return avg >=3D key * load; +#endif } =20 int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se)