public inbox for dwarves@vger.kernel.org
 help / color / mirror / Atom feed
From: Antoine Tenart <atenart@kernel.org>
To: dwarves@vger.kernel.org
Cc: Antoine Tenart <atenart@kernel.org>, pvalerio@redhat.com
Subject: [PATCH 2/2] btf_loader: fix smallest offset in case of bitfields
Date: Wed, 25 Jun 2025 17:23:20 +0200	[thread overview]
Message-ID: <20250625152329.28601-3-atenart@kernel.org> (raw)
In-Reply-To: <20250625152329.28601-1-atenart@kernel.org>

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).

Signed-off-by: Antoine Tenart <atenart@kernel.org>
---
 btf_loader.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/btf_loader.c b/btf_loader.c
index 76771afedd95..3beb5bba5f94 100644
--- a/btf_loader.c
+++ b/btf_loader.c
@@ -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,
-- 
2.49.0


  parent reply	other threads:[~2025-06-25 15:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-25 15:23 [PATCH 0/2] pahole: BTF alignment inference improvements Antoine Tenart
2025-06-25 15:23 ` [PATCH 1/2] btf_loader: infer alignment for zero-length arrays Antoine Tenart
2025-07-04 16:34   ` Alan Maguire
2025-07-08 13:55     ` Antoine Tenart
2025-06-25 15:23 ` Antoine Tenart [this message]
2025-07-04 16:40   ` [PATCH 2/2] btf_loader: fix smallest offset in case of bitfields Alan Maguire
2025-07-08 14:01     ` Antoine Tenart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250625152329.28601-3-atenart@kernel.org \
    --to=atenart@kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=pvalerio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox