bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Yonghong Song <yhs@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@kernel.org>,
	"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
	Heiko Carstens <heiko.carstens@de.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>
Subject: Re: [PATCH bpf] selftests/bpf: fix endianness issues in test_sysctl
Date: Fri, 16 Aug 2019 13:37:36 +0200	[thread overview]
Message-ID: <91EFE17B-3835-4679-8464-A76C885BCD46@linux.ibm.com> (raw)
In-Reply-To: <076513cd-fbde-cf66-ce3b-a6143878f786@fb.com>

> Am 16.08.2019 um 02:05 schrieb Yonghong Song <yhs@fb.com>:
> 
>> +# 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.

  reply	other threads:[~2019-08-16 11:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-15 12:25 [PATCH bpf] selftests/bpf: fix endianness issues in test_sysctl Ilya Leoshkevich
2019-08-15 20:35 ` Andrey Ignatov
2019-08-16 10:21   ` Ilya Leoshkevich
2019-08-16  0:05 ` Yonghong Song
2019-08-16 11:37   ` Ilya Leoshkevich [this message]
2019-08-17  0:06     ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=91EFE17B-3835-4679-8464-A76C885BCD46@linux.ibm.com \
    --to=iii@linux.ibm.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=gor@linux.ibm.com \
    --cc=heiko.carstens@de.ibm.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).