From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Y Song <ys114321@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, netdev <netdev@vger.kernel.org>,
gor@linux.ibm.com, heiko.carstens@de.ibm.com
Subject: Re: [PATCH bpf] bpf: fix narrower loads on s390
Date: Wed, 17 Jul 2019 22:51:43 +0200 [thread overview]
Message-ID: <B91434A8-6056-49E2-852D-6DE5FFD53B29@linux.ibm.com> (raw)
In-Reply-To: <CAH3MdRV-qsJnyZVV1GnxRZ4=3KXTvKSgETp90fyevxycmAiHmA@mail.gmail.com>
> Am 17.07.2019 um 18:25 schrieb Y Song <ys114321@gmail.com>:
>
> On Wed, Jul 17, 2019 at 3:36 AM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>>
>>
>> Here is a better one: len=0x11223344 and we would like to do
>> ((u8 *)&len)[3].
>>
>> len is represented as `11 22 33 44` in memory, so the desired result is
>> 0x44. It can be obtained by doing (*(u32 *)&len) & 0xff, but today the
>> verifier does ((*(u32 *)&len) >> 24) & 0xff instead.
>
> What you described above for the memory layout all makes sense.
> The root cause is for big endian, we should do *((u8 *)&len + 3).
> This is exactly what macros in test_pkt_md_access.c tries to do.
>
> if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
> #define TEST_FIELD(TYPE, FIELD, MASK) \
> { \
> TYPE tmp = *(volatile TYPE *)&skb->FIELD; \
> if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK)) \
> return TC_ACT_SHOT; \
> }
> #else
> #define TEST_FIELD_OFFSET(a, b) ((sizeof(a) - sizeof(b)) / sizeof(b))
> #define TEST_FIELD(TYPE, FIELD, MASK) \
> { \
> TYPE tmp = *((volatile TYPE *)&skb->FIELD + \
> TEST_FIELD_OFFSET(skb->FIELD, TYPE)); \
> if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK)) \
> return TC_ACT_SHOT; \
> }
> #endif
>
> Could you check whether your __BYTE_ORDER__ is set
> correctly or not for this case? You may need to tweak Makefile
> if you are doing cross compilation, I am not sure how as I
> did not have environment.
I’m building natively on s390.
Here is the (formatted) preprocessed C code for the first condition:
{
__u8 tmp = *((volatile __u8 *)&skb->len +
((sizeof(skb->len) - sizeof(__u8)) / sizeof(__u8)));
if (tmp != ((*(volatile __u32 *)&skb->len) & 0xFF)) return 2;
};
So I believe the endianness is chosen correctly.
Here is the clang-generated BPF bytecode for the first condition:
# llvm-objdump -d test_pkt_md_access.o
0000000000000000 process:
0: 71 21 00 03 00 00 00 00 r2 = *(u8 *)(r1 + 3)
1: 61 31 00 00 00 00 00 00 r3 = *(u32 *)(r1 + 0)
2: 57 30 00 00 00 00 00 ff r3 &= 255
3: 5d 23 00 1d 00 00 00 00 if r2 != r3 goto +29 <LBB0_10>
This also looks good to me.
Finally, here is the verifier-generated BPF bytecode:
# bpftool prog dump xlated id 14
; TEST_FIELD(__u8, len, 0xFF);
0: (61) r2 = *(u32 *)(r1 +104)
1: (bc) w2 = w2
2: (74) w2 >>= 24
3: (bc) w2 = w2
4: (54) w2 &= 255
5: (bc) w2 = w2
Here we can see the shift that I'm referring to. I believe we should
translate *(u8 *)(r1 + 3) in this case without this shift on big-endian
machines.
Best regards,
Ilya
next prev parent reply other threads:[~2019-07-17 20:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-16 11:59 [PATCH bpf] bpf: fix narrower loads on s390 Ilya Leoshkevich
2019-07-17 5:08 ` Y Song
2019-07-17 5:11 ` Y Song
2019-07-17 9:21 ` Ilya Leoshkevich
2019-07-17 10:36 ` Ilya Leoshkevich
2019-07-17 16:25 ` Y Song
2019-07-17 20:51 ` Ilya Leoshkevich [this message]
2019-07-17 22:23 ` Y 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=B91434A8-6056-49E2-852D-6DE5FFD53B29@linux.ibm.com \
--to=iii@linux.ibm.com \
--cc=bpf@vger.kernel.org \
--cc=gor@linux.ibm.com \
--cc=heiko.carstens@de.ibm.com \
--cc=netdev@vger.kernel.org \
--cc=ys114321@gmail.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).