1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022. Huawei Technologies Co., Ltd */
6 #include <test_progs.h>
7 #include "htab_update.skel.h"
9 struct htab_update_ctx {
15 static void test_reenter_update(void)
17 struct htab_update *skel;
18 unsigned int key, value;
21 skel = htab_update__open();
22 if (!ASSERT_OK_PTR(skel, "htab_update__open"))
25 /* lookup_elem_raw() may be inlined and find_kernel_btf_id() will return -ESRCH */
26 bpf_program__set_autoload(skel->progs.lookup_elem_raw, true);
27 err = htab_update__load(skel);
28 if (!ASSERT_TRUE(!err || err == -ESRCH, "htab_update__load") || err)
31 skel->bss->pid = getpid();
32 err = htab_update__attach(skel);
33 if (!ASSERT_OK(err, "htab_update__attach"))
36 /* Will trigger the reentrancy of bpf_map_update_elem() */
39 err = bpf_map_update_elem(bpf_map__fd(skel->maps.htab), &key, &value, 0);
40 if (!ASSERT_OK(err, "add element"))
43 ASSERT_EQ(skel->bss->update_err, -EBUSY, "no reentrancy");
45 htab_update__destroy(skel);
48 static void *htab_update_thread(void *arg)
50 struct htab_update_ctx *ctx = arg;
57 pthread_setaffinity_np(pthread_self(), sizeof(cpus), &cpus);
60 while (i++ < ctx->loop && !ctx->stop) {
61 unsigned int key = 0, value = 0;
64 err = bpf_map_update_elem(ctx->fd, &key, &value, 0);
67 return (void *)(long)err;
74 static void test_concurrent_update(void)
76 struct htab_update_ctx ctx;
77 struct htab_update *skel;
82 skel = htab_update__open_and_load();
83 if (!ASSERT_OK_PTR(skel, "htab_update__open_and_load"))
86 ctx.fd = bpf_map__fd(skel->maps.htab);
91 tids = calloc(nr, sizeof(*tids));
92 if (!ASSERT_NEQ(tids, NULL, "no mem"))
95 for (i = 0; i < nr; i++) {
96 err = pthread_create(&tids[i], NULL, htab_update_thread, &ctx);
97 if (!ASSERT_OK(err, "pthread_create")) {
101 for (j = 0; j < i; j++)
102 pthread_join(tids[j], NULL);
107 for (i = 0; i < nr; i++) {
108 void *thread_err = NULL;
110 pthread_join(tids[i], &thread_err);
111 ASSERT_EQ(thread_err, NULL, "update error");
117 htab_update__destroy(skel);
120 void test_htab_update(void)
122 if (test__start_subtest("reenter_update"))
123 test_reenter_update();
124 if (test__start_subtest("concurrent_update"))
125 test_concurrent_update();