Subject: Re: [RFC] net/hsr: Add support for IEC 62439-3 High-availability Seamless Redundancy

From: Arvid Brodin <[email-masked]>
Date: Fri, 6 Apr 2012 19:08:03 +0200

David Miller wrote:
> From: Arvid Brodin <[email-masked]>
> Date: Fri, 6 Apr 2012 17:51:24 +0200
> 
>> Just out of curiosity, what's the mechanism behind this inline
>> assignment that turns the memcpy into an unaligned access? If gcc is 
>> "smart" enough to detect a bunch of char * accesses and turn them 
>> into unaligned 32-bit accesses, isn't that a bug in gcc?
> 
> It's not doing it with "char *", it's doing it with other types like
> the type of the ICMP header in this case.
> 
> memcpy is expanded by the compiler internally into __builtin_memcpy()
> which if it sees the length is reasonably short will inline the
> copy.  And subsequently it uses the alignment of the types involved
> to determine what kinds of loads and stores it can use in that
> inline memcpy().
> 
> So the result is that just because in your case with your compiler
> it doesn't get expanded inline and fault, it doesn't mean it won't
> for someone else.
> 
> You're just lucky, and you really haven't fixed the bug.
> 

Ok. And thanks for the explanation. Would something like this do the 
trick then?

+		/* Need to cast to (char *) below to keep gcc optimizations 
+		   from causing alignment faults. */
+		memcpy(&icmp_param.data.icmph, (char *) icmp_hdr(skb),
+						sizeof(icmp_param.data.icmph));

If not, any suggestions on how to tackle this?

-- 
Arvid Brodin
Enea Services Stockholm AB - since February 16 a part of Xdin in the Alten
Group. Soon we will be working under the common brand Xdin. Read more at
www.xdin.com.