Skip to content

Commit d4bcdb6

Browse files
Xu Kuohaixukuohai
Xu Kuohai
authored andcommitted
bpf, arm64: Support to poke bpf prog
1. Set up the bpf prog entry in the same way as fentry to support trampoline. Now bpf prog entry looks like this: bti c // if BTI enabled mov x9, x30 // save lr nop // to be replaced with jump instruction paciasp // if PAC enabled 2. Update bpf_arch_text_poke() to poke bpf prog. If the instruction to be poked is bpf prog's first instruction, skip to the nop instruction in the prog entry. Signed-off-by: Xu Kuohai <[email protected]>
1 parent c06c03a commit d4bcdb6

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

arch/arm64/net/bpf_jit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
#define A64_BTI_C A64_HINT(AARCH64_INSN_HINT_BTIC)
271271
#define A64_BTI_J A64_HINT(AARCH64_INSN_HINT_BTIJ)
272272
#define A64_BTI_JC A64_HINT(AARCH64_INSN_HINT_BTIJC)
273+
#define A64_NOP A64_HINT(AARCH64_INSN_HINT_NOP)
273274

274275
/* DMB */
275276
#define A64_DMB_ISH aarch64_insn_gen_dmb(AARCH64_INSN_MB_ISH)

arch/arm64/net/bpf_jit_comp.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,23 @@ static bool is_lsi_offset(int offset, int scale)
237237
return true;
238238
}
239239

240-
/* Tail call offset to jump into */
241-
#if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) || \
242-
IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)
243-
#define PROLOGUE_OFFSET 9
240+
#if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
241+
#define BTI_INSNS 1
242+
#else
243+
#define BTI_INSNS 0
244+
#endif
245+
246+
#if IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)
247+
#define PAC_INSNS 1
244248
#else
245-
#define PROLOGUE_OFFSET 8
249+
#define PAC_INSNS 0
246250
#endif
247251

252+
/* Tail call offset to jump into */
253+
#define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 8)
254+
/* Offset of nop instruction in bpf prog entry to be poked */
255+
#define POKE_OFFSET (BTI_INSNS + 1)
256+
248257
static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
249258
{
250259
const struct bpf_prog *prog = ctx->prog;
@@ -281,12 +290,15 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
281290
*
282291
*/
283292

293+
if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
294+
emit(A64_BTI_C, ctx);
295+
296+
emit(A64_MOV(1, A64_R(9), A64_LR), ctx);
297+
emit(A64_NOP, ctx);
298+
284299
/* Sign lr */
285300
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
286301
emit(A64_PACIASP, ctx);
287-
/* BTI landing pad */
288-
else if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
289-
emit(A64_BTI_C, ctx);
290302

291303
/* Save FP and LR registers to stay align with ARM64 AAPCS */
292304
emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
@@ -1552,9 +1564,11 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
15521564
u32 old_insn;
15531565
u32 new_insn;
15541566
u32 replaced;
1567+
unsigned long offset = ~0UL;
15551568
enum aarch64_insn_branch_type branch_type;
1569+
char namebuf[KSYM_NAME_LEN];
15561570

1557-
if (!is_bpf_text_address((long)ip))
1571+
if (!__bpf_address_lookup((unsigned long)ip, NULL, &offset, namebuf))
15581572
/* Only poking bpf text is supported. Since kernel function
15591573
* entry is set up by ftrace, we reply on ftrace to poke kernel
15601574
* functions. For kernel funcitons, bpf_arch_text_poke() is only
@@ -1565,6 +1579,15 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
15651579
*/
15661580
return -EINVAL;
15671581

1582+
/* bpf entry */
1583+
if (offset == 0UL)
1584+
/* skip to the nop instruction in bpf prog entry:
1585+
* bti c // if BTI enabled
1586+
* mov x9, x30
1587+
* nop
1588+
*/
1589+
ip = (u32 *)ip + POKE_OFFSET;
1590+
15681591
if (poke_type == BPF_MOD_CALL)
15691592
branch_type = AARCH64_INSN_BRANCH_LINK;
15701593
else

0 commit comments

Comments
 (0)