selftests/bpf: trace_helpers.c: Add a global ksyms initialization mutex
authorRong Tao <rongtao@cestc.cn>
Thu, 7 Sep 2023 01:59:14 +0000 (09:59 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 8 Sep 2023 23:22:41 +0000 (16:22 -0700)
As Jirka said [0], we just need to make sure that global ksyms
initialization won't race.

[0] https://lore.kernel.org/lkml/ZPCbAs3ItjRd8XVh@krava/

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/tencent_5D0A837E219E2CFDCB0495DAD7D5D1204407@qq.com
tools/testing/selftests/bpf/trace_helpers.c

index dc4efaf538ae95ae1bfa049be351144e58c4ef41..4faa898ff7fc430ecf2a9ec39a7336618b625528 100644 (file)
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <poll.h>
+#include <pthread.h>
 #include <unistd.h>
 #include <linux/perf_event.h>
 #include <sys/mman.h>
@@ -26,6 +27,7 @@ struct ksyms {
 };
 
 static struct ksyms *ksyms;
+static pthread_mutex_t ksyms_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int ksyms__add_symbol(struct ksyms *ksyms, const char *name,
                             unsigned long addr)
@@ -109,8 +111,10 @@ error:
 
 int load_kallsyms(void)
 {
+       pthread_mutex_lock(&ksyms_mutex);
        if (!ksyms)
                ksyms = load_kallsyms_local();
+       pthread_mutex_unlock(&ksyms_mutex);
        return ksyms ? 0 : 1;
 }