linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: scott.bambrough@linaro.org (Scott Bambrough)
To: linux-arm-kernel@lists.infradead.org
Subject: alignment faults in 3.6
Date: Tue, 09 Oct 2012 10:05:52 -0400	[thread overview]
Message-ID: <50742F40.4050704@linaro.org> (raw)
In-Reply-To: <506F0454.3070304@gmail.com>

On 12-10-05 12:01 PM, Rob Herring wrote:
> On 10/05/2012 08:51 AM, Mikael Pettersson wrote:
>> Rob Herring writes:
>>   > On 10/05/2012 03:24 AM, Russell King - ARM Linux wrote:
>>   > > On Fri, Oct 05, 2012 at 09:20:56AM +0100, Mans Rullgard wrote:
>>   > >> On 5 October 2012 08:12, Russell King - ARM Linux
>>   > >> <linux@arm.linux.org.uk> wrote:
>>   > >>> On Fri, Oct 05, 2012 at 03:25:16AM +0100, Mans Rullgard wrote:
>>   > >>>> On 5 October 2012 02:56, Rob Herring <robherring2@gmail.com> wrote:
>>   > >>>>> This struct is the IP header, so a struct ptr is just set to the
>>   > >>>>> beginning of the received data. Since ethernet headers are 14 bytes,
>>   > >>>>> often the IP header is not aligned unless the NIC can place the frame at
>>   > >>>>> a 2 byte offset (which is something I need to investigate). So this
>>   > >>>>> function cannot make any assumptions about the alignment. Does the ABI
>>   > >>>>> define structs have some minimum alignment? Does the struct need to be
>>   > >>>>> declared as packed or something?
>>   > >>>>
>>   > >>>> The ABI defines the alignment of structs as the maximum alignment of its
>>   > >>>> members.  Since this struct contains 32-bit members, the alignment for the
>>   > >>>> whole struct becomes 32 bits as well.  Declaring it as packed tells gcc it
>>   > >>>> might be unaligned (in addition to removing any holes within).
>>   > >>>
>>   > >>> This has come up before in the past.
>>   > >>>
>>   > >>> The Linux network folk will _not_ allow - in any shape or form - for
>>   > >>> this struct to be marked packed (it's the struct which needs to be
>>   > >>> marked packed) because by doing so, it causes GCC to issue byte loads/
>>   > >>> stores on architectures where there isn't a problem, and that decreases
>>   > >>> the performance of the Linux IP stack unnecessarily.
>>   > >>
>>   > >> Which architectures?  I have never seen anything like that.
>>   > >
>>   > > Does it matter?  I'm just relaying the argument against adding __packed
>>   > > which was used before we were forced (by the networking folk) to implement
>>   > > the alignment fault handler.
>>   >
>>   > It doesn't really matter what will be accepted or not as adding __packed
>>   > to struct iphdr doesn't fix the problem anyway. gcc still emits a ldm.
>>   > The only way I've found to eliminate the alignment fault is adding a
>>   > barrier between the 2 loads. That seems like a compiler issue to me if
>>   > there is not a better fix.
>>
>> If you suspect a GCC bug, please prepare a standalone user-space test case
>> and submit it to GCC's bugzilla (I can do the latter if you absolutely do not
>> want to).  It wouldn't be the first alignment-related GCC bug...
>>
>
> Here's a testcase. Compiled on ubuntu precise with
> "arm-linux-gnueabihf-gcc -O2 -marm -march=armv7-a test.c".
>
> typedef unsigned short u16;
> typedef unsigned short __sum16;
> typedef unsigned int __u32;
> typedef unsigned char __u8;
> typedef __u32 __be32;
> typedef u16 __be16;
>
> struct iphdr {
> 	__u8	ihl:4,
> 		version:4;
> 	__u8	tos;
> 	__be16	tot_len;
> 	__be16	id;
> 	__be16	frag_off;
> 	__u8	ttl;
> 	__u8	protocol;
> 	__sum16	check;
> 	__be32	saddr;
> 	__be32	daddr;
> 	/*The options start here. */
> };

I was reading this thread with some interest.  AFAIK, with the default 
alignment rules the above struct is packed; there will be no holes in it.

>
> #define ntohl(x) __swab32((__u32)(__be32)(x))
> #define IP_DF		0x4000		/* Flag: "Don't Fragment"	*/
>
> static inline __attribute__((const)) __u32 __swab32(__u32 x)
> {
> 	__asm__ ("rev %0, %1" : "=r" (x) : "r" (x));
> 	return x;
> }
>
> int main(void * buffer, unsigned int *p_id)
> {
> 	unsigned int id;
> 	int flush = 1;
> 	const struct iphdr *iph = buffer;
> 	__u32 len = *p_id;
> 	
> 	id = ntohl(*(__be32 *)&iph->id);

The above statement is the problem.  I think it is poorly written 
networking code.  It takes the address of a 16 bit quantity (aligned on 
a halfword address), attempts to do a type conversion using pointers, 
then dereference it.  I would have thought:

id = ntohs(iph->id);

would have been enough.

Scott

-- 
Scott Bambrough
Technical Director, Member Services
Linaro Ltd.
email: scott.bambrough at linaro.org
irc: scottb (freenode, irc.linaro.org)
web: http://www.linaro.org
Linaro: The future of Linux on ARM.

  parent reply	other threads:[~2012-10-09 14:05 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-04 23:10 alignment faults in 3.6 Rob Herring
2012-10-05  0:58 ` Michael Hope
2012-10-05  1:26   ` Mans Rullgard
2012-10-05  1:56     ` Rob Herring
2012-10-05  2:25       ` Mans Rullgard
2012-10-05  3:04         ` Rob Herring
2012-10-05  5:37           ` Khem Raj
2012-10-05  7:12         ` Russell King - ARM Linux
2012-10-05  8:20           ` Mans Rullgard
2012-10-05  8:24             ` Russell King - ARM Linux
2012-10-05  8:33               ` Mans Rullgard
2012-10-05  8:33                 ` Russell King - ARM Linux
2012-10-05  8:37                   ` Mans Rullgard
2012-10-05  8:50                     ` Russell King - ARM Linux
2012-10-05 13:49                     ` Mikael Pettersson
2012-10-05 12:24               ` Rob Herring
2012-10-05 13:51                 ` Mikael Pettersson
2012-10-05 16:01                   ` Rob Herring
2012-10-05 22:37                     ` Mans Rullgard
2012-10-05 22:42                       ` Russell King - ARM Linux
2012-10-06  1:41                         ` Nicolas Pitre
2012-10-06 16:04                         ` Mans Rullgard
2012-10-06 16:19                           ` Nicolas Pitre
2012-10-06 16:31                           ` Russell King - ARM Linux
2012-10-06 10:58                     ` Mikael Pettersson
2012-10-09 14:05                     ` Scott Bambrough [this message]
2012-10-09 14:18                       ` Mans Rullgard
2012-10-05 14:05                 ` Russell King - ARM Linux
2012-10-05 14:33                   ` Rob Herring
2012-10-11  0:59                     ` Jon Masters
2012-10-11  2:27                       ` Måns Rullgård
2012-10-11  2:34                         ` Jon Masters
2012-10-11  8:21                         ` David Laight
2012-10-11  8:53                           ` Russell King - ARM Linux
2012-10-11  9:45                           ` Måns Rullgård
2012-10-11 10:00                             ` Eric Dumazet
2012-10-11 10:20                               ` Måns Rullgård
2012-10-11 10:22                               ` Eric Dumazet
2012-10-11 10:32                                 ` Russell King - ARM Linux
2012-10-11 10:49                                   ` Eric Dumazet
2012-10-11 10:56                                     ` Maxime Bizon
2012-10-11 11:28                                       ` Eric Dumazet
2012-10-11 11:47                                         ` Maxime Bizon
2012-10-11 11:54                                           ` Eric Dumazet
2012-10-11 12:00                                             ` Eric Dumazet
2012-10-11 12:51                                             ` Maxime Bizon
2012-10-11 12:59                                               ` Eric Dumazet
2012-10-11 12:28                                     ` Arnd Bergmann
2012-10-11 12:40                                       ` Eric Dumazet
2012-10-11 13:20                                         ` Rob Herring
2012-10-11 13:32                                           ` Måns Rullgård
2012-10-11 13:35                                           ` Arnd Bergmann
2012-10-11 13:47                                           ` Eric Dumazet
2012-10-11 15:23                                             ` Rob Herring
2012-10-11 15:39                                               ` David Laight
2012-10-11 16:18                                                 ` Måns Rullgård
2012-10-12  8:11                                                   ` Arnd Bergmann
2012-10-12  9:03                                                     ` Russell King - ARM Linux
2012-10-12 10:04                                                       ` Eric Dumazet
2012-10-12 12:24                                                         ` Russell King - ARM Linux
2012-10-12 11:00                                                       ` Måns Rullgård
2012-10-12 11:07                                                         ` Russell King - ARM Linux
2012-10-12 11:18                                                           ` Måns Rullgård
2012-10-12 11:44                                                             ` Russell King - ARM Linux
2012-10-12 12:08                                                               ` Eric Dumazet
2012-10-12 14:22                                                                 ` Benjamin LaHaise
2012-10-12 14:36                                                                   ` David Laight
2012-10-12 14:48                                                                   ` Eric Dumazet
2012-10-12 15:00                                                                     ` Benjamin LaHaise
2012-10-12 15:04                                                                     ` Ben Hutchings
2012-10-12 15:47                                                                       ` David Laight
2012-10-12 16:13                                                                         ` Ben Hutchings
2012-10-12 12:16                                                               ` Måns Rullgård
2012-10-12 11:19                                                           ` Russell King - ARM Linux
2012-10-11 16:15                                               ` Eric Dumazet
2012-10-11 16:59                                   ` Catalin Marinas
2012-10-11 10:16                             ` David Laight
2012-10-11 10:46                               ` Måns Rullgård
2012-10-05 16:08   ` Rob Herring
2012-10-05  7:29 ` Russell King - ARM Linux
2012-10-05 10:51   ` Russell King - ARM Linux
2012-10-23 16:30     ` Jon Masters
2012-10-23 16:58       ` Russell King - ARM Linux
2012-10-23 17:15         ` Jon Masters
2012-10-23 19:14         ` Rob Herring

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=50742F40.4050704@linaro.org \
    --to=scott.bambrough@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    /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).