* [PATCH] resolve_btfids: Refactor the sort_btf_by_name function
@ 2026-02-02 12:01 Donglin Peng
2026-02-04 21:52 ` Andrii Nakryiko
0 siblings, 1 reply; 2+ messages in thread
From: Donglin Peng @ 2026-02-02 12:01 UTC (permalink / raw)
To: ast; +Cc: andrii, bpf, linux-kernel, Donglin Peng
From: Donglin Peng <pengdonglin@xiaomi.com>
Preserve original relative order of anonymous or same-named
types to improve the consistency.
No functional changes.
Cc: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Donglin Peng <pengdonglin@xiaomi.com>
---
tools/bpf/resolve_btfids/main.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
index db8d1554bdcc..ca7fcd03efb6 100644
--- a/tools/bpf/resolve_btfids/main.c
+++ b/tools/bpf/resolve_btfids/main.c
@@ -1226,22 +1226,26 @@ static int cmp_type_names(const void *a, const void *b, void *priv)
const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
const char *na, *nb;
+ int r;
na = btf__str_by_offset(btf, ta->name_off);
nb = btf__str_by_offset(btf, tb->name_off);
- return strcmp(na, nb);
+ r = strcmp(na, nb);
+ if (r != 0)
+ return r;
+
+ /* preserve original relative order of anonymous or same-named types */
+ return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
}
static int sort_btf_by_name(struct btf *btf)
{
__u32 *permute_ids = NULL, *id_map = NULL;
int nr_types, i, err = 0;
- __u32 start_id = 0, start_offs = 1, id;
+ __u32 start_id = 0, id;
- if (btf__base_btf(btf)) {
+ if (btf__base_btf(btf))
start_id = btf__type_cnt(btf__base_btf(btf));
- start_offs = 0;
- }
nr_types = btf__type_cnt(btf) - start_id;
permute_ids = calloc(nr_types, sizeof(*permute_ids));
@@ -1259,8 +1263,8 @@ static int sort_btf_by_name(struct btf *btf)
for (i = 0, id = start_id; i < nr_types; i++, id++)
permute_ids[i] = id;
- qsort_r(permute_ids + start_offs, nr_types - start_offs,
- sizeof(*permute_ids), cmp_type_names, btf);
+ qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
+ btf);
for (i = 0; i < nr_types; i++) {
id = permute_ids[i] - start_id;
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] resolve_btfids: Refactor the sort_btf_by_name function
2026-02-02 12:01 [PATCH] resolve_btfids: Refactor the sort_btf_by_name function Donglin Peng
@ 2026-02-04 21:52 ` Andrii Nakryiko
0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2026-02-04 21:52 UTC (permalink / raw)
To: Donglin Peng; +Cc: ast, andrii, bpf, linux-kernel, Donglin Peng
On Mon, Feb 2, 2026 at 4:01 AM Donglin Peng <dolinux.peng@gmail.com> wrote:
>
> From: Donglin Peng <pengdonglin@xiaomi.com>
>
> Preserve original relative order of anonymous or same-named
> types to improve the consistency.
>
> No functional changes.
>
> Cc: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Donglin Peng <pengdonglin@xiaomi.com>
> ---
> tools/bpf/resolve_btfids/main.c | 18 +++++++++++-------
> 1 file changed, 11 insertions(+), 7 deletions(-)
>
patchbot didn't notice this, but I've applied this to bpf-next, thanks
> diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c
> index db8d1554bdcc..ca7fcd03efb6 100644
> --- a/tools/bpf/resolve_btfids/main.c
> +++ b/tools/bpf/resolve_btfids/main.c
> @@ -1226,22 +1226,26 @@ static int cmp_type_names(const void *a, const void *b, void *priv)
> const struct btf_type *ta = btf__type_by_id(btf, *(__u32 *)a);
> const struct btf_type *tb = btf__type_by_id(btf, *(__u32 *)b);
> const char *na, *nb;
> + int r;
>
> na = btf__str_by_offset(btf, ta->name_off);
> nb = btf__str_by_offset(btf, tb->name_off);
> - return strcmp(na, nb);
> + r = strcmp(na, nb);
> + if (r != 0)
> + return r;
> +
> + /* preserve original relative order of anonymous or same-named types */
> + return *(__u32 *)a < *(__u32 *)b ? -1 : 1;
> }
>
> static int sort_btf_by_name(struct btf *btf)
> {
> __u32 *permute_ids = NULL, *id_map = NULL;
> int nr_types, i, err = 0;
> - __u32 start_id = 0, start_offs = 1, id;
> + __u32 start_id = 0, id;
>
> - if (btf__base_btf(btf)) {
> + if (btf__base_btf(btf))
> start_id = btf__type_cnt(btf__base_btf(btf));
> - start_offs = 0;
> - }
> nr_types = btf__type_cnt(btf) - start_id;
>
> permute_ids = calloc(nr_types, sizeof(*permute_ids));
> @@ -1259,8 +1263,8 @@ static int sort_btf_by_name(struct btf *btf)
> for (i = 0, id = start_id; i < nr_types; i++, id++)
> permute_ids[i] = id;
>
> - qsort_r(permute_ids + start_offs, nr_types - start_offs,
> - sizeof(*permute_ids), cmp_type_names, btf);
> + qsort_r(permute_ids, nr_types, sizeof(*permute_ids), cmp_type_names,
> + btf);
>
> for (i = 0; i < nr_types; i++) {
> id = permute_ids[i] - start_id;
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-04 21:52 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-02 12:01 [PATCH] resolve_btfids: Refactor the sort_btf_by_name function Donglin Peng
2026-02-04 21:52 ` Andrii Nakryiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®