From nobody Mon Feb 9 16:52:45 2026 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A8D8285072 for ; Thu, 8 Jan 2026 03:17:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767842224; cv=none; b=C0G7/32fjkAYVx/bBtFA3XLFILEq6bNDlGR3kr7124OUbuUj/i784qTx58MMdviv3pBwsIdyWXEBJMF49sFP5wsQ7FlZ70OtlveEvaoH1d+vmVGASuWPoyH+oIhNrbU/2c3ytKJD4SIrFy94KY6wIW8tkYVsMZj7XOQo1FF4/XM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767842224; c=relaxed/simple; bh=V2vGk2Zy5fJHvJb6bZZNfPor1jIBJ/nlZMkVKHTwBMQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AtdFNcrMtfctVNiQUg0dMF2xUr+JkozHex5ffkjdehIqLB3gdFO43G1h0Usn4QR8XPyR71LgG0YNAg/aTUuqQfJoNM56/ct1swALtjnIkqhsWTOSgAel38xTCgyPZS/UdpLb9r/TPSXWWT+voYAhc08m8vg7IAbzlBfSoDQGQ2s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hBJ/HBOO; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hBJ/HBOO" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7aab061e7cbso3007133b3a.1 for ; Wed, 07 Jan 2026 19:17:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767842221; x=1768447021; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Bty2Mx4T9JTfidtdE5tI1SEQacGBSMSmT3cGtlsXrrQ=; b=hBJ/HBOOqNmmDb60WJtTA3CBMUoSqXFl8m0u7RHYAUSyczcDRXN8mMmdu1PeymdAOT UQ09oYEwBwBXED8N6TIgDPyFSTSa72rDE7YV9aYFzibI4tFlhJJpkPfcpZkinzscZuyc LT+0zRmewT7mqdpnogKV9ejwAQ6nBOqyw+P0a3Qsb17BRfyGBsxgMLGqonz3zcWtWNVz kUNsZQTs6LtL5hScszsmkg1W3d505Q3lgJNvEMPXiRxlFKzp0gt6vOE8OmMk2j1sP/n2 dGosZdFTHvslwOCy1Lf79yHUko39ioX/PGoig/bndqrdMY4jZ7eRgnInGDNWn2AJjuuV 8y8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767842221; x=1768447021; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Bty2Mx4T9JTfidtdE5tI1SEQacGBSMSmT3cGtlsXrrQ=; b=QhhXF6LDKGNZk3Bslh8WeCWk+JU4egrkeSdnqaI44LdPhJtGo6EZoMD+TW84xsKKLg 07caS4Kz5peBDwQCyz73/cPnZRa/cYpI6sXAYYdDPZWbrTMp/H+wPOk6ITnZw50Tkivq RJYiRhizZQCdfQw8gN3hJhVmv6TRC7wxspbAIUjNWCPYt/iYKafRDYjQdeRh9SnRBbky 6dEbac/lPI7V3MVoiehi/a3una1t7808MYn72A/rmXjWq2ZX2yIXEhnDYF2JpwB7/ews r30kEXiFjofnnT0SGl20u/eTgBGp2k3YngP47IA4Jt06hk1VKYX0ZHpySrj+luAdxnw2 +JEg== X-Forwarded-Encrypted: i=1; AJvYcCUYjbB/ANR4loVAc6TJN08jL9b4lOF4s46JfBllIcrYXlQzFV4x6sISCH5fXdd3Nf5RZJAgfw/kW+dzwUY=@vger.kernel.org X-Gm-Message-State: AOJu0YwnOGgq9wX446xrr2IAs2Myo2xvFXTxYFWUc/34uEInqB9w1TEB NESQpsSZdZgQm2DJFAUcC/VdTx8i+ii2FRALacqSTK9ScSrPQIziVlbf X-Gm-Gg: AY/fxX7mdN/y2lRTgQCbelznYhWYh9b4hz6IUk07Z5EjDOV5Co1CC+OxPh6jdjg1xe5 WHaQ3cmSrOEKc7CNJb9WpV+8sDX3lWAZ0QBDFddZJGDQtxRBEoh/sZdx8/C5TXNDlzAhTO3Javm OL5AebtKp1Y+kd7StY12G4gF1ztIOoOnbZVMUCMlWzL+InXTdDqmx+QYmaKtQ5hYLwwImuXwIxl Rmu5HscasHUVaU3gn/2/8Vf9q5mfqiveLrhz0HWe9Tl/eJ3HcEnXHZPTUEaT2WREu6HuBIrDA34 6UtA3P5ljLSJhoKtMdDwNcoBJFzTI6ounYLGylrV26T1F+SHTrkt15LJEDv0kJnm5Y0WrH688RM 37/40zGXt4ua96tSWN1pDqupf4VbHQyXlP9g7ezGjXNdOY3hcYM6svlpNNjiSVmxdLwQKccDNlX A0fVSFqYRAg4GfGzHl8oJQh4pg68E= X-Google-Smtp-Source: AGHT+IHMEKVP3mhXgUpos/Q5KytBuzrqYEqaOFBA7IWN01A7RqP3IhiAjx4qP/6grxzJEKNhfrReQQ== X-Received: by 2002:a05:6a00:3288:b0:7e8:4433:8fac with SMTP id d2e1a72fcca58-81b7fdc379bmr3840681b3a.52.1767842221498; Wed, 07 Jan 2026 19:17:01 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-819c5de655bsm6134860b3a.60.2026.01.07.19.16.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jan 2026 19:17:01 -0800 (PST) From: Donglin Peng To: ast@kernel.org, andrii.nakryiko@gmail.com, eddyz87@gmail.com Cc: zhangxiaoqin@xiaomi.com, ihor.solodrai@linux.dev, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Donglin Peng , Alan Maguire Subject: [PATCH bpf-next v11 03/11] tools/resolve_btfids: Support BTF sorting feature Date: Thu, 8 Jan 2026 11:16:37 +0800 Message-Id: <20260108031645.1350069-4-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260108031645.1350069-1-dolinux.peng@gmail.com> References: <20260108031645.1350069-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Donglin Peng This introduces a new BTF sorting phase that specifically sorts BTF types by name in ascending order, so that the binary search can be used to look up types. Cc: Eduard Zingerman Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Alan Maguire Cc: Ihor Solodrai Cc: Xiaoqin Zhang Signed-off-by: Donglin Peng Acked-by: Eduard Zingerman --- tools/bpf/resolve_btfids/main.c | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index df39982f51df..343d08050116 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -850,6 +850,67 @@ static int dump_raw_btf(struct btf *btf, const char *o= ut_path) return 0; } =20 +/* + * Sort types by name in ascending order resulting in all + * anonymous types being placed before named types. + */ +static int cmp_type_names(const void *a, const void *b, void *priv) +{ + struct btf *btf =3D (struct btf *)priv; + const struct btf_type *ta =3D btf__type_by_id(btf, *(__u32 *)a); + const struct btf_type *tb =3D btf__type_by_id(btf, *(__u32 *)b); + const char *na, *nb; + + na =3D btf__str_by_offset(btf, ta->name_off); + nb =3D btf__str_by_offset(btf, tb->name_off); + return strcmp(na, nb); +} + +static int sort_btf_by_name(struct btf *btf) +{ + __u32 *permute_ids =3D NULL, *id_map =3D NULL; + int nr_types, i, err =3D 0; + __u32 start_id =3D 0, start_offs =3D 1, id; + + if (btf__base_btf(btf)) { + start_id =3D btf__type_cnt(btf__base_btf(btf)); + start_offs =3D 0; + } + nr_types =3D btf__type_cnt(btf) - start_id; + + permute_ids =3D calloc(nr_types, sizeof(*permute_ids)); + if (!permute_ids) { + err =3D -ENOMEM; + goto out; + } + + id_map =3D calloc(nr_types, sizeof(*id_map)); + if (!id_map) { + err =3D -ENOMEM; + goto out; + } + + for (i =3D 0, id =3D start_id; i < nr_types; i++, id++) + permute_ids[i] =3D id; + + qsort_r(permute_ids + start_offs, nr_types - start_offs, + sizeof(*permute_ids), cmp_type_names, btf); + + for (i =3D 0; i < nr_types; i++) { + id =3D permute_ids[i] - start_id; + id_map[id] =3D i + start_id; + } + + err =3D btf__permute(btf, id_map, nr_types, NULL); + if (err) + pr_err("FAILED: btf permute: %s\n", strerror(-err)); + +out: + free(permute_ids); + free(id_map); + return err; +} + static inline int make_out_path(char *buf, u32 buf_sz, const char *in_path= , const char *suffix) { int len =3D snprintf(buf, buf_sz, "%s%s", in_path, suffix); @@ -1025,6 +1086,9 @@ int main(int argc, const char **argv) if (load_btf(&obj)) goto out; =20 + if (sort_btf_by_name(obj.btf)) + goto out; + if (elf_collect(&obj)) goto out; =20 --=20 2.34.1