1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 static void *parallel_map_access(void *arg
)
6 int err
, map_fd
= *(u32
*) arg
;
7 int vars
[17], i
, j
, rnd
, key
= 0;
9 for (i
= 0; i
< 10000; i
++) {
10 err
= bpf_map_lookup_elem_flags(map_fd
, &key
, vars
, BPF_F_LOCK
);
12 printf("lookup failed\n");
17 printf("lookup #%d var[0]=%d\n", i
, vars
[0]);
22 for (j
= 2; j
< 17; j
++) {
25 printf("lookup #%d var[1]=%d var[%d]=%d\n",
35 void test_map_lock(void)
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
;
44 err
= bpf_prog_load(file
, BPF_PROG_TYPE_CGROUP_SKB
, &obj
, &prog_fd
);
46 printf("test_map_lock:bpf_prog_load errno %d\n", errno
);
49 map_fd
[0] = bpf_find_map(__func__
, obj
, "hash_map");
52 map_fd
[1] = bpf_find_map(__func__
, obj
, "array_map");
56 bpf_map_update_elem(map_fd
[0], &key
, vars
, BPF_F_LOCK
);
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
;
74 bpf_object__close(obj
);