tools/testing/selftests/bpf/progs/stack_arg.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/stack_arg.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/stack_arg.c
Extension
.c
Size
6083 bytes
Lines
274
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct timer_elem {
	struct bpf_timer timer;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, struct timer_elem);
} timer_map SEC(".maps");

int timer_result;

#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \
	defined(__BPF_FEATURE_STACK_ARGUMENT)

const volatile bool has_stack_arg = true;

__noinline static int static_func_many_args(int a, int b, int c, int d,
					    int e, int f, int g, int h,
					    int i, int j)
{
	return a + b + c + d + e + f + g + h + i + j;
}

__noinline int global_calls_many_args(int a, int b, int c)
{
	return static_func_many_args(a, b, c, a + 3, a + 4, a + 5, a + 6,
				     a + 7, a + 8, a + 9);
}

SEC("tc")
int test_global_many_args(void)
{
	return global_calls_many_args(1, 2, 3);
}

struct test_data {
	long x;
	long y;
};

/* 1+2+3+4+5+6+7+8+9+10+20 = 75 */
__noinline static long func_with_ptr_stack_arg(long a, long b, long c, long d,
					       long e, long f, long g, long h,
					       long i, struct test_data *p)
{
	return a + b + c + d + e + f + g + h + i + p->x + p->y;
}

__noinline long global_ptr_stack_arg(long a, long b, long c, long d, long e)
{
	struct test_data data = { .x = 10, .y = 20 };

	return func_with_ptr_stack_arg(a, b, c, d, e, a + 5, a + 6, a + 7,
				      a + 8, &data);
}

SEC("tc")
int test_bpf2bpf_ptr_stack_arg(void)
{
	return global_ptr_stack_arg(1, 2, 3, 4, 5);
}

/* 1+2+3+4+5+6+7+10+8+20 = 66 */
__noinline static long func_with_mix_stack_args(long a, long b, long c, long d,
						long e, long f, long g,
						struct test_data *p,
						long h, struct test_data *q)
{
	return a + b + c + d + e + f + g + p->x + h + q->y;
}

__noinline long global_mix_stack_args(long a, long b, long c, long d, long e)
{
	struct test_data p = { .x = 10 };
	struct test_data q = { .y = 20 };

	return func_with_mix_stack_args(a, b, c, d, e, e + 1, e + 2, &p,
					e + 3, &q);
}

SEC("tc")
int test_bpf2bpf_mix_stack_args(void)
{
	return global_mix_stack_args(1, 2, 3, 4, 5);
}

/*
 * Nesting test: func_outer calls func_inner, both with struct pointer

Annotation

Implementation Notes