Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3860059imm; Thu, 17 May 2018 16:31:07 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrBKmU0hBPSQMQsDI+0H+xAWauJYxgPRaHsJ2iUgjlBTFpFnYQ2jguKVw2oREkNnAZ5wSsD X-Received: by 2002:a17:902:8a8c:: with SMTP id p12-v6mr6906626plo.94.1526599867174; Thu, 17 May 2018 16:31:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526599867; cv=none; d=google.com; s=arc-20160816; b=OoHxHHUhH0guqtZYj5BhpDbmUiD3VghtQHTWioq2rf2iV+WUELC350xM0GxbVbBzbU BJ1Cu7tGKPd6eFbiDiOQlkrQxwBo5v2dsah/QuFVTQ1Ux6SzTJ9FvxnhhKWuMPnF2gGj e5umRPaACzU3BebZcN9Gyb6Huh5/2Ca5h30Cf6tk4Aq929rcG07d9DiWyVaGZZN5vY5U sbOl1+HuT4jApAu0DVzfjabIRH6eOLaNaCuTlnVu9qggv6l9AzKwRo8FjDh3P330mnaX h9N5UMYSb+PtHYQ1XlEZZT7Mg/QEirm1G+JdXHzsntBPU2ZL+PhNMJO6tGehbA9qnLHG qyPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=UYNVh84Y4ucbd48xhaof3UEOhXHTsPVNsiS3S/lU4gI=; b=t9L2GP0+XhtS7T3ORRGiLkCI2ZqjyyFYjoFyFqw8N/sHcgpghAYV0dC/wpDVo7OC98 iHR4bmRwl42X5578/ml10C9z3cJlxEov0FPR6A4Jvkympwp9V917ctxOTEOVrHNYliSt Ah3bqB6aRAvYgiaBj2FBZSC/HB+yW8GE+yiREswQvuuNBD+Km2kLeCXEmgoa4FLz4u5E 03bYz7SSVK7PAnEjbe4dov21580eBtlAZ2VDk8faKwQEOfR2V5KDrJ0BHpdCIS1UmHhT q17JOJiAOrancwUkyN+N+KYIZ0UrIC+OPRg+paI6nWuWFBmMWAp6MH7Gk7Rpm50bX79t 0XjQ== ARC-Authentication-Results: i=1; mx.google.com; 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 bh1-v6si5897669plb.481.2018.05.17.16.30.52; Thu, 17 May 2018 16:31:07 -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; 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 S1752420AbeEQXaA (ORCPT + 99 others); Thu, 17 May 2018 19:30:00 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:31398 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751649AbeEQX2l (ORCPT ); Thu, 17 May 2018 19:28:41 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Thu, 17 May 2018 16:28:13 -0700 Received: from sc2-haas01-esx0118.eng.vmware.com (sc2-haas01-esx0118.eng.vmware.com [10.172.44.118]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 43F1D4075C; Thu, 17 May 2018 16:28:40 -0700 (PDT) From: Nadav Amit To: , CC: , Nadav Amit , Christopher Li , Subject: [PATCH 1/6] x86: objtool: use asm macro for better compiler decisions Date: Thu, 17 May 2018 09:13:57 -0700 Message-ID: <20180517161402.78089-2-namit@vmware.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180517161402.78089-1-namit@vmware.com> References: <20180517161402.78089-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-001.vmware.com: namit@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org GCC considers the number of statements in inlined assembly blocks, according to new-lines and semicolons, as an indication to the cost of the block in time and space. This data is distorted by the kernel code, which puts information in alternative sections. As a result, the compiler may perform incorrect inlining and branch optimizations. In the case of objtool, this distortion is extreme, since anyhow the annotations of objtool are discarded during linkage. The solution is to set an assembly macro and call it from the inlinedv assembly block. As a result GCC considers the inline assembly block as a single instruction. This patch slightly increases the kernel size. text data bss dec hex filename 18126699 10066728 2936832 31130259 1db0293 ./vmlinux before 18126824 10067268 2936832 31130924 1db052c ./vmlinux after (+665) But allows more aggressive inlining. Static text symbols: Before: 40033 After: 40015 (-18) Cc: Christopher Li Cc: linux-sparse@vger.kernel.org Signed-off-by: Nadav Amit --- include/linux/compiler.h | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index ab4711c63601..6cbabc6b195a 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -97,19 +97,40 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, * These macros help objtool understand GCC code flow for unreachable code. * The __COUNTER__ based labels are a hack to make each instance of the macros * unique, to convince GCC not to merge duplicate inline asm statements. + * + * The annotation logic is encapsulated within assembly macros, which are then + * called on each annotation. This hack is necessary to prevent GCC from + * considering the inline assembly blocks as costly in time and space, which can + * prevent function inlining and lead to other bad compilation decisions. GCC + * computes inline assembly cost according to the number of perceived number of + * assembly instruction, based on the number of new-lines and semicolons in the + * assembly block. Since the annotations will be discarded during linkage, the + * macros make the annotations to be considered "cheap" and let GCC to emit + * better code. */ +asm(".macro __annotate_reachable counter:req\n" + "\\counter:\n\t" + ".pushsection .discard.reachable\n\t" + ".long \\counter\\()b -.\n\t" + ".popsection\n\t" + ".endm"); + #define annotate_reachable() ({ \ - asm volatile("%c0:\n\t" \ - ".pushsection .discard.reachable\n\t" \ - ".long %c0b - .\n\t" \ - ".popsection\n\t" : : "i" (__COUNTER__)); \ + asm volatile("__annotate_reachable %c0" : : "i" (__COUNTER__)); \ }) + +asm(".macro __annotate_unreachable counter:req\n" + "\\counter:\n\t" + ".pushsection .discard.unreachable\n\t" + ".long \\counter\\()b -.\n\t" + ".popsection\n\t" + ".endm"); + #define annotate_unreachable() ({ \ - asm volatile("%c0:\n\t" \ - ".pushsection .discard.unreachable\n\t" \ - ".long %c0b - .\n\t" \ - ".popsection\n\t" : : "i" (__COUNTER__)); \ + asm volatile("__annotate_unreachable %c0" : : \ + "i" (__COUNTER__)); \ }) + #define ASM_UNREACHABLE \ "999:\n\t" \ ".pushsection .discard.unreachable\n\t" \ -- 2.17.0