mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <linux@weissschuh.net>
To: Josh Poimboeuf <jpoimboe@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>
Cc: linux-kernel@vger.kernel.org, "Thomas Weißschuh" <linux@weissschuh.net>
Subject: [PATCH v2 3/8] objtool: allocate multiple structures with calloc()
Date: Tue, 27 Dec 2022 16:00:59 +0000	[thread overview]
Message-ID: <20221216-objtool-memory-v2-3-17968f85a464@weissschuh.net> (raw)
In-Reply-To: <20221216-objtool-memory-v2-0-17968f85a464@weissschuh.net>

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 <linux@weissschuh.net>
---
 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

-- 
2.39.0

  parent reply	other threads:[~2022-12-27 16:03 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-27 16:00 [PATCH v2 0/8] reduce maximum memory usage Thomas Weißschuh
2022-12-27 16:00 ` [PATCH v2 1/8] objtool: make struct entries[] static and const Thomas Weißschuh
2023-02-01 16:26   ` [tip: objtool/core] objtool: Make " tip-bot2 for Thomas Weißschuh
2023-02-04 10:17   ` tip-bot2 for Thomas Weißschuh
2022-12-27 16:00 ` [PATCH v2 2/8] objtool: make struct check_options static Thomas Weißschuh
2023-02-01 16:26   ` [tip: objtool/core] objtool: Make " tip-bot2 for Thomas Weißschuh
2023-02-04 10:17   ` tip-bot2 for Thomas Weißschuh
2022-12-27 16:00 ` Thomas Weißschuh [this message]
2023-01-30 23:53   ` [PATCH v2 3/8] objtool: allocate multiple structures with calloc() Josh Poimboeuf
2023-02-01 16:26   ` [tip: objtool/core] objtool: Allocate " tip-bot2 for Thomas Weißschuh
2023-02-04 10:17   ` tip-bot2 for Thomas Weißschuh
2022-12-27 16:01 ` [PATCH v2 4/8] objtool: introduce function elf_reloc_set_type Thomas Weißschuh
2022-12-27 16:01 ` [PATCH v2 5/8] objtool: reduce memory usage of struct reloc Thomas Weißschuh
2022-12-29  1:33   ` Rong Tao
2022-12-29  2:26     ` Thomas Weißschuh
2022-12-29  3:29       ` Rong Tao
2022-12-29  5:57         ` Thomas Weißschuh
2023-01-30 23:59   ` Josh Poimboeuf
2022-12-27 16:01 ` [PATCH v2 6/8] objtool: optimize layout of struct symbol Thomas Weißschuh
2023-02-01 16:26   ` [tip: objtool/core] objtool: Optimize " tip-bot2 for Thomas Weißschuh
2023-02-04 10:17   ` tip-bot2 for Thomas Weißschuh
2022-12-27 16:01 ` [PATCH v2 7/8] objtool: optimize layout of struct special_alt Thomas Weißschuh
2023-02-01 16:26   ` [tip: objtool/core] objtool: Optimize " tip-bot2 for Thomas Weißschuh
2023-02-04 10:17   ` tip-bot2 for Thomas Weißschuh
2022-12-27 16:01 ` [PATCH v2 8/8] objtool: explicitly cleanup resources on success Thomas Weißschuh
2023-01-31  0:02   ` Josh Poimboeuf
2023-01-29 21:43 ` [PATCH v2 0/8] objtool: reduce maximum memory usage Thomas Weißschuh
2023-01-31  0:03   ` Josh Poimboeuf
2023-01-31  3:54     ` Thomas Weißschuh
2023-01-31 17:27       ` Josh Poimboeuf
2023-02-07 17:30         ` Josh Poimboeuf
2023-02-01 12:51       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221216-objtool-memory-v2-3-17968f85a464@weissschuh.net \
    --to=linux@weissschuh.net \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®