All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Yafang Shao <laoar.shao@gmail.com>
Cc: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
	andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
	yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com, bpf@vger.kernel.org
Subject: Re: [PATCH v4 bpf-next 2/2] selftests/bpf: Add selftest for fill_link_info
Date: Sat, 5 Aug 2023 21:58:51 +0200	[thread overview]
Message-ID: <ZM6p+++fSnKrEYM5@krava> (raw)
In-Reply-To: <20230804105732.3768-3-laoar.shao@gmail.com>

On Fri, Aug 04, 2023 at 10:57:32AM +0000, Yafang Shao wrote:

SNIP

> +
> +static void kprobe_fill_invalid_user_buffer(int fd)
> +{
> +	struct bpf_link_info info;
> +	__u32 len = sizeof(info);
> +	int err;
> +
> +	memset(&info, 0, sizeof(info));
> +
> +	info.perf_event.kprobe.func_name = 0x1; /* invalid address */
> +	err = bpf_link_get_info_by_fd(fd, &info, &len);
> +	ASSERT_EQ(err, -EINVAL, "invalid_buff_and_len");
> +
> +	info.perf_event.kprobe.name_len = 64;
> +	err = bpf_link_get_info_by_fd(fd, &info, &len);
> +	ASSERT_EQ(err, -EFAULT, "invalid_buff");
> +
> +	info.perf_event.kprobe.func_name = 0;
> +	err = bpf_link_get_info_by_fd(fd, &info, &len);
> +	ASSERT_EQ(err, -EINVAL, "invalid_len");
> +
> +	ASSERT_EQ(info.perf_event.kprobe.addr, 0, "func_addr");
> +	ASSERT_EQ(info.perf_event.kprobe.offset, 0, "func_offset");
> +	ASSERT_EQ(info.perf_event.type, 0, "type");
> +}
> +
> +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,

you could got rid of the retprobe argument and just do

		.retprobe = retprobe == BPF_PERF_EVENT_KRETPROBE,


> +	);
> +	ssize_t offset = 0, entry_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);
> +		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_tp_fill_link_info(struct test_fill_link_info *skel)
> +{
> +	int link_fd, err;
> +
> +	skel->links.tp_run = bpf_program__attach_tracepoint(skel->progs.tp_run, TP_CAT, TP_NAME);
> +	if (!ASSERT_OK_PTR(skel->links.tp_run, "attach_tp"))
> +		return;
> +
> +	link_fd = bpf_link__fd(skel->links.tp_run);
> +	err = verify_perf_link_info(link_fd, BPF_PERF_EVENT_TRACEPOINT, 0, 0, 0);
> +	ASSERT_OK(err, "verify_perf_link_info");
> +	bpf_link__detach(skel->links.tp_run);
> +}
> +
> +static void test_uprobe_fill_link_info(struct test_fill_link_info *skel,
> +				       enum bpf_perf_event_type type, ssize_t offset,
> +				       bool retprobe)
> +{
> +	int link_fd, err;
> +
> +	skel->links.uprobe_run = bpf_program__attach_uprobe(skel->progs.uprobe_run, retprobe,
> +							    0, /* self pid */
> +							    UPROBE_FILE, offset);

same here with 'type == BPF_PERF_EVENT_URETPROBE'


> +	if (!ASSERT_OK_PTR(skel->links.uprobe_run, "attach_uprobe"))
> +		return;
> +
> +	link_fd = bpf_link__fd(skel->links.uprobe_run);
> +	err = verify_perf_link_info(link_fd, type, 0, offset, 0);
> +	ASSERT_OK(err, "verify_perf_link_info");
> +	bpf_link__detach(skel->links.uprobe_run);
> +}
> +

SNIP

> +
> +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",

nit, might be better to use some of the bpf_fentry_test[1-9] functions,
also for KPROBE_FUNC

> +	};
> +	__u64 addrs[KMULTI_CNT];
> +	int link_fd, i, err = 0;
> +
> +	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);

verify_kmulti_user_buffer is actually what you call 'invalid' in other
tests right? seems better to keep it in here unless I miss something

thanks,
jirka

> +	ASSERT_OK(err, "verify_kmulti_link_info");
> +	bpf_link__detach(skel->links.kmulti_run);
> +}
> +

SNIP

  parent reply	other threads:[~2023-08-05 19:58 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
2023-08-05  2:25     ` Yafang Shao
2023-08-05 19:58   ` Jiri Olsa [this message]
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=ZM6p+++fSnKrEYM5@krava \
    --to=olsajiri@gmail.com \
    --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=kpsingh@kernel.org \
    --cc=laoar.shao@gmail.com \
    --cc=martin.lau@linux.dev \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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.