pahole: BTF alignment inference improvements
v2 cover pahole: BTF alignment inference improvements
Hello,
While generating C code using pahole we noticed alignment issues in rare
cases: when having variable size and 0-length arrays, and when a hole follows a bitfield. It's worth noticing alignment is inferred when using BTF as a source as the format does not contain such information. As such the logic is tricky.
The first patch fixes alignment inference for zero-length and variable length arrays. The second patch fixes alignment when a hole follows a bitfield.
I generated a full vmlinux.h using pahole --compile before and after the changes, the differences looked reasonable to me. I've also ran the project's tests using GH actions.
Thanks,
Antoine
Since v1:
- Added comments in both patches to help future readers.
Antoine Tenart (2):
btf_loader: infer alignment for zero-length arrays
btf_loader: fix smallest offset in case of bitfields
btf_loader.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
Signature · 1 line
2.50.0
PATCH v2 1/2 btf_loader: infer alignment for zero-length arrays
Zero sized arrays do not go through the bitfield fixup logic, which in turn makes them to skip the alignment infer logic. The result is zero-length arrays being incorrectly aligned when an explicit statement is needed. E.g. this happens for variable length arrays at end of structures, or for 0-length ones.
Before this patch we can see:
struct skb_ext {
refcount_t refcnt; /* 0 4 */
u8 offset[4]; /* 4 4 */
u8 chunks; /* 8 1 */
/* XXX 7 bytes hole, try to pack */
char data[]; /* 16 0 */
/* size: 16, cachelines: 1, members: 4 */
/* sum members: 9, holes: 1, sum holes: 7 */
/* last cacheline: 16 bytes */} __attribute__((__aligned__(8)));
offsetof(struct skb_ext, data) returns 9.
After this patch we get:
struct skb_ext {
refcount_t refcnt; /* 0 4 */
u8 offset[4]; /* 4 4 */
u8 chunks; /* 8 1 */
/* XXX 7 bytes hole, try to pack */
char data[] __attribute__((__aligned__(8))); /* 16 0 */
/* size: 16, cachelines: 1, members: 4 */
/* sum members: 9, holes: 1, sum holes: 7 */
/* forced alignments: 1, forced holes: 1, sum forced holes: 7 */
/* last cacheline: 16 bytes */} __attribute__((__aligned__(8)));
offsetof(struct skb_ext, data) returns 16.
--- btf_loader.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
btf_loader.c +8 -2
@@ -645,9 +645,15 @@ static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag * pos->byte_size =tag__size (type, cu); pos->bit_size = pos->byte_size *8 ; -/* if BTF data is incorrect and has size == 0, skip field, -* instead of crashing */ +/* If the BTF data is incorrect and has size == 0, skip field +* instead of crashing. However the field can be a zero or +* variable-length array and we still need to infer alignment. +*/ if (pos->byte_size ==0 ) { + pos->alignment =class__infer_alignment (conf, + pos->byte_offset, +tag__natural_alignment (type, cu), + smallest_offset);continue ; }
Signature · 1 line
2.50.0
PATCH v2 2/2 btf_loader: fix smallest offset in case of bitfields
In reply to Antoine TenartIn case the member is a bitfield, using pos->byte_size can be misleading as it can be larger than the actual size. In such case we could miss an actual hole in the struct and not infer the alignment as expected.
Before this patch we can see:
$ pahole nf_tables.btf -C nft_rule_dp
struct nft_rule_dp {
u64 is_last:1; /* 0: 0 8 */
u64 dlen:12; /* 0: 1 8 */
u64 handle:42; /* 0:13 8 *//* XXX 9 bits hole, try to pack */
unsigned char data[]; /* 8 0 */
/* size: 8, cachelines: 1, members: 4 */
/* sum bitfield members: 55 bits, bit holes: 1, sum bit holes: 9 bits */ /* last cacheline: 8 bytes */
};
offsetof(struct nft_rule_dp, data) returns 7.
Where data won't be aligned as expected, as the first member fits on the byte left after the bitfield.
After this patch we get:
$ pahole nf_tables.btf -C nft_rule_dp
struct nft_rule_dp {
u64 is_last:1; /* 0: 0 8 */
u64 dlen:12; /* 0: 1 8 */
u64 handle:42; /* 0:13 8 *//* XXX 9 bits hole, try to pack */
unsigned char data[] __attribute__((__aligned__(2))); /* 8 0 */
/* size: 8, cachelines: 1, members: 4 */
/* sum bitfield members: 55 bits, bit holes: 1, sum bit holes: 9 bits */ /* forced alignments: 1 */
/* last cacheline: 8 bytes */
};
offsetof(struct nft_rule_dp, data) returns 8.
Thanks to Paolo Valerio for helping, including narrowing down where the issue was (BTF decoder).
--- btf_loader.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
btf_loader.c +12 -1
@@ -678,7 +678,18 @@ static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag * pos->byte_offset,tag__natural_alignment (type, cu), smallest_offset); - smallest_offset = pos->byte_offset + pos->byte_size; + +/* Compute the smallest offset between this field and the next +* one. +* +* In case of bitfields we need to take into account the +* actual size being used instead of the underlying type one as +* it could be larger, otherwise we could miss a hole. +*/ + smallest_offset = pos->byte_offset; + smallest_offset += pos->bitfield_size ? + (pos->bitfield_offset + pos->bitfield_size +7 ) /8 : + pos->byte_size; } tag_type->alignment = class__infer_alignment(conf,
Signature · 1 line
2.50.0
Quoted 22 lines · Antoine Tenart
Hello,
While generating C code using pahole we noticed alignment issues in rare
cases: when having variable size and 0-length arrays, and when a hole follows a bitfield. It's worth noticing alignment is inferred when using BTF as a source as the format does not contain such information. As such the logic is tricky.The first patch fixes alignment inference for zero-length and variable length arrays. The second patch fixes alignment when a hole follows a bitfield.
I generated a full
vmlinux.husingpahole --compilebefore and after the changes, the differences looked reasonable to me. I've also ran the project's tests using GH actions.Thanks,
AntoineSince v1:
- Added comments in both patches to help future readers.
Quoted 7 lines
Antoine Tenart (2):
btf_loader: infer alignment for zero-length arrays
btf_loader: fix smallest offset in case of bitfieldsbtf_loader.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
Series applied to "next" branch of
https://git.kernel.org/pub/scm/devel/pahole/pahole.git/
thank you!
Alan