[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220324234123.1608337-3-haoluo@google.com>
Date: Thu, 24 Mar 2022 16:41:23 -0700
From: Hao Luo <haoluo@...gle.com>
To: Alexei Starovoitov <ast@...nel.org>,
Andrii Nakryiko <andrii@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>
Cc: yhs@...com, KP Singh <kpsingh@...nel.org>,
Martin KaFai Lau <kafai@...com>,
Song Liu <songliubraving@...com>, bpf@...r.kernel.org,
linux-kernel@...r.kernel.org, Hao Luo <haoluo@...gle.com>
Subject: [PATCH RFC bpf-next 2/2] selftests/bpf: Test mmapable task local storage.
Tests mmapable task local storage.
Signed-off-by: Hao Luo <haoluo@...gle.com>
---
.../bpf/prog_tests/task_local_storage.c | 38 +++++++++++++++++++
.../bpf/progs/task_local_storage_mmapable.c | 38 +++++++++++++++++++
2 files changed, 76 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c
diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
index 035c263aab1b..24e6edd32a78 100644
--- a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
+++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c
@@ -6,8 +6,10 @@
#include <sys/syscall.h> /* For SYS_xxx definitions */
#include <sys/types.h>
#include <test_progs.h>
+#include <sys/mman.h>
#include "task_local_storage.skel.h"
#include "task_local_storage_exit_creds.skel.h"
+#include "task_local_storage_mmapable.skel.h"
#include "task_ls_recursion.skel.h"
static void test_sys_enter_exit(void)
@@ -81,6 +83,40 @@ static void test_recursion(void)
task_ls_recursion__destroy(skel);
}
+#define MAGIC_VALUE 0xabcd1234
+
+static void test_mmapable(void)
+{
+ struct task_local_storage_mmapable *skel;
+ const long page_size = sysconf(_SC_PAGE_SIZE);
+ int fd, err;
+ void *ptr;
+
+ skel = task_local_storage_mmapable__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+ return;
+
+ fd = bpf_map__fd(skel->maps.mmapable_map);
+ ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (!ASSERT_NEQ(ptr, MAP_FAILED, "mmap"))
+ goto out;
+
+ skel->bss->target_pid = syscall(SYS_gettid);
+
+ err = task_local_storage_mmapable__attach(skel);
+ if (!ASSERT_OK(err, "skel_attach"))
+ goto unmap;
+
+ syscall(SYS_gettid);
+
+ ASSERT_EQ(*(u64 *)ptr, MAGIC_VALUE, "value");
+
+unmap:
+ munmap(ptr, page_size);
+out:
+ task_local_storage_mmapable__destroy(skel);
+}
+
void test_task_local_storage(void)
{
if (test__start_subtest("sys_enter_exit"))
@@ -89,4 +125,6 @@ void test_task_local_storage(void)
test_exit_creds();
if (test__start_subtest("recursion"))
test_recursion();
+ if (test__start_subtest("mmapable"))
+ test_mmapable();
}
diff --git a/tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c b/tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c
new file mode 100644
index 000000000000..8cd82bb7336a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Google */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_MMAPABLE);
+ __type(key, int);
+ __type(value, long);
+} mmapable_map SEC(".maps");
+
+#define MAGIC_VALUE 0xabcd1234
+
+pid_t target_pid = 0;
+
+SEC("tp_btf/sys_enter")
+int BPF_PROG(on_enter, struct pt_regs *regs, long id)
+{
+ struct task_struct *task;
+ long *ptr;
+
+ task = bpf_get_current_task_btf();
+ if (task->pid != target_pid)
+ return 0;
+
+ ptr = bpf_task_storage_get(&mmapable_map, task, 0,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!ptr)
+ return 0;
+
+ *ptr = MAGIC_VALUE;
+ return 0;
+}
--
2.35.1.1021.g381101b075-goog
Powered by blists - more mailing lists