Skip to contentSkip to conversation

pahole: BTF alignment inference improvements

PATCH Superseded Antoine Tenart 2 participants 7 messages Jun 25, 2025 – Jul 8, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

Antoine Tenart (2):
btf_loader: infer alignment for zero-length arrays
btf_loader: fix smallest offset in case of bitfields

 btf_loader.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 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 | 4 ++++
 1 file changed, 4 insertions(+)
btf_loader.c +4 -0
index f4f9f65289b5..76771afedd95 100644
@@ -648,6 +648,10 @@ static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag *
 		/* if BTF data is incorrect and has size == 0, skip field,
 		 * instead of crashing */
 		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.49.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] btf_loader: infer alignment for zero-length arrays

Quoted 39 lines · Antoine Tenart

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 */
Quoted 5 lines

} __attribute__((__aligned__(8)));

offsetof(struct skb_ext, data) returns 16.

with a small suggestion below..

btf_loader.c static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag * · line 649

Quoted 11 lines
---
 btf_loader.c | 4 ++++
 1 file changed, 4 insertions(+)
btf_loader.c +0 -0
index f4f9f65289b5..76771afedd95 100644
@@ -648,6 +648,10 @@ static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag *
 		/* if BTF data is incorrect and has size == 0, skip field,
 		 * instead of crashing */

nit: could we update the comment here to reflect the change, adding something like "however we still need to infer alignment for cases like zero/variable-length arays".

Quoted 8 lines
btf_loader.c +4 -0
 		if (pos->byte_size == 0) {
+			pos->alignment = class__infer_alignment(conf,
+								pos->byte_offset,
+								tag__natural_alignment(type, cu),
+								smallest_offset);
 			continue;
 		}
 
#3 lore raw

Re: [PATCH 1/2] btf_loader: infer alignment for zero-length arrays

Quoted 54 lines · Alan Maguire
Quoted 39 lines · Antoine Tenart

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 */
Quoted 5 lines

} __attribute__((__aligned__(8)));

offsetof(struct skb_ext, data) returns 16.

with a small suggestion below..

Quoted 4 lines
---
 btf_loader.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 11 lines

btf_loader.c static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag * · line 649

Quoted 7 lines
btf_loader.c +0 -0
index f4f9f65289b5..76771afedd95 100644
@@ -648,6 +648,10 @@ static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag *
 		/* if BTF data is incorrect and has size == 0, skip field,
 		 * instead of crashing */

nit: could we update the comment here to reflect the change, adding something like "however we still need to infer alignment for cases like zero/variable-length arays".

Makes sense, I'll improve the comment in v2.

Thanks!

Quoted 11 lines
Quoted 10 lines
btf_loader.c +4 -0
 		if (pos->byte_size == 0) {
+			pos->alignment = class__infer_alignment(conf,
+								pos->byte_offset,
+								tag__natural_alignment(type, cu),
+								smallest_offset);
 			continue;
 		}
 
#4 lore raw

PATCH 2/2 btf_loader: fix smallest offset in case of bitfields

In reply to Antoine Tenart

In 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 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
btf_loader.c +4 -1
index 76771afedd95..3beb5bba5f94 100644
@@ -676,7 +676,10 @@ 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;
+		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.49.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] btf_loader: fix smallest offset in case of bitfields

Quoted 44 lines · Antoine Tenart

In 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 */
};

Quoted 6 lines

offsetof(struct nft_rule_dp, data) returns 8.

Thanks to Paolo Valerio for helping, including narrowing down where the issue was (BTF decoder).

Quoted 9 lines
---
 btf_loader.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
btf_loader.c +0 -0
index 76771afedd95..3beb5bba5f94 100644

btf_loader.c static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag * · line 682

Quoted 8 lines
btf_loader.c +4 -1
@@ -676,7 +676,10 @@ 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;
+		smallest_offset = pos->byte_offset;
+		smallest_offset += pos->bitfield_size ?
+			(pos->bitfield_offset + pos->bitfield_size + 7) / 8 :
+			pos->byte_size;

would be good to have a comment here explaining the situation too; it's quite a subtle problem so anything that helps explain things would be great. Thanks for tracking this down!

Quoted 3 lines
btf_loader.c +0 -0
 	}
 
 	tag_type->alignment = class__infer_alignment(conf,
#6 lore raw

Re: [PATCH 2/2] btf_loader: fix smallest offset in case of bitfields

Quoted 68 lines · Alan Maguire
Quoted 44 lines · Antoine Tenart

In 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 */
};

Quoted 6 lines

offsetof(struct nft_rule_dp, data) returns 8.

Thanks to Paolo Valerio for helping, including narrowing down where the issue was (BTF decoder).

Quoted 14 lines
---
 btf_loader.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
btf_loader.c +1 -1
index 76771afedd95..3beb5bba5f94 100644
@@ -676,7 +676,10 @@ 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;
+		smallest_offset = pos->byte_offset;
Quoted 8 lines

btf_loader.c static int class__fixup_btf_bitfields(const struct conf_load *conf, struct tag * · line 682

Quoted 3 lines
btf_loader.c +3 -0
+		smallest_offset += pos->bitfield_size ?
+			(pos->bitfield_offset + pos->bitfield_size + 7) / 8 :
+			pos->byte_size;

would be good to have a comment here explaining the situation too; it's quite a subtle problem so anything that helps explain things would be great. Thanks for tracking this down!

Will do.

Thanks!