Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpf: Allow 'may_goto 0' instruction #8366

Closed

Conversation

kernel-patches-daemon-bpf[bot]
Copy link

Pull request for series with
subject: bpf: Allow 'may_goto 0' instruction
version: 1
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 87c5441
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 87c5441
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 87c5441
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 87c5441
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 87c5441
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: a8d1c48
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 556a399
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 556a399
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=925938
version: 1

@kernel-patches-daemon-bpf
Copy link
Author

At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=925938 expired. Closing PR.

@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 01f3ce5
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=926682
version: 2

Yonghong Song added 3 commits January 20, 2025 07:24
Commit 011832b ("bpf: Introduce may_goto instruction") added support
for may_goto insn. The 'may_goto 0' insn is disallowed since the insn is
equivalent to a nop as both branch will go to the next insn.

But it is possible that compiler transformation may generate 'may_goto 0'
insn. Emil Tsalapatis from Meta reported such a case which caused
verification failure. For example, for the following code,
   int i, tmp[3];
   for (i = 0; i < 3 && can_loop; i++)
     tmp[i] = 0;
   ...

clang 20 may generate code like
   may_goto 2;
   may_goto 1;
   may_goto 0;
   r1 = 0; /* tmp[0] = 0; */
   r2 = 0; /* tmp[1] = 0; */
   r3 = 0; /* tmp[2] = 0; */

Let us permit 'may_goto 0' insn to avoid verification failure for codes
like the above.

Reported-by: Emil Tsalapatis <[email protected]>
Acked-by: Eduard Zingerman <[email protected]>
Signed-off-by: Yonghong Song <[email protected]>
Acked-by: Daniel Borkmann <[email protected]>
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 <[email protected]>
Add both asm-based and C-based tests which have 'may_goto 0' insns.

For the following code in C-based test,
   int i, tmp[3];
   for (i = 0; i < 3 && can_loop; i++)
       tmp[i] = 0;

The clang compiler (clang 19 and 20) generates
   may_goto 2
   may_goto 1
   may_goto 0
   r1 = 0
   r2 = 0
   r3 = 0

The above asm codes are due to llvm pass SROAPass. This ensures the
successful verification since tmp[0-2] are initialized.  Otherwise,
the code without SROAPass like
   may_goto 5
   r1 = 0
   may_goto 3
   r2 = 0
   may_goto 1
   r3 = 0
will have verification failure.

Although from the source code C-based test should have verification
failure, clang compiler optimization generates code with successful
verification. If gcc generates different asm codes than clang, the
following code can be used for gcc:
   int i, tmp[3];
   for (i = 0; i < 3; i++)
       tmp[i] = 0;

Acked-by: Eduard Zingerman <[email protected]>
Signed-off-by: Yonghong Song <[email protected]>
@kernel-patches-daemon-bpf
Copy link
Author

Upstream branch: 01f3ce5
series: https://patchwork.kernel.org/project/netdevbpf/list/?series=926682
version: 2

@kernel-patches-daemon-bpf
Copy link
Author

At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=926682 expired. Closing PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants