From patchwork Fri Nov 17 14:32:07 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: 166202 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp569372vqn; Fri, 17 Nov 2023 06:32:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IFZozu4qkub8WEWHGeMwSC1hB/scg/4dpZ4ZTVkRoyuNCEQ+SbZXeY8odlsMkY2bXaOV4wJ X-Received: by 2002:a05:6808:2088:b0:3b6:b27c:640a with SMTP id s8-20020a056808208800b003b6b27c640amr26607362oiw.21.1700231543172; Fri, 17 Nov 2023 06:32:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700231543; cv=none; d=google.com; s=arc-20160816; b=iA/gtkXici2GAEHpdF2+PMIFiNsEU0/DCMlWYBzy5eDSJPOBVsFbT+tdxuhfdyG8Vz JTJDYjPzSm/uiYRwC6KVqdA3VAUr+0UDp0TGk/NbkLOq1acK95ORSMIDzyjo6oiOdyaf ibE9rys9Lk0yWQ+qySxIGA3j05wOBYF8UTulHTasx8wyPLCAVZf81W1QPupxvv1yxsmR bPdMtqsorHdDJUFVZIcQEJ7NLqwtLuZe+wYXECcSc9/eponvFhhk4NAViVyPeZNRcc8M E7hxfrznqLk+C2VGfuP1MHa6QDjNZ+yNCgwEJ/YgLHomrwq8T/p86qZ1WRouxt7zhVHT xc+g== 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=K4SlUOcraB1ZrRDjMaBiCvCaXWUmNRHo1TwtO7TWWZc=; fh=JP+m0QkrBl5bASoGnnndWqiIZDsm5uz1GvoVx7ejIms=; b=BMfo6bU9/TsTNWkztpcp10gtav9P6D2kG+99iLtkPeOiPYPk7nbsEu5YV+d4Vglj1i TDgHHPyKpbQHVGpoZg2cbPv+qVHdbMUTBTdJdLm1hLwZ2sIgORQG+V9s6GKroARGD87W UDh11ZA1FpEXQXT+34iuvCikczfhb23M5I3/LT9ECG1i3ACSSX0t7wPOYFi6nf9OI4oZ AE0S1j2Ms3lOTyHw6EGfT9UMx1yoh7IssswWdGfoA68xnQgp6E55iQBm3l7+Gqvm66qy 5dH0prFStMGIq1NU99x0baEGogiNtuUNQedti/lC6voYvRILiztYvE/KWXQfHa6GFQmW fAdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UmCXfLKa; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=iYLnFa0U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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. [23.128.96.37]) by mx.google.com with ESMTPS id x12-20020a9d6d8c000000b006d312230086si645123otp.310.2023.11.17.06.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 06:32:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=UmCXfLKa; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=iYLnFa0U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 7617A803E48F; Fri, 17 Nov 2023 06:32:21 -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 S231459AbjKQOcR (ORCPT + 30 others); Fri, 17 Nov 2023 09:32:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbjKQOcP (ORCPT ); Fri, 17 Nov 2023 09:32:15 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9892A5; Fri, 17 Nov 2023 06:32:11 -0800 (PST) Date: Fri, 17 Nov 2023 14:32:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1700231529; 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=K4SlUOcraB1ZrRDjMaBiCvCaXWUmNRHo1TwtO7TWWZc=; b=UmCXfLKaklKoS7Rrd8EYBonPBLTo1lgCZBidGGyEKuYDdDxYh4y7OzDJJ+39xZon5CCjA3 XYPU6hpwcC5B1PKm9bmKYicUZ9qy+tkkjCVfSg+YfJV7uU4RxUBpq4C7qVUz6Jo7k964yO 032lZxKA5lhfYOuBoVMsDxjygRfAXVKM+HdeFi7qa64Vxx0SzhynRlg2nhm93TT2YcdxAp sEjE2O1B1q3ZGYkDAJ2w80XdzkK68gvtQp9JkeEcIVTQ8iOddo9syQEFJeRPvv4EfZSM6b fwkRhGMybZpF8priJ0YVToc1a5fgwWrlOG9S3WE5P5ganWZwxqSHz3FiSqRkkQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1700231529; 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=K4SlUOcraB1ZrRDjMaBiCvCaXWUmNRHo1TwtO7TWWZc=; b=iYLnFa0UMk8ZEYGlaMfaVIfxrfFMpHvE3yKkVZS8XLZrJpJDvzDi5gHYGRrCpmFeEmsziB L7gbH+wsAlrb+wBQ== 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: <170023152812.391.4668620262387822327.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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]); Fri, 17 Nov 2023 06:32:21 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782821990268641072 X-GMAIL-MSGID: 1782821990268641072 The following commit has been merged into the x86/misc branch of tip: Commit-ID: a24d61c609813963aacc9f6ec8343f4fcaac7243 Gitweb: https://git.kernel.org/tip/a24d61c609813963aacc9f6ec8343f4fcaac7243 Author: Colin Ian King AuthorDate: Thu, 02 Nov 2023 17:49:01 Committer: Dave Hansen CommitterDate: Fri, 17 Nov 2023 06:26:14 -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) {