Skip to contentSkip to conversation

mcount address adjustment

RFC PATCH In review Martin Schwidefsky 5 participants 15 messages May 10, 2011 – May 17, 2011

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

PATCH 1/4 recordmcount mcount address adjustment

In reply to Martin Schwidefsky

From: Martin Schwidefsky <[email protected]>

Introduce mcount_adjust{,_32,_64} to the C implementation of recordmcount analog to $mcount_adjust in the perl script. The adjustment is added to the address of the relocations against the mcount symbol. If this adjustment is done by recordmcount at compile time the ftrace_call_adjust function can be turned into a nop.

---

 scripts/recordmcount.h |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
scripts/recordmcount.h +7 -2
Index: test-2.6/scripts/recordmcount.h
===================================================================
@@ -22,6 +22,7 @@
 #undef is_fake_mcount
 #undef fn_is_fake_mcount
 #undef MIPS_is_fake_mcount
+#undef mcount_adjust
 #undef sift_rel_mcount
 #undef find_secsym_ndx
 #undef __has_rel_mcount
@@ -57,6 +58,7 @@
 # define is_fake_mcount		is_fake_mcount64
 # define fn_is_fake_mcount	fn_is_fake_mcount64
 # define MIPS_is_fake_mcount	MIPS64_is_fake_mcount
+# define mcount_adjust		mcount_adjust_64
 # define Elf_Addr		Elf64_Addr
 # define Elf_Ehdr		Elf64_Ehdr
 # define Elf_Shdr		Elf64_Shdr
@@ -85,6 +87,7 @@
 # define is_fake_mcount		is_fake_mcount32
 # define fn_is_fake_mcount	fn_is_fake_mcount32
 # define MIPS_is_fake_mcount	MIPS32_is_fake_mcount
+# define mcount_adjust		mcount_adjust_32
 # define Elf_Addr		Elf32_Addr
 # define Elf_Ehdr		Elf32_Ehdr
 # define Elf_Shdr		Elf32_Shdr
@@ -123,6 +126,8 @@
 }
 static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
 
+static int mcount_adjust = 0;
+
 /*
  * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
  * _mcount symbol is needed for dynamic function tracer, with it, to disable
@@ -285,8 +290,8 @@
 		}
 
 		if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
-			uint_t const addend = _w(_w(relp->r_offset) - recval);
-
+			uint_t const addend =
+				_w(_w(relp->r_offset) - recval + mcount_adjust);
 			mrelp->r_offset = _w(offbase
 				+ ((void *)mlocp - (void *)mloc0));
 			Elf_r_info(mrelp, recsym, reltype);
#2 lore raw

PATCH 2/4 x86 mcount offset calculation

In reply to Martin Schwidefsky

From: Martin Schwidefsky <[email protected]>

Do the mcount offset adjustment in the recordmcount.pl/recordmcount.[ch] at compile time and not in ftrace_call_adjust at run time.

---

 arch/x86/include/asm/ftrace.h |    7 +++----
 scripts/recordmcount.c        |   10 ++++++++--
 scripts/recordmcount.pl       |    2 ++
 3 files changed, 13 insertions(+), 6 deletions(-)
arch/x86/include/asm/ftrace.h +3 -4
Index: test-2.6/arch/x86/include/asm/ftrace.h
===================================================================
@@ -38,11 +38,10 @@
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
 	/*
-	 * call mcount is "e8 <4 byte offset>"
-	 * The addr points to the 4 byte offset and the caller of this
-	 * function wants the pointer to e8. Simply subtract one.
+	 * addr is the address of the mcount call instruction.
+	 * recordmcount does the necessary offset calculation.
 	 */
-	return addr - 1;
+	return addr;
 }
 
 #ifdef CONFIG_DYNAMIC_FTRACE
scripts/recordmcount.c +8 -2
Index: test-2.6/scripts/recordmcount.c
===================================================================
@@ -300,7 +300,10 @@
 			w2(ehdr->e_machine), fname);
 		fail_file();
 	} break;
-	case EM_386:	 reltype = R_386_32;                   break;
+	case EM_386:
+		reltype = R_386_32;
+		mcount_adjust_32 = -1;
+		break;
 	case EM_ARM:	 reltype = R_ARM_ABS32;
 			 altmcount = "__gnu_mcount_nc";
 			 break;
@@ -311,7 +314,10 @@
 	case EM_S390:    /* reltype: e_class    */ gpfx = '_'; break;
 	case EM_SH:	 reltype = R_SH_DIR32;                 break;
 	case EM_SPARCV9: reltype = R_SPARC_64;     gpfx = '_'; break;
-	case EM_X86_64:	 reltype = R_X86_64_64;                break;
+	case EM_X86_64:
+		reltype = R_X86_64_64;
+		mcount_adjust_64 = -1;
+		break;
 	}  /* end switch */
 
 	switch (ehdr->e_ident[EI_CLASS]) {
scripts/recordmcount.pl +2 -0
Index: test-2.6/scripts/recordmcount.pl
===================================================================
@@ -222,6 +222,7 @@
     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\smcount([+-]0x[0-9a-zA-Z]+)?\$";
     $type = ".quad";
     $alignment = 8;
+    $mcount_adjust = -1;
 
     # force flags for this arch
     $ld .= " -m elf_x86_64";
@@ -231,6 +232,7 @@
 
 } elsif ($arch eq "i386") {
     $alignment = 4;
+    $mcount_adjust = -1;
 
     # force flags for this arch
     $ld .= " -m elf_i386";
#3 lore raw

PATCH 3/4 ia64 mcount offset calculation

In reply to Martin Schwidefsky

From: Martin Schwidefsky <[email protected]>

Do the mcount offset adjustment in the recordmcount.pl/recordmcount.[ch] at compile time and not in ftrace_call_adjust at run time.

---

 arch/ia64/include/asm/ftrace.h |    7 +++++--
 scripts/recordmcount.c         |    7 ++++++-
 scripts/recordmcount.pl        |    1 +
 3 files changed, 12 insertions(+), 3 deletions(-)
arch/ia64/include/asm/ftrace.h +5 -2
Index: test-2.6/arch/ia64/include/asm/ftrace.h
===================================================================
@@ -14,8 +14,11 @@
 
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
-	/* second bundle, insn 2 */
-	return addr - 0x12;
+	/*
+	 * addr is the address of the mcount call instruction.
+	 * recordmcount does the necessary offset calculation.
+	 */
+	return addr;
 }
 
 struct dyn_arch_ftrace {
scripts/recordmcount.c +6 -1
Index: test-2.6/scripts/recordmcount.c
===================================================================
@@ -307,7 +307,12 @@
 	case EM_ARM:	 reltype = R_ARM_ABS32;
 			 altmcount = "__gnu_mcount_nc";
 			 break;
-	case EM_IA_64:	 reltype = R_IA64_IMM64;   gpfx = '_'; break;
+	case EM_IA_64:
+		reltype = R_IA64_IMM64;
+		/* Adjust relocation to second bundle, insn 2 */
+		mcount_adjust_32 = -18;
+		gpfx = '_';
+		break;
 	case EM_MIPS:	 /* reltype: e_class    */ gpfx = '_'; break;
 	case EM_PPC:	 reltype = R_PPC_ADDR32;   gpfx = '_'; break;
 	case EM_PPC64:	 reltype = R_PPC64_ADDR64; gpfx = '_'; break;
scripts/recordmcount.pl +1 -0
Index: test-2.6/scripts/recordmcount.pl
===================================================================
@@ -278,6 +278,7 @@
 
 } elsif ($arch eq "ia64") {
     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
+    $mcount_adjust = -18;
     $type = "data8";
 
     if ($is_module eq "0") {
#9 lore raw

PATCH 4/4 s390 mcount offset calculation

In reply to Martin Schwidefsky

From: Martin Schwidefsky <[email protected]>

Do the mcount offset adjustment in the recordmcount.pl/recordmcount.[ch] at compile time and not in ftrace_call_adjust at run time.

---

 arch/s390/include/asm/ftrace.h |    4 +---
 scripts/recordmcount.c         |    8 ++++++--
 scripts/recordmcount.pl        |    2 ++
 3 files changed, 9 insertions(+), 5 deletions(-)
arch/s390/include/asm/ftrace.h +1 -3
Index: test-2.6/arch/s390/include/asm/ftrace.h
===================================================================
@@ -11,15 +11,13 @@
 
 #ifdef CONFIG_64BIT
 #define MCOUNT_INSN_SIZE  12
-#define MCOUNT_OFFSET	   8
 #else
 #define MCOUNT_INSN_SIZE  20
-#define MCOUNT_OFFSET	   4
 #endif
 
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
-	return addr - MCOUNT_OFFSET;
+	return addr;
 }
 
 #endif /* __ASSEMBLY__ */
scripts/recordmcount.c +6 -2
Index: test-2.6/scripts/recordmcount.c
===================================================================
@@ -338,8 +338,10 @@
 				"unrecognized ET_REL file: %s\n", fname);
 			fail_file();
 		}
-		if (EM_S390 == w2(ehdr->e_machine))
+		if (EM_S390 == w2(ehdr->e_machine)) {
 			reltype = R_390_32;
+			mcount_adjust_32 = -4;
+		}
 		if (EM_MIPS == w2(ehdr->e_machine)) {
 			reltype = R_MIPS_32;
 			is_fake_mcount32 = MIPS32_is_fake_mcount;
@@ -354,8 +356,10 @@
 				"unrecognized ET_REL file: %s\n", fname);
 			fail_file();
 		}
-		if (EM_S390 == w2(ghdr->e_machine))
+		if (EM_S390 == w2(ghdr->e_machine)) {
 			reltype = R_390_64;
+			mcount_adjust_64 = -8;
+		}
 		if (EM_MIPS == w2(ghdr->e_machine)) {
 			reltype = R_MIPS_64;
 			Elf64_r_sym = MIPS64_r_sym;
scripts/recordmcount.pl +2 -0
Index: test-2.6/scripts/recordmcount.pl
===================================================================
@@ -242,12 +242,14 @@
 
 } elsif ($arch eq "s390" && $bits == 32) {
     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_390_32\\s+_mcount\$";
+    $mcount_adjust = -4;
     $alignment = 4;
     $ld .= " -m elf_s390";
     $cc .= " -m31";
 
 } elsif ($arch eq "s390" && $bits == 64) {
     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_390_(PC|PLT)32DBL\\s+_mcount\\+0x2\$";
+    $mcount_adjust = -8;
     $alignment = 8;
     $type = ".quad";
     $ld .= " -m elf64_s390";
#0 lore raw

cover mcount address adjustment

This series of patches aims to simplify the mcount address adjustment for ftrace. The frace code wants to know the start address of each function, not the address of the relocation against the mcount symbol which have been recorded to the __mcount_loc section. The ftrace_call_adjust function is used to calculate the start address of the function from the mcount relocation at runtime.

For x86, ia64 and s390 the adjustment is a constant offset. There is no need to do the adjustment at runtime, it can be done by recordmcount at compile time. Blackfin already does this with the mcount_adjust variable in the pearl version of recordmcount. After teaching the C version of recordmcount about mcount_adjust x86, ia64 and s390 can be converted to a nop ftrace_call_adjust function.

That leaves arm as the last remaining architecture with a non trivial ftrace_call_adjust function. There the least significant bit is removed from the address with an and operation. The comment says this is done for Thumb-2. This implies that for Thumb-1 the offset is 0 and for Thumb-2 the offset is -1, correct? If there is a way to distinguish the two targets in recordmcount at compile time we could convert arm as well. Which would allow us to remove the ftrace_call_adjust function.

Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
#10 lore raw
Quoted 5 lines · Martin Schwidefsky

That leaves arm as the last remaining architecture with a non trivial ftrace_call_adjust function. There the least significant bit is removed from the address with an and operation. The comment says this is done for Thumb-2. This implies that for Thumb-1 the offset is 0 and for Thumb-2 the offset is -1, correct?

ARM supports building the kernel using either the ARM instruction set or the Thumb-2 instruction set. The kernel cannot be built with the "Thumb-1" instruction set (btw usually referred to as just Thumb).

Thumb-2 via recordmcount.pl needs the clearing of the lsb because the relocation (R_ARM_ABS32) that gets used for the assembly file that recordmcount.pl generates and assembles dictates that the lsb be set if the target symbol is Thumb/Thumb-2 function. mcount_adjust would not help here since the ORing is done later, when the relocation is applied.

Thumb-2 via recordmcount.c does not need the clearing of the lsb in ftrace_call_adjust.

Building with the ARM instruction set also does not need the clearing of the lsb.

Quoted 3 lines

Thumb-2 the offset is -1, correct? If there is a way to distinguish the two targets in recordmcount at compile time we could convert arm as well. Which would allow us to remove the ftrace_call_adjust function.

To remove ftrace_call_adjust, we could either deprecate the recordmcount.pl usage for ARM (you already have to edit the Kconfig to use it) or modify it to generate specific relocations explicitly instead of using the assembler data directives.

#11 lore raw
Quoted 12 lines · On Wed, 11 May 2011 22:53:55 +0530 Rabin Vincent <[email protected]> wrote:
Quoted 5 lines · Martin Schwidefsky

That leaves arm as the last remaining architecture with a non trivial ftrace_call_adjust function. There the least significant bit is removed from the address with an and operation. The comment says this is done for Thumb-2. This implies that for Thumb-1 the offset is 0 and for Thumb-2 the offset is -1, correct?

ARM supports building the kernel using either the ARM instruction set or the Thumb-2 instruction set. The kernel cannot be built with the "Thumb-1" instruction set (btw usually referred to as just Thumb).

Quoted 6 lines

Thumb-2 via recordmcount.pl needs the clearing of the lsb because the relocation (R_ARM_ABS32) that gets used for the assembly file that recordmcount.pl generates and assembles dictates that the lsb be set if the target symbol is Thumb/Thumb-2 function. mcount_adjust would not help here since the ORing is done later, when the relocation is applied.

Hmm, from what I can make out the C version of recordmcount uses R_ARM_ABS32 as well.

Quoted 2 lines

Thumb-2 via recordmcount.c does not need the clearing of the lsb in ftrace_call_adjust.

So the clearing of the lsb is only required if the recordmcount.pl script is used?

Quoted 2 lines

Building with the ARM instruction set also does not need the clearing of the lsb.

Who does the ORing? I can't find anything in recordmount.pl/recordmcount.c which looks like doing an OR, does the assembler do that based on the symbol type?

Quoted 8 lines
Quoted 3 lines

Thumb-2 the offset is -1, correct? If there is a way to distinguish the two targets in recordmcount at compile time we could convert arm as well. Which would allow us to remove the ftrace_call_adjust function.

To remove ftrace_call_adjust, we could either deprecate the recordmcount.pl usage for ARM (you already have to edit the Kconfig to use it) or modify it to generate specific relocations explicitly instead of using the assembler data directives.

Hmm, it would be a desirable property if the C version and the pearl version of recordmcount would do the same. Or we could remove the arm support from the pearl script, the C version is faster anyway.

Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
#12 lore raw
Quoted 11 lines · Martin Schwidefsky
Quoted 7 lines · On Wed, 11 May 2011 22:53:55 +0530 Rabin Vincent <[email protected]> wrote:

On Tue, May 10, 2011 at 13:40, Martin Schwidefsky <[email protected]> wrote:
Thumb-2 via recordmcount.pl needs the clearing of the lsb because the relocation (R_ARM_ABS32) that gets used for the assembly file
that recordmcount.pl generates and assembles dictates that the lsb be set if the target symbol is Thumb/Thumb-2 function. ?mcount_adjust would not help here since the ORing is done later, when the relocation is applied.

Hmm, from what I can make out the C version of recordmcount uses R_ARM_ABS32 as well.

Right. It worked when I initially implemented ARM support there because recordmcount.c always found the STT_SECTION symbol as a base and not a STT_FUNC symbol. However, I noticed yesterday that this does not happen in some cases, so I sent a patch to avoid STT_FUNC symbol as bases on ARM, not because of this relocation, but because of a slightly different oddity of Thumb symbols:

http://lkml.org/lkml/2011/5/11/304

(The relocation problem alone could be solved by using R_ARM_ABS32_NOI
instead.)

Quoted 6 lines
Quoted 3 lines

Thumb-2 via recordmcount.c does not need the clearing of the lsb in ftrace_call_adjust.

So the clearing of the lsb is only required if the recordmcount.pl script is used?

Yes.

Quoted 6 lines
Quoted 2 lines

Building with the ARM instruction set also does not need the clearing of the lsb.

Who does the ORing? I can't find anything in recordmount.pl/recordmcount.c which looks like doing an OR, does the assembler do that based on the symbol type?

The lsb is set to 1 by the linker, when it applies the relocations as it links vmlinux.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Thumb-2 the offset is -1, correct? If there is a way to distinguish the two targets in recordmcount at compile time we could convert arm as well. Which would allow us to remove the ftrace_call_adjust function.

Quoted 8 lines
Quoted 4 lines

To remove ftrace_call_adjust, we could either deprecate the recordmcount.pl usage for ARM (you already have to edit the Kconfig to use it) or modify it to generate specific relocations explicitly instead of using the assembler data directives.

Hmm, it would be a desirable property if the C version and the pearl version of recordmcount would do the same. Or we could remove the arm support from the pearl script, the C version is faster anyway.

I'm OK with removing the ARM support from recordmcount.pl; it doesn't seem needed to make significant modifications to it for ARM when we don't use it anyway.

#13 lore raw
Quoted 55 lines · Rabin Vincent
Quoted 11 lines · Martin Schwidefsky
Quoted 7 lines · On Wed, 11 May 2011 22:53:55 +0530 Rabin Vincent <[email protected]> wrote:

On Tue, May 10, 2011 at 13:40, Martin Schwidefsky <[email protected]> wrote:
Thumb-2 via recordmcount.pl needs the clearing of the lsb because the relocation (R_ARM_ABS32) that gets used for the assembly file
that recordmcount.pl generates and assembles dictates that the lsb be set if the target symbol is Thumb/Thumb-2 function. ?mcount_adjust would not help here since the ORing is done later, when the relocation is applied.

Hmm, from what I can make out the C version of recordmcount uses R_ARM_ABS32 as well.

Right. It worked when I initially implemented ARM support there because recordmcount.c always found the STT_SECTION symbol as a base and not a STT_FUNC symbol. However, I noticed yesterday that this does not happen in some cases, so I sent a patch to avoid STT_FUNC symbol as bases on ARM, not because of this relocation, but because of a slightly different oddity of Thumb symbols:

http://lkml.org/lkml/2011/5/11/304

(The relocation problem alone could be solved by using R_ARM_ABS32_NOI
instead.)

Quoted 6 lines
Quoted 3 lines

Thumb-2 via recordmcount.c does not need the clearing of the lsb in ftrace_call_adjust.

So the clearing of the lsb is only required if the recordmcount.pl script is used?

Yes.

Quoted 6 lines
Quoted 2 lines

Building with the ARM instruction set also does not need the clearing of the lsb.

Who does the ORing? I can't find anything in recordmount.pl/recordmcount.c which looks like doing an OR, does the assembler do that based on the symbol type?

The lsb is set to 1 by the linker, when it applies the relocations as it links vmlinux.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Thumb-2 the offset is -1, correct? If there is a way to distinguish the two targets in recordmcount at compile time we could convert arm as well. Which would allow us to remove the ftrace_call_adjust function.

To remove ftrace_call_adjust, we could either deprecate the recordmcount.pl usage for ARM (you already have to edit the Kconfig to use it) or modify it to generate specific relocations explicitly instead of using the assembler data directives.

Quoted 7 lines
Quoted 3 lines

Hmm, it would be a desirable property if the C version and the pearl version of recordmcount would do the same. Or we could remove the arm support from the pearl script, the C version is faster anyway.

I'm OK with removing the ARM support from recordmcount.pl; it doesn't seem needed to make significant modifications to it for ARM when we don't use it anyway.

Is there any reason why the recordmcount.pl would ever be used now that the C implementation exists?

I notice that arch/arm/Kconfig has:

config ARM
...
select HAVE_C_RECORDMCOUNT

so deprecating ARM support from recordmcount.pl seems unlikely to hurt anyone.

The C implementation seems to have worked fine when I was testing dynamic ftrace with Thumb-2 recently.

Cheers
---Dave

maintainer
#14 lore raw
Quoted 9 lines · Dave Martin

Is there any reason why the recordmcount.pl would ever be used now that the C implementation exists?

I notice that arch/arm/Kconfig has:

config ARM
...
select HAVE_C_RECORDMCOUNT

Quoted 5 lines

so deprecating ARM support from recordmcount.pl seems unlikely to hurt anyone.

The C implementation seems to have worked fine when I was testing dynamic ftrace with Thumb-2 recently.

It's there only as a backup. But you're right. I may as well start removing the recordmcount.pl support from those that have the HAVE_C_RECORDMCOUNT set, and then all users are gone (which may now be the case) either keep it around as a backup for testing against recordmcount.c, or remove it completely.

-- Steve

maintainer
#4 lore raw

Re: [patch 3/4] ia64 mcount offset calculation

In reply to Martin Schwidefsky

Tony,

Can you give me an Acked-by for this patch?

Thanks,

-- Steve

Quoted 62 lines · Martin Schwidefsky

plain text document attachment (302-mcount-adjust-ia64.diff)
From: Martin Schwidefsky <[email protected]>

Do the mcount offset adjustment in the recordmcount.pl/recordmcount.[ch] at compile time and not in ftrace_call_adjust at run time.

---

 arch/ia64/include/asm/ftrace.h |    7 +++++--
 scripts/recordmcount.c         |    7 ++++++-
 scripts/recordmcount.pl        |    1 +
 3 files changed, 12 insertions(+), 3 deletions(-)

Index: test-2.6/arch/ia64/include/asm/ftrace.h
===================================================================

arch/ia64/include/asm/ftrace.h +5 -2
@@ -14,8 +14,11 @@
 
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
-	/* second bundle, insn 2 */
-	return addr - 0x12;
+	/*
+	 * addr is the address of the mcount call instruction.
+	 * recordmcount does the necessary offset calculation.
+	 */
+	return addr;
 }
 
 struct dyn_arch_ftrace {

Index: test-2.6/scripts/recordmcount.c
===================================================================

scripts/recordmcount.c +6 -1
@@ -307,7 +307,12 @@
 	case EM_ARM:	 reltype = R_ARM_ABS32;
 			 altmcount = "__gnu_mcount_nc";
 			 break;
-	case EM_IA_64:	 reltype = R_IA64_IMM64;   gpfx = '_'; break;
+	case EM_IA_64:
+		reltype = R_IA64_IMM64;
+		/* Adjust relocation to second bundle, insn 2 */
+		mcount_adjust_32 = -18;
+		gpfx = '_';
+		break;
 	case EM_MIPS:	 /* reltype: e_class    */ gpfx = '_'; break;
 	case EM_PPC:	 reltype = R_PPC_ADDR32;   gpfx = '_'; break;
 	case EM_PPC64:	 reltype = R_PPC64_ADDR64; gpfx = '_'; break;

Index: test-2.6/scripts/recordmcount.pl
===================================================================

scripts/recordmcount.pl +1 -0
@@ -278,6 +278,7 @@
 
 } elsif ($arch eq "ia64") {
     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
+    $mcount_adjust = -18;
     $type = "data8";
 
     if ($is_module eq "0") {
#5 lore raw

RE: [patch 3/4] ia64 mcount offset calculation

I'm not sure how to test it - but it looks somewhat suspect.

The difficulty is in the way that ia64 tools report program addresses within bundles.

ia64 crams 3 41-bit instructions (and a 5-bit template) into a 16-byte "bundle".

Some tools report consecutive addresses like this:

0x000
0x001
0x002
0x010
0x011
0x012

where the low nibble is the instruction number within the bundle. Others go for:

0x000
0x006
0x00c
0x010
0x016
0x01c

with the illusion that instructions take 6-byte, 6bytes, 4-bytes.

This patch does some fast and loose translation from hex (0x12) in the kernel to decimal (18) in the user tools ... which doesn't look like it fits with either of these conventions.

-Tony

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Steven Rostedt
Sent: Monday, May 16, 2011 11:58 AM
To: Luck, Tony
Cc: Martin Schwidefsky; [email protected]
Subject: Re: [patch 3/4] ia64 mcount offset calculation

Tony,

Can you give me an Acked-by for this patch?

Thanks,

-- Steve

Quoted 55 lines · Martin Schwidefsky

plain text document attachment (302-mcount-adjust-ia64.diff)
From: Martin Schwidefsky <[email protected]>

Do the mcount offset adjustment in the recordmcount.pl/recordmcount.[ch] at compile time and not in ftrace_call_adjust at run time.

---

 arch/ia64/include/asm/ftrace.h |    7 +++++--
 scripts/recordmcount.c         |    7 ++++++-
 scripts/recordmcount.pl        |    1 +
 3 files changed, 12 insertions(+), 3 deletions(-)

Index: test-2.6/arch/ia64/include/asm/ftrace.h
===================================================================

arch/ia64/include/asm/ftrace.h +5 -2
@@ -14,8 +14,11 @@
 
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
-	/* second bundle, insn 2 */
-	return addr - 0x12;
+	/*
+	 * addr is the address of the mcount call instruction.
+	 * recordmcount does the necessary offset calculation.
+	 */
+	return addr;
 }
 
 struct dyn_arch_ftrace {

Index: test-2.6/scripts/recordmcount.c
===================================================================

scripts/recordmcount.c +6 -1
@@ -307,7 +307,12 @@
 	case EM_ARM:	 reltype = R_ARM_ABS32;
 			 altmcount = "__gnu_mcount_nc";
 			 break;
-	case EM_IA_64:	 reltype = R_IA64_IMM64;   gpfx = '_'; break;
+	case EM_IA_64:
+		reltype = R_IA64_IMM64;
+		/* Adjust relocation to second bundle, insn 2 */
+		mcount_adjust_32 = -18;
+		gpfx = '_';
+		break;
 	case EM_MIPS:	 /* reltype: e_class    */ gpfx = '_'; break;
 	case EM_PPC:	 reltype = R_PPC_ADDR32;   gpfx = '_'; break;
 	case EM_PPC64:	 reltype = R_PPC64_ADDR64; gpfx = '_'; break;

Index: test-2.6/scripts/recordmcount.pl
===================================================================

scripts/recordmcount.pl line 284

Quoted 7 lines
scripts/recordmcount.pl +1 -0
@@ -278,6 +278,7 @@
 
 } elsif ($arch eq "ia64") {
     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
+    $mcount_adjust = -18;
     $type = "data8";
 
     if ($is_module eq "0") {

--
To unsubscribe from this list: send the line "unsubscribe linux-arch" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#6 lore raw

RE: [patch 3/4] ia64 mcount offset calculation

Quoted 1 line · Tony

I'm not sure how to test it - but it looks somewhat suspect.

You could apply the first patch and this patch and see if ftrace still works :)

Quoted 17 lines

The difficulty is in the way that ia64 tools report program addresses within bundles.

ia64 crams 3 41-bit instructions (and a 5-bit template) into a 16-byte "bundle".

Some tools report consecutive addresses like this:

0x000
0x001
0x002
0x010
0x011
0x012

where the low nibble is the instruction number within the bundle. Others go for:

Quoted 8 lines

0x000
0x006
0x00c
0x010
0x016
0x01c

with the illusion that instructions take 6-byte, 6bytes, 4-bytes.

And I'm amazed that this wonderful architecture didn't become the defacto for all users!

Quoted 4 lines

This patch does some fast and loose translation from hex (0x12) in the kernel to decimal (18) in the user tools ... which doesn't look like it fits with either of these conventions.

Could be broken. I'll leave it out of my next push.

Thanks,

-- Steve

#7 lore raw

Re: [patch 3/4] ia64 mcount offset calculation

In reply to Luck, Tony
Quoted 3 lines · Tony

This patch does some fast and loose translation from hex (0x12) in the kernel to decimal (18) in the user tools ... which doesn't look like it fits with either of these conventions.

All the patch does is to move an address calculation from the kernel "addr-0x12" to user space by subtracting the constant offset from the addend value for the
relocation: "addend = _w(_w(relp->r_offset) - recval + mcount_adjust)".

The offset for ia64 is -18. I'd be very surprised if the outcome of the move would yield a different end result. But as I took another look at the patch I noticed a bug, for EM_IA64 we need to set mcount_adjust_64 and not mcount_adjust_32 as it is a 64-bit architecture. New patch below.

I tried to do a test compile with/without the patch but with the latest git kernels TRACING_SUPPORT depends on TRACE_IRQFLAGS_SUPPORT which is missing for ia64. No luck ..

---
Subject: [PATCH] ia64 mcount offset calculation

From: Martin Schwidefsky <[email protected]>

Do the mcount offset adjustment in the recordmcount.pl/recordmcount.[ch] at compile time and not in ftrace_call_adjust at run time.

---

 arch/ia64/include/asm/ftrace.h |    7 +++++--
 scripts/recordmcount.c         |    7 ++++++-
 scripts/recordmcount.pl        |    1 +
 3 files changed, 12 insertions(+), 3 deletions(-)
arch/ia64/include/asm/ftrace.h +5 -2
diff -urpN linux-2.6/arch/ia64/include/asm/ftrace.h linux-2.6-patched/arch/ia64/include/asm/ftrace.h
@@ -14,8 +14,11 @@ extern void _mcount(unsigned long pfs, u
 
 static inline unsigned long ftrace_call_adjust(unsigned long addr)
 {
-	/* second bundle, insn 2 */
-	return addr - 0x12;
+	/*
+	 * addr is the address of the mcount call instruction.
+	 * recordmcount does the necessary offset calculation.
+	 */
+	return addr;
 }
 
 struct dyn_arch_ftrace {
scripts/recordmcount.c +6 -1
diff -urpN linux-2.6/scripts/recordmcount.c linux-2.6-patched/scripts/recordmcount.c
@@ -307,7 +307,12 @@ do_file(char const *const fname)
 	case EM_ARM:	 reltype = R_ARM_ABS32;
 			 altmcount = "__gnu_mcount_nc";
 			 break;
-	case EM_IA_64:	 reltype = R_IA64_IMM64;   gpfx = '_'; break;
+	case EM_IA_64:
+		reltype = R_IA64_IMM64;
+		/* Adjust relocation to second bundle, insn 2 */
+		mcount_adjust_64 = -18;
+		gpfx = '_';
+		break;
 	case EM_MIPS:	 /* reltype: e_class    */ gpfx = '_'; break;
 	case EM_PPC:	 reltype = R_PPC_ADDR32;   gpfx = '_'; break;
 	case EM_PPC64:	 reltype = R_PPC64_ADDR64; gpfx = '_'; break;
scripts/recordmcount.pl +1 -0
diff -urpN linux-2.6/scripts/recordmcount.pl linux-2.6-patched/scripts/recordmcount.pl
@@ -278,6 +278,7 @@ if ($arch eq "x86_64") {
 
 } elsif ($arch eq "ia64") {
     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
+    $mcount_adjust = -18;
     $type = "data8";
 
     if ($is_module eq "0") {
Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
maintainer
#8 lore raw

Re: [patch 3/4] ia64 mcount offset calculation

Quoted 5 lines · Martin Schwidefsky

On Mon, 16 May 2011 12:17:15 -0700

I tried to do a test compile with/without the patch but with the latest git kernels TRACING_SUPPORT depends on TRACE_IRQFLAGS_SUPPORT which is missing for ia64. No luck ..

How hard would it be to add TRACE_IRQFLAGS_SUPPORT. That should allow for including not only tracing, but also lockdep.

Although, I may be able to make function tracing work without it.

-- Steve