> Am 16.08.2019 um 02:05 schrieb Yonghong Song <yhs@xxxxxx>: > >> +# define __bpf_constant_be64_to_cpu(x) ___constant_swab64(x) > > bpf_endian.h is used for both bpf program and native applications. > Could you make sure it works for bpf programs? It should be, but want to > double check. Yes: #include <linux/compiler_attributes.h> #include "bpf_endian.h" u64 answer() { return __bpf_constant_be64_to_cpu(42); } compiles to r0 = 3026418949592973312 ll exit on x86. > The __constant_swab64 looks like a little bit expensive > for bpf programs compared to __builtin_bswap64. But > __builtin_bswap64 may not be available for all architectures, esp. > 32bit system. So macro __bpf__ is required to use it. Isn't ___constant_swab64 supposed to be 100% compile-time? Also, I think __builtin_bswap64 should be available everywhere for userspace. At least the following test does not indicate any problems: for cc in "x86_64-linux-gnu-gcc -m32" \ "x86_64-linux-gnu-gcc -m64" \ "aarch64-linux-gnu-gcc" \ "arm-linux-gnueabihf-gcc" \ "mips64el-linux-gnuabi64-gcc" \ "powerpc64le-linux-gnu-gcc -m32" \ "s390x-linux-gnu-gcc -m31" \ "s390x-linux-gnu-gcc -m64" \ "sparc64-linux-gnu-gcc -m32" \ "sparc64-linux-gnu-gcc -m64" \ "clang -target bpf -m32" \ "clang -target bpf -m64"; do echo "*** $cc ***" echo "long long f(long long x) { return __builtin_bswap64(x); }" | \ $cc -x c -S - -O3 -o -; done Only sparc64 doesn't support it directly, but then it just calls libgcc's __bswapdi2. This might not be ok only for kernel native code (though even there we have e.g. arch/arm/lib/bswapsdi2.S), but I don't think this header is used in such context anyway. >> >> BPF_MOV64_REG(BPF_REG_1, BPF_REG_7), >> @@ -1344,20 +1379,26 @@ static size_t probe_prog_length(const struct bpf_insn *fp) >> static int fixup_sysctl_value(const char *buf, size_t buf_len, >> struct bpf_insn *prog, size_t insn_num) >> { >> - uint32_t value_num = 0; >> + uint64_t value_num = 0; >> uint8_t c, i; >> >> if (buf_len > sizeof(value_num)) { >> log_err("Value is too big (%zd) to use in fixup", buf_len); >> return -1; >> } >> + if (prog[insn_num].code != (BPF_LD | BPF_DW | BPF_IMM)) { >> + log_err("Can fixup only BPF_LD_IMM64 insns"); >> + return -1; >> + } >> >> for (i = 0; i < buf_len; ++i) { >> c = buf[i]; >> value_num |= (c << i * 8); >> } >> + value_num = __bpf_le64_to_cpu(value_num); > > Can we avoid to use __bpf_le64_to_cpu? > Look like we already having the value in buf, can we just cast it > to get value_num. Note that bpf program and host always have > the same endianness. This way, no endianness conversion > is needed. I think this might be dangerous in case buf is smaller than 8 bytes.