From patchwork Tue Dec 27 16:00:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 36952 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1460025wrt; Tue, 27 Dec 2022 08:04:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXvRzcXcrPaPA++v8OsRFxYt+0YzTMT7j+VHohfsN6niPznG3eVE1lP9P7z6szct6gPd8NbO X-Received: by 2002:a17:906:9be4:b0:7c0:e99a:e353 with SMTP id de36-20020a1709069be400b007c0e99ae353mr16925207ejc.56.1672157047758; Tue, 27 Dec 2022 08:04:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672157047; cv=none; d=google.com; s=arc-20160816; b=yP6LzlxXI2c3K5IuBWxsvUhZ2cYvH+XspxwEhC/yuvCP75wE9zfEB+hvgdogzl5dlh F/I7MZeKpSQp//5v1cufrk20EsDutt9kFjL/3TnTRnRqxWW5RP6NFr+phDwTy6MGvPrz deLjP6x/Y8j17XQG+vL9urWBJZVb2tmP0BdBSLQ1GM0aQqnOkeDk40W3e7HiHxFHqPTv pOxnDGTzPZZXRvlINyUf/bbxkpN3u1yzkYfIBlAwd2rZK1udGuRfhkpEbECU0PTomaRJ vA6UwS/2X+HNQKlwRs2hPcPxURjyVVyLmvVVxV/rkSvejHob8Y4NCj56iK7UfsOpc1tx vB7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:dkim-signature :from; bh=qOTXX+dAJQwd6zDAQlFhVwbPesRa20vvo+2rudlfamM=; b=BCFoveGBFunlAkK9v8KTGDS7aXr+RkNsw1e0PmPvF7F98ExRDRMscDH+HqMtInGF2B lfhigrHhBk2i6MYriN8vKwB+B2f0ZDNL1bUiyCviISY6Cp2ZM85KRHrYBRkO9bcgqwAN Kqmou33ZuUXYFf4qPFMFRu6CwGIiWvlNbNCX8EcSDYW147J5HVMfW11qhmIKeHMY4UBI rbmKiJOQLVooOOBG8Po6BJXNly9mdZEAlJfaAgXqj7eiE4v7fQOvuV4tZmURAmmPKAs/ maSil4Cn9Ue+inQwrykEt9CfqQoil7HOpdWzuJiY9+gNXFHjjL+FeqLCeXRim9yAw/lz 4ABA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@weissschuh.net header.s=mail header.b=B1CyNrKU; 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 p20-20020a1709060dd400b007ae754729d1si8254216eji.883.2022.12.27.08.03.43; Tue, 27 Dec 2022 08:04:07 -0800 (PST) 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=fail header.i=@weissschuh.net header.s=mail header.b=B1CyNrKU; 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 S231749AbiL0QDA (ORCPT + 99 others); Tue, 27 Dec 2022 11:03:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231502AbiL0QCt (ORCPT ); Tue, 27 Dec 2022 11:02:49 -0500 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 195FCF1A for ; Tue, 27 Dec 2022 08:02:47 -0800 (PST) From: Thomas =?utf-8?q?Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=weissschuh.net; s=mail; t=1672156964; bh=y+8ND3SPE2pShaR1P8+M7kgXvRP1pJvPmxf10cM5y8Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=B1CyNrKUwOFlrC/zgBHc9Up0AmSlWdkNnf/zucgJzpvF9KscVOFZm+v5sv8KqNX8+ zJDIt/c/lFIz2XKLHYcnLcL0CipdrSUrrdz3v4OKojwRNd4PXb0emo6oWcB34vAlre +x/sKyBaX97HIJX6tO5KNXs8LtnVCgIld5bTNL1o= Date: Tue, 27 Dec 2022 16:00:59 +0000 Subject: [PATCH v2 3/8] objtool: allocate multiple structures with calloc() MIME-Version: 1.0 Message-Id: <20221216-objtool-memory-v2-3-17968f85a464@weissschuh.net> References: <20221216-objtool-memory-v2-0-17968f85a464@weissschuh.net> In-Reply-To: <20221216-objtool-memory-v2-0-17968f85a464@weissschuh.net> To: Josh Poimboeuf , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Thomas =?utf-8?q?Wei=C3=9Fschuh?= X-Mailer: b4 0.11.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1672156865; l=3464; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=y+8ND3SPE2pShaR1P8+M7kgXvRP1pJvPmxf10cM5y8Q=; b=bm0NcSswg7oFUqzRMCfQCFgtM2ia9e0hYr/UJH2Ja8VEhCPmcRWUO4dZSpBYyAgPY3kP2q/Xxvf8 FkDHETPnD/lDwkqgNbWghVPYCVeObcNKKSO8u0USlUOGuitMyCGH X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,SPF_HELO_NONE,SPF_PASS autolearn=no 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?1753383748127601106?= X-GMAIL-MSGID: =?utf-8?q?1753383748127601106?= By using calloc() instead of malloc() in a loop, libc does not have to keep around bookkeeping information for each single structure. This reduces maximum memory usage while processing vmlinux.o from 3153325 KB to 3035668 KB (-3.7%) on my notebooks "localmodconfig". Signed-off-by: Thomas Weißschuh --- tools/objtool/elf.c | 42 ++++++++++++++++++------------------- tools/objtool/include/objtool/elf.h | 4 ++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 7eae95f33a72..9c326efb8cd9 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -285,13 +285,13 @@ static int read_sections(struct elf *elf) !elf_alloc_hash(section_name, sections_nr)) return -1; + elf->section_data = calloc(sections_nr, sizeof(*sec)); + if (!elf->section_data) { + perror("calloc"); + return -1; + } for (i = 0; i < sections_nr; i++) { - sec = malloc(sizeof(*sec)); - if (!sec) { - perror("malloc"); - return -1; - } - memset(sec, 0, sizeof(*sec)); + sec = &elf->section_data[i]; INIT_LIST_HEAD(&sec->symbol_list); INIT_LIST_HEAD(&sec->reloc_list); @@ -423,13 +423,13 @@ static int read_symbols(struct elf *elf) !elf_alloc_hash(symbol_name, symbols_nr)) return -1; + elf->symbol_data = calloc(symbols_nr, sizeof(*sym)); + if (!elf->symbol_data) { + perror("calloc"); + return -1; + } for (i = 0; i < symbols_nr; i++) { - sym = malloc(sizeof(*sym)); - if (!sym) { - perror("malloc"); - return -1; - } - memset(sym, 0, sizeof(*sym)); + sym = &elf->symbol_data[i]; sym->idx = i; @@ -919,13 +919,13 @@ static int read_relocs(struct elf *elf) sec->base->reloc = sec; nr_reloc = 0; + sec->reloc_data = calloc(sec->sh.sh_size / sec->sh.sh_entsize, sizeof(*reloc)); + if (!sec->reloc_data) { + perror("calloc"); + return -1; + } for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) { - reloc = malloc(sizeof(*reloc)); - if (!reloc) { - perror("malloc"); - return -1; - } - memset(reloc, 0, sizeof(*reloc)); + reloc = &sec->reloc_data[i]; switch (sec->sh.sh_type) { case SHT_REL: if (read_rel_reloc(sec, i, reloc, &symndx)) @@ -1458,16 +1458,16 @@ void elf_close(struct elf *elf) list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) { list_del(&sym->list); hash_del(&sym->hash); - free(sym); } list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) { list_del(&reloc->list); hash_del(&reloc->hash); - free(reloc); } list_del(&sec->list); - free(sec); + free(sec->reloc_data); } + free(elf->symbol_data); + free(elf->section_data); free(elf); } diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h index bb60fd42b46f..1c90f0ac0d53 100644 --- a/tools/objtool/include/objtool/elf.h +++ b/tools/objtool/include/objtool/elf.h @@ -39,6 +39,7 @@ struct section { char *name; int idx; bool changed, text, rodata, noinstr, init, truncate; + struct reloc *reloc_data; }; struct symbol { @@ -104,6 +105,9 @@ struct elf { struct hlist_head *section_hash; struct hlist_head *section_name_hash; struct hlist_head *reloc_hash; + + struct section *section_data; + struct symbol *symbol_data; }; #define OFFSET_STRIDE_BITS 4