On Sun, Sep 04, 2022 at 10:41:28PM +0200, Kumar Kartikeya Dwivedi wrote: > For a lot of use cases in future patches, we will want to modify the > state of registers part of some same 'group' (e.g. same ref_obj_id). It > won't just be limited to releasing reference state, but setting a type > flag dynamically based on certain actions, etc. > > Hence, we need a way to easily pass a callback to the function that > iterates over all registers in current bpf_verifier_state in all frames > upto (and including) the curframe. > > While in C++ we would be able to easily use a lambda to pass state and > the callback together, sadly we aren't using C++ in the kernel. The next > best thing to avoid defining a function for each case seems like > statement expressions in GNU C. The kernel already uses them heavily, > hence they can passed to the macro in the style of a lambda. The > statement expression will then be substituted in the for loop bodies. > > Variables __state and __reg are set to current bpf_func_state and reg > for each invocation of the expression inside the passed in verifier > state. > > Then, convert mark_ptr_or_null_regs, clear_all_pkt_pointers, > release_reference, find_good_pkt_pointers, find_equal_scalars to > use bpf_expr_for_each_reg_in_vstate. > > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> > --- > include/linux/bpf_verifier.h | 21 ++++++ > kernel/bpf/verifier.c | 135 ++++++++--------------------------- > 2 files changed, 49 insertions(+), 107 deletions(-) > > diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h > index c6d550978d63..73d9443d0074 100644 > --- a/include/linux/bpf_verifier.h > +++ b/include/linux/bpf_verifier.h > @@ -354,6 +354,27 @@ struct bpf_verifier_state { > iter < frame->allocated_stack / BPF_REG_SIZE; \ > iter++, reg = bpf_get_spilled_reg(iter, frame)) > > +/* Invoke __expr over regsiters in __vst, setting __state and __reg */ > +#define bpf_expr_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \ Very nice. I renamed it to bpf_for_each_reg_in_vstate to make it less verbose and more consistent with existing bpf_for_each_spilled_reg. And applied to bpf-next.