BPF Archive on lore.kernel.org
 help / color / 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] bpf: fix accessing bpf_sysctl.file_pos on s390
Date: Fri, 16 Aug 2019 12:34:41 +0200
Message-ID: <AA7D06F3-907C-434D-8387-9481FF99BC4C@linux.ibm.com> (raw)
In-Reply-To: <515ad8ab-9ded-5573-b2c5-37ee9c23dd6e@fb.com>

> Am 16.08.2019 um 01:01 schrieb Yonghong Song <yhs@fb.com>:
> 
> 
> 
> On 8/15/19 4:20 AM, Ilya Leoshkevich wrote:
>> "ctx:file_pos sysctl:read write ok" fails on s390 with "Read value  !=
>> nux". This is because verifier rewrites a complete 32-bit
>> bpf_sysctl.file_pos update to a partial update of the first 32 bits of
>> 64-bit *bpf_sysctl_kern.ppos, which is not correct on big-endian
>> systems.
>> 
>> Fix by using an offset on big-endian systems.
>> 
>> Ditto for bpf_sysctl.file_pos reads. Currently the test does not detect
>> a problem there, since it expects to see 0, which it gets with high
>> probability in error cases, so change it to seek to offset 3 and expect
>> 3 in bpf_sysctl.file_pos.
>> 
>> Fixes: e1550bfe0de4 ("bpf: Add file_pos field to bpf_sysctl ctx")
>> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
>> ---
>>  include/linux/filter.h                    | 10 ++++++++++
>>  kernel/bpf/cgroup.c                       |  9 +++++++--
>>  tools/testing/selftests/bpf/test_sysctl.c |  9 ++++++++-
>>  3 files changed, 25 insertions(+), 3 deletions(-)
>> 
>> diff --git a/include/linux/filter.h b/include/linux/filter.h
>> index 92c6e31fb008..94e81c56d81c 100644
>> --- a/include/linux/filter.h
>> +++ b/include/linux/filter.h
>> @@ -760,6 +760,16 @@ bpf_ctx_narrow_load_shift(u32 off, u32 size, u32 size_default)
>>  #endif
>>  }
>> 
>> +static inline s16
>> +bpf_ctx_narrow_access_offset(size_t variable_size, size_t access_size)
>> +{
>> +#ifdef __LITTLE_ENDIAN
>> +	return 0;
>> +#else
>> +	return variable_size - access_size;
>> +#endif
>> +}
> 
> The change looks correct to me.
> But now in include/linux/filter.h we have to macros:
> 
> static inline u8
> bpf_ctx_narrow_load_shift(u32 off, u32 size, u32 size_default)
> {
>         u8 load_off = off & (size_default - 1);
> 
> #ifdef __LITTLE_ENDIAN
>         return load_off * 8;
> #else
>         return (size_default - (load_off + size)) * 8;
> #endif
> }
> 
> static inline s16
> bpf_ctx_narrow_access_offset(size_t variable_size, size_t access_size)
> {
> #ifdef __LITTLE_ENDIAN
>         return 0;
> #else
>         return variable_size - access_size;
> #endif
> }
> 
> It would be good if we can have ifdef __LITTLE_ENDIAN only in one place.
> How about something like below:
> 
> static inline u8
> bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 size_default)
> {
>         u8 access_off = off & (size_default - 1);
> 
> #ifdef __LITTLE_ENDIAN
>         return access_off;
> #else
>         return size_default - (access_off + size);
> #endif
> }
> 
> static inline u8
> bpf_ctx_narrow_load_shift(u32 off, u32 size, u32 size_default)
> {
>         return bpf_ctx_narrow_access_offset(off, size, size_default) * 8;
> }

This does indeed look better, thanks! In this case, we don't even need
bpf_ctx_narrow_load_shift() anymore, since doing

u8 shift = bpf_ctx_narrow_access_offset(
       off, size, size_default) * 8;

directly is quite readable. I will test and send a v2.

      reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-15 11:20 Ilya Leoshkevich
2019-08-15 19:58 ` Andrey Ignatov
2019-08-15 23:01 ` Yonghong Song
2019-08-16 10:34   ` Ilya Leoshkevich [this message]

Reply instructions:

You may reply publically 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=AA7D06F3-907C-434D-8387-9481FF99BC4C@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

BPF Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/bpf/0 bpf/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 bpf bpf/ https://lore.kernel.org/bpf \
		bpf@vger.kernel.org bpf@archiver.kernel.org
	public-inbox-index bpf


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.bpf


AGPL code for this site: git clone https://public-inbox.org/ public-inbox