Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3959919img; Mon, 25 Mar 2019 23:33:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqybez/6vMJ1eiJrUsiV03qCMFsyJJuCxR/OWfU1gvxRlYEdcuLUPSkZkMiTtb9jvE+XrEWS X-Received: by 2002:a63:481:: with SMTP id 123mr27796568pge.167.1553581996328; Mon, 25 Mar 2019 23:33:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553581996; cv=none; d=google.com; s=arc-20160816; b=nXca6I/ep2mDH0wtggu0SK4jNipUuZf4EgOVsPVKMw4kgOFUDDvKAg6Fnpd2xV+jVU 4CaRiPlMqAyIXOiTZaqa8OxiGF9co4TxkV9RJIsOpLkV3Dwg4L+W2fg5bOeQtDgGeXNE R8uc+UIFdSj1Bg12QNG+8BatU9Fi10GT/Duk1dKSL11JCreHDABW5WYQd0q5O2axmbiH K6/LsOClDpDbo8OIiS8XV7D28zV3Izg0CzmoK+9dLUtQDPygChYtOhngHWepFHVqmonY HB5V8p2ST6rY5aALnX9k9hbQyHeUh4y0LNbNp57YpLkh4Vyt/6W1lLBCcsRFfMJg1PGi qDlA== 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=aG74mrywfNZECDbPLZO71R5KohRYNI2rzOSkA4AKvS0=; b=AaMjWHjlDRhERthsTAmTj2gJuE+UZv1l6mAlCf4NMn9AzyFSV9hjsPpY84ZTlAUGgr VizeqrEfdJEIqkTM9mNEhlo0y9JJ9Ss2IGUl3tz0nAqSh0gcLUxm5Wh1ouRdY7+E57TZ Q0LlpW8wva0U5QR7J6ONT9yPCVm4N9ACcfjCk04WbUhmZq+ZsHI/w4W4YHO9Z7b1lx4q Z0eqmpY+IQlsM2o3ijRYHXTOK94lyvTvnCK3Uyl0/YGkIlzU269IY1vtEOdVy7M8/T/t yfMFBqkn+4rK8ltMdKD08VgZ2htPabkmb42OPC4b/8Wa3hY9jyPOu6m2M0NEnhvoXpRT hEEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pFbEflln; 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 x8si11866438pll.207.2019.03.25.23.33.01; Mon, 25 Mar 2019 23:33:16 -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=pFbEflln; 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 S1731294AbfCZGcB (ORCPT + 99 others); Tue, 26 Mar 2019 02:32:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:41386 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731275AbfCZGb7 (ORCPT ); Tue, 26 Mar 2019 02:31:59 -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 5653020863; Tue, 26 Mar 2019 06:31:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553581918; bh=vACHm2q4bj398dRKP7fdfEkhhwgZMfEvNhIZQktEMuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pFbEfllnymFfhhB6+bjmHw+Pa9qbJgpGZdvwm+Jnq9XMmW2nAEYzAD4FlxQVpwltN VgCkp7ewzBfwaq0479Qy9mQFHCgB51FRiCaLOzIB6wyNbkVGoCizkeH0Xt3Fbvr/3x UNdQFJiElKjLgHu9gdBoFez04fPwjGS7k/tcv/c4= 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.9 24/30] lib/int_sqrt: optimize small argument Date: Tue, 26 Mar 2019 15:30:03 +0900 Message-Id: <20190326042608.381928654@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190326042607.558087893@linuxfoundation.org> References: <20190326042607.558087893@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.9-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;