Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2026 Meta Platforms, Inc and affiliates. */
3#include <linux/bpf.h>
4#include <bpf/bpf_helpers.h>
5#include "bpf_misc.h"
6
7/*
8 * Macro tricks to tersely define for long non-recursive call chains. Add
9 * computation to the functions prevent tail recursion from reducing the
10 * stack size to 0.
11 */
12
13#define CAT(a, b) a ## b
14#define XCAT(a, b) CAT(a, b)
15
16#define F_0 \
17__attribute__((noinline)) \
18int f0(unsigned long a) \
19{ \
20 volatile long b = a + 16; \
21 if (a == 0) \
22 return 0; \
23 return b; \
24}
25
26#define FN(n, prev) \
27__attribute__((noinline)) \
28int XCAT(f, n)(unsigned long a) \
29{ \
30 volatile long b = XCAT(f, prev)(a - 1); \
31 if (!b) \
32 return 0; \
33 return b + 1; \
34}
35
36/* Call chain 33 levels deep. */
37#define F_1 F_0 FN(1, 0)
38#define F_2 F_1 FN(2, 1)
39#define F_3 F_2 FN(3, 2)
40#define F_4 F_3 FN(4, 3)
41#define F_5 F_4 FN(5, 4)
42#define F_6 F_5 FN(6, 5)
43#define F_7 F_6 FN(7, 6)
44#define F_8 F_7 FN(8, 7)
45#define F_9 F_8 FN(9, 8)
46#define F_10 F_9 FN(10, 9)
47#define F_11 F_10 FN(11, 10)
48#define F_12 F_11 FN(12, 11)
49#define F_13 F_12 FN(13, 12)
50#define F_14 F_13 FN(14, 13)
51#define F_15 F_14 FN(15, 14)
52#define F_16 F_15 FN(16, 15)
53#define F_17 F_16 FN(17, 16)
54#define F_18 F_17 FN(18, 17)
55#define F_19 F_18 FN(19, 18)
56#define F_20 F_19 FN(20, 19)
57#define F_21 F_20 FN(21, 20)
58#define F_22 F_21 FN(22, 21)
59#define F_23 F_22 FN(23, 22)
60#define F_24 F_23 FN(24, 23)
61#define F_25 F_24 FN(25, 24)
62#define F_26 F_25 FN(26, 25)
63#define F_27 F_26 FN(27, 26)
64#define F_28 F_27 FN(28, 27)
65#define F_29 F_28 FN(29, 28)
66#define F_30 F_29 FN(30, 29)
67#define F_31 F_30 FN(31, 30)
68#define F_32 F_31 FN(32, 31)
69
70#define CAT2(a, b) a ## b
71#define XCAT2(a, b) CAT2(a, b)
72
73#define F(n) XCAT2(F_, n)
74
75F(32)
76
77/* Ensure that even 32 levels deep, the function verifies. */
78SEC("syscall")
79__success
80int global_func_deep_stack_success(struct __sk_buff *skb)
81{
82 return f31(55);
83}
84
85/*
86 * Check we actually honor stack limits (33 * 16 = 528 > 512 = MAX_STACK_DEPTH).
87 * The stack depth is 16 because the verifier calls round_up_stack_depth() on
88 * the size.
89 */
90SEC("syscall")
91__failure __msg("combined stack size of 34 calls")
92int global_func_deep_stack_fail(struct __sk_buff *skb)
93{
94 return f32(123);
95}