Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

riscv, bpf: Extract emit_st() helper

There's a lot of redundant code related to store from immediate
operations, let's extract emit_st() to make code more compact.

Signed-off-by: Pu Lehui <pulehui@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Acked-by: Björn Töpel <bjorn@kernel.org>
Link: https://lore.kernel.org/bpf/20250719091730.2660197-3-pulehui@huaweicloud.com

authored by

Pu Lehui and committed by
Daniel Borkmann
d92c11a6 02fc01ad

+26 -109
+26 -109
arch/riscv/net/bpf_jit_comp64.c
··· 577 577 } 578 578 } 579 579 580 + static int emit_st(u8 rd, s16 off, s32 imm, u8 size, struct rv_jit_context *ctx) 581 + { 582 + int insns_start; 583 + 584 + emit_imm(RV_REG_T1, imm, ctx); 585 + if (is_12b_int(off)) { 586 + insns_start = ctx->ninsns; 587 + emit_stx_insn(rd, off, RV_REG_T1, size, ctx); 588 + return ctx->ninsns - insns_start; 589 + } 590 + 591 + emit_imm(RV_REG_T2, off, ctx); 592 + emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 593 + insns_start = ctx->ninsns; 594 + emit_stx_insn(RV_REG_T2, 0, RV_REG_T1, size, ctx); 595 + return ctx->ninsns - insns_start; 596 + } 597 + 580 598 static int emit_stx(u8 rd, s16 off, u8 rs, u8 size, struct rv_jit_context *ctx) 581 599 { 582 600 int insns_start; ··· 1888 1870 1889 1871 /* ST: *(size *)(dst + off) = imm */ 1890 1872 case BPF_ST | BPF_MEM | BPF_B: 1891 - emit_imm(RV_REG_T1, imm, ctx); 1892 - if (is_12b_int(off)) { 1893 - emit(rv_sb(rd, off, RV_REG_T1), ctx); 1894 - break; 1895 - } 1896 - 1897 - emit_imm(RV_REG_T2, off, ctx); 1898 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1899 - emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx); 1900 - break; 1901 - 1902 1873 case BPF_ST | BPF_MEM | BPF_H: 1903 - emit_imm(RV_REG_T1, imm, ctx); 1904 - if (is_12b_int(off)) { 1905 - emit(rv_sh(rd, off, RV_REG_T1), ctx); 1906 - break; 1907 - } 1908 - 1909 - emit_imm(RV_REG_T2, off, ctx); 1910 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1911 - emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx); 1912 - break; 1913 1874 case BPF_ST | BPF_MEM | BPF_W: 1914 - emit_imm(RV_REG_T1, imm, ctx); 1915 - if (is_12b_int(off)) { 1916 - emit_sw(rd, off, RV_REG_T1, ctx); 1917 - break; 1918 - } 1919 - 1920 - emit_imm(RV_REG_T2, off, ctx); 1921 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1922 - emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx); 1923 - break; 1924 1875 case BPF_ST | BPF_MEM | BPF_DW: 1925 - emit_imm(RV_REG_T1, imm, ctx); 1926 - if (is_12b_int(off)) { 1927 - emit_sd(rd, off, RV_REG_T1, ctx); 1928 - break; 1929 - } 1930 - 1931 - emit_imm(RV_REG_T2, off, ctx); 1932 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1933 - emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx); 1934 - break; 1935 - 1876 + /* ST | PROBE_MEM32: *(size *)(dst + RV_REG_ARENA + off) = imm */ 1936 1877 case BPF_ST | BPF_PROBE_MEM32 | BPF_B: 1937 1878 case BPF_ST | BPF_PROBE_MEM32 | BPF_H: 1938 1879 case BPF_ST | BPF_PROBE_MEM32 | BPF_W: 1939 1880 case BPF_ST | BPF_PROBE_MEM32 | BPF_DW: 1940 1881 { 1941 - int insn_len, insns_start; 1882 + int insn_len; 1942 1883 1943 - emit_add(RV_REG_T3, rd, RV_REG_ARENA, ctx); 1944 - rd = RV_REG_T3; 1945 - 1946 - /* Load imm to a register then store it */ 1947 - emit_imm(RV_REG_T1, imm, ctx); 1948 - 1949 - switch (BPF_SIZE(code)) { 1950 - case BPF_B: 1951 - if (is_12b_int(off)) { 1952 - insns_start = ctx->ninsns; 1953 - emit(rv_sb(rd, off, RV_REG_T1), ctx); 1954 - insn_len = ctx->ninsns - insns_start; 1955 - break; 1956 - } 1957 - 1958 - emit_imm(RV_REG_T2, off, ctx); 1959 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1960 - insns_start = ctx->ninsns; 1961 - emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx); 1962 - insn_len = ctx->ninsns - insns_start; 1963 - break; 1964 - case BPF_H: 1965 - if (is_12b_int(off)) { 1966 - insns_start = ctx->ninsns; 1967 - emit(rv_sh(rd, off, RV_REG_T1), ctx); 1968 - insn_len = ctx->ninsns - insns_start; 1969 - break; 1970 - } 1971 - 1972 - emit_imm(RV_REG_T2, off, ctx); 1973 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1974 - insns_start = ctx->ninsns; 1975 - emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx); 1976 - insn_len = ctx->ninsns - insns_start; 1977 - break; 1978 - case BPF_W: 1979 - if (is_12b_int(off)) { 1980 - insns_start = ctx->ninsns; 1981 - emit_sw(rd, off, RV_REG_T1, ctx); 1982 - insn_len = ctx->ninsns - insns_start; 1983 - break; 1984 - } 1985 - 1986 - emit_imm(RV_REG_T2, off, ctx); 1987 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 1988 - insns_start = ctx->ninsns; 1989 - emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx); 1990 - insn_len = ctx->ninsns - insns_start; 1991 - break; 1992 - case BPF_DW: 1993 - if (is_12b_int(off)) { 1994 - insns_start = ctx->ninsns; 1995 - emit_sd(rd, off, RV_REG_T1, ctx); 1996 - insn_len = ctx->ninsns - insns_start; 1997 - break; 1998 - } 1999 - 2000 - emit_imm(RV_REG_T2, off, ctx); 2001 - emit_add(RV_REG_T2, RV_REG_T2, rd, ctx); 2002 - insns_start = ctx->ninsns; 2003 - emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx); 2004 - insn_len = ctx->ninsns - insns_start; 2005 - break; 1884 + if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) { 1885 + emit_add(RV_REG_T3, rd, RV_REG_ARENA, ctx); 1886 + rd = RV_REG_T3; 2006 1887 } 2007 1888 2008 - ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER, 2009 - insn_len); 1889 + insn_len = emit_st(rd, off, imm, BPF_SIZE(code), ctx); 1890 + 1891 + ret = add_exception_handler(insn, ctx, REG_DONT_CLEAR_MARKER, insn_len); 2010 1892 if (ret) 2011 1893 return ret; 2012 - 2013 1894 break; 2014 1895 } 2015 1896