Skip to contentSkip to conversation

Add BTF layout to BTF

PATCH Superseded Alan Maguire 3 participants 21 messages Feb 27 – Mar 1

Subsystems (6)
Kbuild Kselftest BPF BPF selftests +2 more subsystems

v10 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v10 cover Add BTF layout to BTF

Update struct btf_header to add a new "layout" section containing a description of how to parse the BTF kinds known about at BTF encoding time. This provides the opportunity for tools that might not know all of these kinds - as is the case when older tools run on more newly-generated BTF - to still parse the BTF provided, even if it cannot all be used.

Also add support to bpftool to dump metadata about BTF; its size, header information and kind layout section.

The ideas here were discussed at [1], with further discussion at [2].

Patches for pahole will enable the layout addition during BTF generation are at [3], but even absent these the addition of the layout feature in the final patch in this series should not break anything since such unknown features are simply ignored during pahole BTF generation.

Changes since v9: [4]:

- fix memcpy header size overrun (review bot, patch 2)
- return size computation directly (Andrii, patch 333)
- revert to original unknown kind logging (Alexei/review bot, patch 6)
- gap-checking logic can be simplified now that we have 4-byte aligned types and layout together (patch 6)
- fix naming of layout offset, unconditionally emit layout info in json (Quentin, review bot, patch 8)
- fix metadata output in man page to include flags (review bot, patch 9)

Changes since v8: [5]:

- updated name from "kind_layout" to "layout" (Andrii)
- moved layout info to inbetween types and strings since both types and layout info align on 4 bytes (Andrii)
- use embedded btf_header (Eduard)
- when consulting layout, fall back to base BTF if none found in split BTF; this will allow us to only encode layout info in vmlinux rather than repeating it for each module.

Changes since v7: [6]:

- Fixed comment style in UAPI headers (Mykyta, patch 1)
- Simplify calcuation of header size using min() (Mykyta, patch 2)
- simplify computation of bounds for kind (Mykyta, patch 3)
- Added utility functions for updating type, string offsets when data is added; this simplifies the code and encapsulates such updates more clearly (patch 2)

Changes since v6: [7]:

BPF review bot caught some memory leaks around freeing
of kind layout; more importantly, it noted that we were breaking with the contiguous BTF representation for btf_new_empty_opts(). Doing so meant that freeing kind_layout could not be predicated on having btf->modifiable set, so adpoted the contiguous raw data layout for BTF to be consistent with type/string storage (patches 2,4)
- Moved checks for kind overflow prior to referencing kinds to avoid any risk of overrun (patches 3, 8)
- Tightened up kind layout header offset/len header validation to catch invalid combinations early in btf_parse_hdr() (patch 2)
- Fixed selftest to verify calloc success (patch 7)

Changes since v5: [8]:

- removed flags field from kind layout; it is not really workable since we would have to define semantics of all possible future flags today to be usable. Instead stick to parsing only, which means each kind just needs the length of the singular and vlen-specified objects (Alexei)
- added documentation for bpftool BTF metadata dump (Quentin, patch 9)

Changes since v4: [9]:

- removed CRC generation since it is not needed to handle modules built at different time than kernel; distilled base BTF supports this now
- fixed up bpftool display of empty kind names, comment/documentation indentation (Quentin, patches 8, 9)

Changes since v3 [10]:

- fixed mismerge issues with kbuild changes for BTF generation (patches 9, 14)
- fixed a few small issues in libbpf with kind layout representation (patches 2, 4)

Changes since v2 [11]:

- drop "optional" kind flag (Andrii, patch 1)
- allocate "struct btf_header" for struct btf to ensure we can always access new fields (Andrii, patch 2)
- use an internal BTF kind array in btf.c to simplify kind encoding (Andrii, patch 2)
- drop use of kind layout information for in-kernel parsing, since the kernel needs to be strict in what it accepts (Andrii, patch 6)
- added CRC verification for BTF objects and for matching with base object (Alexei, patches 7,8)
- fixed bpftool json output (Quentin, patch 10)
- added standalone module BTF support, tests (patches 13-17)

Changes since RFC
- Terminology change from meta -> kind_layout
(Alexei and Andrii)
- Simplify representation, removing meta header
and just having kind layout section (Alexei)
- Fixed bpftool to have JSON support, support
prefix match, documented changes (Quentin)
- Separated metadata opts into add_kind_layout
and add_crc
- Added additional positive/negative tests
to cover basic unknown kind, one with an
info_sz object following it and one with
N elem_sz elements following it.
- Updated pahole-flags to use help output
rather than version to see if features
are present

[1] https://lore.kernel.org/bpf/CAEf4BzYjWHRdNNw4B=eOXOs_ONrDwrgX4bn=Nuc1g8JPFC34MA@mail.gmail.com/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://lore.kernel.org/dwarves/[email protected]/
[4] https://lore.kernel.org/bpf/[email protected]/
[5] https://lore.kernel.org/bpf/[email protected]/
[6] https://lore.kernel.org/dwarves/[email protected]/
[7] https://lore.kernel.org/bpf/[email protected]/
[8] https://lore.kernel.org/bpf/[email protected]/
[9] https://lore.kernel.org/bpf/[email protected]/
[10] https://lore.kernel.org/bpf/[email protected]/
[11] https://lore.kernel.org/bpf/[email protected]/

Alan Maguire (10):
btf: add BTF kind layout encoding to UAPI
libbpf: Support layout section handling in BTF
libbpf: use layout to compute an unknown kind size
libbpf: Add layout encoding support
libbpf: BTF validation can use layout for unknown kinds
btf: support kernel parsing of BTF with layout info selftests/bpf: test kind encoding/decoding
bpftool: add BTF dump "format meta" to dump header/metadata
bpftool: Update doc to describe bpftool btf dump .. format metadata kbuild, bpf: Specify "layout" optional feature

 include/uapi/linux/btf.h                      |  12 +
 kernel/bpf/btf.c                              |  50 ++-
 scripts/Makefile.btf                          |   2 +
 .../bpf/bpftool/Documentation/bpftool-btf.rst |  28 +-
 tools/bpf/bpftool/bash-completion/bpftool     |   2 +-
 tools/bpf/bpftool/btf.c                       |  91 +++-
 tools/include/uapi/linux/btf.h                |  12 +
 tools/lib/bpf/btf.c                           | 418 +++++++++++++-----
 tools/lib/bpf/btf.h                           |  20 +
 tools/lib/bpf/libbpf.map                      |   1 +
 .../selftests/bpf/prog_tests/btf_kind.c       | 206 +++++++++
 11 files changed, 710 insertions(+), 132 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_kind.c
Signature · 1 line
2.39.3
#1 lore raw

PATCH v10 1/10 btf: add BTF kind layout encoding to UAPI

BTF kind layouts provide information to parse BTF kinds. By separating parsing BTF from using all the information it provides, we allow BTF to encode new features even if they cannot be used by readers. This will be helpful in particular for cases where older tools are used to parse newer BTF with kinds the older tools do not recognize; the BTF can still be parsed in such cases using kind layout.

The intent is to support encoding of kind layouts optionally so that tools like pahole can add this information. For each kind, we record

- length of singular element following struct btf_type
- length of each of the btf_vlen() elements following
- a (currently unused) flags field

The ideas here were discussed at [1], [2]; hence

[1] https://lore.kernel.org/bpf/CAEf4BzYjWHRdNNw4B=eOXOs_ONrDwrgX4bn=Nuc1g8JPFC34MA@mail.gmail.com/
[2] https://lore.kernel.org/bpf/[email protected]/

---
 include/uapi/linux/btf.h       | 12 ++++++++++++
 tools/include/uapi/linux/btf.h | 12 ++++++++++++
 2 files changed, 24 insertions(+)
include/uapi/linux/btf.h +12 -0
index 266d4ffa6c07..638615ebddc2 100644
@@ -8,6 +8,16 @@
 #define BTF_MAGIC	0xeB9F
 #define BTF_VERSION	1
 
+/*
+ * BTF layout section consists of a struct btf_layout for each known
+ * kind at BTF encoding time.
+ */
+struct btf_layout {
+	__u8 info_sz;		/* size of singular element after btf_type */
+	__u8 elem_sz;		/* size of each of btf_vlen(t) elements */
+	__u16 flags;		/* currently unused */
+};
+
 struct btf_header {
 	__u16	magic;
 	__u8	version;
@@ -19,6 +29,8 @@ struct btf_header {
 	__u32	type_len;	/* length of type section	*/
 	__u32	str_off;	/* offset of string section	*/
 	__u32	str_len;	/* length of string section	*/
+	__u32	layout_off;	/* offset of layout section	*/
+	__u32	layout_len;	/* length of layout section	*/
 };
 
 /* Max # of type identifier */
tools/include/uapi/linux/btf.h +12 -0
index 266d4ffa6c07..638615ebddc2 100644
@@ -8,6 +8,16 @@
 #define BTF_MAGIC	0xeB9F
 #define BTF_VERSION	1
 
+/*
+ * BTF layout section consists of a struct btf_layout for each known
+ * kind at BTF encoding time.
+ */
+struct btf_layout {
+	__u8 info_sz;		/* size of singular element after btf_type */
+	__u8 elem_sz;		/* size of each of btf_vlen(t) elements */
+	__u16 flags;		/* currently unused */
+};
+
 struct btf_header {
 	__u16	magic;
 	__u8	version;
@@ -19,6 +29,8 @@ struct btf_header {
 	__u32	type_len;	/* length of type section	*/
 	__u32	str_off;	/* offset of string section	*/
 	__u32	str_len;	/* length of string section	*/
+	__u32	layout_off;	/* offset of layout section	*/
+	__u32	layout_len;	/* length of layout section	*/
 };
 
 /* Max # of type identifier */
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH v10 bpf-next 01/10] btf: add BTF kind layout encoding to UAPI

Adding these two fields changes sizeof(struct btf_header) from 24 to 32 bytes. In libbpf's btf_parse_hdr(), the non-native endian path has a strict equality check:
Quoted 4 lines
tools/include/uapi/linux/btf.h +0 -0
index 266d4ffa6c07..638615ebddc2 100644

[ ... ]

tools/include/uapi/linux/btf.h struct btf_header { · line 34

Quoted 7 lines
tools/include/uapi/linux/btf.h +2 -0
@@ -19,6 +29,8 @@ struct btf_header {
 	__u32	type_len;	/* length of type section	*/
 	__u32	str_off;	/* offset of string section	*/
 	__u32	str_len;	/* length of string section	*/
+	__u32	layout_off;	/* offset of layout section	*/
+	__u32	layout_len;	/* length of layout section	*/
 };

Adding these two fields changes sizeof(struct btf_header) from 24 to 32 bytes. In libbpf's btf_parse_hdr(), the non-native endian path has a strict equality check:

tools/lib/bpf/btf.c:btf_parse_hdr() {
    if (hdr->magic == bswap_16(BTF_MAGIC)) {
        btf->swapped_endian = true;
        if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
            pr_warn("Can't load BTF with non-native endianness "
                    "due to unsupported header length %u\n",
                    bswap_32(hdr->hdr_len));
            return -ENOTSUP;
        }
    ...
}

All existing cross-endian BTF has hdr_len=24. After this UAPI change, sizeof(struct btf_header) becomes 32, so bswap_32(24) != 32 and the check rejects them.

Would it make sense to update this check in the same patch as the header change? As-is, recompiling libbpf with the new header breaks loading of any pre-existing non-native-endian BTF data.

This appears to be addressed in the next patch in the series, aa01655822a6 ("libbpf: Support layout section handling in BTF"), which relaxes the check to a minimum size comparison.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22482435858

AI-authorship-score: low
AI-authorship-explanation: Standard UAPI structure definition from a known kernel developer with prior lore discussion links and Suggested-by tag, consistent with human authorship.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: UAPI header change increases sizeof(struct btf_header), breaking libbpf cross-endian BTF loading for all pre-existing non-native-endian BTF data; fixed in the next patch in the series.

#3 lore raw

Re: [PATCH v10 bpf-next 01/10] btf: add BTF kind layout encoding to UAPI

Quoted 35 lines · On 27/02/2026 10:52, [email protected] wrote:
Quoted 4 lines
tools/include/uapi/linux/btf.h +0 -0
index 266d4ffa6c07..638615ebddc2 100644

[ ... ]

tools/include/uapi/linux/btf.h struct btf_header { · line 34

Quoted 7 lines
tools/include/uapi/linux/btf.h +2 -0
@@ -19,6 +29,8 @@ struct btf_header {
 	__u32	type_len;	/* length of type section	*/
 	__u32	str_off;	/* offset of string section	*/
 	__u32	str_len;	/* length of string section	*/
+	__u32	layout_off;	/* offset of layout section	*/
+	__u32	layout_len;	/* length of layout section	*/
 };

Adding these two fields changes sizeof(struct btf_header) from 24 to 32 bytes. In libbpf's btf_parse_hdr(), the non-native endian path has a strict equality check:

tools/lib/bpf/btf.c:btf_parse_hdr() {
    if (hdr->magic == bswap_16(BTF_MAGIC)) {
        btf->swapped_endian = true;
        if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
            pr_warn("Can't load BTF with non-native endianness "
                    "due to unsupported header length %u\n",
                    bswap_32(hdr->hdr_len));
            return -ENOTSUP;
        }
    ...
}

All existing cross-endian BTF has hdr_len=24. After this UAPI change, sizeof(struct btf_header) becomes 32, so bswap_32(24) != 32 and the check rejects them.

Quoted 9 lines

Would it make sense to update this check in the same patch as the header change? As-is, recompiling libbpf with the new header breaks loading of any pre-existing non-native-endian BTF data.

This appears to be addressed in the next patch in the series, aa01655822a6 ("libbpf: Support layout section handling in BTF"), which relaxes the check to a minimum size comparison.

In order to handle all the fallout of the header size change in a 100% bisect-friendly way it would be necessary to squash patches 1,2 and 6 in this series into a single patch dealing with the UAPI, libbpf and kernel handling of same. I'm open to doing this, but am conscious of the fact the larger patch might make it harder to review (for a human at least). We used to explicitly separate UAPI from other changes for BPF, not sure if that's as much of a concern any more..

Quoted 12 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22482435858

AI-authorship-score: low
AI-authorship-explanation: Standard UAPI structure definition from a known kernel developer with prior lore discussion links and Suggested-by tag, consistent with human authorship.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: UAPI header change increases sizeof(struct btf_header), breaking libbpf cross-endian BTF loading for all pre-existing non-native-endian BTF data; fixed in the next patch in the series.

#4 lore raw

Re: [PATCH v10 bpf-next 01/10] btf: add BTF kind layout encoding to UAPI

Quoted 47 lines · Alan Maguire
Quoted 35 lines · On 27/02/2026 10:52, [email protected] wrote:
Quoted 4 lines
tools/include/uapi/linux/btf.h +0 -0
index 266d4ffa6c07..638615ebddc2 100644

[ ... ]

tools/include/uapi/linux/btf.h struct btf_header { · line 34

Quoted 7 lines
tools/include/uapi/linux/btf.h +2 -0
@@ -19,6 +29,8 @@ struct btf_header {
     __u32   type_len;       /* length of type section       */
     __u32   str_off;        /* offset of string section     */
     __u32   str_len;        /* length of string section     */
+    __u32   layout_off;     /* offset of layout section     */
+    __u32   layout_len;     /* length of layout section     */
 };

Adding these two fields changes sizeof(struct btf_header) from 24 to 32 bytes. In libbpf's btf_parse_hdr(), the non-native endian path has a strict equality check:

tools/lib/bpf/btf.c:btf_parse_hdr() {
    if (hdr->magic == bswap_16(BTF_MAGIC)) {
        btf->swapped_endian = true;
        if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
            pr_warn("Can't load BTF with non-native endianness "
                    "due to unsupported header length %u\n",
                    bswap_32(hdr->hdr_len));
            return -ENOTSUP;
        }
    ...
}

All existing cross-endian BTF has hdr_len=24. After this UAPI change, sizeof(struct btf_header) becomes 32, so bswap_32(24) != 32 and the check rejects them.

Quoted 9 lines

Would it make sense to update this check in the same patch as the header change? As-is, recompiling libbpf with the new header breaks loading of any pre-existing non-native-endian BTF data.

This appears to be addressed in the next patch in the series, aa01655822a6 ("libbpf: Support layout section handling in BTF"), which relaxes the check to a minimum size comparison.

Quoted 7 lines

In order to handle all the fallout of the header size change in a 100% bisect-friendly way it would be necessary to squash patches 1,2 and 6 in this series into a single patch dealing with the UAPI, libbpf and kernel handling of same. I'm open to doing this, but am conscious of the fact the larger patch might make it harder to review (for a human at least). We used to explicitly separate UAPI from other changes for BPF, not sure if that's as much of a concern any more..

Yeah. Ignore the bot. No need to squash things.

#5 lore raw

PATCH v10 2/10 libbpf: Support layout section handling in BTF

In reply to Alan Maguire

Support reading in layout fixing endian issues on reading; also support writing layout section to raw BTF object. There is not yet an API to populate the layout with meaningful information.

As part of this, we need to consider multiple valid BTF header sizes; the original or the layout-extended headers.
So to support this, the "struct btf" representation is modified to contain a "struct btf_header" and we copy the valid portion from the raw data to it; this means we can always safely check fields like btf->hdr.layout_len

---
 tools/lib/bpf/btf.c | 306 ++++++++++++++++++++++++++++----------------
 1 file changed, 198 insertions(+), 108 deletions(-)
tools/lib/bpf/btf.c +198 -108
index 83fe79ffcb8f..7b58724a90a2 100644
@@ -40,42 +40,53 @@ struct btf {
 
 	/*
 	 * When BTF is loaded from an ELF or raw memory it is stored
-	 * in a contiguous memory block. The hdr, type_data, and, strs_data
+	 * in a contiguous memory block. The type_data, layout and strs_data
 	 * point inside that memory region to their respective parts of BTF
 	 * representation:
 	 *
-	 * +--------------------------------+
-	 * |  Header  |  Types  |  Strings  |
-	 * +--------------------------------+
-	 * ^          ^         ^
-	 * |          |         |
-	 * hdr        |         |
-	 * types_data-+         |
-	 * strs_data------------+
+	 * +----------------------------------------+---------------+
+	 * |  Header  |  Types  |  Optional layout  |  Strings      |
+	 * +--------------------------------------------------------+
+	 * ^          ^         ^                   ^
+	 * |          |         |                   |
+	 * raw_data   |         |                   |
+	 * types_data-+         |                   |
+	 * layout---------------+                   |
+	 * strs_data--------------------------------+
+	 *
+	 * A separate struct btf_header is embedded as btf->hdr,
+	 * and header information is copied into it.  This allows us
+	 * to handle header data for various header formats; the original,
+	 * the extended header with layout info, etc.
 	 *
 	 * If BTF data is later modified, e.g., due to types added or
 	 * removed, BTF deduplication performed, etc, this contiguous
-	 * representation is broken up into three independently allocated
-	 * memory regions to be able to modify them independently.
+	 * representation is broken up into four independent memory
+	 * regions.
+	 *
 	 * raw_data is nulled out at that point, but can be later allocated
 	 * and cached again if user calls btf__raw_data(), at which point
-	 * raw_data will contain a contiguous copy of header, types, and
-	 * strings:
+	 * raw_data will contain a contiguous copy of header, types, optional
+	 * layout and strings.  layout optionally points to a
+	 * btf_layout array - this allows us to encode information about
+	 * the kinds known at encoding time.  If layout is NULL no
+	 * layout information is encoded.
 	 *
-	 * +----------+  +---------+  +-----------+
-	 * |  Header  |  |  Types  |  |  Strings  |
-	 * +----------+  +---------+  +-----------+
-	 * ^             ^            ^
-	 * |             |            |
-	 * hdr           |            |
-	 * types_data----+            |
-	 * strset__data(strs_set)-----+
+	 * +----------+  +---------+  +-----------+   +-----------+
+	 * |  Header  |  |  Types  |  |  Layout   |   |  Strings  |
+	 * +----------+  +---------+  +-----------+   +-----------+
+	 * ^             ^            ^               ^
+	 * |             |            |               |
+	 * hdr           |            |               |
+	 * types_data----+            |               |
+	 * layout---------------------+               |
+	 * strset__data(strs_set)---------------------+
 	 *
-	 *               +----------+---------+-----------+
-	 *               |  Header  |  Types  |  Strings  |
-	 * raw_data----->+----------+---------+-----------+
+	 *               +----------+---------+-------------------+-----------+
+	 *               |  Header  |  Types  |  Optional Layout  |  Strings  |
+	 * raw_data----->+----------+---------+-------------------+-----------+
 	 */
-	struct btf_header *hdr;
+	struct btf_header hdr;
 
 	void *types_data;
 	size_t types_data_cap; /* used size stored in hdr->type_len */
@@ -125,6 +136,13 @@ struct btf {
 	/* whether raw_data is a (read-only) mmap */
 	bool raw_data_is_mmap;
 
+	/* is BTF modifiable? i.e. is it split into separate sections as described above? */
+	bool modifiable;
+	/* Points either at raw kind layout data in parsed BTF (if present), or
+	 * at an allocated kind layout array when BTF is modifiable.
+	 */
+	void *layout;
+
 	/* BTF object FD, if loaded into kernel */
 	int fd;
 
@@ -216,7 +234,7 @@ static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
 	return 0;
 }
 
-static void btf_bswap_hdr(struct btf_header *h)
+static void btf_bswap_hdr(struct btf_header *h, __u32 hdr_len)
 {
 	h->magic = bswap_16(h->magic);
 	h->hdr_len = bswap_32(h->hdr_len);
@@ -224,66 +242,95 @@ static void btf_bswap_hdr(struct btf_header *h)
 	h->type_len = bswap_32(h->type_len);
 	h->str_off = bswap_32(h->str_off);
 	h->str_len = bswap_32(h->str_len);
+	/* May be operating on raw data with hdr_len that does not include below fields */
+	if (hdr_len >= sizeof(struct btf_header)) {
+		h->layout_off = bswap_32(h->layout_off);
+		h->layout_len = bswap_32(h->layout_len);
+	}
 }
 
 static int btf_parse_hdr(struct btf *btf)
 {
-	struct btf_header *hdr = btf->hdr;
+	struct btf_header *hdr = btf->raw_data;
+	__u32 hdr_len = hdr->hdr_len;
 	__u32 meta_left;
 
-	if (btf->raw_size < sizeof(struct btf_header)) {
+	if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
 		pr_debug("BTF header not found\n");
 		return -EINVAL;
 	}
 
 	if (hdr->magic == bswap_16(BTF_MAGIC)) {
 		btf->swapped_endian = true;
-		if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
+		hdr_len = bswap_32(hdr->hdr_len);
+		if (hdr_len < offsetofend(struct btf_header, str_len)) {
 			pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
-				bswap_32(hdr->hdr_len));
+				hdr_len);
 			return -ENOTSUP;
 		}
-		btf_bswap_hdr(hdr);
+		btf_bswap_hdr(hdr, hdr_len);
 	} else if (hdr->magic != BTF_MAGIC) {
 		pr_debug("Invalid BTF magic: %x\n", hdr->magic);
 		return -EINVAL;
 	}
 
-	if (btf->raw_size < hdr->hdr_len) {
+	if (btf->raw_size < hdr_len) {
 		pr_debug("BTF header len %u larger than data size %u\n",
-			 hdr->hdr_len, btf->raw_size);
+			 hdr_len, btf->raw_size);
 		return -EINVAL;
 	}
 
-	meta_left = btf->raw_size - hdr->hdr_len;
-	if (meta_left < (long long)hdr->str_off + hdr->str_len) {
+	memcpy(&btf->hdr, hdr, min((size_t)hdr_len, sizeof(struct btf_header)));
+
+	meta_left = btf->raw_size - hdr_len;
+	if (meta_left < (long long)btf->hdr.str_off + btf->hdr.str_len) {
 		pr_debug("Invalid BTF total size: %u\n", btf->raw_size);
 		return -EINVAL;
 	}
 
-	if ((long long)hdr->type_off + hdr->type_len > hdr->str_off) {
+	if ((long long)btf->hdr.type_off + btf->hdr.type_len > btf->hdr.str_off) {
 		pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n",
-			 hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len);
+			 btf->hdr.type_off, btf->hdr.type_len, btf->hdr.str_off,
+			 btf->hdr.str_len);
 		return -EINVAL;
 	}
 
-	if (hdr->type_off % 4) {
+	if (btf->hdr.type_off % 4) {
 		pr_debug("BTF type section is not aligned to 4 bytes\n");
 		return -EINVAL;
 	}
 
+	if (btf->hdr.layout_len == 0)
+		return 0;
+
+	/* optional layout section sits between types and strings */
+	if (btf->hdr.layout_off % 4) {
+		pr_debug("BTF layout section is not aligned to 4 bytes\n");
+		return -EINVAL;
+	}
+	if (btf->hdr.layout_off < (long long)btf->hdr.type_off + btf->hdr.type_len) {
+		pr_debug("Invalid BTF data sections layout: type data at %u + %u,  layout data at %u + %u\n",
+			 btf->hdr.type_off, btf->hdr.type_len,
+			 btf->hdr.layout_off, btf->hdr.layout_len);
+		return -EINVAL;
+	}
+	if ((long long)btf->hdr.layout_off + btf->hdr.layout_len > btf->hdr.str_off ||
+	    btf->hdr.layout_off > btf->hdr.str_off) {
+		pr_debug("Invalid BTF data sections layout: layout data at %u + %u, strings data at %u\n",
+			 btf->hdr.layout_off, btf->hdr.layout_len, btf->hdr.str_off);
+		return -EINVAL;
+	}
 	return 0;
 }
 
 static int btf_parse_str_sec(struct btf *btf)
 {
-	const struct btf_header *hdr = btf->hdr;
 	const char *start = btf->strs_data;
-	const char *end = start + btf->hdr->str_len;
+	const char *end = start + btf->hdr.str_len;
 
-	if (btf->base_btf && hdr->str_len == 0)
+	if (btf->base_btf && btf->hdr.str_len == 0)
 		return 0;
-	if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) {
+	if (!btf->hdr.str_len || btf->hdr.str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) {
 		pr_debug("Invalid BTF string section\n");
 		return -EINVAL;
 	}
@@ -294,6 +341,20 @@ static int btf_parse_str_sec(struct btf *btf)
 	return 0;
 }
 
+static int btf_parse_layout_sec(struct btf *btf)
+{
+	if (!btf->hdr.layout_len)
+		return 0;
+
+	if (btf->hdr.layout_len % sizeof(struct btf_layout) != 0) {
+		pr_debug("Invalid BTF kind layout section\n");
+		return -EINVAL;
+	}
+	btf->layout = btf->raw_data + btf->hdr.hdr_len + btf->hdr.layout_off;
+
+	return 0;
+}
+
 static int btf_type_size(const struct btf_type *t)
 {
 	const int base_size = sizeof(struct btf_type);
@@ -421,9 +482,8 @@ static int btf_bswap_type_rest(struct btf_type *t)
 
 static int btf_parse_type_sec(struct btf *btf)
 {
-	struct btf_header *hdr = btf->hdr;
 	void *next_type = btf->types_data;
-	void *end_type = next_type + hdr->type_len;
+	void *end_type = next_type + btf->hdr.type_len;
 	int err, type_size;
 
 	while (next_type + sizeof(struct btf_type) <= end_type) {
@@ -1012,7 +1072,8 @@ __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
 
 static bool btf_is_modifiable(const struct btf *btf)
 {
-	return (void *)btf->hdr != btf->raw_data;
+	/* BTF is modifiable if split into multiple sections */
+	return btf->modifiable;
 }
 
 static void btf_free_raw_data(struct btf *btf)
@@ -1036,14 +1097,14 @@ void btf__free(struct btf *btf)
 
 	if (btf_is_modifiable(btf)) {
 		/* if BTF was modified after loading, it will have a split
-		 * in-memory representation for header, types, and strings
+		 * in-memory representation for types, strings and layout
 		 * sections, so we need to free all of them individually. It
 		 * might still have a cached contiguous raw data present,
 		 * which will be unconditionally freed below.
 		 */
-		free(btf->hdr);
 		free(btf->types_data);
 		strset__free(btf->strs_set);
+		free(btf->layout);
 	}
 	btf_free_raw_data(btf);
 	free(btf->raw_data_swapped);
@@ -1055,6 +1116,7 @@ void btf__free(struct btf *btf)
 
 static struct btf *btf_new_empty(struct btf *base_btf)
 {
+	struct btf_header *hdr;
 	struct btf *btf;
 
 	btf = calloc(1, sizeof(*btf));
@@ -1072,7 +1134,7 @@ static struct btf *btf_new_empty(struct btf *base_btf)
 	if (base_btf) {
 		btf->base_btf = base_btf;
 		btf->start_id = btf__type_cnt(base_btf);
-		btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
+		btf->start_str_off = base_btf->hdr.str_len + base_btf->start_str_off;
 		btf->swapped_endian = base_btf->swapped_endian;
 	}
 
@@ -1084,14 +1146,15 @@ static struct btf *btf_new_empty(struct btf *base_btf)
 		return ERR_PTR(-ENOMEM);
 	}
 
-	btf->hdr = btf->raw_data;
-	btf->hdr->hdr_len = sizeof(struct btf_header);
-	btf->hdr->magic = BTF_MAGIC;
-	btf->hdr->version = BTF_VERSION;
+	hdr = btf->raw_data;
+	hdr->hdr_len = sizeof(struct btf_header);
+	hdr->magic = BTF_MAGIC;
+	hdr->version = BTF_VERSION;
 
-	btf->types_data = btf->raw_data + btf->hdr->hdr_len;
-	btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
-	btf->hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */
+	btf->types_data = btf->raw_data + hdr->hdr_len;
+	btf->strs_data = btf->raw_data + hdr->hdr_len;
+	hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */
+	memcpy(&btf->hdr, hdr, sizeof(*hdr));
 
 	return btf;
 }
@@ -1124,7 +1187,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b
 	if (base_btf) {
 		btf->base_btf = base_btf;
 		btf->start_id = btf__type_cnt(base_btf);
-		btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
+		btf->start_str_off = base_btf->hdr.str_len + base_btf->start_str_off;
 	}
 
 	if (is_mmap) {
@@ -1141,15 +1204,15 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b
 
 	btf->raw_size = size;
 
-	btf->hdr = btf->raw_data;
 	err = btf_parse_hdr(btf);
 	if (err)
 		goto done;
 
-	btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off;
-	btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off;
+	btf->strs_data = btf->raw_data + btf->hdr.hdr_len + btf->hdr.str_off;
+	btf->types_data = btf->raw_data + btf->hdr.hdr_len + btf->hdr.type_off;
 
 	err = btf_parse_str_sec(btf);
+	err = err ?: btf_parse_layout_sec(btf);
 	err = err ?: btf_parse_type_sec(btf);
 	err = err ?: btf_sanity_check(btf);
 	if (err)
@@ -1601,7 +1664,7 @@ static const void *btf_strs_data(const struct btf *btf)
 
 static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
 {
-	struct btf_header *hdr = btf->hdr;
+	const struct btf_header *hdr = &btf->hdr;
 	struct btf_type *t;
 	void *data, *p;
 	__u32 data_sz;
@@ -1614,14 +1677,17 @@ static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endi
 	}
 
 	data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len;
+	if (btf->layout)
+		data_sz += hdr->layout_len;
+
 	data = calloc(1, data_sz);
 	if (!data)
 		return NULL;
 	p = data;
 
-	memcpy(p, hdr, hdr->hdr_len);
+	memcpy(p, hdr, min((__u32)sizeof(struct btf_header), hdr->hdr_len));
 	if (swap_endian)
-		btf_bswap_hdr(p);
+		btf_bswap_hdr(p, hdr->hdr_len);
 	p += hdr->hdr_len;
 
 	memcpy(p, btf->types_data, hdr->type_len);
@@ -1639,8 +1705,12 @@ static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endi
 	}
 	p += hdr->type_len;
 
+	if (btf->layout) {
+		memcpy(p, btf->layout, hdr->layout_len);
+		p += hdr->layout_len;
+	}
+
 	memcpy(p, btf_strs_data(btf), hdr->str_len);
-	p += hdr->str_len;
 
 	*size = data_sz;
 	return data;
@@ -1675,7 +1745,7 @@ const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
 {
 	if (offset < btf->start_str_off)
 		return btf__str_by_offset(btf->base_btf, offset);
-	else if (offset - btf->start_str_off < btf->hdr->str_len)
+	else if (offset - btf->start_str_off < btf->hdr.str_len)
 		return btf_strs_data(btf) + (offset - btf->start_str_off);
 	else
 		return errno = EINVAL, NULL;
@@ -1783,12 +1853,12 @@ static void btf_invalidate_raw_data(struct btf *btf)
 }
 
 /* Ensure BTF is ready to be modified (by splitting into a three memory
- * regions for header, types, and strings). Also invalidate cached
+ * regions for types, strings and layout. Also invalidate cached
  * raw_data, if any.
  */
 static int btf_ensure_modifiable(struct btf *btf)
 {
-	void *hdr, *types;
+	void *types, *layout = NULL;
 	struct strset *set = NULL;
 	int err = -ENOMEM;
 
@@ -1798,45 +1868,53 @@ static int btf_ensure_modifiable(struct btf *btf)
 		return 0;
 	}
 
-	/* split raw data into three memory regions */
-	hdr = malloc(btf->hdr->hdr_len);
-	types = malloc(btf->hdr->type_len);
-	if (!hdr || !types)
+	/* split raw data into memory regions; btf->hdr is done already. */
+	types = malloc(btf->hdr.type_len);
+	if (!types)
 		goto err_out;
+	memcpy(types, btf->types_data, btf->hdr.type_len);
 
-	memcpy(hdr, btf->hdr, btf->hdr->hdr_len);
-	memcpy(types, btf->types_data, btf->hdr->type_len);
+	if (btf->hdr.layout_len) {
+		layout = malloc(btf->hdr.layout_len);
+		if (!layout)
+			goto err_out;
+		memcpy(layout, btf->raw_data + btf->hdr.hdr_len + btf->hdr.layout_off,
+		       btf->hdr.layout_len);
+	}
 
 	/* build lookup index for all strings */
-	set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr->str_len);
+	set = strset__new(BTF_MAX_STR_OFFSET, btf->strs_data, btf->hdr.str_len);
 	if (IS_ERR(set)) {
 		err = PTR_ERR(set);
 		goto err_out;
 	}
 
 	/* only when everything was successful, update internal state */
-	btf->hdr = hdr;
 	btf->types_data = types;
-	btf->types_data_cap = btf->hdr->type_len;
+	btf->types_data_cap = btf->hdr.type_len;
 	btf->strs_data = NULL;
 	btf->strs_set = set;
+	if (layout)
+		btf->layout = layout;
 	/* if BTF was created from scratch, all strings are guaranteed to be
 	 * unique and deduplicated
 	 */
-	if (btf->hdr->str_len == 0)
+	if (btf->hdr.str_len == 0)
 		btf->strs_deduped = true;
-	if (!btf->base_btf && btf->hdr->str_len == 1)
+	if (!btf->base_btf && btf->hdr.str_len == 1)
 		btf->strs_deduped = true;
 
 	/* invalidate raw_data representation */
 	btf_invalidate_raw_data(btf);
 
+	btf->modifiable = true;
+
 	return 0;
 
 err_out:
 	strset__free(set);
-	free(hdr);
 	free(types);
+	free(layout);
 	return err;
 }
 
@@ -1889,7 +1967,7 @@ int btf__add_str(struct btf *btf, const char *s)
 	if (off < 0)
 		return libbpf_err(off);
 
-	btf->hdr->str_len = strset__data_size(btf->strs_set);
+	btf->hdr.str_len = strset__data_size(btf->strs_set);
 
 	return btf->start_str_off + off;
 }
@@ -1897,7 +1975,7 @@ int btf__add_str(struct btf *btf, const char *s)
 static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
 {
 	return libbpf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
-			      btf->hdr->type_len, UINT_MAX, add_sz);
+			      btf->hdr.type_len, UINT_MAX, add_sz);
 }
 
 static void btf_type_inc_vlen(struct btf_type *t)
@@ -1905,16 +1983,31 @@ static void btf_type_inc_vlen(struct btf_type *t)
 	t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t));
 }
 
+static void btf_hdr_update_type_len(struct btf *btf, int new_len)
+{
+	btf->hdr.type_len = new_len;
+	if (btf->layout) {
+		btf->hdr.layout_off = new_len;
+		btf->hdr.str_off = btf->hdr.layout_off + btf->hdr.layout_len;
+	} else {
+		btf->hdr.str_off = new_len;
+	}
+}
+
+static void btf_hdr_update_str_len(struct btf *btf, int new_len)
+{
+	btf->hdr.str_len = new_len;
+}
+
 static int btf_commit_type(struct btf *btf, int data_sz)
 {
 	int err;
 
-	err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
+	err = btf_add_type_idx_entry(btf, btf->hdr.type_len);
 	if (err)
 		return libbpf_err(err);
 
-	btf->hdr->type_len += data_sz;
-	btf->hdr->str_off += data_sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + data_sz);
 	btf->nr_types++;
 	return btf->start_id + btf->nr_types - 1;
 }
@@ -2018,9 +2111,9 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
 	/* remember original strings section size if we have to roll back
 	 * partial strings section changes
 	 */
-	old_strs_len = btf->hdr->str_len;
+	old_strs_len = btf->hdr.str_len;
 
-	data_sz = src_btf->hdr->type_len;
+	data_sz = src_btf->hdr.type_len;
 	cnt = btf__type_cnt(src_btf) - 1;
 
 	/* pre-allocate enough memory for new types */
@@ -2094,8 +2187,7 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
 	 * update type count and various internal offsets and sizes to
 	 * "commit" the changes and made them visible to the outside world.
 	 */
-	btf->hdr->type_len += data_sz;
-	btf->hdr->str_off += data_sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + data_sz);
 	btf->nr_types += cnt;
 
 	hashmap__free(p.str_off_map);
@@ -2106,13 +2198,13 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
 	/* zero out preallocated memory as if it was just allocated with
 	 * libbpf_add_mem()
 	 */
-	memset(btf->types_data + btf->hdr->type_len, 0, data_sz);
-	memset(btf->strs_data + old_strs_len, 0, btf->hdr->str_len - old_strs_len);
+	memset(btf->types_data + btf->hdr.type_len, 0, data_sz);
+	memset(btf->strs_data + old_strs_len, 0, btf->hdr.str_len - old_strs_len);
 
 	/* and now restore original strings section size; types data size
 	 * wasn't modified, so doesn't need restoring, see big comment above
 	 */
-	btf->hdr->str_len = old_strs_len;
+	btf_hdr_update_str_len(btf, old_strs_len);
 
 	hashmap__free(p.str_off_map);
 
@@ -2430,8 +2522,7 @@ int btf__add_field(struct btf *btf, const char *name, int type_id,
 	/* update parent type's vlen and kflag */
 	t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
 
-	btf->hdr->type_len += sz;
-	btf->hdr->str_off += sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
 	return 0;
 }
 
@@ -2540,8 +2631,7 @@ int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
 	if (value < 0)
 		t->info = btf_type_info(btf_kind(t), btf_vlen(t), true);
 
-	btf->hdr->type_len += sz;
-	btf->hdr->str_off += sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
 	return 0;
 }
 
@@ -2612,8 +2702,7 @@ int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value)
 	t = btf_last_type(btf);
 	btf_type_inc_vlen(t);
 
-	btf->hdr->type_len += sz;
-	btf->hdr->str_off += sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
 	return 0;
 }
 
@@ -2851,8 +2940,7 @@ int btf__add_func_param(struct btf *btf, const char *name, int type_id)
 	t = btf_last_type(btf);
 	btf_type_inc_vlen(t);
 
-	btf->hdr->type_len += sz;
-	btf->hdr->str_off += sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
 	return 0;
 }
 
@@ -2988,8 +3076,7 @@ int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __
 	t = btf_last_type(btf);
 	btf_type_inc_vlen(t);
 
-	btf->hdr->type_len += sz;
-	btf->hdr->str_off += sz;
+	btf_hdr_update_type_len(btf, btf->hdr.type_len + sz);
 	return 0;
 }
 
@@ -3953,7 +4040,7 @@ static int btf_dedup_strings(struct btf_dedup *d)
 
 	/* replace BTF string data and hash with deduped ones */
 	strset__free(d->btf->strs_set);
-	d->btf->hdr->str_len = strset__data_size(d->strs_set);
+	btf_hdr_update_str_len(d->btf, strset__data_size(d->strs_set));
 	d->btf->strs_set = d->strs_set;
 	d->strs_set = NULL;
 	d->btf->strs_deduped = true;
@@ -5400,14 +5487,17 @@ static int btf_dedup_compact_types(struct btf_dedup *d)
 	/* shrink struct btf's internal types index and update btf_header */
 	d->btf->nr_types = next_type_id - d->btf->start_id;
 	d->btf->type_offs_cap = d->btf->nr_types;
-	d->btf->hdr->type_len = p - d->btf->types_data;
+	d->btf->hdr.type_len = p - d->btf->types_data;
 	new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap,
 				       sizeof(*new_offs));
 	if (d->btf->type_offs_cap && !new_offs)
 		return -ENOMEM;
 	d->btf->type_offs = new_offs;
-	d->btf->hdr->str_off = d->btf->hdr->type_len;
-	d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len;
+	if (d->btf->layout)
+		d->btf->hdr.layout_off = d->btf->hdr.type_len;
+	d->btf->hdr.str_off = d->btf->hdr.type_len + d->btf->hdr.layout_len;
+	d->btf->raw_size = d->btf->hdr.hdr_len + d->btf->hdr.type_len + d->btf->hdr.layout_len +
+			   d->btf->hdr.str_len;
 	return 0;
 }
 
@@ -5865,7 +5955,7 @@ int btf__distill_base(const struct btf *src_btf, struct btf **new_base_btf,
 		goto done;
 	}
 	dist.split_start_id = btf__type_cnt(old_base);
-	dist.split_start_str = old_base->hdr->str_len;
+	dist.split_start_str = old_base->hdr.str_len;
 
 	/* Pass over src split BTF; generate the list of base BTF type ids it
 	 * references; these will constitute our distilled BTF set to be
@@ -5934,14 +6024,14 @@ int btf__distill_base(const struct btf *src_btf, struct btf **new_base_btf,
 
 const struct btf_header *btf_header(const struct btf *btf)
 {
-	return btf->hdr;
+	return &btf->hdr;
 }
 
 void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
 {
 	btf->base_btf = (struct btf *)base_btf;
 	btf->start_id = btf__type_cnt(base_btf);
-	btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
+	btf->start_str_off = base_btf->hdr.str_len + base_btf->start_str_off;
 }
 
 int btf__relocate(struct btf *btf, const struct btf *base_btf)
@@ -6008,7 +6098,7 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
 		goto done;
 	}
 
-	new_types = calloc(btf->hdr->type_len, 1);
+	new_types = calloc(btf->hdr.type_len, 1);
 	if (!new_types) {
 		err = -ENOMEM;
 		goto done;
Signature · 1 line
2.39.3
#6 lore raw

Re: [PATCH v10 bpf-next 02/10] libbpf: Support layout section handling in BTF

Could the read of hdr->hdr_len at offset 4 above be an out-of-bounds access when raw_size is less than 8?
Quoted 4 lines
tools/lib/bpf/btf.c +0 -0
index 83fe79ffcb8f..7b58724a90a2 100644

[ ... ]

tools/lib/bpf/btf.c static void btf_bswap_hdr(struct btf_header *h) · line 261

Quoted 12 lines

static int btf_parse_hdr(struct btf *btf)
{

tools/lib/bpf/btf.c +3 -2
-	struct btf_header *hdr = btf->hdr;
+	struct btf_header *hdr = btf->raw_data;
+	__u32 hdr_len = hdr->hdr_len;
 	__u32 meta_left;
 
-	if (btf->raw_size < sizeof(struct btf_header)) {
+	if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
 		pr_debug("BTF header not found\n");
 		return -EINVAL;
 	}

Could the read of hdr->hdr_len at offset 4 above be an out-of-bounds access when raw_size is less than 8?

The previous code set hdr as a pointer without dereferencing it, then checked raw_size before accessing any header fields. The new code reads hdr->hdr_len before the raw_size check.

btf_new() calls btf_parse_hdr() with whatever size the caller provided via btf__new() or btf_parse_elf(), so small or corrupt input could reach here. On the mmap path the data could sit at a page boundary where offset 4 would fault.

Moving the initialization of hdr_len to after the size check would restore the pre-patch safety:

    struct btf_header *hdr = btf->raw_data;
    __u32 hdr_len;
    __u32 meta_left;

    if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
        ...
    }

    hdr_len = hdr->hdr_len;

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22482435858

AI-authorship-score: low
AI-authorship-explanation: Patch went through 10 review iterations on lore with human interactions; the subtle ordering bug is characteristic of human-authored refactoring.
issues-found: 1
issue-severity-score: medium
issue-severity-explanation: btf_parse_hdr() reads hdr->hdr_len from raw_data before validating raw_size, causing an out-of-bounds heap read on small or corrupt input via the public btf__new() API.

#7 lore raw

Re: [PATCH v10 bpf-next 02/10] libbpf: Support layout section handling in BTF

Quoted 36 lines · On Fri, Feb 27, 2026 at 2:52 AM <[email protected]> wrote:
Quoted 5 lines
tools/lib/bpf/btf.c +0 -0
index 83fe79ffcb8f..7b58724a90a2 100644

[ ... ]

tools/lib/bpf/btf.c static void btf_bswap_hdr(struct btf_header *h) · line 261

Quoted 12 lines

static int btf_parse_hdr(struct btf *btf)
{

tools/lib/bpf/btf.c +3 -2
-     struct btf_header *hdr = btf->hdr;
+     struct btf_header *hdr = btf->raw_data;
+     __u32 hdr_len = hdr->hdr_len;
      __u32 meta_left;
 
-     if (btf->raw_size < sizeof(struct btf_header)) {
+     if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
              pr_debug("BTF header not found\n");
              return -EINVAL;
      }

Could the read of hdr->hdr_len at offset 4 above be an out-of-bounds access when raw_size is less than 8?

The previous code set hdr as a pointer without dereferencing it, then checked raw_size before accessing any header fields. The new code reads hdr->hdr_len before the raw_size check.

btf_new() calls btf_parse_hdr() with whatever size the caller provided via btf__new() or btf_parse_elf(), so small or corrupt input could reach here. On the mmap path the data could sit at a page boundary where offset 4 would fault.

Moving the initialization of hdr_len to after the size check would restore the pre-patch safety:

Quoted 9 lines
    struct btf_header *hdr = btf->raw_data;
    __u32 hdr_len;
    __u32 meta_left;

    if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
        ...
    }

    hdr_len = hdr->hdr_len;

bot is correct.
Let's avoid potential oob read.

#8 lore raw

PATCH v10 3/10 libbpf: use layout to compute an unknown kind size

In reply to Alan Maguire

This allows BTF parsing to proceed even if we do not know the kind. Fall back to base BTF layout if layout information is not in split BTF.

---
 tools/lib/bpf/btf.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
tools/lib/bpf/btf.c +33 -9
index 7b58724a90a2..db13c15a0049 100644
@@ -355,7 +355,32 @@ static int btf_parse_layout_sec(struct btf *btf)
 	return 0;
 }
 
-static int btf_type_size(const struct btf_type *t)
+/* for unknown kinds, consult kind layout. */
+static int btf_type_size_unknown(const struct btf *btf, const struct btf_type *t)
+{
+	__u32 layout_kinds = btf->hdr.layout_len / sizeof(struct btf_layout);
+	struct btf_layout *l = btf->layout;
+	__u16 vlen = btf_vlen(t);
+	__u32 kind = btf_kind(t);
+
+	/* Fall back to base BTF if needed as they share layout information */
+	if (!l) {
+		struct btf *base_btf = btf->base_btf;
+
+		if (base_btf) {
+			l = base_btf->layout;
+			layout_kinds = base_btf->hdr.layout_len / sizeof(struct btf_layout);
+		}
+	}
+	if (!l || kind >= layout_kinds) {
+		pr_debug("Unsupported BTF_KIND: %u\n", btf_kind(t));
+		return -EINVAL;
+	}
+
+	return sizeof(struct btf_type) + l[kind].info_sz + vlen * l[kind].elem_sz;
+}
+
+static int btf_type_size(const struct btf *btf, const struct btf_type *t)
 {
 	const int base_size = sizeof(struct btf_type);
 	__u16 vlen = btf_vlen(t);
@@ -391,8 +416,7 @@ static int btf_type_size(const struct btf_type *t)
 	case BTF_KIND_DECL_TAG:
 		return base_size + sizeof(struct btf_decl_tag);
 	default:
-		pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
-		return -EINVAL;
+		return btf_type_size_unknown(btf, t);
 	}
 }
 
@@ -490,7 +514,7 @@ static int btf_parse_type_sec(struct btf *btf)
 		if (btf->swapped_endian)
 			btf_bswap_type_base(next_type);
 
-		type_size = btf_type_size(next_type);
+		type_size = btf_type_size(btf, next_type);
 		if (type_size < 0)
 			return type_size;
 		if (next_type + type_size > end_type) {
@@ -2056,7 +2080,7 @@ static int btf_add_type(struct btf_pipe *p, const struct btf_type *src_type)
 	__u32 *str_off;
 	int sz, err;
 
-	sz = btf_type_size(src_type);
+	sz = btf_type_size(p->src, src_type);
 	if (sz < 0)
 		return libbpf_err(sz);
 
@@ -2138,7 +2162,7 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
 		struct btf_field_iter it;
 		__u32 *type_id, *str_off;
 
-		sz = btf_type_size(t);
+		sz = btf_type_size(src_btf, t);
 		if (sz < 0) {
 			/* unlikely, has to be corrupted src_btf */
 			err = sz;
@@ -5473,7 +5497,7 @@ static int btf_dedup_compact_types(struct btf_dedup *d)
 			continue;
 
 		t = btf__type_by_id(d->btf, id);
-		len = btf_type_size(t);
+		len = btf_type_size(d->btf, t);
 		if (len < 0)
 			return len;
 
@@ -6136,7 +6160,7 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
 
 		id = order_map[i];
 		t = btf__type_by_id(btf, id);
-		type_size = btf_type_size(t);
+		type_size = btf_type_size(btf, t);
 		memcpy(nt, t, type_size);
 
 		/* fix up referenced IDs for BTF */
@@ -6162,7 +6186,7 @@ int btf__permute(struct btf *btf, __u32 *id_map, __u32 id_map_cnt,
 
 	for (nt = new_types, i = 0; i < id_map_cnt - start_offs; i++) {
 		btf->type_offs[i] = nt - new_types;
-		nt += btf_type_size(nt);
+		nt += btf_type_size(btf, nt);
 	}
 
 	free(order_map);
Signature · 1 line
2.39.3
#9 lore raw

PATCH v10 4/10 libbpf: Add layout encoding support

In reply to Alan Maguire

Support encoding of BTF layout data via btf__new_empty_opts().

Current supported opts are base_btf and add_layout.

Layout information is maintained in btf.c in the layouts[] array; when BTF is created with the add_layout option it represents the current view of supported BTF kinds.

---
 tools/lib/bpf/btf.c      | 62 ++++++++++++++++++++++++++++++++++++++--
 tools/lib/bpf/btf.h      | 20 +++++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 80 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +59 -3
index db13c15a0049..0c7fc0b49f20 100644
@@ -29,6 +29,35 @@
 
 static struct btf_type btf_void;
 
+/* Describe how kinds are laid out; some have a singular element following the "struct btf_type",
+ * some have BTF_INFO_VLEN(t->info) elements.  Specify sizes for both.  Flags are currently unused.
+ * Kind layout can be optionally added to the BTF representation in a dedicated section to
+ * facilitate parsing.  New kinds must be added here.
+ */
+static struct btf_layout layouts[NR_BTF_KINDS] = {
+/*				singular element size		vlen element(s) size		flags */
+[BTF_KIND_UNKN] =	{	0,				0,				0 },
+[BTF_KIND_INT] =	{	sizeof(__u32),			0,				0 },
+[BTF_KIND_PTR] =	{	0,				0,				0 },
+[BTF_KIND_ARRAY] =	{	sizeof(struct btf_array),	0,				0 },
+[BTF_KIND_STRUCT] =	{	0,				sizeof(struct btf_member),	0 },
+[BTF_KIND_UNION] =	{	0,				sizeof(struct btf_member),	0 },
+[BTF_KIND_ENUM] =	{	0,				sizeof(struct btf_enum),	0 },
+[BTF_KIND_FWD] =	{	0,				0,				0 },
+[BTF_KIND_TYPEDEF] =	{	0,				0,				0 },
+[BTF_KIND_VOLATILE] =	{	0,				0,				0 },
+[BTF_KIND_CONST] =	{	0,				0,				0 },
+[BTF_KIND_RESTRICT] =	{	0,				0,				0 },
+[BTF_KIND_FUNC] =	{	0,				0,				0 },
+[BTF_KIND_FUNC_PROTO] =	{	0,				sizeof(struct btf_param),	0 },
+[BTF_KIND_VAR] =	{	sizeof(struct btf_var),		0,				0 },
+[BTF_KIND_DATASEC] =	{	0,				sizeof(struct btf_var_secinfo),	0 },
+[BTF_KIND_FLOAT] =	{	0,				0,				0 },
+[BTF_KIND_DECL_TAG] =	{	sizeof(struct btf_decl_tag),	0,				0 },
+[BTF_KIND_TYPE_TAG] =	{	0,				0,				0 },
+[BTF_KIND_ENUM64] =	{	0,				sizeof(struct btf_enum64),	0 },
+};
+
 struct btf {
 	/* raw BTF data in native endianness */
 	void *raw_data;
@@ -1138,8 +1167,10 @@ void btf__free(struct btf *btf)
 	free(btf);
 }
 
-static struct btf *btf_new_empty(struct btf *base_btf)
+static struct btf *btf_new_empty(struct btf_new_opts *opts)
 {
+	bool add_layout = OPTS_GET(opts, add_layout, false);
+	struct btf *base_btf = OPTS_GET(opts, base_btf, NULL);
 	struct btf_header *hdr;
 	struct btf *btf;
 
@@ -1164,6 +1195,8 @@ static struct btf *btf_new_empty(struct btf *base_btf)
 
 	/* +1 for empty string at offset 0 */
 	btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1);
+	if (add_layout)
+		btf->raw_size += sizeof(layouts);
 	btf->raw_data = calloc(1, btf->raw_size);
 	if (!btf->raw_data) {
 		free(btf);
@@ -1178,6 +1211,15 @@ static struct btf *btf_new_empty(struct btf *base_btf)
 	btf->types_data = btf->raw_data + hdr->hdr_len;
 	btf->strs_data = btf->raw_data + hdr->hdr_len;
 	hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */
+
+	if (add_layout) {
+		hdr->layout_len = sizeof(layouts);
+		btf->layout = btf->raw_data + hdr->hdr_len;
+		memcpy(btf->layout, layouts, sizeof(layouts));
+		btf->strs_data += sizeof(layouts);
+		hdr->str_off += sizeof(layouts);
+	}
+
 	memcpy(&btf->hdr, hdr, sizeof(*hdr));
 
 	return btf;
@@ -1185,12 +1227,26 @@ static struct btf *btf_new_empty(struct btf *base_btf)
 
 struct btf *btf__new_empty(void)
 {
-	return libbpf_ptr(btf_new_empty(NULL));
+	LIBBPF_OPTS(btf_new_opts, opts);
+
+	return libbpf_ptr(btf_new_empty(&opts));
 }
 
 struct btf *btf__new_empty_split(struct btf *base_btf)
 {
-	return libbpf_ptr(btf_new_empty(base_btf));
+	LIBBPF_OPTS(btf_new_opts, opts);
+
+	opts.base_btf = base_btf;
+
+	return libbpf_ptr(btf_new_empty(&opts));
+}
+
+struct btf *btf__new_empty_opts(struct btf_new_opts *opts)
+{
+	if (!OPTS_VALID(opts, btf_new_opts))
+		return libbpf_err_ptr(-EINVAL);
+
+	return libbpf_ptr(btf_new_empty(opts));
 }
 
 static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, bool is_mmap)
tools/lib/bpf/btf.h +20 -0
index b30008c267c0..a1f8deca2603 100644
@@ -109,6 +109,26 @@ LIBBPF_API struct btf *btf__new_empty(void);
  */
 LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);
 
+struct btf_new_opts {
+	size_t sz;
+	struct btf *base_btf;	/* optional base BTF */
+	bool add_layout;	/* add BTF layout information */
+	size_t:0;
+};
+#define btf_new_opts__last_field add_layout
+
+/**
+ * @brief **btf__new_empty_opts()** creates an unpopulated BTF object with
+ * optional *base_btf* and BTF kind layout description if *add_layout*
+ * is set
+ * @return new BTF object instance which has to be eventually freed with
+ * **btf__free()**
+ *
+ * On error, NULL is returned and the thread-local `errno` variable is
+ * set to the error code.
+ */
+LIBBPF_API struct btf *btf__new_empty_opts(struct btf_new_opts *opts);
+
 /**
  * @brief **btf__distill_base()** creates new versions of the split BTF
  * *src_btf* and its base BTF. The new base BTF will only contain the types
tools/lib/bpf/libbpf.map +1 -0
index d18fbcea7578..0f740fb27a12 100644
@@ -453,5 +453,6 @@ LIBBPF_1.7.0 {
 		bpf_map__exclusive_program;
 		bpf_prog_assoc_struct_ops;
 		bpf_program__assoc_struct_ops;
+		btf__new_empty_opts;
 		btf__permute;
 } LIBBPF_1.6.0;
Signature · 1 line
2.39.3
#10 lore raw

Re: [PATCH v10 bpf-next 04/10] libbpf: Add layout encoding support

Quoted 21 lines · Alan Maguire

Support encoding of BTF layout data via btf__new_empty_opts().

Current supported opts are base_btf and add_layout.

Layout information is maintained in btf.c in the layouts[] array; when BTF is created with the add_layout option it represents the current view of supported BTF kinds.

---
 tools/lib/bpf/btf.c      | 62 ++++++++++++++++++++++++++++++++++++++--
 tools/lib/bpf/btf.h      | 20 +++++++++++++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 80 insertions(+), 3 deletions(-)
tools/lib/bpf/btf.c +0 -0
index db13c15a0049..0c7fc0b49f20 100644

tools/lib/bpf/btf.c line 36

Quoted 8 lines
tools/lib/bpf/btf.c +5 -0
@@ -29,6 +29,35 @@
 
 static struct btf_type btf_void;
 
+/* Describe how kinds are laid out; some have a singular element following the "struct btf_type",
+ * some have BTF_INFO_VLEN(t->info) elements.  Specify sizes for both.  Flags are currently unused.
+ * Kind layout can be optionally added to the BTF representation in a dedicated section to
+ * facilitate parsing.  New kinds must be added here.
+ */

Pls use proper kernel comment style in any new code.

#11 lore raw

PATCH v10 5/10 libbpf: BTF validation can use layout for unknown kinds

In reply to Alan Maguire

BTF parsing can use layout to navigate unknown kinds, so btf_validate_type() should take layout information into account to avoid failure when an unrecognized kind is met.

---
 tools/lib/bpf/btf.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/lib/bpf/btf.c +6 -2
index 0c7fc0b49f20..d33208fdbbfd 100644
@@ -704,8 +704,12 @@ static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __
 		break;
 	}
 	default:
-		pr_warn("btf: type [%u]: unrecognized kind %u\n", id, kind);
-		return -EINVAL;
+		/* Kind may be represented in kind layout information. */
+		if (btf_type_size_unknown(btf, t) < 0) {
+			pr_warn("btf: type [%u]: unrecognized kind %u\n", id, kind);
+			return -EINVAL;
+		}
+		break;
 	}
 	return 0;
 }
Signature · 1 line
2.39.3
#12 lore raw

PATCH v10 6/10 btf: support kernel parsing of BTF with layout info

In reply to Alan Maguire

Validate layout if present, but because the kernel must be strict in what it accepts, reject BTF with unsupported kinds, even if they are in the layout information.

---
 kernel/bpf/btf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 46 insertions(+), 4 deletions(-)
kernel/bpf/btf.c +46 -4
index 4872d2a6c42d..e5a9774907fe 100644
@@ -270,6 +270,7 @@ struct btf {
 	struct btf_id_dtor_kfunc_tab *dtor_kfunc_tab;
 	struct btf_struct_metas *struct_meta_tab;
 	struct btf_struct_ops_tab *struct_ops_tab;
+	struct btf_layout *layout;
 
 	/* split BTF support */
 	struct btf *base_btf;
@@ -5517,7 +5518,8 @@ static int btf_parse_str_sec(struct btf_verifier_env *env)
 	start = btf->nohdr_data + hdr->str_off;
 	end = start + hdr->str_len;
 
-	if (end != btf->data + btf->data_size) {
+	if (hdr->hdr_len < sizeof(struct btf_header) &&
+	    end != btf->data + btf->data_size) {
 		btf_verifier_log(env, "String section is not at the end");
 		return -EINVAL;
 	}
@@ -5538,9 +5540,41 @@ static int btf_parse_str_sec(struct btf_verifier_env *env)
 	return 0;
 }
 
+static int btf_parse_layout_sec(struct btf_verifier_env *env)
+{
+	const struct btf_header *hdr = &env->btf->hdr;
+	struct btf *btf = env->btf;
+	void *start, *end;
+
+	if (hdr->hdr_len < sizeof(struct btf_header) ||
+	    hdr->layout_len == 0)
+		return 0;
+
+	/* Llayout section must align to 4 bytes */
+	if (hdr->layout_off & (sizeof(u32) - 1)) {
+		btf_verifier_log(env, "Unaligned layout_off");
+		return -EINVAL;
+	}
+	start = btf->nohdr_data + hdr->layout_off;
+	end = start + hdr->layout_len;
+
+	if (hdr->layout_len < sizeof(struct btf_layout)) {
+		btf_verifier_log(env, "Layout section is too small");
+		return -EINVAL;
+	}
+	if (end > btf->data + btf->data_size) {
+		btf_verifier_log(env, "Layout section is too big");
+		return -EINVAL;
+	}
+	btf->layout = start;
+
+	return 0;
+}
+
 static const size_t btf_sec_info_offset[] = {
 	offsetof(struct btf_header, type_off),
 	offsetof(struct btf_header, str_off),
+	offsetof(struct btf_header, layout_off)
 };
 
 static int btf_sec_info_cmp(const void *a, const void *b)
@@ -5556,24 +5590,28 @@ static int btf_check_sec_info(struct btf_verifier_env *env,
 {
 	struct btf_sec_info secs[ARRAY_SIZE(btf_sec_info_offset)];
 	u32 total, expected_total, i;
+	u32 nr_secs = ARRAY_SIZE(btf_sec_info_offset);
 	const struct btf_header *hdr;
 	const struct btf *btf;
 
 	btf = env->btf;
 	hdr = &btf->hdr;
 
+	if (hdr->hdr_len < sizeof(struct btf_header) || hdr->layout_len == 0)
+		nr_secs--;
+
 	/* Populate the secs from hdr */
-	for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++)
+	for (i = 0; i < nr_secs; i++)
 		secs[i] = *(struct btf_sec_info *)((void *)hdr +
 						   btf_sec_info_offset[i]);
 
-	sort(secs, ARRAY_SIZE(btf_sec_info_offset),
+	sort(secs, nr_secs,
 	     sizeof(struct btf_sec_info), btf_sec_info_cmp, NULL);
 
 	/* Check for gaps and overlap among sections */
 	total = 0;
 	expected_total = btf_data_size - hdr->hdr_len;
-	for (i = 0; i < ARRAY_SIZE(btf_sec_info_offset); i++) {
+	for (i = 0; i < nr_secs; i++) {
 		if (expected_total < secs[i].off) {
 			btf_verifier_log(env, "Invalid section offset");
 			return -EINVAL;
@@ -5929,6 +5967,10 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
 	if (err)
 		goto errout;
 
+	err = btf_parse_layout_sec(env);
+	if (err)
+		goto errout;
+
 	err = btf_parse_type_sec(env);
 	if (err)
 		goto errout;
Signature · 1 line
2.39.3
#13 lore raw

Re: [PATCH v10 bpf-next 06/10] btf: support kernel parsing of BTF with layout info

Should layout_len also be checked for alignment to sizeof(struct btf_layout)? The libbpf counterpart in btf_parse_layout_sec() validates this:
Quoted 4 lines
kernel/bpf/btf.c +0 -0
index 4872d2a6c42d..e5a9774907fe 100644

[ ... ]

Quoted 12 lines
kernel/bpf/btf.c +12 -0
+static int btf_parse_layout_sec(struct btf_verifier_env *env)
+{
+	const struct btf_header *hdr = &env->btf->hdr;
+	struct btf *btf = env->btf;
+	void *start, *end;
+
+	if (hdr->hdr_len < sizeof(struct btf_header) ||
+	    hdr->layout_len == 0)
+		return 0;
+
+	/* Llayout section must align to 4 bytes */
+	if (hdr->layout_off & (sizeof(u32) - 1)) {

kernel/bpf/btf.c static int btf_parse_str_sec(struct btf_verifier_env *env) · line 5564

Quoted 10 lines
kernel/bpf/btf.c +10 -0
+		btf_verifier_log(env, "Unaligned layout_off");
+		return -EINVAL;
+	}
+	start = btf->nohdr_data + hdr->layout_off;
+	end = start + hdr->layout_len;
+
+	if (hdr->layout_len < sizeof(struct btf_layout)) {
+		btf_verifier_log(env, "Layout section is too small");
+		return -EINVAL;
+	}

Should layout_len also be checked for alignment to sizeof(struct btf_layout)? The libbpf counterpart in btf_parse_layout_sec() validates this:

    if (btf->hdr.layout_len % sizeof(struct btf_layout) != 0) {
        pr_debug("Invalid BTF kind layout section\n");
        return -EINVAL;
    }

Without this check the kernel accepts a layout section with a partial trailing entry (e.g. layout_len = 5 passes the >= 4 check here). The commit message notes "the kernel must be strict in what it accepts," so matching the libbpf validation seems appropriate.

kernel/bpf/btf.c static int btf_parse_str_sec(struct btf_verifier_env *env) · line 5572

Quoted 8 lines
kernel/bpf/btf.c +8 -0
+	if (end > btf->data + btf->data_size) {
+		btf_verifier_log(env, "Layout section is too big");
+		return -EINVAL;
+	}
+	btf->layout = start;
+
+	return 0;
+}

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22482435858

AI-authorship-score: low
AI-authorship-explanation: Part of a v10 series with extensive mailing list discussion, demonstrates deep kernel BTF expertise, and the Llayout typo is a characteristically human error.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Missing layout_len modular validation allows the kernel to accept malformed BTF that libbpf rejects, but no kernel code currently iterates over layout entries so the impact is limited to inconsistent validation.

#14 lore raw

PATCH v10 7/10 selftests/bpf: test kind encoding/decoding

In reply to Alan Maguire

verify btf__new_empty_opts() adds layouts for all kinds supported, and after adding kind-related types for an unknown kind, ensure that parsing uses this info when that kind is encountered rather than giving up.

---
 .../selftests/bpf/prog_tests/btf_kind.c       | 206 ++++++++++++++++++
 1 file changed, 206 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_kind.c
tools/testing/selftests/bpf/prog_tests/btf_kind.c new file +206 -0
new file mode 100644
index 000000000000..09246dc61f5a
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, Oracle and/or its affiliates. */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+#include <bpf/libbpf.h>
+
+/* verify kind encoding exists for each kind */
+static void test_btf_kind_encoding(void)
+{
+	LIBBPF_OPTS(btf_new_opts, opts);
+	const struct btf_header *hdr;
+	const void *raw_btf;
+	struct btf *btf;
+	__u32 raw_size;
+
+	opts.add_layout = true;
+	btf = btf__new_empty_opts(&opts);
+	if (!ASSERT_OK_PTR(btf, "btf_new"))
+		return;
+
+	raw_btf = btf__raw_data(btf, &raw_size);
+	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
+		return;
+
+	hdr = raw_btf;
+
+	ASSERT_EQ(hdr->layout_off % 4, 0, "layout_aligned");
+	ASSERT_EQ(hdr->layout_len, sizeof(struct btf_layout) * NR_BTF_KINDS,
+		  "layout_len");
+	ASSERT_EQ(hdr->str_off, hdr->layout_off + hdr->layout_len, "str_after_layout");
+	btf__free(btf);
+
+	opts.add_layout = false;
+	btf = btf__new_empty_opts(&opts);
+	if (!ASSERT_OK_PTR(btf, "btf_new"))
+		return;
+
+	raw_btf = btf__raw_data(btf, &raw_size);
+	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
+		return;
+
+	hdr = raw_btf;
+
+	ASSERT_EQ(hdr->layout_off, 0, "no_layout_off");
+	ASSERT_EQ(hdr->layout_len, 0, "no_layout_len");
+	ASSERT_EQ(hdr->str_off, hdr->type_off + hdr->type_len, "strs_after_types");
+	btf__free(btf);
+}
+
+static void write_raw_btf(const char *btf_path, void *raw_btf, size_t raw_size)
+{
+	int fd = open(btf_path, O_WRONLY | O_CREAT);
+
+	write(fd, raw_btf, raw_size);
+	close(fd);
+}
+
+/* fabricate an unrecognized kind at BTF_KIND_MAX + 1, and after adding
+ * the appropriate struct/typedefs to the BTF such that it recognizes
+ * this kind, ensure that parsing of BTF containing the unrecognized kind
+ * can succeed.
+ */
+void test_btf_kind_decoding(void)
+{
+	__s32 int_id, unrec_id, id, id2;
+	LIBBPF_OPTS(btf_new_opts, opts);
+	struct btf *btf, *new_btf;
+	struct btf_layout *l;
+	struct btf_header *hdr;
+	const void *raw_btf;
+	struct btf_type *t;
+	char btf_path[64];
+	void *new_raw_btf;
+	void *str_data;
+	__u32 raw_size;
+
+	opts.add_layout = true;
+	btf = btf__new_empty_opts(&opts);
+	if (!ASSERT_OK_PTR(btf, "btf_new"))
+		return;
+
+	int_id = btf__add_int(btf, "test_char", 1, BTF_INT_CHAR);
+	if (!ASSERT_GT(int_id, 0, "add_int_id"))
+		return;
+
+	/* now create our type with unrecognized kind by adding a typedef kind
+	 * we will overwrite it with our unrecognized kind value.
+	 */
+	unrec_id = btf__add_typedef(btf, "unrec_kind", int_id);
+	if (!ASSERT_GT(unrec_id, 0, "add_unrec_id"))
+		return;
+
+	/* add an id after it that we will look up to verify we can parse
+	 * beyond unrecognized kinds.
+	 */
+	id = btf__add_typedef(btf, "test_lookup", int_id);
+	if (!ASSERT_GT(id, 0, "add_test_lookup_id"))
+		return;
+	id2 = btf__add_typedef(btf, "test_lookup2", int_id);
+	if (!ASSERT_GT(id2, 0, "add_test_lookup_id2"))
+		return;
+
+	raw_btf = (void *)btf__raw_data(btf, &raw_size);
+	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
+		return;
+
+	new_raw_btf = calloc(1, raw_size + sizeof(*l));
+	if (!ASSERT_OK_PTR(new_raw_btf, "calloc_raw_btf"))
+		return;
+	memcpy(new_raw_btf, raw_btf, raw_size);
+
+	hdr = new_raw_btf;
+
+	/* move strings to make space for one new layout description */
+	raw_size += sizeof(*l);
+	str_data = new_raw_btf + hdr->hdr_len + hdr->str_off;
+	memmove(str_data + sizeof(*l), str_data, hdr->str_len);
+	hdr->str_off += sizeof(*l);
+
+	/* add new layout description */
+	hdr->layout_len += sizeof(*l);
+	l = new_raw_btf + hdr->hdr_len + hdr->layout_off;
+	l[NR_BTF_KINDS].info_sz = 0;
+	l[NR_BTF_KINDS].elem_sz = 0;
+	l[NR_BTF_KINDS].flags = 0;
+
+	/* now modify our typedef added above to be an unrecognized kind. */
+	t = (void *)hdr + hdr->hdr_len + hdr->type_off + sizeof(struct btf_type) +
+		sizeof(__u32);
+	t->info = (NR_BTF_KINDS << 24);
+
+	/* now write our BTF to a raw file, ready for parsing. */
+	snprintf(btf_path, sizeof(btf_path), "/tmp/btf_kind.%d", getpid());
+
+	write_raw_btf(btf_path, new_raw_btf, raw_size);
+
+	/* verify parsing succeeds, and that we can read type info past
+	 * the unrecognized kind.
+	 */
+	new_btf = btf__parse_raw(btf_path);
+	if (ASSERT_OK_PTR(new_btf, "btf__parse_raw")) {
+		ASSERT_EQ(btf__find_by_name(new_btf, "unrec_kind"), unrec_id,
+			  "unrec_kind_found");
+		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup",
+						 BTF_KIND_TYPEDEF), id,
+			  "verify_id_lookup");
+		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup2",
+						 BTF_KIND_TYPEDEF), id2,
+			  "verify_id2_lookup");
+	}
+	btf__free(new_btf);
+
+	/* next, change info_sz to equal sizeof(struct btf_type); this means the
+	 * "test_lookup" kind will be reinterpreted as a singular info element
+	 * following the unrecognized kind.
+	 */
+	l[NR_BTF_KINDS].info_sz = sizeof(struct btf_type);
+	write_raw_btf(btf_path, new_raw_btf, raw_size);
+
+	new_btf = btf__parse_raw(btf_path);
+	if (ASSERT_OK_PTR(new_btf, "btf__parse_raw")) {
+		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup",
+						 BTF_KIND_TYPEDEF), -ENOENT,
+			  "verify_id_not_found");
+		/* id of "test_lookup2" will be id2 -1 as we have removed one type */
+		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup2",
+						 BTF_KIND_TYPEDEF), id2 - 1,
+			  "verify_id_lookup2");
+
+	}
+	btf__free(new_btf);
+
+	/* next, change elem_sz to equal sizeof(struct btf_type)/2 and set
+	 * vlen associated with unrecognized type to 2; this allows us to verify
+	 * vlen-specified BTF can still be parsed.
+	 */
+	l[NR_BTF_KINDS].info_sz = 0;
+	l[NR_BTF_KINDS].elem_sz = sizeof(struct btf_type)/2;
+	t->info |= 2;
+	write_raw_btf(btf_path, new_raw_btf, raw_size);
+
+	new_btf = btf__parse_raw(btf_path);
+	if (ASSERT_OK_PTR(new_btf, "btf__parse_raw")) {
+		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup",
+						 BTF_KIND_TYPEDEF), -ENOENT,
+			  "verify_id_not_found");
+		/* id of "test_lookup2" will be id2 -1 as we have removed one type */
+		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup2",
+						 BTF_KIND_TYPEDEF), id2 - 1,
+			  "verify_id_lookup2");
+
+	}
+	btf__free(new_btf);
+	free(new_raw_btf);
+	unlink(btf_path);
+	btf__free(btf);
+}
+
+void test_btf_kind(void)
+{
+	if (test__start_subtest("btf_kind_encoding"))
+		test_btf_kind_encoding();
+	if (test__start_subtest("btf_kind_decoding"))
+		test_btf_kind_decoding();
+}
Signature · 1 line
2.39.3
#15 lore raw

PATCH v10 8/10 bpftool: add BTF dump "format meta" to dump header/metadata

In reply to Alan Maguire

Provide a way to dump BTF metadata info via bpftool; this consists of BTF size, header fields and layout info (if available); for example

$ bpftool btf dump file vmlinux format meta
size 6516862
magic 0xeb9f
version 1
flags 0x0
hdr_len 32
type_len 3929096
type_off 0
str_len 2587654
str_off 3929176
layout_len 80
layout_off 3929096
kind 0    UNKNOWN    info_sz 0    elem_sz 0    flags 0
kind 1    INT        info_sz 4    elem_sz 0    flags 0
kind 2    PTR        info_sz 0    elem_sz 0    flags 0
kind 3    ARRAY      info_sz 12   elem_sz 0    flags 0
kind 4    STRUCT     info_sz 0    elem_sz 12   flags 0
kind 5    UNION      info_sz 0    elem_sz 12   flags 0
kind 6    ENUM       info_sz 0    elem_sz 8    flags 0
kind 7    FWD        info_sz 0    elem_sz 0    flags 0
kind 8    TYPEDEF    info_sz 0    elem_sz 0    flags 0
kind 9    VOLATILE   info_sz 0    elem_sz 0    flags 0
kind 10   CONST      info_sz 0    elem_sz 0    flags 0
kind 11   RESTRICT   info_sz 0    elem_sz 0    flags 0
kind 12   FUNC       info_sz 0    elem_sz 0    flags 0
kind 13   FUNC_PROTO info_sz 0    elem_sz 8    flags 0
kind 14   VAR        info_sz 4    elem_sz 0    flags 0
kind 15   DATASEC    info_sz 0    elem_sz 12   flags 0
kind 16   FLOAT      info_sz 0    elem_sz 0    flags 0
kind 17   DECL_TAG   info_sz 4    elem_sz 0    flags 0
kind 18   TYPE_TAG   info_sz 0    elem_sz 0    flags 0
kind 19   ENUM64     info_sz 0    elem_sz 12   flags 0

JSON output is also supported:

$ bpftool -p btf dump file /var/tmp/vmlinux.kind_layout format meta
{
    "size": 6516862,
    "header": {
        "magic": 60319,
        "version": 1,
        "flags": 0,
        "hdr_len": 32,
        "type_len": 3929096,
        "type_off": 0,
        "str_len": 2587654,
        "str_off": 3929176,
        "layout_len": 80,
        "layout_off": 3929096
    },
    "layouts": [{
            "kind": 0,
            "name": "UNKNOWN",
            "info_sz": 0,
            "elem_sz": 0,
            "flags": 0
        },{
            "kind": 1,
            "name": "INT",
            "info_sz": 4,
            "elem_sz": 0,
            "flags": 0
        },{
            "kind": 2,
            "name": "PTR",
            "info_sz": 0,
            "elem_sz": 0,
            "flags": 0
        },{
...
---
 tools/bpf/bpftool/bash-completion/bpftool |  2 +-
 tools/bpf/bpftool/btf.c                   | 91 ++++++++++++++++++++++-
 2 files changed, 89 insertions(+), 4 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +1 -1
index a28f0cc522e4..d49d6a70cb74 100644
@@ -950,7 +950,7 @@ _bpftool()
                             return 0
                             ;;
                         format)
-                            COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
+                            COMPREPLY=( $( compgen -W "c raw meta" -- "$cur" ) )
                             ;;
                         root_id)
                             return 0;
tools/bpf/bpftool/btf.c +88 -3

124 lines not on this page · raw message

Signature · 1 line
2.39.3
#16 lore raw

Re: [PATCH v10 bpf-next 08/10] bpftool: add BTF dump "format meta" to dump header/metadata

Quoted 118 lines · Alan Maguire

Provide a way to dump BTF metadata info via bpftool; this consists of BTF size, header fields and layout info (if available); for example

$ bpftool btf dump file vmlinux format meta
size 6516862
magic 0xeb9f
version 1
flags 0x0
hdr_len 32
type_len 3929096
type_off 0
str_len 2587654
str_off 3929176
layout_len 80
layout_off 3929096
kind 0    UNKNOWN    info_sz 0    elem_sz 0    flags 0
kind 1    INT        info_sz 4    elem_sz 0    flags 0
kind 2    PTR        info_sz 0    elem_sz 0    flags 0
kind 3    ARRAY      info_sz 12   elem_sz 0    flags 0
kind 4    STRUCT     info_sz 0    elem_sz 12   flags 0
kind 5    UNION      info_sz 0    elem_sz 12   flags 0
kind 6    ENUM       info_sz 0    elem_sz 8    flags 0
kind 7    FWD        info_sz 0    elem_sz 0    flags 0
kind 8    TYPEDEF    info_sz 0    elem_sz 0    flags 0
kind 9    VOLATILE   info_sz 0    elem_sz 0    flags 0
kind 10   CONST      info_sz 0    elem_sz 0    flags 0
kind 11   RESTRICT   info_sz 0    elem_sz 0    flags 0
kind 12   FUNC       info_sz 0    elem_sz 0    flags 0
kind 13   FUNC_PROTO info_sz 0    elem_sz 8    flags 0
kind 14   VAR        info_sz 4    elem_sz 0    flags 0
kind 15   DATASEC    info_sz 0    elem_sz 12   flags 0
kind 16   FLOAT      info_sz 0    elem_sz 0    flags 0
kind 17   DECL_TAG   info_sz 4    elem_sz 0    flags 0
kind 18   TYPE_TAG   info_sz 0    elem_sz 0    flags 0
kind 19   ENUM64     info_sz 0    elem_sz 12   flags 0

JSON output is also supported:

$ bpftool -p btf dump file /var/tmp/vmlinux.kind_layout format meta
{
    "size": 6516862,
    "header": {
        "magic": 60319,
        "version": 1,
        "flags": 0,
        "hdr_len": 32,
        "type_len": 3929096,
        "type_off": 0,
        "str_len": 2587654,
        "str_off": 3929176,
        "layout_len": 80,
        "layout_off": 3929096
    },
    "layouts": [{
            "kind": 0,
            "name": "UNKNOWN",
            "info_sz": 0,
            "elem_sz": 0,
            "flags": 0
        },{
            "kind": 1,
            "name": "INT",
            "info_sz": 4,
            "elem_sz": 0,
            "flags": 0
        },{
            "kind": 2,
            "name": "PTR",
            "info_sz": 0,
            "elem_sz": 0,
            "flags": 0
        },{
...
---
 tools/bpf/bpftool/bash-completion/bpftool |  2 +-
 tools/bpf/bpftool/btf.c                   | 91 ++++++++++++++++++++++-
 2 files changed, 89 insertions(+), 4 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +1 -1
index a28f0cc522e4..d49d6a70cb74 100644
@@ -950,7 +950,7 @@ _bpftool()
                             return 0
                             ;;
                         format)
-                            COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
+                            COMPREPLY=( $( compgen -W "c raw meta" -- "$cur" ) )
                             ;;
                         root_id)
                             return 0;
tools/bpf/bpftool/btf.c +15 -0
index 946612029dee..08dc6a86bc85 100644
@@ -835,6 +835,87 @@ static int dump_btf_c(const struct btf *btf,
        return err;
 }
 
+static int dump_btf_meta(const struct btf *btf)
+{
+       __u32 layout_off = 0, layout_len = 0;
+       __u32 data_sz, i, nr_kinds = 0;
+       const struct btf_header *hdr;
+       const struct btf_layout *l;
+       const void *data;
+
+       data = btf__raw_data(btf, &data_sz);
+       if (!data)
+               return -ENOMEM;
+       hdr = data;
+       if (json_output) {
+               jsonw_start_object(json_wtr);           /* metadata object */
+               jsonw_uint_field(json_wtr, "size", data_sz);

tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 862

Quoted 10 lines
tools/bpf/bpftool/btf.c +10 -0
+               jsonw_name(json_wtr, "header");
+               jsonw_start_object(json_wtr);           /* header object */
+               jsonw_uint_field(json_wtr, "magic", hdr->magic);
+               jsonw_uint_field(json_wtr, "version", hdr->version);
+               jsonw_uint_field(json_wtr, "flags", hdr->flags);
+               jsonw_uint_field(json_wtr, "hdr_len", hdr->hdr_len);
+               jsonw_uint_field(json_wtr, "type_len", hdr->type_len);
+               jsonw_uint_field(json_wtr, "type_off", hdr->type_off);
+               jsonw_uint_field(json_wtr, "str_len", hdr->str_len);
+               jsonw_uint_field(json_wtr, "str_off", hdr->str_off);

I don't think this part is useful.
People can hex dump it if they really want to.

Quoted 31 lines
tools/bpf/bpftool/btf.c +31 -0
+       } else {
+               printf("size %-10u\n", data_sz);
+               printf("magic 0x%-10x\nversion %-10d\nflags 0x%-10x\nhdr_len %-10u\n",
+                      hdr->magic, hdr->version, hdr->flags, hdr->hdr_len);
+               printf("type_len %-10u\ntype_off %-10u\n", hdr->type_len, hdr->type_off);
+               printf("str_len %-10u\nstr_off %-10u\n", hdr->str_len, hdr->str_off);
+       }
+
+       if (data_sz >= hdr->hdr_len && hdr->hdr_len >= sizeof(struct btf_header)) {
+               layout_off = hdr->layout_off;
+               layout_len = hdr->layout_len;
+               if (layout_len > 0 &&
+                   (long long)layout_off + layout_len + hdr->hdr_len <= data_sz) {
+                       l = (void *)hdr + hdr->hdr_len + layout_off;
+                       nr_kinds = layout_len / sizeof(*l);
+               }
+       } else {
+               if (!json_output)
+                       return 0;
+       }
+
+       /* unconditionally display (possibly empty) layout info for json only */
+       if (json_output) {
+               jsonw_uint_field(json_wtr, "layout_len", layout_len);
+               jsonw_uint_field(json_wtr, "layout_off", layout_off);
+               jsonw_end_object(json_wtr);             /* end header object */
+
+               jsonw_name(json_wtr, "layouts");
+               jsonw_start_array(json_wtr);
+               for (i = 0; i < nr_kinds; i++) {
+                       jsonw_start_object(json_wtr);

tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 903

Quoted 10 lines
tools/bpf/bpftool/btf.c +10 -0
+                       jsonw_uint_field(json_wtr, "kind", i);
+                       if (i < NR_BTF_KINDS)
+                               jsonw_string_field(json_wtr, "name", btf_kind_str[i]);
+                       else
+                               jsonw_null_field(json_wtr, "name");
+                       jsonw_uint_field(json_wtr, "info_sz", l[i].info_sz);
+                       jsonw_uint_field(json_wtr, "elem_sz", l[i].elem_sz);
+                       jsonw_uint_field(json_wtr, "flags", l[i].flags);
+                       jsonw_end_object(json_wtr);
+               }

This part, I guess, is ok-ish.
I wouldn't do it either though. Feels like a debug feature.

#17 lore raw

Re: [PATCH v10 bpf-next 08/10] bpftool: add BTF dump "format meta" to dump header/metadata

Quoted 169 lines · Alan Maguire
Quoted 118 lines

Provide a way to dump BTF metadata info via bpftool; this consists of BTF size, header fields and layout info (if available); for example

$ bpftool btf dump file vmlinux format meta
size 6516862
magic 0xeb9f
version 1
flags 0x0
hdr_len 32
type_len 3929096
type_off 0
str_len 2587654
str_off 3929176
layout_len 80
layout_off 3929096
kind 0    UNKNOWN    info_sz 0    elem_sz 0    flags 0
kind 1    INT        info_sz 4    elem_sz 0    flags 0
kind 2    PTR        info_sz 0    elem_sz 0    flags 0
kind 3    ARRAY      info_sz 12   elem_sz 0    flags 0
kind 4    STRUCT     info_sz 0    elem_sz 12   flags 0
kind 5    UNION      info_sz 0    elem_sz 12   flags 0
kind 6    ENUM       info_sz 0    elem_sz 8    flags 0
kind 7    FWD        info_sz 0    elem_sz 0    flags 0
kind 8    TYPEDEF    info_sz 0    elem_sz 0    flags 0
kind 9    VOLATILE   info_sz 0    elem_sz 0    flags 0
kind 10   CONST      info_sz 0    elem_sz 0    flags 0
kind 11   RESTRICT   info_sz 0    elem_sz 0    flags 0
kind 12   FUNC       info_sz 0    elem_sz 0    flags 0
kind 13   FUNC_PROTO info_sz 0    elem_sz 8    flags 0
kind 14   VAR        info_sz 4    elem_sz 0    flags 0
kind 15   DATASEC    info_sz 0    elem_sz 12   flags 0
kind 16   FLOAT      info_sz 0    elem_sz 0    flags 0
kind 17   DECL_TAG   info_sz 4    elem_sz 0    flags 0
kind 18   TYPE_TAG   info_sz 0    elem_sz 0    flags 0
kind 19   ENUM64     info_sz 0    elem_sz 12   flags 0

JSON output is also supported:

$ bpftool -p btf dump file /var/tmp/vmlinux.kind_layout format meta
{
    "size": 6516862,
    "header": {
        "magic": 60319,
        "version": 1,
        "flags": 0,
        "hdr_len": 32,
        "type_len": 3929096,
        "type_off": 0,
        "str_len": 2587654,
        "str_off": 3929176,
        "layout_len": 80,
        "layout_off": 3929096
    },
    "layouts": [{
            "kind": 0,
            "name": "UNKNOWN",
            "info_sz": 0,
            "elem_sz": 0,
            "flags": 0
        },{
            "kind": 1,
            "name": "INT",
            "info_sz": 4,
            "elem_sz": 0,
            "flags": 0
        },{
            "kind": 2,
            "name": "PTR",
            "info_sz": 0,
            "elem_sz": 0,
            "flags": 0
        },{
...
---
 tools/bpf/bpftool/bash-completion/bpftool |  2 +-
 tools/bpf/bpftool/btf.c                   | 91 ++++++++++++++++++++++-
 2 files changed, 89 insertions(+), 4 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +1 -1
index a28f0cc522e4..d49d6a70cb74 100644
@@ -950,7 +950,7 @@ _bpftool()
                             return 0
                             ;;
                         format)
-                            COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
+                            COMPREPLY=( $( compgen -W "c raw meta" -- "$cur" ) )
                             ;;
                         root_id)
                             return 0;
tools/bpf/bpftool/btf.c +15 -0
index 946612029dee..08dc6a86bc85 100644
@@ -835,6 +835,87 @@ static int dump_btf_c(const struct btf *btf,
        return err;
 }
 
+static int dump_btf_meta(const struct btf *btf)
+{
+       __u32 layout_off = 0, layout_len = 0;
+       __u32 data_sz, i, nr_kinds = 0;
+       const struct btf_header *hdr;
+       const struct btf_layout *l;
+       const void *data;
+
+       data = btf__raw_data(btf, &data_sz);
+       if (!data)
+               return -ENOMEM;
+       hdr = data;
+       if (json_output) {
+               jsonw_start_object(json_wtr);           /* metadata object */
+               jsonw_uint_field(json_wtr, "size", data_sz);

tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 862

Quoted 10 lines
tools/bpf/bpftool/btf.c +10 -0
+               jsonw_name(json_wtr, "header");
+               jsonw_start_object(json_wtr);           /* header object */
+               jsonw_uint_field(json_wtr, "magic", hdr->magic);
+               jsonw_uint_field(json_wtr, "version", hdr->version);
+               jsonw_uint_field(json_wtr, "flags", hdr->flags);
+               jsonw_uint_field(json_wtr, "hdr_len", hdr->hdr_len);
+               jsonw_uint_field(json_wtr, "type_len", hdr->type_len);
+               jsonw_uint_field(json_wtr, "type_off", hdr->type_off);
+               jsonw_uint_field(json_wtr, "str_len", hdr->str_len);
+               jsonw_uint_field(json_wtr, "str_off", hdr->str_off);

I don't think this part is useful.
People can hex dump it if they really want to.

Quoted 36 lines
tools/bpf/bpftool/btf.c +36 -0
+       } else {
+               printf("size %-10u\n", data_sz);
+               printf("magic 0x%-10x\nversion %-10d\nflags 0x%-10x\nhdr_len %-10u\n",
+                      hdr->magic, hdr->version, hdr->flags, hdr->hdr_len);
+               printf("type_len %-10u\ntype_off %-10u\n", hdr->type_len, hdr->type_off);
+               printf("str_len %-10u\nstr_off %-10u\n", hdr->str_len, hdr->str_off);
+       }
+
+       if (data_sz >= hdr->hdr_len && hdr->hdr_len >= sizeof(struct btf_header)) {
+               layout_off = hdr->layout_off;
+               layout_len = hdr->layout_len;
+               if (layout_len > 0 &&
+                   (long long)layout_off + layout_len + hdr->hdr_len <= data_sz) {
+                       l = (void *)hdr + hdr->hdr_len + layout_off;
+                       nr_kinds = layout_len / sizeof(*l);
+               }
+       } else {
+               if (!json_output)
+                       return 0;
+       }
+
+       /* unconditionally display (possibly empty) layout info for json only */
+       if (json_output) {
+               jsonw_uint_field(json_wtr, "layout_len", layout_len);
+               jsonw_uint_field(json_wtr, "layout_off", layout_off);
+               jsonw_end_object(json_wtr);             /* end header object */
+
+               jsonw_name(json_wtr, "layouts");
+               jsonw_start_array(json_wtr);
+               for (i = 0; i < nr_kinds; i++) {
+                       jsonw_start_object(json_wtr);
+                       jsonw_uint_field(json_wtr, "kind", i);
+                       if (i < NR_BTF_KINDS)
+                               jsonw_string_field(json_wtr, "name", btf_kind_str[i]);
+                       else
+                               jsonw_null_field(json_wtr, "name");
Quoted 8 lines

tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 903

Quoted 5 lines
tools/bpf/bpftool/btf.c +5 -0
+                       jsonw_uint_field(json_wtr, "info_sz", l[i].info_sz);
+                       jsonw_uint_field(json_wtr, "elem_sz", l[i].elem_sz);
+                       jsonw_uint_field(json_wtr, "flags", l[i].flags);
+                       jsonw_end_object(json_wtr);
+               }

This part, I guess, is ok-ish.
I wouldn't do it either though. Feels like a debug feature.

Sure, we can omit the bpftool changes for v11 and add them later if there's a need.

#18 lore raw

PATCH v10 9/10 bpftool: Update doc to describe bpftool btf dump .. format metadata

In reply to Alan Maguire

...and provide an example of output.

---
 .../bpf/bpftool/Documentation/bpftool-btf.rst | 28 +++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-btf.rst +26 -2
index d47dddc2b4ee..1004f9bf0a43 100644
@@ -28,7 +28,7 @@ BTF COMMANDS
 | **bpftool** **btf help**
 |
 | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }
-| *FORMAT* := { **raw** | **c** [**unsorted**] }
+| *FORMAT* := { **raw** | **c** [**unsorted**] | **meta** }
 | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
 | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
 
@@ -65,7 +65,8 @@ bpftool btf dump *BTF_SRC* [format *FORMAT*] [root_id *ROOT_ID*]
     **format** option can be used to override default (raw) output format. Raw
     (**raw**) or C-syntax (**c**) output formats are supported. With C-style
     formatting, the output is sorted by default. Use the **unsorted** option
-    to avoid sorting the output.
+    to avoid sorting the output.  BTF metadata can be displayed with the
+    **meta** option.
 
     **root_id** option can be used to filter a dump to a single type and all
     its dependent types. It cannot be used with any other types of filtering
@@ -267,3 +268,26 @@ All the standard ways to specify map or program are supported:
   [104859] FUNC 'smbalert_work' type_id=9695 linkage=static
   [104860] FUNC 'smbus_alert' type_id=71367 linkage=static
   [104861] FUNC 'smbus_do_alert' type_id=84827 linkage=static
+
+Display BTF metadata from file vmlinux
+
+**# bpftool btf dump file vmlinux format meta**
+
+::
+
+  size 5161076
+  magic 0xeb9f
+  version 1
+  flags 0x1
+  hdr_len 40
+  type_len 3036368
+  type_off 0
+  str_len 2124588
+  str_off 3036368
+  layout_len 80
+  layout_off 5160956
+  kind 0    UNKNOWN    info_sz 0    elem_sz 0   flags 0
+  kind 1    INT        info_sz 0    elem_sz 0   flags 0
+  kind 2    PTR        info_sz 0    elem_sz 0   flags 0
+  kind 3    ARRAY      info_sz 0    elem_sz 0   flags 0
+  kind 4    STRUCT     info_sz 0    elem_sz 0   flags 0
Signature · 1 line
2.39.3
#19 lore raw

Re: [PATCH v10 bpf-next 09/10] bpftool: Update doc to describe bpftool btf dump .. format metadata

Does this example reflect actual tool output? A few values appear inconsistent with the implementation in dump_btf_meta() and the layout encoding in libbpf:
Quoted 3 lines
tools/bpf/bpftool/Documentation/bpftool-btf.rst +0 -0

[ ... ]

Quoted 15 lines
tools/bpf/bpftool/Documentation/bpftool-btf.rst +13 -0
@@ -267,3 +268,26 @@
   [104861] FUNC 'smbus_do_alert' type_id=84827 linkage=static
+
+Display BTF metadata from file vmlinux
+
+**# bpftool btf dump file vmlinux format meta**
+
+::
+
+  size 5161076
+  magic 0xeb9f
+  version 1
+  flags 0x1
+  hdr_len 40
+  type_len 3036368

tools/bpf/bpftool/Documentation/bpftool-btf.rst line 291

Quoted 10 lines
tools/bpf/bpftool/Documentation/bpftool-btf.rst +10 -0
+  type_off 0
+  str_len 2124588
+  str_off 3036368
+  layout_len 80
+  layout_off 5160956
+  kind 0    UNKNOWN    info_sz 0    elem_sz 0   flags 0
+  kind 1    INT        info_sz 0    elem_sz 0   flags 0
+  kind 2    PTR        info_sz 0    elem_sz 0   flags 0
+  kind 3    ARRAY      info_sz 0    elem_sz 0   flags 0
+  kind 4    STRUCT     info_sz 0    elem_sz 0   flags 0

Does this example reflect actual tool output? A few values appear inconsistent with the implementation in dump_btf_meta() and the layout encoding in libbpf:

The kind layout values are all zeros, but the layouts[] array in tools/lib/bpf/btf.c populates non-zero values for known kinds:

  [BTF_KIND_INT]    = { sizeof(__u32),            0, 0 },  /* info_sz=4  */
  [BTF_KIND_ARRAY]  = { sizeof(struct btf_array), 0, 0 },  /* info_sz=12 */

[BTF_KIND_STRUCT] = { 0, sizeof(struct btf_member), 0 }, /* elem_sz=12 */

The implementation commit (62f2c6445a46) also shows the correct non-zero values in its own example:

  kind 1    INT        info_sz 4    elem_sz 0    flags 0
  kind 3    ARRAY      info_sz 12   elem_sz 0    flags 0
  kind 4    STRUCT     info_sz 0    elem_sz 12   flags 0

Would it be worth regenerating this example from an actual bpftool run to make sure the values match?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22482435858

AI-authorship-score: low
AI-authorship-explanation: Straightforward RST documentation update with iterative human review history spanning 2023-2026, typical of a developer documenting their own feature.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Documentation example output does not match actual tool behavior; kind layout values are all zeros when the implementation produces non-zero values for known kinds.

#20 lore raw

PATCH v10 10/10 kbuild, bpf: Specify "layout" optional feature

In reply to Alan Maguire

The "layout" feature will add metadata about BTF kinds to the generated BTF; its absence in pahole will not trigger an error so it is safe to add unconditionally as it will simply be ignored if pahole does not support it.

---
 scripts/Makefile.btf | 2 ++
 1 file changed, 2 insertions(+)
scripts/Makefile.btf +2 -0
index 562a04b40e06..e66e13e79653 100644
@@ -18,6 +18,8 @@ pahole-flags-$(call test-ge, $(pahole-ver), 126)  = -j$(JOBS) --btf_features=enc
 
 pahole-flags-$(call test-ge, $(pahole-ver), 130) += --btf_features=attributes
 
+pahole-flags-$(call test-ge, $(pahole-ver), 131) += --btf_features=layout
+
 endif
 
 pahole-flags-$(CONFIG_PAHOLE_HAS_LANG_EXCLUDE)		+= --lang_exclude=rust
Signature · 1 line
2.39.3