From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 68AF1C10DCE for ; Wed, 18 Mar 2020 19:29:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 37D2820772 for ; Wed, 18 Mar 2020 19:29:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 37D2820772 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=SDF.ORG Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AA9896B000A; Wed, 18 Mar 2020 15:29:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A5A386B0037; Wed, 18 Mar 2020 15:29:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96FEC6B0080; Wed, 18 Mar 2020 15:29:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0211.hostedemail.com [216.40.44.211]) by kanga.kvack.org (Postfix) with ESMTP id 7C0196B000A for ; Wed, 18 Mar 2020 15:29:47 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 2FAE3824805A for ; Wed, 18 Mar 2020 19:29:47 +0000 (UTC) X-FDA: 76609472814.28.flame12_726c367c3ad02 X-HE-Tag: flame12_726c367c3ad02 X-Filterd-Recvd-Size: 4025 Received: from mx.sdf.org (mx.sdf.org [205.166.94.20]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Wed, 18 Mar 2020 19:29:46 +0000 (UTC) Received: from sdf.org (IDENT:lkml@otaku.sdf.org [205.166.94.8]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id 02IJTYML008547 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO); Wed, 18 Mar 2020 19:29:34 GMT Received: (from lkml@localhost) by sdf.org (8.15.2/8.12.8/Submit) id 02IJTYIX014002; Wed, 18 Mar 2020 19:29:34 GMT Date: Wed, 18 Mar 2020 19:29:34 +0000 From: George Spelvin To: Dan Williams Cc: Kees Cook , Linux MM , Andrew Morton , lkml@sdf.org Subject: Re: [PATCH v2] mm/shuffle.c: Fix races in add_to_free_area_random() Message-ID: <20200318192934.GD2281@SDF.ORG> References: <20200317135035.GA19442@SDF.ORG> <202003171435.41F7F0DF9@keescook> <20200317230612.GB19442@SDF.ORG> <202003171619.23210A7E0@keescook> <20200318014410.GA2281@SDF.ORG> <20200318082035.GB2281@SDF.ORG> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Mar 18, 2020 at 10:36:10AM -0700, Dan Williams wrote: > On Wed, Mar 18, 2020 at 1:20 AM George Spelvin wrote: >> On Tue, Mar 17, 2020 at 08:53:55PM -0700, Dan Williams wrote: >>> I had the impression that unless unlikely is "mostly never" then it >>> can do more harm than good. Is a branch guaranteed to be taken every >>> BITS_PER_LONG'th occurrence really a candidate for unlikely() >>> annotation? >> >> I had to look this up. GCC manual: >> >> For the purposes of branch prediction optimizations, the probability >> that a '__builtin_expect' expression is 'true' is controlled by GCC's >> 'builtin-expect-probability' parameter, which defaults to 90%. You can >> also use '__builtin_expect_with_probability' to explicitly assign a >> probability value to individual expressions. >> >> So I think that <= 10% is good enough, which is true in this case. >> >> I was tring to encourage the compiler to: >> * Place this code path out of line, and >> * Not do the stack manipulations (build a frame, spill registers) >> needed for a non-leaf function if this path isn't taken. > > Understood, I think it's ok in this case because the shuffling only > happens for order-10 page free events by default so it will be > difficult to measure the perf impact either way. But in other kernel > contexts I think unlikely() annotation should come with numbers, 90% > not taken is not sufficient in and of itself. I'm not sure I fully understand your point. I *think* you're editorializing on unlikely() in general and not this specific code, but it's a little hard to follow. Your mention of "order-10 page free events" is confusing. Do you mean "(order-10 page) free events", i.e. freeing of 1024 consecutive pages? Or are you using "order" as a synonym for "approximately" and you mean "approximately 10 (page free event)s"? We both agree (I hope) that the number here is obvious on brief inspection: 1/BITS_PER_LONG. GCC's heuristics are tuned to value cycles on the fast path 9x as much as cycles on the slow path, so it will spend up to 9 cycles on the slow path to save a cycle on the fast path. I've found one comment (https://pastebin.com/S8Y8tqZy) saying that GCC < 9.x was a lot sloppier on the cost ratio and could pessimize the code if the branch was more than ~ 1% taken. Perhaps that's what you're remembering? Fortunately, 1/64 = 1.56% is fairly close to 1%. so I'm not too worried. > You can add: > > Acked-by: Dan Williams Thank you!