From patchwork Thu Nov 9 15:41:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 163440 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp522087vqs; Thu, 9 Nov 2023 07:43:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IHF9PDFzOGBXK5GSZreQkvyxBw6nWe65SkQSrOTvvzNNqIpwD1a8O/THa/MQ3tcpgxWIqZ4 X-Received: by 2002:a17:90b:4d11:b0:27d:c36:e12c with SMTP id mw17-20020a17090b4d1100b0027d0c36e12cmr2077556pjb.9.1699544635239; Thu, 09 Nov 2023 07:43:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699544635; cv=none; d=google.com; s=arc-20160816; b=wwK66/nN3vEIkZwN3EUPlWuQ5jjs0XY681IJGjxkypT59Yny/RDEWp+/i/TW3Emc0p 4YvyQkd83TDThvo4oKk4p3NLttpVPPbQP7rVSxFKdJMELCls0jrlckUJKdHpvcsisQ72 S9XSamxmBAy39C5cPcrpW1bORjVbfw5XM8SC2+C23U9tJB69G8t3Y4hx6ut+gWUUvlAM 7l7PhA9z2CrU9QYS9gpxLJyaXmWFvr2asGB/SFYKVXilAipMRBHw79pjAFsbZDG4eO3U N47ctBQ1E2j0Efq1NiN17/Mz8x7VZTjKG1dIG5yaUeq5Z0eGiATaDWnjsAjQp5StHkK2 HEcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=bv6Yj8YYO8U9m5DfpJG2dPx7BM1yh8k4cwSobi7Y5ws=; fh=JP+m0QkrBl5bASoGnnndWqiIZDsm5uz1GvoVx7ejIms=; b=SxO6docJORwTjkLlMeSlz2qRoGIFG19WNKNnh5e3Iy2kMRGe8eqYLph6Q+M3GkPT+e Q3VdSzPTLhOzzCFNpNKMZ6d2Rl8cpA2o/iAIW+mh9dEz18Hf8+lv2EE7xPjN8H8WuULU DAl5vT+qR8tOxPkKjyYz2rJ1Ij4dA41VvqViVsL5vqkjH2n4x/uIWR0XgYbluwhupk5h yPlTQEHv4rDY1J5TkTTBozDC/2FjFmmTJCq52Q8bFd4ORhvjhG2ix6ugm3hggogmxy6+ muVo8MONDHgBkoSvaRwnauijF2fYjiyt7/3g6bkYSMNht29d25OLQ3f9aRaPs9fu892s 5Ndw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="mr8uDf/h"; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id mh5-20020a17090b4ac500b0027d0bf1eef1si2062707pjb.98.2023.11.09.07.43.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 07:43:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="mr8uDf/h"; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 08F388374D7C; Thu, 9 Nov 2023 07:43:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344254AbjKIPnr (ORCPT + 31 others); Thu, 9 Nov 2023 10:43:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344220AbjKIPms (ORCPT ); Thu, 9 Nov 2023 10:42:48 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCA664487; Thu, 9 Nov 2023 07:41:40 -0800 (PST) Date: Thu, 09 Nov 2023 15:41:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699544498; 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; bh=bv6Yj8YYO8U9m5DfpJG2dPx7BM1yh8k4cwSobi7Y5ws=; b=mr8uDf/hE/BVTLV0tY4Mq+692GnQrLEfziLsoU8D/L0jYBmejHAAfsVSl+YS7VVia8k17k 7isCYrggzOC8jeNqKO0jsRiXfkvkv8fQVehyO+yA/sSTZxVKE9EfcTHfHyWZuWelyg25Ay fxcR3PLdwgk+89E8uSyaVDrZ7z8R2CM0RZvOdEAGiVJgFzge9ROL3IWVJsHF7/fZ+pBK5X NQ/rwV9ZFV0JLUgDzkEb9h00BRu2GYPGT7ERYTsM4sDCRWmzwoV4ws82SjyCmi2ieXz3X7 Zl3F9b60lkVC7fa/4D8uVYm42ZHTBEpfZsPXx2WdJsdvA3WI+vfQ/eZ9mWp42g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699544498; 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; bh=bv6Yj8YYO8U9m5DfpJG2dPx7BM1yh8k4cwSobi7Y5ws=; b=pK8HVc891GjTF6RzV7vk/AzoRlG3ZzqfXUm/QXTfjRqH2+QN0QHlNi0RCG0Sb/g+Oi309w elusyQQOnBF42qBg== From: "tip-bot2 for Colin Ian King" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/misc] x86/lib: Fix overflow when counting digits Cc: Colin Ian King , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169954449765.3135.6225096248572680656.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 09 Nov 2023 07:43:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782101715284326749 X-GMAIL-MSGID: 1782101715284326749 The following commit has been merged into the x86/misc branch of tip: Commit-ID: f9bb4627d3a4cfc2af77f4320da75a75be51ed07 Gitweb: https://git.kernel.org/tip/f9bb4627d3a4cfc2af77f4320da75a75be51ed07 Author: Colin Ian King AuthorDate: Thu, 02 Nov 2023 17:49:01 Committer: Dave Hansen CommitterDate: Thu, 09 Nov 2023 07:33:56 -08:00 x86/lib: Fix overflow when counting digits tl;dr: The num_digits() function has a theoretical overflow issue. But it doesn't affect any actual in-tree users. Fix it by using a larger type for one of the local variables. Long version: There is an overflow in variable m in function num_digits when val is >= 1410065408 which leads to the digit calculation loop to iterate more times than required. This results in either more digits being counted or in some cases (for example where val is 1932683193) the value of m eventually overflows to zero and the while loop spins forever). Currently the function num_digits is currently only being used for small values of val in the SMP boot stage for digit counting on the number of cpus and NUMA nodes, so the overflow is never encountered. However it is useful to fix the overflow issue in case the function is used for other purposes in the future. (The issue was discovered while investigating the digit counting performance in various kernel helper functions rather than any real-world use-case). The simplest fix is to make m a long long, the overhead in multiplication speed for a long long is very minor for small values of val less than 10000 on modern processors. The alternative fix is to replace the multiplication with a constant division by 10 loop (this compiles down to an multiplication and shift) without needing to make m a long long, but this is slightly slower than the fix in this commit when measured on a range of x86 processors). [ dhansen: subject and changelog tweaks ] Fixes: 646e29a1789a ("x86: Improve the printout of the SMP bootup CPU table") Signed-off-by: Colin Ian King Signed-off-by: Dave Hansen Link: https://lore.kernel.org/all/20231102174901.2590325-1-colin.i.king%40gmail.com --- arch/x86/lib/misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/lib/misc.c b/arch/x86/lib/misc.c index 92cd8ec..40b81c3 100644 --- a/arch/x86/lib/misc.c +++ b/arch/x86/lib/misc.c @@ -8,7 +8,7 @@ */ int num_digits(int val) { - int m = 10; + long long m = 10; int d = 1; if (val < 0) {