Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp549487rdb; Thu, 2 Nov 2023 10:51:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGoEa02uW+bQsoxdvYVvI4sD1OFp54U7GGrqeqAD5ByfQlXCd+Sc2UwFXexm0T7Wd9tdjJD X-Received: by 2002:a05:6a00:248e:b0:6c3:60c2:8415 with SMTP id c14-20020a056a00248e00b006c360c28415mr219573pfv.30.1698947483837; Thu, 02 Nov 2023 10:51:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698947483; cv=none; d=google.com; s=arc-20160816; b=yBgm+E0ZLm/Y4UMcZboxIeCiwn3P/0yzMvBnkf66gtpDeIyBcfc3X5sJXxbRDWVSwH 5DoA2UJQlA7P5XGwv2OXZHasEl6VE/9ieuj7ouLZJZ/VvDZlPsozkEFdOc64J0XXf4JP dtC+KwkcLM/bherHWSSiIY5Ox1o7i9Fj9+r0pA1pPRMXNSt1qPixBd2uvba+lmgPDKYC BoazIfeXr/Hzj9P1nU5gDfcCozdeU5vPciZzouIy8rub2teHNxwUyZeLXigGUNVSil52 J6tD2vVCd3S4EUcKNZAbLGRk1Yggfwq4/jQOEi4SXK4c1cx5OJ1816sVJSQ+Fbn9EZGV Q4Eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=C9hmcoXIDDv1qs4YiLqL8xCdffaGDtdVzFefhq4fLAw=; fh=pudc4Lu2+vLctiyrRyPxfwEWJfCWFw26dnwj3O3+qqE=; b=VkaUB2WWnU/XwYsLo2ccKOKx4Rnn7DmDsFFNPLEf7sKQiyl7Qc8bC0FEx4LPHAwWRT CLKsL33AbWRvB5paDvPzZ+XF4j8R1cJGcuMHo3XQCDyfGHibVIoWeU9K5Ga+WAr8c1zZ i3KMckLR8fvf2aeQmKs0KbLPWknZ+7qFXY2FQke+Jk0jqbWLeMBTqFObGnRpGCgVHbG3 cj1QvD7tdDRNom/WrZ62OWb5e2YkDd6Jydw4ukRHIIWkBmjGiB1Da+s9Qk96WbNhtZ4m L9x11/pm//4xe8f/BDUq32Ho4UNmbhB/+/324rYhIjia4PKwFOlAE6+JOFePm6OEIl2H eXcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=NQ59NtHg; 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=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y9-20020a655b49000000b005b9305bfbd6si41004pgr.302.2023.11.02.10.50.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 10:50:25 -0700 (PDT) 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=@gmail.com header.s=20230601 header.b=NQ59NtHg; 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=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 7D62982AE660; Thu, 2 Nov 2023 10:49:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343499AbjKBRtL (ORCPT + 99 others); Thu, 2 Nov 2023 13:49:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344264AbjKBRtJ (ORCPT ); Thu, 2 Nov 2023 13:49:09 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 318A01A4; Thu, 2 Nov 2023 10:49:05 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-32fb1d757f7so305567f8f.0; Thu, 02 Nov 2023 10:49:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698947343; x=1699552143; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=C9hmcoXIDDv1qs4YiLqL8xCdffaGDtdVzFefhq4fLAw=; b=NQ59NtHg/ktg7FLt0Qgixl+Vg4c1Fw9795aPBRTqRsdZKx1evxBegg9UnLAAep68KY e+L6bOUKHoqfgi1uI6kEKNTAgXDYe7Y8FhyDX6WZ2UUmvsFIR64kUj0Uvi1kCezvceVc 0IUb9fnV3LDt1Z1YkcEWF+QgDLRP9fqgXz4EbJZiRsO2PoDmZ7Hti4b2HKEOIMQc+47l NgeGQfu0GHfWIvsJBG/nSqWGixYHhTaS64aZrEecrU1tROwTCpSC5UM6R1WrLAYdDvAF FPs9gGnAD/OAP7/nbIJ6p/uWlAnKbP0l6XdFH08z14JCxqMdOjgC048zyVX4EV3W9cH8 7H6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947343; x=1699552143; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=C9hmcoXIDDv1qs4YiLqL8xCdffaGDtdVzFefhq4fLAw=; b=V+PYrPyXlvAlBIn2G8XXhHkzdTKTxmmnoHqKD2geX7oVQ1IdXr3sTZF+6wjzhWtZZP kEfbekfN+5E+mQ3k3Qz6dUHjRA1vopva+RPtzfbkxpjoHi8A7VFIJqPaHm+dRDQQK/YP BDm5DLwB6c3e8rrOpk9fOjIhQ6LCq/KczseCGh4hou85Tv3uDRvCBPVHabbrrTJ8apYe i2zSJ07+C58M97jfvLlbBRQCgFrlYYMk+CHYR+ENOLceX2XrKKtqJ+xAn8y12rwrNBEN ri5gqng+QCmuzD5X6xLyucbmOo3INA7YPMLjIjCop3Gn56HARCAhWW+MUEfJGeGT6RzF 7EUA== X-Gm-Message-State: AOJu0YxKEWDq0Qhm5bPZwR9kPoOURxJ7eiQXCi9w6kF4PdD5yIUvoqoy rFhGDBpyulf49E95Pzlkg4I= X-Received: by 2002:a05:6000:1569:b0:32f:7bee:f300 with SMTP id 9-20020a056000156900b0032f7beef300mr16194258wrz.4.1698947342925; Thu, 02 Nov 2023 10:49:02 -0700 (PDT) Received: from localhost (cpc154979-craw9-2-0-cust193.16-3.cable.virginm.net. [80.193.200.194]) by smtp.gmail.com with ESMTPSA id h11-20020a056000000b00b0031980783d78sm3008641wrx.54.2023.11.02.10.49.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 10:49:02 -0700 (PDT) From: Colin Ian King To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" Cc: kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH][V2] x86/lib: Fix overflow of variable m when val >= 1410065408 Date: Thu, 2 Nov 2023 17:49:01 +0000 Message-Id: <20231102174901.2590325-1-colin.i.king@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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]); Thu, 02 Nov 2023 10:49:16 -0700 (PDT) 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). Fixes: 646e29a1789a ("x86: Improve the printout of the SMP bootup CPU table") Signed-off-by: Colin Ian King --- V2: Make m long long instead of long to fix issue for i386 as well as x86-64 --- 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 92cd8ecc3a2c..41e26e246d8f 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) { -- 2.39.2