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