Commit b0970f23 authored by Daniel Borkmann's avatar Daniel Borkmann

Merge branch 'bpf-accurate-prog-addr'

Song Liu says:

====================
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.
====================
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parents 7de414a9 ff1889fc
...@@ -553,7 +553,6 @@ bool is_bpf_text_address(unsigned long addr) ...@@ -553,7 +553,6 @@ bool is_bpf_text_address(unsigned long addr)
int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
char *sym) char *sym)
{ {
unsigned long symbol_start, symbol_end;
struct bpf_prog_aux *aux; struct bpf_prog_aux *aux;
unsigned int it = 0; unsigned int it = 0;
int ret = -ERANGE; int ret = -ERANGE;
...@@ -566,10 +565,9 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, ...@@ -566,10 +565,9 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
if (it++ != symnum) if (it++ != symnum)
continue; continue;
bpf_get_prog_addr_region(aux->prog, &symbol_start, &symbol_end);
bpf_get_prog_name(aux->prog, sym); bpf_get_prog_name(aux->prog, sym);
*value = symbol_start; *value = (unsigned long)aux->prog->bpf_func;
*type = BPF_SYM_ELF_TYPE; *type = BPF_SYM_ELF_TYPE;
ret = 0; ret = 0;
......
...@@ -2158,11 +2158,11 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, ...@@ -2158,11 +2158,11 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
} }
ulen = info.nr_jited_ksyms; 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 (info.nr_jited_ksyms && ulen) {
if (bpf_dump_raw_ok()) { if (bpf_dump_raw_ok()) {
unsigned long ksym_addr;
u64 __user *user_ksyms; u64 __user *user_ksyms;
ulong ksym_addr;
u32 i; u32 i;
/* copy the address of the kernel symbol /* copy the address of the kernel symbol
...@@ -2170,10 +2170,17 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, ...@@ -2170,10 +2170,17 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
*/ */
ulen = min_t(u32, info.nr_jited_ksyms, ulen); ulen = min_t(u32, info.nr_jited_ksyms, ulen);
user_ksyms = u64_to_user_ptr(info.jited_ksyms); user_ksyms = u64_to_user_ptr(info.jited_ksyms);
for (i = 0; i < ulen; i++) { if (prog->aux->func_cnt) {
ksym_addr = (ulong) prog->aux->func[i]->bpf_func; for (i = 0; i < ulen; i++) {
ksym_addr &= PAGE_MASK; ksym_addr = (unsigned long)
if (put_user((u64) ksym_addr, &user_ksyms[i])) 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; return -EFAULT;
} }
} else { } else {
...@@ -2182,7 +2189,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, ...@@ -2182,7 +2189,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
} }
ulen = info.nr_jited_func_lens; 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 (info.nr_jited_func_lens && ulen) {
if (bpf_dump_raw_ok()) { if (bpf_dump_raw_ok()) {
u32 __user *user_lens; u32 __user *user_lens;
...@@ -2191,9 +2198,16 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, ...@@ -2191,9 +2198,16 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
/* copy the JITed image lengths for each function */ /* copy the JITed image lengths for each function */
ulen = min_t(u32, info.nr_jited_func_lens, ulen); ulen = min_t(u32, info.nr_jited_func_lens, ulen);
user_lens = u64_to_user_ptr(info.jited_func_lens); user_lens = u64_to_user_ptr(info.jited_func_lens);
for (i = 0; i < ulen; i++) { if (prog->aux->func_cnt) {
func_len = prog->aux->func[i]->jited_len; for (i = 0; i < ulen; i++) {
if (put_user(func_len, &user_lens[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; return -EFAULT;
} }
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment