Skip to content

Commit ffc41ce

Browse files
author
Alexei Starovoitov
committed
Merge branch 'support-passing-bpf-iterator-to-kfuncs'
Andrii Nakryiko says: ==================== Support passing BPF iterator to kfuncs Add support for passing BPF iterator state to any kfunc. Such kfunc has to declare such argument with valid `struct bpf_iter_<type> *` type and should use "__iter" suffix in argument name, following the established suffix-based convention. We add a simple test/demo iterator getter in bpf_testmod. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 01ac89d + b0cd726 commit ffc41ce

File tree

5 files changed

+127
-29
lines changed

5 files changed

+127
-29
lines changed

include/linux/btf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
580580
int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
581581
bool btf_types_are_same(const struct btf *btf1, u32 id1,
582582
const struct btf *btf2, u32 id2);
583+
int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx);
583584
#else
584585
static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
585586
u32 type_id)
@@ -654,6 +655,10 @@ static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
654655
{
655656
return false;
656657
}
658+
static inline int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx)
659+
{
660+
return -EOPNOTSUPP;
661+
}
657662
#endif
658663

659664
static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)

kernel/bpf/btf.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8047,15 +8047,44 @@ BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE)
80478047
BTF_TRACING_TYPE_xxx
80488048
#undef BTF_TRACING_TYPE
80498049

8050+
/* Validate well-formedness of iter argument type.
8051+
* On success, return positive BTF ID of iter state's STRUCT type.
8052+
* On error, negative error is returned.
8053+
*/
8054+
int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx)
8055+
{
8056+
const struct btf_param *arg;
8057+
const struct btf_type *t;
8058+
const char *name;
8059+
int btf_id;
8060+
8061+
if (btf_type_vlen(func) <= arg_idx)
8062+
return -EINVAL;
8063+
8064+
arg = &btf_params(func)[arg_idx];
8065+
t = btf_type_skip_modifiers(btf, arg->type, NULL);
8066+
if (!t || !btf_type_is_ptr(t))
8067+
return -EINVAL;
8068+
t = btf_type_skip_modifiers(btf, t->type, &btf_id);
8069+
if (!t || !__btf_type_is_struct(t))
8070+
return -EINVAL;
8071+
8072+
name = btf_name_by_offset(btf, t->name_off);
8073+
if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1))
8074+
return -EINVAL;
8075+
8076+
return btf_id;
8077+
}
8078+
80508079
static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,
80518080
const struct btf_type *func, u32 func_flags)
80528081
{
80538082
u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY);
8054-
const char *name, *sfx, *iter_name;
8055-
const struct btf_param *arg;
8083+
const char *sfx, *iter_name;
80568084
const struct btf_type *t;
80578085
char exp_name[128];
80588086
u32 nr_args;
8087+
int btf_id;
80598088

80608089
/* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */
80618090
if (!flags || (flags & (flags - 1)))
@@ -8066,28 +8095,21 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,
80668095
if (nr_args < 1)
80678096
return -EINVAL;
80688097

8069-
arg = &btf_params(func)[0];
8070-
t = btf_type_skip_modifiers(btf, arg->type, NULL);
8071-
if (!t || !btf_type_is_ptr(t))
8072-
return -EINVAL;
8073-
t = btf_type_skip_modifiers(btf, t->type, NULL);
8074-
if (!t || !__btf_type_is_struct(t))
8075-
return -EINVAL;
8076-
8077-
name = btf_name_by_offset(btf, t->name_off);
8078-
if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1))
8079-
return -EINVAL;
8098+
btf_id = btf_check_iter_arg(btf, func, 0);
8099+
if (btf_id < 0)
8100+
return btf_id;
80808101

80818102
/* sizeof(struct bpf_iter_<type>) should be a multiple of 8 to
80828103
* fit nicely in stack slots
80838104
*/
8105+
t = btf_type_by_id(btf, btf_id);
80848106
if (t->size == 0 || (t->size % 8))
80858107
return -EINVAL;
80868108

80878109
/* validate bpf_iter_<type>_{new,next,destroy}(struct bpf_iter_<type> *)
80888110
* naming pattern
80898111
*/
8090-
iter_name = name + sizeof(ITER_PREFIX) - 1;
8112+
iter_name = btf_name_by_offset(btf, t->name_off) + sizeof(ITER_PREFIX) - 1;
80918113
if (flags & KF_ITER_NEW)
80928114
sfx = "new";
80938115
else if (flags & KF_ITER_NEXT)

kernel/bpf/verifier.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7970,27 +7970,38 @@ static bool is_iter_destroy_kfunc(struct bpf_kfunc_call_arg_meta *meta)
79707970
return meta->kfunc_flags & KF_ITER_DESTROY;
79717971
}
79727972

7973-
static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg)
7973+
static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg_idx,
7974+
const struct btf_param *arg)
79747975
{
79757976
/* btf_check_iter_kfuncs() guarantees that first argument of any iter
79767977
* kfunc is iter state pointer
79777978
*/
7978-
return arg == 0 && is_iter_kfunc(meta);
7979+
if (is_iter_kfunc(meta))
7980+
return arg_idx == 0;
7981+
7982+
/* iter passed as an argument to a generic kfunc */
7983+
return btf_param_match_suffix(meta->btf, arg, "__iter");
79797984
}
79807985

79817986
static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx,
79827987
struct bpf_kfunc_call_arg_meta *meta)
79837988
{
79847989
struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
79857990
const struct btf_type *t;
7986-
const struct btf_param *arg;
7987-
int spi, err, i, nr_slots;
7988-
u32 btf_id;
7991+
int spi, err, i, nr_slots, btf_id;
79897992

7990-
/* btf_check_iter_kfuncs() ensures we don't need to validate anything here */
7991-
arg = &btf_params(meta->func_proto)[0];
7992-
t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */
7993-
t = btf_type_skip_modifiers(meta->btf, t->type, &btf_id); /* STRUCT */
7993+
/* For iter_{new,next,destroy} functions, btf_check_iter_kfuncs()
7994+
* ensures struct convention, so we wouldn't need to do any BTF
7995+
* validation here. But given iter state can be passed as a parameter
7996+
* to any kfunc, if arg has "__iter" suffix, we need to be a bit more
7997+
* conservative here.
7998+
*/
7999+
btf_id = btf_check_iter_arg(meta->btf, meta->func_proto, regno - 1);
8000+
if (btf_id < 0) {
8001+
verbose(env, "expected valid iter pointer as arg #%d\n", regno);
8002+
return -EINVAL;
8003+
}
8004+
t = btf_type_by_id(meta->btf, btf_id);
79948005
nr_slots = t->size / BPF_REG_SIZE;
79958006

79968007
if (is_iter_new_kfunc(meta)) {
@@ -8012,7 +8023,9 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id
80128023
if (err)
80138024
return err;
80148025
} else {
8015-
/* iter_next() or iter_destroy() expect initialized iter state*/
8026+
/* iter_next() or iter_destroy(), as well as any kfunc
8027+
* accepting iter argument, expect initialized iter state
8028+
*/
80168029
err = is_iter_reg_valid_init(env, reg, meta->btf, btf_id, nr_slots);
80178030
switch (err) {
80188031
case 0:
@@ -11382,7 +11395,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1138211395
if (is_kfunc_arg_dynptr(meta->btf, &args[argno]))
1138311396
return KF_ARG_PTR_TO_DYNPTR;
1138411397

11385-
if (is_kfunc_arg_iter(meta, argno))
11398+
if (is_kfunc_arg_iter(meta, argno, &args[argno]))
1138611399
return KF_ARG_PTR_TO_ITER;
1138711400

1138811401
if (is_kfunc_arg_list_head(meta->btf, &args[argno]))

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ bpf_testmod_test_mod_kfunc(int i)
141141

142142
__bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
143143
{
144-
if (cnt < 0) {
145-
it->cnt = 0;
144+
it->cnt = cnt;
145+
146+
if (cnt < 0)
146147
return -EINVAL;
147-
}
148148

149149
it->value = value;
150-
it->cnt = cnt;
151150

152151
return 0;
153152
}
@@ -162,6 +161,14 @@ __bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
162161
return &it->value;
163162
}
164163

164+
__bpf_kfunc s64 bpf_iter_testmod_seq_value(int val, struct bpf_iter_testmod_seq* it__iter)
165+
{
166+
if (it__iter->cnt < 0)
167+
return 0;
168+
169+
return val + it__iter->value;
170+
}
171+
165172
__bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
166173
{
167174
it->cnt = 0;
@@ -531,6 +538,7 @@ BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
531538
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
532539
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
533540
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
541+
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value)
534542
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
535543
BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
536544
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)

tools/testing/selftests/bpf/progs/iters_testmod_seq.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct bpf_iter_testmod_seq {
1212

1313
extern int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt) __ksym;
1414
extern s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq *it) __ksym;
15+
extern s64 bpf_iter_testmod_seq_value(int blah, struct bpf_iter_testmod_seq *it) __ksym;
1516
extern void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it) __ksym;
1617

1718
const volatile __s64 exp_empty = 0 + 1;
@@ -76,4 +77,53 @@ int testmod_seq_truncated(const void *ctx)
7677
return 0;
7778
}
7879

80+
SEC("?raw_tp")
81+
__failure
82+
__msg("expected an initialized iter_testmod_seq as arg #2")
83+
int testmod_seq_getter_before_bad(const void *ctx)
84+
{
85+
struct bpf_iter_testmod_seq it;
86+
87+
return bpf_iter_testmod_seq_value(0, &it);
88+
}
89+
90+
SEC("?raw_tp")
91+
__failure
92+
__msg("expected an initialized iter_testmod_seq as arg #2")
93+
int testmod_seq_getter_after_bad(const void *ctx)
94+
{
95+
struct bpf_iter_testmod_seq it;
96+
s64 sum = 0, *v;
97+
98+
bpf_iter_testmod_seq_new(&it, 100, 100);
99+
100+
while ((v = bpf_iter_testmod_seq_next(&it))) {
101+
sum += *v;
102+
}
103+
104+
bpf_iter_testmod_seq_destroy(&it);
105+
106+
return sum + bpf_iter_testmod_seq_value(0, &it);
107+
}
108+
109+
SEC("?socket")
110+
__success __retval(1000000)
111+
int testmod_seq_getter_good(const void *ctx)
112+
{
113+
struct bpf_iter_testmod_seq it;
114+
s64 sum = 0, *v;
115+
116+
bpf_iter_testmod_seq_new(&it, 100, 100);
117+
118+
while ((v = bpf_iter_testmod_seq_next(&it))) {
119+
sum += *v;
120+
}
121+
122+
sum *= bpf_iter_testmod_seq_value(0, &it);
123+
124+
bpf_iter_testmod_seq_destroy(&it);
125+
126+
return sum;
127+
}
128+
79129
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)