Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3969411img; Mon, 25 Mar 2019 23:47:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyIufjOijGi3XsWLIr6BoLU6sFkuWUr5ULzWSKJk6trlILk8ETIfuyPJadgsISYnq6+7YsU X-Received: by 2002:a63:4a4d:: with SMTP id j13mr26879014pgl.16.1553582858880; Mon, 25 Mar 2019 23:47:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553582858; cv=none; d=google.com; s=arc-20160816; b=PdB30069eJ7SHEYiDk9Dap5HTxpRKvSmcKWgGFeLON4rR3E9OKraRBHLpS3fvtbU26 VTAXPSFC/xF275Crx5O/68HQeWNSaHl1WrkKfKjRWJmE07Cupfzot6amr0Uaw9OG9zKS L7nbs04u946Tc676ShhSUFR3K2GTu/jVFauLEBjYSMCC4UPDIvewZeyDzOunR94W033v oZxXcHrkWA3z9dhjVYu5vtM9cAQ0PtJZE0qPDpX1KKpQYx67MQp5xwXEGfyY51gWN46m xrr7aboWPS7r+ZInBwc8cMmMlOg1tUgMJGsnBIaQ/HUDbS86P/MkWqoqljvRfGUAIJ19 6WSA== 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=xQS+MzR8oiTaj34oJDSAyT+Yw8zUJ13OFGE4fo6QTuY=; b=to1HLumH8R3diDDpPjZwagQJvmPuE8A12NcquGPcIDsh4hEnwBAtae5ZcPL447xSpt QZc1oIW+X1BSN3wnN/kDvteCXF15e8fEAohaVgIQHM5Hjd9yZDuk+AyQF74TTeSo/CpK w4wy5Mj8dLmEoFR+Wv5zqXPw4vpPM7aarw3n5DTnnifoNeu9sWixbmcVCm5rt+kD7u2k +jJcIqzLlqIzcewGEUDB/OdHKIoRVqXhzpitWLlotpXNpZA7IOW90GACgREAuKkECU0s uyJ9I8iZT4S4V0V3xMHFB1pz3WG6/vEGN7vcYjHjB/G8o6yqADjWzLYIAFEAN+sqFIh/ b0hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=F1spVo7h; 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 d14si9585709pln.404.2019.03.25.23.47.24; Mon, 25 Mar 2019 23:47:38 -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=F1spVo7h; 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 S1731833AbfCZGeR (ORCPT + 99 others); Tue, 26 Mar 2019 02:34:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:44580 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731822AbfCZGeO (ORCPT ); Tue, 26 Mar 2019 02:34:14 -0400 Received: from localhost (unknown [104.132.152.111]) (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 4047F2070B; Tue, 26 Mar 2019 06:34:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553582053; bh=rXjFtthL8cGAlqzo83zPkhsGt2RchtB6KoJGCNPPNpk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F1spVo7hQQ9g3kKJMWRuLVnGQiHgZ5WrP+KIt+IH546AaqBQJUENJqTQoIdgQDh96 K1yUWbEGaXuR4ZOGygrbALnuUUIsmnKE9l2r+7JhZOAAxGArNU7YUtpaFVVstny9q9 enc7H0sF++/SAlcv+8bOIWzcIwf06RuMgPpldoBc= 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.14 31/41] lib/int_sqrt: optimize small argument Date: Tue, 26 Mar 2019 15:30:08 +0900 Message-Id: <20190326042651.600856386@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190326042649.889479098@linuxfoundation.org> References: <20190326042649.889479098@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.14-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 @@ -23,6 +23,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;