Skip to content

Commit ab71416

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). 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. Acked-by: Tejun Heo <[email protected]> Signed-off-by: Yonghong Song <[email protected]>
1 parent 8b1d9c5 commit ab71416

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

include/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,11 @@ struct bpf_prog_aux {
15251525
bool exception_boundary;
15261526
bool is_extended; /* true if extended by freplace program */
15271527
bool jits_use_priv_stack;
1528+
bool priv_stack_requested;
15281529
u64 prog_array_member_cnt; /* counts how many times as member of prog_array */
15291530
struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cnt */
15301531
struct bpf_arena *arena;
1532+
void (*recursion_detected)(struct bpf_prog *prog); /* callback if recursion is detected */
15311533
/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
15321534
const struct btf_type *attach_func_proto;
15331535
/* 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
@@ -879,6 +879,7 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
879879
case BPF_PROG_TYPE_TRACING:
880880
return prog->expected_attach_type != BPF_TRACE_ITER;
881881
case BPF_PROG_TYPE_STRUCT_OPS:
882+
return prog->aux->jits_use_priv_stack;
882883
case BPF_PROG_TYPE_LSM:
883884
return false;
884885
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6110,7 +6110,7 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog)
61106110
case BPF_PROG_TYPE_TRACING:
61116111
case BPF_PROG_TYPE_LSM:
61126112
case BPF_PROG_TYPE_STRUCT_OPS:
6113-
if (bpf_prog_check_recur(prog))
6113+
if (prog->aux->priv_stack_requested || bpf_prog_check_recur(prog))
61146114
return PRIV_STACK_ADAPTIVE;
61156115
fallthrough;
61166116
default:
@@ -22053,6 +22053,11 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
2205322053
}
2205422054
}
2205522055

22056+
if (prog->aux->priv_stack_requested && !bpf_jit_supports_private_stack()) {
22057+
verbose(env, "Private stack not supported by jit\n");
22058+
return -EACCES;
22059+
}
22060+
2205622061
/* btf_ctx_access() used this to provide argument type info */
2205722062
prog->aux->ctx_arg_info =
2205822063
st_ops_desc->arg_info[member_idx].info;

0 commit comments

Comments
 (0)