-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bpf-allow-may_goto-0-instruction'
Yonghong Song says: ==================== Emil Tsalapatis from Meta reported such a case where 'may_goto 0' insn is generated by clang-19 compiler and this caused verification failure since 'may_goto 0' is rejected by verifier. In fact, 'may_goto 0' insn is actually a no-op and it won't hurt verification. The only side effect is that the verifier will convert the insn to a sequence of codes like /* r10 - 8 stores the implicit loop count */ r11 = *(u64 *)(r10 -8) if r11 == 0x0 goto pc+2 r11 -= 1 *(u64 *)(r10 -8) = r11 With this patch set 'may_goto 0' insns are allowed in verification which also removes those insns. Changelogs: v1 -> v2: - Instead of a separate function, removing 'may_goto 0' in existing func opt_remove_nops(). ==================== Link: https://patch.msgid.link/20250118192019.2123689-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
- Loading branch information
Showing
4 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ | ||
|
||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include "../../../include/linux/filter.h" | ||
#include "bpf_misc.h" | ||
|
||
SEC("raw_tp") | ||
__description("may_goto 0") | ||
__arch_x86_64 | ||
__xlated("0: r0 = 1") | ||
__xlated("1: exit") | ||
__success | ||
__naked void may_goto_simple(void) | ||
{ | ||
asm volatile ( | ||
".8byte %[may_goto];" | ||
"r0 = 1;" | ||
".8byte %[may_goto];" | ||
"exit;" | ||
: | ||
: __imm_insn(may_goto, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0)) | ||
: __clobber_all); | ||
} | ||
|
||
SEC("raw_tp") | ||
__description("batch 2 of may_goto 0") | ||
__arch_x86_64 | ||
__xlated("0: r0 = 1") | ||
__xlated("1: exit") | ||
__success | ||
__naked void may_goto_batch_0(void) | ||
{ | ||
asm volatile ( | ||
".8byte %[may_goto1];" | ||
".8byte %[may_goto1];" | ||
"r0 = 1;" | ||
".8byte %[may_goto1];" | ||
".8byte %[may_goto1];" | ||
"exit;" | ||
: | ||
: __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0)) | ||
: __clobber_all); | ||
} | ||
|
||
SEC("raw_tp") | ||
__description("may_goto batch with offsets 2/1/0") | ||
__arch_x86_64 | ||
__xlated("0: r0 = 1") | ||
__xlated("1: exit") | ||
__success | ||
__naked void may_goto_batch_1(void) | ||
{ | ||
asm volatile ( | ||
".8byte %[may_goto1];" | ||
".8byte %[may_goto2];" | ||
".8byte %[may_goto3];" | ||
"r0 = 1;" | ||
".8byte %[may_goto1];" | ||
".8byte %[may_goto2];" | ||
".8byte %[may_goto3];" | ||
"exit;" | ||
: | ||
: __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 2 /* offset */, 0)), | ||
__imm_insn(may_goto2, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 1 /* offset */, 0)), | ||
__imm_insn(may_goto3, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0)) | ||
: __clobber_all); | ||
} | ||
|
||
SEC("raw_tp") | ||
__description("may_goto batch with offsets 2/0") | ||
__arch_x86_64 | ||
__xlated("0: *(u64 *)(r10 -8) = 8388608") | ||
__xlated("1: r11 = *(u64 *)(r10 -8)") | ||
__xlated("2: if r11 == 0x0 goto pc+3") | ||
__xlated("3: r11 -= 1") | ||
__xlated("4: *(u64 *)(r10 -8) = r11") | ||
__xlated("5: r0 = 1") | ||
__xlated("6: r0 = 2") | ||
__xlated("7: exit") | ||
__success | ||
__naked void may_goto_batch_2(void) | ||
{ | ||
asm volatile ( | ||
".8byte %[may_goto1];" | ||
".8byte %[may_goto3];" | ||
"r0 = 1;" | ||
"r0 = 2;" | ||
"exit;" | ||
: | ||
: __imm_insn(may_goto1, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 2 /* offset */, 0)), | ||
__imm_insn(may_goto3, BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0 /* offset */, 0)) | ||
: __clobber_all); | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ | ||
|
||
#include "bpf_misc.h" | ||
#include "bpf_experimental.h" | ||
|
||
int gvar; | ||
|
||
SEC("raw_tp") | ||
__description("C code with may_goto 0") | ||
__success | ||
int may_goto_c_code(void) | ||
{ | ||
int i, tmp[3]; | ||
|
||
for (i = 0; i < 3 && can_loop; i++) | ||
tmp[i] = 0; | ||
|
||
for (i = 0; i < 3 && can_loop; i++) | ||
tmp[i] = gvar - i; | ||
|
||
for (i = 0; i < 3 && can_loop; i++) | ||
gvar += tmp[i]; | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |