Re: [RFC PATCH v4 2/7] libbpf: Add BTF permutation support for type reordering
From: Eduard Zingerman
Date: Tue Nov 04 2025 - 18:45:06 EST
On Tue, 2025-11-04 at 21:40 +0800, Donglin Peng wrote:
> From: pengdonglin <pengdonglin@xxxxxxxxxx>
>
> Introduce btf__permute() API to allow in-place rearrangement of BTF types.
> This function reorganizes BTF type order according to a provided array of
> type IDs, updating all type references to maintain consistency.
>
> The permutation process involves:
> 1. Shuffling types into new order based on the provided ID mapping
> 2. Remapping all type ID references to point to new locations
> 3. Handling BTF extension data if provided via options
>
> This is particularly useful for optimizing type locality after BTF
> deduplication or for meeting specific layout requirements in specialized
> use cases.
>
> Cc: Eduard Zingerman <eddyz87@xxxxxxxxx>
> Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
> Cc: Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx>
> Cc: Alan Maguire <alan.maguire@xxxxxxxxxx>
> Cc: Song Liu <song@xxxxxxxxxx>
> Signed-off-by: pengdonglin <pengdonglin@xxxxxxxxxx>
> Signed-off-by: Donglin Peng <dolinux.peng@xxxxxxxxx>
Acked-by: Eduard Zingerman <eddyz87@xxxxxxxxx>
[...]
> --- a/tools/lib/bpf/btf.h
> +++ b/tools/lib/bpf/btf.h
> @@ -273,6 +273,40 @@ LIBBPF_API int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts);
> */
> LIBBPF_API int btf__relocate(struct btf *btf, const struct btf *base_btf);
>
> +struct btf_permute_opts {
> + size_t sz;
> + /* optional .BTF.ext info along the main BTF info */
> + struct btf_ext *btf_ext;
> + size_t :0;
> +};
> +#define btf_permute_opts__last_field btf_ext
> +
> +/**
> + * @brief **btf__permute()** rearranges BTF types in-place according to specified mapping
> + * @param btf BTF object to permute
> + * @param ids Array defining new type order. Must contain exactly btf->nr_types elements,
> + * each being a valid type ID in range [btf->start_id, btf->start_id + btf->nr_types - 1]
> + * @param opts Optional parameters, including BTF extension data for reference updates
> + * @return 0 on success, negative error code on failure
> + *
> + * **btf__permute()** performs an in-place permutation of BTF types, rearranging them
> + * according to the order specified in @p ids array. After reordering, all type references
> + * within the BTF data and optional BTF extension are updated to maintain consistency.
> + *
> + * The permutation process consists of two phases:
> + * 1. Type shuffling: Physical reordering of type data in memory
> + * 2. Reference remapping: Updating all type ID references to new locations
Nit: Please drop this paragraph: it is an implementation detail, not
user-facing behavior, and it is obvious from the function code.
> + *
> + * This is particularly useful for optimizing type locality after BTF deduplication
> + * or for meeting specific layout requirements in specialized use cases.
Nit: Please drop this paragraph as well.
> + *
> + * On error, negative error code is returned and errno is set appropriately.
> + * Common error codes include:
> + * - -EINVAL: Invalid parameters or invalid ID mapping (e.g., duplicate IDs, out-of-range IDs)
> + * - -ENOMEM: Memory allocation failure during permutation process
> + */
> +LIBBPF_API int btf__permute(struct btf *btf, __u32 *ids, const struct btf_permute_opts *opts);
> +
> struct btf_dump;
>
> struct btf_dump_opts {
[...]