Skip to content

Commit 3324b58

Browse files
borkmanndavem330
authored andcommitted
ebpf: misc core cleanup
Besides others, move bpf_tail_call_proto to the remaining definitions of other protos, improve comments a bit (i.e. remove some obvious ones, where the code is already self-documenting, add objectives for others), simplify bpf_prog_array_compatible() a bit. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 17ca8cb commit 3324b58

2 files changed

Lines changed: 58 additions & 48 deletions

File tree

‎kernel/bpf/core.c‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
#include <linux/vmalloc.h>
2727
#include <linux/random.h>
2828
#include <linux/moduleloader.h>
29-
#include <asm/unaligned.h>
3029
#include <linux/bpf.h>
3130

31+
#include <asm/unaligned.h>
32+
3233
/* Registers */
3334
#define BPF_R0 regs[BPF_REG_0]
3435
#define BPF_R1 regs[BPF_REG_1]
@@ -62,6 +63,7 @@ void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, uns
6263
ptr = skb_network_header(skb) + k - SKF_NET_OFF;
6364
else if (k >= SKF_LL_OFF)
6465
ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
66+
6567
if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
6668
return ptr;
6769

@@ -176,15 +178,6 @@ noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
176178
return 0;
177179
}
178180

179-
const struct bpf_func_proto bpf_tail_call_proto = {
180-
.func = NULL,
181-
.gpl_only = false,
182-
.ret_type = RET_VOID,
183-
.arg1_type = ARG_PTR_TO_CTX,
184-
.arg2_type = ARG_CONST_MAP_PTR,
185-
.arg3_type = ARG_ANYTHING,
186-
};
187-
188181
/**
189182
* __bpf_prog_run - run eBPF program on a given context
190183
* @ctx: is the data we are operating on
@@ -650,36 +643,35 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
650643
return 0;
651644
}
652645

653-
void __weak bpf_int_jit_compile(struct bpf_prog *prog)
654-
{
655-
}
656-
657-
bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp)
646+
bool bpf_prog_array_compatible(struct bpf_array *array,
647+
const struct bpf_prog *fp)
658648
{
659-
if (array->owner_prog_type) {
660-
if (array->owner_prog_type != fp->type)
661-
return false;
662-
if (array->owner_jited != fp->jited)
663-
return false;
664-
} else {
649+
if (!array->owner_prog_type) {
650+
/* There's no owner yet where we could check for
651+
* compatibility.
652+
*/
665653
array->owner_prog_type = fp->type;
666654
array->owner_jited = fp->jited;
655+
656+
return true;
667657
}
668-
return true;
658+
659+
return array->owner_prog_type == fp->type &&
660+
array->owner_jited == fp->jited;
669661
}
670662

671-
static int check_tail_call(const struct bpf_prog *fp)
663+
static int bpf_check_tail_call(const struct bpf_prog *fp)
672664
{
673665
struct bpf_prog_aux *aux = fp->aux;
674666
int i;
675667

676668
for (i = 0; i < aux->used_map_cnt; i++) {
669+
struct bpf_map *map = aux->used_maps[i];
677670
struct bpf_array *array;
678-
struct bpf_map *map;
679671

680-
map = aux->used_maps[i];
681672
if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
682673
continue;
674+
683675
array = container_of(map, struct bpf_array, map);
684676
if (!bpf_prog_array_compatible(array, fp))
685677
return -EINVAL;
@@ -689,22 +681,25 @@ static int check_tail_call(const struct bpf_prog *fp)
689681
}
690682

691683
/**
692-
* bpf_prog_select_runtime - select execution runtime for BPF program
684+
* bpf_prog_select_runtime - select exec runtime for BPF program
693685
* @fp: bpf_prog populated with internal BPF program
694686
*
695-
* try to JIT internal BPF program, if JIT is not available select interpreter
696-
* BPF program will be executed via BPF_PROG_RUN() macro
687+
* Try to JIT eBPF program, if JIT is not available, use interpreter.
688+
* The BPF program will be executed via BPF_PROG_RUN() macro.
697689
*/
698690
int bpf_prog_select_runtime(struct bpf_prog *fp)
699691
{
700692
fp->bpf_func = (void *) __bpf_prog_run;
701693

702-
/* Probe if internal BPF can be JITed */
703694
bpf_int_jit_compile(fp);
704-
/* Lock whole bpf_prog as read-only */
705695
bpf_prog_lock_ro(fp);
706696

707-
return check_tail_call(fp);
697+
/* The tail call compatibility check can only be done at
698+
* this late stage as we need to determine, if we deal
699+
* with JITed or non JITed program concatenations and not
700+
* all eBPF JITs might immediately support all features.
701+
*/
702+
return bpf_check_tail_call(fp);
708703
}
709704
EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
710705

@@ -736,6 +731,21 @@ const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
736731
const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
737732
const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
738733

734+
/* Always built-in helper functions. */
735+
const struct bpf_func_proto bpf_tail_call_proto = {
736+
.func = NULL,
737+
.gpl_only = false,
738+
.ret_type = RET_VOID,
739+
.arg1_type = ARG_PTR_TO_CTX,
740+
.arg2_type = ARG_CONST_MAP_PTR,
741+
.arg3_type = ARG_ANYTHING,
742+
};
743+
744+
/* For classic BPF JITs that don't implement bpf_int_jit_compile(). */
745+
void __weak bpf_int_jit_compile(struct bpf_prog *prog)
746+
{
747+
}
748+
739749
/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
740750
* skb_copy_bits(), so provide a weak definition of it for NET-less config.
741751
*/

‎kernel/bpf/helpers.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
4545
}
4646

4747
const struct bpf_func_proto bpf_map_lookup_elem_proto = {
48-
.func = bpf_map_lookup_elem,
49-
.gpl_only = false,
50-
.ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
51-
.arg1_type = ARG_CONST_MAP_PTR,
52-
.arg2_type = ARG_PTR_TO_MAP_KEY,
48+
.func = bpf_map_lookup_elem,
49+
.gpl_only = false,
50+
.ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
51+
.arg1_type = ARG_CONST_MAP_PTR,
52+
.arg2_type = ARG_PTR_TO_MAP_KEY,
5353
};
5454

5555
static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
@@ -64,13 +64,13 @@ static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
6464
}
6565

6666
const struct bpf_func_proto bpf_map_update_elem_proto = {
67-
.func = bpf_map_update_elem,
68-
.gpl_only = false,
69-
.ret_type = RET_INTEGER,
70-
.arg1_type = ARG_CONST_MAP_PTR,
71-
.arg2_type = ARG_PTR_TO_MAP_KEY,
72-
.arg3_type = ARG_PTR_TO_MAP_VALUE,
73-
.arg4_type = ARG_ANYTHING,
67+
.func = bpf_map_update_elem,
68+
.gpl_only = false,
69+
.ret_type = RET_INTEGER,
70+
.arg1_type = ARG_CONST_MAP_PTR,
71+
.arg2_type = ARG_PTR_TO_MAP_KEY,
72+
.arg3_type = ARG_PTR_TO_MAP_VALUE,
73+
.arg4_type = ARG_ANYTHING,
7474
};
7575

7676
static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
@@ -84,11 +84,11 @@ static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
8484
}
8585

8686
const struct bpf_func_proto bpf_map_delete_elem_proto = {
87-
.func = bpf_map_delete_elem,
88-
.gpl_only = false,
89-
.ret_type = RET_INTEGER,
90-
.arg1_type = ARG_CONST_MAP_PTR,
91-
.arg2_type = ARG_PTR_TO_MAP_KEY,
87+
.func = bpf_map_delete_elem,
88+
.gpl_only = false,
89+
.ret_type = RET_INTEGER,
90+
.arg1_type = ARG_CONST_MAP_PTR,
91+
.arg2_type = ARG_PTR_TO_MAP_KEY,
9292
};
9393

9494
static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)

0 commit comments

Comments
 (0)