Skip to contentSkip to conversation

ftrace/recordmcount: Handle object files without section symbols

PATCH In review Naveen N. Rao 2 participants 8 messages Apr 28, 2022 – Apr 29, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover ftrace/recordmcount: Handle object files without section symbols

This is v2 of the series posted at:
http://lkml.kernel.org/r/[email protected]

For v2, the first patch is slightly modified to skip the loop, rather than depending on addr == 0 to do so. The second patch is updated to make this behavior be opt-in by architectures so that they can validate the read mcount locations.

- Naveen

Naveen N. Rao (2):
ftrace: Drop duplicate mcount locations
ftrace: recordmcount: Handle sections with no non-weak symbols

 Makefile                           |  4 ++
 arch/powerpc/Kconfig               |  1 +
 arch/powerpc/include/asm/ftrace.h  |  8 +--
 arch/powerpc/kernel/trace/ftrace.c | 11 ++++
 kernel/trace/Kconfig               |  6 ++
 kernel/trace/ftrace.c              | 13 ++++-
 scripts/Makefile.build             |  3 +
 scripts/recordmcount.c             |  6 +-
 scripts/recordmcount.h             | 94 ++++++++++++++++++++++++++----
 9 files changed, 125 insertions(+), 21 deletions(-)

base-commit: 83d8a0d166119de813cad27ae7d61f54f9aea707

Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/2 ftrace: Drop duplicate mcount locations

In the absence of section symbols [1], objtool (today) and recordmcount (with a subsequent patch) generate __mcount_loc relocation records with weak symbols as the base. This works fine as long as those weak symbols are not overridden, but if they are, these can result in duplicate entries in the final vmlinux mcount location table. This will cause ftrace to fail when trying to patch the same location twice. Fix this by dropping duplicate locations during ftrace init.

[1] https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d1bcae833b32f1

---
 kernel/trace/ftrace.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +12 -1
index 4f1d2f5e726341..038610f1803987 100644
@@ -6496,7 +6496,7 @@ static int ftrace_process_locs(struct module *mod,
 	struct dyn_ftrace *rec;
 	unsigned long count;
 	unsigned long *p;
-	unsigned long addr;
+	unsigned long addr, prev_addr = 0;
 	unsigned long flags = 0; /* Shut up gcc */
 	int ret = -ENOMEM;
 
@@ -6550,6 +6550,7 @@ static int ftrace_process_locs(struct module *mod,
 	while (p < end) {
 		unsigned long end_offset;
 		addr = ftrace_call_adjust(*p++);
+
 		/*
 		 * Some architecture linkers will pad between
 		 * the different mcount_loc sections of different
@@ -6559,6 +6560,15 @@ static int ftrace_process_locs(struct module *mod,
 		if (!addr)
 			continue;
 
+		/*
+		 * Drop duplicate entries, which can happen when weak
+		 * functions are overridden, and __mcount_loc relocation
+		 * records were generated against function names due to
+		 * absence of non-weak section symbols.
+		 */
+		if (addr == prev_addr)
+			continue;
+
 		end_offset = (pg->index+1) * sizeof(pg->records[0]);
 		if (end_offset > PAGE_SIZE << pg->order) {
 			/* We should have allocated enough */
@@ -6569,6 +6579,7 @@ static int ftrace_process_locs(struct module *mod,
 
 		rec = &pg->records[pg->index++];
 		rec->ip = addr;
+		prev_addr = addr;
 	}
 
 	/* We should have used all pages */
Signature · 1 line
2.35.1
#2 lore raw

PATCH v2 2/2 ftrace: recordmcount: Handle sections with no non-weak symbols

In reply to Naveen N. Rao

Kernel builds on powerpc are failing with the below error [1]:
CC kernel/kexec_file.o
Cannot find symbol for section 9: .text.unlikely. kernel/kexec_file.o: failed

Since commit d1bcae833b32f1 ("ELF: Don't generate unused section symbols") [2], binutils started dropping section symbols that it thought were unused. This isn't an issue in general, but with kexec_file.c, gcc is placing kexec_arch_apply_relocations[_add] into a separate .text.unlikely section and the section symbol ".text.unlikely" is being dropped. Due to this, recordmcount is unable to find a non-weak symbol in .text.unlikely to generate a relocation record against.

Handle this by falling back to a weak symbol, similar to what objtool does in commit 44f6a7c0755d8d ("objtool: Fix seg fault with Clang non-section symbols"). This approach however can result in duplicate and/or invalid addresses in the final vmlinux mcount location table.

As an example, with this commit, relocation records for __mcount_loc for kexec_file.o now include two entries with the weak functions arch_kexec_apply_relocations() and arch_kexec_apply_relocation_add() as the relocation bases:

...

  0000000000000080 R_PPC64_ADDR64    .text+0x0000000000001d34
  0000000000000088 R_PPC64_ADDR64    .text+0x0000000000001fec
  0000000000000090 R_PPC64_ADDR64    arch_kexec_apply_relocations_add+0x000000000000000c
  0000000000000098 R_PPC64_ADDR64    arch_kexec_apply_relocations+0x000000000000000c

Powerpc does not override these functions today, so these get converted to correct offsets in the mcount location table in vmlinux.

If one or both of these weak functions are overridden in future, in the final vmlinux mcount table, references to these will change over to the non-weak variant which has its own mcount location entry. As such, there will now be two entries for these functions.

On ppc32, mcount location is always the third instruction in a function. On ppc64 with elf abi v2 (ppc64le), mcount location depends on whether the function has a global entry (fourth instruction) or not (second instruction), but this is expected to be the same across weak/non-weak implementations of a function. As such, in both these scenarios, as well as with other architectures where mcount location is at the same offset into a function, the two mcount entries will point to the same address. Ftrace skips the duplicate entries due to a previous commit.

But, with ppc64 elf abi v1 which only supports the old -pg flag, mcount location can differ between the weak and non-weak variants of a function. In such scenarios, one of the two mcount entries will be invalid. Such architectures need to validate mcount locations by ensuring that the instruction(s) at those locations are as expected. On powerpc, this can be a simple check to ensure that the instruction is a 'bl'. This check can be further tightened as necessary.

Introduce a config option HAVE_MCOUNT_LOC_VALIDATION that architectures can select to indicate support for validating the mcount locations during ftrace initialization. Add a flag (-a) to recordmcount which can then be passed to allow recordmcount to emit relocation records using weak symbols as the base.

[1] https://github.com/linuxppc/issues/issues/388
[2] https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d1bcae833b32f1

---
 Makefile                           |  4 ++
 arch/powerpc/Kconfig               |  1 +
 arch/powerpc/include/asm/ftrace.h  |  8 +--
 arch/powerpc/kernel/trace/ftrace.c | 11 ++++
 kernel/trace/Kconfig               |  6 ++
 scripts/Makefile.build             |  3 +
 scripts/recordmcount.c             |  6 +-
 scripts/recordmcount.h             | 94 ++++++++++++++++++++++++++----
 8 files changed, 113 insertions(+), 20 deletions(-)
Makefile +4 -0
index 29e273d3f8ccbf..b2a9fdb49815fb 100644
@@ -858,6 +858,10 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
     BUILD_C_RECORDMCOUNT := y
     export BUILD_C_RECORDMCOUNT
   endif
+  ifdef CONFIG_HAVE_MCOUNT_LOC_VALIDATION
+    HAVE_MCOUNT_LOC_VALIDATION := y
+    export HAVE_MCOUNT_LOC_VALIDATION
+  endif
 endif
 ifdef CONFIG_HAVE_FENTRY
   # s390-linux-gnu-gcc did not support -mfentry until gcc-9.
arch/powerpc/Kconfig +1 -0
index 174edabb74fa11..acae4085aa6d6b 100644
@@ -229,6 +229,7 @@ config PPC
 	select HAVE_KRETPROBES
 	select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
 	select HAVE_LIVEPATCH			if HAVE_DYNAMIC_FTRACE_WITH_REGS
+	select HAVE_MCOUNT_LOC_VALIDATION
 	select HAVE_MOD_ARCH_SPECIFIC
 	select HAVE_NMI				if PERF_EVENTS || (PPC64 && PPC_BOOK3S)
 	select HAVE_OPTPROBES
arch/powerpc/include/asm/ftrace.h +1 -7
index d83758acd1c7c3..d8b104ed2fdf38 100644
@@ -12,13 +12,7 @@
 
 #ifndef __ASSEMBLY__
 extern void _mcount(void);
-
-static inline unsigned long ftrace_call_adjust(unsigned long addr)
-{
-       /* relocation of mcount call site is the same as the address */
-       return addr;
-}
-
+unsigned long ftrace_call_adjust(unsigned long addr);
 unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
 				    unsigned long sp);
 
arch/powerpc/kernel/trace/ftrace.c +11 -0
index 4ee04aacf9f13c..976c08cd0573f7 100644
@@ -858,6 +858,17 @@ void arch_ftrace_update_code(int command)
 	ftrace_modify_all_code(command);
 }
 
+unsigned long ftrace_call_adjust(unsigned long addr)
+{
+	ppc_inst_t op = ppc_inst_read((u32 *)addr);
+
+	if (!is_bl_op(op))
+		return 0;
+
+	/* relocation of mcount call site is the same as the address */
+	return addr;
+}
+
 #ifdef CONFIG_PPC64
 #define PACATOC offsetof(struct paca_struct, kernel_toc)
 
kernel/trace/Kconfig +6 -0
index 2c43e327a619f7..f1704c764fa66a 100644
@@ -81,6 +81,12 @@ config HAVE_C_RECORDMCOUNT
 	help
 	  C version of recordmcount available?
 
+config HAVE_MCOUNT_LOC_VALIDATION
+	bool
+	help
+	  Arch validates mcount locations and can cope with invalid entries
+	  in the mcount table.
+
 config HAVE_BUILDTIME_MCOUNT_SORT
        bool
        help
scripts/Makefile.build +3 -0
index 9717e6f6fb3149..4401103991fd5c 100644
@@ -199,6 +199,9 @@ ifdef BUILD_C_RECORDMCOUNT
 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
   RECORDMCOUNT_FLAGS = -w
 endif
+ifdef HAVE_MCOUNT_LOC_VALIDATION
+  RECORDMCOUNT_FLAGS += -a
+endif
 # Due to recursion, we must skip empty.o.
 # The empty.o file is created in the make process in order to determine
 # the target endianness and word size. It is made before all other C
scripts/recordmcount.c +5 -1
index cce12e1971d853..30308109446e86 100644
@@ -50,6 +50,7 @@ static char gpfx;	/* prefix for global symbol name (sometimes '_') */
 static struct stat sb;	/* Remember .st_size, etc. */
 static const char *altmcount;	/* alternate mcount symbol name */
 static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
+static int allow_weak_sym_rel; /* allow relocations using weak symbols */
 static void *file_map;	/* pointer of the mapped file */
 static void *file_end;	/* pointer to the end of the mapped file */
 static int file_updated; /* flag to state file was changed */
@@ -631,8 +632,11 @@ int main(int argc, char *argv[])
 	int c;
 	int i;
 
-	while ((c = getopt(argc, argv, "w")) >= 0) {
+	while ((c = getopt(argc, argv, "aw")) >= 0) {
 		switch (c) {
+		case 'a':
+			allow_weak_sym_rel = 1;
+			break;
 		case 'w':
 			warn_on_notrace_sect = 1;
 			break;
scripts/recordmcount.h +82 -12

159 lines not on this page · raw message

Signature · 1 line
2.35.1
maintainer
#3 lore raw

Re: [PATCH v2 2/2] ftrace: recordmcount: Handle sections with no non-weak symbols

Quoted 7 lines · On Thu, 28 Apr 2022 22:49:52 +0530 "Naveen N. Rao"…

But, with ppc64 elf abi v1 which only supports the old -pg flag, mcount location can differ between the weak and non-weak variants of a function. In such scenarios, one of the two mcount entries will be invalid. Such architectures need to validate mcount locations by ensuring that the instruction(s) at those locations are as expected. On powerpc, this can be a simple check to ensure that the instruction is a 'bl'. This check can be further tightened as necessary.

I was thinking about this more, and I was thinking that we could create another section; Perhaps __mcount_loc_weak. And place these in that section. That way, we could check if these symbols to see if there's already a symbol for it, and if there is, then drop it.

-- Steve

#4 lore raw

Re: [PATCH v2 2/2] ftrace: recordmcount: Handle sections with no non-weak symbols

Quoted 11 lines · Steven Rostedt
Quoted 7 lines · On Thu, 28 Apr 2022 22:49:52 +0530 "Naveen N. Rao"…

But, with ppc64 elf abi v1 which only supports the old -pg flag, mcount location can differ between the weak and non-weak variants of a function. In such scenarios, one of the two mcount entries will be invalid. Such architectures need to validate mcount locations by ensuring that the instruction(s) at those locations are as expected. On powerpc, this can be a simple check to ensure that the instruction is a 'bl'. This check can be further tightened as necessary.

Quoted 4 lines

I was thinking about this more, and I was thinking that we could create another section; Perhaps __mcount_loc_weak. And place these in that section. That way, we could check if these symbols to see if there's already a symbol for it, and if there is, then drop it.

If I'm understanding your suggestion right:
- we now create a new section in each object file: __mcount_loc_weak, and capture such relocations using weak symbols there.
- we then ask the linker to put these separately between, say, __start_mcount_loc_weak and __stop_mcount_loc_weak
- on ftrace init, we go through entries in this range, but discard those that belong to functions that also have an entry between __start_mcount_loc and __stop_mcount loc.

The primary issue I see here is that the mcount locations within the new weak section will end up being offsets from a different function in vmlinux, since the linker does not create a symbol for the weak functions that were over-ridden.

As an example, in the issue described in this patch set, if powerpc starts over-riding kexec_arch_apply_relocations(), then the current weak implementation in kexec_file.o gets carried over to the final vmlinux, but the instructions will instead appear under the previous function in kexec_file.o: crash_prepare_elf64_headers(). This function may or may not be traced to begin with, so we won't be able to figure out if this is valid or not.

- Naveen

maintainer
#5 lore raw

Re: [PATCH v2 2/2] ftrace: recordmcount: Handle sections with no non-weak symbols

Quoted 3 lines · On Fri, 29 Apr 2022 23:09:19 +0530 "Naveen N. Rao"…

If I'm understanding your suggestion right:
- we now create a new section in each object file: __mcount_loc_weak, and capture such relocations using weak symbols there.

Yes, but it would be putting the same information it puts into __mcount_loc but also add it here too. That is, it will duplicate the data.

Quoted 2 lines

- we then ask the linker to put these separately between, say, __start_mcount_loc_weak and __stop_mcount_loc_weak

Yes, but it will also go in the location between __start_mcount_loc and __stop_mcount_loc.

Quoted 3 lines

- on ftrace init, we go through entries in this range, but discard those that belong to functions that also have an entry between __start_mcount_loc and __stop_mcount loc.

But we should be able to know if it was overridden or not, by seeing if there's another function that was called. Or at least, we can validate them to make sure that they are correct.

Quoted 5 lines

The primary issue I see here is that the mcount locations within the new weak section will end up being offsets from a different function in vmlinux, since the linker does not create a symbol for the weak functions that were over-ridden.

The point of this section is to know which functions in __mcount_loc may have been overridden, as they would be found in the __mcount_loc_weak section. And then we can do something "special" to them.

Quoted 8 lines

As an example, in the issue described in this patch set, if powerpc starts over-riding kexec_arch_apply_relocations(), then the current weak implementation in kexec_file.o gets carried over to the final vmlinux, but the instructions will instead appear under the previous function in kexec_file.o: crash_prepare_elf64_headers(). This function may or may not be traced to begin with, so we won't be able to figure out if this is valid or not.

If it was overridden, then there would be two entries for function that overrides the weak function in the __mcount_loc section, right? One for the new function, and one that was overridden. Of course this could be more complex if the new function had been marked notrace.

I was thinking of doing this just so that we know what functions are weak and perhaps need extra processing.

Another issue with weak functions and ftrace just came up here:

https://lore.kernel.org/all/[email protected]/

-- Steve

#6 lore raw

Re: [PATCH v2 2/2] ftrace: recordmcount: Handle sections with no non-weak symbols

Quoted 25 lines · Steven Rostedt
Quoted 3 lines · On Fri, 29 Apr 2022 23:09:19 +0530 "Naveen N. Rao"…

If I'm understanding your suggestion right:
- we now create a new section in each object file: __mcount_loc_weak, and capture such relocations using weak symbols there.

Yes, but it would be putting the same information it puts into __mcount_loc but also add it here too. That is, it will duplicate the data.

Quoted 2 lines

- we then ask the linker to put these separately between, say, __start_mcount_loc_weak and __stop_mcount_loc_weak

Yes, but it will also go in the location between __start_mcount_loc and __stop_mcount_loc.

Quoted 3 lines

- on ftrace init, we go through entries in this range, but discard those that belong to functions that also have an entry between __start_mcount_loc and __stop_mcount loc.

But we should be able to know if it was overridden or not, by seeing if there's another function that was called. Or at least, we can validate them to make sure that they are correct.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

The primary issue I see here is that the mcount locations within the new weak section will end up being offsets from a different function in vmlinux, since the linker does not create a symbol for the weak functions that were over-ridden.

The point of this section is to know which functions in __mcount_loc may have been overridden, as they would be found in the __mcount_loc_weak section. And then we can do something "special" to them.

I'm not sure I follow that. How are you intending to figure out which functions were overridden by looking at entries in the __mcount_loc_weak section?

In the final vmlinux image, we only get offsets into .text for all mcount locations, but no symbol information. The only hint is the fact that a single kallsym symbol has multiple mcount locations within it. Even then, the symbol with duplicate mcount entries won't be the function that was overridden.

We could do a kallsyms_lookup() on each entry and consult the __mcount_loc_weak section to identify duplicates, but that looks to be very expensive.

Did you have a different approach in mind?

Quoted 7 lines
Quoted 3 lines

As an example, in the issue described in this patch set, if powerpc starts over-riding kexec_arch_apply_relocations(), then the current weak implementation in kexec_file.o gets carried over to the final vmlinux, but the instructions will instead appear under the previous function in kexec_file.o: crash_prepare_elf64_headers(). This function may or may not be traced to begin with, so we won't be able to figure out if this is valid or not.

If it was overridden, then there would be two entries for function that overrides the weak function in the __mcount_loc section, right? One for the new function, and one that was overridden.

In the final vmlinux, we will have two entries: one pointing at the correct function, while the other will point to some other function name. So, at least from kallsym perspective, duplicate mcount entries won't be for the function that was overridden, but some arbitrary function that came before the weak function in the object file.

Quoted 9 lines

Of course this could be more
complex if the new function had been marked notrace.

I was thinking of doing this just so that we know what functions are weak and perhaps need extra processing.

Another issue with weak functions and ftrace just came up here:

https://lore.kernel.org/all/[email protected]/

I noticed this just yesterday:

 # cat available_filter_functions | sort | uniq -d | wc -l
 430

I'm fairly certain that some of those are due to weak functions -- I just wasn't sure if all of those were.

I suppose this will now also be a problem with ftrace_location(), given that it was recently changed to look at an entire function for mcount locations?

Thanks,
Naveen

maintainer
#7 lore raw

Re: [PATCH v2 2/2] ftrace: recordmcount: Handle sections with no non-weak symbols

Quoted 7 lines · On Sat, 30 Apr 2022 01:03:01 +0530 "Naveen N. Rao"…
Quoted 3 lines

The point of this section is to know which functions in __mcount_loc may have been overridden, as they would be found in the __mcount_loc_weak section. And then we can do something "special" to them.

I'm not sure I follow that. How are you intending to figure out which functions were overridden by looking at entries in the __mcount_loc_weak section?

If there's duplicates (or something strange with the offset) then we could delete the one that has the match in the weak section.

Quoted 7 lines

In the final vmlinux image, we only get offsets into .text for all mcount locations, but no symbol information. The only hint is the fact that a single kallsym symbol has multiple mcount locations within it. Even then, the symbol with duplicate mcount entries won't be the function that was overridden.

Quoted 5 lines

We could do a kallsyms_lookup() on each entry and consult the __mcount_loc_weak section to identify duplicates, but that looks to be very expensive.

Did you have a different approach in mind?

We only need to look at the ones in the weak section. How many that is may determine if that's feasible or not.

Quoted 15 lines
Quoted 11 lines
Quoted 10 lines

As an example, in the issue described in this patch set, if powerpc starts over-riding kexec_arch_apply_relocations(), then the current weak implementation in kexec_file.o gets carried over to the final vmlinux, but the instructions will instead appear under the previous function in kexec_file.o: crash_prepare_elf64_headers(). This function may or may not be traced to begin with, so we won't be able to figure out if this is valid or not.

Quoted 3 lines

If it was overridden, then there would be two entries for function that overrides the weak function in the __mcount_loc section, right? One for the new function, and one that was overridden.

Quoted 5 lines

In the final vmlinux, we will have two entries: one pointing at the correct function, while the other will point to some other function name. So, at least from kallsym perspective, duplicate mcount entries won't be for the function that was overridden, but some arbitrary function that came before the weak function in the object file.

Right, and we should be able to find them.

Quoted 11 lines
Quoted 10 lines

Of course this could be more
complex if the new function had been marked notrace.

I was thinking of doing this just so that we know what functions are weak and perhaps need extra processing.

Another issue with weak functions and ftrace just came up here:

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

I noticed this just yesterday:

  # cat available_filter_functions | sort | uniq -d | wc -l
  430

I'm fairly certain that some of those are due to weak functions -- I just wasn't sure if all of those were.

Probably :-/

Quoted 5 lines

I suppose this will now also be a problem with ftrace_location(), given that it was recently changed to look at an entire function for mcount locations?

Maybe. I have to focus on other things at the moment, but I'll try to find some time to look at this deeper.

-- Steve