Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3792942img; Mon, 25 Mar 2019 18:38:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqzbfgPRncWr1ThReOo8JaghhV2DlyR4Q3b6fuO5mOxGMP1MUPCTOumaWQMVpd9gwWYdwpbZ X-Received: by 2002:a17:902:e101:: with SMTP id cc1mr16572361plb.129.1553564304947; Mon, 25 Mar 2019 18:38:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553564304; cv=none; d=google.com; s=arc-20160816; b=kadYT2u6ebmcH+prqwsIuPdDqNJC2djCIR+ylULcDjlZqWPGiXh/xsjCmrxkFm/SxP MOGMrTdIbeGZD5h0VgaOrp54gMH9ux0FV9AYu/BNDEew077utmHOy9w5k0/6fa+D0gkU 5WXjws97sR23sILOokXdUxRJxfoOqBcwUjogeTu76Pw21gH/7K0TWiY0J5P/9HE0HVLr d1PzlVFuD1Qou3LYN5o8xfubHWA0wCww+HGPcBYs2U69CWxZc6sgjO35wzK1oSVER7Dm jaUkxq/zUXPPn7GCZKPGnjjU87OCVVv690JDIdESE5a84nUdqVIt9475AmJYfjxR3fYX 9j/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=PdZtLst/fLzqRfwYuiuThNi3dAa40UKucQKfYrq+UhM=; b=Sp54MqwPNZ0H3R6oSHJLlVWWKRO/L/x/A6VWd9NoSOlB2XqyVnaFBQw1cbAy2fm3qz UPnBOdCH2fbXKRk40BJDB9KtFZkJFBtpDBqA7XtUnWkArBJLIiMsXWFrldAj7cGqE/68 shvnR8ZjkFmpkE2T6IhOSvD5kZrKq96m+Uo++ddNVHVA1U2uPzZq45lry5HskRpQ3uu6 Ueu8cXRNoiWEXhKP/BgQKb1nCY+4IeAOrK2glqmqbf1COMRDouRPX2OQnMj/KHVG1JWA hJZQYnOli5mYYRp0RvZhIRB3mphq8nbNeUmJd2kcJ+l1wmdHjwcYMwD2Vb2HVKG8as2Z CwDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CoMdTXwu; 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 g30si16425579plg.102.2019.03.25.18.38.09; Mon, 25 Mar 2019 18:38:24 -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=CoMdTXwu; 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 S1730706AbfCZBgP (ORCPT + 99 others); Mon, 25 Mar 2019 21:36:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:38440 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727412AbfCZBgP (ORCPT ); Mon, 25 Mar 2019 21:36:15 -0400 Received: from localhost (li1825-44.members.linode.com [172.104.248.44]) (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 544AC2075D; Tue, 26 Mar 2019 01:36:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553564174; bh=TFx0oLPulbQzVZaLavrifA9raXpTGOpN5q1N9OpsvSw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=CoMdTXwu12Kl3wWpS//JPvxgZVcFlI54l9z3ukVv+lnyKSzvJ6nL4k7W5wp0frYB1 F7TMW0RZC7+hzcKlxa4I5lrL1Uhal8qiz6B8R1OYFnBpT37DxD64ro5HdwMcwLUOBb wPnI8uP+Gl/DEgfS6twuFSGjMoT7qXmaYhlSuT1w= Date: Tue, 26 Mar 2019 10:36:07 +0900 From: Greg KH To: Arnd Bergmann Cc: stable@vger.kernel.org, Will Deacon , Florian La Roche , Peter Zijlstra , Anshul Garg , Linus Torvalds , Davidlohr Bueso , Thomas Gleixner , Ingo Molnar , Joe Perches , David Miller , Matthew Wilcox , Kees Cook , Michael Davidson , Andrew Morton , linux-kernel@vger.kernel.org Subject: Re: [BACKPORT 4.4.y 24/25] lib/int_sqrt: optimize small argument Message-ID: <20190326013607.GD21198@kroah.com> References: <20190322154425.3852517-1-arnd@arndb.de> <20190322154425.3852517-25-arnd@arndb.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190322154425.3852517-25-arnd@arndb.de> User-Agent: Mutt/1.11.4 (2019-03-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 22, 2019 at 04:44:15PM +0100, Arnd Bergmann wrote: > From: Peter Zijlstra > > 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 > (cherry picked from commit 3f3295709edea6268ff1609855f498035286af73) > Signed-off-by: Arnd Bergmann > --- > lib/int_sqrt.c | 3 +++ > 1 file changed, 3 insertions(+) Also added to 4.14.y and 4.9.y