Skip to content

Commit c217abc

Browse files
Jiong Wangborkmann
authored andcommitted
nfp: bpf: support arithmetic indirect right shift (BPF_ARSH | BPF_X)
Code logic is similar with arithmetic right shift by constant, and NFP get indirect shift amount through source A operand of PREV_ALU. It is possible to fall back to logic right shift if the MSB is known to be zero from range info, however there is no benefit to do this given logic indirect right shift use the same number and cycle of instruction sequence. Suppose the MSB of regX is the bit we want to replicate to fill in all the vacant positions, and regY contains the shift amount, then we could use single instruction to set up both. [alu, --, regY, OR, regX] -- NOTE: the PREV_ALU result doesn't need to write to any destination register. Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent f43d0f1 commit c217abc

1 file changed

Lines changed: 89 additions & 10 deletions

File tree

  • drivers/net/ethernet/netronome/nfp/bpf

‎drivers/net/ethernet/netronome/nfp/bpf/jit.c‎

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,36 +1919,114 @@ static int shr_reg64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta)
19191919
/* Code logic is the same as __shr_imm64 except ashr requires signedness bit
19201920
* told through PREV_ALU result.
19211921
*/
1922-
static int ashr_imm64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta)
1922+
static int __ashr_imm64(struct nfp_prog *nfp_prog, u8 dst, u8 shift_amt)
19231923
{
1924-
const struct bpf_insn *insn = &meta->insn;
1925-
u8 dst = insn->dst_reg * 2;
1926-
1927-
if (insn->imm < 32) {
1924+
if (shift_amt < 32) {
19281925
emit_shf(nfp_prog, reg_both(dst), reg_a(dst + 1), SHF_OP_NONE,
1929-
reg_b(dst), SHF_SC_R_DSHF, insn->imm);
1926+
reg_b(dst), SHF_SC_R_DSHF, shift_amt);
19301927
/* Set signedness bit. */
19311928
emit_alu(nfp_prog, reg_none(), reg_a(dst + 1), ALU_OP_OR,
19321929
reg_imm(0));
19331930
emit_shf(nfp_prog, reg_both(dst + 1), reg_none(), SHF_OP_ASHR,
1934-
reg_b(dst + 1), SHF_SC_R_SHF, insn->imm);
1935-
} else if (insn->imm == 32) {
1931+
reg_b(dst + 1), SHF_SC_R_SHF, shift_amt);
1932+
} else if (shift_amt == 32) {
19361933
/* NOTE: this also helps setting signedness bit. */
19371934
wrp_reg_mov(nfp_prog, dst, dst + 1);
19381935
emit_shf(nfp_prog, reg_both(dst + 1), reg_none(), SHF_OP_ASHR,
19391936
reg_b(dst + 1), SHF_SC_R_SHF, 31);
1940-
} else if (insn->imm > 32) {
1937+
} else if (shift_amt > 32) {
19411938
emit_alu(nfp_prog, reg_none(), reg_a(dst + 1), ALU_OP_OR,
19421939
reg_imm(0));
19431940
emit_shf(nfp_prog, reg_both(dst), reg_none(), SHF_OP_ASHR,
1944-
reg_b(dst + 1), SHF_SC_R_SHF, insn->imm - 32);
1941+
reg_b(dst + 1), SHF_SC_R_SHF, shift_amt - 32);
19451942
emit_shf(nfp_prog, reg_both(dst + 1), reg_none(), SHF_OP_ASHR,
19461943
reg_b(dst + 1), SHF_SC_R_SHF, 31);
19471944
}
19481945

19491946
return 0;
19501947
}
19511948

1949+
static int ashr_imm64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta)
1950+
{
1951+
const struct bpf_insn *insn = &meta->insn;
1952+
u8 dst = insn->dst_reg * 2;
1953+
1954+
return __ashr_imm64(nfp_prog, dst, insn->imm);
1955+
}
1956+
1957+
static void ashr_reg64_lt32_high(struct nfp_prog *nfp_prog, u8 dst, u8 src)
1958+
{
1959+
/* NOTE: the first insn will set both indirect shift amount (source A)
1960+
* and signedness bit (MSB of result).
1961+
*/
1962+
emit_alu(nfp_prog, reg_none(), reg_a(src), ALU_OP_OR, reg_b(dst + 1));
1963+
emit_shf_indir(nfp_prog, reg_both(dst + 1), reg_none(), SHF_OP_ASHR,
1964+
reg_b(dst + 1), SHF_SC_R_SHF);
1965+
}
1966+
1967+
static void ashr_reg64_lt32_low(struct nfp_prog *nfp_prog, u8 dst, u8 src)
1968+
{
1969+
/* NOTE: it is the same as logic shift because we don't need to shift in
1970+
* signedness bit when the shift amount is less than 32.
1971+
*/
1972+
return shr_reg64_lt32_low(nfp_prog, dst, src);
1973+
}
1974+
1975+
static void ashr_reg64_lt32(struct nfp_prog *nfp_prog, u8 dst, u8 src)
1976+
{
1977+
ashr_reg64_lt32_low(nfp_prog, dst, src);
1978+
ashr_reg64_lt32_high(nfp_prog, dst, src);
1979+
}
1980+
1981+
static void ashr_reg64_ge32(struct nfp_prog *nfp_prog, u8 dst, u8 src)
1982+
{
1983+
emit_alu(nfp_prog, reg_none(), reg_a(src), ALU_OP_OR, reg_b(dst + 1));
1984+
emit_shf_indir(nfp_prog, reg_both(dst), reg_none(), SHF_OP_ASHR,
1985+
reg_b(dst + 1), SHF_SC_R_SHF);
1986+
emit_shf(nfp_prog, reg_both(dst + 1), reg_none(), SHF_OP_ASHR,
1987+
reg_b(dst + 1), SHF_SC_R_SHF, 31);
1988+
}
1989+
1990+
/* Like ashr_imm64, but need to use indirect shift. */
1991+
static int ashr_reg64(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta)
1992+
{
1993+
const struct bpf_insn *insn = &meta->insn;
1994+
u64 umin, umax;
1995+
u8 dst, src;
1996+
1997+
dst = insn->dst_reg * 2;
1998+
umin = meta->umin;
1999+
umax = meta->umax;
2000+
if (umin == umax)
2001+
return __ashr_imm64(nfp_prog, dst, umin);
2002+
2003+
src = insn->src_reg * 2;
2004+
if (umax < 32) {
2005+
ashr_reg64_lt32(nfp_prog, dst, src);
2006+
} else if (umin >= 32) {
2007+
ashr_reg64_ge32(nfp_prog, dst, src);
2008+
} else {
2009+
u16 label_ge32, label_end;
2010+
2011+
label_ge32 = nfp_prog_current_offset(nfp_prog) + 6;
2012+
emit_br_bset(nfp_prog, reg_a(src), 5, label_ge32, 0);
2013+
ashr_reg64_lt32_low(nfp_prog, dst, src);
2014+
label_end = nfp_prog_current_offset(nfp_prog) + 6;
2015+
emit_br(nfp_prog, BR_UNC, label_end, 2);
2016+
/* ashr_reg64_lt32_high packed in delay slot. */
2017+
ashr_reg64_lt32_high(nfp_prog, dst, src);
2018+
2019+
if (!nfp_prog_confirm_current_offset(nfp_prog, label_ge32))
2020+
return -EINVAL;
2021+
ashr_reg64_ge32(nfp_prog, dst, src);
2022+
2023+
if (!nfp_prog_confirm_current_offset(nfp_prog, label_end))
2024+
return -EINVAL;
2025+
}
2026+
2027+
return 0;
2028+
}
2029+
19522030
static int mov_reg(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta)
19532031
{
19542032
const struct bpf_insn *insn = &meta->insn;
@@ -2775,6 +2853,7 @@ static const instr_cb_t instr_cb[256] = {
27752853
[BPF_ALU64 | BPF_LSH | BPF_K] = shl_imm64,
27762854
[BPF_ALU64 | BPF_RSH | BPF_X] = shr_reg64,
27772855
[BPF_ALU64 | BPF_RSH | BPF_K] = shr_imm64,
2856+
[BPF_ALU64 | BPF_ARSH | BPF_X] = ashr_reg64,
27782857
[BPF_ALU64 | BPF_ARSH | BPF_K] = ashr_imm64,
27792858
[BPF_ALU | BPF_MOV | BPF_X] = mov_reg,
27802859
[BPF_ALU | BPF_MOV | BPF_K] = mov_imm,

0 commit comments

Comments
 (0)