Skip to content

Commit e16301f

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpf: Simplify freeing logic in linfo and jited_linfo
This patch simplifies the linfo freeing logic by combining "bpf_prog_free_jited_linfo()" and "bpf_prog_free_unused_jited_linfo()" into the new "bpf_prog_jit_attempt_done()". It is a prep work for the kernel function call support. In a later patch, freeing the kernel function call descriptors will also be done in the "bpf_prog_jit_attempt_done()". "bpf_prog_free_linfo()" is removed since it is only called by "__bpf_prog_put_noref()". The kvfree() are directly called instead. It also takes this chance to s/kcalloc/kvcalloc/ for the jited_linfo allocation. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210325015130.1544323-1-kafai@fb.com
1 parent 36e7985 commit e16301f

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

include/linux/filter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,7 @@ void bpf_prog_free_linfo(struct bpf_prog *prog);
877877
void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
878878
const u32 *insn_to_jit_off);
879879
int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog);
880-
void bpf_prog_free_jited_linfo(struct bpf_prog *prog);
881-
void bpf_prog_free_unused_jited_linfo(struct bpf_prog *prog);
880+
void bpf_prog_jit_attempt_done(struct bpf_prog *prog);
882881

883882
struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
884883
struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags);

kernel/bpf/core.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,22 @@ int bpf_prog_alloc_jited_linfo(struct bpf_prog *prog)
143143
if (!prog->aux->nr_linfo || !prog->jit_requested)
144144
return 0;
145145

146-
prog->aux->jited_linfo = kcalloc(prog->aux->nr_linfo,
147-
sizeof(*prog->aux->jited_linfo),
148-
GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
146+
prog->aux->jited_linfo = kvcalloc(prog->aux->nr_linfo,
147+
sizeof(*prog->aux->jited_linfo),
148+
GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
149149
if (!prog->aux->jited_linfo)
150150
return -ENOMEM;
151151

152152
return 0;
153153
}
154154

155-
void bpf_prog_free_jited_linfo(struct bpf_prog *prog)
155+
void bpf_prog_jit_attempt_done(struct bpf_prog *prog)
156156
{
157-
kfree(prog->aux->jited_linfo);
158-
prog->aux->jited_linfo = NULL;
159-
}
160-
161-
void bpf_prog_free_unused_jited_linfo(struct bpf_prog *prog)
162-
{
163-
if (prog->aux->jited_linfo && !prog->aux->jited_linfo[0])
164-
bpf_prog_free_jited_linfo(prog);
157+
if (prog->aux->jited_linfo &&
158+
(!prog->jited || !prog->aux->jited_linfo[0])) {
159+
kvfree(prog->aux->jited_linfo);
160+
prog->aux->jited_linfo = NULL;
161+
}
165162
}
166163

167164
/* The jit engine is responsible to provide an array
@@ -217,12 +214,6 @@ void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
217214
insn_to_jit_off[linfo[i].insn_off - insn_start - 1];
218215
}
219216

220-
void bpf_prog_free_linfo(struct bpf_prog *prog)
221-
{
222-
bpf_prog_free_jited_linfo(prog);
223-
kvfree(prog->aux->linfo);
224-
}
225-
226217
struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
227218
gfp_t gfp_extra_flags)
228219
{
@@ -1866,15 +1857,13 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
18661857
return fp;
18671858

18681859
fp = bpf_int_jit_compile(fp);
1869-
if (!fp->jited) {
1870-
bpf_prog_free_jited_linfo(fp);
1860+
bpf_prog_jit_attempt_done(fp);
18711861
#ifdef CONFIG_BPF_JIT_ALWAYS_ON
1862+
if (!fp->jited) {
18721863
*err = -ENOTSUPP;
18731864
return fp;
1874-
#endif
1875-
} else {
1876-
bpf_prog_free_unused_jited_linfo(fp);
18771865
}
1866+
#endif
18781867
} else {
18791868
*err = bpf_prog_offload_compile(fp);
18801869
if (*err)

kernel/bpf/syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,8 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
16941694
{
16951695
bpf_prog_kallsyms_del_all(prog);
16961696
btf_put(prog->aux->btf);
1697-
bpf_prog_free_linfo(prog);
1697+
kvfree(prog->aux->jited_linfo);
1698+
kvfree(prog->aux->linfo);
16981699
if (prog->aux->attach_btf)
16991700
btf_put(prog->aux->attach_btf);
17001701

kernel/bpf/verifier.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,7 +11741,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1174111741
prog->bpf_func = func[0]->bpf_func;
1174211742
prog->aux->func = func;
1174311743
prog->aux->func_cnt = env->subprog_cnt;
11744-
bpf_prog_free_unused_jited_linfo(prog);
11744+
bpf_prog_jit_attempt_done(prog);
1174511745
return 0;
1174611746
out_free:
1174711747
for (i = 0; i < env->subprog_cnt; i++) {
@@ -11764,7 +11764,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1176411764
insn->off = 0;
1176511765
insn->imm = env->insn_aux_data[i].call_imm;
1176611766
}
11767-
bpf_prog_free_jited_linfo(prog);
11767+
bpf_prog_jit_attempt_done(prog);
1176811768
return err;
1176911769
}
1177011770

0 commit comments

Comments
 (0)