bpf/selftests: Add test for writable bare tracepoint
authorHou Tao <houtao1@huawei.com>
Mon, 4 Oct 2021 09:48:57 +0000 (17:48 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 8 Oct 2021 20:22:57 +0000 (13:22 -0700)
Add a writable bare tracepoint in bpf_testmod module, and
trigger its calling when reading /sys/kernel/bpf_testmod
with a specific buffer length. The reading will return
the value in writable context if the early return flag
is enabled in writable context.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211004094857.30868-4-hotforest@gmail.com
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
tools/testing/selftests/bpf/prog_tests/module_attach.c
tools/testing/selftests/bpf/progs/test_module_attach.c
tools/testing/selftests/bpf/test_progs.c
tools/testing/selftests/bpf/test_progs.h

index 89c6d58e5dd68f28063bf81f2e4dcdf5d69cc05f..11ee801e75e7e048d6c1ea8e3dd4d252f274c35b 100644 (file)
@@ -34,6 +34,21 @@ DECLARE_TRACE(bpf_testmod_test_write_bare,
        TP_ARGS(task, ctx)
 );
 
+#undef BPF_TESTMOD_DECLARE_TRACE
+#ifdef DECLARE_TRACE_WRITABLE
+#define BPF_TESTMOD_DECLARE_TRACE(call, proto, args, size) \
+       DECLARE_TRACE_WRITABLE(call, PARAMS(proto), PARAMS(args), size)
+#else
+#define BPF_TESTMOD_DECLARE_TRACE(call, proto, args, size) \
+       DECLARE_TRACE(call, PARAMS(proto), PARAMS(args))
+#endif
+
+BPF_TESTMOD_DECLARE_TRACE(bpf_testmod_test_writable_bare,
+       TP_PROTO(struct bpf_testmod_test_writable_ctx *ctx),
+       TP_ARGS(ctx),
+       sizeof(struct bpf_testmod_test_writable_ctx)
+);
+
 #endif /* _BPF_TESTMOD_EVENTS_H */
 
 #undef TRACE_INCLUDE_PATH
index b892948dc134c9a70674a59c434f38f7cba3c329..5d52ea2768df4480a496ad87457fe8b862d57457 100644 (file)
@@ -50,6 +50,16 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
        if (bpf_testmod_loop_test(101) > 100)
                trace_bpf_testmod_test_read(current, &ctx);
 
+       /* Magic number to enable writable tp */
+       if (len == 64) {
+               struct bpf_testmod_test_writable_ctx writable = {
+                       .val = 1024,
+               };
+               trace_bpf_testmod_test_writable_bare(&writable);
+               if (writable.early_ret)
+                       return snprintf(buf, len, "%d\n", writable.val);
+       }
+
        return -EIO; /* always fail */
 }
 EXPORT_SYMBOL(bpf_testmod_test_read);
index b3892dc401113ec781b47450b1f220d1a509a229..0d71e2607832e5944ca4e95042c7f3331202f1b6 100644 (file)
@@ -17,4 +17,9 @@ struct bpf_testmod_test_write_ctx {
        size_t len;
 };
 
+struct bpf_testmod_test_writable_ctx {
+       bool early_ret;
+       int val;
+};
+
 #endif /* _BPF_TESTMOD_H */
index 1797a6e4d6d84363b7b74bfe2f37986ed0507e67..6d0e50dcf47cc90943f15406727ba993b35b54d7 100644 (file)
@@ -2,10 +2,36 @@
 /* Copyright (c) 2020 Facebook */
 
 #include <test_progs.h>
+#include <stdbool.h>
 #include "test_module_attach.skel.h"
 
 static int duration;
 
+static int trigger_module_test_writable(int *val)
+{
+       int fd, err;
+       char buf[65];
+       ssize_t rd;
+
+       fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
+       err = -errno;
+       if (!ASSERT_GE(fd, 0, "testmode_file_open"))
+               return err;
+
+       rd = read(fd, buf, sizeof(buf) - 1);
+       err = -errno;
+       if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) {
+               close(fd);
+               return err;
+       }
+
+       buf[rd] = '\0';
+       *val = strtol(buf, NULL, 0);
+       close(fd);
+
+       return 0;
+}
+
 static int delete_module(const char *name, int flags)
 {
        return syscall(__NR_delete_module, name, flags);
@@ -19,6 +45,7 @@ void test_module_attach(void)
        struct test_module_attach__bss *bss;
        struct bpf_link *link;
        int err;
+       int writable_val = 0;
 
        skel = test_module_attach__open();
        if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
@@ -51,6 +78,14 @@ void test_module_attach(void)
        ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet");
        ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret");
 
+       bss->raw_tp_writable_bare_early_ret = true;
+       bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4;
+       ASSERT_OK(trigger_module_test_writable(&writable_val),
+                 "trigger_writable");
+       ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in");
+       ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val,
+                 "writable_test_out");
+
        test_module_attach__detach(skel);
 
        /* attach fentry/fexit and make sure it get's module reference */
index bd37ceec55877a129fab09428c3e674cdcd42270..b36857093f71fea44eb42b068e0aff1e5d5b70b1 100644 (file)
@@ -27,6 +27,20 @@ int BPF_PROG(handle_raw_tp_bare,
        return 0;
 }
 
+int raw_tp_writable_bare_in_val = 0;
+int raw_tp_writable_bare_early_ret = 0;
+int raw_tp_writable_bare_out_val = 0;
+
+SEC("raw_tp.w/bpf_testmod_test_writable_bare")
+int BPF_PROG(handle_raw_tp_writable_bare,
+            struct bpf_testmod_test_writable_ctx *writable)
+{
+       raw_tp_writable_bare_in_val = writable->val;
+       writable->early_ret = raw_tp_writable_bare_early_ret;
+       writable->val = raw_tp_writable_bare_out_val;
+       return 0;
+}
+
 __u32 tp_btf_read_sz = 0;
 
 SEC("tp_btf/bpf_testmod_test_read")
index 2ed01f615d20fb53f568386bebdd374f28a17bd9..007b4ff85fea7e4d581229a6f95ce6c07bdf95b4 100644 (file)
@@ -747,7 +747,7 @@ int trigger_module_test_read(int read_sz)
 {
        int fd, err;
 
-       fd = open("/sys/kernel/bpf_testmod", O_RDONLY);
+       fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
        err = -errno;
        if (!ASSERT_GE(fd, 0, "testmod_file_open"))
                return err;
@@ -769,7 +769,7 @@ int trigger_module_test_write(int write_sz)
        memset(buf, 'a', write_sz);
        buf[write_sz-1] = '\0';
 
-       fd = open("/sys/kernel/bpf_testmod", O_WRONLY);
+       fd = open(BPF_TESTMOD_TEST_FILE, O_WRONLY);
        err = -errno;
        if (!ASSERT_GE(fd, 0, "testmod_file_open")) {
                free(buf);
index 94bef0aa74cf5ebc3c5573dc5da0410e326b0e20..9b8a1810b700a36b354f822e422012b17b24d232 100644 (file)
@@ -301,3 +301,5 @@ int trigger_module_test_write(int write_sz);
 #else
 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
 #endif
+
+#define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"