diff mbox series

[v4,bpf-next,4/4] selftests/bpf: add bpf_iter test with bpf_get_task_stack()

Message ID 20200629055530.3244342-5-songliubraving@fb.com
State Superseded
Headers show
Series bpf: introduce bpf_get_task_stack() | expand

Commit Message

Song Liu June 29, 2020, 5:55 a.m. UTC
The new test is similar to other bpf_iter tests. It dumps all
/proc/<pid>/stack to a seq_file. Here is some example output:

pid:     2873 num_entries:        3
[<0>] worker_thread+0xc6/0x380
[<0>] kthread+0x135/0x150
[<0>] ret_from_fork+0x22/0x30

pid:     2874 num_entries:        9
[<0>] __bpf_get_stack+0x15e/0x250
[<0>] bpf_prog_22a400774977bb30_dump_task_stack+0x4a/0xb3c
[<0>] bpf_iter_run_prog+0x81/0x170
[<0>] __task_seq_show+0x58/0x80
[<0>] bpf_seq_read+0x1c3/0x3b0
[<0>] vfs_read+0x9e/0x170
[<0>] ksys_read+0xa7/0xe0
[<0>] do_syscall_64+0x4c/0xa0
[<0>] entry_SYSCALL_64_after_hwframe+0x44/0xa9

Note: To print the output, it is necessary to modify the selftest.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 .../selftests/bpf/prog_tests/bpf_iter.c       | 17 +++++++++
 .../selftests/bpf/progs/bpf_iter_task_stack.c | 37 +++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c

Comments

Yonghong Song June 29, 2020, 3:06 p.m. UTC | #1
On 6/28/20 10:55 PM, Song Liu wrote:
> The new test is similar to other bpf_iter tests. It dumps all
> /proc/<pid>/stack to a seq_file. Here is some example output:
> 
> pid:     2873 num_entries:        3
> [<0>] worker_thread+0xc6/0x380
> [<0>] kthread+0x135/0x150
> [<0>] ret_from_fork+0x22/0x30
> 
> pid:     2874 num_entries:        9
> [<0>] __bpf_get_stack+0x15e/0x250
> [<0>] bpf_prog_22a400774977bb30_dump_task_stack+0x4a/0xb3c
> [<0>] bpf_iter_run_prog+0x81/0x170
> [<0>] __task_seq_show+0x58/0x80
> [<0>] bpf_seq_read+0x1c3/0x3b0
> [<0>] vfs_read+0x9e/0x170
> [<0>] ksys_read+0xa7/0xe0
> [<0>] do_syscall_64+0x4c/0xa0
> [<0>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
> 
> Note: To print the output, it is necessary to modify the selftest.

I do not know what this sentence means. It seems confusing
and probably not needed.

> 
> Signed-off-by: Song Liu <songliubraving@fb.com>

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   .../selftests/bpf/prog_tests/bpf_iter.c       | 17 +++++++++
>   .../selftests/bpf/progs/bpf_iter_task_stack.c | 37 +++++++++++++++++++
>   2 files changed, 54 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> index 1e2e0fced6e81..fed42755416db 100644
> --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
> @@ -5,6 +5,7 @@
>   #include "bpf_iter_netlink.skel.h"
>   #include "bpf_iter_bpf_map.skel.h"
>   #include "bpf_iter_task.skel.h"
> +#include "bpf_iter_task_stack.skel.h"
>   #include "bpf_iter_task_file.skel.h"
>   #include "bpf_iter_tcp4.skel.h"
>   #include "bpf_iter_tcp6.skel.h"
> @@ -110,6 +111,20 @@ static void test_task(void)
>   	bpf_iter_task__destroy(skel);
>   }
>   
> +static void test_task_stack(void)
> +{
> +	struct bpf_iter_task_stack *skel;
> +
> +	skel = bpf_iter_task_stack__open_and_load();
> +	if (CHECK(!skel, "bpf_iter_task_stack__open_and_load",
> +		  "skeleton open_and_load failed\n"))
> +		return;
> +
> +	do_dummy_read(skel->progs.dump_task_stack);
> +
> +	bpf_iter_task_stack__destroy(skel);
> +}
> +
>   static void test_task_file(void)
>   {
>   	struct bpf_iter_task_file *skel;
> @@ -452,6 +467,8 @@ void test_bpf_iter(void)
>   		test_bpf_map();
>   	if (test__start_subtest("task"))
>   		test_task();
> +	if (test__start_subtest("task_stack"))
> +		test_task_stack();
>   	if (test__start_subtest("task_file"))
>   		test_task_file();
>   	if (test__start_subtest("tcp4"))
> diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c b/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c
> new file mode 100644
> index 0000000000000..e40d32a2ed93d
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2020 Facebook */
> +#include "bpf_iter.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +#define MAX_STACK_TRACE_DEPTH   64
> +unsigned long entries[MAX_STACK_TRACE_DEPTH];
> +#define SIZE_OF_ULONG (sizeof(unsigned long))
> +
> +SEC("iter/task")
> +int dump_task_stack(struct bpf_iter__task *ctx)
> +{
> +	struct seq_file *seq = ctx->meta->seq;
> +	struct task_struct *task = ctx->task;
> +	long i, retlen;
> +
> +	if (task == (void *)0)
> +		return 0;
> +
> +	retlen = bpf_get_task_stack(task, entries,
> +				    MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, 0);
> +	if (retlen < 0)
> +		return 0;
> +
> +	BPF_SEQ_PRINTF(seq, "pid: %8u num_entries: %8u\n", task->pid,
> +		       retlen / SIZE_OF_ULONG);
> +	for (i = 0; i < MAX_STACK_TRACE_DEPTH; i++) {
> +		if (retlen > i * SIZE_OF_ULONG)
> +			BPF_SEQ_PRINTF(seq, "[<0>] %pB\n", (void *)entries[i]);
> +	}
> +	BPF_SEQ_PRINTF(seq, "\n");
> +
> +	return 0;
> +}
>
Song Liu June 29, 2020, 4:56 p.m. UTC | #2
> On Jun 29, 2020, at 8:06 AM, Yonghong Song <yhs@fb.com> wrote:
> 
> 
> 
> On 6/28/20 10:55 PM, Song Liu wrote:
>> The new test is similar to other bpf_iter tests. It dumps all
>> /proc/<pid>/stack to a seq_file. Here is some example output:
>> pid:     2873 num_entries:        3
>> [<0>] worker_thread+0xc6/0x380
>> [<0>] kthread+0x135/0x150
>> [<0>] ret_from_fork+0x22/0x30
>> pid:     2874 num_entries:        9
>> [<0>] __bpf_get_stack+0x15e/0x250
>> [<0>] bpf_prog_22a400774977bb30_dump_task_stack+0x4a/0xb3c
>> [<0>] bpf_iter_run_prog+0x81/0x170
>> [<0>] __task_seq_show+0x58/0x80
>> [<0>] bpf_seq_read+0x1c3/0x3b0
>> [<0>] vfs_read+0x9e/0x170
>> [<0>] ksys_read+0xa7/0xe0
>> [<0>] do_syscall_64+0x4c/0xa0
>> [<0>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
>> Note: To print the output, it is necessary to modify the selftest.
> 
> I do not know what this sentence means. It seems confusing
> and probably not needed.

It means current do_dummy_read() doesn't check/print the contents of the 
seq_file:

        /* not check contents, but ensure read() ends without error */
        while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
                ;

> 
>> Signed-off-by: Song Liu <songliubraving@fb.com>
> 
> Acked-by: Yonghong Song <yhs@fb.com>

Thanks!

[...]
Yonghong Song June 29, 2020, 6:22 p.m. UTC | #3
On 6/29/20 9:56 AM, Song Liu wrote:
> 
> 
>> On Jun 29, 2020, at 8:06 AM, Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 6/28/20 10:55 PM, Song Liu wrote:
>>> The new test is similar to other bpf_iter tests. It dumps all
>>> /proc/<pid>/stack to a seq_file. Here is some example output:
>>> pid:     2873 num_entries:        3
>>> [<0>] worker_thread+0xc6/0x380
>>> [<0>] kthread+0x135/0x150
>>> [<0>] ret_from_fork+0x22/0x30
>>> pid:     2874 num_entries:        9
>>> [<0>] __bpf_get_stack+0x15e/0x250
>>> [<0>] bpf_prog_22a400774977bb30_dump_task_stack+0x4a/0xb3c
>>> [<0>] bpf_iter_run_prog+0x81/0x170
>>> [<0>] __task_seq_show+0x58/0x80
>>> [<0>] bpf_seq_read+0x1c3/0x3b0
>>> [<0>] vfs_read+0x9e/0x170
>>> [<0>] ksys_read+0xa7/0xe0
>>> [<0>] do_syscall_64+0x4c/0xa0
>>> [<0>] entry_SYSCALL_64_after_hwframe+0x44/0xa9
>>> Note: To print the output, it is necessary to modify the selftest.
>>
>> I do not know what this sentence means. It seems confusing
>> and probably not needed.
> 
> It means current do_dummy_read() doesn't check/print the contents of the
> seq_file:
> 
>          /* not check contents, but ensure read() ends without error */
>          while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
>                  ;

I see. Thanks. It could be great if the commit message is more
explicit about what 'modify' is.

>>
>>> Signed-off-by: Song Liu <songliubraving@fb.com>
>>
>> Acked-by: Yonghong Song <yhs@fb.com>
> 
> Thanks!
> 
> [...]
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
index 1e2e0fced6e81..fed42755416db 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c
@@ -5,6 +5,7 @@ 
 #include "bpf_iter_netlink.skel.h"
 #include "bpf_iter_bpf_map.skel.h"
 #include "bpf_iter_task.skel.h"
+#include "bpf_iter_task_stack.skel.h"
 #include "bpf_iter_task_file.skel.h"
 #include "bpf_iter_tcp4.skel.h"
 #include "bpf_iter_tcp6.skel.h"
@@ -110,6 +111,20 @@  static void test_task(void)
 	bpf_iter_task__destroy(skel);
 }
 
+static void test_task_stack(void)
+{
+	struct bpf_iter_task_stack *skel;
+
+	skel = bpf_iter_task_stack__open_and_load();
+	if (CHECK(!skel, "bpf_iter_task_stack__open_and_load",
+		  "skeleton open_and_load failed\n"))
+		return;
+
+	do_dummy_read(skel->progs.dump_task_stack);
+
+	bpf_iter_task_stack__destroy(skel);
+}
+
 static void test_task_file(void)
 {
 	struct bpf_iter_task_file *skel;
@@ -452,6 +467,8 @@  void test_bpf_iter(void)
 		test_bpf_map();
 	if (test__start_subtest("task"))
 		test_task();
+	if (test__start_subtest("task_stack"))
+		test_task_stack();
 	if (test__start_subtest("task_file"))
 		test_task_file();
 	if (test__start_subtest("tcp4"))
diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c b/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c
new file mode 100644
index 0000000000000..e40d32a2ed93d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c
@@ -0,0 +1,37 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "bpf_iter.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define MAX_STACK_TRACE_DEPTH   64
+unsigned long entries[MAX_STACK_TRACE_DEPTH];
+#define SIZE_OF_ULONG (sizeof(unsigned long))
+
+SEC("iter/task")
+int dump_task_stack(struct bpf_iter__task *ctx)
+{
+	struct seq_file *seq = ctx->meta->seq;
+	struct task_struct *task = ctx->task;
+	long i, retlen;
+
+	if (task == (void *)0)
+		return 0;
+
+	retlen = bpf_get_task_stack(task, entries,
+				    MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, 0);
+	if (retlen < 0)
+		return 0;
+
+	BPF_SEQ_PRINTF(seq, "pid: %8u num_entries: %8u\n", task->pid,
+		       retlen / SIZE_OF_ULONG);
+	for (i = 0; i < MAX_STACK_TRACE_DEPTH; i++) {
+		if (retlen > i * SIZE_OF_ULONG)
+			BPF_SEQ_PRINTF(seq, "[<0>] %pB\n", (void *)entries[i]);
+	}
+	BPF_SEQ_PRINTF(seq, "\n");
+
+	return 0;
+}