All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Kui-Feng Lee <kuifeng@fb.com>,
	bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, kernel-team@fb.com
Subject: Re: [PATCH bpf-next v8 4/5] selftests/bpf: Test parameterized task BPF iterators.
Date: Tue, 30 Aug 2022 15:54:43 -0700	[thread overview]
Message-ID: <7e24948d-4e50-2058-ba6c-05dcee9b7426@fb.com> (raw)
In-Reply-To: <20220829192317.486946-5-kuifeng@fb.com>



On 8/29/22 12:23 PM, Kui-Feng Lee wrote:
> Test iterators of vma, files and tasks.
> 
> Ensure the API works appropriately to visit all tasks,
> tasks in a process, or a particular task.
> 
> Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
> ---
>   .../selftests/bpf/prog_tests/bpf_iter.c       | 282 ++++++++++++++++--
>   .../selftests/bpf/prog_tests/btf_dump.c       |   2 +-
>   .../selftests/bpf/progs/bpf_iter_task.c       |   9 +
>   .../selftests/bpf/progs/bpf_iter_task_file.c  |   9 +-
>   .../selftests/bpf/progs/bpf_iter_task_vma.c   |   7 +-
>   .../selftests/bpf/progs/bpf_iter_vma_offset.c |  37 +++
>   6 files changed, 322 insertions(+), 24 deletions(-)
>   create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c
> 
[...]
> +
> +static void test_task_vma_offset_common(struct bpf_iter_attach_opts *opts, bool one_proc)
> +{
> +	struct bpf_iter_vma_offset *skel;
> +	struct bpf_link *link;
> +	char buf[16] = {};
> +	int iter_fd, len;
> +	int pgsz, shift;
> +
> +	skel = bpf_iter_vma_offset__open_and_load();
> +	if (!ASSERT_OK_PTR(skel, "bpf_iter_vma_offset__open_and_load"))
> +		return;
> +
> +	skel->bss->pid = getpid();
> +	skel->bss->address = (uintptr_t)trigger_func;
> +	for (pgsz = getpagesize(), shift = 0; pgsz; pgsz >>= 1, shift++)
> +		;
> +	skel->bss->page_shift = shift;

if getpagesize() is 4K, shift will be 13 which is incorrect.

> +
> +	link = bpf_program__attach_iter(skel->progs.get_vma_offset, opts);
> +	if (!ASSERT_OK_PTR(link, "attach_iter"))
> +		return;
> +
> +	iter_fd = bpf_iter_create(bpf_link__fd(link));
> +	if (!ASSERT_GT(iter_fd, 0, "create_iter"))
> +		goto exit;
> +
> +	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
> +		;
> +	buf[15] = 0;
> +	ASSERT_EQ(strcmp(buf, "OK\n"), 0, "strcmp");
> +
> +	ASSERT_EQ(skel->bss->offset, get_uprobe_offset(trigger_func), "offset");
> +	if (one_proc)
> +		ASSERT_EQ(skel->bss->unique_tgid_cnt, 1, "unique_tgid_count");
> +	else
> +		ASSERT_GT(skel->bss->unique_tgid_cnt, 1, "unique_tgid_count");
> +
> +	close(iter_fd);
> +
> +exit:
> +	bpf_link__destroy(link);
> +}
> +
> +static void test_task_vma_offset(void)
> +{
> +	LIBBPF_OPTS(bpf_iter_attach_opts, opts);
> +	union bpf_iter_link_info linfo;
> +
> +	memset(&linfo, 0, sizeof(linfo));
> +	linfo.task.pid = getpid();
> +	opts.link_info = &linfo;
> +	opts.link_info_len = sizeof(linfo);
> +
> +	test_task_vma_offset_common(&opts, true);
> +
> +	linfo.task.pid = 0;
> +	linfo.task.tid = getpid();
> +	test_task_vma_offset_common(&opts, true);
> +
> +	test_task_vma_offset_common(NULL, false);
> +}
> +
>   void test_bpf_iter(void)
>   {
> +	ASSERT_OK(pthread_mutex_init(&do_nothing_mutex, NULL), "pthread_mutex_init");
> +
>   	if (test__start_subtest("btf_id_or_null"))
>   		test_btf_id_or_null();
>   	if (test__start_subtest("ipv6_route"))
> @@ -1335,8 +1569,12 @@ void test_bpf_iter(void)
>   		test_netlink();
>   	if (test__start_subtest("bpf_map"))
>   		test_bpf_map();
> -	if (test__start_subtest("task"))
> -		test_task();
> +	if (test__start_subtest("task_tid"))
> +		test_task_tid();
> +	if (test__start_subtest("task_pid"))
> +		test_task_pid();
> +	if (test__start_subtest("task_pidfd"))
> +		test_task_pidfd();
>   	if (test__start_subtest("task_sleepable"))
>   		test_task_sleepable();
>   	if (test__start_subtest("task_stack"))
> @@ -1397,4 +1635,6 @@ void test_bpf_iter(void)
>   		test_ksym_iter();
>   	if (test__start_subtest("bpf_sockmap_map_iter_fd"))
>   		test_bpf_sockmap_map_iter_fd();
> +	if (test__start_subtest("vma_offset"))
> +		test_task_vma_offset();
>   }
[...]
> diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c b/tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c
> new file mode 100644
> index 000000000000..03db36da5da8
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/bpf_iter_vma_offset.c
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
> +#include "bpf_iter.h"
> +#include <bpf/bpf_helpers.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +__u32 unique_tgid_cnt = 0;
> +uintptr_t address = 0;
> +uintptr_t offset = 0;
> +__u32 last_tgid = 0;
> +__u32 pid = 0;
> +__u32 page_shift = 0;

page_shift is not used here.

> +
> +SEC("iter/task_vma")
> +int get_vma_offset(struct bpf_iter__task_vma *ctx)
> +{
> +	struct vm_area_struct *vma = ctx->vma;
> +	struct seq_file *seq = ctx->meta->seq;
> +	struct task_struct *task = ctx->task;
> +
> +	if (task == NULL || vma == NULL)
> +		return 0;
> +
> +	if (last_tgid != task->tgid)
> +		unique_tgid_cnt++;
> +	last_tgid = task->tgid;
> +
> +	if (task->tgid != pid)
> +		return 0;
> +
> +	if (vma->vm_start <= address && vma->vm_end > address) {
> +		offset = address - vma->vm_start + (vma->vm_pgoff << 12);

'12' -> page_shift?

> +		BPF_SEQ_PRINTF(seq, "OK\n");
> +	}
> +	return 0;
> +}

  reply	other threads:[~2022-08-30 22:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-29 19:23 [PATCH bpf-next v8 0/5] Parameterize task iterators Kui-Feng Lee
2022-08-29 19:23 ` [PATCH bpf-next v8 1/5] bpf: " Kui-Feng Lee
2022-08-30 23:54   ` Yonghong Song
2022-08-31  0:35     ` Kui-Feng Lee
2022-08-31  2:37       ` Yonghong Song
2022-08-31  3:59         ` Yonghong Song
2022-08-31 16:39         ` Kui-Feng Lee
2022-08-29 19:23 ` [PATCH bpf-next v8 2/5] bpf: Handle bpf_link_info for the parameterized task BPF iterators Kui-Feng Lee
2022-08-29 19:23 ` [PATCH bpf-next v8 3/5] bpf: Handle show_fdinfo " Kui-Feng Lee
2022-08-30  0:56   ` Kui-Feng Lee
2022-08-30  0:58     ` Kui-Feng Lee
2022-08-30 21:17   ` Yonghong Song
2022-08-29 19:23 ` [PATCH bpf-next v8 4/5] selftests/bpf: Test " Kui-Feng Lee
2022-08-30 22:54   ` Yonghong Song [this message]
2022-08-29 19:23 ` [PATCH bpf-next v8 5/5] bpftool: Show parameters of BPF task iterators Kui-Feng Lee
2022-08-30 16:52   ` Quentin Monnet
2022-08-31  1:10     ` Kui-Feng Lee
2022-08-30 23:56   ` Yonghong Song
2022-08-31  1:11     ` Kui-Feng Lee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7e24948d-4e50-2058-ba6c-05dcee9b7426@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=kuifeng@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.