1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2023. Huawei Technologies Co., Ltd */ 3 #define _GNU_SOURCE 4 #include <unistd.h> 5 #include <sys/syscall.h> 6 #include <test_progs.h> 7 #include <bpf/btf.h> 8 #include "access_map_in_map.skel.h" 9 10 struct thread_ctx { 11 pthread_barrier_t barrier; 12 int outer_map_fd; 13 int start, abort; 14 int loop, err; 15 }; 16 17 static int wait_for_start_or_abort(struct thread_ctx *ctx) 18 { 19 while (!ctx->start && !ctx->abort) 20 usleep(1); 21 return ctx->abort ? -1 : 0; 22 } 23 24 static void *update_map_fn(void *data) 25 { 26 struct thread_ctx *ctx = data; 27 int loop = ctx->loop, err = 0; 28 29 if (wait_for_start_or_abort(ctx) < 0) 30 return NULL; 31 pthread_barrier_wait(&ctx->barrier); 32 33 while (loop-- > 0) { 34 int fd, zero = 0; 35 36 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 4, 1, NULL); 37 if (fd < 0) { 38 err |= 1; 39 pthread_barrier_wait(&ctx->barrier); 40 continue; 41 } 42 43 /* Remove the old inner map */ 44 if (bpf_map_update_elem(ctx->outer_map_fd, &zero, &fd, 0) < 0) 45 err |= 2; 46 close(fd); 47 pthread_barrier_wait(&ctx->barrier); 48 } 49 50 ctx->err = err; 51 52 return NULL; 53 } 54 55 static void *access_map_fn(void *data) 56 { 57 struct thread_ctx *ctx = data; 58 int loop = ctx->loop; 59 60 if (wait_for_start_or_abort(ctx) < 0) 61 return NULL; 62 pthread_barrier_wait(&ctx->barrier); 63 64 while (loop-- > 0) { 65 /* Access the old inner map */ 66 syscall(SYS_getpgid); 67 pthread_barrier_wait(&ctx->barrier); 68 } 69 70 return NULL; 71 } 72 73 static void test_map_in_map_access(const char *prog_name, const char *map_name) 74 { 75 struct access_map_in_map *skel; 76 struct bpf_map *outer_map; 77 struct bpf_program *prog; 78 struct thread_ctx ctx; 79 pthread_t tid[2]; 80 int err; 81 82 skel = access_map_in_map__open(); 83 if (!ASSERT_OK_PTR(skel, "access_map_in_map open")) 84 return; 85 86 prog = bpf_object__find_program_by_name(skel->obj, prog_name); 87 if (!ASSERT_OK_PTR(prog, "find program")) 88 goto out; 89 bpf_program__set_autoload(prog, true); 90 91 outer_map = bpf_object__find_map_by_name(skel->obj, map_name); 92 if (!ASSERT_OK_PTR(outer_map, "find map")) 93 goto out; 94 95 err = access_map_in_map__load(skel); 96 if (!ASSERT_OK(err, "access_map_in_map load")) 97 goto out; 98 99 err = access_map_in_map__attach(skel); 100 if (!ASSERT_OK(err, "access_map_in_map attach")) 101 goto out; 102 103 skel->bss->tgid = getpid(); 104 105 memset(&ctx, 0, sizeof(ctx)); 106 pthread_barrier_init(&ctx.barrier, NULL, 2); 107 ctx.outer_map_fd = bpf_map__fd(outer_map); 108 ctx.loop = 4; 109 110 err = pthread_create(&tid[0], NULL, update_map_fn, &ctx); 111 if (!ASSERT_OK(err, "close_thread")) 112 goto out; 113 114 err = pthread_create(&tid[1], NULL, access_map_fn, &ctx); 115 if (!ASSERT_OK(err, "read_thread")) { 116 ctx.abort = 1; 117 pthread_join(tid[0], NULL); 118 goto out; 119 } 120 121 ctx.start = 1; 122 pthread_join(tid[0], NULL); 123 pthread_join(tid[1], NULL); 124 125 ASSERT_OK(ctx.err, "err"); 126 out: 127 access_map_in_map__destroy(skel); 128 } 129 130 void test_map_in_map(void) 131 { 132 if (test__start_subtest("acc_map_in_array")) 133 test_map_in_map_access("access_map_in_array", "outer_array_map"); 134 if (test__start_subtest("sleepable_acc_map_in_array")) 135 test_map_in_map_access("sleepable_access_map_in_array", "outer_array_map"); 136 if (test__start_subtest("acc_map_in_htab")) 137 test_map_in_map_access("access_map_in_htab", "outer_htab_map"); 138 if (test__start_subtest("sleepable_acc_map_in_htab")) 139 test_map_in_map_access("sleepable_access_map_in_htab", "outer_htab_map"); 140 } 141 142
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.