On Wed, Sep 09, 2020 at 06:11:55PM +0100, Lorenz Bauer wrote: > The mapping between bpf_arg_type and bpf_reg_type is encoded in a big > hairy if statement that is hard to follow. The debug output also leaves > to be desired: if a reg_type doesn't match we only print one of the > options, instead printing all the valid ones. > > Convert the if statement into a table which is then used to drive type > checking. If none of the reg_types match we print all options, e.g.: > > R2 type=rdonly_buf expected=fp, pkt, pkt_meta, map_value > > Signed-off-by: Lorenz Bauer <lmb@xxxxxxxxxxxxxx> > --- > include/linux/bpf.h | 1 + > kernel/bpf/verifier.c | 189 +++++++++++++++++++++++++----------------- > 2 files changed, 116 insertions(+), 74 deletions(-) > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > index aabbc8bb52bd..7cf7e90f55bb 100644 > --- a/include/linux/bpf.h > +++ b/include/linux/bpf.h > @@ -292,6 +292,7 @@ enum bpf_arg_type { > ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */ > ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */ > ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */ > + __BPF_ARG_TYPE_MAX, > }; > > /* type of values returned from helper functions */ > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index ebf900fceaf0..9c9728c81d4c 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -3856,12 +3856,6 @@ static bool arg_type_is_mem_size(enum bpf_arg_type type) > type == ARG_CONST_SIZE_OR_ZERO; > } > > -static bool arg_type_is_alloc_mem_ptr(enum bpf_arg_type type) > -{ > - return type == ARG_PTR_TO_ALLOC_MEM || > - type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; > -} > - > static bool arg_type_is_alloc_size(enum bpf_arg_type type) > { > return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; > @@ -3910,14 +3904,121 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env, > return 0; > } > > +struct bpf_reg_types { > + const enum bpf_reg_type types[10]; > +}; > + > +static const struct bpf_reg_types map_key_value_types = { > + .types = { > + PTR_TO_STACK, > + PTR_TO_PACKET, > + PTR_TO_PACKET_META, > + PTR_TO_MAP_VALUE, > + }, > +}; > + > +static const struct bpf_reg_types sock_types = { > + .types = { > + PTR_TO_SOCK_COMMON, > + PTR_TO_SOCKET, > + PTR_TO_TCP_SOCK, > + PTR_TO_XDP_SOCK, > + }, > +}; > + > +static const struct bpf_reg_types mem_types = { > + .types = { > + PTR_TO_STACK, > + PTR_TO_PACKET, > + PTR_TO_PACKET_META, > + PTR_TO_MAP_VALUE, > + PTR_TO_MEM, > + PTR_TO_RDONLY_BUF, > + PTR_TO_RDWR_BUF, > + }, > +}; > + > +static const struct bpf_reg_types int_ptr_types = { > + .types = { > + PTR_TO_STACK, > + PTR_TO_PACKET, > + PTR_TO_PACKET_META, > + PTR_TO_MAP_VALUE, > + }, > +}; > + > +static const struct bpf_reg_types fullsock_types = { > + .types = { > + PTR_TO_SOCKET, > + PTR_TO_BTF_ID, PTR_TO_BTF_ID should be added at the sockmap-iter-update patch instead. Others lgtm.