Skip to content

Commit 37bb4c3

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
bpf: Support private stack for struct_ops progs
For struct_ops progs, whether a particular prog will use private stack or not (prog->aux->use_priv_stack) will be set before actual insn-level verification for that prog. One particular implementation is to piggyback on struct_ops->check_member(). The next patch will have an example for this. The struct_ops->check_member() will set prog->aux->use_priv_stack to be true which enables private stack usage with ignoring BPF_PRIV_STACK_MIN_SIZE limit. If use_priv_stack is true for a particular struct_ops prog, bpf trampoline will need to do recursion checks (one level at this point) to avoid stack overwrite. A field (recursion_skipped()) is added to bpf_prog_aux structure such that if bpf_prog->aux->recursion_skipped is set by the struct_ops subsystem, the function will be called to terminate the prog run, collect related info, etc. Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
1 parent 45d389a commit 37bb4c3

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
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_skipped)(struct bpf_prog *prog); /* callback if recursion is skipped */
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
@@ -889,6 +889,7 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog)
889889
case BPF_PROG_TYPE_TRACING:
890890
return prog->expected_attach_type != BPF_TRACE_ITER;
891891
case BPF_PROG_TYPE_STRUCT_OPS:
892+
return prog->aux->use_priv_stack;
892893
case BPF_PROG_TYPE_LSM:
893894
return false;
894895
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_skipped)
903+
prog->aux->recursion_skipped(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_skipped)
981+
prog->aux->recursion_skipped(prog);
978982
return 0;
979983
}
980984
return bpf_prog_start_time();

kernel/bpf/verifier.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6023,17 +6023,31 @@ static int check_ptr_alignment(struct bpf_verifier_env *env,
60236023

60246024
static int bpf_enable_priv_stack(struct bpf_verifier_env *env)
60256025
{
6026+
bool force_priv_stack = env->prog->aux->use_priv_stack;
60266027
struct bpf_subprog_info *si;
6028+
int ret;
6029+
6030+
if (!bpf_jit_supports_private_stack()) {
6031+
if (force_priv_stack) {
6032+
verbose(env, "Private stack not supported by jit\n");
6033+
return -EACCES;
6034+
}
60276035

6028-
if (!bpf_jit_supports_private_stack())
60296036
return NO_PRIV_STACK;
6037+
}
60306038

6039+
ret = PRIV_STACK_ADAPTIVE;
60316040
switch (env->prog->type) {
60326041
case BPF_PROG_TYPE_KPROBE:
60336042
case BPF_PROG_TYPE_TRACEPOINT:
60346043
case BPF_PROG_TYPE_PERF_EVENT:
60356044
case BPF_PROG_TYPE_RAW_TRACEPOINT:
60366045
break;
6046+
case BPF_PROG_TYPE_STRUCT_OPS:
6047+
if (!force_priv_stack)
6048+
return NO_PRIV_STACK;
6049+
ret = PRIV_STACK_ALWAYS;
6050+
break;
60376051
case BPF_PROG_TYPE_TRACING:
60386052
if (env->prog->expected_attach_type != BPF_TRACE_ITER)
60396053
break;
@@ -6044,11 +6058,18 @@ static int bpf_enable_priv_stack(struct bpf_verifier_env *env)
60446058

60456059
si = env->subprog_info;
60466060
for (int i = 0; i < env->subprog_cnt; i++) {
6047-
if (si[i].has_tail_call)
6061+
if (si[i].has_tail_call) {
6062+
if (ret == PRIV_STACK_ALWAYS) {
6063+
verbose(env,
6064+
"Private stack not supported due to tail call presence\n");
6065+
return -EACCES;
6066+
}
6067+
60486068
return NO_PRIV_STACK;
6069+
}
60496070
}
60506071

6051-
return PRIV_STACK_ADAPTIVE;
6072+
return ret;
60526073
}
60536074

60546075
static int round_up_stack_depth(struct bpf_verifier_env *env, int stack_depth)
@@ -6121,7 +6142,8 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx,
61216142
idx, subprog_depth);
61226143
return -EACCES;
61236144
}
6124-
if (subprog_depth >= BPF_PRIV_STACK_MIN_SIZE) {
6145+
if (priv_stack_supported == PRIV_STACK_ALWAYS ||
6146+
subprog_depth >= BPF_PRIV_STACK_MIN_SIZE) {
61256147
subprog[idx].use_priv_stack = true;
61266148
subprog_visited[idx] = 1;
61276149
}
@@ -6271,6 +6293,12 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
62716293
depth_frame, subtree_depth);
62726294
return -EACCES;
62736295
}
6296+
if (orig_priv_stack_supported == PRIV_STACK_ALWAYS) {
6297+
verbose(env,
6298+
"Private stack not supported due to possible nested subprog run\n");
6299+
ret = -EACCES;
6300+
goto out;
6301+
}
62746302
if (orig_priv_stack_supported == PRIV_STACK_ADAPTIVE) {
62756303
for (int i = 0; i < env->subprog_cnt; i++)
62766304
si[i].use_priv_stack = false;

0 commit comments

Comments
 (0)