Skip to contentSkip to conversation

tools/perf: Add text_end to "struct dso" to save .text section size

PATCH Superseded Athira Rajeev 2 participants 6 messages Aug 17, 2023 – Sep 7, 2023 perf events

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 tools/perf: Add text_end to "struct dso" to save .text section size

Update "struct dso" to include new member "text_end". This new field will represent the offset for end of text section for a dso. This value is derived as:
sh_size (Size of section in byes) + sh_offset (Section file offst) of the elf header for text.

To resolve the address from a sample, perf looks at the DSO maps. In case of address from a kernel module, there were some address found to be not resolved. This was observed while running perf test for "Object code reading". Though the ip falls beteen the start address of the loaded module (perf map->start ) and end address ( perf map->end), it was unresolved.

Example:

    Reading object code for memory address: 0xc008000007f0142c
    File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    On file address is: 0x1114cc
    Objdump command is: objdump -z -d --start-address=0x11142c --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    objdump read too few bytes: 128
    test child finished with -1

Here, module is loaded at:

    # cat /proc/modules | grep xfs
    xfs 2228224 3 - Live 0xc008000007d00000

From objdump for xfs module, text section is:

    text 0010f7bc  0000000000000000 0000000000000000 000000a0 2**4

Here the offset for 0xc008000007f0142c ie 0x112074 falls out .text section which is up to 0x10f7bc.

In this case for module, the address 0xc008000007e11fd4 is pointing to stub instructions. This address range represents the module stubs which is allocated on module load and hence is not part of DSO offset.

To identify such address, which falls out of text section and within module end, added the new field "text_end" to "struct dso".

---
Changelog:
v1 -> v2:
Changed commit message to explain need for "text_end"

 tools/perf/util/dso.h        | 1 +
 tools/perf/util/symbol-elf.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/dso.h +1 -0
index b41c9782c754..70fe0fe69bef 100644
@@ -181,6 +181,7 @@ struct dso {
 	u8		 rel;
 	struct build_id	 bid;
 	u64		 text_offset;
+	u64		 text_end;
 	const char	 *short_name;
 	const char	 *long_name;
 	u16		 long_name_len;
tools/perf/util/symbol-elf.c +3 -1
index 8bd466d1c2bd..252d26a59e64 100644
@@ -1512,8 +1512,10 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	}
 
 	if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
-				".text", NULL))
+				".text", NULL)) {
 		dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
+		dso->text_end = tshdr.sh_offset + tshdr.sh_size;
+	}
 
 	if (runtime_ss->opdsec)
 		opddata = elf_rawdata(runtime_ss->opdsec, NULL);
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 2/2 tools/perf/tests: Fix object code reading to skip address that falls out of text section

The testcase "Object code reading" fails in somecases for "fs_something" sub test as below:

    Reading object code for memory address: 0xc008000007f0142c
    File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    On file address is: 0x1114cc
    Objdump command is: objdump -z -d --start-address=0x11142c --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    objdump read too few bytes: 128
    test child finished with -1

This can alo be reproduced when running perf record with workload that exercises fs_something() code. In the test setup, this is exercising xfs code since root is xfs.

    # perf record ./a.out
    # perf report -v |grep "xfs.ko"
      0.76% a.out /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007de5efc B [k] xlog_cil_commit
      0.74% a.out  /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007d5ae18 B [k] xfs_btree_key_offset
      0.74% a.out  /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007e11fd4 B [k] 0x0000000000112074

Here addr "0xc008000007e11fd4" is not resolved. since this is a kernel module, its offset is from the DSO. Xfs module is loaded at 0xc008000007d00000

   # cat /proc/modules | grep xfs
    xfs 2228224 3 - Live 0xc008000007d00000

And size is 0x220000. So its loaded between  0xc008000007d00000 and 0xc008000007f20000. From objdump, text section is:

    text 0010f7bc  0000000000000000 0000000000000000 000000a0 2**4

Hence perf captured ip maps to 0x112074 which is:
( ip - start of module ) + a0

This offset 0x112074 falls out .text section which is up to 0x10f7bc In this case for module, the address 0xc008000007e11fd4 is pointing to stub instructions. This address range represents the module stubs which is allocated on module load and hence is not part of DSO offset.

To address this issue in "object code reading", skip the sample if address falls out of text section and is within the module end. Use the "text_end" member of "struct dso" to do this check.

To address this issue in "perf report", exploring an option of having stubs range as part of the /proc/kallsyms, so that perf report can resolve addresses in stubs range

However this patch uses text_end to skip the stub range for Object code reading testcase.

---
 tools/perf/tests/code-reading.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/perf/tests/code-reading.c +8 -0
index ed3815163d1b..911f8fa13677 100644
@@ -269,6 +269,14 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
 	if (addr + len > map__end(al.map))
 		len = map__end(al.map) - addr;
 
+	/* Check if the ip offset falls in stubs sections for kernel modules */
+	if (strstr(dso->long_name, ".ko")) {
+		if ((al.addr < map__end(al.map)) && (al.addr > dso->text_end)) {
+			pr_debug(" - skipping\n");
+			goto out;
+		}
+	}
+
 	/* Read the object code using perf */
 	ret_len = dso__data_read_offset(dso, maps__machine(thread__maps(thread)),
 					al.addr, buf1, len);
Signature · 1 line
2.31.1
reviewer
#2 lore raw

Re: [PATCH V2 2/2] tools/perf/tests: Fix object code reading to skip address that falls out of text section

Quoted 61 lines · Athira Rajeev

The testcase "Object code reading" fails in somecases for "fs_something" sub test as below:

    Reading object code for memory address: 0xc008000007f0142c
    File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    On file address is: 0x1114cc
    Objdump command is: objdump -z -d --start-address=0x11142c --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    objdump read too few bytes: 128
    test child finished with -1

This can alo be reproduced when running perf record with workload that exercises fs_something() code. In the test setup, this is exercising xfs code since root is xfs.

    # perf record ./a.out
    # perf report -v |grep "xfs.ko"
      0.76% a.out /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007de5efc B [k] xlog_cil_commit
      0.74% a.out  /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007d5ae18 B [k] xfs_btree_key_offset
      0.74% a.out  /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007e11fd4 B [k] 0x0000000000112074

Here addr "0xc008000007e11fd4" is not resolved. since this is a kernel module, its offset is from the DSO. Xfs module is loaded at 0xc008000007d00000

   # cat /proc/modules | grep xfs
    xfs 2228224 3 - Live 0xc008000007d00000

And size is 0x220000. So its loaded between  0xc008000007d00000 and 0xc008000007f20000. From objdump, text section is:

    text 0010f7bc  0000000000000000 0000000000000000 000000a0 2**4

Hence perf captured ip maps to 0x112074 which is:
( ip - start of module ) + a0

This offset 0x112074 falls out .text section which is up to 0x10f7bc In this case for module, the address 0xc008000007e11fd4 is pointing to stub instructions. This address range represents the module stubs which is allocated on module load and hence is not part of DSO offset.

To address this issue in "object code reading", skip the sample if address falls out of text section and is within the module end. Use the "text_end" member of "struct dso" to do this check.

To address this issue in "perf report", exploring an option of having stubs range as part of the /proc/kallsyms, so that perf report can resolve addresses in stubs range

However this patch uses text_end to skip the stub range for Object code reading testcase.

---
 tools/perf/tests/code-reading.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/perf/tests/code-reading.c +0 -0
index ed3815163d1b..911f8fa13677 100644

tools/perf/tests/code-reading.c static int read_object_code(u64 addr, size_t len, u8 cpumode, · line 272

Quoted 4 lines
tools/perf/tests/code-reading.c +1 -0
@@ -269,6 +269,14 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
 	if (addr + len > map__end(al.map))
 		len = map__end(al.map) - addr;
 
+	/* Check if the ip offset falls in stubs sections for kernel modules */

What arches have stubs - maybe expand the comment a bit e.g.
"Some architectures (such as blah blah) have stubs (trampolines) in kernel modules to manage long jumps"

tools/perf/tests/code-reading.c static int read_object_code(u64 addr, size_t len, u8 cpumode, · line 274

Quoted 2 lines
tools/perf/tests/code-reading.c +2 -0
+	if (strstr(dso->long_name, ".ko")) {
+		if ((al.addr < map__end(al.map)) && (al.addr > dso->text_end)) {

Why check al.addr < map__end(al.map) ? addr must be on the map mustn't it?

Also please remove redundant parentheses.

tools/perf/tests/code-reading.c static int read_object_code(u64 addr, size_t len, u8 cpumode, · line 275

Quoted 1 line
tools/perf/tests/code-reading.c +1 -0
+			pr_debug(" - skipping\n");

"skipping" but why. Maybe "skipping module address after text end"

Quoted 7 lines
tools/perf/tests/code-reading.c +4 -0
+			goto out;
+		}
+	}
+
 	/* Read the object code using perf */
 	ret_len = dso__data_read_offset(dso, maps__machine(thread__maps(thread)),
 					al.addr, buf1, len);
#3 lore raw

Re: [PATCH V2 2/2] tools/perf/tests: Fix object code reading to skip address that falls out of text section

Quoted 69 lines · Adrian Hunter
Quoted 64 lines · Athira Rajeev

The testcase "Object code reading" fails in somecases for "fs_something" sub test as below:

Reading object code for memory address: 0xc008000007f0142c
File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
On file address is: 0x1114cc
Objdump command is: objdump -z -d --start-address=0x11142c --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko objdump read too few bytes: 128
test child finished with -1

This can alo be reproduced when running perf record with workload that exercises fs_something() code. In the test setup, this is exercising xfs code since root is xfs.

   # perf record ./a.out
   # perf report -v |grep "xfs.ko"
     0.76% a.out /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007de5efc B [k] xlog_cil_commit
     0.74% a.out  /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007d5ae18 B [k] xfs_btree_key_offset
     0.74% a.out  /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko  0xc008000007e11fd4 B [k] 0x0000000000112074

Here addr "0xc008000007e11fd4" is not resolved. since this is a kernel module, its offset is from the DSO. Xfs module is loaded at 0xc008000007d00000

  # cat /proc/modules | grep xfs
   xfs 2228224 3 - Live 0xc008000007d00000

And size is 0x220000. So its loaded between 0xc008000007d00000 and 0xc008000007f20000. From objdump, text section is:

   text 0010f7bc  0000000000000000 0000000000000000 000000a0 2**4

Hence perf captured ip maps to 0x112074 which is:
( ip - start of module ) + a0

This offset 0x112074 falls out .text section which is up to 0x10f7bc In this case for module, the address 0xc008000007e11fd4 is pointing to stub instructions. This address range represents the module stubs which is allocated on module load and hence is not part of DSO offset.

To address this issue in "object code reading", skip the sample if address falls out of text section and is within the module end. Use the "text_end" member of "struct dso" to do this check.

To address this issue in "perf report", exploring an option of having stubs range as part of the /proc/kallsyms, so that perf report can resolve addresses in stubs range

However this patch uses text_end to skip the stub range for Object code reading testcase.

---
tools/perf/tests/code-reading.c | 8 ++++++++

1 file changed, 8 insertions(+)
tools/perf/tests/code-reading.c +0 -0
index ed3815163d1b..911f8fa13677 100644
@@ -269,6 +269,14 @@ static int read_object_code(u64 addr, size_t len, u8 cpumode,
 if (addr + len > map__end(al.map))
 len = map__end(al.map) - addr;
 
Quoted 4 lines

tools/perf/tests/code-reading.c static int read_object_code(u64 addr, size_t len, u8 cpumode, · line 272

Quoted 1 line
tools/perf/tests/code-reading.c +1 -0
+ /* Check if the ip offset falls in stubs sections for kernel modules */

What arches have stubs - maybe expand the comment a bit e.g.
"Some architectures (such as blah blah) have stubs (trampolines) in kernel modules to manage long jumps”

Ok

Quoted 5 lines

tools/perf/tests/code-reading.c static int read_object_code(u64 addr, size_t len, u8 cpumode, · line 274

Quoted 3 lines
tools/perf/tests/code-reading.c +2 -0
+ if (strstr(dso->long_name, ".ko")) {
+ if ((al.addr < map__end(al.map)) && (al.addr > dso->text_end)) {

Why check al.addr < map__end(al.map) ? addr must be on the map mustn't it?

Yes, you are right.

Quoted 6 lines

Also please remove redundant parentheses.

tools/perf/tests/code-reading.c static int read_object_code(u64 addr, size_t len, u8 cpumode, · line 275

Quoted 1 line
tools/perf/tests/code-reading.c +1 -0
+ pr_debug(" - skipping\n");

"skipping" but why. Maybe "skipping module address after text end"

Sure, will post V2 with these changes

Thanks
Athira

Quoted 8 lines
Quoted 8 lines
tools/perf/tests/code-reading.c +4 -0
+ goto out;
+ }
+ }
+
 /* Read the object code using perf */
 ret_len = dso__data_read_offset(dso, maps__machine(thread__maps(thread)),
 al.addr, buf1, len);
reviewer
#4 lore raw
In reply to Athira Rajeev
Quoted 70 lines · Athira Rajeev

Update "struct dso" to include new member "text_end". This new field will represent the offset for end of text section for a dso. This value is derived as:
sh_size (Size of section in byes) + sh_offset (Section file offst) of the elf header for text.

To resolve the address from a sample, perf looks at the DSO maps. In case of address from a kernel module, there were some address found to be not resolved. This was observed while running perf test for "Object code reading". Though the ip falls beteen the start address of the loaded module (perf map->start ) and end address ( perf map->end), it was unresolved.

Example:

    Reading object code for memory address: 0xc008000007f0142c
    File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    On file address is: 0x1114cc
    Objdump command is: objdump -z -d --start-address=0x11142c --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
    objdump read too few bytes: 128
    test child finished with -1

Here, module is loaded at:

    # cat /proc/modules | grep xfs
    xfs 2228224 3 - Live 0xc008000007d00000

From objdump for xfs module, text section is:

    text 0010f7bc  0000000000000000 0000000000000000 000000a0 2**4

Here the offset for 0xc008000007f0142c ie 0x112074 falls out .text section which is up to 0x10f7bc.

In this case for module, the address 0xc008000007e11fd4 is pointing to stub instructions. This address range represents the module stubs which is allocated on module load and hence is not part of DSO offset.

To identify such address, which falls out of text section and within module end, added the new field "text_end" to "struct dso".

---
Changelog:
v1 -> v2:
Changed commit message to explain need for "text_end"

 tools/perf/util/dso.h        | 1 +
 tools/perf/util/symbol-elf.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/dso.h +1 -0
index b41c9782c754..70fe0fe69bef 100644
@@ -181,6 +181,7 @@ struct dso {
 	u8		 rel;
 	struct build_id	 bid;
 	u64		 text_offset;
+	u64		 text_end;
 	const char	 *short_name;
 	const char	 *long_name;
 	u16		 long_name_len;
tools/perf/util/symbol-elf.c +0 -0
index 8bd466d1c2bd..252d26a59e64 100644
@@ -1512,8 +1512,10 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	}

tools/perf/util/symbol-elf.c dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss, · line 1521

Quoted 10 lines
tools/perf/util/symbol-elf.c +3 -1
 
 	if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
-				".text", NULL))
+				".text", NULL)) {
 		dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
+		dso->text_end = tshdr.sh_offset + tshdr.sh_size;
+	}
 
 	if (runtime_ss->opdsec)
 		opddata = elf_rawdata(runtime_ss->opdsec, NULL);

Should probably amend dso__load_bfd_symbols() also

#5 lore raw
Quoted 83 lines · Adrian Hunter
Quoted 78 lines · Athira Rajeev

Update "struct dso" to include new member "text_end". This new field will represent the offset for end of text section for a dso. This value is derived as:
sh_size (Size of section in byes) + sh_offset (Section file offst) of the elf header for text.

To resolve the address from a sample, perf looks at the DSO maps. In case of address from a kernel module, there were some address found to be not resolved. This was observed while running perf test for "Object code reading". Though the ip falls beteen the start address of the loaded module (perf map->start ) and end address ( perf map->end), it was unresolved.

Example:

Reading object code for memory address: 0xc008000007f0142c
File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
On file address is: 0x1114cc
Objdump command is: objdump -z -d --start-address=0x11142c --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko objdump read too few bytes: 128
test child finished with -1

Here, module is loaded at:

   # cat /proc/modules | grep xfs
   xfs 2228224 3 - Live 0xc008000007d00000

From objdump for xfs module, text section is:

   text 0010f7bc  0000000000000000 0000000000000000 000000a0 2**4

Here the offset for 0xc008000007f0142c ie 0x112074 falls out .text section which is up to 0x10f7bc.

In this case for module, the address 0xc008000007e11fd4 is pointing to stub instructions. This address range represents the module stubs which is allocated on module load and hence is not part of DSO offset.

To identify such address, which falls out of text section and within module end, added the new field "text_end" to "struct dso".

---
Changelog:
v1 -> v2:
Changed commit message to explain need for "text_end"

tools/perf/util/dso.h | 1 +
tools/perf/util/symbol-elf.c | 4 +++-

2 files changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/dso.h +1 -0
index b41c9782c754..70fe0fe69bef 100644
@@ -181,6 +181,7 @@ struct dso {
 u8  rel;
 struct build_id  bid;
 u64  text_offset;
+ u64  text_end;
 const char  *short_name;
 const char  *long_name;
 u16  long_name_len;
tools/perf/util/symbol-elf.c +3 -1
index 8bd466d1c2bd..252d26a59e64 100644
@@ -1512,8 +1512,10 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 }
 
 if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
- ".text", NULL))
+ ".text", NULL)) {
 dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
+ dso->text_end = tshdr.sh_offset + tshdr.sh_size;
+ }
 
Quoted 4 lines

tools/perf/util/symbol-elf.c dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss, · line 1521

Quoted 2 lines
tools/perf/util/symbol-elf.c +0 -0
 if (runtime_ss->opdsec)
 opddata = elf_rawdata(runtime_ss->opdsec, NULL);

Should probably amend dso__load_bfd_symbols() also

Hi Adrian,

Thanks for checking. Will address this in V2

Thanks
Athira