1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 4 static void *parallel_map_access(void *arg) 5 { 6 int err, map_fd = *(u32 *) arg; 7 int vars[17], i, j, rnd, key = 0; 8 9 for (i = 0; i < 10000; i++) { 10 err = bpf_map_lookup_elem_flags(map_fd, &key, vars, BPF_F_LOCK); 11 if (err) { 12 printf("lookup failed\n"); 13 error_cnt++; 14 goto out; 15 } 16 if (vars[0] != 0) { 17 printf("lookup #%d var[0]=%d\n", i, vars[0]); 18 error_cnt++; 19 goto out; 20 } 21 rnd = vars[1]; 22 for (j = 2; j < 17; j++) { 23 if (vars[j] == rnd) 24 continue; 25 printf("lookup #%d var[1]=%d var[%d]=%d\n", 26 i, rnd, j, vars[j]); 27 error_cnt++; 28 goto out; 29 } 30 } 31 out: 32 pthread_exit(arg); 33 } 34 35 void test_map_lock(void) 36 { 37 const char *file = "./test_map_lock.o"; 38 int prog_fd, map_fd[2], vars[17] = {}; 39 pthread_t thread_id[6]; 40 struct bpf_object *obj = NULL; 41 int err = 0, key = 0, i; 42 void *ret; 43 44 err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd); 45 if (err) { 46 printf("test_map_lock:bpf_prog_load errno %d\n", errno); 47 goto close_prog; 48 } 49 map_fd[0] = bpf_find_map(__func__, obj, "hash_map"); 50 if (map_fd[0] < 0) 51 goto close_prog; 52 map_fd[1] = bpf_find_map(__func__, obj, "array_map"); 53 if (map_fd[1] < 0) 54 goto close_prog; 55 56 bpf_map_update_elem(map_fd[0], &key, vars, BPF_F_LOCK); 57 58 for (i = 0; i < 4; i++) 59 assert(pthread_create(&thread_id[i], NULL, 60 &spin_lock_thread, &prog_fd) == 0); 61 for (i = 4; i < 6; i++) 62 assert(pthread_create(&thread_id[i], NULL, 63 ¶llel_map_access, &map_fd[i - 4]) == 0); 64 for (i = 0; i < 4; i++) 65 assert(pthread_join(thread_id[i], &ret) == 0 && 66 ret == (void *)&prog_fd); 67 for (i = 4; i < 6; i++) 68 assert(pthread_join(thread_id[i], &ret) == 0 && 69 ret == (void *)&map_fd[i - 4]); 70 goto close_prog_noerr; 71 close_prog: 72 error_cnt++; 73 close_prog_noerr: 74 bpf_object__close(obj); 75 } 76