|
| 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 | + |
| 32 | + while (loop-- > 0) { |
| 33 | + int fd, zero = 0; |
| 34 | + |
| 35 | + fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 4, 1, NULL); |
| 36 | + if (fd < 0) { |
| 37 | + err |= 1; |
| 38 | + pthread_barrier_wait(&ctx->barrier); |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + /* Remove the old inner map */ |
| 43 | + if (bpf_map_update_elem(ctx->outer_map_fd, &zero, &fd, 0) < 0) |
| 44 | + err |= 2; |
| 45 | + close(fd); |
| 46 | + pthread_barrier_wait(&ctx->barrier); |
| 47 | + } |
| 48 | + |
| 49 | + ctx->err = err; |
| 50 | + |
| 51 | + return NULL; |
| 52 | +} |
| 53 | + |
| 54 | +static void *access_map_fn(void *data) |
| 55 | +{ |
| 56 | + struct thread_ctx *ctx = data; |
| 57 | + int loop = ctx->loop; |
| 58 | + |
| 59 | + if (wait_for_start_or_abort(ctx) < 0) |
| 60 | + return NULL; |
| 61 | + |
| 62 | + while (loop-- > 0) { |
| 63 | + /* Access the old inner map */ |
| 64 | + syscall(SYS_getpgid); |
| 65 | + pthread_barrier_wait(&ctx->barrier); |
| 66 | + } |
| 67 | + |
| 68 | + return NULL; |
| 69 | +} |
| 70 | + |
| 71 | +static void test_map_in_map_access(const char *prog_name, const char *map_name) |
| 72 | +{ |
| 73 | + struct access_map_in_map *skel; |
| 74 | + struct bpf_map *outer_map; |
| 75 | + struct bpf_program *prog; |
| 76 | + struct thread_ctx ctx; |
| 77 | + pthread_t tid[2]; |
| 78 | + int err; |
| 79 | + |
| 80 | + skel = access_map_in_map__open(); |
| 81 | + if (!ASSERT_OK_PTR(skel, "access_map_in_map open")) |
| 82 | + return; |
| 83 | + |
| 84 | + prog = bpf_object__find_program_by_name(skel->obj, prog_name); |
| 85 | + if (!ASSERT_OK_PTR(prog, "find program")) |
| 86 | + goto out; |
| 87 | + bpf_program__set_autoload(prog, true); |
| 88 | + |
| 89 | + outer_map = bpf_object__find_map_by_name(skel->obj, map_name); |
| 90 | + if (!ASSERT_OK_PTR(outer_map, "find map")) |
| 91 | + goto out; |
| 92 | + |
| 93 | + err = access_map_in_map__load(skel); |
| 94 | + if (!ASSERT_OK(err, "access_map_in_map load")) |
| 95 | + goto out; |
| 96 | + |
| 97 | + err = access_map_in_map__attach(skel); |
| 98 | + if (!ASSERT_OK(err, "access_map_in_map attach")) |
| 99 | + goto out; |
| 100 | + |
| 101 | + skel->bss->tgid = getpid(); |
| 102 | + |
| 103 | + memset(&ctx, 0, sizeof(ctx)); |
| 104 | + pthread_barrier_init(&ctx.barrier, NULL, 2); |
| 105 | + ctx.outer_map_fd = bpf_map__fd(outer_map); |
| 106 | + ctx.loop = 8; |
| 107 | + |
| 108 | + err = pthread_create(&tid[0], NULL, update_map_fn, &ctx); |
| 109 | + if (!ASSERT_OK(err, "close_thread")) |
| 110 | + goto out; |
| 111 | + |
| 112 | + err = pthread_create(&tid[1], NULL, access_map_fn, &ctx); |
| 113 | + if (!ASSERT_OK(err, "read_thread")) { |
| 114 | + ctx.abort = 1; |
| 115 | + pthread_join(tid[0], NULL); |
| 116 | + goto out; |
| 117 | + } |
| 118 | + |
| 119 | + ctx.start = 1; |
| 120 | + pthread_join(tid[0], NULL); |
| 121 | + pthread_join(tid[1], NULL); |
| 122 | + |
| 123 | + ASSERT_OK(ctx.err, "err"); |
| 124 | +out: |
| 125 | + access_map_in_map__destroy(skel); |
| 126 | +} |
| 127 | + |
| 128 | +void test_map_in_map(void) |
| 129 | +{ |
| 130 | + if (test__start_subtest("acc_map_in_array")) |
| 131 | + test_map_in_map_access("access_map_in_array", "outer_array_map"); |
| 132 | + if (test__start_subtest("sleepable_acc_map_in_array")) |
| 133 | + test_map_in_map_access("sleepable_access_map_in_array", "outer_array_map"); |
| 134 | + if (test__start_subtest("acc_map_in_htab")) |
| 135 | + test_map_in_map_access("access_map_in_htab", "outer_htab_map"); |
| 136 | + if (test__start_subtest("sleepable_acc_map_in_htab")) |
| 137 | + test_map_in_map_access("sleepable_access_map_in_htab", "outer_htab_map"); |
| 138 | +} |
0 commit comments