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 */
698690int 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}
709704EXPORT_SYMBOL_GPL (bpf_prog_select_runtime );
710705
@@ -736,6 +731,21 @@ const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
736731const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak ;
737732const 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 */
0 commit comments