All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bpf: fix uapi bpf_prog_info fields alignment
@ 2019-06-04 11:39 Baruch Siach
  2019-06-04 12:04 ` Geert Uytterhoeven
  2019-06-04 15:16 ` Alexei Starovoitov
  0 siblings, 2 replies; 8+ messages in thread
From: Baruch Siach @ 2019-06-04 11:39 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann
  Cc: Martin KaFai Lau, Song Liu, Yonghong Song, netdev, bpf,
	Dmitry V . Levin, Baruch Siach, Jiri Olsa, Geert Uytterhoeven,
	Linus Torvalds

Merge commit 1c8c5a9d38f60 ("Merge
git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
applications") by taking the gpl_compatible 1-bit field definition from
commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
bpf_prog_info") as is. That breaks architectures with 16-bit alignment
like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
following fields.

Thanks to Dmitry V. Levin his analysis of this bug history.

Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Baruch Siach <baruch@tkos.co.il>
---
 include/uapi/linux/bpf.h       | 2 +-
 tools/include/uapi/linux/bpf.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 63e0cf66f01a..fe73829b5b1c 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3140,7 +3140,7 @@ struct bpf_prog_info {
 	__aligned_u64 map_ids;
 	char name[BPF_OBJ_NAME_LEN];
 	__u32 ifindex;
-	__u32 gpl_compatible:1;
+	__u32 gpl_compatible;
 	__u64 netns_dev;
 	__u64 netns_ino;
 	__u32 nr_jited_ksyms;
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 63e0cf66f01a..fe73829b5b1c 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -3140,7 +3140,7 @@ struct bpf_prog_info {
 	__aligned_u64 map_ids;
 	char name[BPF_OBJ_NAME_LEN];
 	__u32 ifindex;
-	__u32 gpl_compatible:1;
+	__u32 gpl_compatible;
 	__u64 netns_dev;
 	__u64 netns_ino;
 	__u32 nr_jited_ksyms;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-04 11:39 [PATCH] bpf: fix uapi bpf_prog_info fields alignment Baruch Siach
@ 2019-06-04 12:04 ` Geert Uytterhoeven
  2019-06-04 15:12   ` Baruch Siach
  2019-06-04 15:16 ` Alexei Starovoitov
  1 sibling, 1 reply; 8+ messages in thread
From: Geert Uytterhoeven @ 2019-06-04 12:04 UTC (permalink / raw)
  To: Baruch Siach
  Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, netdev, bpf, Dmitry V . Levin, Jiri Olsa,
	Linus Torvalds

Hi Baruch,

On Tue, Jun 4, 2019 at 1:40 PM Baruch Siach <baruch@tkos.co.il> wrote:
> Merge commit 1c8c5a9d38f60 ("Merge
> git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
> fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
> applications") by taking the gpl_compatible 1-bit field definition from
> commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
> bpf_prog_info") as is. That breaks architectures with 16-bit alignment
> like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
> following fields.
>
> Thanks to Dmitry V. Levin his analysis of this bug history.
>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Baruch Siach <baruch@tkos.co.il>

Thanks for your patch!

> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3140,7 +3140,7 @@ struct bpf_prog_info {
>         __aligned_u64 map_ids;
>         char name[BPF_OBJ_NAME_LEN];
>         __u32 ifindex;
> -       __u32 gpl_compatible:1;
> +       __u32 gpl_compatible;
>         __u64 netns_dev;
>         __u64 netns_ino;

Wouldn't it be better to change the types of the fields that require
8-byte alignment from __u64 to __aligned_u64, like is already used
for the map_ids fields?

Without that, some day people will need to add a new flag, and will
convert the 32-bit flag to a bitfield again to make space, reintroducing
the issue.

>         __u32 nr_jited_ksyms;
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index 63e0cf66f01a..fe73829b5b1c 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h
> @@ -3140,7 +3140,7 @@ struct bpf_prog_info {
>         __aligned_u64 map_ids;
>         char name[BPF_OBJ_NAME_LEN];
>         __u32 ifindex;
> -       __u32 gpl_compatible:1;
> +       __u32 gpl_compatible;
>         __u64 netns_dev;
>         __u64 netns_ino;

Same here.

>         __u32 nr_jited_ksyms;
> --
> 2.20.1
>


-- 
Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-04 12:04 ` Geert Uytterhoeven
@ 2019-06-04 15:12   ` Baruch Siach
  0 siblings, 0 replies; 8+ messages in thread
From: Baruch Siach @ 2019-06-04 15:12 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, netdev, bpf, Dmitry V . Levin, Jiri Olsa,
	Linus Torvalds

Hi Geert,

On Tue, Jun 04 2019, Geert Uytterhoeven wrote:
> On Tue, Jun 4, 2019 at 1:40 PM Baruch Siach <baruch@tkos.co.il> wrote:
>> Merge commit 1c8c5a9d38f60 ("Merge
>> git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
>> fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
>> applications") by taking the gpl_compatible 1-bit field definition from
>> commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
>> bpf_prog_info") as is. That breaks architectures with 16-bit alignment
>> like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
>> following fields.
>>
>> Thanks to Dmitry V. Levin his analysis of this bug history.
>>
>> Cc: Jiri Olsa <jolsa@kernel.org>
>> Cc: Daniel Borkmann <daniel@iogearbox.net>
>> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
>> Cc: Linus Torvalds <torvalds@linux-foundation.org>
>> Signed-off-by: Baruch Siach <baruch@tkos.co.il>
>
> Thanks for your patch!
>
>> --- a/include/uapi/linux/bpf.h
>> +++ b/include/uapi/linux/bpf.h
>> @@ -3140,7 +3140,7 @@ struct bpf_prog_info {
>>         __aligned_u64 map_ids;
>>         char name[BPF_OBJ_NAME_LEN];
>>         __u32 ifindex;
>> -       __u32 gpl_compatible:1;
>> +       __u32 gpl_compatible;
>>         __u64 netns_dev;
>>         __u64 netns_ino;
>
> Wouldn't it be better to change the types of the fields that require
> 8-byte alignment from __u64 to __aligned_u64, like is already used
> for the map_ids fields?
>
> Without that, some day people will need to add a new flag, and will
> convert the 32-bit flag to a bitfield again to make space, reintroducing
> the issue.

This is a minimal fix that restores the original fix of commit
36f9814a494. Would __aligned_u64 cause any negative side effect on
current ABI?

baruch

>>         __u32 nr_jited_ksyms;
>> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
>> index 63e0cf66f01a..fe73829b5b1c 100644
>> --- a/tools/include/uapi/linux/bpf.h
>> +++ b/tools/include/uapi/linux/bpf.h
>> @@ -3140,7 +3140,7 @@ struct bpf_prog_info {
>>         __aligned_u64 map_ids;
>>         char name[BPF_OBJ_NAME_LEN];
>>         __u32 ifindex;
>> -       __u32 gpl_compatible:1;
>> +       __u32 gpl_compatible;
>>         __u64 netns_dev;
>>         __u64 netns_ino;
>
> Same here.
>
>>         __u32 nr_jited_ksyms;

--
     http://baruch.siach.name/blog/                  ~. .~   Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
   - baruch@tkos.co.il - tel: +972.52.368.4656, http://www.tkos.co.il -

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-04 11:39 [PATCH] bpf: fix uapi bpf_prog_info fields alignment Baruch Siach
  2019-06-04 12:04 ` Geert Uytterhoeven
@ 2019-06-04 15:16 ` Alexei Starovoitov
  2019-06-04 15:23   ` Geert Uytterhoeven
  1 sibling, 1 reply; 8+ messages in thread
From: Alexei Starovoitov @ 2019-06-04 15:16 UTC (permalink / raw)
  To: Baruch Siach
  Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, Network Development, bpf, Dmitry V . Levin,
	Jiri Olsa, Geert Uytterhoeven, Linus Torvalds

On Tue, Jun 4, 2019 at 4:40 AM Baruch Siach <baruch@tkos.co.il> wrote:
>
> Merge commit 1c8c5a9d38f60 ("Merge
> git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
> fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
> applications") by taking the gpl_compatible 1-bit field definition from
> commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
> bpf_prog_info") as is. That breaks architectures with 16-bit alignment
> like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
> following fields.

The commit log is misleading and incorrect.
Since compiler makes it into 16-bit field, it's a compiler bug.
u32 in C should stay as u32 regardless of architecture.

> Thanks to Dmitry V. Levin his analysis of this bug history.
>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Baruch Siach <baruch@tkos.co.il>
> ---
>  include/uapi/linux/bpf.h       | 2 +-
>  tools/include/uapi/linux/bpf.h | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 63e0cf66f01a..fe73829b5b1c 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3140,7 +3140,7 @@ struct bpf_prog_info {
>         __aligned_u64 map_ids;
>         char name[BPF_OBJ_NAME_LEN];
>         __u32 ifindex;
> -       __u32 gpl_compatible:1;
> +       __u32 gpl_compatible;
>         __u64 netns_dev;

No need.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-04 15:16 ` Alexei Starovoitov
@ 2019-06-04 15:23   ` Geert Uytterhoeven
  2019-06-04 15:30     ` Alexei Starovoitov
  0 siblings, 1 reply; 8+ messages in thread
From: Geert Uytterhoeven @ 2019-06-04 15:23 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Baruch Siach, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Song Liu, Yonghong Song, Network Development,
	bpf, Dmitry V . Levin, Jiri Olsa, Linus Torvalds

Hi Alexei,

On Tue, Jun 4, 2019 at 5:17 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
> On Tue, Jun 4, 2019 at 4:40 AM Baruch Siach <baruch@tkos.co.il> wrote:
> > Merge commit 1c8c5a9d38f60 ("Merge
> > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
> > fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
> > applications") by taking the gpl_compatible 1-bit field definition from
> > commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
> > bpf_prog_info") as is. That breaks architectures with 16-bit alignment
> > like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
> > following fields.
>
> The commit log is misleading and incorrect.
> Since compiler makes it into 16-bit field, it's a compiler bug.
> u32 in C should stay as u32 regardless of architecture.

C99 says (Section 6.7.2.1, Structure and union specifiers, Semantics)

    10  An implementation may allocate any addressable storage unit
        large enough to hold a bit-field.

$ cat hello.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

struct x {
        unsigned int bit : 1;
        unsigned char byte;
};

int main(int argc, char *argv[])
{
        struct x x;

        printf("byte is at offset %zu\n", (uintptr_t)&x.byte - (uintptr_t)&x);
        printf("sizeof(x) = %zu\n", sizeof(x));
        exit(0);
}
$ gcc -Wall hello.c -o hello && ./hello
byte is at offset 1
sizeof(x) = 4
$ uname -m
x86_64

So the compiler allocates a single byte, even on a 64-bit platform!
The gap is solely determined by the alignment rule for the
successive field.

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-04 15:23   ` Geert Uytterhoeven
@ 2019-06-04 15:30     ` Alexei Starovoitov
  2019-06-05  4:06       ` Baruch Siach
  0 siblings, 1 reply; 8+ messages in thread
From: Alexei Starovoitov @ 2019-06-04 15:30 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Baruch Siach, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Song Liu, Yonghong Song, Network Development,
	bpf, Dmitry V . Levin, Jiri Olsa, Linus Torvalds

On Tue, Jun 04, 2019 at 05:23:46PM +0200, Geert Uytterhoeven wrote:
> Hi Alexei,
> 
> On Tue, Jun 4, 2019 at 5:17 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> > On Tue, Jun 4, 2019 at 4:40 AM Baruch Siach <baruch@tkos.co.il> wrote:
> > > Merge commit 1c8c5a9d38f60 ("Merge
> > > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
> > > fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
> > > applications") by taking the gpl_compatible 1-bit field definition from
> > > commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
> > > bpf_prog_info") as is. That breaks architectures with 16-bit alignment
> > > like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
> > > following fields.
> >
> > The commit log is misleading and incorrect.
> > Since compiler makes it into 16-bit field, it's a compiler bug.
> > u32 in C should stay as u32 regardless of architecture.
> 
> C99 says (Section 6.7.2.1, Structure and union specifiers, Semantics)
> 
>     10  An implementation may allocate any addressable storage unit
>         large enough to hold a bit-field.
> 
> $ cat hello.c
> #include <stdio.h>
> #include <stdint.h>
> #include <stdlib.h>
> 
> struct x {
>         unsigned int bit : 1;
>         unsigned char byte;
> };
> 
> int main(int argc, char *argv[])
> {
>         struct x x;
> 
>         printf("byte is at offset %zu\n", (uintptr_t)&x.byte - (uintptr_t)&x);
>         printf("sizeof(x) = %zu\n", sizeof(x));
>         exit(0);
> }
> $ gcc -Wall hello.c -o hello && ./hello
> byte is at offset 1
> sizeof(x) = 4
> $ uname -m
> x86_64
> 
> So the compiler allocates a single byte, even on a 64-bit platform!
> The gap is solely determined by the alignment rule for the
> successive field.

argh. then we need something like this:
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 7c6aef253173..a2ac0b961251 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3174,6 +3174,7 @@ struct bpf_prog_info {
        char name[BPF_OBJ_NAME_LEN];
        __u32 ifindex;
        __u32 gpl_compatible:1;
+       __u32 :31;
        __u64 netns_dev;
        __u64 netns_ino;
        __u32 nr_jited_ksyms;


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-04 15:30     ` Alexei Starovoitov
@ 2019-06-05  4:06       ` Baruch Siach
  2019-06-05  4:13         ` Alexei Starovoitov
  0 siblings, 1 reply; 8+ messages in thread
From: Baruch Siach @ 2019-06-05  4:06 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Geert Uytterhoeven, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Song Liu, Yonghong Song, Network Development,
	bpf, Dmitry V . Levin, Jiri Olsa, Linus Torvalds, Arnd Bergmann,
	linux-arch

Hi Alexei,

(Adding Arnd and linux-arch to Cc)

On Tue, Jun 04, 2019 at 08:30:29AM -0700, Alexei Starovoitov wrote:
> On Tue, Jun 04, 2019 at 05:23:46PM +0200, Geert Uytterhoeven wrote:
> > On Tue, Jun 4, 2019 at 5:17 PM Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > > On Tue, Jun 4, 2019 at 4:40 AM Baruch Siach <baruch@tkos.co.il> wrote:
> > > > Merge commit 1c8c5a9d38f60 ("Merge
> > > > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
> > > > fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
> > > > applications") by taking the gpl_compatible 1-bit field definition from
> > > > commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
> > > > bpf_prog_info") as is. That breaks architectures with 16-bit alignment
> > > > like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
> > > > following fields.
> > >
> > > The commit log is misleading and incorrect.
> > > Since compiler makes it into 16-bit field, it's a compiler bug.
> > > u32 in C should stay as u32 regardless of architecture.
> > 
> > C99 says (Section 6.7.2.1, Structure and union specifiers, Semantics)
> > 
> >     10  An implementation may allocate any addressable storage unit
> >         large enough to hold a bit-field.
> > 
> > $ cat hello.c
> > #include <stdio.h>
> > #include <stdint.h>
> > #include <stdlib.h>
> > 
> > struct x {
> >         unsigned int bit : 1;
> >         unsigned char byte;
> > };
> > 
> > int main(int argc, char *argv[])
> > {
> >         struct x x;
> > 
> >         printf("byte is at offset %zu\n", (uintptr_t)&x.byte - (uintptr_t)&x);
> >         printf("sizeof(x) = %zu\n", sizeof(x));
> >         exit(0);
> > }
> > $ gcc -Wall hello.c -o hello && ./hello
> > byte is at offset 1
> > sizeof(x) = 4
> > $ uname -m
> > x86_64
> > 
> > So the compiler allocates a single byte, even on a 64-bit platform!
> > The gap is solely determined by the alignment rule for the
> > successive field.
> 
> argh. then we need something like this:
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 7c6aef253173..a2ac0b961251 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3174,6 +3174,7 @@ struct bpf_prog_info {
>         char name[BPF_OBJ_NAME_LEN];
>         __u32 ifindex;
>         __u32 gpl_compatible:1;
> +       __u32 :31;
>         __u64 netns_dev;
>         __u64 netns_ino;
>         __u32 nr_jited_ksyms;

Is that guaranteed to work across platforms/compilers? Maybe an anonymous 
union would be safer? Something like:

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 63e0cf66f01a..06c9fb314ea5 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3140,7 +3140,10 @@ struct bpf_prog_info {
 	__aligned_u64 map_ids;
 	char name[BPF_OBJ_NAME_LEN];
 	__u32 ifindex;
-	__u32 gpl_compatible:1;
+	union {
+		__u32 gpl_compatible:1;
+		__u32 pad;
+	};
 	__u64 netns_dev;
 	__u64 netns_ino;
 	__u32 nr_jited_ksyms;

baruch

-- 
     http://baruch.siach.name/blog/                  ~. .~   Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
   - baruch@tkos.co.il - tel: +972.2.679.5364, http://www.tkos.co.il -

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] bpf: fix uapi bpf_prog_info fields alignment
  2019-06-05  4:06       ` Baruch Siach
@ 2019-06-05  4:13         ` Alexei Starovoitov
  0 siblings, 0 replies; 8+ messages in thread
From: Alexei Starovoitov @ 2019-06-05  4:13 UTC (permalink / raw)
  To: Baruch Siach
  Cc: Geert Uytterhoeven, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Song Liu, Yonghong Song, Network Development,
	bpf, Dmitry V . Levin, Jiri Olsa, Linus Torvalds, Arnd Bergmann,
	linux-arch

On Tue, Jun 4, 2019 at 9:06 PM Baruch Siach <baruch@tkos.co.il> wrote:
>
> Hi Alexei,
>
> (Adding Arnd and linux-arch to Cc)
>
> On Tue, Jun 04, 2019 at 08:30:29AM -0700, Alexei Starovoitov wrote:
> > On Tue, Jun 04, 2019 at 05:23:46PM +0200, Geert Uytterhoeven wrote:
> > > On Tue, Jun 4, 2019 at 5:17 PM Alexei Starovoitov
> > > <alexei.starovoitov@gmail.com> wrote:
> > > > On Tue, Jun 4, 2019 at 4:40 AM Baruch Siach <baruch@tkos.co.il> wrote:
> > > > > Merge commit 1c8c5a9d38f60 ("Merge
> > > > > git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next") undid the
> > > > > fix from commit 36f9814a494 ("bpf: fix uapi hole for 32 bit compat
> > > > > applications") by taking the gpl_compatible 1-bit field definition from
> > > > > commit b85fab0e67b162 ("bpf: Add gpl_compatible flag to struct
> > > > > bpf_prog_info") as is. That breaks architectures with 16-bit alignment
> > > > > like m68k. Widen gpl_compatible to 32-bit to restore alignment of the
> > > > > following fields.
> > > >
> > > > The commit log is misleading and incorrect.
> > > > Since compiler makes it into 16-bit field, it's a compiler bug.
> > > > u32 in C should stay as u32 regardless of architecture.
> > >
> > > C99 says (Section 6.7.2.1, Structure and union specifiers, Semantics)
> > >
> > >     10  An implementation may allocate any addressable storage unit
> > >         large enough to hold a bit-field.
> > >
> > > $ cat hello.c
> > > #include <stdio.h>
> > > #include <stdint.h>
> > > #include <stdlib.h>
> > >
> > > struct x {
> > >         unsigned int bit : 1;
> > >         unsigned char byte;
> > > };
> > >
> > > int main(int argc, char *argv[])
> > > {
> > >         struct x x;
> > >
> > >         printf("byte is at offset %zu\n", (uintptr_t)&x.byte - (uintptr_t)&x);
> > >         printf("sizeof(x) = %zu\n", sizeof(x));
> > >         exit(0);
> > > }
> > > $ gcc -Wall hello.c -o hello && ./hello
> > > byte is at offset 1
> > > sizeof(x) = 4
> > > $ uname -m
> > > x86_64
> > >
> > > So the compiler allocates a single byte, even on a 64-bit platform!
> > > The gap is solely determined by the alignment rule for the
> > > successive field.
> >
> > argh. then we need something like this:
> > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> > index 7c6aef253173..a2ac0b961251 100644
> > --- a/include/uapi/linux/bpf.h
> > +++ b/include/uapi/linux/bpf.h
> > @@ -3174,6 +3174,7 @@ struct bpf_prog_info {
> >         char name[BPF_OBJ_NAME_LEN];
> >         __u32 ifindex;
> >         __u32 gpl_compatible:1;
> > +       __u32 :31;
> >         __u64 netns_dev;
> >         __u64 netns_ino;
> >         __u32 nr_jited_ksyms;
>
> Is that guaranteed to work across platforms/compilers? Maybe an anonymous

it works on archs I have access to.
please try it on yours.

> union would be safer? Something like:
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 63e0cf66f01a..06c9fb314ea5 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3140,7 +3140,10 @@ struct bpf_prog_info {
>         __aligned_u64 map_ids;
>         char name[BPF_OBJ_NAME_LEN];
>         __u32 ifindex;
> -       __u32 gpl_compatible:1;
> +       union {
> +               __u32 gpl_compatible:1;
> +               __u32 pad;
> +       };

that's a potential uapi breakage.
We use anonymous bitfields in other places in uapi.
If some gcc on some arch doesn't understand them
it's a bigger breakage and such gcc would need
to be fixed regardless.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-06-05  4:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-04 11:39 [PATCH] bpf: fix uapi bpf_prog_info fields alignment Baruch Siach
2019-06-04 12:04 ` Geert Uytterhoeven
2019-06-04 15:12   ` Baruch Siach
2019-06-04 15:16 ` Alexei Starovoitov
2019-06-04 15:23   ` Geert Uytterhoeven
2019-06-04 15:30     ` Alexei Starovoitov
2019-06-05  4:06       ` Baruch Siach
2019-06-05  4:13         ` Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.