Messages in this thread | | | From | David Laight <> | Subject | RE: [PATCH v2 04/15] ACPI: table: replace __attribute__((packed)) by __packed | Date | Thu, 1 Apr 2021 14:23:17 +0000 |
| |
From: Rafael J. Wysocki > Sent: 01 April 2021 14:50 ... > So what exactly is wrong with using "packed"? It is way easier to > understand for a casual reader of the code.
Because it is usually wrong!
If I have: struct foo { u64 val; } __packed;
And then have: u64 bar(struct foo *foo) { return foo->val; }
The on some cpu the compiler has to generate the equivalent of: u8 *x = (void *)&foo->val; return x[0] | x[1] << 8 | x[2] << 16 | x[3] << 24 | x[4] << 32 | x[5] << 40 | x[6] << 48 | x[7] << 56;
If you can guarantee that the structure is 32bit aligned then it can generate the simpler: u32 *x = (void *)&foo->val; return x[0] | x[1] << 32;
(Yes I've missed out the 64-bit casts)
This is why you should almost never use __packed.
There are historic structures with 64 bit items on 4 byte boundaries (and 32 bit values on 2 byte boundaries). Typically most of the fields are shorter so can be read directly (although they might need a byte-swapping load).
David
- Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
| |