Skip to content

Commit 65633ed

Browse files
Yonghong Songintel-lab-lkp
authored andcommitted
bpf: Support private stack for struct_ops progs
For struct_ops progs, whether a particular prog uses private stack depends on prog->aux->priv_stack_requested setting before actual insn-level verification for that prog. One particular implementation is to piggyback on struct_ops->check_member(). The next patch has an example for this. The struct_ops->check_member() sets prog->aux->priv_stack_requested to be true which enables private stack usage. The struct_ops prog follows the same rule as kprobe/tracing progs after function bpf_enable_priv_stack(). For example, even a struct_ops prog requests private stack, it could still use normal kernel stack if the stack size is small (< 64 bytes). The prog->aux->priv_stack_requested is also used for recursion checking for struct_ops progs. Similar to tracing progs, nested same cpu same prog run will be skipped. A field (recursion_detected()) is added to bpf_prog_aux structure. If bpf_prog->aux->recursion_detected is implemented by the struct_ops subsystem and nested same cpu/prog happens, the function will be triggered to report an error, collect related info, etc. Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
1 parent a502b69 commit 65633ed

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,7 @@ struct bpf_prog_aux {
15281528
u64 prog_array_member_cnt; /* counts how many times as member of prog_array */
15291529
struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cnt */
15301530
struct bpf_arena *arena;
1531+
void (*recursion_detected)(struct bpf_prog *prog); /* callback if recursion is detected */
15311532
/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
15321533
const struct btf_type *attach_func_proto;
15331534
/* function name for valid attach_btf_id */

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
895895
case BPF_PROG_TYPE_TRACING:
896896
return prog->expected_attach_type != BPF_TRACE_ITER;
897897
case BPF_PROG_TYPE_STRUCT_OPS:
898+
return prog->aux->priv_stack_requested;
898899
case BPF_PROG_TYPE_LSM:
899900
return false;
900901
default:

kernel/bpf/trampoline.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,8 @@ static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tram
899899

900900
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
901901
bpf_prog_inc_misses_counter(prog);
902+
if (prog->aux->recursion_detected)
903+
prog->aux->recursion_detected(prog);
902904
return 0;
903905
}
904906
return bpf_prog_start_time();
@@ -975,6 +977,8 @@ u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
975977

976978
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
977979
bpf_prog_inc_misses_counter(prog);
980+
if (prog->aux->recursion_detected)
981+
prog->aux->recursion_detected(prog);
978982
return 0;
979983
}
980984
return bpf_prog_start_time();

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6279,6 +6279,7 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
62796279
}
62806280
}
62816281

6282+
env->prog->aux->priv_stack_requested = false;
62826283
if (si[0].priv_stack_mode == PRIV_STACK_ADAPTIVE)
62836284
env->prog->aux->priv_stack_requested = true;
62846285

@@ -21986,6 +21987,11 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
2198621987
}
2198721988
}
2198821989

21990+
if (prog->aux->priv_stack_requested && !bpf_jit_supports_private_stack()) {
21991+
verbose(env, "Private stack not supported by jit\n");
21992+
return -EACCES;
21993+
}
21994+
2198921995
/* btf_ctx_access() used this to provide argument type info */
2199021996
prog->aux->ctx_arg_info =
2199121997
st_ops_desc->arg_info[member_idx].info;

0 commit comments

Comments
 (0)