From nobody Wed Apr 8 21:36:06 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA0D4C4332F for ; Wed, 2 Nov 2022 21:47:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231311AbiKBVrJ (ORCPT ); Wed, 2 Nov 2022 17:47:09 -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> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; =20 + INIT_LIST_HEAD(&sym->reloc_list); INIT_LIST_HEAD(&sym->pv_target); sym->alias =3D sym; =20 @@ -557,6 +558,7 @@ int elf_add_reloc(struct elf *elf, struc reloc->sym =3D sym; reloc->addend =3D addend; =20 + 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)); =20 @@ -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; =20 - list_for_each_entry(reloc, &sec->reloc_list, list) { - if (reloc->sym =3D=3D sym) { - sec->changed =3D true; - break; - } - } - } + list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) + reloc->sec->changed =3D true; } =20 /* @@ -902,11 +893,12 @@ static int read_rela_reloc(struct sectio =20 static int read_relocs(struct elf *elf) { + unsigned long nr_reloc, max_reloc =3D 0, tot_reloc =3D 0; struct section *sec; struct reloc *reloc; - int i; unsigned int symndx; - unsigned long nr_reloc, max_reloc =3D 0, tot_reloc =3D 0; + struct symbol *sym; + int i; =20 if (!elf_alloc_hash(reloc, elf->text_size / 16)) return -1; @@ -947,13 +939,14 @@ static int read_relocs(struct elf *elf) =20 reloc->sec =3D sec; reloc->idx =3D i; - reloc->sym =3D find_symbol_by_index(elf, symndx); + reloc->sym =3D sym =3D find_symbol_by_index(elf, symndx); if (!reloc->sym) { WARN("can't find reloc entry symbol %d for %s", symndx, sec->name); return -1; } =20 + 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)); =20 --- 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; }; =20 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;