Skip to contentSkip to conversation

ARM: module: Add all unwind tables when load module

PATCH Superseded Chen Zhongjin 2 participants 3 messages Mar 31, 2022 – Apr 1, 2022 Arm (32-bit)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ARM: module: Add all unwind tables when load module

For EABI stack unwinding, when loading .ko module the EXIDX sections will be added to a unwind_table list.

However not all EXIDX sections are added because EXIDX sections are searched by hardcoded section names.

For functions in other sections such as .ref.text or .kprobes.text, gcc generates seprated EXIDX sections (such as .ARM.exidx.ref.text or .ARM.exidx.kprobes.text).

These extra EXIDX sections are not loaded, so when unwinding functions in these sections, we will failed with:

unwind: Index not found xxx

To fix that, I refactor the code for searching and adding EXIDX sections:

- Check section type to search EXIDX tables (0x70000001) instead of strcmp() the hardcoded names. Then find the corresponding text sections by their section names.

- Add a unwind_table list in module->arch to save their own unwind_table instead of the fixed-lenth array.

- Save .ARM.exidx.init.text section ptr, because it should be cleaned after module init.

Now all EXIDX sections of .ko can be added correctly.

---
Changes v1 -> v2:
- Rename "table_list" to "unwind_list" for consistency.
- Drop unnecessary locals variable "txtname" and "sectype".
- Merge condition checks for sh_flags and sh_type.

---
 arch/arm/include/asm/module.h | 17 ++-------
 arch/arm/include/asm/unwind.h |  1 +
 arch/arm/kernel/module.c      | 70 ++++++++++++++++-------------------
 3 files changed, 37 insertions(+), 51 deletions(-)
arch/arm/include/asm/module.h +4 -13
index cfffae67c04e..8139b6a33a22 100644
@@ -3,20 +3,10 @@
 #define _ASM_ARM_MODULE_H
 
 #include <asm-generic/module.h>
-
-struct unwind_table;
+#include <asm/unwind.h>
 
 #ifdef CONFIG_ARM_UNWIND
-enum {
-	ARM_SEC_INIT,
-	ARM_SEC_DEVINIT,
-	ARM_SEC_CORE,
-	ARM_SEC_EXIT,
-	ARM_SEC_DEVEXIT,
-	ARM_SEC_HOT,
-	ARM_SEC_UNLIKELY,
-	ARM_SEC_MAX,
-};
+#define ELF_SECTION_UNWIND 0x70000001
 #endif
 
 #define PLT_ENT_STRIDE		L1_CACHE_BYTES
@@ -36,7 +26,8 @@ struct mod_plt_sec {
 
 struct mod_arch_specific {
 #ifdef CONFIG_ARM_UNWIND
-	struct unwind_table *unwind[ARM_SEC_MAX];
+	struct unwind_table unwind_list;
+	struct unwind_table *init_table;
 #endif
 #ifdef CONFIG_ARM_MODULE_PLTS
 	struct mod_plt_sec	core;
arch/arm/include/asm/unwind.h +1 -0
index 0f8a3439902d..b51f85417f58 100644
@@ -24,6 +24,7 @@ struct unwind_idx {
 
 struct unwind_table {
 	struct list_head list;
+	struct list_head mod_list;
 	const struct unwind_idx *start;
 	const struct unwind_idx *origin;
 	const struct unwind_idx *stop;
arch/arm/kernel/module.c +32 -38

96 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 471

Quoted 5 lines · Chen Zhongjin
arch/arm/kernel/module.c +3 -0
 	for (s = sechdrs; s < sechdrs_end; s++) {
 		const char *secname = secstrs + s->sh_name;
+		const Elf_Shdr *txt_sec = find_mod_section(hdr, sechdrs,
+				strcmp(".ARM.exidx", secname) ?
+				secname + strlen(".ARM.exidx") : ".text");

In addition to my comments on the above...

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 474

Quoted 4 lines
arch/arm/kernel/module.c +2 -1
-		if (!(s->sh_flags & SHF_ALLOC))
+		if (!(s->sh_flags & SHF_ALLOC) ||
+			s->sh_type != ELF_SECTION_UNWIND)

Please align "s->sh_type" with the ! on the line above (don't use a tab for this!)

Quoted 18 lines
arch/arm/kernel/module.c +0 -16
 			continue;
 
-		if (strcmp(".ARM.exidx.init.text", secname) == 0)
-			maps[ARM_SEC_INIT].unw_sec = s;
-		else if (strcmp(".ARM.exidx", secname) == 0)
-			maps[ARM_SEC_CORE].unw_sec = s;
-		else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
-			maps[ARM_SEC_EXIT].unw_sec = s;
-		else if (strcmp(".ARM.exidx.text.unlikely", secname) == 0)
-			maps[ARM_SEC_UNLIKELY].unw_sec = s;
-		else if (strcmp(".ARM.exidx.text.hot", secname) == 0)
-			maps[ARM_SEC_HOT].unw_sec = s;
-		else if (strcmp(".init.text", secname) == 0)
-			maps[ARM_SEC_INIT].txt_sec = s;
-		else if (strcmp(".text", secname) == 0)
-			maps[ARM_SEC_CORE].txt_sec = s;
-		else if (strcmp(".exit.text", secname) == 0)
-			maps[ARM_SEC_EXIT].txt_sec = s;

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 482

Quoted 10 lines
arch/arm/kernel/module.c +6 -4
-		else if (strcmp(".text.unlikely", secname) == 0)
-			maps[ARM_SEC_UNLIKELY].txt_sec = s;
-		else if (strcmp(".text.hot", secname) == 0)
-			maps[ARM_SEC_HOT].txt_sec = s;
+		if (txt_sec) {
+			struct unwind_table *table =
+				unwind_table_add(s->sh_addr,
+						s->sh_size,
+						txt_sec->sh_addr,
+						txt_sec->sh_size);

blank line here please.

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 483

Quoted 1 line
arch/arm/kernel/module.c +1 -0
+			list_add(&table->mod_list, &unwind_list->mod_list);

and here.

Otherwise, looks good. Thanks.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
#2 lore raw

Very appreciate for your patient review! For my first patch it really helps a lot :D

v3 update pushed now.

Sorry forgot CC for last mail.

Quoted 50 lines · Russell King (Oracle)

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 471

Quoted 5 lines · Chen Zhongjin
  	for (s = sechdrs; s < sechdrs_end; s++) {
  		const char *secname = secstrs + s->sh_name;
arch/arm/kernel/module.c +3 -0
+		const Elf_Shdr *txt_sec = find_mod_section(hdr, sechdrs,
+				strcmp(".ARM.exidx", secname) ?
+				secname + strlen(".ARM.exidx") : ".text");

In addition to my comments on the above...

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 474

Quoted 4 lines
arch/arm/kernel/module.c +2 -1
-		if (!(s->sh_flags & SHF_ALLOC))
+		if (!(s->sh_flags & SHF_ALLOC) ||
+			s->sh_type != ELF_SECTION_UNWIND)

Please align "s->sh_type" with the ! on the line above (don't use a tab for this!)

Quoted 18 lines

continue;

arch/arm/kernel/module.c +0 -16
-		if (strcmp(".ARM.exidx.init.text", secname) == 0)
-			maps[ARM_SEC_INIT].unw_sec = s;
-		else if (strcmp(".ARM.exidx", secname) == 0)
-			maps[ARM_SEC_CORE].unw_sec = s;
-		else if (strcmp(".ARM.exidx.exit.text", secname) == 0)
-			maps[ARM_SEC_EXIT].unw_sec = s;
-		else if (strcmp(".ARM.exidx.text.unlikely", secname) == 0)
-			maps[ARM_SEC_UNLIKELY].unw_sec = s;
-		else if (strcmp(".ARM.exidx.text.hot", secname) == 0)
-			maps[ARM_SEC_HOT].unw_sec = s;
-		else if (strcmp(".init.text", secname) == 0)
-			maps[ARM_SEC_INIT].txt_sec = s;
-		else if (strcmp(".text", secname) == 0)
-			maps[ARM_SEC_CORE].txt_sec = s;
-		else if (strcmp(".exit.text", secname) == 0)
-			maps[ARM_SEC_EXIT].txt_sec = s;

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 482

Quoted 10 lines
arch/arm/kernel/module.c +6 -4
-		else if (strcmp(".text.unlikely", secname) == 0)
-			maps[ARM_SEC_UNLIKELY].txt_sec = s;
-		else if (strcmp(".text.hot", secname) == 0)
-			maps[ARM_SEC_HOT].txt_sec = s;
+		if (txt_sec) {
+			struct unwind_table *table =
+				unwind_table_add(s->sh_addr,
+						s->sh_size,
+						txt_sec->sh_addr,
+						txt_sec->sh_size);

blank line here please.

arch/arm/kernel/module.c int module_finalize(const Elf32_Ehdr *hdr, const Elf_Shdr *sechdrs, · line 483

Quoted 1 line
arch/arm/kernel/module.c +1 -0
+			list_add(&table->mod_list, &unwind_list->mod_list);

and here.

Otherwise, looks good. Thanks.