public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Yafang Shao <laoar.shao@gmail.com>,
	ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
	andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
	kpsingh@kernel.org, sdf@google.com, haoluo@google.com,
	jolsa@kernel.org
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v4 bpf-next 2/2] selftests/bpf: Add selftest for fill_link_info
Date: Fri, 4 Aug 2023 09:21:55 -0700	[thread overview]
Message-ID: <5fd74eee-34ce-6dfc-57f1-897ce26fc3d4@linux.dev> (raw)
In-Reply-To: <20230804105732.3768-3-laoar.shao@gmail.com>



On 8/4/23 3:57 AM, Yafang Shao wrote:
> Add selftest for the fill_link_info of uprobe, kprobe and tracepoint.
> The result:
> 
>    $ tools/testing/selftests/bpf/test_progs --name=fill_link_info
>    #79/1    fill_link_info/kprobe_link_info:OK
>    #79/2    fill_link_info/kretprobe_link_info:OK
>    #79/3    fill_link_info/kprobe_fill_invalid_user_buff:OK
>    #79/4    fill_link_info/tracepoint_link_info:OK
>    #79/5    fill_link_info/uprobe_link_info:OK
>    #79/6    fill_link_info/uretprobe_link_info:OK
>    #79/7    fill_link_info/kprobe_multi_link_info:OK
>    #79/8    fill_link_info/kretprobe_multi_link_info:OK
>    #79/9    fill_link_info/kprobe_multi_ubuff:OK
>    #79      fill_link_info:OK
>    Summary: 1/9 PASSED, 0 SKIPPED, 0 FAILED
> 
> The test case for kprobe_multi won't be run on aarch64, as it is not
> supported.
> 
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>

Ack with a few nits below.

Acked-by: Yonghong Song <yonghong.song@linux.dev>

> ---
>   tools/testing/selftests/bpf/DENYLIST.aarch64       |   3 +
>   .../selftests/bpf/prog_tests/fill_link_info.c      | 337 +++++++++++++++++++++
>   .../selftests/bpf/progs/test_fill_link_info.c      |  42 +++
>   3 files changed, 382 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/fill_link_info.c
>   create mode 100644 tools/testing/selftests/bpf/progs/test_fill_link_info.c
> 
> diff --git a/tools/testing/selftests/bpf/DENYLIST.aarch64 b/tools/testing/selftests/bpf/DENYLIST.aarch64
> index 3b61e8b..b2f46b6 100644
> --- a/tools/testing/selftests/bpf/DENYLIST.aarch64
> +++ b/tools/testing/selftests/bpf/DENYLIST.aarch64
> @@ -12,3 +12,6 @@ kprobe_multi_test/skel_api                       # libbpf: failed to load BPF sk
>   module_attach                                    # prog 'kprobe_multi': failed to auto-attach: -95
>   fentry_test/fentry_many_args                     # fentry_many_args:FAIL:fentry_many_args_attach unexpected error: -524
>   fexit_test/fexit_many_args                       # fexit_many_args:FAIL:fexit_many_args_attach unexpected error: -524
> +fill_link_info/kprobe_multi_link_info            # bpf_program__attach_kprobe_multi_opts unexpected error: -95
> +fill_link_info/kretprobe_multi_link_info         # bpf_program__attach_kprobe_multi_opts unexpected error: -95
> +fill_link_info/kprobe_multi_ubuff                # bpf_program__attach_kprobe_multi_opts unexpected error: -95
> diff --git a/tools/testing/selftests/bpf/prog_tests/fill_link_info.c b/tools/testing/selftests/bpf/prog_tests/fill_link_info.c
> new file mode 100644
> index 0000000..001a8b5
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/fill_link_info.c
> @@ -0,0 +1,337 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */
> +
> +#include <string.h>
> +#include <linux/bpf.h>
> +#include <linux/limits.h>
> +#include <test_progs.h>
> +#include "trace_helpers.h"
> +#include "test_fill_link_info.skel.h"
> +
> +#define TP_CAT "sched"
> +#define TP_NAME "sched_switch"
> +#define KPROBE_FUNC "tcp_rcv_established"
> +#define UPROBE_FILE "/proc/self/exe"
> +#define KMULTI_CNT (4)
> +
> +/* uprobe attach point */
> +static noinline void uprobe_func(void)
> +{
> +	asm volatile ("");
> +}
> +
> +static int verify_perf_link_info(int fd, enum bpf_perf_event_type type, long addr,
> +				 ssize_t offset, ssize_t entry_offset)
> +{
> +	struct bpf_link_info info;
> +	__u32 len = sizeof(info);
> +	char buf[PATH_MAX];
> +	int err = 0;
> +
> +	memset(&info, 0, sizeof(info));
> +	buf[0] = '\0';
> +
> +again:
> +	err = bpf_link_get_info_by_fd(fd, &info, &len);
> +	if (!ASSERT_OK(err, "get_link_info"))
> +		return -1;
> +
> +	if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_PERF_EVENT, "link_type"))
> +		return -1;
> +	if (!ASSERT_EQ(info.perf_event.type, type, "perf_type_match"))
> +		return -1;
> +
> +	switch (info.perf_event.type) {
> +	case BPF_PERF_EVENT_KPROBE:
> +	case BPF_PERF_EVENT_KRETPROBE:
> +		ASSERT_EQ(info.perf_event.kprobe.offset, offset, "kprobe_offset");
> +
> +		/* In case kernel.kptr_restrict is not permitted or MAX_SYMS is reached */
> +		if (addr)
> +			ASSERT_EQ(info.perf_event.kprobe.addr, addr + entry_offset,
> +				  "kprobe_addr");
> +
> +		if (!info.perf_event.kprobe.func_name) {
> +			ASSERT_EQ(info.perf_event.kprobe.name_len, 0, "name_len");
> +			info.perf_event.kprobe.func_name = ptr_to_u64(&buf);
> +			info.perf_event.kprobe.name_len = sizeof(buf);
> +			goto again;
> +		}
> +
> +		err = strncmp(u64_to_ptr(info.perf_event.kprobe.func_name), KPROBE_FUNC,
> +			      strlen(KPROBE_FUNC));
> +		ASSERT_EQ(err, 0, "cmp_kprobe_func_name");
> +		break;
> +	case BPF_PERF_EVENT_TRACEPOINT:
> +		if (!info.perf_event.tracepoint.tp_name) {
> +			ASSERT_EQ(info.perf_event.tracepoint.name_len, 0, "name_len");
> +			info.perf_event.tracepoint.tp_name = ptr_to_u64(&buf);
> +			info.perf_event.tracepoint.name_len = sizeof(buf);
> +			goto again;
> +		}
> +
> +		err = strncmp(u64_to_ptr(info.perf_event.tracepoint.tp_name), TP_NAME,
> +			      strlen(TP_NAME));
> +		ASSERT_EQ(err, 0, "cmp_tp_name");
> +		break;
> +	case BPF_PERF_EVENT_UPROBE:
> +	case BPF_PERF_EVENT_URETPROBE:
> +		ASSERT_EQ(info.perf_event.uprobe.offset, offset, "uprobe_offset");
> +
> +		if (!info.perf_event.uprobe.file_name) {
> +			ASSERT_EQ(info.perf_event.uprobe.name_len, 0, "name_len");
> +			info.perf_event.uprobe.file_name = ptr_to_u64(&buf);
> +			info.perf_event.uprobe.name_len = sizeof(buf);
> +			goto again;
> +		}
> +
> +		err = strncmp(u64_to_ptr(info.perf_event.uprobe.file_name), UPROBE_FILE,
> +			      strlen(UPROBE_FILE));
> +			ASSERT_EQ(err, 0, "cmp_file_name");
> +		break;
> +	default:

Is the 'default' case possible? Probably not, right? Then
let us add
		err = -1;
to indicate this path is not possible.

> +		break;
> +	}
> +	return err;
> +}
> +
[...]
> +
> +static void test_kprobe_fill_link_info(struct test_fill_link_info *skel,
> +				       enum bpf_perf_event_type type,
> +				       bool retprobe, bool invalid)
> +{
> +	DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
> +		.attach_mode = PROBE_ATTACH_MODE_LINK,
> +		.retprobe = retprobe,
> +	);
> +	ssize_t offset = 0, entry_offset = 0;

Remove 'offset = 0'.

> +	int link_fd, err;
> +	long addr;
> +
> +	skel->links.kprobe_run = bpf_program__attach_kprobe_opts(skel->progs.kprobe_run,
> +								 KPROBE_FUNC, &opts);
> +	if (!ASSERT_OK_PTR(skel->links.kprobe_run, "attach_kprobe"))
> +		return;
> +
> +	link_fd = bpf_link__fd(skel->links.kprobe_run);
> +	addr = ksym_get_addr(KPROBE_FUNC);
> +	if (!invalid) {
> +		/* See also arch_adjust_kprobe_addr(). */
> +		if (skel->kconfig->CONFIG_X86_KERNEL_IBT)
> +			entry_offset = 4;
> +		err = verify_perf_link_info(link_fd, type, addr, offset, entry_offset);

Replease 'offset' with '0'.

> +		ASSERT_OK(err, "verify_perf_link_info");
> +	} else {
> +		kprobe_fill_invalid_user_buffer(link_fd);
> +	}
> +	bpf_link__detach(skel->links.kprobe_run);
> +}
> +
[...]
> +
> +static void test_kprobe_multi_fill_link_info(struct test_fill_link_info *skel,
> +					     bool retprobe, bool buffer)
> +{
> +	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
> +	const char *syms[KMULTI_CNT] = {
> +		"schedule_timeout_interruptible",
> +		"schedule_timeout_uninterruptible",
> +		"schedule_timeout_idle",
> +		"schedule_timeout_killable",
> +	};
> +	__u64 addrs[KMULTI_CNT];
> +	int link_fd, i, err = 0;

'err = 0' => 'err'.

> +
> +	qsort(syms, KMULTI_CNT, sizeof(syms[0]), symbols_cmp_r);
> +	opts.syms = syms;
> +	opts.cnt = KMULTI_CNT;
> +	opts.retprobe = retprobe;
> +	skel->links.kmulti_run = bpf_program__attach_kprobe_multi_opts(skel->progs.kmulti_run,
> +								       NULL, &opts);
> +	if (!ASSERT_OK_PTR(skel->links.kmulti_run, "attach_kprobe_multi"))
> +		return;
> +
> +	link_fd = bpf_link__fd(skel->links.kmulti_run);
> +	for (i = 0; i < KMULTI_CNT; i++)
> +		addrs[i] = ksym_get_addr(syms[i]);
> +
> +	if (!buffer)
> +		err = verify_kmulti_link_info(link_fd, addrs, retprobe);
> +	else
> +		verify_kmulti_user_buffer(link_fd, addrs);
> +	ASSERT_OK(err, "verify_kmulti_link_info");

if (!buffer) {
	err = verify_kmulti_link_info(link_fd, addrs, retprobe);
	ASSERT_OK(err, "verify_kmulti_link_info");
} else {
	verify_kmulti_user_buffer(link_fd, addrs);
}

> +	bpf_link__detach(skel->links.kmulti_run);
> +}
> +
[...]

  reply	other threads:[~2023-08-04 16:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-04 10:57 [PATCH v4 bpf-next 0/2] bpf: Fix fill_link_info and add selftest Yafang Shao
2023-08-04 10:57 ` [PATCH v4 bpf-next 1/2] bpf: Fix uninitialized symbol in bpf_perf_link_fill_kprobe() Yafang Shao
2023-08-05 19:58   ` Jiri Olsa
2023-08-04 10:57 ` [PATCH v4 bpf-next 2/2] selftests/bpf: Add selftest for fill_link_info Yafang Shao
2023-08-04 16:21   ` Yonghong Song [this message]
2023-08-05  2:25     ` Yafang Shao
2023-08-05 19:58   ` Jiri Olsa
2023-08-06 14:33     ` Yafang Shao

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=5fd74eee-34ce-6dfc-57f1-897ce26fc3d4@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=laoar.shao@gmail.com \
    --cc=martin.lau@linux.dev \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox