Re: [PATCH v4 bpf-next 2/2] selftests/bpf: Add selftest for fill_link_info

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Aug 6, 2023 at 3:58 AM Jiri Olsa <olsajiri@xxxxxxxxx> wrote:
>
> 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,

good point. will change it and the others.

>
>
> > +     );
> > +     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

will use them instead.

>
> > +     };
> > +     __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

will rename it.

-- 
Regards
Yafang





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux