1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <sys/mman.h>
4 #include "test_mmap.skel.h"
5 
6 struct map_data {
7 	__u64 val[512 * 4];
8 };
9 
10 static size_t roundup_page(size_t sz)
11 {
12 	long page_size = sysconf(_SC_PAGE_SIZE);
13 	return (sz + page_size - 1) / page_size * page_size;
14 }
15 
16 void test_mmap(void)
17 {
18 	const size_t bss_sz = roundup_page(sizeof(struct test_mmap__bss));
19 	const size_t map_sz = roundup_page(sizeof(struct map_data));
20 	const int zero = 0, one = 1, two = 2, far = 1500;
21 	const long page_size = sysconf(_SC_PAGE_SIZE);
22 	int err, duration = 0, i, data_map_fd;
23 	struct bpf_map *data_map, *bss_map;
24 	void *bss_mmaped = NULL, *map_mmaped = NULL, *tmp1, *tmp2;
25 	struct test_mmap__bss *bss_data;
26 	struct map_data *map_data;
27 	struct test_mmap *skel;
28 	__u64 val = 0;
29 
30 
31 	skel = test_mmap__open_and_load();
32 	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
33 		return;
34 
35 	bss_map = skel->maps.bss;
36 	data_map = skel->maps.data_map;
37 	data_map_fd = bpf_map__fd(data_map);
38 
39 	bss_mmaped = mmap(NULL, bss_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
40 			  bpf_map__fd(bss_map), 0);
41 	if (CHECK(bss_mmaped == MAP_FAILED, "bss_mmap",
42 		  ".bss mmap failed: %d\n", errno)) {
43 		bss_mmaped = NULL;
44 		goto cleanup;
45 	}
46 	/* map as R/W first */
47 	map_mmaped = mmap(NULL, map_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
48 			  data_map_fd, 0);
49 	if (CHECK(map_mmaped == MAP_FAILED, "data_mmap",
50 		  "data_map mmap failed: %d\n", errno)) {
51 		map_mmaped = NULL;
52 		goto cleanup;
53 	}
54 
55 	bss_data = bss_mmaped;
56 	map_data = map_mmaped;
57 
58 	CHECK_FAIL(bss_data->in_val);
59 	CHECK_FAIL(bss_data->out_val);
60 	CHECK_FAIL(skel->bss->in_val);
61 	CHECK_FAIL(skel->bss->out_val);
62 	CHECK_FAIL(map_data->val[0]);
63 	CHECK_FAIL(map_data->val[1]);
64 	CHECK_FAIL(map_data->val[2]);
65 	CHECK_FAIL(map_data->val[far]);
66 
67 	err = test_mmap__attach(skel);
68 	if (CHECK(err, "attach_raw_tp", "err %d\n", err))
69 		goto cleanup;
70 
71 	bss_data->in_val = 123;
72 	val = 111;
73 	CHECK_FAIL(bpf_map_update_elem(data_map_fd, &zero, &val, 0));
74 
75 	usleep(1);
76 
77 	CHECK_FAIL(bss_data->in_val != 123);
78 	CHECK_FAIL(bss_data->out_val != 123);
79 	CHECK_FAIL(skel->bss->in_val != 123);
80 	CHECK_FAIL(skel->bss->out_val != 123);
81 	CHECK_FAIL(map_data->val[0] != 111);
82 	CHECK_FAIL(map_data->val[1] != 222);
83 	CHECK_FAIL(map_data->val[2] != 123);
84 	CHECK_FAIL(map_data->val[far] != 3 * 123);
85 
86 	CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &zero, &val));
87 	CHECK_FAIL(val != 111);
88 	CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &one, &val));
89 	CHECK_FAIL(val != 222);
90 	CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &two, &val));
91 	CHECK_FAIL(val != 123);
92 	CHECK_FAIL(bpf_map_lookup_elem(data_map_fd, &far, &val));
93 	CHECK_FAIL(val != 3 * 123);
94 
95 	/* data_map freeze should fail due to R/W mmap() */
96 	err = bpf_map_freeze(data_map_fd);
97 	if (CHECK(!err || errno != EBUSY, "no_freeze",
98 		  "data_map freeze succeeded: err=%d, errno=%d\n", err, errno))
99 		goto cleanup;
100 
101 	/* unmap R/W mapping */
102 	err = munmap(map_mmaped, map_sz);
103 	map_mmaped = NULL;
104 	if (CHECK(err, "data_map_munmap", "data_map munmap failed: %d\n", errno))
105 		goto cleanup;
106 
107 	/* re-map as R/O now */
108 	map_mmaped = mmap(NULL, map_sz, PROT_READ, MAP_SHARED, data_map_fd, 0);
109 	if (CHECK(map_mmaped == MAP_FAILED, "data_mmap",
110 		  "data_map R/O mmap failed: %d\n", errno)) {
111 		map_mmaped = NULL;
112 		goto cleanup;
113 	}
114 	map_data = map_mmaped;
115 
116 	/* map/unmap in a loop to test ref counting */
117 	for (i = 0; i < 10; i++) {
118 		int flags = i % 2 ? PROT_READ : PROT_WRITE;
119 		void *p;
120 
121 		p = mmap(NULL, map_sz, flags, MAP_SHARED, data_map_fd, 0);
122 		if (CHECK_FAIL(p == MAP_FAILED))
123 			goto cleanup;
124 		err = munmap(p, map_sz);
125 		if (CHECK_FAIL(err))
126 			goto cleanup;
127 	}
128 
129 	/* data_map freeze should now succeed due to no R/W mapping */
130 	err = bpf_map_freeze(data_map_fd);
131 	if (CHECK(err, "freeze", "data_map freeze failed: err=%d, errno=%d\n",
132 		  err, errno))
133 		goto cleanup;
134 
135 	/* mapping as R/W now should fail */
136 	tmp1 = mmap(NULL, map_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
137 		    data_map_fd, 0);
138 	if (CHECK(tmp1 != MAP_FAILED, "data_mmap", "mmap succeeded\n")) {
139 		munmap(tmp1, map_sz);
140 		goto cleanup;
141 	}
142 
143 	bss_data->in_val = 321;
144 	usleep(1);
145 	CHECK_FAIL(bss_data->in_val != 321);
146 	CHECK_FAIL(bss_data->out_val != 321);
147 	CHECK_FAIL(skel->bss->in_val != 321);
148 	CHECK_FAIL(skel->bss->out_val != 321);
149 	CHECK_FAIL(map_data->val[0] != 111);
150 	CHECK_FAIL(map_data->val[1] != 222);
151 	CHECK_FAIL(map_data->val[2] != 321);
152 	CHECK_FAIL(map_data->val[far] != 3 * 321);
153 
154 	/* check some more advanced mmap() manipulations */
155 
156 	/* map all but last page: pages 1-3 mapped */
157 	tmp1 = mmap(NULL, 3 * page_size, PROT_READ, MAP_SHARED,
158 			  data_map_fd, 0);
159 	if (CHECK(tmp1 == MAP_FAILED, "adv_mmap1", "errno %d\n", errno))
160 		goto cleanup;
161 
162 	/* unmap second page: pages 1, 3 mapped */
163 	err = munmap(tmp1 + page_size, page_size);
164 	if (CHECK(err, "adv_mmap2", "errno %d\n", errno)) {
165 		munmap(tmp1, map_sz);
166 		goto cleanup;
167 	}
168 
169 	/* map page 2 back */
170 	tmp2 = mmap(tmp1 + page_size, page_size, PROT_READ,
171 		    MAP_SHARED | MAP_FIXED, data_map_fd, 0);
172 	if (CHECK(tmp2 == MAP_FAILED, "adv_mmap3", "errno %d\n", errno)) {
173 		munmap(tmp1, page_size);
174 		munmap(tmp1 + 2*page_size, page_size);
175 		goto cleanup;
176 	}
177 	CHECK(tmp1 + page_size != tmp2, "adv_mmap4",
178 	      "tmp1: %p, tmp2: %p\n", tmp1, tmp2);
179 
180 	/* re-map all 4 pages */
181 	tmp2 = mmap(tmp1, 4 * page_size, PROT_READ, MAP_SHARED | MAP_FIXED,
182 		    data_map_fd, 0);
183 	if (CHECK(tmp2 == MAP_FAILED, "adv_mmap5", "errno %d\n", errno)) {
184 		munmap(tmp1, 3 * page_size); /* unmap page 1 */
185 		goto cleanup;
186 	}
187 	CHECK(tmp1 != tmp2, "adv_mmap6", "tmp1: %p, tmp2: %p\n", tmp1, tmp2);
188 
189 	map_data = tmp2;
190 	CHECK_FAIL(bss_data->in_val != 321);
191 	CHECK_FAIL(bss_data->out_val != 321);
192 	CHECK_FAIL(skel->bss->in_val != 321);
193 	CHECK_FAIL(skel->bss->out_val != 321);
194 	CHECK_FAIL(map_data->val[0] != 111);
195 	CHECK_FAIL(map_data->val[1] != 222);
196 	CHECK_FAIL(map_data->val[2] != 321);
197 	CHECK_FAIL(map_data->val[far] != 3 * 321);
198 
199 	munmap(tmp2, 4 * page_size);
200 cleanup:
201 	if (bss_mmaped)
202 		CHECK_FAIL(munmap(bss_mmaped, bss_sz));
203 	if (map_mmaped)
204 		CHECK_FAIL(munmap(map_mmaped, map_sz));
205 	test_mmap__destroy(skel);
206 }
207