From patchwork Fri Oct 28 19:40:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 12532 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1023466wru; Fri, 28 Oct 2022 13:15:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7+H3V94r4g+BH2Kjv276wkHVBsfRYhH0Z9RCJdBnLuSw4R6e3YzI++SaF2X+0QujAEiB9a X-Received: by 2002:a63:6b49:0:b0:46a:fcba:308f with SMTP id g70-20020a636b49000000b0046afcba308fmr1124767pgc.8.1666988143733; Fri, 28 Oct 2022 13:15:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666988143; cv=none; d=google.com; s=arc-20160816; b=BIBY+23Zm/2OYeaEDFDoIptP0LUvzrZHqQTqCeAd60DD7SYy/WN26d97T7i1CQEdSc gb9VC9Hpebbev9Qntqas//qIzo/S9XQBtdAlGZwxsnC28GXRkoiJ2pDu/gwB14Un138o JMzpdkareg4TCPYBsxkKgfjAJHU0G9U1LgjxL2Yzp0sPYue7Fdon/AY9K5iCIxlDlcuN wSH+zPbYszWwfn7FgO00TCr4kIgML8D/Vm70+jJwl2ph/nQp6/ckxV5d9+vz8PWRXd7v V8gQ0oIcdmw31/qE4H/hjF7UWxRO1sRFBHVVZvL2jFNH2a4D3KAb4qE0gYKKoz2jg61S VGrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=w2TcGA3omIeq3yxp81jgH+z11BiFQX/+KG2uxbRcnKg=; b=T/jHptytIFJ9u1VGAh+KrpOaK09gQ+WZRTPr3G711fz2p3jBCNX++GOLPZ64gopN4q uLpUpCz4ef7tjnvmodLh1Cnmt+DpCFBWo+VCtbMwkWC7cqtPPvYYRQnUnosWT2m+I65r k6HfU59wGW00OaLKsCLXrEs4LjlpZ8xjcrDtIULukvrRbZn2wOOP3Fweqt7Ge++g8q8V /SeqbWuYEWeohtVQBUZn8pyosohiseagQ+3n54p5IRyjsF/G11H2WxXao3GjQGwpsMnN +YgczEvioKf0dz85agifmoVG1KKHsL5xTY70HMqxpqEhdZHqMU3uAUCyi13OcLiOhXiR obXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=ks+sTiRY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z17-20020a631911000000b00434ac3b9915si6371936pgl.790.2022.10.28.13.15.30; Fri, 28 Oct 2022 13:15:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=ks+sTiRY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230163AbiJ1TsX (ORCPT + 99 others); Fri, 28 Oct 2022 15:48:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230142AbiJ1TsS (ORCPT ); Fri, 28 Oct 2022 15:48:18 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11C6A24419D for ; Fri, 28 Oct 2022 12:48:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=w2TcGA3omIeq3yxp81jgH+z11BiFQX/+KG2uxbRcnKg=; b=ks+sTiRY6cSm9Wr/5Ajk0dRWgD 8FOEc3MhC69/nxeFJfF4MsM5p2kWKC0wMsBYpMgeuLjF4ynIO07RIqoFH10m/rbeHWi7aAjPcH4XL BL93ujKDf988/1YnHhVgktI3/a9FCh3gG44Sp2dT6uAXcGTfGB0X7JnbFnwtbMPXUnVHzi5hmXtjd iqLtkIK/EC2vzuPd3jlzDWNKIXLHwAqwxyGEx4o2T/I6hFbLiWgvf7q6KMMYp/eW0aF5Bvx+PJjO3 +eUjCJW1JFrIw8bbpdV88/LkK9ictwhcmKs/wwt4bIH9/j4C3Pkv0L8PFQlX6SMutqrEXhwsr7+3+ j/QVtvRQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ooVKR-007ARB-1D; Fri, 28 Oct 2022 19:47:43 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id B57CC300322; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 980AB2C6B7F85; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Message-ID: <20221028194453.330970755@infradead.org> User-Agent: quilt/0.66 Date: Fri, 28 Oct 2022 21:40:23 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, djwong@kernel.org, yujie.liu@intel.com, tglx@linutronix.de, jpoimboe@kernel.org, joao.moreira@intel.com, samitolvanen@google.com Subject: [PATCH 1/5] kallsyms: Revert "Take callthunks into account" References: <20221028194022.388521751@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747963760040765245?= X-GMAIL-MSGID: =?utf-8?q?1747963760040765245?= This is a full revert of commit: f1389181622a ("kallsyms: Take callthunks into account") The commit assumes a number of things that are not quite right. Notably it assumes every symbol has PADDING_BYTES in front of it that are not claimed by another symbol. This is not true; even when compiled with: -fpatchable-function-entry=${PADDING_BYTES},${PADDING_BYTES} Notably things like .cold subfunctions do not need to adhere to this change in ABI. It it also not true when build with CFI_CLANG, which claims these PADDING_BYTES in the __cfi_##name symbol. Once the prefix bytes are not consistent and or otherwise claimed the approach this patch takes goes out the window and kallsym resolution will report invalid symbol names. Therefore revert this to make room for another approach. Reported-by: Reported-by: kernel test robot Signed-off-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/r/202210241614.2ae4c1f5-yujie.liu@intel.com --- kernel/kallsyms.c | 45 +++++---------------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -293,12 +293,6 @@ static unsigned long get_symbol_pos(unsi return low; } -#ifdef CONFIG_FUNCTION_PADDING_BYTES -#define PADDING_BYTES CONFIG_FUNCTION_PADDING_BYTES -#else -#define PADDING_BYTES 0 -#endif - /* * Lookup an address but don't bother to find any names. */ @@ -306,25 +300,13 @@ int kallsyms_lookup_size_offset(unsigned unsigned long *offset) { char namebuf[KSYM_NAME_LEN]; - int ret; - - addr += PADDING_BYTES; if (is_ksym_addr(addr)) { get_symbol_pos(addr, symbolsize, offset); - ret = 1; - goto found; + return 1; } - - ret = !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf); - if (!ret) { - ret = !!__bpf_address_lookup(addr, symbolsize, - offset, namebuf); - } -found: - if (ret && offset) - *offset -= PADDING_BYTES; - return ret; + return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) || + !!__bpf_address_lookup(addr, symbolsize, offset, namebuf); } static const char *kallsyms_lookup_buildid(unsigned long addr, @@ -337,8 +319,6 @@ static const char *kallsyms_lookup_build namebuf[KSYM_NAME_LEN - 1] = 0; namebuf[0] = 0; - addr += PADDING_BYTES; - if (is_ksym_addr(addr)) { unsigned long pos; @@ -368,8 +348,6 @@ static const char *kallsyms_lookup_build found: cleanup_symbol_name(namebuf); - if (ret && offset) - *offset -= PADDING_BYTES; return ret; } @@ -396,8 +374,6 @@ int lookup_symbol_name(unsigned long add symname[0] = '\0'; symname[KSYM_NAME_LEN - 1] = '\0'; - addr += PADDING_BYTES; - if (is_ksym_addr(addr)) { unsigned long pos; @@ -425,8 +401,6 @@ int lookup_symbol_attrs(unsigned long ad name[0] = '\0'; name[KSYM_NAME_LEN - 1] = '\0'; - addr += PADDING_BYTES; - if (is_ksym_addr(addr)) { unsigned long pos; @@ -443,8 +417,6 @@ int lookup_symbol_attrs(unsigned long ad return res; found: - if (offset) - *offset -= PADDING_BYTES; cleanup_symbol_name(name); return 0; } @@ -470,15 +442,8 @@ static int __sprint_symbol(char *buffer, len = strlen(buffer); offset -= symbol_offset; - if (add_offset) { - char s = '+'; - - if ((long)offset < 0) { - s = '-'; - offset = 0UL - offset; - } - len += sprintf(buffer + len, "%c%#lx/%#lx", s, offset, size); - } + if (add_offset) + len += sprintf(buffer + len, "+%#lx/%#lx", offset, size); if (modname) { len += sprintf(buffer + len, " [%s", modname); From patchwork Fri Oct 28 19:40:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 12527 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1014382wru; Fri, 28 Oct 2022 12:55:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7qI0RAWVT+Ra1D2ktfTgudaDQDLxU4pexuRd7w+7CHrG1jICZttuJo/PdRC9GKmW5DLxXK X-Received: by 2002:a17:906:7313:b0:78e:c2a:a3fa with SMTP id di19-20020a170906731300b0078e0c2aa3famr836267ejc.581.1666986956686; Fri, 28 Oct 2022 12:55:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666986956; cv=none; d=google.com; s=arc-20160816; b=tWBO1p6aKhhUH2rIpwMQMkDMREMP+bF1Kfj4VajM2hEK8wcG6izY1V6956sar1jdqb Q+JAnM6vBBfvZqo6Ud2WfPw0LrHClpoNsjbgIui58n7jfF9nQnIyJAfI+w1WIvD3Yqy+ XqyCOt71BeftCti03TmqScn8dUR2quOilKLdZikfKwc6hlvPeX1KSlQYil5F2Wwr8bxg XhikJyDGKkDkyQKUofg77jM7V+UiMpVyXEI+jl9hib5k4u8rkSPEQ0ylX6z8IaEQhlzz 2OrVDjiwMdasar9Ss1AEewtc8zLsQOAkmg+HDKIDNnE+P6csltxT2wDYoVKwqYEf4OLH ikdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=IORdw0i358+WCHTJMo8FHxkOZ204c2jlWb6nUgvX6XI=; b=SDQM3f04gGEG+InP+3rGFOCShKV28AZmqMqsvN/YXl8ZYOYyl7qT3l74M30Ml16Bf0 tzyOCEDo/fogJCReSWT5xjECvLAEzK+UU/Ooa+8WBPE+e2ksJGZj5PzZxEImPg4JJXkB Gmmxr/2zhU6P4fwMOTjYH4vFe5gVrgVI2uS4cPYM5nvQ8W5WPYKZUvU1/GdpI+K/JDKo TfSB9tIT88fNQOTn1SCe1tPB3ATE/WU1wvKkP7Du82GkfvElUWGpccpg5jeb06A7m736 pKy2EHna7votuUSN2z26Ms+CoylKBIdgUsqPuHnLIatrge3sa79dMyH9BGin5MQlKPvE SQJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=mHVGpEbm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h15-20020a1709063b4f00b007826cb6f57csi566847ejf.407.2022.10.28.12.55.33; Fri, 28 Oct 2022 12:55:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=mHVGpEbm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230136AbiJ1TsG (ORCPT + 99 others); Fri, 28 Oct 2022 15:48:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230002AbiJ1Trz (ORCPT ); Fri, 28 Oct 2022 15:47:55 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AED6A241B07 for ; Fri, 28 Oct 2022 12:47:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=IORdw0i358+WCHTJMo8FHxkOZ204c2jlWb6nUgvX6XI=; b=mHVGpEbm8sOcQ7hwd9786o/oxg p0whUxIGOUwNbBtGadYe5jv9R6b0FVQTQZ31Y0chVZi2yOPZeuFHU2k6PMkVd2WCCfp/DpXjArrDN n21ePo41DzyAu18b+ipSYfhhDicimP6xlfVzqsGDBmAdy6N9YcDq1c3svOG8zNwOWqyvVEe/RI6FZ 2UHw4VGe31/g6jdAx3jSJY6jEPN5pCwU+6suxlXpuykF1znt84zgQGaiXOQcLbAmOm989AO4Mdy7k d61HL9Y6l9tFWVqkCMmyPW4BeqtYIWIhru1d9GO6siabvRPt4lTHoPIHBIkGy1DKbEg/cZrskSKUG XNuSXtdQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ooVKV-001V5n-VM; Fri, 28 Oct 2022 19:47:48 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id B74FF30035C; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 99F592C6DAB20; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Message-ID: <20221028194453.396634875@infradead.org> User-Agent: quilt/0.66 Date: Fri, 28 Oct 2022 21:40:24 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, djwong@kernel.org, yujie.liu@intel.com, tglx@linutronix.de, jpoimboe@kernel.org, joao.moreira@intel.com, samitolvanen@google.com Subject: [PATCH 2/5] objtool: Slice up elf_create_section_symbol() References: <20221028194022.388521751@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747962514868072734?= X-GMAIL-MSGID: =?utf-8?q?1747962514868072734?= In order to facilitate creation of more symbol types, slice up elf_create_section_symbol() to extract a generic helper that deals with adding ELF symbols. Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/elf.c | 56 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -717,11 +717,11 @@ static int elf_update_symbol(struct elf } static struct symbol * -elf_create_section_symbol(struct elf *elf, struct section *sec) +__elf_create_symbol(struct elf *elf, struct symbol *sym) { struct section *symtab, *symtab_shndx; Elf32_Word first_non_local, new_idx; - struct symbol *sym, *old; + struct symbol *old; symtab = find_section_by_name(elf, ".symtab"); if (symtab) { @@ -731,27 +731,16 @@ elf_create_section_symbol(struct elf *el return NULL; } - sym = calloc(1, sizeof(*sym)); - if (!sym) { - perror("malloc"); - return NULL; - } + new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize; - sym->name = sec->name; - sym->sec = sec; - - // st_name 0 - sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION); - // st_other 0 - // st_value 0 - // st_size 0 + if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL) + goto non_local; /* * Move the first global symbol, as per sh_info, into a new, higher * symbol index. This fees up a spot for a new local symbol. */ first_non_local = symtab->sh.sh_info; - new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize; old = find_symbol_by_index(elf, first_non_local); if (old) { old->idx = new_idx; @@ -769,18 +758,43 @@ elf_create_section_symbol(struct elf *el new_idx = first_non_local; } + /* + * Either way, we will add a LOCAL symbol. + */ + symtab->sh.sh_info += 1; + +non_local: sym->idx = new_idx; if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { WARN("elf_update_symbol"); return NULL; } - /* - * Either way, we added a LOCAL symbol. - */ - symtab->sh.sh_info += 1; + return sym; +} + +static struct symbol * +elf_create_section_symbol(struct elf *elf, struct section *sec) +{ + struct symbol *sym = calloc(1, sizeof(*sym)); + + if (!sym) { + perror("malloc"); + return NULL; + } + + sym->name = sec->name; + sym->sec = sec; + + // st_name 0 + sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION); + // st_other 0 + // st_value 0 + // st_size 0 - elf_add_symbol(elf, sym); + sym = __elf_create_symbol(elf, sym); + if (sym) + elf_add_symbol(elf, sym); return sym; } From patchwork Fri Oct 28 19:40:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 12526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1014125wru; Fri, 28 Oct 2022 12:55:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6GPLuknoN0CL6uGKZ6ichIDoWzeGiFyUimJq1sMAFP/ZEqoyYn4dFUuSG4Gp0jwu8J4VlW X-Received: by 2002:a17:907:b07:b0:7a8:beb3:aa2a with SMTP id h7-20020a1709070b0700b007a8beb3aa2amr883927ejl.459.1666986911118; Fri, 28 Oct 2022 12:55:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666986911; cv=none; d=google.com; s=arc-20160816; b=uHXF8XXZBn/lruQP5t+BBCI6Bz7McvA3nF5O5lNViIHxWkycAGkZi1E4idBDlK9jGt Ukw1EVwfqKre6gYpsNPFbqWGYKLdOBgNBgBb1mghReeICf9ddPTAKWmW5h2wc1FmDHGz HBCG1gPpQf3S/8wECWNin1KL3Vp0+DiSkhAZQtU/zQ+RdhkKOVr5nTkYwGJ2y/dOoudQ t8VOS8jpB9I+51F0PZla+FrT2hciONOajQ2N8peAtT5LJlX1rDmYHeIMZz+3uxD02qHI 0TL9BAUhAt38kKeoYX4dklUYcfgmAYcwU197nU9SmlHlo5jjIxVigg4dfFOBD0UShy9/ mnXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=3QcO6i1OADBJrO89uFWVOZLcBxalIqOKw3AE7W+Oknw=; b=lxtfMGLFrwX2x/IBbWM2EffyJh/Kc4Hgvc6g9NzWdO7XUeqXkyjh9wGyMaPybzvTPk uktGITxZt1uhgrX+UUBqzvCfh7AewIx5q5AqLMZQYNO7x+0zU1VA7RoEShnr5E2Y0Mhe yzzoEtgyR1RlzeMEfLaI4vdo17lWlIypv44A7/9MDgxMWtGRMlv+rGY/8XyrxhX2dDl3 AqjowcwykUFYjAVE4+zEj5VPpUpq/G9awUuCMIm7lmczFBGRsHbWhDUrYAgeyaggm+iR HwcESPt0+em8JQ02zGgX+98uervRS2Mh0KjCEHcFPZj6CWqCDlxbqBRiEfLesdGDKHeO B4Sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=unaldPb3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xi9-20020a170906dac900b00782e1a663dcsi5760548ejb.435.2022.10.28.12.54.46; Fri, 28 Oct 2022 12:55:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=unaldPb3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230108AbiJ1Tr5 (ORCPT + 99 others); Fri, 28 Oct 2022 15:47:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbiJ1Trz (ORCPT ); Fri, 28 Oct 2022 15:47:55 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78069241B37 for ; Fri, 28 Oct 2022 12:47:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=3QcO6i1OADBJrO89uFWVOZLcBxalIqOKw3AE7W+Oknw=; b=unaldPb3HwjbCtO0iBmuGZTk6j juei5a08VijPiQrm129/7iCtv3PUdfkDT4r4emLHSA8PKltscoNN2VYUewY2eG/k0gbbTIvxyWXTK +pWLutlNYuznSLqIlz8sv6PdRv+++S9LYb2GUhA69JqylEY1OXW55EgSsX6ANz7hbW1u74qOGd4j3 GmtIrPhP1EQhaV4HP64GxnKfZfU77aXsRB/qE1sS26zfagxhP87AHpKXfY/EqIaI5gJtHTT/0SaSi PCiL1A1q5QOfVoH73H6y1ihD8g1PZb/RqRIf3nxDvRQCYC5Hkr0WgG0pdoZizfWjdILDZiEJaCcvP EAs2VNLQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ooVKV-001V5p-VU; Fri, 28 Oct 2022 19:47:48 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id B90B7300872; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 9F8672B9B03D1; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Message-ID: <20221028194453.461658986@infradead.org> User-Agent: quilt/0.66 Date: Fri, 28 Oct 2022 21:40:25 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, djwong@kernel.org, yujie.liu@intel.com, tglx@linutronix.de, jpoimboe@kernel.org, joao.moreira@intel.com, samitolvanen@google.com Subject: [PATCH 3/5] objtool: Avoid O(bloody terrible) behaviour -- an ode to libelf References: <20221028194022.388521751@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747962466966661765?= X-GMAIL-MSGID: =?utf-8?q?1747962466966661765?= Due to how gelf_update_sym*() requires an Elf_Data pointer, and how libelf keeps Elf_Data in a linked list per section, elf_update_symbol() ends up having to iterate this list on each update to find the correct Elf_Data for the index'ed symbol. By allocating one Elf_Data per new symbol, the list grows per new symbol, giving an effective O(n^2) insertion time. This is obviously bloody terrible. Therefore over-allocate the Elf_Data when an extention is needed. Except it turns out libelf disregards Elf_Scn::sh_size in favour of the sum of Elf_Data::d_size. IOW it will happily write out all the unused space and fill it with: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND entries (aka zeros). Which obviously violates the STB_LOCAL placement rule, and is a general pain in the backside for not being the desired behaviour. Manually fix-up the Elf_Data size to avoid this problem before calling elf_update(). This significantly improves performance when adding a significant number of symbols. Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/elf.c | 89 +++++++++++++++++++++++++++++++++--- tools/objtool/include/objtool/elf.h | 2 2 files changed, 84 insertions(+), 7 deletions(-) --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -634,6 +634,12 @@ static int elf_update_symbol(struct elf /* end-of-list */ if (!symtab_data) { + /* + * Over-allocate to avoid O(n^2) symbol creation + * behaviour. The down side is that libelf doesn't + * like this; see elf_truncate_section() for the fixup. + */ + int num = max(1U, sym->idx/3); void *buf; if (idx) { @@ -647,28 +653,34 @@ static int elf_update_symbol(struct elf if (t) shndx_data = elf_newdata(t); - buf = calloc(1, entsize); + buf = calloc(num, entsize); if (!buf) { WARN("malloc"); return -1; } symtab_data->d_buf = buf; - symtab_data->d_size = entsize; + symtab_data->d_size = num * entsize; symtab_data->d_align = 1; symtab_data->d_type = ELF_T_SYM; - symtab->sh.sh_size += entsize; symtab->changed = true; + symtab->truncate = true; if (t) { - shndx_data->d_buf = &sym->sec->idx; - shndx_data->d_size = sizeof(Elf32_Word); + buf = calloc(num, sizeof(Elf32_Word)); + if (!buf) { + WARN("malloc"); + return -1; + } + + shndx_data->d_buf = buf; + shndx_data->d_size = num * sizeof(Elf32_Word); shndx_data->d_align = sizeof(Elf32_Word); shndx_data->d_type = ELF_T_WORD; - symtab_shndx->sh.sh_size += sizeof(Elf32_Word); symtab_shndx->changed = true; + symtab_shndx->truncate = true; } break; @@ -770,6 +782,14 @@ __elf_create_symbol(struct elf *elf, str return NULL; } + symtab->sh.sh_size += symtab->sh.sh_entsize; + symtab->changed = true; + + if (symtab_shndx) { + symtab_shndx->sh.sh_size += sizeof(Elf32_Word); + symtab_shndx->changed = true; + } + return sym; } @@ -1286,6 +1306,60 @@ int elf_write_reloc(struct elf *elf, str return 0; } +/* + * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size + * do you: + * + * A) adhere to the section header and truncate the data, or + * B) ignore the section header and write out all the data you've got? + * + * Yes, libelf sucks and we need to manually truncate if we over-allocate data. + */ +static int elf_truncate_section(struct elf *elf, struct section *sec) +{ + u64 size = sec->sh.sh_size; + bool truncated = false; + Elf_Data *data = NULL; + Elf_Scn *s; + + s = elf_getscn(elf->elf, sec->idx); + if (!s) { + WARN_ELF("elf_getscn"); + return -1; + } + + for (;;) { + /* get next data descriptor for the relevant section */ + data = elf_getdata(s, data); + + if (!data) { + if (size) { + WARN("end of section data but non-zero size left\n"); + return -1; + } + return 0; + } + + if (truncated) { + /* when we remove symbols */ + WARN("truncated; but more data\n"); + return -1; + } + + if (!data->d_size) { + WARN("zero size data"); + return -1; + } + + if (data->d_size > size) { + truncated = true; + data->d_size = size; + } + + size -= data->d_size; + } +} + int elf_write(struct elf *elf) { struct section *sec; @@ -1296,6 +1370,9 @@ int elf_write(struct elf *elf) /* Update changed relocation sections and section headers: */ list_for_each_entry(sec, &elf->sections, list) { + if (sec->truncate) + elf_truncate_section(elf, sec); + if (sec->changed) { s = elf_getscn(elf->elf, sec->idx); if (!s) { --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -38,7 +38,7 @@ struct section { Elf_Data *data; char *name; int idx; - bool changed, text, rodata, noinstr, init; + bool changed, text, rodata, noinstr, init, truncate; }; struct symbol { From patchwork Fri Oct 28 19:40:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 12530 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1021506wru; Fri, 28 Oct 2022 13:10:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5K/K+qHVsons4qNI/ASx67EvE96PJOBZQAbAXgzD72+3WuBNuqYogI3zidUor2DAyXaEiG X-Received: by 2002:a65:5886:0:b0:439:8dd3:18d4 with SMTP id d6-20020a655886000000b004398dd318d4mr1061509pgu.430.1666987855909; Fri, 28 Oct 2022 13:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666987855; cv=none; d=google.com; s=arc-20160816; b=xHAbe0pcZT0Ouc6ySgsG9hFg1JT5sAtS7frTdDhoBJdvLw4UE9HYil0BJgCQd2tq9a lc+pcdNwtqVo8+D9j/8gEQK0rVL7Zf8g0uDrMorycyVWsXeTTogx0KgLVCtAVveWEE5x LeEHc/WZula3UPPguwIgrpdxuRoG3WDrIHtQU4ERsjp2VpYGvjvNgEHDwKloMr6mmPr3 biz9Ohpnvc1q0FcWurxkTkiTqubY5Ve1Jy/ACMmvzRavIFw98LcHfZBdKQ/MIfcf3Cya BXJr1MVzIRwDSUzNBBAqUXVcXcXjOxo0xZbysfIwmmywMLBXQ1/i03GdlzyDJk9fEW/7 g+BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=z2Eui6jBMsF3Ela2LYf1dY1EBM7vCZblnAbR38nKIkI=; b=zs+yRz0UBfn2SkIhxKfhxyQ2E72SDuP6A7iAU6u5jaamSAwgSgLEoIZYMGZLBsw/s6 zcyb/w9GydhCVguJTKH8r0HCnj1sgKKl+MWc48vkLMX7oEJMB6FwXYEZGTvZXTSWcMwa W4VohIMR8DN1VQ9JpWd2F+szHRuiP+dNPMZMdwCBAGfVMS8+lqJSkF9bzWFTYMvRXEnq GXz4aB47baZ+DtMTv9MI4KDy6tK1RcxgKeR91u3tb7HUvgeXdOey5qLz+N1RTVGBM8TP JoKArkT1iaKAysa/cTFRSdCYMmevG5jaETL0BFg+76pUBGwcs3PyAWFHlPCJhJ7BuRkB oeLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=BVqYFG1e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c12-20020a170902b68c00b0017ef8bf8a91si5819151pls.439.2022.10.28.13.10.40; Fri, 28 Oct 2022 13:10:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=BVqYFG1e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230142AbiJ1Ts0 (ORCPT + 99 others); Fri, 28 Oct 2022 15:48:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbiJ1TsS (ORCPT ); Fri, 28 Oct 2022 15:48:18 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11EC52441A3 for ; Fri, 28 Oct 2022 12:48:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=z2Eui6jBMsF3Ela2LYf1dY1EBM7vCZblnAbR38nKIkI=; b=BVqYFG1epHvfEzIIJb0n58krM9 xnJTzqfsYLp/JBl9vrfbgqtBjvzlBYQybmClTjleQ0/gC+pgAFkRaFphTPExqCrYN+dXAjIe9isRM K09mxSJKJPhNe9iTfcsZMTiWLVQlGsQW7B8WC2g0WeBuOb8FqPCSsXfLB3l3rxdXyegK2w/bwmz6k mx4YSscqMTPvHDPfg2Gt9jBpfm0+SKncvir8OnW7z8nI88YiycDbTocKrevtF4ZV2eBUXbRaO7EZO 4Bchr3inxodksr2ZlK62noDmzNgrPPNWy6YoJst3TMhAaTAs07W7JJ5b4Yc+CnfdPRigs2QoWn3qJ Hm2cvT/A==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ooVKR-007ARC-1D; Fri, 28 Oct 2022 19:47:43 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id BD18B300BAE; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id A37982C6DAB2D; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Message-ID: <20221028194453.526899822@infradead.org> User-Agent: quilt/0.66 Date: Fri, 28 Oct 2022 21:40:26 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, djwong@kernel.org, yujie.liu@intel.com, tglx@linutronix.de, jpoimboe@kernel.org, joao.moreira@intel.com, samitolvanen@google.com Subject: [PATCH 4/5] objtool: Add option to generate prefix symbols References: <20221028194022.388521751@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747963458393805865?= X-GMAIL-MSGID: =?utf-8?q?1747963458393805865?= When code is compiled with: -fpatchable-function-entry=${PADDING_BYTES},${PADDING_BYTES} functions will have PADDING_BYTES of NOP in front of them. Unwinders and other things that symbolize code locations will typically attribute these bytes to the preceding function. Given that these bytes nominally belong to the following symbol this mis-attribution is confusing. Inspired by the fact that CFI_CLANG emits __cfi_##name symbols to claim these bytes, allow objtool to emit __pfx_##name symbols to do the same. Therefore add the objtool --prefix=N argument, to conditionally place a __pfx_##name symbol at N bytes ahead of symbol 'name' when: all these preceding bytes are NOP and name-N is an instruction boundary. Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/builtin-check.c | 1 + tools/objtool/check.c | 27 +++++++++++++++++++++++++++ tools/objtool/elf.c | 30 ++++++++++++++++++++++++++++++ tools/objtool/include/objtool/builtin.h | 1 + tools/objtool/include/objtool/elf.h | 2 ++ 5 files changed, 61 insertions(+) --- a/tools/objtool/builtin-check.c +++ b/tools/objtool/builtin-check.c @@ -75,6 +75,7 @@ const struct option check_options[] = { OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"), OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"), OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"), + OPT_INTEGER(0, "prefix", &opts.prefix, "generate prefix symbols"), OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"), OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"), OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"), --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -3972,6 +3972,31 @@ static bool ignore_unreachable_insn(stru return false; } +static int add_prefix_symbol(struct objtool_file *file, struct symbol *func, + struct instruction *insn) +{ + if (!opts.prefix) + return 0; + + for (;;) { + struct instruction *prev = list_prev_entry(insn, list); + + if (&prev->list == &file->insn_list) + break; + + if (prev->type != INSN_NOP) + break; + + insn = prev; + if (func->offset - prev->offset == opts.prefix) { + elf_create_prefix_symbol(file->elf, func, -opts.prefix); + break; + } + } + + return 0; +} + static int validate_symbol(struct objtool_file *file, struct section *sec, struct symbol *sym, struct insn_state *state) { @@ -3990,6 +4015,8 @@ static int validate_symbol(struct objtoo if (!insn || insn->ignore || insn->visited) return 0; + add_prefix_symbol(file, sym, insn); + state->uaccess = sym->uaccess_safe; ret = validate_branch(file, insn_func(insn), insn, *state); --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -819,6 +819,36 @@ elf_create_section_symbol(struct elf *el return sym; } +static int elf_add_string(struct elf *elf, struct section *strtab, char *str); + +struct symbol * +elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long addend) +{ + struct symbol *sym = calloc(1, sizeof(*sym)); + size_t namelen = strlen(orig->name) + sizeof("__pfx_"); + char *name = malloc(namelen); + + if (!sym || !name) { + perror("malloc"); + return NULL; + } + + snprintf(name, namelen, "__pfx_%s", orig->name); + + sym->name = name; + sym->sec = orig->sec; + + sym->sym.st_name = elf_add_string(elf, NULL, name); + sym->sym.st_info = orig->sym.st_info; + sym->sym.st_value = orig->sym.st_value + addend; + + sym = __elf_create_symbol(elf, sym); + if (sym) + elf_add_symbol(elf, sym); + + return sym; +} + int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int type, struct section *insn_sec, unsigned long insn_off) --- a/tools/objtool/include/objtool/builtin.h +++ b/tools/objtool/include/objtool/builtin.h @@ -26,6 +26,7 @@ struct opts { bool stackval; bool static_call; bool uaccess; + int prefix; /* options: */ bool backtrace; --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -146,6 +146,8 @@ static inline bool has_multiple_files(st struct elf *elf_open_read(const char *name, int flags); struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr); +struct symbol *elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long addend); + int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, unsigned int type, struct symbol *sym, s64 addend); int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, From patchwork Fri Oct 28 19:40:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 12528 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1014387wru; Fri, 28 Oct 2022 12:55:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Fxu5fBx51wk7Iw3yLgrS3KmYtK3/gVzjF9d93xzcD/lXlqU3JZxwpRb5OnZlo7LPpmfc1 X-Received: by 2002:a05:6402:42c7:b0:461:bea2:8a70 with SMTP id i7-20020a05640242c700b00461bea28a70mr1077536edc.261.1666986957863; Fri, 28 Oct 2022 12:55:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666986957; cv=none; d=google.com; s=arc-20160816; b=vPfW4ju/e8S7t7/802QHeYXbUzlNI/DnUiOH/EM+UuaOnkOV3s69ygPID1UpT4E2hM 9SHmfPZEys0N9neAdCQFcbNVDO+3RXGWC+htMUvFLYCPEmO/dx9dZFCAMfsk+OUWODWP 86pTuORwEiJvc+IBjUwnY+3v3MvAkEVvAORxHJUxXwI6x7vxzuVDLgV1ouaIZQr13ZHg g+t5+tZ6cZVyaWPxpT3VJH/P7drQCdrIEHCORgsTXf+1pebouHgEb+ticjJzMHTkS3Tt FAD10gEH60RChDT2r8w1Dw+QV72DHQO+UU0ZBEEBOMW1uuXK50crwLVlda9mg2ShgLqz yQ/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:subject:cc:to:from:date :user-agent:message-id:dkim-signature; bh=dw2+KyAKk/Vgq7gh3A5Vj3COoPkFBdj0Z6avPUlckh4=; b=Vle++BJQhYhhUW9Wbe+WfQXK1KRTYrfgA1UdUB77UrzYIWp53SuJ80VKaUQJ+GhT7n sAdaP4k+AhAAeNjm2E7q5ox2t+t+mKCBy78cIaT82RFpidmLehda+qXwQg5Kj9UxTgGR 2uzWokoFbOpkgDlKbdk8kB+5TRgnSApAKQqA89FP8VZLep+0tiVZKIrIaXElBoSL473m CeO3k0YB8R8mvBMjwdsh8ecrAf9KCMRkcbdzwk4N0HnyQAgDy9qEEEhhs0ozqHJ42TXE lNkO3/thB8FopqQXFHL0eKf46TKDO6UsuMCdD9zyV5ymaIIrH8acSGbMnQnUee0AvVRi Rc/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=qeQgNOm1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t6-20020a056402524600b004574154f09asi1173248edd.529.2022.10.28.12.55.34; Fri, 28 Oct 2022 12:55:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=qeQgNOm1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230151AbiJ1TsK (ORCPT + 99 others); Fri, 28 Oct 2022 15:48:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229946AbiJ1Trz (ORCPT ); Fri, 28 Oct 2022 15:47:55 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76BD6241B35 for ; Fri, 28 Oct 2022 12:47:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=dw2+KyAKk/Vgq7gh3A5Vj3COoPkFBdj0Z6avPUlckh4=; b=qeQgNOm135IQHespPW8tvobp0V xikJ7xB2qqPBPAkaBL+pXNXryXDzY40SdFMIATyVZY1JFlOA6t9rDpCCgLGBBIArPoXgT/POmZ7DT AewooojZW7SNctC8ZLtyopjhGxrOmc4I9PMiiwO3O2sNpz5oRqZTyHgS2GjvkZlPbUqCUAaqBM8DV 4zd6qANrbAaxe06rPPKQ2QJ+jwH62H44tVnTZU7+wXh/N4doeluoBp69JNo6Tlf/PgV5d/wj0Lnyj uH++5Q5Qy4/2Wj5P4dt2SM5aBTdgrcfVOQ2fgd4yqYGW8neDvfi//ilCMsWPX5oTpni4S3yt0SQrr JaTR6KGw==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ooVKW-001V60-V0; Fri, 28 Oct 2022 19:47:49 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 20B90300C19; Fri, 28 Oct 2022 21:47:42 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id A7BBD2C6DAB2E; Fri, 28 Oct 2022 21:47:41 +0200 (CEST) Message-ID: <20221028194453.592512209@infradead.org> User-Agent: quilt/0.66 Date: Fri, 28 Oct 2022 21:40:27 +0200 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, djwong@kernel.org, yujie.liu@intel.com, tglx@linutronix.de, jpoimboe@kernel.org, joao.moreira@intel.com, samitolvanen@google.com Subject: [PATCH 5/5] x86: Add prefix symbols for function padding References: <20221028194022.388521751@infradead.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747962516634290289?= X-GMAIL-MSGID: =?utf-8?q?1747962516634290289?= When code is compiled with: -fpatchable-function-entry=${PADDING_BYTES},${PADDING_BYTES} functions will have PADDING_BYTES of NOP in front of them. Unwinders and other things that symbolize code locations will typically attribute these bytes to the preceding function. Given that these bytes nominally belong to the following symbol this mis-attribution is confusing. Inspired by the fact that CFI_CLANG emits __cfi_##name symbols to claim these bytes, use objtool to emit __pfx_##name symbols to do the same when CFI_CLANG is not used. This then shows the callthunk for symbol 'name' as: __pfx_##name+0x6/0x10 Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/Kconfig | 4 ++++ scripts/Makefile.lib | 1 + 2 files changed, 5 insertions(+) --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2472,6 +2472,10 @@ config CALL_THUNKS def_bool n select FUNCTION_ALIGNMENT_16B +config PREFIX_SYMBOLS + def_bool y + depends on CALL_THUNKS && !CFI_CLANG + menuconfig SPECULATION_MITIGATIONS bool "Mitigations for speculative execution vulnerabilities" default y --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -265,6 +265,7 @@ objtool-args-$(CONFIG_STACK_VALIDATION) objtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE) += --static-call objtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION) += --uaccess objtool-args-$(CONFIG_GCOV_KERNEL) += --no-unreachable +objtool-args-$(CONFIG_PREFIX_SYMBOLS) += --prefix=$(CONFIG_FUNCTION_PADDING_BYTES) objtool-args = $(objtool-args-y) \ $(if $(delay-objtool), --link) \ From patchwork Wed Nov 2 21:46:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Zijlstra X-Patchwork-Id: 14496 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp163779wru; Wed, 2 Nov 2022 15:00:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4+9WFgjBL1Y3bdS3XTfPO7RcSGyRCNX7TpUX0HE/ZLy9i2Vi60CC3p9kYM4Z7Q6RGesZ/o X-Received: by 2002:a05:6402:28d:b0:463:3509:76af with SMTP id l13-20020a056402028d00b00463350976afmr21107565edv.226.1667426458257; Wed, 02 Nov 2022 15:00:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667426458; cv=none; d=google.com; s=arc-20160816; b=zlmc7lxZ3IAt5yf99kEUiOniTtW2NGld8Pza8rMCVGkdQ7UrESM0mKpwYcsHBblFPy Gmyw75eQx+aAdvm61CJJN6ZDOzJ9oKIs3dDZqhjeaeZCsmicWF2jKRiwfN+E5/skR6pW kUH86D2HVZuC6kf9xH4cTRfe4HHXZi4XgNUjOvgHSjQpfBFXNL9XVL5EFnzM4fONBYN1 28BKZxOdM+baIxNHtpAP4PsuGnj7+QOVgaaUJ3cTAjghOspVIhUNT7W0XG1ksPba2lNr ynMqVZ0HPHoumlfp2ApdP4vqsI5E5YJP4l9HNefTZJ+kL99534JyDiyqzMD61MhLQTWp aSng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=PAqLk4vbz/jMyf0acd8AaUtbY70n/pCagLl/I8+aAy4=; b=mvoHV6uGBka2dXjhj1oyRwu3II7g9ZnjGoGrkDUwqtIXMuvBH3vq8Mx+EvRtee5RzO MpNzEefGDCoXfNkovBvhRu3to9xR6q/ZdklqwuNU6idS//h+1noysFLUMIfsfosxjRH5 S12llTbqjGw3dA5iNGnTi3FziAY9cxUiU4wecngF9P2+hKNZtsVj2HNISPkSzPxVAuh2 JEDxkaLLCklAt8CRa7iWkdnCKAkPrrofMGUToBI4QmCj6UTH/jmv4njTEneIoOD4u1H0 vNsJzJ71xk2wXzdJgWRa6JObeVgnvVmC3fQvrvv9/xO6r+zpR79xu6f48w1YSFzTxKrR Pcqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=Mlqf86uj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t4-20020a056402524400b0045dbfc32399si20353795edd.552.2022.11.02.15.00.34; Wed, 02 Nov 2022 15:00:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=Mlqf86uj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231307AbiKBVrI (ORCPT + 99 others); Wed, 2 Nov 2022 17:47:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230315AbiKBVrG (ORCPT ); Wed, 2 Nov 2022 17:47:06 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8478AE0C0 for ; Wed, 2 Nov 2022 14:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=PAqLk4vbz/jMyf0acd8AaUtbY70n/pCagLl/I8+aAy4=; b=Mlqf86ujjgIDW8LACKKsN0DHlm UE/XS328m2Mynx6qIbbIMNH7OMcdrWxt1NG/yi2z8JTyqZAmBPPs6AOxF/FD/MwVvj35nvy4gdk9n vat5p8hyus8Q3AGatMGa6evGstcpSAOYACyj2J4oVg5iUD+gqQD0HYdN85JfhudFs3E64QY7cmn3Q C3GIL3A5aBislzBCL5ULSUHc7IjQqiJw1CsUCoRfGathVddINOSktcpEFfJ4IpzuUxxvkeWBMZPSE T85+0KXqR9+sNisVISO+i+m/lTBBOABPaGL0HvU83unLPNmTcjekGgMN1J0CJbe8CO9FrIJDDFDlk MN3S3f3A==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1oqLZO-008W7N-Cu; Wed, 02 Nov 2022 21:46:47 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id D1282300130; Wed, 2 Nov 2022 22:46:44 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id ADC7C20B23194; Wed, 2 Nov 2022 22:46:44 +0100 (CET) Date: Wed, 2 Nov 2022 22:46:44 +0100 From: Peter Zijlstra To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, djwong@kernel.org, yujie.liu@intel.com, tglx@linutronix.de, jpoimboe@kernel.org, joao.moreira@intel.com, samitolvanen@google.com Subject: [PATCH 6/5] objtool: Optimize elf_dirty_reloc_sym() Message-ID: References: <20221028194022.388521751@infradead.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221028194022.388521751@infradead.org> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748423365765089813?= X-GMAIL-MSGID: =?utf-8?q?1748423365765089813?= Subject: objtool: Optimize elf_dirty_reloc_sym() From: Peter Zijlstra Date: Wed Nov 2 22:31:19 CET 2022 When moving a symbol in the symtab its index changes and any reloc referring that symtol-table-index will need to be rewritten too. In order to facilitate this, objtool simply marks the whole reloc section 'changed' which will cause the whole section to be re-generated. However, finding the relocs that use any given symbol is implemented rather crudely -- a fully iteration of all sections and their relocs. Given that some builds have over 20k sections (kallsyms etc..) iterating all that for *each* symbol moved takes a bit of time. Instead have each symbol keep a list of relocs that reference it. This *vastly* improves build times for certain configs. Reported-by: Borislav Petkov Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/elf.c | 27 ++++++++++----------------- tools/objtool/include/objtool/elf.h | 2 ++ 2 files changed, 12 insertions(+), 17 deletions(-) --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -356,6 +356,7 @@ static void elf_add_symbol(struct elf *e struct rb_node *pnode; struct symbol *iter; + INIT_LIST_HEAD(&sym->reloc_list); INIT_LIST_HEAD(&sym->pv_target); sym->alias = sym; @@ -557,6 +558,7 @@ int elf_add_reloc(struct elf *elf, struc reloc->sym = sym; reloc->addend = addend; + list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); list_add_tail(&reloc->list, &sec->reloc->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); @@ -573,21 +575,10 @@ int elf_add_reloc(struct elf *elf, struc */ static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) { - struct section *sec; - - list_for_each_entry(sec, &elf->sections, list) { - struct reloc *reloc; - - if (sec->changed) - continue; + struct reloc *reloc; - list_for_each_entry(reloc, &sec->reloc_list, list) { - if (reloc->sym == sym) { - sec->changed = true; - break; - } - } - } + list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) + reloc->sec->changed = true; } /* @@ -902,11 +893,12 @@ static int read_rela_reloc(struct sectio static int read_relocs(struct elf *elf) { + unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0; struct section *sec; struct reloc *reloc; - int i; unsigned int symndx; - unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0; + struct symbol *sym; + int i; if (!elf_alloc_hash(reloc, elf->text_size / 16)) return -1; @@ -947,13 +939,14 @@ static int read_relocs(struct elf *elf) reloc->sec = sec; reloc->idx = i; - reloc->sym = find_symbol_by_index(elf, symndx); + reloc->sym = sym = find_symbol_by_index(elf, symndx); if (!reloc->sym) { WARN("can't find reloc entry symbol %d for %s", symndx, sec->name); return -1; } + list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); list_add_tail(&reloc->list, &sec->reloc_list); elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -62,6 +62,7 @@ struct symbol { u8 fentry : 1; u8 profiling_func : 1; struct list_head pv_target; + struct list_head reloc_list; }; struct reloc { @@ -73,6 +74,7 @@ struct reloc { }; struct section *sec; struct symbol *sym; + struct list_head sym_reloc_entry; unsigned long offset; unsigned int type; s64 addend;