Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp54360rdb; Wed, 1 Nov 2023 16:52:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGCE8N5ifUlJ04zkypRSkDrn2DiWZFVxztXm40PKhNM84KENq6aJn6uSAgBpRkmhOL9fKv0 X-Received: by 2002:a05:6358:5908:b0:168:e702:5108 with SMTP id g8-20020a056358590800b00168e7025108mr22981798rwf.5.1698882750647; Wed, 01 Nov 2023 16:52:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698882750; cv=none; d=google.com; s=arc-20160816; b=TlccNn/hb6O2ck3skFMn/xUh+wZoNbFE6vAkv5UregVxJI2N8s8GfiXyoxPWEdDZKJ koUFN2nl/zZONm3w4wYi+xl6du+MAqEHsC+DRr+Uy1BX/b44wN5rtFUP8yOvlE94LqC8 AfQxNW7hmR6VQDUM9/2sm2F9VtPG53oO20fbduZoPyscBHnaAVmzNN7HPYa5cN7qHZyw uJf/74m7a89S8ORaYy9O3aBoVFz7CXEPHLLy3t9Cm6nKgHqptcsnP0CnPqmIx1kbT4gC E7L9rF1AJkYweLsVgMFA38ImahLp3Sg8JrLYnPfQp4rhnyXVeOL9diCth5LtlaMJujoX js4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=KRgbwZSg+I8F4GIWxZtVNCJODdPzeC1pCi/Dya698bY=; fh=ncr8ZaSLjGjnnAS2PtKJfjvJzObSD3HJkB2L202vEdI=; b=tGO1Ku/xVRZSL7/M0yCS130BExNbcJ8iyh0Vfnrfs62pVbSJ2bCBaIrKhYH8FKFN8x grf4LY/0cxHDv+/4xXq5HHcbzBTVcMpKNkaPA1HTEWZ5Pl+kxl84215x9p3N8kPn4xtQ aiMrhm8XUr3HwLuEheLOXHlvrvN/Or9gDuYOWqBFAoqGIksSugHY9b/aIS14HL2oo5OR E8KDr+xKXP0YX8vwH2z3Tr8fj8QfgXL7BD6IX7gFzspKRTMbc95vmkHRhj0+dvThXHg2 YMWNz6dVNqN2jRzQV3wKDHDkaTCwHox3R1dXJPMv4jHV4f1kS/BxgZVhbTQl32JMMMAt Uh4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=jRTV0RXO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id o14-20020a656a4e000000b00589fcc39ef1si995018pgu.365.2023.11.01.16.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 16:52:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=jRTV0RXO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 494CE806B9E5; Wed, 1 Nov 2023 16:52:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229517AbjKAXv3 (ORCPT + 99 others); Wed, 1 Nov 2023 19:51:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231359AbjKAXv0 (ORCPT ); Wed, 1 Nov 2023 19:51:26 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27A29126; Wed, 1 Nov 2023 16:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Sender:Reply-To:Content-ID:Content-Description; bh=KRgbwZSg+I8F4GIWxZtVNCJODdPzeC1pCi/Dya698bY=; b=jRTV0RXOOtdPIBwTv30bAO9kVO eG7/s4QyT4OltzFZZ/ZFrZpucTGfEwluKPNcSgw0wErQqeG3pcS0NKJSg2yvrFOrMDRv3BCxstay7 24aCUC9B8ow6ha+pnJnNitqSDC4w0ZIQn0d1XWbiiwgJmke7qnsXwqmMvcZcAholaSRUT2zkzPwZm p0w/ebAS9BUM8YTPsPviPUxRQAMaELqTcp3TenRwXinrB+dHusyIlI5VjUIHNPA9esobcXjBBmgLe knyZjy8lQlvU5ed/lkSZPQP8gVT3Osavf142MA5P8BMGFqKRjTHFYRjmXJaeKLODpo0lK2vDtqcU3 1wwYzrpQ==; Received: from [50.53.46.231] (helo=[192.168.254.15]) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qyKzD-008MgL-1I; Wed, 01 Nov 2023 23:50:59 +0000 Message-ID: <4db3c258-a7c8-4e92-83db-797da32dd336@infradead.org> Date: Wed, 1 Nov 2023 16:50:57 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] x86/lib: Fix overflow of variable m when val >= 1410065408 Content-Language: en-US To: Colin Ian King , 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 References: <20231101153237.2214698-1-colin.i.king@gmail.com> From: Randy Dunlap In-Reply-To: <20231101153237.2214698-1-colin.i.king@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Wed, 01 Nov 2023 16:52:23 -0700 (PDT) On 11/1/23 08:32, Colin Ian King wrote: > 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 encounterd. 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 int, the overhead in > multiplication speed for a 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 int, 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 Reviewed-by: Randy Dunlap Tested-by: Randy Dunlap num_digits() now works for all int values. Thanks. > --- > 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 m = 10; > int d = 1; > > if (val < 0) { -- ~Randy