Skip to content

Commit 116eb78

Browse files
Björn TöpelAlexei Starovoitov
authored andcommitted
bpf, x86: Align dispatcher branch targets to 16B
>From Intel 64 and IA-32 Architectures Optimization Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler Coding Rule 11: All branch targets should be 16-byte aligned. This commits aligns branch targets according to the Intel manual. The nops used to align branch targets make the dispatcher larger, and therefore the number of supported dispatch points/programs are descreased from 64 to 48. Signed-off-by: Björn Töpel <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e754f5a commit 116eb78

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,26 @@ static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
15481548
return 0;
15491549
}
15501550

1551+
static void emit_nops(u8 **pprog, unsigned int len)
1552+
{
1553+
unsigned int i, noplen;
1554+
u8 *prog = *pprog;
1555+
int cnt = 0;
1556+
1557+
while (len > 0) {
1558+
noplen = len;
1559+
1560+
if (noplen > ASM_NOP_MAX)
1561+
noplen = ASM_NOP_MAX;
1562+
1563+
for (i = 0; i < noplen; i++)
1564+
EMIT1(ideal_nops[noplen][i]);
1565+
len -= noplen;
1566+
}
1567+
1568+
*pprog = prog;
1569+
}
1570+
15511571
static int emit_fallback_jump(u8 **pprog)
15521572
{
15531573
u8 *prog = *pprog;
@@ -1570,8 +1590,8 @@ static int emit_fallback_jump(u8 **pprog)
15701590

15711591
static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
15721592
{
1593+
u8 *jg_reloc, *jg_target, *prog = *pprog;
15731594
int pivot, err, jg_bytes = 1, cnt = 0;
1574-
u8 *jg_reloc, *prog = *pprog;
15751595
s64 jg_offset;
15761596

15771597
if (a == b) {
@@ -1620,6 +1640,14 @@ static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs)
16201640
if (err)
16211641
return err;
16221642

1643+
/* From Intel 64 and IA-32 Architectures Optimization
1644+
* Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
1645+
* Coding Rule 11: All branch targets should be 16-byte
1646+
* aligned.
1647+
*/
1648+
jg_target = PTR_ALIGN(prog, 16);
1649+
if (jg_target != prog)
1650+
emit_nops(&prog, jg_target - prog);
16231651
jg_offset = prog - jg_reloc;
16241652
emit_code(jg_reloc - jg_bytes, jg_offset, jg_bytes);
16251653

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ struct bpf_trampoline {
471471
u64 selector;
472472
};
473473

474-
#define BPF_DISPATCHER_MAX 64 /* Fits in 2048B */
474+
#define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
475475

476476
struct bpf_dispatcher_prog {
477477
struct bpf_prog *prog;

0 commit comments

Comments
 (0)