From: Yonghong Song <yhs@fb.com>
To: Song Liu <songliubraving@fb.com>
Cc: bpf <bpf@vger.kernel.org>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"ast@kernel.org" <ast@kernel.org>,
"daniel@iogearbox.net" <daniel@iogearbox.net>,
"andrii@kernel.org" <andrii@kernel.org>,
Kernel Team <Kernel-team@fb.com>,
"kpsingh@kernel.org" <kpsingh@kernel.org>
Subject: Re: [PATCH v2 bpf-next 2/2] selftests/bpf: add tests for bpf_find_vma
Date: Thu, 4 Nov 2021 10:24:38 -0700 [thread overview]
Message-ID: <bf627fc3-7a89-2184-0f6e-e2bd8016759e@fb.com> (raw)
In-Reply-To: <10CD7450-873D-4136-819B-A1C8F6473E4D@fb.com>
On 11/4/21 10:17 AM, Song Liu wrote:
>
>
>> On Nov 4, 2021, at 10:07 AM, Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 11/4/21 12:00 AM, Song Liu wrote:
>>> Add tests for bpf_find_vma in perf_event program and kprobe program. The
>>> perf_event program is triggered from NMI context, so the second call of
>>> bpf_find_vma() will return -EBUSY (irq_work busy). The kprobe program,
>>> on the other hand, does not have this constraint.
>>> Also add test for illegal writes to task or vma from the callback
>>> function. The verifier should reject both cases.
>>> Signed-off-by: Song Liu <songliubraving@fb.com>
>
> [...]
>
>>> +static void test_find_vma_pe(struct find_vma *skel)
>>> +{
>>> + struct bpf_link *link = NULL;
>>> + volatile int j = 0;
>>> + int pfd = -1, i;
>>> +
>>> + pfd = open_pe();
>>> + if (pfd < 0) {
>>> + if (pfd == -ENOENT || pfd == -EOPNOTSUPP) {
>>> + printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__);
>>> + test__skip();
>>> + }
>>> + if (!ASSERT_GE(pfd, 0, "perf_event_open"))
>>> + goto cleanup;
>>> + }
>>> +
>>> + link = bpf_program__attach_perf_event(skel->progs.handle_pe, pfd);
>>> + if (!ASSERT_OK_PTR(link, "attach_perf_event"))
>>> + goto cleanup;
>>> +
>>> + for (i = 0; i < 1000000; ++i)
>>> + ++j;
>>
>> Does this really work? Compiler could do
>> j += 1000000;
>
> I think compiler won't do it with volatile j?
Ya. volatile j should be fine.
>
>>
>>> +
>>> + test_and_reset_skel(skel, -EBUSY /* in nmi, irq_work is busy */);
>>> +cleanup:
>>> + bpf_link__destroy(link);
>>> + close(pfd);
>>> + /* caller will clean up skel */
>>
>> Above comment is not needed. It should be clear from the code.
>>
[...]
>>
>>> +int handle_getpid(void)
>>> +{
>>> + struct task_struct *task = bpf_get_current_task_btf();
>>> + struct callback_ctx data = {0};
>>> +
>>> + if (task->pid != target_pid)
>>> + return 0;
>>> +
>>> + find_addr_ret = bpf_find_vma(task, addr, check_vma, &data, 0);
>>> +
>>> + /* this should return -ENOENT */
>>> + find_zero_ret = bpf_find_vma(task, 0, check_vma, &data, 0);
>>> + return 0;
>>> +}
>>> +
>>> +SEC("perf_event")
>>> +int handle_pe(void)
>>> +{
>>> + struct task_struct *task = bpf_get_current_task_btf();
>>> + struct callback_ctx data = {0};
>>> +
>>> + if (task->pid != target_pid)
>>> + return 0;
>>
>> This is tricky. How do we guarantee task->pid == target_pid hit?
>> This probably mostly okay in serial running mode. But it may
>> become more challenging if test_progs is running in parallel mode?
>
> This is on a per task perf_event, so it shouldn't hit other tasks.
I see. we have the following parameters for perf_event open.
pid == 0 and cpu == -1
This measures the calling process/thread on any CPU.
So yes, we are fine then.
>
>>
>>> +
>>> + find_addr_ret = bpf_find_vma(task, addr, check_vma, &data, 0);
>>> +
>>> + /* In NMI, this should return -EBUSY, as the previous call is using
>>> + * the irq_work.
>>> + */
>>> + find_zero_ret = bpf_find_vma(task, 0, check_vma, &data, 0);
>>> + return 0;
>>> +}
[...]
next prev parent reply other threads:[~2021-11-04 17:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-04 7:00 [PATCH v2 bpf-next 0/2] introduce bpf_find_vma Song Liu
2021-11-04 7:00 ` [PATCH v2 bpf-next 1/2] bpf: introduce helper bpf_find_vma Song Liu
2021-11-04 16:46 ` Yonghong Song
2021-11-04 20:37 ` kernel test robot
2021-11-04 21:11 ` Song Liu
2021-11-04 7:00 ` [PATCH v2 bpf-next 2/2] selftests/bpf: add tests for bpf_find_vma Song Liu
2021-11-04 16:18 ` Song Liu
2021-11-04 17:07 ` Yonghong Song
2021-11-04 17:17 ` Song Liu
2021-11-04 17:24 ` Yonghong Song [this message]
2021-11-04 17:36 ` Song Liu
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=bf627fc3-7a89-2184-0f6e-e2bd8016759e@fb.com \
--to=yhs@fb.com \
--cc=Kernel-team@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kpsingh@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox