Skip to content

Commit 7961780

Browse files
Daniel Borkmanndavem330
authored andcommitted
filter: bpf_jit_comp: refactor and unify BPF JIT image dump output
If bpf_jit_enable > 1, then we dump the emitted JIT compiled image after creation. Currently, only SPARC and PowerPC has similar output as in the reference implementation on x86_64. Make a small helper function in order to reduce duplicated code and make the dump output uniform across architectures x86_64, SPARC, PPC, ARM (e.g. on ARM flen, pass and proglen are currently not shown, but would be interesting to know as well), also for future BPF JIT implementations on other archs. Cc: Mircea Gherzan <mgherzan@gmail.com> Cc: Matt Evans <matt@ozlabs.org> Cc: Eric Dumazet <eric.dumazet@google.com> Cc: David S. Miller <davem@davemloft.net> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d5e07e6 commit 7961780

5 files changed

Lines changed: 19 additions & 23 deletions

File tree

‎arch/arm/net/bpf_jit_32.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,8 @@ void bpf_jit_compile(struct sk_filter *fp)
918918
#endif
919919

920920
if (bpf_jit_enable > 1)
921-
print_hex_dump(KERN_INFO, "BPF JIT code: ",
922-
DUMP_PREFIX_ADDRESS, 16, 4, ctx.target,
923-
alloc_size, false);
921+
/* there are 2 passes here */
922+
bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
924923

925924
fp->bpf_func = (void *)ctx.target;
926925
out:

‎arch/powerpc/net/bpf_jit_comp.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,16 +671,12 @@ void bpf_jit_compile(struct sk_filter *fp)
671671
}
672672

673673
if (bpf_jit_enable > 1)
674-
pr_info("flen=%d proglen=%u pass=%d image=%p\n",
675-
flen, proglen, pass, image);
674+
/* Note that we output the base address of the code_base
675+
* rather than image, since opcodes are in code_base.
676+
*/
677+
bpf_jit_dump(flen, proglen, pass, code_base);
676678

677679
if (image) {
678-
if (bpf_jit_enable > 1)
679-
print_hex_dump(KERN_ERR, "JIT code: ",
680-
DUMP_PREFIX_ADDRESS,
681-
16, 1, code_base,
682-
proglen, false);
683-
684680
bpf_flush_icache(code_base, code_base + (proglen/4));
685681
/* Function descriptor nastiness: Address + TOC */
686682
((u64 *)image)[0] = (u64)code_base;

‎arch/sparc/net/bpf_jit_comp.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,9 @@ cond_branch: f_offset = addrs[i + filter[i].jf];
795795
}
796796

797797
if (bpf_jit_enable > 1)
798-
pr_err("flen=%d proglen=%u pass=%d image=%p\n",
799-
flen, proglen, pass, image);
798+
bpf_jit_dump(flen, proglen, pass, image);
800799

801800
if (image) {
802-
if (bpf_jit_enable > 1)
803-
print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
804-
16, 1, image, proglen, false);
805801
bpf_flush_icache(image, image + proglen);
806802
fp->bpf_func = (void *)image;
807803
}

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -725,17 +725,12 @@ cond_branch: f_offset = addrs[i + filter[i].jf] - addrs[i];
725725
}
726726
oldproglen = proglen;
727727
}
728+
728729
if (bpf_jit_enable > 1)
729-
pr_err("flen=%d proglen=%u pass=%d image=%p\n",
730-
flen, proglen, pass, image);
730+
bpf_jit_dump(flen, proglen, pass, image);
731731

732732
if (image) {
733-
if (bpf_jit_enable > 1)
734-
print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
735-
16, 1, image, proglen, false);
736-
737733
bpf_flush_icache(image, image + proglen);
738-
739734
fp->bpf_func = (void *)image;
740735
}
741736
out:

‎include/linux/filter.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ extern int sk_get_filter(struct sock *sk, struct sock_filter __user *filter, uns
5050
#ifdef CONFIG_BPF_JIT
5151
extern void bpf_jit_compile(struct sk_filter *fp);
5252
extern void bpf_jit_free(struct sk_filter *fp);
53+
54+
static inline void bpf_jit_dump(unsigned int flen, unsigned int proglen,
55+
u32 pass, void *image)
56+
{
57+
pr_err("flen=%u proglen=%u pass=%u image=%p\n",
58+
flen, proglen, pass, image);
59+
if (image)
60+
print_hex_dump(KERN_ERR, "JIT code: ", DUMP_PREFIX_ADDRESS,
61+
16, 1, image, proglen, false);
62+
}
5363
#define SK_RUN_FILTER(FILTER, SKB) (*FILTER->bpf_func)(SKB, FILTER->insns)
5464
#else
5565
static inline void bpf_jit_compile(struct sk_filter *fp)

0 commit comments

Comments
 (0)