~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/map_in_map.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php