selftests: bpf: Add a new test for bare tracepoints
authorQais Yousef <qais.yousef@arm.com>
Tue, 19 Jan 2021 12:22:37 +0000 (12:22 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 20 Jan 2021 22:14:05 +0000 (14:14 -0800)
Reuse module_attach infrastructure to add a new bare tracepoint to check
we can attach to it as a raw tracepoint.

Signed-off-by: Qais Yousef <qais.yousef@arm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210119122237.2426878-3-qais.yousef@arm.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

index b83ea44..89c6d58 100644 (file)
@@ -28,6 +28,12 @@ TRACE_EVENT(bpf_testmod_test_read,
                  __entry->pid, __entry->comm, __entry->off, __entry->len)
 );
 
+/* A bare tracepoint with no event associated with it */
+DECLARE_TRACE(bpf_testmod_test_write_bare,
+       TP_PROTO(struct task_struct *task, struct bpf_testmod_test_write_ctx *ctx),
+       TP_ARGS(task, ctx)
+);
+
 #endif /* _BPF_TESTMOD_EVENTS_H */
 
 #undef TRACE_INCLUDE_PATH
index 0b991e1..141d8da 100644 (file)
@@ -31,9 +31,28 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
 EXPORT_SYMBOL(bpf_testmod_test_read);
 ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
 
+noinline ssize_t
+bpf_testmod_test_write(struct file *file, struct kobject *kobj,
+                     struct bin_attribute *bin_attr,
+                     char *buf, loff_t off, size_t len)
+{
+       struct bpf_testmod_test_write_ctx ctx = {
+               .buf = buf,
+               .off = off,
+               .len = len,
+       };
+
+       trace_bpf_testmod_test_write_bare(current, &ctx);
+
+       return -EIO; /* always fail */
+}
+EXPORT_SYMBOL(bpf_testmod_test_write);
+ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
+
 static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
-       .attr = { .name = "bpf_testmod", .mode = 0444, },
+       .attr = { .name = "bpf_testmod", .mode = 0666, },
        .read = bpf_testmod_test_read,
+       .write = bpf_testmod_test_write,
 };
 
 static int bpf_testmod_init(void)
index b81adfe..b3892dc 100644 (file)
@@ -11,4 +11,10 @@ struct bpf_testmod_test_read_ctx {
        size_t len;
 };
 
+struct bpf_testmod_test_write_ctx {
+       char *buf;
+       loff_t off;
+       size_t len;
+};
+
 #endif /* _BPF_TESTMOD_H */
index 50796b6..5bc53d5 100644 (file)
@@ -21,9 +21,34 @@ static int trigger_module_test_read(int read_sz)
        return 0;
 }
 
+static int trigger_module_test_write(int write_sz)
+{
+       int fd, err;
+       char *buf = malloc(write_sz);
+
+       if (!buf)
+               return -ENOMEM;
+
+       memset(buf, 'a', write_sz);
+       buf[write_sz-1] = '\0';
+
+       fd = open("/sys/kernel/bpf_testmod", O_WRONLY);
+       err = -errno;
+       if (CHECK(fd < 0, "testmod_file_open", "failed: %d\n", err)) {
+               free(buf);
+               return err;
+       }
+
+       write(fd, buf, write_sz);
+       close(fd);
+       free(buf);
+       return 0;
+}
+
 void test_module_attach(void)
 {
        const int READ_SZ = 456;
+       const int WRITE_SZ = 457;
        struct test_module_attach* skel;
        struct test_module_attach__bss *bss;
        int err;
@@ -48,8 +73,10 @@ void test_module_attach(void)
 
        /* trigger tracepoint */
        ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
+       ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
 
        ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
+       ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
        ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
        ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
        ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
index efd1e28..bd37cee 100644 (file)
@@ -17,6 +17,16 @@ int BPF_PROG(handle_raw_tp,
        return 0;
 }
 
+__u32 raw_tp_bare_write_sz = 0;
+
+SEC("raw_tp/bpf_testmod_test_write_bare")
+int BPF_PROG(handle_raw_tp_bare,
+            struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx)
+{
+       raw_tp_bare_write_sz = BPF_CORE_READ(write_ctx, len);
+       return 0;
+}
+
 __u32 tp_btf_read_sz = 0;
 
 SEC("tp_btf/bpf_testmod_test_read")