Skip to content

asm: add support for the may_goto insn and JCOND opcode#1898

Open
mtardy wants to merge 1 commit intocilium:mainfrom
mtardy:pr/mtardy/add-bpf-jcond-insn
Open

asm: add support for the may_goto insn and JCOND opcode#1898
mtardy wants to merge 1 commit intocilium:mainfrom
mtardy:pr/mtardy/add-bpf-jcond-insn

Conversation

@mtardy
Copy link
Copy Markdown
Member

@mtardy mtardy commented Nov 7, 2025

The may_goto instruction is a bit special and relatively new, from March 2024, added by 011832b97b31 ("bpf: Introduce may_goto instruction")1. It uses the BPF_JCOND opcode 0xe0 with the source register being BPF_MAY_GOTO = 0. Note that this instruction is not documented in the latest instruction set kernel documentation2.

As I understand, the goal of this instruction is to be injected in loops (also potentially by LLVM) to offload the verifier responsability of loop verification to a runtime check. The runtime check being decrementing a counter so far.

I realized when writing this PR that I had never used the asm package of c/ebpf and that I didn't have the time right now specifically to dive into this. So I'll take that later, or if you want to guide me on what would be the best strategy for you for this "a bit weird" instruction.

Footnotes

  1. https://lore.kernel.org/bpf/20240306031929.42666-2-alexei.starovoitov@gmail.com.

  2. https://www.kernel.org/doc/html/v6.17/bpf/standardization/instruction-set.html#jump-instructions.

@mtardy mtardy force-pushed the pr/mtardy/add-bpf-jcond-insn branch from 8c9c939 to 1605af2 Compare November 7, 2025 11:31
@mtardy
Copy link
Copy Markdown
Member Author

mtardy commented Nov 7, 2025

cc @dylandreimerink

@ti-mo
Copy link
Copy Markdown
Contributor

ti-mo commented Apr 1, 2026

@mtardy @dylandreimerink What's required to get this merged? Is there a compiler that emits this kind of instruction yet?

@dylandreimerink
Copy link
Copy Markdown
Member

I think we just have to double check that tests have been updated to our liking. Any clang version can emit the instruction with some inline asm magic. For some reason the macros have not been added to libbpf and are only in the in-kernel experimental header https://elixir.bootlin.com/linux/v6.19.10/source/tools/testing/selftests/bpf/bpf_experimental.h#L435

But it can already be used. Primary example would be sched_ext, which has its own library with in-BPF datastructure implementations that use this any time they loop. https://github.com/sched-ext/scx/blob/d4dee55a1bb07db6ae74d7ef0cd2bd4338e6b55f/lib/alloc/stack.bpf.c#L155
https://github.com/sched-ext/scx/blob/d4dee55a1bb07db6ae74d7ef0cd2bd4338e6b55f/scheds/include/bpf_arena_common.bpf.h#L97

@dylandreimerink dylandreimerink force-pushed the pr/mtardy/add-bpf-jcond-insn branch from 1605af2 to a58beaa Compare April 7, 2026 14:41
The `may_goto` instruction is a bit special and relatively new, from
March 2024, added by 011832b97b31 ("bpf: Introduce may_goto
instruction")[^1]. It uses the `BPF_JCOND` opcode `0xe0` with the source
register being `BPF_MAY_GOTO = 0`. Note that this instruction is not
documented in the latest instruction set kernel documentation[^2].

As I understand, the goal of this instruction is to be injected in loops
(also potentially by LLVM) to offload the verifier responsibility of
loop verification to a runtime check. The runtime check being
decrementing a counter so far.

[^1]: https://lore.kernel.org/bpf/20240306031929.42666-2-alexei.starovoitov@gmail.com
[^2]: https://www.kernel.org/doc/html/v6.17/bpf/standardization/instruction-set.html#jump-instructions

Co-Authored-By: Mahe Tardy <mahe.tardy@gmail.com>
Signed-off-by: Dylan Reimerink <dylan.reimerink@isovalent.com>
@dylandreimerink dylandreimerink force-pushed the pr/mtardy/add-bpf-jcond-insn branch from a58beaa to 6562472 Compare April 7, 2026 14:42
@dylandreimerink dylandreimerink marked this pull request as ready for review April 7, 2026 14:54
@dylandreimerink dylandreimerink requested a review from a team as a code owner April 7, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants