From: Yonghong Song <yonghong.song@linux.dev>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Eduard Zingerman <eddyz87@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Kernel Team <kernel-team@fb.com>,
Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next 2/3] bpf: Remove 'may_goto 0' instruction
Date: Thu, 16 Jan 2025 19:43:10 -0800 [thread overview]
Message-ID: <ea125a8d-9804-4dd9-983b-1e741a1a4f1d@linux.dev> (raw)
In-Reply-To: <CAADnVQ+4ZJNdBU0D8kwe75VOp5x9xLrueEQk4hD1RDR_CJ63Fg@mail.gmail.com>
On 1/16/25 5:45 PM, Alexei Starovoitov wrote:
> On Thu, Jan 16, 2025 at 11:42 AM Eduard Zingerman <eddyz87@gmail.com> wrote:
>> On Wed, 2025-01-15 at 21:51 -0800, Yonghong Song wrote:
>>> Since 'may_goto 0' insns are actually no-op, let us remove them.
>>> Otherwise, verifier will generate code like
>>> /* r10 - 8 stores the implicit loop count */
>>> r11 = *(u64 *)(r10 -8)
>>> if r11 == 0x0 goto pc+2
>>> r11 -= 1
>>> *(u64 *)(r10 -8) = r11
>>>
>>> which is the pure overhead.
>>>
>>> The following code patterns (from the previous commit) are also
>>> handled:
>>> may_goto 2
>>> may_goto 1
>>> may_goto 0
>>>
>>> With this commit, the above three 'may_goto' insns are all
>>> eliminated.
>>>
>>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>>> ---
>> Technically this is a side-effect, it subtracts 1 from total loop budget.
>> An alternative transformation might be:
>>
>> r11 = *(u64 *)(r10 -8)
>> if r11 == 0x0 goto pc+2
>> r11 -= 3 <---------------- note 3 here
>> *(u64 *)(r10 -8) = r11
>>
>> On the other hand, it looks like there is no way to trick verifier
>> into an infinite loop by removing these statements, so this should be
>> safe modulo exceeding the 8M iterations budget.
>>
>> Acked-by: Eduard Zingerman <eddyz87@gmail.com>
>>
>>> kernel/bpf/verifier.c | 36 ++++++++++++++++++++++++++++++++++++
>>> 1 file changed, 36 insertions(+)
>>>
>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>>> index edf3cc42a220..72b474bfba2d 100644
>>> --- a/kernel/bpf/verifier.c
>>> +++ b/kernel/bpf/verifier.c
>>> @@ -20133,6 +20133,40 @@ static int opt_remove_nops(struct bpf_verifier_env *env)
>>> return 0;
>>> }
>>>
>>> +static int opt_remove_useless_may_gotos(struct bpf_verifier_env *env)
>>> +{
>>> + struct bpf_insn *insn = env->prog->insnsi;
>>> + int i, j, err, last_may_goto, removed_cnt;
>>> + int insn_cnt = env->prog->len;
>>> +
>>> + for (i = 0; i < insn_cnt; i++) {
>>> + if (!is_may_goto_insn(&insn[i]))
>>> + continue;
>>> +
>>> + for (j = i + 1; j < insn_cnt; j++) {
>>> + if (!is_may_goto_insn(&insn[j]))
>>> + break;
>>> + }
>>> +
>>> + last_may_goto = --j;
>>> + removed_cnt = 0;
>>> + while (j >= i) {
>>> + if (insn[j].off == 0) {
>>> + err = verifier_remove_insns(env, j, 1);
>> Nit: given how ineffective the verifier_remove_insns() is I'd count
>> the number of matching may_goto's and removed them using one call
>> to verifier_remove_insns().
> True,
> but more generally I don't see why may_goto needs special treatment.
> opt_remove_nops() should handle both.
>
> if (memcmp(&insn[i], &ja, sizeof(ja)) &&
> memcmp(&insn[i], &may_goto0, sizeof(ja)))
> continue;
>
> will almost work.
> In the sequence of may_goto +2, +1, +0
> only the last one will be removed, I think,
> but opt_remove_nops() can be tweaked to achieve that as well.
> - i--;
> + i -= 2;
>
> will do ?
Okay. Let me give a try.
>
> pw-bot: cr
next prev parent reply other threads:[~2025-01-17 3:43 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-16 5:51 [PATCH bpf-next 0/3] bpf: Allow 'may_goto 0' instruction Yonghong Song
2025-01-16 5:51 ` [PATCH bpf-next 1/3] " Yonghong Song
2025-01-16 19:23 ` Eduard Zingerman
2025-01-16 5:51 ` [PATCH bpf-next 2/3] bpf: Remove " Yonghong Song
2025-01-16 19:42 ` Eduard Zingerman
2025-01-17 1:45 ` Alexei Starovoitov
2025-01-17 3:43 ` Yonghong Song [this message]
2025-01-16 5:51 ` [PATCH bpf-next 3/3] selftests/bpf: Add some tests related to 'may_goto 0' insns Yonghong Song
2025-01-16 19:49 ` Eduard Zingerman
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=ea125a8d-9804-4dd9-983b-1e741a1a4f1d@linux.dev \
--to=yonghong.song@linux.dev \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@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