Skip to contentSkip to conversation

dwarf_loader: Merged CU hash table fix and sizing

PATCH New ElXreno 1 participant 3 messages Sep 28

lore
Share & tools
lore

ElXreno

author
#0 lore raw

cover dwarf_loader: Merged CU hash table fix and sizing

Patch 1 fixes an out-of-bounds read when a merged CU looks up .debug_types, and stops the merged table size from leaking into the CUs loaded after it. Patch 2 sizes the merged CU tables from .debug_info, which makes BTF encoding of a ThinLTO vmlinux 17% faster.

tests/ gives the same per-test results with and without the series here (75 tests: 52 ok, 16 failed, 7 skipped); the failures happen on master as well. inter_cu_refs.sh and dwz_alt_file.sh, which skip here for lack of a perf with DWARF and of dwz, pass when run with both.

ElXreno (2):
dwarf_loader: Keep the hash table size in struct dwarf_cu
dwarf_loader: Size merged CU hash tables from the amount of DWARF

 dwarf_loader.c     | 70 +++++++++++++++++++++++++++++-----------------
 man-pages/pahole.1 |  4 +--
 2 files changed, 47 insertions(+), 27 deletions(-)

base-commit: c23e8b30c6dee2f7fd8aa07e9c49e8e4f31bc16c

Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/2 dwarf_loader: Keep the hash table size in struct dwarf_cu

cus__merge_and_process_cu() sizes the merged CU hash tables by raising the global hashtags__bits, which every lookup uses to pick a bucket. The .debug_types tables were allocated before that with the old value, so lookups through dcu->type_unit read past them:

  $ echo 'struct s { int a; } v; int main(void) { return v.a; }' > t.c
  $ gcc -gdwarf-4 -fdebug-types-section t.c -o t
  $ pahole -F dwarf --btf_features=force_cu_merging t
  struct s {
  	int                        a;                    /*     0     4 */

/* size: 4, cachelines: 1, members: 1 */
/* last cacheline: 4 bytes */
};
Segmentation fault (core dumped) pahole -F dwarf --btf_features=force_cu_merging t

With -fsanitize=address:

AddressSanitizer:DEADLYSIGNAL
=================================================================
==3587655==ERROR: AddressSanitizer: SEGV on unknown address 0x709eec8aecc8 (pc 0x71ceed04645b bp 0x7ffdbc1ce760 sp 0x7ffdbc1ce760 T0) ==3587655==The signal is caused by a READ memory access.

      #0 0x71ceed04645b in hashtags__find /build/source/dwarf_loader.c:293
      #1 0x71ceed04645b in __dwarf_cu__find_type_by_ref /build/source/dwarf_loader.c:498
      #2 0x71ceed057ea3 in tag__recode_dwarf_type /build/source/dwarf_loader.c:4125
      #3 0x71ceed058c99 in cu__recode_dwarf_types_table /build/source/dwarf_loader.c:4235
      #4 0x71ceed058c99 in cu__recode_dwarf_types /build/source/dwarf_loader.c:4245
      #5 0x71ceed05944b in cus__merge_and_process_cu /build/source/dwarf_loader.c:5199
      #6 0x71ceed05944b in cus__load_module /build/source/dwarf_loader.c:5274
      #7 0x71ceed05944b in cus__process_dwflmod /build/source/dwarf_loader.c:5339
      #8 0x71ceecfa13c8 in __dwfl_getmodules_internal (/nix/store/dyxcy223n2jch1kdf35wxcc39qmz8if5-elfutils-0.195/lib/libdw.so.1+0x603c8) (BuildId: 7da69f67158848e14bbae81b88e52ce03588fc75)
      #9 0x71ceed0475f1 in cus__process_file /build/source/dwarf_loader.c:5405
      #10 0x71ceed0475f1 in dwarf__load_file /build/source/dwarf_loader.c:5442
      #11 0x71ceed01beb3 in cus__load_file /build/source/dwarves.c:2199
      #12 0x71ceed01ceb0 in cus__load_files /build/source/dwarves.c:2897
      #13 0x606bb7c3e059 in main /build/source/pahole.c:3757
      #14 0x71ceecc96284 in __libc_start_call_main (/nix/store/lm3pknxi0ipypy3lxh1wmm8wvvavdwrn-glibc-2.42-84/lib/libc.so.6+0x2b284) (BuildId: 67802a55cff437269d8d9608af391b6f583fca73)
      #15 0x71ceecc96337 in __libc_start_main@GLIBC_2.2.5 (/nix/store/lm3pknxi0ipypy3lxh1wmm8wvvavdwrn-glibc-2.42-84/lib/libc.so.6+0x2b337) (BuildId: 67802a55cff437269d8d9608af391b6f583fca73)
      #16 0x606bb7c3f144 in _start (/nix/store/rpa278wbckgfxlmpxpkmfdhlk7fbxkr2-pahole-korg-asan-1.32-c23e8b3/bin/pahole+0xf144)

==3587655==Register values:

  rax = 0x0000709eec8aecc8  rbx = 0x00006ebeeb9e0080  rcx = 0x000000000000002b  rdx = 0x00000e13dd915d99
  rdi = 0x00006e4eeb9e0020  rsi = 0x000000000000001d  rbp = 0x00007ffdbc1ce760  rsp = 0x00007ffdbc1ce760
   r8 = 0x0000000000000000   r9 = 0x0000000000000000  r10 = 0x00006ebeeb9e0071  r11 = 0x00006ebeeb9e0050
  r12 = 0x00006efeeb9e0200  r13 = 0x00006ebeeb9e0094  r14 = 0x00006efeeb9e0280  r15 = 0x0000606bb7c564a8

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /build/source/dwarf_loader.c:293 in hashtags__find ==3587655==ABORTING

The raised value also stays for the rest of the run, so the CUs of any file loaded after a merged one get 2^21 bucket tables instead of 2^12.

Keep the size in each dwarf_cu and hash with it.

Fixes 39227909db3c ("dwarf_loader: Permit merging all DWARF CU's for clang LTO built binary")
Assisted-by LLM
---
 dwarf_loader.c | 49 ++++++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 23 deletions(-)
dwarf_loader.c +26 -23
index 1e5363a..53047f8 100644
@@ -80,9 +80,9 @@ static inline void libdw__lock_unlock(void) { pthread_mutex_unlock(&libdw__lock)
 static uint32_t hashtags__bits = 12;
 static uint32_t max_hashtags__bits = 21;
 
-static uint32_t hashtags__fn(Dwarf_Off key)
+static uint32_t hashtags__fn(Dwarf_Off key, uint32_t hash_bits)
 {
-	return hash_64(key, hashtags__bits);
+	return hash_64(key, hash_bits);
 }
 
 bool no_bitfield_type_recode = true;
@@ -172,6 +172,7 @@ struct alt_pu {
 struct dwarf_cu {
 	struct hlist_head *hash_tags;	/* tags (functions, etc.) keyed by DWARF offset */
 	struct hlist_head *hash_types;	/* types keyed by DWARF offset */
+	uint32_t  hash_bits;
 	struct dwarf_tag *last_type_lookup;
 	struct cu *cu;
 	struct dwarf_cu *type_unit;	/* .debug_types offset space */
@@ -185,12 +186,13 @@ struct dwarf_cu {
 	bool	  processing_alt;	/* true while processing alt-file DIEs */
 };
 
-static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu)
+static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu, uint32_t hash_bits)
 {
 	static struct dwarf_tag sentinel_dtag = { .id = ULLONG_MAX, };
-	uint64_t hashtags_size = 1UL << hashtags__bits;
+	uint64_t hashtags_size = 1UL << hash_bits;
 
 	dcu->cu = cu;
+	dcu->hash_bits = hash_bits;
 
 	dcu->hash_tags = cu__malloc(cu, sizeof(struct hlist_head) * hashtags_size);
 	if (!dcu->hash_tags)
@@ -228,11 +230,11 @@ static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu)
 	return 0;
 }
 
-static struct dwarf_cu *dwarf_cu__new(struct cu *cu)
+static struct dwarf_cu *dwarf_cu__new(struct cu *cu, uint32_t hash_bits)
 {
 	struct dwarf_cu *dwarf_cu = cu__zalloc(cu, sizeof(*dwarf_cu));
 
-	if (dwarf_cu != NULL && dwarf_cu__init(dwarf_cu, cu) != 0) {
+	if (dwarf_cu != NULL && dwarf_cu__init(dwarf_cu, cu, hash_bits) != 0) {
 		cu__free(cu, dwarf_cu);
 		dwarf_cu = NULL;
 	}
@@ -272,22 +274,22 @@ static void __tag__print_type_not_found(struct tag *tag, const char *func)
 #define tag__print_type_not_found(tag) \
 	__tag__print_type_not_found(tag, __func__)
 
-static void hashtags__hash(struct hlist_head *hashtable,
+static void hashtags__hash(struct hlist_head *hashtable, uint32_t hash_bits,
 			   struct dwarf_tag *dtag)
 {
-	struct hlist_head *head = hashtable + hashtags__fn(dtag->id);
+	struct hlist_head *head = hashtable + hashtags__fn(dtag->id, hash_bits);
 	hlist_add_head(&dtag->hash_node, head);
 }
 
 static struct dwarf_tag *hashtags__find(const struct hlist_head *hashtable,
-					const Dwarf_Off id)
+					uint32_t hash_bits, const Dwarf_Off id)
 {
 	if (id == 0)
 		return NULL;
 
 	struct dwarf_tag *tpos;
 	struct hlist_node *pos;
-	uint32_t bucket = hashtags__fn(id);
+	uint32_t bucket = hashtags__fn(id, hash_bits);
 	const struct hlist_head *head = hashtable + bucket;
 
 	hlist_for_each_entry(tpos, pos, head, hash_node) {
@@ -317,7 +319,7 @@ static void cu__hash(struct cu *cu, struct tag *tag)
 	struct hlist_head *hashtable = tag__is_tag_type(tag) ?
 							target->hash_types :
 							target->hash_tags;
-	hashtags__hash(hashtable, tag__dwarf(tag));
+	hashtags__hash(hashtable, target->hash_bits, tag__dwarf(tag));
 }
 
 /* Binary search for the alt PU whose [start, end) range contains offset.
@@ -410,7 +412,7 @@ static void dwarf_cu__prune_unreferenced_alt_pus(struct dwarf_cu *dcu)
 	if (alt == NULL || dcu->nr_alt_pus == 0)
 		return;
 
-	uint64_t hashtags_size = 1UL << hashtags__bits;
+	uint64_t hashtags_size = 1UL << alt->hash_bits;
 
 	for (uint64_t i = 0; i < hashtags_size; i++) {
 		struct dwarf_tag *dtag;
@@ -467,7 +469,7 @@ static struct dwarf_tag *__dwarf_cu__find_tag_by_ref(const struct dwarf_cu *cu,
 		if (cu == NULL)
 			return NULL;
 	}
-	return hashtags__find(cu->hash_tags, ref);
+	return hashtags__find(cu->hash_tags, cu->hash_bits, ref);
 }
 
 #define dwarf_cu__find_tag_by_ref(cu, dtag, field) \
@@ -495,7 +497,7 @@ static struct dwarf_tag *__dwarf_cu__find_type_by_ref(struct dwarf_cu *dcu,
 	if (dcu->last_type_lookup->id == ref)
 		return dcu->last_type_lookup;
 
-	struct dwarf_tag *dtag = hashtags__find(dcu->hash_types, ref);
+	struct dwarf_tag *dtag = hashtags__find(dcu->hash_types, dcu->hash_bits, ref);
 
 	if (dtag)
 		dcu->last_type_lookup = dtag;
@@ -4565,7 +4567,7 @@ static int __cus__load_debug_types(struct cus *cus, struct conf_load *conf, Dwfl
 				return DWARF_CB_ABORT;
 			}
 
-			dcu = dwarf_cu__new(cu);
+			dcu = dwarf_cu__new(cu, hashtags__bits);
 			if (dcu == NULL) {
 				cu__delete(cu);
 				return DWARF_CB_ABORT;
@@ -4826,7 +4828,7 @@ static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die *
 		return NULL;
 	}
 
-	struct dwarf_cu *dcu = dwarf_cu__new(cu);
+	struct dwarf_cu *dcu = dwarf_cu__new(cu, hashtags__bits);
 
 	if (dcu == NULL) {
 		cu__delete(cu);
@@ -5081,14 +5083,15 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 			 * Let us start with max_hashtags__bits and
 			 * go down to find a proper hashtag bit value.
 			 */
-			uint32_t default_hbits = hashtags__bits;
-			for (hashtags__bits = max_hashtags__bits;
-			     hashtags__bits >= default_hbits;
-			     hashtags__bits--) {
-				if (dwarf_cu__init(dcu, cu) == 0)
+			uint32_t hash_bits;
+
+			for (hash_bits = max_hashtags__bits;
+			     hash_bits >= hashtags__bits;
+			     hash_bits--) {
+				if (dwarf_cu__init(dcu, cu, hash_bits) == 0)
 					break;
 			}
-			if (hashtags__bits < default_hbits)
+			if (hash_bits < hashtags__bits)
 				goto out_abort;
 
 			dcu->cu = cu;
@@ -5116,7 +5119,7 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 			Dwarf *alt_dw = dwarf_getalt(dw);
 
 			if (alt_dw != NULL) {
-				struct dwarf_cu *alt_dcu = dwarf_cu__new(cu);
+				struct dwarf_cu *alt_dcu = dwarf_cu__new(cu, dcu->hash_bits);
 
 				if (alt_dcu == NULL)
 					goto out_abort;
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 2/2 dwarf_loader: Size merged CU hash tables from the amount of DWARF

In reply to ElXreno

Merged CUs always start at 2^21 buckets per hash table, whatever the object size: too few for a ThinLTO vmlinux with 551 MB of .debug_info, far too many for a module.

Size the tables at one bucket per 32 bytes of .debug_info, clamped to [hashtags__bits, max_hashtags__bits], and raise the max to 26. The tables of a dwz alternate file are sized from its own .debug_info.

clang 21 ThinLTO 7.2.7 kernel, 5855 modules, BTF byte-identical:

vmlinux, perf stat -r5: 39.92 s -> 32.93 s, +- 0.2%

                            max RSS 10.7 GiB -> 11.2 GiB

modules, 16 in parallel: 85.4 s -> 76.7 s wall, 212 s -> 137 s sys

Assisted-by LLM
---
 dwarf_loader.c     | 25 +++++++++++++++++++++----
 man-pages/pahole.1 |  4 ++--
 2 files changed, 23 insertions(+), 6 deletions(-)
dwarf_loader.c +21 -4
index 53047f8..aa129b3 100644
@@ -78,7 +78,7 @@ static inline void libdw__lock_unlock(void) { pthread_mutex_unlock(&libdw__lock)
 #endif
 
 static uint32_t hashtags__bits = 12;
-static uint32_t max_hashtags__bits = 21;
+static uint32_t max_hashtags__bits = 26;
 
 static uint32_t hashtags__fn(Dwarf_Off key, uint32_t hash_bits)
 {
@@ -5048,6 +5048,22 @@ out_early:
 	return dcus->error;
 }
 
+static uint32_t dwarf__hash_bits(Dwarf *dw)
+{
+	Dwarf_Off off = 0, noff;
+	size_t cuhl;
+	uint32_t bits;
+
+	while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
+		off = noff;
+
+	bits = fls64(off / 32);
+	if (bits < hashtags__bits)
+		return hashtags__bits;
+
+	return bits < max_hashtags__bits ? bits : max_hashtags__bits;
+}
+
 static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 				     Dwfl_Module *mod, Dwarf *dw, Elf *elf,
 				     const char *filename,
@@ -5080,12 +5096,12 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 				goto out_abort;
 
 			/* Merged cu tends to need a lot more memory.
-			 * Let us start with max_hashtags__bits and
+			 * Size the hash tables from the amount of DWARF and
 			 * go down to find a proper hashtag bit value.
 			 */
 			uint32_t hash_bits;
 
-			for (hash_bits = max_hashtags__bits;
+			for (hash_bits = dwarf__hash_bits(dw);
 			     hash_bits >= hashtags__bits;
 			     hash_bits--) {
 				if (dwarf_cu__init(dcu, cu, hash_bits) == 0)
@@ -5119,7 +5135,8 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 			Dwarf *alt_dw = dwarf_getalt(dw);
 
 			if (alt_dw != NULL) {
-				struct dwarf_cu *alt_dcu = dwarf_cu__new(cu, dcu->hash_bits);
+				struct dwarf_cu *alt_dcu =
+					dwarf_cu__new(cu, dwarf__hash_bits(alt_dw));
 
 				if (alt_dcu == NULL)
 					goto out_abort;
man-pages/pahole.1 +2 -2
index 35ea9f4..24c7d31 100644
@@ -162,8 +162,8 @@ The default format path used is equivalent to "-F dwarf,btf,ctf".
 .TP
 .B \-\-hashbits=BITS
 Allows specifying the number of bits for the debugging format loader to use.
-The only one affected so far is the "dwarf" one, its default now is 15, the
-maximum for it is now 21 bits.
+The only one affected so far is the "dwarf" one, its default now is 12, the
+maximum for it is now 26 bits.
 Tweak it to see if it improves performance as the kernel evolves and more
 types and functions have to be loaded.
 
Signature · 1 line
2.55.0