summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorYonghong Song <yonghong.song@linux.dev>2026-09-12 12:52:21 -0700
committerAlexei Starovoitov <ast@kernel.org>2026-09-12 20:54:18 -0700
commit4dd676ca5b7a3bdc18e217bdd618c5e73016aee4 (patch)
tree76e930454c71b1b2f18c89be3e60783cf288e272 /kernel
parent3f1023cab8130e8d43271b57d36139273a6c48a0 (diff)
bpf: Support by-value struct arguments up to 16 bytes
A global function taking a struct or union by value is rejected today: Arg#1 type STRUCT in tar() is not supported yet. Accept one of at most 16 bytes, which arrives in one or two consecutive argument registers. Only structs composed entirely of scalars are taken for now; btf_struct_is_composed_of() enforces that. The slots of a value are independent of each other, so the compiler may split one across the last argument register and the stack, as in static void f(int a, int b, int c, int d, struct pair p); or place it wholly past the registers, and the verifier describes either the same way. What has to follow the slots is stack_arg_cnt, recomputed from the slots consumed, together with the "no stack args in global functions" and JIT support checks, and the MAX_BPF_FUNC_ARGS bound, which now has to account for a parameter that takes two slots at once. Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20260912195221.987808-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/btf.c80
-rw-r--r--kernel/bpf/verifier.c2
2 files changed, 68 insertions, 14 deletions
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index cdbae53bb94d..c560ee0c4347 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -8018,6 +8018,29 @@ static int btf_validate_return_type(struct bpf_verifier_env *env, struct btf *bt
return -EOPNOTSUPP;
}
+static int btf_check_arg_slots(struct bpf_verifier_log *log, const char *tname,
+ bool is_global, u32 slot_cnt,
+ struct bpf_subprog_info *sub)
+{
+ if (slot_cnt <= MAX_BPF_FUNC_REG_ARGS)
+ return 0;
+
+ if (is_global) {
+ bpf_log(log,
+ "global function %s() needs %d > %d argument slots, "
+ "stack args not supported\n",
+ tname, slot_cnt, MAX_BPF_FUNC_REG_ARGS);
+ return -EINVAL;
+ }
+ if (!bpf_jit_supports_stack_args()) {
+ bpf_log(log, "JIT does not support function %s() with %d argument slots\n",
+ tname, slot_cnt);
+ return -EFAULT;
+ }
+ sub->stack_arg_cnt = slot_cnt - MAX_BPF_FUNC_REG_ARGS;
+ return 0;
+}
+
/* Process BTF of a function to produce high-level expectation of function
* arguments (like ARG_PTR_TO_CTX, or ARG_PTR_TO_MEM, etc). This information
* is cached in subprog info for reuse.
@@ -8087,20 +8110,9 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
MAX_BPF_FUNC_ARGS, tname, nargs);
return -EFAULT;
}
- if (nargs > MAX_BPF_FUNC_REG_ARGS) {
- if (!bpf_jit_supports_stack_args()) {
- bpf_log(log, "JIT does not support function %s() with %d args\n",
- tname, nargs);
- return -EFAULT;
- }
- sub->stack_arg_cnt = nargs - MAX_BPF_FUNC_REG_ARGS;
- }
-
- if (is_global && nargs > MAX_BPF_FUNC_REG_ARGS) {
- bpf_log(log, "global function %s has %d > %d args, stack args not supported\n",
- tname, nargs, MAX_BPF_FUNC_REG_ARGS);
- return -EINVAL;
- }
+ err = btf_check_arg_slots(log, tname, is_global, nargs, sub);
+ if (err)
+ return err;
err = btf_validate_return_type(env, btf, t, subprog, is_global);
if (err) {
@@ -8125,6 +8137,9 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
for (i = 0, slots_used = 0; i < nargs; i++) {
u32 tags = 0;
+ if (slots_used >= MAX_BPF_FUNC_ARGS)
+ goto too_many_slots;
+
err = btf_scan_decl_tags(env, btf, fn_t, i, is_global, &tags);
if (err)
return err;
@@ -8253,6 +8268,33 @@ skip_pointer:
sub->args[slots_used++].arg_type = ARG_SCALAR;
continue;
}
+ if (btf_type_is_struct(t)) {
+ u32 nslots;
+
+ if (!t->size || t->size > 2 * BPF_REG_SIZE) {
+ if (!is_global)
+ return -EINVAL;
+ bpf_log(log,
+ "Arg#%d type %s in %s() has size %u, only 1 to %d bytes "
+ "can be passed by value\n",
+ i, btf_type_str(t), tname, t->size, 2 * BPF_REG_SIZE);
+ return -EINVAL;
+ }
+ if (!btf_struct_is_composed_of(env, btf, t, BTF_MEMBER_SCALAR)) {
+ if (!is_global)
+ return -EINVAL;
+ bpf_log(log, "Arg#%d type %s in %s() is not composed of scalars\n",
+ i, btf_type_str(t), tname);
+ return -EINVAL;
+ }
+
+ nslots = (t->size + BPF_REG_SIZE - 1) / BPF_REG_SIZE;
+ if (slots_used + nslots > MAX_BPF_FUNC_ARGS)
+ goto too_many_slots;
+ while (nslots--)
+ sub->args[slots_used++].arg_type = ARG_SCALAR;
+ continue;
+ }
if (!is_global)
return -EINVAL;
bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
@@ -8260,11 +8302,21 @@ skip_pointer:
return -EINVAL;
}
+ err = btf_check_arg_slots(log, tname, is_global, slots_used, sub);
+ if (err)
+ return err;
sub->arg_slot_cnt = slots_used;
sub->args_cached = true;
return 0;
+
+too_many_slots:
+ if (!is_global)
+ return -EINVAL;
+ bpf_log(log, "Arguments of %s() need more than %d argument slots\n",
+ tname, MAX_BPF_FUNC_ARGS);
+ return -EINVAL;
}
static void btf_type_show(const struct btf *btf, u32 type_id, void *obj,
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index cf2696d7b778..d8963d50d85b 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -10259,6 +10259,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
func = btf_type_by_id(btf, env->prog->aux->func_info[subprog].type_id);
func_proto = btf_type_by_id(btf, func->type);
args = btf_params(func_proto);
+ if (sub->arg_slot_cnt != btf_type_vlen(func_proto))
+ args = NULL;
ret = check_outgoing_stack_args(env, caller, sub->arg_slot_cnt,
bpf_subprog_name(env, subprog), btf, args);
if (ret)