• Martin KaFai Lau's avatar
    selftests/bpf: Test gen_prologue and gen_epilogue · 47e69431
    Martin KaFai Lau authored
    This test adds a new struct_ops "bpf_testmod_st_ops" in bpf_testmod.
    The ops of the bpf_testmod_st_ops is triggered by new kfunc calls
    "bpf_kfunc_st_ops_test_*logue". These new kfunc calls are
    primarily used by the SEC("syscall") program. The test triggering
    sequence is like:
        SEC("syscall")
        syscall_prologue(struct st_ops_args *args)
            bpf_kfunc_st_op_test_prologue(args)
    	    st_ops->test_prologue(args)
    
    .gen_prologue adds 1000 to args->a
    .gen_epilogue adds 10000 to args->a
    .gen_epilogue will also set the r0 to 2 * args->a.
    
    The .gen_prologue and .gen_epilogue of the bpf_testmod_st_ops
    will test the prog->aux->attach_func_name to decide if
    it needs to generate codes.
    
    The main programs of the pro_epilogue.c will call a
    new kfunc bpf_kfunc_st_ops_inc10 which does "args->a += 10".
    It will also call a subprog() which does "args->a += 1".
    
    This patch uses the test_loader infra to check the __xlated
    instructions patched after gen_prologue and/or gen_epilogue.
    The __xlated check is based on Eduard's example (Thanks!) in v1.
    
    args->a is returned by the struct_ops prog (either the main prog
    or the epilogue). Thus, the __retval of the SEC("syscall") prog
    is checked. For example, when triggering the ops in the
    'SEC("struct_ops/test_epilogue") int test_epilogue'
    The expected args->a is +1 (subprog call) + 10 (kfunc call)
        	     	     	+ 10000 (.gen_epilogue) = 10011.
    The expected return value is 2 * 10011 (.gen_epilogue).
    Suggested-by: default avatarEduard Zingerman <eddyz87@gmail.com>
    Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
    Link: https://lore.kernel.org/r/20240829210833.388152-7-martin.lau@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    47e69431
pro_epilogue.c 3.46 KB