Skip to content

Commit 3a6984e

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf,x64: implement jump padding in jit'
Gary Lin says: ==================== This patch series implements jump padding to x64 jit to cover some corner cases that used to consume more than 20 jit passes and caused failure. v4: - Add the detailed comments about the possible padding bytes - Add the second test case which triggers jmp_cond padding and imm32 nop jmp padding. - Add the new test case as another subprog v3: - Copy the instructions of prologue separately or the size calculation of the first BPF instruction would include the prologue. - Replace WARN_ONCE() with pr_err() and EFAULT - Use MAX_PASSES in the for loop condition check - Remove the "padded" flag from x64_jit_data. For the extra pass of subprogs, padding is always enabled since it won't hurt the images that converge without padding. v2: - Simplify the sample code in the commit description and provide the jit code - Check the expected padding bytes with WARN_ONCE - Move the 'padded' flag to 'struct x64_jit_data' - Remove the EXPECTED_FAIL flag from bpf_fill_maxinsns11() in test_bpf - Add 2 verifier tests ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 95204c9 + eb33aa2 commit 3a6984e

File tree

4 files changed

+209
-34
lines changed

4 files changed

+209
-34
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 112 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,31 @@ static void detect_reg_usage(struct bpf_insn *insn, int insn_cnt,
869869
}
870870
}
871871

872+
static int emit_nops(u8 **pprog, int len)
873+
{
874+
u8 *prog = *pprog;
875+
int i, noplen, cnt = 0;
876+
877+
while (len > 0) {
878+
noplen = len;
879+
880+
if (noplen > ASM_NOP_MAX)
881+
noplen = ASM_NOP_MAX;
882+
883+
for (i = 0; i < noplen; i++)
884+
EMIT1(ideal_nops[noplen][i]);
885+
len -= noplen;
886+
}
887+
888+
*pprog = prog;
889+
890+
return cnt;
891+
}
892+
893+
#define INSN_SZ_DIFF (((addrs[i] - addrs[i - 1]) - (prog - temp)))
894+
872895
static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
873-
int oldproglen, struct jit_context *ctx)
896+
int oldproglen, struct jit_context *ctx, bool jmp_padding)
874897
{
875898
bool tail_call_reachable = bpf_prog->aux->tail_call_reachable;
876899
struct bpf_insn *insn = bpf_prog->insnsi;
@@ -880,7 +903,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
880903
bool seen_exit = false;
881904
u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
882905
int i, cnt = 0, excnt = 0;
883-
int proglen = 0;
906+
int ilen, proglen = 0;
884907
u8 *prog = temp;
885908
int err;
886909

@@ -894,7 +917,13 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
894917
bpf_prog_was_classic(bpf_prog), tail_call_reachable,
895918
bpf_prog->aux->func_idx != 0);
896919
push_callee_regs(&prog, callee_regs_used);
897-
addrs[0] = prog - temp;
920+
921+
ilen = prog - temp;
922+
if (image)
923+
memcpy(image + proglen, temp, ilen);
924+
proglen += ilen;
925+
addrs[0] = proglen;
926+
prog = temp;
898927

899928
for (i = 1; i <= insn_cnt; i++, insn++) {
900929
const s32 imm32 = insn->imm;
@@ -903,8 +932,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
903932
u8 b2 = 0, b3 = 0;
904933
s64 jmp_offset;
905934
u8 jmp_cond;
906-
int ilen;
907935
u8 *func;
936+
int nops;
908937

909938
switch (insn->code) {
910939
/* ALU */
@@ -1502,6 +1531,30 @@ st: if (is_imm8(insn->off))
15021531
}
15031532
jmp_offset = addrs[i + insn->off] - addrs[i];
15041533
if (is_imm8(jmp_offset)) {
1534+
if (jmp_padding) {
1535+
/* To keep the jmp_offset valid, the extra bytes are
1536+
* padded before the jump insn, so we substract the
1537+
* 2 bytes of jmp_cond insn from INSN_SZ_DIFF.
1538+
*
1539+
* If the previous pass already emits an imm8
1540+
* jmp_cond, then this BPF insn won't shrink, so
1541+
* "nops" is 0.
1542+
*
1543+
* On the other hand, if the previous pass emits an
1544+
* imm32 jmp_cond, the extra 4 bytes(*) is padded to
1545+
* keep the image from shrinking further.
1546+
*
1547+
* (*) imm32 jmp_cond is 6 bytes, and imm8 jmp_cond
1548+
* is 2 bytes, so the size difference is 4 bytes.
1549+
*/
1550+
nops = INSN_SZ_DIFF - 2;
1551+
if (nops != 0 && nops != 4) {
1552+
pr_err("unexpected jmp_cond padding: %d bytes\n",
1553+
nops);
1554+
return -EFAULT;
1555+
}
1556+
cnt += emit_nops(&prog, nops);
1557+
}
15051558
EMIT2(jmp_cond, jmp_offset);
15061559
} else if (is_simm32(jmp_offset)) {
15071560
EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
@@ -1524,11 +1577,55 @@ st: if (is_imm8(insn->off))
15241577
else
15251578
jmp_offset = addrs[i + insn->off] - addrs[i];
15261579

1527-
if (!jmp_offset)
1528-
/* Optimize out nop jumps */
1580+
if (!jmp_offset) {
1581+
/*
1582+
* If jmp_padding is enabled, the extra nops will
1583+
* be inserted. Otherwise, optimize out nop jumps.
1584+
*/
1585+
if (jmp_padding) {
1586+
/* There are 3 possible conditions.
1587+
* (1) This BPF_JA is already optimized out in
1588+
* the previous run, so there is no need
1589+
* to pad any extra byte (0 byte).
1590+
* (2) The previous pass emits an imm8 jmp,
1591+
* so we pad 2 bytes to match the previous
1592+
* insn size.
1593+
* (3) Similarly, the previous pass emits an
1594+
* imm32 jmp, and 5 bytes is padded.
1595+
*/
1596+
nops = INSN_SZ_DIFF;
1597+
if (nops != 0 && nops != 2 && nops != 5) {
1598+
pr_err("unexpected nop jump padding: %d bytes\n",
1599+
nops);
1600+
return -EFAULT;
1601+
}
1602+
cnt += emit_nops(&prog, nops);
1603+
}
15291604
break;
1605+
}
15301606
emit_jmp:
15311607
if (is_imm8(jmp_offset)) {
1608+
if (jmp_padding) {
1609+
/* To avoid breaking jmp_offset, the extra bytes
1610+
* are padded before the actual jmp insn, so
1611+
* 2 bytes is substracted from INSN_SZ_DIFF.
1612+
*
1613+
* If the previous pass already emits an imm8
1614+
* jmp, there is nothing to pad (0 byte).
1615+
*
1616+
* If it emits an imm32 jmp (5 bytes) previously
1617+
* and now an imm8 jmp (2 bytes), then we pad
1618+
* (5 - 2 = 3) bytes to stop the image from
1619+
* shrinking further.
1620+
*/
1621+
nops = INSN_SZ_DIFF - 2;
1622+
if (nops != 0 && nops != 3) {
1623+
pr_err("unexpected jump padding: %d bytes\n",
1624+
nops);
1625+
return -EFAULT;
1626+
}
1627+
cnt += emit_nops(&prog, INSN_SZ_DIFF - 2);
1628+
}
15321629
EMIT2(0xEB, jmp_offset);
15331630
} else if (is_simm32(jmp_offset)) {
15341631
EMIT1_off32(0xE9, jmp_offset);
@@ -1671,26 +1768,6 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
16711768
return 0;
16721769
}
16731770

1674-
static void emit_nops(u8 **pprog, unsigned int len)
1675-
{
1676-
unsigned int i, noplen;
1677-
u8 *prog = *pprog;
1678-
int cnt = 0;
1679-
1680-
while (len > 0) {
1681-
noplen = len;
1682-
1683-
if (noplen > ASM_NOP_MAX)
1684-
noplen = ASM_NOP_MAX;
1685-
1686-
for (i = 0; i < noplen; i++)
1687-
EMIT1(ideal_nops[noplen][i]);
1688-
len -= noplen;
1689-
}
1690-
1691-
*pprog = prog;
1692-
}
1693-
16941771
static void emit_align(u8 **pprog, u32 align)
16951772
{
16961773
u8 *target, *prog = *pprog;
@@ -2065,6 +2142,9 @@ struct x64_jit_data {
20652142
struct jit_context ctx;
20662143
};
20672144

2145+
#define MAX_PASSES 20
2146+
#define PADDING_PASSES (MAX_PASSES - 5)
2147+
20682148
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
20692149
{
20702150
struct bpf_binary_header *header = NULL;
@@ -2074,6 +2154,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
20742154
struct jit_context ctx = {};
20752155
bool tmp_blinded = false;
20762156
bool extra_pass = false;
2157+
bool padding = false;
20772158
u8 *image = NULL;
20782159
int *addrs;
20792160
int pass;
@@ -2110,6 +2191,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21102191
image = jit_data->image;
21112192
header = jit_data->header;
21122193
extra_pass = true;
2194+
padding = true;
21132195
goto skip_init_addrs;
21142196
}
21152197
addrs = kmalloc_array(prog->len + 1, sizeof(*addrs), GFP_KERNEL);
@@ -2135,8 +2217,10 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21352217
* may converge on the last pass. In such case do one more
21362218
* pass to emit the final image.
21372219
*/
2138-
for (pass = 0; pass < 20 || image; pass++) {
2139-
proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
2220+
for (pass = 0; pass < MAX_PASSES || image; pass++) {
2221+
if (!padding && pass >= PADDING_PASSES)
2222+
padding = true;
2223+
proglen = do_jit(prog, addrs, image, oldproglen, &ctx, padding);
21402224
if (proglen <= 0) {
21412225
out_image:
21422226
image = NULL;

lib/test_bpf.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static int __bpf_fill_ja(struct bpf_test *self, unsigned int len,
345345

346346
static int bpf_fill_maxinsns11(struct bpf_test *self)
347347
{
348-
/* Hits 70 passes on x86_64, so cannot get JITed there. */
348+
/* Hits 70 passes on x86_64 and triggers NOPs padding. */
349349
return __bpf_fill_ja(self, BPF_MAXINSNS, 68);
350350
}
351351

@@ -5318,15 +5318,10 @@ static struct bpf_test tests[] = {
53185318
{
53195319
"BPF_MAXINSNS: Jump, gap, jump, ...",
53205320
{ },
5321-
#if defined(CONFIG_BPF_JIT_ALWAYS_ON) && defined(CONFIG_X86)
5322-
CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
5323-
#else
53245321
CLASSIC | FLAG_NO_DATA,
5325-
#endif
53265322
{ },
53275323
{ { 0, 0xababcbac } },
53285324
.fill_helper = bpf_fill_maxinsns11,
5329-
.expected_errcode = -ENOTSUPP,
53305325
},
53315326
{
53325327
"BPF_MAXINSNS: jump over MSH",

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,78 @@ static void bpf_fill_scale(struct bpf_test *self)
296296
}
297297
}
298298

299+
static int bpf_fill_torturous_jumps_insn_1(struct bpf_insn *insn)
300+
{
301+
unsigned int len = 259, hlen = 128;
302+
int i;
303+
304+
insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32);
305+
for (i = 1; i <= hlen; i++) {
306+
insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, hlen);
307+
insn[i + hlen] = BPF_JMP_A(hlen - i);
308+
}
309+
insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 1);
310+
insn[len - 1] = BPF_EXIT_INSN();
311+
312+
return len;
313+
}
314+
315+
static int bpf_fill_torturous_jumps_insn_2(struct bpf_insn *insn)
316+
{
317+
unsigned int len = 4100, jmp_off = 2048;
318+
int i, j;
319+
320+
insn[0] = BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32);
321+
for (i = 1; i <= jmp_off; i++) {
322+
insn[i] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, i, jmp_off);
323+
}
324+
insn[i++] = BPF_JMP_A(jmp_off);
325+
for (; i <= jmp_off * 2 + 1; i+=16) {
326+
for (j = 0; j < 16; j++) {
327+
insn[i + j] = BPF_JMP_A(16 - j - 1);
328+
}
329+
}
330+
331+
insn[len - 2] = BPF_MOV64_IMM(BPF_REG_0, 2);
332+
insn[len - 1] = BPF_EXIT_INSN();
333+
334+
return len;
335+
}
336+
337+
static void bpf_fill_torturous_jumps(struct bpf_test *self)
338+
{
339+
struct bpf_insn *insn = self->fill_insns;
340+
int i = 0;
341+
342+
switch (self->retval) {
343+
case 1:
344+
self->prog_len = bpf_fill_torturous_jumps_insn_1(insn);
345+
return;
346+
case 2:
347+
self->prog_len = bpf_fill_torturous_jumps_insn_2(insn);
348+
return;
349+
case 3:
350+
/* main */
351+
insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 4);
352+
insn[i++] = BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 1, 0, 262);
353+
insn[i++] = BPF_ST_MEM(BPF_B, BPF_REG_10, -32, 0);
354+
insn[i++] = BPF_MOV64_IMM(BPF_REG_0, 3);
355+
insn[i++] = BPF_EXIT_INSN();
356+
357+
/* subprog 1 */
358+
i += bpf_fill_torturous_jumps_insn_1(insn + i);
359+
360+
/* subprog 2 */
361+
i += bpf_fill_torturous_jumps_insn_2(insn + i);
362+
363+
self->prog_len = i;
364+
return;
365+
default:
366+
self->prog_len = 0;
367+
break;
368+
}
369+
}
370+
299371
/* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */
300372
#define BPF_SK_LOOKUP(func) \
301373
/* struct bpf_sock_tuple tuple = {} */ \

tools/testing/selftests/bpf/verifier/jit.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,27 @@
105105
.result = ACCEPT,
106106
.retval = 2,
107107
},
108+
{
109+
"jit: torturous jumps, imm8 nop jmp and pure jump padding",
110+
.insns = { },
111+
.fill_helper = bpf_fill_torturous_jumps,
112+
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
113+
.result = ACCEPT,
114+
.retval = 1,
115+
},
116+
{
117+
"jit: torturous jumps, imm32 nop jmp and jmp_cond padding",
118+
.insns = { },
119+
.fill_helper = bpf_fill_torturous_jumps,
120+
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
121+
.result = ACCEPT,
122+
.retval = 2,
123+
},
124+
{
125+
"jit: torturous jumps in subprog",
126+
.insns = { },
127+
.fill_helper = bpf_fill_torturous_jumps,
128+
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
129+
.result = ACCEPT,
130+
.retval = 3,
131+
},

0 commit comments

Comments
 (0)