Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp499697yba; Mon, 1 Apr 2019 10:31:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqwEIL8Kb3rkJExBoy6rgcDpCKiY2vgcA5+A8SCQZwJZ5PFi1EpVPFNPxvnFC2wQJaUHBFHD X-Received: by 2002:aa7:8252:: with SMTP id e18mr8922383pfn.105.1554139913766; Mon, 01 Apr 2019 10:31:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554139913; cv=none; d=google.com; s=arc-20160816; b=at1wr9rpF+g1ZW5TFUgc8N1BUn4FAHbNx6vrp8uz6zTMuJ1eywafbHnU6jptJ9HlBD 2uzijd84vXtfkyqlRIZD1SPBqwRhvSjuy7XpFIvw7zNpzoujAmPmm6o1LBl58R7rPXUN 0YnktYS5aR6ohbB171fmXKrarErJLV1AHunK4qLgeBARRFM3pzFLEtfsQBpeflqluqRt 7nlNzhlKcgfL2hiCygjR6f5ekw7eaqeTuqfI7WDJfTGbc2ogOzqj39XR2nCUq+NvMu8Y kX+9JZZZklztj0l2Qd+1LncvzkGOnKSXf+BWwDbeYn+Kq6hujLMHKhvANBATIQJ+hJJU /6Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5COykiDTQxpyX12rv/6+MV/6ykEKBDdSL64tkvPIfIA=; b=xgzGOmjNgn7cd1ytYE0qa+Il5phGi5ujHzhyGu7vNXvYMKpsXRK1RHK91wqCWCaKno bN1NEM7a3HCUoVxH0dN9qz8Gx4gjTMSMsHYnVriYwJrjHk8o48wLk3Nv8ACCP1Y1vuCH hGkUcmOiMMFj3j8fWQTuEuN5msfHT7Su4EiPG0tdRXbo2h0fRSOUvNsANLYvc7G6PCpz shlvXb97CG1udZPthCr8zCaMoPY+ve0WDniguHzhKXuZtlQ/7P8CYQf8g07OzZkQJ1lQ JvXQdQx4N6gBOMRiYwnNSLZI/3ghvoyx0XshgU/yUETLR2mVcwIuVa9A2zHdOAjgL8y6 LMnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="YF/zFJL+"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b8si8974739pgq.424.2019.04.01.10.31.38; Mon, 01 Apr 2019 10:31:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="YF/zFJL+"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733208AbfDARbE (ORCPT + 99 others); Mon, 1 Apr 2019 13:31:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:38280 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732645AbfDARbA (ORCPT ); Mon, 1 Apr 2019 13:31:00 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 05D062146E; Mon, 1 Apr 2019 17:30:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554139859; bh=pej0Pbt+qhk8m2a8gnsOK7/fLuPxk62dTTJxGE/Foz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YF/zFJL+YeuCO2VacSekYMS/VZ2w5HlevSRk6RINGw/IqAQGIQqF038y9VJ2ROZ8J 2dUTum9QNoSwKhuPe1tasqsCZbCfGOHtuhHTKxpY4HspzGtbMMRMaHpJ8h7HiGA9zp oseEG5ibibPr27bdjg0oLGDZTZvxV/aUSfg47Yi0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Anshul Garg , Linus Torvalds , Davidlohr Bueso , Thomas Gleixner , Ingo Molnar , Will Deacon , Joe Perches , David Miller , Matthew Wilcox , Kees Cook , Michael Davidson , Andrew Morton , Arnd Bergmann Subject: [PATCH 4.4 034/131] lib/int_sqrt: optimize small argument Date: Mon, 1 Apr 2019 19:01:44 +0200 Message-Id: <20190401170055.035782875@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190401170051.645954551@linuxfoundation.org> References: <20190401170051.645954551@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Peter Zijlstra commit 3f3295709edea6268ff1609855f498035286af73 upstream. The current int_sqrt() computation is sub-optimal for the case of small @x. Which is the interesting case when we're going to do cumulative distribution functions on idle times, which we assume to be a random variable, where the target residency of the deepest idle state gives an upper bound on the variable (5e6ns on recent Intel chips). In the case of small @x, the compute loop: while (m != 0) { b = y + m; y >>= 1; if (x >= b) { x -= b; y += m; } m >>= 2; } can be reduced to: while (m > x) m >>= 2; Because y==0, b==m and until x>=m y will remain 0. And while this is computationally equivalent, it runs much faster because there's less code, in particular less branches. cycles: branches: branch-misses: OLD: hot: 45.109444 +- 0.044117 44.333392 +- 0.002254 0.018723 +- 0.000593 cold: 187.737379 +- 0.156678 44.333407 +- 0.002254 6.272844 +- 0.004305 PRE: hot: 67.937492 +- 0.064124 66.999535 +- 0.000488 0.066720 +- 0.001113 cold: 232.004379 +- 0.332811 66.999527 +- 0.000488 6.914634 +- 0.006568 POST: hot: 43.633557 +- 0.034373 45.333132 +- 0.002277 0.023529 +- 0.000681 cold: 207.438411 +- 0.125840 45.333132 +- 0.002277 6.976486 +- 0.004219 Averages computed over all values <128k using a LFSR to generate order. Cold numbers have a LFSR based branch trace buffer 'confuser' ran between each int_sqrt() invocation. Link: http://lkml.kernel.org/r/20171020164644.876503355@infradead.org Fixes: 30493cc9dddb ("lib/int_sqrt.c: optimize square root algorithm") Signed-off-by: Peter Zijlstra (Intel) Suggested-by: Anshul Garg Acked-by: Linus Torvalds Cc: Davidlohr Bueso Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Will Deacon Cc: Joe Perches Cc: David Miller Cc: Matthew Wilcox Cc: Kees Cook Cc: Michael Davidson Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Arnd Bergmann Signed-off-by: Greg Kroah-Hartman --- lib/int_sqrt.c | 3 +++ 1 file changed, 3 insertions(+) --- a/lib/int_sqrt.c +++ b/lib/int_sqrt.c @@ -22,6 +22,9 @@ unsigned long int_sqrt(unsigned long x) return x; m = 1UL << (BITS_PER_LONG - 2); + while (m > x) + m >>= 2; + while (m != 0) { b = y + m; y >>= 1;