netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH  v2 bpf 0/3] show more accurrate bpf program address
@ 2018-11-02 17:16 Song Liu
  2018-11-02 17:16 ` [PATCH v2 bpf 1/3] bpf: show real jited prog address in /proc/kallsyms Song Liu
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Song Liu @ 2018-11-02 17:16 UTC (permalink / raw)
  To: netdev; +Cc: kernel-team, Song Liu, ast, daniel, sandipan

Changes v1 -> v2:
1. Added main program length to bpf_prog_info->jited_fun_lens (3/3).
2. Updated commit message of 1/3 and 2/3 with more background about the
   address masking, and why it is still save after the changes.
3. Replace "ulong" with "unsigned long".

This set improves bpf program address showed in /proc/kallsyms and in
bpf_prog_info. First, real program address is showed instead of page
address. Second, when there is no subprogram, bpf_prog_info->jited_ksyms
and bpf_prog_info->jited_fun_lens returns the main prog address and
length.

Song Liu (3):
  bpf: show real jited prog address in /proc/kallsyms
  bpf: show real jited address in bpf_prog_info->jited_ksyms
  bpf: show main program address and length in bpf_prog_info

 kernel/bpf/core.c    |  4 +---
 kernel/bpf/syscall.c | 34 ++++++++++++++++++++++++----------
 2 files changed, 25 insertions(+), 13 deletions(-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH  v2 bpf 1/3] bpf: show real jited prog address in /proc/kallsyms
  2018-11-02 17:16 [PATCH v2 bpf 0/3] show more accurrate bpf program address Song Liu
@ 2018-11-02 17:16 ` Song Liu
  2018-11-02 17:16 ` [PATCH v2 bpf 2/3] bpf: show real jited address in bpf_prog_info->jited_ksyms Song Liu
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2018-11-02 17:16 UTC (permalink / raw)
  To: netdev; +Cc: kernel-team, Song Liu, ast, daniel, sandipan

Currently, /proc/kallsyms shows page address of jited bpf program. The
main reason here is to not expose randomized start address. However,
This is not ideal for detailed profiling (find hot instructions from
stack traces). This patch replaces the page address with real prog start
address.

This change is OK because these addresses are still protected by sysctl
kptr_restrict (see kallsyms_show_value()), and only programs loaded by
root are added to kallsyms (see bpf_prog_kallsyms_add()).

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 kernel/bpf/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 6377225b2082..1a796e0799ec 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -553,7 +553,6 @@ bool is_bpf_text_address(unsigned long addr)
 int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
 		    char *sym)
 {
-	unsigned long symbol_start, symbol_end;
 	struct bpf_prog_aux *aux;
 	unsigned int it = 0;
 	int ret = -ERANGE;
@@ -566,10 +565,9 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
 		if (it++ != symnum)
 			continue;
 
-		bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
 		bpf_get_prog_name(aux->prog, sym);
 
-		*value = symbol_start;
+		*value = (unsigned long)aux->prog->bpf_func;
 		*type  = BPF_SYM_ELF_TYPE;
 
 		ret = 0;
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH  v2 bpf 2/3] bpf: show real jited address in bpf_prog_info->jited_ksyms
  2018-11-02 17:16 [PATCH v2 bpf 0/3] show more accurrate bpf program address Song Liu
  2018-11-02 17:16 ` [PATCH v2 bpf 1/3] bpf: show real jited prog address in /proc/kallsyms Song Liu
@ 2018-11-02 17:16 ` Song Liu
  2018-11-02 17:16 ` [PATCH v2 bpf 3/3] bpf: show main program address and length in bpf_prog_info Song Liu
  2018-11-02 20:45 ` [PATCH v2 bpf 0/3] show more accurrate bpf program address Daniel Borkmann
  3 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2018-11-02 17:16 UTC (permalink / raw)
  To: netdev; +Cc: kernel-team, Song Liu, ast, daniel, sandipan

Currently, jited_ksyms in bpf_prog_info shows page addresses of jited
bpf program. The main reason here is to not expose randomized start
address. However, this is not ideal for detailed profiling (find hot
instructions from stack traces). This patch replaces the page address
with real prog start address.

This change is OK because bpf_prog_get_info_by_fd() is only available
to root.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 kernel/bpf/syscall.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ccb93277aae2..34a9eef5992c 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2172,7 +2172,6 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 			user_ksyms = u64_to_user_ptr(info.jited_ksyms);
 			for (i = 0; i < ulen; i++) {
 				ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
-				ksym_addr &= PAGE_MASK;
 				if (put_user((u64) ksym_addr, &user_ksyms[i]))
 					return -EFAULT;
 			}
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH  v2 bpf 3/3] bpf: show main program address and length in bpf_prog_info
  2018-11-02 17:16 [PATCH v2 bpf 0/3] show more accurrate bpf program address Song Liu
  2018-11-02 17:16 ` [PATCH v2 bpf 1/3] bpf: show real jited prog address in /proc/kallsyms Song Liu
  2018-11-02 17:16 ` [PATCH v2 bpf 2/3] bpf: show real jited address in bpf_prog_info->jited_ksyms Song Liu
@ 2018-11-02 17:16 ` Song Liu
  2018-11-02 20:45 ` [PATCH v2 bpf 0/3] show more accurrate bpf program address Daniel Borkmann
  3 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2018-11-02 17:16 UTC (permalink / raw)
  To: netdev; +Cc: kernel-team, Song Liu, ast, daniel, sandipan

Currently, when there is no subprog (prog->aux->func_cnt == 0),
bpf_prog_info does not return any jited_ksyms or jited_func_lens. This
patch adds main program address (prog->bpf_func) and main program
length (prog->jited_len) to bpf_prog_info.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 kernel/bpf/syscall.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 34a9eef5992c..9418174c276c 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2158,11 +2158,11 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 	}
 
 	ulen = info.nr_jited_ksyms;
-	info.nr_jited_ksyms = prog->aux->func_cnt;
+	info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
 	if (info.nr_jited_ksyms && ulen) {
 		if (bpf_dump_raw_ok()) {
+			unsigned long ksym_addr;
 			u64 __user *user_ksyms;
-			ulong ksym_addr;
 			u32 i;
 
 			/* copy the address of the kernel symbol
@@ -2170,9 +2170,17 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 			 */
 			ulen = min_t(u32, info.nr_jited_ksyms, ulen);
 			user_ksyms = u64_to_user_ptr(info.jited_ksyms);
-			for (i = 0; i < ulen; i++) {
-				ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
-				if (put_user((u64) ksym_addr, &user_ksyms[i]))
+			if (prog->aux->func_cnt) {
+				for (i = 0; i < ulen; i++) {
+					ksym_addr = (unsigned long)
+						prog->aux->func[i]->bpf_func;
+					if (put_user((u64) ksym_addr,
+						     &user_ksyms[i]))
+						return -EFAULT;
+				}
+			} else {
+				ksym_addr = (unsigned long) prog->bpf_func;
+				if (put_user((u64) ksym_addr, &user_ksyms[0]))
 					return -EFAULT;
 			}
 		} else {
@@ -2181,7 +2189,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 	}
 
 	ulen = info.nr_jited_func_lens;
-	info.nr_jited_func_lens = prog->aux->func_cnt;
+	info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
 	if (info.nr_jited_func_lens && ulen) {
 		if (bpf_dump_raw_ok()) {
 			u32 __user *user_lens;
@@ -2190,9 +2198,16 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 			/* copy the JITed image lengths for each function */
 			ulen = min_t(u32, info.nr_jited_func_lens, ulen);
 			user_lens = u64_to_user_ptr(info.jited_func_lens);
-			for (i = 0; i < ulen; i++) {
-				func_len = prog->aux->func[i]->jited_len;
-				if (put_user(func_len, &user_lens[i]))
+			if (prog->aux->func_cnt) {
+				for (i = 0; i < ulen; i++) {
+					func_len =
+						prog->aux->func[i]->jited_len;
+					if (put_user(func_len, &user_lens[i]))
+						return -EFAULT;
+				}
+			} else {
+				func_len = prog->jited_len;
+				if (put_user(func_len, &user_lens[0]))
 					return -EFAULT;
 			}
 		} else {
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 bpf 0/3] show more accurrate bpf program address
  2018-11-02 17:16 [PATCH v2 bpf 0/3] show more accurrate bpf program address Song Liu
                   ` (2 preceding siblings ...)
  2018-11-02 17:16 ` [PATCH v2 bpf 3/3] bpf: show main program address and length in bpf_prog_info Song Liu
@ 2018-11-02 20:45 ` Daniel Borkmann
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Borkmann @ 2018-11-02 20:45 UTC (permalink / raw)
  To: Song Liu, netdev; +Cc: kernel-team, ast, sandipan

On 11/02/2018 06:16 PM, Song Liu wrote:
> Changes v1 -> v2:
> 1. Added main program length to bpf_prog_info->jited_fun_lens (3/3).
> 2. Updated commit message of 1/3 and 2/3 with more background about the
>    address masking, and why it is still save after the changes.
> 3. Replace "ulong" with "unsigned long".
> 
> This set improves bpf program address showed in /proc/kallsyms and in
> bpf_prog_info. First, real program address is showed instead of page
> address. Second, when there is no subprogram, bpf_prog_info->jited_ksyms
> and bpf_prog_info->jited_fun_lens returns the main prog address and
> length.
> 
> Song Liu (3):
>   bpf: show real jited prog address in /proc/kallsyms
>   bpf: show real jited address in bpf_prog_info->jited_ksyms
>   bpf: show main program address and length in bpf_prog_info
> 
>  kernel/bpf/core.c    |  4 +---
>  kernel/bpf/syscall.c | 34 ++++++++++++++++++++++++----------
>  2 files changed, 25 insertions(+), 13 deletions(-)
> 
> --
> 2.17.1
> 

Applied to bpf, thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-11-03  5:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-02 17:16 [PATCH v2 bpf 0/3] show more accurrate bpf program address Song Liu
2018-11-02 17:16 ` [PATCH v2 bpf 1/3] bpf: show real jited prog address in /proc/kallsyms Song Liu
2018-11-02 17:16 ` [PATCH v2 bpf 2/3] bpf: show real jited address in bpf_prog_info->jited_ksyms Song Liu
2018-11-02 17:16 ` [PATCH v2 bpf 3/3] bpf: show main program address and length in bpf_prog_info Song Liu
2018-11-02 20:45 ` [PATCH v2 bpf 0/3] show more accurrate bpf program address Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).