History

bpf: Split subprogram and kfunc collection

add_subprog_and_kfunc() combines two operations with different ordering requirements. Subprogram discovery must precede validation of func_info and line_info, while kfunc descriptors are only needed by the verifier after its initial program setup is complete. Split the helper into add_subprogs() and add_kfuncs() so each operation can be placed according to its actual dependencies. Keep both calls adjacent and in their existing phase for now, and add short comments describing their roles. No functional change is intended for valid programs.

Kumar Kartikeya Dwivedi committed 3 months ago

Commit 41f36ff1 parent 04962af1 file · +32 −9Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/verifier.c+32−9
@@ -2837,7 +2837,7 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · 2836 unmodified lines
2837 return 0;2837 return 0;
2838}2838}
2839 2839
2840static int add_subprog_and_kfunc(struct bpf_verifier_env *env)2840static int add_subprogs(struct bpf_verifier_env *env)
2841{2841{
2842 struct bpf_subprog_info *subprog = env->subprog_info;2842 struct bpf_subprog_info *subprog = env->subprog_info;
2843 int i, ret, insn_cnt = env->prog->len, ex_cb_insn;2843 int i, ret, insn_cnt = env->prog->len, ex_cb_insn;
@@ -2849,8 +2849,7 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env) · 5 unmodified lines
2849 return ret;2849 return ret;
2850 2850
2851 for (i = 0; i < insn_cnt; i++, insn++) {2851 for (i = 0; i < insn_cnt; i++, insn++) {
2852 if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) &&2852 if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn))
2853 !bpf_pseudo_kfunc_call(insn))
2854 continue;2853 continue;
2855 2854
2856 if (!env->bpf_capable) {2855 if (!env->bpf_capable) {
@@ -2858,11 +2857,7 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env) · 1 unmodified line
2858 return -EPERM;2857 return -EPERM;
2859 }2858 }
2860 2859
2861 if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn))2860 ret = add_subprog(env, i + insn->imm + 1);
2862 ret = add_subprog(env, i + insn->imm + 1);
2863 else
2864 ret = bpf_add_kfunc_call(env, insn->imm, insn->off);
2865
2866 if (ret < 0)2861 if (ret < 0)
2867 return ret;2862 return ret;
2868 }2863 }
@@ -2900,6 +2895,28 @@ static int add_subprog_and_kfunc(struct bpf_verifier_env *env) · 31 unmodified lines
2900 return 0;2895 return 0;
2901}2896}
2902 2897
2898static int add_kfuncs(struct bpf_verifier_env *env)
2899{
2900 struct bpf_insn *insn = env->prog->insnsi;
2901 int i, ret, insn_cnt = env->prog->len;
2902
2903 for (i = 0; i < insn_cnt; i++, insn++) {
2904 if (!bpf_pseudo_kfunc_call(insn))
2905 continue;
2906
2907 if (!env->bpf_capable) {
2908 verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n");
2909 return -EPERM;
2910 }
2911
2912 ret = bpf_add_kfunc_call(env, insn->imm, insn->off);
2913 if (ret < 0)
2914 return ret;
2915 }
2916
2917 return 0;
2918}
2919
2903static int check_subprogs(struct bpf_verifier_env *env)2920static int check_subprogs(struct bpf_verifier_env *env)
2904{2921{
2905 int i, subprog_start, subprog_end, off, cur_subprog = 0;2922 int i, subprog_start, subprog_end, off, cur_subprog = 0;
@@ -20140,7 +20157,13 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, · 17234 unmodified lines
20140 if (ret < 0)20157 if (ret < 0)
20141 goto skip_full_check;20158 goto skip_full_check;
20142 20159
20143 ret = add_subprog_and_kfunc(env);20160 /* Discover all subprograms before validating their layout and BTF. */
20161 ret = add_subprogs(env);
20162 if (ret < 0)
20163 goto skip_full_check;
20164
20165 /* Collect the kfunc descriptors used during verification. */
20166 ret = add_kfuncs(env);
20144 if (ret < 0)20167 if (ret < 0)
20145 goto skip_full_check;20168 goto skip_full_check;
20146 20169