From nobody Sat Jun 13 14:52:57 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 3B6F83FB7FB; Wed, 6 May 2026 15:51:55 +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=1778082721; cv=none; b=qbjZZqTotiLvzegFv3/uohKtUBxPIFv463tez5uwzJX4ZWDLosfLRgKnFMMQEH328JojUGjj93gSyKVkBMK7Wm6AW02fRe/4+yW33R7DrHkIWNUq0cE7w7E9k+lw3911/z9002KqujUqZpwGIwL7LL6Hf3oEHT/cowfaD6YbNJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778082721; c=relaxed/simple; bh=Ys1mOpNbp5+xVQ62OVPYsuZTKr1ckkZtvE7v4rvgjP0=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=FDbSvD3iWVoAUtByB0Q//GjDM0MRHJgdqQCXqcOL/UmNYOXV5ky5/AMhXO1sQXtwbB1ZI+wJsikmsrnquiLbSj6lQ48xnaiDb/K7X1PdWUVgUvWd4w9NvpSzTT/g0ZPnZG4TthScdlJqaIuv6HVlecdfzuZFQLteMBTvCVSWO04= 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=hT+qtkx9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mldsXuEq; 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="hT+qtkx9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mldsXuEq" Date: Wed, 06 May 2026 15:51:50 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1778082711; 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=THECyDpNbL3eH8XHquC90EYUz6mKKwObZy0/n5HdlGs=; b=hT+qtkx99NcoQ8w40CQuTjrIll8Hyr/jb9skiWfKFenGJvEwye85ProGq1LgMsumV0cw3E sybQNdBf1iA9FmsIgA9ohIfnn8n3B3yFjDu6CndM1X+HRnDX3TINIaLRl96ZvqaM38Pfgm NC7nrCUjucGsO3nS/OZE1oCh2yxvplHeywCDqEm2hfYITdHfmEbPDDypBWJLUXbfGWiK+y BEs0iPC77yeVDBg93EeRA9Ja+LMHWrYhOTBI3mapHfsK60b53WiwBho6j3wY8JuYuOikQS P/EmuDMny3ks4HD6IqLS/+jQVkJAPaWItshr9+LSSfx9cKZ/THSDiTirEkUsCw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1778082711; 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=THECyDpNbL3eH8XHquC90EYUz6mKKwObZy0/n5HdlGs=; b=mldsXuEqQMXYgzv3JmmIu5bqwP27l3OU4+57eCQcqKORkLogABWwBAZMPBm7M2z3T74gGF ARDRRzvAtEmbM5Bg== 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: <177808271032.424702.7019537438009855631.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: b6eee96843e8d088200f01b035da98e72067c5fe Gitweb: https://git.kernel.org/tip/b6eee96843e8d088200f01b035da98e72= 067c5fe Author: Zhan Xusheng AuthorDate: Fri, 01 May 2026 12:40:06 +02:00 Committer: Peter Zijlstra CommitterDate: Wed, 06 May 2026 17:41:17 +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)