Skip to content

Commit 15a3b79

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-use-vrealloc-in-bpf_patch_insn_data'
Eduard Zingerman says: ==================== bpf: use vrealloc() in bpf_patch_insn_data() Function bpf_patch_insn_data() uses vzalloc/vfree pair to allocate memory for updated insn_aux_data. These operations are expensive for big programs where a lot of rewrites happen, e.g. for pyperf180 test case. The pair can be replaced with a call to vrealloc in order to reduce the number of actual memory allocations. Perf stat w/o this patch: $ perf stat -B --all-kernel -r10 -- ./veristat -q pyperf180.bpf.o ... 2201.25 msec task-clock # 0.973 CPUs utilized ( +- 2.20% ) 188 context-switches # 85.406 /sec ( +- 9.29% ) 15 cpu-migrations # 6.814 /sec ( +- 5.64% ) 5 page-faults # 2.271 /sec ( +- 3.27% ) 4315057974 instructions # 1.28 insn per cycle # 0.33 stalled cycles per insn ( +- 0.03% ) 3366141387 cycles # 1.529 GHz ( +- 0.21% ) 1420810964 stalled-cycles-frontend # 42.21% frontend cycles idle ( +- 0.23% ) 1049956791 branches # 476.981 M/sec ( +- 0.03% ) 60591781 branch-misses # 5.77% of all branches ( +- 0.07% ) 2.2632 +- 0.0527 seconds time elapsed ( +- 2.33% ) Perf stat with this patch: 1227.15 msec task-clock # 0.963 CPUs utilized ( +- 2.27% ) 170 context-switches # 138.532 /sec ( +- 5.62% ) 2 cpu-migrations # 1.630 /sec ( +- 33.37% ) 5 page-faults # 4.074 /sec ( +- 4.47% ) 3312254304 instructions # 2.17 insn per cycle # 0.15 stalled cycles per insn ( +- 0.03% ) 1528944717 cycles # 1.246 GHz ( +- 0.31% ) 501475146 stalled-cycles-frontend # 32.80% frontend cycles idle ( +- 0.50% ) 730426891 branches # 595.222 M/sec ( +- 0.03% ) 17372363 branch-misses # 2.38% of all branches ( +- 0.16% ) 1.2744 +- 0.0301 seconds time elapsed ( +- 2.36% ) Changelog: v1: https://lore.kernel.org/bpf/[email protected]/T/#t v1 -> v2: - added missing memset(0) in adjust_insn_aux_data(), this fixes CI failure reported in [1]. [1] https://github.com/kernel-patches/bpf/actions/runs/16787563163/job/47542309875 ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 911c035 + 77620d1 commit 15a3b79

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

kernel/bpf/verifier.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,7 @@ static int mark_irq_flag_read(struct bpf_verifier_env *env, struct bpf_reg_state
36633663
* code only. It returns TRUE if the source or destination register operates
36643664
* on 64-bit, otherwise return FALSE.
36653665
*/
3666-
static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn,
3666+
static bool is_reg64(struct bpf_insn *insn,
36673667
u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t)
36683668
{
36693669
u8 code, class, op;
@@ -3774,14 +3774,14 @@ static int insn_def_regno(const struct bpf_insn *insn)
37743774
}
37753775

37763776
/* Return TRUE if INSN has defined any 32-bit value explicitly. */
3777-
static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn)
3777+
static bool insn_has_def32(struct bpf_insn *insn)
37783778
{
37793779
int dst_reg = insn_def_regno(insn);
37803780

37813781
if (dst_reg == -1)
37823782
return false;
37833783

3784-
return !is_reg64(env, insn, dst_reg, NULL, DST_OP);
3784+
return !is_reg64(insn, dst_reg, NULL, DST_OP);
37853785
}
37863786

37873787
static void mark_insn_zext(struct bpf_verifier_env *env,
@@ -3812,7 +3812,7 @@ static int __check_reg_arg(struct bpf_verifier_env *env, struct bpf_reg_state *r
38123812
mark_reg_scratched(env, regno);
38133813

38143814
reg = &regs[regno];
3815-
rw64 = is_reg64(env, insn, regno, reg, t);
3815+
rw64 = is_reg64(insn, regno, reg, t);
38163816
if (t == SRC_OP) {
38173817
/* check whether register used as source operand can be read */
38183818
if (reg->type == NOT_INIT) {
@@ -20699,35 +20699,32 @@ static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
2069920699
* [0, off) and [off, end) to new locations, so the patched range stays zero
2070020700
*/
2070120701
static void adjust_insn_aux_data(struct bpf_verifier_env *env,
20702-
struct bpf_insn_aux_data *new_data,
2070320702
struct bpf_prog *new_prog, u32 off, u32 cnt)
2070420703
{
20705-
struct bpf_insn_aux_data *old_data = env->insn_aux_data;
20704+
struct bpf_insn_aux_data *data = env->insn_aux_data;
2070620705
struct bpf_insn *insn = new_prog->insnsi;
20707-
u32 old_seen = old_data[off].seen;
20706+
u32 old_seen = data[off].seen;
2070820707
u32 prog_len;
2070920708
int i;
2071020709

2071120710
/* aux info at OFF always needs adjustment, no matter fast path
2071220711
* (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the
2071320712
* original insn at old prog.
2071420713
*/
20715-
old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1);
20714+
data[off].zext_dst = insn_has_def32(insn + off + cnt - 1);
2071620715

2071720716
if (cnt == 1)
2071820717
return;
2071920718
prog_len = new_prog->len;
2072020719

20721-
memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
20722-
memcpy(new_data + off + cnt - 1, old_data + off,
20723-
sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
20720+
memmove(data + off + cnt - 1, data + off,
20721+
sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
20722+
memset(data + off, 0, sizeof(struct bpf_insn_aux_data) * (cnt - 1));
2072420723
for (i = off; i < off + cnt - 1; i++) {
2072520724
/* Expand insni[off]'s seen count to the patched range. */
20726-
new_data[i].seen = old_seen;
20727-
new_data[i].zext_dst = insn_has_def32(env, insn + i);
20725+
data[i].seen = old_seen;
20726+
data[i].zext_dst = insn_has_def32(insn + i);
2072820727
}
20729-
env->insn_aux_data = new_data;
20730-
vfree(old_data);
2073120728
}
2073220729

2073320730
static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
@@ -20765,10 +20762,14 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of
2076520762
struct bpf_insn_aux_data *new_data = NULL;
2076620763

2076720764
if (len > 1) {
20768-
new_data = vzalloc(array_size(env->prog->len + len - 1,
20769-
sizeof(struct bpf_insn_aux_data)));
20765+
new_data = vrealloc(env->insn_aux_data,
20766+
array_size(env->prog->len + len - 1,
20767+
sizeof(struct bpf_insn_aux_data)),
20768+
GFP_KERNEL_ACCOUNT | __GFP_ZERO);
2077020769
if (!new_data)
2077120770
return NULL;
20771+
20772+
env->insn_aux_data = new_data;
2077220773
}
2077320774

2077420775
new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
@@ -20780,7 +20781,7 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of
2078020781
vfree(new_data);
2078120782
return NULL;
2078220783
}
20783-
adjust_insn_aux_data(env, new_data, new_prog, off, len);
20784+
adjust_insn_aux_data(env, new_prog, off, len);
2078420785
adjust_subprog_starts(env, off, len);
2078520786
adjust_poke_descs(new_prog, off, len);
2078620787
return new_prog;
@@ -21131,7 +21132,7 @@ static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
2113121132
* BPF_STX + SRC_OP, so it is safe to pass NULL
2113221133
* here.
2113321134
*/
21134-
if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) {
21135+
if (is_reg64(&insn, load_reg, NULL, DST_OP)) {
2113521136
if (class == BPF_LD &&
2113621137
BPF_MODE(code) == BPF_IMM)
2113721138
i++;

0 commit comments

Comments
 (0)