All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: <dwarves@vger.kernel.org>, <acme@kernel.org>, <bpf@vger.kernel.org>
Cc: <andrii@kernel.org>, <kernel-team@fb.com>,
	Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@redhat.com>
Subject: [PATCH dwarves 1/2] btf_encoder: fix BTF variable generation for kernel modules
Date: Thu, 10 Dec 2020 20:11:37 -0800	[thread overview]
Message-ID: <20201211041139.589692-2-andrii@kernel.org> (raw)
In-Reply-To: <20201211041139.589692-1-andrii@kernel.org>

Fix pahole's logic for determining per-CPU variables. For vmlinux,
btfe->percpu_base_addr is always 0, so it didn't matter at which point to
subtract it to get offset that later was matched against corresponding ELF
symbol.

For kernel module, though, the situation is different. Kernel module's per-CPU
data section has non-zero offset, which is taken into account in all DWARF
variable addresses calculation. For such cases, it's important to subtract
section offset (btfe->percpu_base_addr) before ELF symbol look up is
performed.

This patch also records per-CPU data section size and uses it for early
filtering of non-per-CPU variables by their address.

Fixes: 2e719cca6672 ("btf_encoder: revamp how per-CPU variables are encoded")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 btf_encoder.c | 21 ++++++++++++++++-----
 libbtf.c      |  1 +
 libbtf.h      |  1 +
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index c40f059580da..a7d484765ce2 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -651,7 +651,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 		printf("search cu '%s' for percpu global variables.\n", cu->name);
 
 	cu__for_each_variable(cu, core_id, pos) {
-		uint32_t size, type, linkage, offset;
+		uint32_t size, type, linkage;
 		const char *name;
 		uint64_t addr;
 		int id;
@@ -665,12 +665,24 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 
 		/* addr has to be recorded before we follow spec */
 		addr = var->ip.addr;
-		if (var->spec)
-			var = var->spec;
+
+		/* DWARF takes into account .data..percpu section offset
+		 * within its segment, which for vmlinux is 0, but for kernel
+		 * modules is >0. ELF symbols, on the other hand, don't take
+		 * into account these offsets (as they are relative to the
+		 * section start), so to match DWARF and ELF symbols we need
+		 * to negate the section base address here.
+		 */
+		if (addr < btfe->percpu_base_addr || addr >= btfe->percpu_base_addr + btfe->percpu_sec_sz)
+			continue;
+		addr -= btfe->percpu_base_addr;
 
 		if (!percpu_var_exists(addr, &size, &name))
 			continue; /* not a per-CPU variable */
 
+		if (var->spec)
+			var = var->spec;
+
 		if (var->ip.tag.type == 0) {
 			fprintf(stderr, "error: found variable '%s' in CU '%s' that has void type\n",
 				name, cu->name);
@@ -701,8 +713,7 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 		 * add a BTF_VAR_SECINFO in btfe->percpu_secinfo, which will be added into
 		 * btfe->types later when we add BTF_VAR_DATASEC.
 		 */
-		offset = addr - btfe->percpu_base_addr;
-		id = btf_elf__add_var_secinfo(&btfe->percpu_secinfo, id, offset, size);
+		id = btf_elf__add_var_secinfo(&btfe->percpu_secinfo, id, addr, size);
 		if (id < 0) {
 			err = -1;
 			fprintf(stderr, "error: failed to encode section info for variable '%s' at addr 0x%lx\n",
diff --git a/libbtf.c b/libbtf.c
index 246762c4b4e1..16e1d451e433 100644
--- a/libbtf.c
+++ b/libbtf.c
@@ -170,6 +170,7 @@ try_as_raw_btf:
 	}
 	btfe->percpu_shndx = elf_ndxscn(sec);
 	btfe->percpu_base_addr = shdr.sh_addr;
+	btfe->percpu_sec_sz = shdr.sh_size;
 
 	return btfe;
 
diff --git a/libbtf.h b/libbtf.h
index 71f6cecbea93..191f5862a695 100644
--- a/libbtf.h
+++ b/libbtf.h
@@ -26,6 +26,7 @@ struct btf_elf {
 	bool		  raw_btf; // "/sys/kernel/btf/vmlinux"
 	uint32_t	  percpu_shndx;
 	uint64_t	  percpu_base_addr;
+	uint64_t	  percpu_sec_sz;
 	struct btf	  *btf;
 	struct btf	  *base_btf;
 };
-- 
2.24.1


  reply	other threads:[~2020-12-11  4:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-11  4:11 [PATCH dwarves 0/2] Fix pahole to emit kernel module BTF variables Andrii Nakryiko
2020-12-11  4:11 ` Andrii Nakryiko [this message]
2020-12-11  4:11 ` [PATCH dwarves 2/2] btf_encoder: fix skipping per-CPU variables at offset 0 Andrii Nakryiko
2020-12-15  0:50   ` Hao Luo
2020-12-13 20:27 ` [PATCH dwarves 0/2] Fix pahole to emit kernel module BTF variables Jiri Olsa
2020-12-14 13:43   ` Arnaldo Carvalho de Melo
2020-12-15  1:28     ` Andrii Nakryiko
2020-12-15 13:28       ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201211041139.589692-2-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=acme@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=jolsa@redhat.com \
    --cc=kernel-team@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.