xref: /linux/tools/testing/selftests/bpf/progs/wq_failures.c (revision f879306834818ebd1722a4372079610cdd466fec)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Benjamin Tissoires
3  */
4 
5 #include "bpf_experimental.h"
6 #include <bpf/bpf_helpers.h>
7 #include "bpf_misc.h"
8 #include "../bpf_testmod/bpf_testmod_kfunc.h"
9 
10 char _license[] SEC("license") = "GPL";
11 
12 struct elem {
13 	struct bpf_wq w;
14 };
15 
16 struct {
17 	__uint(type, BPF_MAP_TYPE_ARRAY);
18 	__uint(max_entries, 2);
19 	__type(key, int);
20 	__type(value, struct elem);
21 } array SEC(".maps");
22 
23 struct {
24 	__uint(type, BPF_MAP_TYPE_LRU_HASH);
25 	__uint(max_entries, 4);
26 	__type(key, int);
27 	__type(value, struct elem);
28 } lru SEC(".maps");
29 
30 /* callback for non sleepable workqueue */
31 static int wq_callback(void *map, int *key, void *value)
32 {
33 	bpf_kfunc_common_test();
34 	return 0;
35 }
36 
37 /* callback for sleepable workqueue */
38 static int wq_cb_sleepable(void *map, int *key, void *value)
39 {
40 	bpf_kfunc_call_test_sleepable();
41 	return 0;
42 }
43 
44 SEC("tc")
45 /* test that bpf_wq_init takes a map as a second argument
46  */
47 __log_level(2)
48 __flag(BPF_F_TEST_STATE_FREQ)
49 __failure
50 __msg(": (85) call bpf_wq_init#") /* anchor message */
51 __msg("pointer in R2 isn't map pointer")
52 long test_wq_init_nomap(void *ctx)
53 {
54 	struct bpf_wq *wq;
55 	struct elem *val;
56 	int key = 0;
57 
58 	val = bpf_map_lookup_elem(&array, &key);
59 	if (!val)
60 		return -1;
61 
62 	wq = &val->w;
63 	if (bpf_wq_init(wq, &key, 0) != 0)
64 		return -3;
65 
66 	return 0;
67 }
68 
69 SEC("tc")
70 /* test that the workqueue is part of the map in bpf_wq_init
71  */
72 __log_level(2)
73 __flag(BPF_F_TEST_STATE_FREQ)
74 __failure
75 __msg(": (85) call bpf_wq_init#") /* anchor message */
76 __msg("workqueue pointer in R1 map_uid=0 doesn't match map pointer in R2 map_uid=0")
77 long test_wq_init_wrong_map(void *ctx)
78 {
79 	struct bpf_wq *wq;
80 	struct elem *val;
81 	int key = 0;
82 
83 	val = bpf_map_lookup_elem(&array, &key);
84 	if (!val)
85 		return -1;
86 
87 	wq = &val->w;
88 	if (bpf_wq_init(wq, &lru, 0) != 0)
89 		return -3;
90 
91 	return 0;
92 }
93 
94 SEC("?tc")
95 __log_level(2)
96 __failure
97 /* check that the first argument of bpf_wq_set_callback()
98  * is a correct bpf_wq pointer.
99  */
100 __msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */
101 __msg("arg#0 doesn't point to a map value")
102 long test_wrong_wq_pointer(void *ctx)
103 {
104 	int key = 0;
105 	struct bpf_wq *wq;
106 
107 	wq = bpf_map_lookup_elem(&array, &key);
108 	if (!wq)
109 		return 1;
110 
111 	if (bpf_wq_init(wq, &array, 0))
112 		return 2;
113 
114 	if (bpf_wq_set_callback((void *)&wq, wq_callback, 0))
115 		return 3;
116 
117 	return -22;
118 }
119 
120 SEC("?tc")
121 __log_level(2)
122 __failure
123 /* check that the first argument of bpf_wq_set_callback()
124  * is a correct bpf_wq pointer.
125  */
126 __msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */
127 __msg("off 1 doesn't point to 'struct bpf_wq' that is at 0")
128 long test_wrong_wq_pointer_offset(void *ctx)
129 {
130 	int key = 0;
131 	struct bpf_wq *wq;
132 
133 	wq = bpf_map_lookup_elem(&array, &key);
134 	if (!wq)
135 		return 1;
136 
137 	if (bpf_wq_init(wq, &array, 0))
138 		return 2;
139 
140 	if (bpf_wq_set_callback((void *)wq + 1, wq_cb_sleepable, 0))
141 		return 3;
142 
143 	return -22;
144 }
145