Skip to content

Commit 1f1e864

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Handle sign-extenstin ctx member accesses
Currently, if user accesses a ctx member with signed types, the compiler will generate an unsigned load followed by necessary left and right shifts. With the introduction of sign-extension load, compiler may just emit a ldsx insn instead. Let us do a final movsx sign extension to the final unsigned ctx load result to satisfy original sign extension requirement. Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8100928 commit 1f1e864

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17716,6 +17716,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
1771617716

1771717717
for (i = 0; i < insn_cnt; i++, insn++) {
1771817718
bpf_convert_ctx_access_t convert_ctx_access;
17719+
u8 mode;
1771917720

1772017721
if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
1772117722
insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
@@ -17797,6 +17798,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
1779717798

1779817799
ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
1779917800
size = BPF_LDST_BYTES(insn);
17801+
mode = BPF_MODE(insn->code);
1780017802

1780117803
/* If the read access is a narrower load of the field,
1780217804
* convert to a 4/8-byte load, to minimum program type specific
@@ -17856,6 +17858,10 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
1785617858
(1ULL << size * 8) - 1);
1785717859
}
1785817860
}
17861+
if (mode == BPF_MEMSX)
17862+
insn_buf[cnt++] = BPF_RAW_INSN(BPF_ALU64 | BPF_MOV | BPF_X,
17863+
insn->dst_reg, insn->dst_reg,
17864+
size * 8, 0);
1785917865

1786017866
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
1786117867
if (!new_prog)

0 commit comments

Comments
 (0)