I won't pretend to understand all the subtleties of 
__attribute__((packed)), but I did learn something about it when I 
hacked USB/IP to run on ARM, for the NSLU2, last year.  The issue 
boiled down to this:

struct S {
   int intfield;
   char charfield;
} /* maybe add: __attribute__((packed)) here */ ;

f(struct S * s) {
   ...
   (s->intfield)++;
   ...
}

g() {
   char c;
   struct S s;
   (s.intfield)++;
   f(&s);
}

If there are no attributes anywhere this will obviously work.  There 
will be a 3-byte gap between c and s.

What happens when we add __attribute__((packed)) to struct S?  In g(), 
s will now be placed immediately after c.  So s.intfield is unaligned.  
On x86, this doesn't matter.  On ARM (etc.) this does matter, and 
inside g() the compiler will use about 8 instructions to do the ++.  It 
will work, but a lot more slowly.  However, the ++ in f() WILL NOT 
WORK!  The compiler assumes that the pointer that is passed IS 
NATURALLY ALIGNED, and when you pass an unaligned pointer from g() IT 
DOES NOT ISSUE A WARNING!  I believe that the MMU can be configured to 
generate a bus error in this case, but this seemed to be disabled on 
the NSLU2 kernel and it just silently continued having not done the 
store.  There could be some difference between kernel and user code in 
this respect.

Based on my experiece - it took me a long time to debug this - I would 
suggest not adding __attribute__((packed)) to a struct unless it is 
absolutely necessary, and if you do add it you should then check every 
place where an address of a struct of that type is taken, and see how 
that pointer is used.  If the pointer is just cast to a char* (i.e. the 
struct is treated as a lump of memory) you're safe; but if fields of 
the struct are accessed via a pointer there could be problems (with no 
compiler warnings).

Sorry if I'm just repeating stuff that you all already know....

Phil.





-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
linux-usb-devel@lists.sourceforge.net
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel

Reply via email to