[PATCH 21/22] objtool: Free insns when done

From: Josh Poimboeuf
Date: Tue May 30 2023 - 13:22:32 EST


Free the decoded instructions as they're no longer needed after this
point. This frees up a big chunk of heap, which will come handy when
skipping the reading of DWARF section data.

Signed-off-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
---
tools/objtool/check.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 65c59b0b1e96..80d3a97e442b 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4587,6 +4587,34 @@ static int disas_warned_funcs(struct objtool_file *file)
return 0;
}

+struct insn_chunk {
+ void *addr;
+ struct insn_chunk *next;
+};
+
+/*
+ * Reduce peak RSS usage by freeing insns memory before writing the ELF file,
+ * which can trigger more allocations for .debug_* sections whose data hasn't
+ * been read yet.
+ */
+static void free_insns(struct objtool_file *file)
+{
+ struct instruction *insn;
+ struct insn_chunk *chunks = NULL, *chunk;
+
+ for_each_insn(file, insn) {
+ if (!insn->idx) {
+ chunk = malloc(sizeof(*chunk));
+ chunk->addr = insn;
+ chunk->next = chunks;
+ chunks = chunk;
+ }
+ }
+
+ for (chunk = chunks; chunk; chunk = chunk->next)
+ free(chunk->addr);
+}
+
int check(struct objtool_file *file)
{
int ret, warnings = 0;
@@ -4731,6 +4759,8 @@ int check(struct objtool_file *file)
warnings += ret;
}

+ free_insns(file);
+
if (opts.verbose)
disas_warned_funcs(file);

--
2.40.1