Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2051838imm; Wed, 16 May 2018 07:11:20 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrFwjMUSOvKA/Z0OtEZvwYdD7MnMV6993FcL7vuTDn1HOKNI45tbn6+oQmLWy3Lv1dbannF X-Received: by 2002:a62:5610:: with SMTP id k16-v6mr1138930pfb.19.1526479880520; Wed, 16 May 2018 07:11:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526479880; cv=none; d=google.com; s=arc-20160816; b=NIFGe1Y6dzeLqCgAhwtLaHtpnxUXLyX+5zIfBoHgEUZEYkMil88/x4mIpnvMPCJ0Wt JJZxadVdH7+ErCN3o/LJuschLFeSu6H8ucxKJMMS9BEBOR05C96Rbtdl5igdeYew/oCU iT3n0SL/kHVceHAAvf2+J4O1Iz8h6c6lZCUFU/pTSE/nd898UpOhhxQQLHAdDjy4Nrp1 ZDVImxE3lCBBH7fQnvApS9ZkIJcfGeX+5hwJOLMQH6M1iMZVjiy/it2aNWRQ9u9WXSHA 0ckB9jEUUAXn75pyLNFaCP0MJwWQXkVIiXLgLNRYvKTiKeKYPy/Nvpdp2mdkgyAbmoP4 5eZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=tk0EDm/wP6CTy0xk5kU81rrUf8gmkiTOLK8PxjfvVnY=; b=xsLpa9Z2X2PbOX6jadQE2apCl9Rd3yg4f+XJrxc1uz/OPYgnjjND5amD5UffY0K+Cp OnCvtL9MUwwC3i68bw0BQyjD6i7sEHX+eLLsyE+syCHjhl2+bklNasZa/qmQnbUMGKnm oOWg8u6pkcLFZ2TwqRoShEGnp3mT09ZiQiknK6C6OusQQsJE/qZecAl3hIIB3axKhhc7 akVuza0mHL5cdI4zmAJbHoR9AJEUtZd41lUUY1z9XzqoLDLUl8btn+NljE2N24VPl79W XD+0dOCYXBfH9F2jP7WaxJSAzzXoawLYtcgLmY84wh2FJnhod9dTZj7dHbmkeRSt5fye uL5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=ZOHilDLR; dkim=fail header.i=@chromium.org header.s=google header.b=ilWBVeh9; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d90-v6si2664721pld.92.2018.05.16.07.11.06; Wed, 16 May 2018 07:11:20 -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=fail header.i=@google.com header.s=20161025 header.b=ZOHilDLR; dkim=fail header.i=@chromium.org header.s=google header.b=ilWBVeh9; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752351AbeEPOJu (ORCPT + 99 others); Wed, 16 May 2018 10:09:50 -0400 Received: from mail-vk0-f65.google.com ([209.85.213.65]:42182 "EHLO mail-vk0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752026AbeEPOJs (ORCPT ); Wed, 16 May 2018 10:09:48 -0400 Received: by mail-vk0-f65.google.com with SMTP id j7-v6so564981vkc.9 for ; Wed, 16 May 2018 07:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=tk0EDm/wP6CTy0xk5kU81rrUf8gmkiTOLK8PxjfvVnY=; b=ZOHilDLR1l1En0mQcDE70/NukJ8ojwFFXNxzcD0MM2JJapgLR+lobOeS/TPGyZVnQP qJZ4C2SbZo9oRSUaXevai/eJFrpjtu9P0Jm8yRp378wicDicM9U4x8mfQgSa5Bfj0tOR hW6taoOM/pTPSex4l0QUZkgWRIz4vVSAJqOWzdS9hbmnlBDwlKsLv7QlAxQrzWuzMypq JZ1aIypoSQeLK9PyJtWjFJKornq913Rp6YUGE9v8WdT+UFT696iJ0KMQJ2Q6icm+6c3J rOSZC2hiAsdl9CYwjTo7amT49s1RlVF3XEh6aqPSjO4MV8aqt7SEvr11aKEsBKQ5Scac Kmdg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=tk0EDm/wP6CTy0xk5kU81rrUf8gmkiTOLK8PxjfvVnY=; b=ilWBVeh9tRIB+USLD+VH/WB6g2Nbj2DHxJ09MaIoMlhcMFCh/qiHLmCBP8glDyUjUf SG1fui+dzTz9KSyTY5eXDbkMqDgyW86k9Jz9Ocb/GWefyIFjN+MLQQSYKpo7gx5xAtCx BQZUFgQLwOgP6MEQ8t/EOLF59XtzbZ9AApZB0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=tk0EDm/wP6CTy0xk5kU81rrUf8gmkiTOLK8PxjfvVnY=; b=Vf7mZF8+RzslRs7BQvKjVNxTnf5kBN7zB8EDh0cJJkXCRhR7h1EBSg0Bn9zmNUZSzo dVgA6APz89ibdZDuLokVn1EZHnfujzJ+YuvZwVrDFUri13K5jazkh+TOOeZ5IzPvSFCZ T3Smxwri8YkZ0Zb8HwI4zWbk0DU/rigGIPPOH0XQw9rahloZzwSKFIrqL/neESD/Gtq1 11R9vPW3d7dbmW1xRFJIFkNGgs7wHEPuykE7PZKZ1ECOnKq/9uDk1vjxqXw0e3xC7wxA xPFthVevti0knqebAXOZIgWE8OmFdv/nezoBS/GQCebGmiPCU1ha59fXk9yJ8dqC2Dbu 0wZA== X-Gm-Message-State: ALKqPwdYDeOV+9qOc5LpMcoNLfp3FK+tOPOzlwe2K0DWz9V1+kQVz5gN MSqCv0caUMiSa5S9TkgIYjPkE8FmwBhu0TlVZZFjpQ== X-Received: by 2002:a1f:78a:: with SMTP id 132-v6mr976096vkh.96.1526479787735; Wed, 16 May 2018 07:09:47 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a1f:bd1:0:0:0:0:0 with HTTP; Wed, 16 May 2018 07:09:46 -0700 (PDT) In-Reply-To: <20180515141124.84254-18-namit@vmware.com> References: <20180515141124.84254-1-namit@vmware.com> <20180515141124.84254-18-namit@vmware.com> From: Kees Cook Date: Wed, 16 May 2018 07:09:46 -0700 X-Google-Sender-Auth: CalvUtI-o9jZ6A_ZDtvzltBNi0I Message-ID: Subject: Re: [RFC 8/8] bitops: prevent compiler inline decision distortion To: Nadav Amit Cc: LKML , Nadav Amit , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , X86 ML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 15, 2018 at 7:11 AM, Nadav Amit wrote: > There are several places in the kernel in which there is a condition > that is based on whether the input is known to be constant in > compilation time. If it is, there are complex computations, which only > take place during compilation time. What we found while rewriting the min()/max() defines was that GCC's __builtin_choose_expr() evaluates constant expressions, not the wider possibility of constant values. And the older the GCC the less __builtin_constant_p() would resolve to a constant expression, which would force __builtin_choose_expr() along the "non constant" path. > Although this scheme works correctly, when GCC computes the expected > cost of this code in time and size, it disregards the fact that the > computations of the "constant" case will not take place during runtime > for the non-constant case. The cost of these functions is considered to > be much higher. As a result, inline and branching decisions of the > compiler are distorted. Specifically, functions are less likely to be > inlined due to their preserved big size and execution time. > > One of this cases is test_bit() which performs some computations for > constant inputs. > > The solution is to use __builtin_choose_expr() to detect whether the > input is constant instead of a C condition. GCC evaluates the builtin > earlier, which allows it to improve code-generation decisions. In the case of test_bit(), I *think* all the optimization-desired cases are already only constant expressions, so I think this is safe, but I wouldn't want to do this generally for uses of __builtin_constant_p(). -Kees > > This patch allows function such as bitmap_pos_to_ord() to be inlined. > Its overall effect on size: > > text data bss dec hex filename > 18149165 10064176 2936832 31150173 1db505d ./vmlinux before > 18149210 10064048 2936832 31150090 1db500a ./vmlinux after (-83) > > Static text symbols: > Before: 39643 > After: 39632 (-11) > > Cc: Thomas Gleixner > Cc: Ingo Molnar > Cc: "H. Peter Anvin" > Cc: x86@kernel.org > > Signed-off-by: Nadav Amit > --- > arch/x86/include/asm/bitops.h | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h > index 9f645ba57dbb..f1cb1c9125a9 100644 > --- a/arch/x86/include/asm/bitops.h > +++ b/arch/x86/include/asm/bitops.h > @@ -349,10 +349,10 @@ static __always_inline bool variable_test_bit(long nr, volatile const unsigned l > static bool test_bit(int nr, const volatile unsigned long *addr); > #endif > > -#define test_bit(nr, addr) \ > - (__builtin_constant_p((nr)) \ > - ? constant_test_bit((nr), (addr)) \ > - : variable_test_bit((nr), (addr))) > +#define test_bit(nr, addr) \ > + __builtin_choose_expr(__builtin_constant_p((nr)), \ > + constant_test_bit((nr), (addr)), \ > + variable_test_bit((nr), (addr))) > > /** > * __ffs - find first set bit in word > -- > 2.17.0 > -- Kees Cook Pixel Security