xref: /linux/tools/testing/selftests/bpf/progs/stream.c (revision d9104cec3e8fe4b458b74709853231385779001f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3 #include <vmlinux.h>
4 #include <bpf/bpf_tracing.h>
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7 #include "bpf_experimental.h"
8 
9 struct arr_elem {
10 	struct bpf_res_spin_lock lock;
11 };
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_ARRAY);
15 	__uint(max_entries, 1);
16 	__type(key, int);
17 	__type(value, struct arr_elem);
18 } arrmap SEC(".maps");
19 
20 #define ENOSPC 28
21 #define _STR "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
22 
23 int size;
24 
25 SEC("syscall")
26 __success __retval(0)
stream_exhaust(void * ctx)27 int stream_exhaust(void *ctx)
28 {
29 	/* Use global variable for loop convergence. */
30 	size = 0;
31 	bpf_repeat(BPF_MAX_LOOPS) {
32 		if (bpf_stream_printk(BPF_STDOUT, _STR) == -ENOSPC && size == 99954)
33 			return 0;
34 		size += sizeof(_STR) - 1;
35 	}
36 	return 1;
37 }
38 
39 SEC("syscall")
40 __success __retval(0)
stream_cond_break(void * ctx)41 int stream_cond_break(void *ctx)
42 {
43 	while (can_loop)
44 		;
45 	return 0;
46 }
47 
48 SEC("syscall")
49 __success __retval(0)
stream_deadlock(void * ctx)50 int stream_deadlock(void *ctx)
51 {
52 	struct bpf_res_spin_lock *lock, *nlock;
53 
54 	lock = bpf_map_lookup_elem(&arrmap, &(int){0});
55 	if (!lock)
56 		return 1;
57 	nlock = bpf_map_lookup_elem(&arrmap, &(int){0});
58 	if (!nlock)
59 		return 1;
60 	if (bpf_res_spin_lock(lock))
61 		return 1;
62 	if (bpf_res_spin_lock(nlock)) {
63 		bpf_res_spin_unlock(lock);
64 		return 0;
65 	}
66 	bpf_res_spin_unlock(nlock);
67 	bpf_res_spin_unlock(lock);
68 	return 1;
69 }
70 
71 SEC("syscall")
72 __success __retval(0)
stream_syscall(void * ctx)73 int stream_syscall(void *ctx)
74 {
75 	bpf_stream_printk(BPF_STDOUT, "foo");
76 	return 0;
77 }
78 
79 char _license[] SEC("license") = "GPL";
80