selftests/bpf: Add tests for subprog topological ordering

Add few tests for topo sort:
- linear chain: main -> A -> B
- diamond: main -> A, main -> B, A -> C, B -> C
- mixed global/static: main -> global -> static leaf
- shared callee: main -> leaf, main -> global -> leaf
- duplicate calls: main calls same subprog twice
- no calls: single subprog

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20260403024422.87231-4-alexei.starovoitov@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Alexei Starovoitov 2026-04-02 19:44:18 -07:00
parent e6898ec751
commit 427c07ddb9
2 changed files with 228 additions and 0 deletions

View File

@ -93,6 +93,7 @@
#include "verifier_stack_ptr.skel.h"
#include "verifier_store_release.skel.h"
#include "verifier_subprog_precision.skel.h"
#include "verifier_subprog_topo.skel.h"
#include "verifier_subreg.skel.h"
#include "verifier_tailcall.skel.h"
#include "verifier_tailcall_jit.skel.h"
@ -238,6 +239,7 @@ void test_verifier_spin_lock(void) { RUN(verifier_spin_lock); }
void test_verifier_stack_ptr(void) { RUN(verifier_stack_ptr); }
void test_verifier_store_release(void) { RUN(verifier_store_release); }
void test_verifier_subprog_precision(void) { RUN(verifier_subprog_precision); }
void test_verifier_subprog_topo(void) { RUN(verifier_subprog_topo); }
void test_verifier_subreg(void) { RUN(verifier_subreg); }
void test_verifier_tailcall(void) { RUN(verifier_tailcall); }
void test_verifier_tailcall_jit(void) { RUN(verifier_tailcall_jit); }

View File

@ -0,0 +1,226 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
/* linear chain main -> A -> B */
__naked __noinline __used
static unsigned long linear_b(void)
{
asm volatile (
"r0 = 42;"
"exit;"
);
}
__naked __noinline __used
static unsigned long linear_a(void)
{
asm volatile (
"call linear_b;"
"exit;"
);
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = linear_b")
__msg("topo_order[1] = linear_a")
__msg("topo_order[2] = topo_linear")
__naked int topo_linear(void)
{
asm volatile (
"call linear_a;"
"exit;"
);
}
/* diamond main -> A, main -> B, A -> C, B -> C */
__naked __noinline __used
static unsigned long diamond_c(void)
{
asm volatile (
"r0 = 1;"
"exit;"
);
}
__naked __noinline __used
static unsigned long diamond_b(void)
{
asm volatile (
"call diamond_c;"
"exit;"
);
}
__naked __noinline __used
static unsigned long diamond_a(void)
{
asm volatile (
"call diamond_c;"
"exit;"
);
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = diamond_c")
__msg("topo_order[3] = topo_diamond")
__naked int topo_diamond(void)
{
asm volatile (
"call diamond_a;"
"call diamond_b;"
"exit;"
);
}
/* main -> global_a (global) -> static_leaf (static, leaf) */
__naked __noinline __used
static unsigned long static_leaf(void)
{
asm volatile (
"r0 = 7;"
"exit;"
);
}
__noinline __used
int global_a(int x)
{
return static_leaf();
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = static_leaf")
__msg("topo_order[1] = global_a")
__msg("topo_order[2] = topo_mixed")
__naked int topo_mixed(void)
{
asm volatile (
"r1 = 0;"
"call global_a;"
"exit;"
);
}
/*
* shared static callee from global and main:
* main -> shared_leaf (static)
* main -> global_b (global) -> shared_leaf (static)
*/
__naked __noinline __used
static unsigned long shared_leaf(void)
{
asm volatile (
"r0 = 99;"
"exit;"
);
}
__noinline __used
int global_b(int x)
{
return shared_leaf();
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = shared_leaf")
__msg("topo_order[1] = global_b")
__msg("topo_order[2] = topo_shared")
__naked int topo_shared(void)
{
asm volatile (
"call shared_leaf;"
"r1 = 0;"
"call global_b;"
"exit;"
);
}
/* duplicate calls to the same subprog */
__naked __noinline __used
static unsigned long dup_leaf(void)
{
asm volatile (
"r0 = 0;"
"exit;"
);
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = dup_leaf")
__msg("topo_order[1] = topo_dup_calls")
__naked int topo_dup_calls(void)
{
asm volatile (
"call dup_leaf;"
"call dup_leaf;"
"exit;"
);
}
/* main calls bpf_loop() with loop_cb as the callback */
static int loop_cb(int idx, void *ctx)
{
return 0;
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = loop_cb")
__msg("topo_order[1] = topo_loop_cb")
int topo_loop_cb(void)
{
bpf_loop(1, loop_cb, NULL, 0);
return 0;
}
/*
* bpf_loop callback calling another subprog
* main -> bpf_loop(callback=loop_cb2) -> loop_cb2 -> loop_cb2_leaf
*/
__naked __noinline __used
static unsigned long loop_cb2_leaf(void)
{
asm volatile (
"r0 = 0;"
"exit;"
);
}
static int loop_cb2(int idx, void *ctx)
{
return loop_cb2_leaf();
}
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = loop_cb2_leaf")
__msg("topo_order[1] = loop_cb2")
__msg("topo_order[2] = topo_loop_cb_chain")
int topo_loop_cb_chain(void)
{
bpf_loop(1, loop_cb2, NULL, 0);
return 0;
}
/* no calls (single subprog) */
SEC("?raw_tp")
__success __log_level(2)
__msg("topo_order[0] = topo_no_calls")
__naked int topo_no_calls(void)
{
asm volatile (
"r0 = 0;"
"exit;"
);
}
char _license[] SEC("license") = "GPL";