• Andrii Nakryiko's avatar
    bpf: abstract away global subprog arg preparation logic from reg state setup · 4ba1d0f2
    Andrii Nakryiko authored
    btf_prepare_func_args() is used to understand expectations and
    restrictions on global subprog arguments. But current implementation is
    hard to extend, as it intermixes BTF-based func prototype parsing and
    interpretation logic with setting up register state at subprog entry.
    
    Worse still, those registers are not completely set up inside
    btf_prepare_func_args(), requiring some more logic later in
    do_check_common(). Like calling mark_reg_unknown() and similar
    initialization operations.
    
    This intermixing of BTF interpretation and register state setup is
    problematic. First, it causes duplication of BTF parsing logic for global
    subprog verification (to set up initial state of global subprog) and
    global subprog call sites analysis (when we need to check that whatever
    is being passed into global subprog matches expectations), performed in
    btf_check_subprog_call().
    
    Given we want to extend global func argument with tags later, this
    duplication is problematic. So refactor btf_prepare_func_args() to do
    only BTF-based func proto and args parsing, returning high-level
    argument "expectations" only, with no regard to specifics of register
    state. I.e., if it's a context argument, instead of setting register
    state to PTR_TO_CTX, we return ARG_PTR_TO_CTX enum for that argument as
    "an argument specification" for further processing inside
    do_check_common(). Similarly for SCALAR arguments, PTR_TO_MEM, etc.
    
    This allows to reuse btf_prepare_func_args() in following patches at
    global subprog call site analysis time. It also keeps register setup
    code consistently in one place, do_check_common().
    
    Besides all this, we cache this argument specs information inside
    env->subprog_info, eliminating the need to redo these potentially
    expensive BTF traversals, especially if BPF program's BTF is big and/or
    there are lots of global subprog calls.
    Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231215011334.2307144-2-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    4ba1d0f2
bpf.h 102 KB