Skip to content

Commit 02ab695

Browse files
Alexei Starovoitovdavem330
authored andcommitted
net: filter: add "load 64-bit immediate" eBPF instruction
add BPF_LD_IMM64 instruction to load 64-bit immediate value into a register. All previous instructions were 8-byte. This is first 16-byte instruction. Two consecutive 'struct bpf_insn' blocks are interpreted as single instruction: insn[0].code = BPF_LD | BPF_DW | BPF_IMM insn[0].dst_reg = destination register insn[0].imm = lower 32-bit insn[1].code = 0 insn[1].imm = upper 32-bit All unused fields must be zero. Classic BPF has similar instruction: BPF_LD | BPF_W | BPF_IMM which loads 32-bit immediate value into a register. x64 JITs it as single 'movabsq %rax, imm64' arm64 may JIT as sequence of four 'movk x0, #imm16, lsl #shift' insn Note that old eBPF programs are binary compatible with new interpreter. It helps eBPF programs load 64-bit constant into a register with one instruction instead of using two registers and 4 instructions: BPF_MOV32_IMM(R1, imm32) BPF_ALU64_IMM(BPF_LSH, R1, 32) BPF_MOV32_IMM(R2, imm32) BPF_ALU64_REG(BPF_OR, R1, R2) User space generated programs will use this instruction to load constants only. To tell kernel that user space needs a pointer the _pseudo_ variant of this instruction may be added later, which will use extra bits of encoding to indicate what type of pointer user space is asking kernel to provide. For example 'off' or 'src_reg' fields can be used for such purpose. src_reg = 1 could mean that user space is asking kernel to validate and load in-kernel map pointer. src_reg = 2 could mean that user space needs readonly data section pointer src_reg = 3 could mean that user space needs a pointer to per-cpu local data All such future pseudo instructions will not be carrying the actual pointer as part of the instruction, but rather will be treated as a request to kernel to provide one. The kernel will verify the request_for_a_pointer, then will drop _pseudo_ marking and will store actual internal pointer inside the instruction, so the end result is the interpreter and JITs never see pseudo BPF_LD_IMM64 insns and only operate on generic BPF_LD_IMM64 that loads 64-bit immediate into a register. User space never operates on direct pointers and verifier can easily recognize request_for_pointer vs other instructions. Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5b4c314 commit 02ab695

5 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎Documentation/networking/filter.txt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ Size modifier is one of ...
951951

952952
Mode modifier is one of:
953953

954-
BPF_IMM 0x00 /* classic BPF only, reserved in eBPF */
954+
BPF_IMM 0x00 /* used for 32-bit mov in classic BPF and 64-bit in eBPF */
955955
BPF_ABS 0x20
956956
BPF_IND 0x40
957957
BPF_MEM 0x60
@@ -995,6 +995,12 @@ BPF_XADD | BPF_DW | BPF_STX: lock xadd *(u64 *)(dst_reg + off16) += src_reg
995995
Where size is one of: BPF_B or BPF_H or BPF_W or BPF_DW. Note that 1 and
996996
2 byte atomic increments are not supported.
997997

998+
eBPF has one 16-byte instruction: BPF_LD | BPF_DW | BPF_IMM which consists
999+
of two consecutive 'struct bpf_insn' 8-byte blocks and interpreted as single
1000+
instruction that loads 64-bit immediate value into a dst_reg.
1001+
Classic BPF has similar instruction: BPF_LD | BPF_W | BPF_IMM which loads
1002+
32-bit immediate value into a register.
1003+
9981004
Testing
9991005
-------
10001006

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,23 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
393393
EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
394394
break;
395395

396+
case BPF_LD | BPF_IMM | BPF_DW:
397+
if (insn[1].code != 0 || insn[1].src_reg != 0 ||
398+
insn[1].dst_reg != 0 || insn[1].off != 0) {
399+
/* verifier must catch invalid insns */
400+
pr_err("invalid BPF_LD_IMM64 insn\n");
401+
return -EINVAL;
402+
}
403+
404+
/* movabsq %rax, imm64 */
405+
EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
406+
EMIT(insn[0].imm, 4);
407+
EMIT(insn[1].imm, 4);
408+
409+
insn++;
410+
i++;
411+
break;
412+
396413
/* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
397414
case BPF_ALU | BPF_MOD | BPF_X:
398415
case BPF_ALU | BPF_DIV | BPF_X:

‎include/linux/filter.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ enum {
166166
.off = 0, \
167167
.imm = IMM })
168168

169+
/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
170+
#define BPF_LD_IMM64(DST, IMM) \
171+
BPF_LD_IMM64_RAW(DST, 0, IMM)
172+
173+
#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
174+
((struct bpf_insn) { \
175+
.code = BPF_LD | BPF_DW | BPF_IMM, \
176+
.dst_reg = DST, \
177+
.src_reg = SRC, \
178+
.off = 0, \
179+
.imm = (__u32) (IMM) }), \
180+
((struct bpf_insn) { \
181+
.code = 0, /* zero is reserved opcode */ \
182+
.dst_reg = 0, \
183+
.src_reg = 0, \
184+
.off = 0, \
185+
.imm = ((__u64) (IMM)) >> 32 })
186+
169187
/* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
170188

171189
#define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \

‎kernel/bpf/core.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
242242
[BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
243243
[BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
244244
[BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
245+
[BPF_LD | BPF_IMM | BPF_DW] = &&LD_IMM_DW,
245246
};
246247
void *ptr;
247248
int off;
@@ -301,6 +302,10 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
301302
ALU64_MOV_K:
302303
DST = IMM;
303304
CONT;
305+
LD_IMM_DW:
306+
DST = (u64) (u32) insn[0].imm | ((u64) (u32) insn[1].imm) << 32;
307+
insn++;
308+
CONT;
304309
ALU64_ARSH_X:
305310
(*(s64 *) &DST) >>= SRC;
306311
CONT;

‎lib/test_bpf.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,27 @@ static struct bpf_test tests[] = {
17351735
{ },
17361736
{ { 1, 0 } },
17371737
},
1738+
{
1739+
"load 64-bit immediate",
1740+
.u.insns_int = {
1741+
BPF_LD_IMM64(R1, 0x567800001234L),
1742+
BPF_MOV64_REG(R2, R1),
1743+
BPF_MOV64_REG(R3, R2),
1744+
BPF_ALU64_IMM(BPF_RSH, R2, 32),
1745+
BPF_ALU64_IMM(BPF_LSH, R3, 32),
1746+
BPF_ALU64_IMM(BPF_RSH, R3, 32),
1747+
BPF_ALU64_IMM(BPF_MOV, R0, 0),
1748+
BPF_JMP_IMM(BPF_JEQ, R2, 0x5678, 1),
1749+
BPF_EXIT_INSN(),
1750+
BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
1751+
BPF_EXIT_INSN(),
1752+
BPF_ALU64_IMM(BPF_MOV, R0, 1),
1753+
BPF_EXIT_INSN(),
1754+
},
1755+
INTERNAL,
1756+
{ },
1757+
{ { 0, 1 } }
1758+
},
17381759
};
17391760

17401761
static struct net_device dev;

0 commit comments

Comments
 (0)