Skip to content

Commit 169c317

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
bpf: Add gen_epilogue to bpf_verifier_ops
This patch adds a .gen_epilogue to the bpf_verifier_ops. It is similar to the existing .gen_prologue. Instead of allowing a subsystem to run code at the beginning of a bpf prog, it allows the subsystem to run code just before the bpf prog exit. One of the use case is to allow the upcoming bpf qdisc to ensure that the skb->dev is the same as the qdisc->dev_queue->dev. The bpf qdisc struct_ops implementation could either fix it up or drop the skb. Another use case could be in bpf_tcp_ca.c to enforce snd_cwnd has sane value (e.g. non zero). The epilogue can do the useful thing (like checking skb->dev) if it can access the bpf prog's ctx. Unlike prologue, r1 may not hold the ctx pointer. This patch saves the r1 in the stack if the .gen_epilogue has returned some instructions in the "epilogue_buf". The existing .gen_prologue is done in convert_ctx_accesses(). The new .gen_epilogue is done in the convert_ctx_accesses() also. When it sees the (BPF_JMP | BPF_EXIT) instruction, it will be patched with the earlier generated "epilogue_buf". The epilogue patching is only done for the main prog. Only one epilogue will be patched to the main program. When the bpf prog has multiple BPF_EXIT instructions, a BPF_JA is used to goto the earlier patched epilogue. Majority of the archs support (BPF_JMP32 | BPF_JA): x86, arm, s390, risv64, loongarch, powerpc and arc. This patch keeps it simple and always use (BPF_JMP32 | BPF_JA). A new macro BPF_JMP32_A is added to generate the (BPF_JMP32 | BPF_JA) insn. Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d5c4771 commit 169c317

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,8 @@ struct bpf_verifier_ops {
974974
struct bpf_insn_access_aux *info);
975975
int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
976976
const struct bpf_prog *prog);
977+
int (*gen_epilogue)(struct bpf_insn *insn, const struct bpf_prog *prog,
978+
s16 ctx_stack_off);
977979
int (*gen_ld_abs)(const struct bpf_insn *orig,
978980
struct bpf_insn *insn_buf);
979981
u32 (*convert_ctx_access)(enum bpf_access_type type,

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ struct bpf_verifier_env {
783783
*/
784784
char tmp_str_buf[TMP_STR_BUF_LEN];
785785
struct bpf_insn insn_buf[INSN_BUF_SIZE];
786+
struct bpf_insn epilogue_buf[INSN_BUF_SIZE];
786787
};
787788

788789
static inline struct bpf_func_info_aux *subprog_aux(struct bpf_verifier_env *env, int subprog)

include/linux/filter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ static inline bool insn_is_cast_user(const struct bpf_insn *insn)
437437
.off = OFF, \
438438
.imm = 0 })
439439

440+
/* Unconditional jumps, gotol pc + imm32 */
441+
442+
#define BPF_JMP32_A(IMM) \
443+
((struct bpf_insn) { \
444+
.code = BPF_JMP32 | BPF_JA, \
445+
.dst_reg = 0, \
446+
.src_reg = 0, \
447+
.off = 0, \
448+
.imm = IMM })
449+
440450
/* Relative call */
441451

442452
#define BPF_CALL_REL(TGT) \

kernel/bpf/verifier.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19677,15 +19677,39 @@ static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
1967719677
*/
1967819678
static int convert_ctx_accesses(struct bpf_verifier_env *env)
1967919679
{
19680+
struct bpf_subprog_info *subprogs = env->subprog_info;
1968019681
const struct bpf_verifier_ops *ops = env->ops;
19681-
int i, cnt, size, ctx_field_size, delta = 0;
19682+
int i, cnt, size, ctx_field_size, delta = 0, epilogue_cnt = 0;
1968219683
const int insn_cnt = env->prog->len;
19684+
struct bpf_insn *epilogue_buf = env->epilogue_buf;
1968319685
struct bpf_insn *insn_buf = env->insn_buf;
1968419686
struct bpf_insn *insn;
1968519687
u32 target_size, size_default, off;
1968619688
struct bpf_prog *new_prog;
1968719689
enum bpf_access_type type;
1968819690
bool is_narrower_load;
19691+
int epilogue_idx = 0;
19692+
19693+
if (ops->gen_epilogue) {
19694+
epilogue_cnt = ops->gen_epilogue(epilogue_buf, env->prog,
19695+
-(subprogs[0].stack_depth + 8));
19696+
if (epilogue_cnt >= INSN_BUF_SIZE) {
19697+
verbose(env, "bpf verifier is misconfigured\n");
19698+
return -EINVAL;
19699+
} else if (epilogue_cnt) {
19700+
/* Save the ARG_PTR_TO_CTX for the epilogue to use */
19701+
cnt = 0;
19702+
subprogs[0].stack_depth += 8;
19703+
insn_buf[cnt++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_1,
19704+
-subprogs[0].stack_depth);
19705+
insn_buf[cnt++] = env->prog->insnsi[0];
19706+
new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
19707+
if (!new_prog)
19708+
return -ENOMEM;
19709+
env->prog = new_prog;
19710+
delta += cnt - 1;
19711+
}
19712+
}
1968919713

1969019714
if (ops->gen_prologue || env->seen_direct_write) {
1969119715
if (!ops->gen_prologue) {
@@ -19742,6 +19766,25 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
1974219766
insn->code = BPF_STX | BPF_PROBE_ATOMIC | BPF_SIZE(insn->code);
1974319767
env->prog->aux->num_exentries++;
1974419768
continue;
19769+
} else if (insn->code == (BPF_JMP | BPF_EXIT) &&
19770+
epilogue_cnt &&
19771+
i + delta < subprogs[1].start) {
19772+
/* Generate epilogue for the main prog */
19773+
if (epilogue_idx) {
19774+
/* jump back to the earlier generated epilogue */
19775+
insn_buf[0] = BPF_JMP32_A(epilogue_idx - i - delta - 1);
19776+
cnt = 1;
19777+
} else {
19778+
memcpy(insn_buf, epilogue_buf,
19779+
epilogue_cnt * sizeof(*epilogue_buf));
19780+
cnt = epilogue_cnt;
19781+
/* epilogue_idx cannot be 0. It must have at
19782+
* least one ctx ptr saving insn before the
19783+
* epilogue.
19784+
*/
19785+
epilogue_idx = i + delta;
19786+
}
19787+
goto patch_insn_buf;
1974519788
} else {
1974619789
continue;
1974719790
}
@@ -19878,6 +19921,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
1987819921
insn->dst_reg, insn->dst_reg,
1987919922
size * 8, 0);
1988019923

19924+
patch_insn_buf:
1988119925
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
1988219926
if (!new_prog)
1988319927
return -ENOMEM;

0 commit comments

Comments
 (0)