xref: /linux/tools/testing/selftests/bpf/progs/stack_arg.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
179e7ec00SYonghong Song // SPDX-License-Identifier: GPL-2.0
279e7ec00SYonghong Song /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
379e7ec00SYonghong Song 
479e7ec00SYonghong Song #include <vmlinux.h>
579e7ec00SYonghong Song #include <stdbool.h>
679e7ec00SYonghong Song #include <bpf/bpf_helpers.h>
779e7ec00SYonghong Song #include "bpf_kfuncs.h"
879e7ec00SYonghong Song 
979e7ec00SYonghong Song #define CLOCK_MONOTONIC 1
1079e7ec00SYonghong Song 
1179e7ec00SYonghong Song struct timer_elem {
1279e7ec00SYonghong Song 	struct bpf_timer timer;
1379e7ec00SYonghong Song };
1479e7ec00SYonghong Song 
1579e7ec00SYonghong Song struct {
1679e7ec00SYonghong Song 	__uint(type, BPF_MAP_TYPE_ARRAY);
1779e7ec00SYonghong Song 	__uint(max_entries, 1);
1879e7ec00SYonghong Song 	__type(key, int);
1979e7ec00SYonghong Song 	__type(value, struct timer_elem);
2079e7ec00SYonghong Song } timer_map SEC(".maps");
2179e7ec00SYonghong Song 
2279e7ec00SYonghong Song int timer_result;
2379e7ec00SYonghong Song 
2490e43f1bSPuranjay Mohan #if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \
2590e43f1bSPuranjay Mohan 	defined(__BPF_FEATURE_STACK_ARGUMENT)
2679e7ec00SYonghong Song 
2779e7ec00SYonghong Song const volatile bool has_stack_arg = true;
2879e7ec00SYonghong Song 
2979e7ec00SYonghong Song __noinline static int static_func_many_args(int a, int b, int c, int d,
30*157317baSPuranjay Mohan 					    int e, int f, int g, int h,
31*157317baSPuranjay Mohan 					    int i, int j)
3279e7ec00SYonghong Song {
33*157317baSPuranjay Mohan 	return a + b + c + d + e + f + g + h + i + j;
3479e7ec00SYonghong Song }
3579e7ec00SYonghong Song 
3679e7ec00SYonghong Song __noinline int global_calls_many_args(int a, int b, int c)
3779e7ec00SYonghong Song {
38*157317baSPuranjay Mohan 	return static_func_many_args(a, b, c, a + 3, a + 4, a + 5, a + 6,
39*157317baSPuranjay Mohan 				     a + 7, a + 8, a + 9);
4079e7ec00SYonghong Song }
4179e7ec00SYonghong Song 
4279e7ec00SYonghong Song SEC("tc")
4379e7ec00SYonghong Song int test_global_many_args(void)
4479e7ec00SYonghong Song {
4579e7ec00SYonghong Song 	return global_calls_many_args(1, 2, 3);
4679e7ec00SYonghong Song }
4779e7ec00SYonghong Song 
4879e7ec00SYonghong Song struct test_data {
4979e7ec00SYonghong Song 	long x;
5079e7ec00SYonghong Song 	long y;
5179e7ec00SYonghong Song };
5279e7ec00SYonghong Song 
53*157317baSPuranjay Mohan /* 1+2+3+4+5+6+7+8+9+10+20 = 75 */
5479e7ec00SYonghong Song __noinline static long func_with_ptr_stack_arg(long a, long b, long c, long d,
55*157317baSPuranjay Mohan 					       long e, long f, long g, long h,
56*157317baSPuranjay Mohan 					       long i, struct test_data *p)
5779e7ec00SYonghong Song {
58*157317baSPuranjay Mohan 	return a + b + c + d + e + f + g + h + i + p->x + p->y;
5979e7ec00SYonghong Song }
6079e7ec00SYonghong Song 
6179e7ec00SYonghong Song __noinline long global_ptr_stack_arg(long a, long b, long c, long d, long e)
6279e7ec00SYonghong Song {
6379e7ec00SYonghong Song 	struct test_data data = { .x = 10, .y = 20 };
6479e7ec00SYonghong Song 
65*157317baSPuranjay Mohan 	return func_with_ptr_stack_arg(a, b, c, d, e, a + 5, a + 6, a + 7,
66*157317baSPuranjay Mohan 				      a + 8, &data);
6779e7ec00SYonghong Song }
6879e7ec00SYonghong Song 
6979e7ec00SYonghong Song SEC("tc")
7079e7ec00SYonghong Song int test_bpf2bpf_ptr_stack_arg(void)
7179e7ec00SYonghong Song {
7279e7ec00SYonghong Song 	return global_ptr_stack_arg(1, 2, 3, 4, 5);
7379e7ec00SYonghong Song }
7479e7ec00SYonghong Song 
75*157317baSPuranjay Mohan /* 1+2+3+4+5+6+7+10+8+20 = 66 */
7679e7ec00SYonghong Song __noinline static long func_with_mix_stack_args(long a, long b, long c, long d,
77*157317baSPuranjay Mohan 						long e, long f, long g,
78*157317baSPuranjay Mohan 						struct test_data *p,
79*157317baSPuranjay Mohan 						long h, struct test_data *q)
8079e7ec00SYonghong Song {
81*157317baSPuranjay Mohan 	return a + b + c + d + e + f + g + p->x + h + q->y;
8279e7ec00SYonghong Song }
8379e7ec00SYonghong Song 
8479e7ec00SYonghong Song __noinline long global_mix_stack_args(long a, long b, long c, long d, long e)
8579e7ec00SYonghong Song {
8679e7ec00SYonghong Song 	struct test_data p = { .x = 10 };
8779e7ec00SYonghong Song 	struct test_data q = { .y = 20 };
8879e7ec00SYonghong Song 
89*157317baSPuranjay Mohan 	return func_with_mix_stack_args(a, b, c, d, e, e + 1, e + 2, &p,
90*157317baSPuranjay Mohan 					e + 3, &q);
9179e7ec00SYonghong Song }
9279e7ec00SYonghong Song 
9379e7ec00SYonghong Song SEC("tc")
9479e7ec00SYonghong Song int test_bpf2bpf_mix_stack_args(void)
9579e7ec00SYonghong Song {
9679e7ec00SYonghong Song 	return global_mix_stack_args(1, 2, 3, 4, 5);
9779e7ec00SYonghong Song }
9879e7ec00SYonghong Song 
9979e7ec00SYonghong Song /*
10079e7ec00SYonghong Song  * Nesting test: func_outer calls func_inner, both with struct pointer
10179e7ec00SYonghong Song  * as stack arg.
10279e7ec00SYonghong Song  *
103*157317baSPuranjay Mohan  * func_inner: (a+1)+...+(i+1) + p->x + p->y
104*157317baSPuranjay Mohan  *           = 2+3+4+5+6+7+8+9+10+10+20 = 84
10579e7ec00SYonghong Song  */
10679e7ec00SYonghong Song __noinline static long func_inner_ptr(long a, long b, long c, long d,
107*157317baSPuranjay Mohan 				      long e, long f, long g, long h,
108*157317baSPuranjay Mohan 				      long i, struct test_data *p)
10979e7ec00SYonghong Song {
110*157317baSPuranjay Mohan 	return a + b + c + d + e + f + g + h + i + p->x + p->y;
11179e7ec00SYonghong Song }
11279e7ec00SYonghong Song 
11379e7ec00SYonghong Song __noinline static long func_outer_ptr(long a, long b, long c, long d,
114*157317baSPuranjay Mohan 				      long e, long f, long g, long h,
115*157317baSPuranjay Mohan 				      long i, struct test_data *p)
11679e7ec00SYonghong Song {
117*157317baSPuranjay Mohan 	return func_inner_ptr(a + 1, b + 1, c + 1, d + 1, e + 1,
118*157317baSPuranjay Mohan 			      f + 1, g + 1, h + 1, i + 1, p);
11979e7ec00SYonghong Song }
12079e7ec00SYonghong Song 
12179e7ec00SYonghong Song __noinline long global_nesting_ptr(long a, long b, long c, long d, long e)
12279e7ec00SYonghong Song {
12379e7ec00SYonghong Song 	struct test_data data = { .x = 10, .y = 20 };
12479e7ec00SYonghong Song 
125*157317baSPuranjay Mohan 	return func_outer_ptr(a, b, c, d, e, a + 5, a + 6, a + 7, a + 8,
126*157317baSPuranjay Mohan 			      &data);
12779e7ec00SYonghong Song }
12879e7ec00SYonghong Song 
12979e7ec00SYonghong Song SEC("tc")
13079e7ec00SYonghong Song int test_bpf2bpf_nesting_stack_arg(void)
13179e7ec00SYonghong Song {
13279e7ec00SYonghong Song 	return global_nesting_ptr(1, 2, 3, 4, 5);
13379e7ec00SYonghong Song }
13479e7ec00SYonghong Song 
135*157317baSPuranjay Mohan /* 1+2+3+4+5+6+7+8+9+sizeof(pkt_v4) = 45+54 = 99 */
13679e7ec00SYonghong Song __noinline static long func_with_dynptr(long a, long b, long c, long d,
137*157317baSPuranjay Mohan 					long e, long f, long g, long h,
138*157317baSPuranjay Mohan 					long i, struct bpf_dynptr *ptr)
13979e7ec00SYonghong Song {
140*157317baSPuranjay Mohan 	return a + b + c + d + e + f + g + h + i + bpf_dynptr_size(ptr);
14179e7ec00SYonghong Song }
14279e7ec00SYonghong Song 
14379e7ec00SYonghong Song __noinline long global_dynptr_stack_arg(void *ctx __arg_ctx, long a, long b,
14479e7ec00SYonghong Song 					long c, long d)
14579e7ec00SYonghong Song {
14679e7ec00SYonghong Song 	struct bpf_dynptr ptr;
14779e7ec00SYonghong Song 
14879e7ec00SYonghong Song 	bpf_dynptr_from_skb(ctx, 0, &ptr);
149*157317baSPuranjay Mohan 	return func_with_dynptr(a, b, c, d, d + 1, d + 2, d + 3, d + 4,
150*157317baSPuranjay Mohan 				d + 5, &ptr);
15179e7ec00SYonghong Song }
15279e7ec00SYonghong Song 
15379e7ec00SYonghong Song SEC("tc")
15479e7ec00SYonghong Song int test_bpf2bpf_dynptr_stack_arg(struct __sk_buff *skb)
15579e7ec00SYonghong Song {
15679e7ec00SYonghong Song 	return global_dynptr_stack_arg(skb, 1, 2, 3, 4);
15779e7ec00SYonghong Song }
15879e7ec00SYonghong Song 
159*157317baSPuranjay Mohan /* foo1: a+b+c+d+e+f+g+h+i+j */
160*157317baSPuranjay Mohan __noinline static int foo1(int a, int b, int c, int d, int e,
16179e7ec00SYonghong Song 			   int f, int g, int h, int i, int j)
16279e7ec00SYonghong Song {
16379e7ec00SYonghong Song 	return a + b + c + d + e + f + g + h + i + j;
16479e7ec00SYonghong Song }
16579e7ec00SYonghong Song 
166*157317baSPuranjay Mohan /* foo2: a+b+c+d+e+f+g+h+i+j+k+l */
167*157317baSPuranjay Mohan __noinline static int foo2(int a, int b, int c, int d, int e,
168*157317baSPuranjay Mohan 			   int f, int g, int h, int i, int j,
169*157317baSPuranjay Mohan 			   int k, int l)
170*157317baSPuranjay Mohan {
171*157317baSPuranjay Mohan 	return a + b + c + d + e + f + g + h + i + j + k + l;
172*157317baSPuranjay Mohan }
173*157317baSPuranjay Mohan 
174*157317baSPuranjay Mohan /* global_two_callees calls foo1 (5 stack args) and foo2 (7 stack args).
17579e7ec00SYonghong Song  * The outgoing stack arg area is sized for foo2 (the larger callee).
17679e7ec00SYonghong Song  * Stores for foo1 are a subset of the area used by foo2.
177*157317baSPuranjay Mohan  * Result: foo1(1..10) + foo2(1..12) = 55 + 78 = 133
17879e7ec00SYonghong Song  *
17979e7ec00SYonghong Song  * Pass a-e through so the compiler can't constant-fold the stack args away.
18079e7ec00SYonghong Song  */
18179e7ec00SYonghong Song __noinline int global_two_callees(int a, int b, int c, int d, int e)
18279e7ec00SYonghong Song {
18379e7ec00SYonghong Song 	int ret;
18479e7ec00SYonghong Song 
185*157317baSPuranjay Mohan 	ret = foo1(a, b, c, d, e, a + 5, a + 6, a + 7, a + 8, a + 9);
186*157317baSPuranjay Mohan 	ret += foo2(a, b, c, d, e, a + 5, a + 6, a + 7, a + 8, a + 9,
187*157317baSPuranjay Mohan 		    a + 10, a + 11);
18879e7ec00SYonghong Song 	return ret;
18979e7ec00SYonghong Song }
19079e7ec00SYonghong Song 
19179e7ec00SYonghong Song SEC("tc")
19279e7ec00SYonghong Song int test_two_callees(void)
19379e7ec00SYonghong Song {
19479e7ec00SYonghong Song 	return global_two_callees(1, 2, 3, 4, 5);
19579e7ec00SYonghong Song }
19679e7ec00SYonghong Song 
197*157317baSPuranjay Mohan const volatile int timer_base = 10;
198*157317baSPuranjay Mohan 
19979e7ec00SYonghong Song static int timer_cb_many_args(void *map, int *key, struct bpf_timer *timer)
20079e7ec00SYonghong Song {
201*157317baSPuranjay Mohan 	int v = timer_base;
202*157317baSPuranjay Mohan 
203*157317baSPuranjay Mohan 	timer_result = static_func_many_args(v, v * 2, v * 3, v * 4, v * 5,
204*157317baSPuranjay Mohan 					     v * 6, v * 7, v * 8, v * 9,
205*157317baSPuranjay Mohan 					     v * 10);
20679e7ec00SYonghong Song 	return 0;
20779e7ec00SYonghong Song }
20879e7ec00SYonghong Song 
20979e7ec00SYonghong Song SEC("tc")
21079e7ec00SYonghong Song int test_async_cb_many_args(void)
21179e7ec00SYonghong Song {
21279e7ec00SYonghong Song 	struct timer_elem *elem;
21379e7ec00SYonghong Song 	int key = 0;
21479e7ec00SYonghong Song 
21579e7ec00SYonghong Song 	elem = bpf_map_lookup_elem(&timer_map, &key);
21679e7ec00SYonghong Song 	if (!elem)
21779e7ec00SYonghong Song 		return -1;
21879e7ec00SYonghong Song 
21979e7ec00SYonghong Song 	bpf_timer_init(&elem->timer, &timer_map, CLOCK_MONOTONIC);
22079e7ec00SYonghong Song 	bpf_timer_set_callback(&elem->timer, timer_cb_many_args);
22179e7ec00SYonghong Song 	bpf_timer_start(&elem->timer, 1, 0);
22279e7ec00SYonghong Song 	return 0;
22379e7ec00SYonghong Song }
22479e7ec00SYonghong Song 
22579e7ec00SYonghong Song #else
22679e7ec00SYonghong Song 
22779e7ec00SYonghong Song const volatile bool has_stack_arg = false;
22879e7ec00SYonghong Song 
22979e7ec00SYonghong Song SEC("tc")
23079e7ec00SYonghong Song int test_global_many_args(void)
23179e7ec00SYonghong Song {
23279e7ec00SYonghong Song 	return 0;
23379e7ec00SYonghong Song }
23479e7ec00SYonghong Song 
23579e7ec00SYonghong Song SEC("tc")
23679e7ec00SYonghong Song int test_bpf2bpf_ptr_stack_arg(void)
23779e7ec00SYonghong Song {
23879e7ec00SYonghong Song 	return 0;
23979e7ec00SYonghong Song }
24079e7ec00SYonghong Song 
24179e7ec00SYonghong Song SEC("tc")
24279e7ec00SYonghong Song int test_bpf2bpf_mix_stack_args(void)
24379e7ec00SYonghong Song {
24479e7ec00SYonghong Song 	return 0;
24579e7ec00SYonghong Song }
24679e7ec00SYonghong Song 
24779e7ec00SYonghong Song SEC("tc")
24879e7ec00SYonghong Song int test_bpf2bpf_nesting_stack_arg(void)
24979e7ec00SYonghong Song {
25079e7ec00SYonghong Song 	return 0;
25179e7ec00SYonghong Song }
25279e7ec00SYonghong Song 
25379e7ec00SYonghong Song SEC("tc")
25479e7ec00SYonghong Song int test_bpf2bpf_dynptr_stack_arg(struct __sk_buff *skb)
25579e7ec00SYonghong Song {
25679e7ec00SYonghong Song 	return 0;
25779e7ec00SYonghong Song }
25879e7ec00SYonghong Song 
25979e7ec00SYonghong Song SEC("tc")
26079e7ec00SYonghong Song int test_two_callees(void)
26179e7ec00SYonghong Song {
26279e7ec00SYonghong Song 	return 0;
26379e7ec00SYonghong Song }
26479e7ec00SYonghong Song 
26579e7ec00SYonghong Song SEC("tc")
26679e7ec00SYonghong Song int test_async_cb_many_args(void)
26779e7ec00SYonghong Song {
26879e7ec00SYonghong Song 	return 0;
26979e7ec00SYonghong Song }
27079e7ec00SYonghong Song 
27179e7ec00SYonghong Song #endif
27279e7ec00SYonghong Song 
27379e7ec00SYonghong Song char _license[] SEC("license") = "GPL";
274