public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH] net: ipv6: fix alignment errors on ARM
@ 2023-01-18 17:52 Sergei Antonov
  2023-01-19  8:18 ` Vyacheslav V. Mitrofanov
  2023-02-03 18:20 ` Tom Rini
  0 siblings, 2 replies; 5+ messages in thread
From: Sergei Antonov @ 2023-01-18 17:52 UTC (permalink / raw)
  To: u-boot, v.v.mitrofanov; +Cc: Sergei Antonov

Commands "ping6" and "tftpboot ... -ipv6" did not work on ARM because
machine code expects 4-byte alignment and some structures from net6.h
are not aligned in memory.

Fix by adding __packed, since it is already used in this file.

Signed-off-by: Sergei Antonov <saproj@gmail.com>
---
 include/net6.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/net6.h b/include/net6.h
index 9b3de028e6dc..2d7c5a096046 100644
--- a/include/net6.h
+++ b/include/net6.h
@@ -24,7 +24,7 @@ struct in6_addr {
 #define s6_addr		in6_u.u6_addr8
 #define s6_addr16	in6_u.u6_addr16
 #define s6_addr32	in6_u.u6_addr32
-};
+} __packed;
 
 #define IN6ADDRSZ	sizeof(struct in6_addr)
 #define INETHADDRSZ	sizeof(net_ethaddr)
@@ -62,7 +62,7 @@ struct ip6_hdr {
 	u8		hop_limit;
 	struct in6_addr	saddr;
 	struct in6_addr	daddr;
-};
+} __packed;
 #define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
 
 /* struct udp_hdr - User Datagram Protocol header */
@@ -164,7 +164,7 @@ struct icmp6hdr {
 #define icmp6_addrconf_managed	icmp6_dataun.u_nd_ra.managed
 #define icmp6_addrconf_other	icmp6_dataun.u_nd_ra.other
 #define icmp6_rt_lifetime	icmp6_dataun.u_nd_ra.rt_lifetime
-};
+} __packed;
 
 extern struct in6_addr const net_null_addr_ip6;	/* NULL IPv6 address */
 extern struct in6_addr net_gateway6;	/* Our gateways IPv6 address */
-- 
2.34.1


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

* Re: [PATCH] net: ipv6: fix alignment errors on ARM
  2023-01-18 17:52 [PATCH] net: ipv6: fix alignment errors on ARM Sergei Antonov
@ 2023-01-19  8:18 ` Vyacheslav V. Mitrofanov
  2023-01-19 11:05   ` Sergei Antonov
  2023-02-03 18:20 ` Tom Rini
  1 sibling, 1 reply; 5+ messages in thread
From: Vyacheslav V. Mitrofanov @ 2023-01-19  8:18 UTC (permalink / raw)
  To: sjg@chromium.org, u-boot@lists.denx.de, rfried.dev@gmail.com,
	saproj@gmail.com

On Wed, 2023-01-18 at 20:52 +0300, Sergei Antonov wrote:
> Commands "ping6" and "tftpboot ... -ipv6" did not work on ARM because
> machine code expects 4-byte alignment and some structures from net6.h
> are not aligned in memory.
> 
> Fix by adding __packed, since it is already used in this file.
> 
> Signed-off-by: Sergei Antonov <saproj@gmail.com>
> ---
>  include/net6.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/net6.h b/include/net6.h
> index 9b3de028e6dc..2d7c5a096046 100644
> --- a/include/net6.h
> +++ b/include/net6.h
> @@ -24,7 +24,7 @@ struct in6_addr {
>  #define s6_addr                in6_u.u6_addr8
>  #define s6_addr16      in6_u.u6_addr16
>  #define s6_addr32      in6_u.u6_addr32
> -};
> +} __packed;
> 
>  #define IN6ADDRSZ      sizeof(struct in6_addr)
>  #define INETHADDRSZ    sizeof(net_ethaddr)
> @@ -62,7 +62,7 @@ struct ip6_hdr {
>         u8              hop_limit;
>         struct in6_addr saddr;
>         struct in6_addr daddr;
> -};
> +} __packed;
>  #define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
> 
>  /* struct udp_hdr - User Datagram Protocol header */
> @@ -164,7 +164,7 @@ struct icmp6hdr {
>  #define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed
>  #define icmp6_addrconf_other   icmp6_dataun.u_nd_ra.other
>  #define icmp6_rt_lifetime      icmp6_dataun.u_nd_ra.rt_lifetime
> -};
> +} __packed;
> 
>  extern struct in6_addr const net_null_addr_ip6;        /* NULL IPv6
> address */
>  extern struct in6_addr net_gateway6;   /* Our gateways IPv6 address
> */
> --
> 2.34.1
> 
Hello, Sergei!

I didn't get you a little bit. You mean holes between fields of
structures or alignment of the beginning?

Frankly speaking I always thought that in that kind of structure like
below it is not necessary. To be honest I disassembled the same code on
arm and there were no holes. There is no "pack" attribute in linux too.
https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/in6.h#L33

May be it could be other kind of error? 
union
{
u8      u6_addr8[16];
__be16  u6_addr16[8];
__be32  u6_addr32[4];
};
If you don't mind I add Ramon and Simon...

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

* Re: [PATCH] net: ipv6: fix alignment errors on ARM
  2023-01-19  8:18 ` Vyacheslav V. Mitrofanov
@ 2023-01-19 11:05   ` Sergei Antonov
  2023-01-19 11:56     ` Vyacheslav V. Mitrofanov
  0 siblings, 1 reply; 5+ messages in thread
From: Sergei Antonov @ 2023-01-19 11:05 UTC (permalink / raw)
  To: Vyacheslav V. Mitrofanov
  Cc: sjg@chromium.org, u-boot@lists.denx.de, rfried.dev@gmail.com

On Thu, 19 Jan 2023 at 11:18, Vyacheslav V. Mitrofanov
<v.v.mitrofanov@yadro.com> wrote:
>
> On Wed, 2023-01-18 at 20:52 +0300, Sergei Antonov wrote:
> > Commands "ping6" and "tftpboot ... -ipv6" did not work on ARM because
> > machine code expects 4-byte alignment and some structures from net6.h
> > are not aligned in memory.
> >
> > Fix by adding __packed, since it is already used in this file.
> >
> > Signed-off-by: Sergei Antonov <saproj@gmail.com>
> > ---
> >  include/net6.h | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/net6.h b/include/net6.h
> > index 9b3de028e6dc..2d7c5a096046 100644
> > --- a/include/net6.h
> > +++ b/include/net6.h
> > @@ -24,7 +24,7 @@ struct in6_addr {
> >  #define s6_addr                in6_u.u6_addr8
> >  #define s6_addr16      in6_u.u6_addr16
> >  #define s6_addr32      in6_u.u6_addr32
> > -};
> > +} __packed;
> >
> >  #define IN6ADDRSZ      sizeof(struct in6_addr)
> >  #define INETHADDRSZ    sizeof(net_ethaddr)
> > @@ -62,7 +62,7 @@ struct ip6_hdr {
> >         u8              hop_limit;
> >         struct in6_addr saddr;
> >         struct in6_addr daddr;
> > -};
> > +} __packed;
> >  #define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
> >
> >  /* struct udp_hdr - User Datagram Protocol header */
> > @@ -164,7 +164,7 @@ struct icmp6hdr {
> >  #define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed
> >  #define icmp6_addrconf_other   icmp6_dataun.u_nd_ra.other
> >  #define icmp6_rt_lifetime      icmp6_dataun.u_nd_ra.rt_lifetime
> > -};
> > +} __packed;
> >
> >  extern struct in6_addr const net_null_addr_ip6;        /* NULL IPv6
> > address */
> >  extern struct in6_addr net_gateway6;   /* Our gateways IPv6 address
> > */
> > --
> > 2.34.1
> >
> Hello, Sergei!
>
> I didn't get you a little bit. You mean holes between fields of
> structures or alignment of the beginning?

No, it is not about holes between fields. It is about the address at
which a structure is placed in memory. In combination with store
merging it leads to unaligned memory access. At least for struct
ip6_hdr, which I studied using assembly listing files. The struct
ip6_hdr is not placed at 4-byte aligned address because it is 14 bytes
off from the beginning of the Ethernet packet.

Also see how the corresponding IPv4 struct ip_hdr from net.h was fixed
by commit 704f3acfcf553. I should have referred to it in my commit
description.

> Frankly speaking I always thought that in that kind of structure like
> below it is not necessary. To be honest I disassembled the same code on
> arm and there were no holes.

The following code from net6.c

int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
                int nextheader, int hoplimit, int payload_len)
{
        struct ip6_hdr *ip6 = (struct ip6_hdr *)xip;

        ip6->version = 6;
        ip6->priority = 0;
        ip6->flow_lbl[0] = 0;
        ip6->flow_lbl[1] = 0;
        ip6->flow_lbl[2] = 0;

is optimized into:
801ab138:       e3a02060        mov     r2, #96 @ 0x60
801ab13c:       e1a04000        mov     r4, r0
801ab140:       e5802000        str     r2, [r0]

The last instruction does all 5 different assignments from C code at once.

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

* Re: [PATCH] net: ipv6: fix alignment errors on ARM
  2023-01-19 11:05   ` Sergei Antonov
@ 2023-01-19 11:56     ` Vyacheslav V. Mitrofanov
  0 siblings, 0 replies; 5+ messages in thread
From: Vyacheslav V. Mitrofanov @ 2023-01-19 11:56 UTC (permalink / raw)
  To: saproj@gmail.com
  Cc: sjg@chromium.org, u-boot@lists.denx.de, rfried.dev@gmail.com

On Thu, 2023-01-19 at 14:05 +0300, Sergei Antonov wrote:
> 
> On Thu, 19 Jan 2023 at 11:18, Vyacheslav V. Mitrofanov
> <v.v.mitrofanov@yadro.com> wrote:
> > On Wed, 2023-01-18 at 20:52 +0300, Sergei Antonov wrote:
> > > Commands "ping6" and "tftpboot ... -ipv6" did not work on ARM
> > > because
> > > machine code expects 4-byte alignment and some structures from
> > > net6.h
> > > are not aligned in memory.
> > > 
> > > Fix by adding __packed, since it is already used in this file.
> > > 
> > > Signed-off-by: Sergei Antonov <saproj@gmail.com>
> > > ---
> > >  include/net6.h | 6 +++---
> > >  1 file changed, 3 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/include/net6.h b/include/net6.h
> > > index 9b3de028e6dc..2d7c5a096046 100644
> > > --- a/include/net6.h
> > > +++ b/include/net6.h
> > > @@ -24,7 +24,7 @@ struct in6_addr {
> > >  #define s6_addr                in6_u.u6_addr8
> > >  #define s6_addr16      in6_u.u6_addr16
> > >  #define s6_addr32      in6_u.u6_addr32
> > > -};
> > > +} __packed;
> > > 
> > >  #define IN6ADDRSZ      sizeof(struct in6_addr)
> > >  #define INETHADDRSZ    sizeof(net_ethaddr)
> > > @@ -62,7 +62,7 @@ struct ip6_hdr {
> > >         u8              hop_limit;
> > >         struct in6_addr saddr;
> > >         struct in6_addr daddr;
> > > -};
> > > +} __packed;
> > >  #define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
> > > 
> > >  /* struct udp_hdr - User Datagram Protocol header */
> > > @@ -164,7 +164,7 @@ struct icmp6hdr {
> > >  #define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed
> > >  #define icmp6_addrconf_other   icmp6_dataun.u_nd_ra.other
> > >  #define icmp6_rt_lifetime      icmp6_dataun.u_nd_ra.rt_lifetime
> > > -};
> > > +} __packed;
> > > 
> > >  extern struct in6_addr const net_null_addr_ip6;        /* NULL
> > > IPv6
> > > address */
> > >  extern struct in6_addr net_gateway6;   /* Our gateways IPv6
> > > address
> > > */
> > > --
> > > 2.34.1
> > > 
> > Hello, Sergei!
> > 
> > I didn't get you a little bit. You mean holes between fields of
> > structures or alignment of the beginning?
> 
> No, it is not about holes between fields. It is about the address at
> which a structure is placed in memory. In combination with store
> merging it leads to unaligned memory access. At least for struct
> ip6_hdr, which I studied using assembly listing files. The struct
> ip6_hdr is not placed at 4-byte aligned address because it is 14
> bytes
> off from the beginning of the Ethernet packet.
> 
> Also see how the corresponding IPv4 struct ip_hdr from net.h was
> fixed
> by commit 704f3acfcf553. I should have referred to it in my commit
> description.
> 
I got you. It makes sense.
Thanks!

Reviewed-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>

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

* Re: [PATCH] net: ipv6: fix alignment errors on ARM
  2023-01-18 17:52 [PATCH] net: ipv6: fix alignment errors on ARM Sergei Antonov
  2023-01-19  8:18 ` Vyacheslav V. Mitrofanov
@ 2023-02-03 18:20 ` Tom Rini
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Rini @ 2023-02-03 18:20 UTC (permalink / raw)
  To: Sergei Antonov; +Cc: u-boot, v.v.mitrofanov

[-- Attachment #1: Type: text/plain, Size: 483 bytes --]

On Wed, Jan 18, 2023 at 08:52:18PM +0300, Sergei Antonov wrote:

> Commands "ping6" and "tftpboot ... -ipv6" did not work on ARM because
> machine code expects 4-byte alignment and some structures from net6.h
> are not aligned in memory.
> 
> Fix by adding __packed, since it is already used in this file.
> 
> Signed-off-by: Sergei Antonov <saproj@gmail.com>
> Reviewed-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

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

end of thread, other threads:[~2023-02-03 18:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-18 17:52 [PATCH] net: ipv6: fix alignment errors on ARM Sergei Antonov
2023-01-19  8:18 ` Vyacheslav V. Mitrofanov
2023-01-19 11:05   ` Sergei Antonov
2023-01-19 11:56     ` Vyacheslav V. Mitrofanov
2023-02-03 18:20 ` Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox