From patchwork Tue Oct 25 22:27:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019950 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A1BBECDFA1 for ; Tue, 25 Oct 2022 22:28:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231602AbiJYW2p (ORCPT ); Tue, 25 Oct 2022 18:28:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230327AbiJYW2m (ORCPT ); Tue, 25 Oct 2022 18:28:42 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 017D36CD1C for ; Tue, 25 Oct 2022 15:28:41 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id sc25so16461758ejc.12 for ; Tue, 25 Oct 2022 15:28:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=d4uISy0p6MDMzgs1vbawW1AEByrQTOVsdIc9n7bmQ58=; b=Mc/WJoaSNQGhqSkm5lsv0Af0hBWppy26GXv2IfXVk99UwXM8mIA4nBmSiiNEsclaHo zBXtvqwbEY8yTdZIjLxF0/lpEx+Fb831uwg7JwpE5LCrBbyghurPWx7aS8+J1qCweQ4y HFmO5aZIg8z4434Y3DgesK911L/xdjC1bqVtKXNJVcrCxbRKXUbOCcfyVlnFThhQqxZh F210kNt1STmorJAA+k+IaPl0bCBZC0iuOKBzno98yPQ+mvWx7nXHN64e20RONtgUllTr /vsQkhcWJBqh3c8zEa+gXsMjFfgEM3lnmnQKszNFm4cOkxmAT2B4DGymMRhsJJXrbcdh 23WQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d4uISy0p6MDMzgs1vbawW1AEByrQTOVsdIc9n7bmQ58=; b=5Uw1UTinAWq7dBWZK4fJ0BCPJxX9bCkWJXcfi4QvEGaSfPg+7W4PV1eP0Ta374supV 01K0iKqWmwFWp+Hqr8CWN1EWnh/seXFXY6mzfvW24s+TruZQw0TeWFt8h4yMCkAZPYgf aNDvfARWy639MOuTNTs2CrCBiP7Vh9AxUaAM0JTNYcPyfGclinjqouvfYyLeYQp+32Jl diF0aY/YDoACzZAZ33G+tQUwl7jMNgrjSviHH1SPa/Wh+BbHOYB39KMirAujnrgAJM/d 3jM8/f1iVpEibv2xCZiwnwKbCh5zPxcsu5boLiWj8sbPUWD8ult9jyGJqJ/UwhACRlWQ zBkQ== X-Gm-Message-State: ACrzQf15e/AWrtZPjpfE6h4KogNhrn62XTBTTp3GNm53TtxVzVZBwIoI eJMdpYa40lje4kEmDWrbndiIoUc6EGovgGHY X-Google-Smtp-Source: AMsMyM5s4eZXorDwLtY9lXz7QLMKI+N61ylAfCfqWAUQ92laAey/s/48wc4K+sEGrGdSr5iG/PUe4Q== X-Received: by 2002:a17:906:9c82:b0:781:5752:4f2b with SMTP id fj2-20020a1709069c8200b0078157524f2bmr33400243ejc.561.1666736919301; Tue, 25 Oct 2022 15:28:39 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:38 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 01/12] libbpf: Deduplicate unambigous standalone forward declarations Date: Wed, 26 Oct 2022 01:27:50 +0300 Message-Id: <20221025222802.2295103-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Deduplicate forward declarations that don't take part in type graphs comparisons if declaration name is unambiguous. Example: CU #1: struct foo; // standalone forward declaration struct foo *some_global; CU #2: struct foo { int x; }; struct foo *another_global; The `struct foo` from CU #1 is not a part of any definition that is compared against another definition while `btf_dedup_struct_types` processes structural types. The the BTF after `btf_dedup_struct_types` the BTF looks as follows: [1] STRUCT 'foo' size=4 vlen=1 ... [2] INT 'int' size=4 ... [3] PTR '(anon)' type_id=1 [4] FWD 'foo' fwd_kind=struct [5] PTR '(anon)' type_id=4 This commit adds a new pass `btf_dedup_standalone_fwds`, that maps such forward declarations to structs or unions with identical name in case if the name is not ambiguous. The pass is positioned before `btf_dedup_ref_types` so that types [3] and [5] could be merged as a same type after [1] and [4] are merged. The final result for the example above looks as follows: [1] STRUCT 'foo' size=4 vlen=1 'x' type_id=2 bits_offset=0 [2] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED [3] PTR '(anon)' type_id=1 For defconfig kernel with BTF enabled this removes 63 forward declarations. Examples of removed declarations: `pt_regs`, `in6_addr`. The running time of `btf__dedup` function is increased by about 3%. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/btf.c | 178 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 174 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index d88647da2c7f..c34c68d8e8a0 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -2881,6 +2881,7 @@ static int btf_dedup_strings(struct btf_dedup *d); static int btf_dedup_prim_types(struct btf_dedup *d); static int btf_dedup_struct_types(struct btf_dedup *d); static int btf_dedup_ref_types(struct btf_dedup *d); +static int btf_dedup_standalone_fwds(struct btf_dedup *d); static int btf_dedup_compact_types(struct btf_dedup *d); static int btf_dedup_remap_types(struct btf_dedup *d); @@ -2988,15 +2989,16 @@ static int btf_dedup_remap_types(struct btf_dedup *d); * Algorithm summary * ================= * - * Algorithm completes its work in 6 separate passes: + * Algorithm completes its work in 7 separate passes: * * 1. Strings deduplication. * 2. Primitive types deduplication (int, enum, fwd). * 3. Struct/union types deduplication. - * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func + * 4. Standalone fwd declarations deduplication. + * 5. Reference types deduplication (pointers, typedefs, arrays, funcs, func * protos, and const/volatile/restrict modifiers). - * 5. Types compaction. - * 6. Types remapping. + * 6. Types compaction. + * 7. Types remapping. * * Algorithm determines canonical type descriptor, which is a single * representative type for each truly unique type. This canonical type is the @@ -3060,6 +3062,11 @@ int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts) pr_debug("btf_dedup_struct_types failed:%d\n", err); goto done; } + err = btf_dedup_standalone_fwds(d); + if (err < 0) { + pr_debug("btf_dedup_standalone_fwd failed:%d\n", err); + goto done; + } err = btf_dedup_ref_types(d); if (err < 0) { pr_debug("btf_dedup_ref_types failed:%d\n", err); @@ -4525,6 +4532,169 @@ static int btf_dedup_ref_types(struct btf_dedup *d) return 0; } +/* + * `name_off_map` maps name offsets to type ids (essentially __u32 -> __u32). + * + * The __u32 key/value representations are cast to `void *` before passing + * to `hashmap__*` functions. These pseudo-pointers are never dereferenced. + * + */ +static struct hashmap *name_off_map__new(void) +{ + return hashmap__new(btf_dedup_identity_hash_fn, + btf_dedup_equal_fn, + NULL); +} + +static int name_off_map__find(struct hashmap *map, __u32 name_off, __u32 *type_id) +{ + /* This has to be sizeof(void *) in order to be passed to hashmap__find */ + void *tmp; + int found = hashmap__find(map, (void *)(ptrdiff_t)name_off, &tmp); + /* + * __u64 cast is necessary to avoid pointer to integer conversion size warning. + * It is fine to get rid of this warning as `void *` is used as an integer value. + */ + if (found) + *type_id = (__u64)tmp; + return found; +} + +static int name_off_map__set(struct hashmap *map, __u32 name_off, __u32 type_id) +{ + return hashmap__set(map, (void *)(size_t)name_off, (void *)(size_t)type_id, + NULL, NULL); +} + +/* + * Collect a `name_off_map` that maps type names to type ids for all + * canonical structs and unions. If the same name is shared by several + * canonical types use a special value 0 to indicate this fact. + */ +static int btf_dedup_fill_unique_names_map(struct btf_dedup *d, struct hashmap *names_map) +{ + int i, err = 0; + __u32 type_id, collision_id; + __u16 kind; + struct btf_type *t; + + for (i = 0; i < d->btf->nr_types; i++) { + type_id = d->btf->start_id + i; + t = btf_type_by_id(d->btf, type_id); + kind = btf_kind(t); + + if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION) + continue; + + /* Skip non-canonical types */ + if (type_id != d->map[type_id]) + continue; + + err = 0; + if (name_off_map__find(names_map, t->name_off, &collision_id)) { + /* Mark non-unique names with 0 */ + if (collision_id != 0 && collision_id != type_id) + err = name_off_map__set(names_map, t->name_off, 0); + } else { + err = name_off_map__set(names_map, t->name_off, type_id); + } + + if (err < 0) + return err; + } + + return 0; +} + +static int btf_dedup_standalone_fwd(struct btf_dedup *d, + struct hashmap *names_map, + __u32 type_id) +{ + struct btf_type *t = btf_type_by_id(d->btf, type_id); + __u16 kind = btf_kind(t); + enum btf_fwd_kind fwd_kind = BTF_INFO_KFLAG(t->info); + + struct btf_type *cand_t; + __u16 cand_kind; + __u32 cand_id = 0; + + if (kind != BTF_KIND_FWD) + return 0; + + /* Skip if this FWD already has a mapping */ + if (type_id != d->map[type_id]) + return 0; + + name_off_map__find(names_map, t->name_off, &cand_id); + if (!cand_id) + return 0; + + cand_t = btf_type_by_id(d->btf, cand_id); + cand_kind = btf_kind(cand_t); + if (!(cand_kind == BTF_KIND_STRUCT && fwd_kind == BTF_FWD_STRUCT) && + !(cand_kind == BTF_KIND_UNION && fwd_kind == BTF_FWD_UNION)) + return 0; + + d->map[type_id] = cand_id; + + return 0; +} + +/* + * Standalone fwd declarations deduplication. + * + * The lion's share of all FWD declarations is resolved during + * `btf_dedup_struct_types` phase when different type graphs are + * compared against each other. However, if in some compilation unit a + * FWD declaration is not a part of a type graph compared against + * another type graph that declaration's canonical type would not be + * changed. Example: + * + * CU #1: + * + * struct foo; + * struct foo *some_global; + * + * CU #2: + * + * struct foo { int u; }; + * struct foo *another_global; + * + * After `btf_dedup_struct_types` the BTF looks as follows: + * + * [1] STRUCT 'foo' size=4 vlen=1 ... + * [2] INT 'int' size=4 ... + * [3] PTR '(anon)' type_id=1 + * [4] FWD 'foo' fwd_kind=struct + * [5] PTR '(anon)' type_id=4 + * + * This pass assumes that such FWD declarations should be mapped to + * structs or unions with identical name in case if the name is not + * ambiguous. + */ +static int btf_dedup_standalone_fwds(struct btf_dedup *d) +{ + int i, err; + struct hashmap *names_map = name_off_map__new(); + + if (!names_map) + return -ENOMEM; + + err = btf_dedup_fill_unique_names_map(d, names_map); + if (err < 0) + goto exit; + + for (i = 0; i < d->btf->nr_types; i++) { + err = btf_dedup_standalone_fwd(d, names_map, d->btf->start_id + i); + if (err < 0) + goto exit; + } + +exit: + hashmap__free(names_map); + return err; +} + /* * Compact types. * From patchwork Tue Oct 25 22:27:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019951 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4830AFA3740 for ; Tue, 25 Oct 2022 22:28:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229544AbiJYW2q (ORCPT ); Tue, 25 Oct 2022 18:28:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232169AbiJYW2m (ORCPT ); Tue, 25 Oct 2022 18:28:42 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BD2D13D2F for ; Tue, 25 Oct 2022 15:28:42 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id ud5so12984148ejc.4 for ; Tue, 25 Oct 2022 15:28:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=uJajvPe9ilG6Fw3YANChPsLWSoYVpkS46sMZxS+1HnE=; b=H/gvAtjVJLzu7N39rVafy1BNAdS++1RF2K8/pZVv0B+Z0ayakGtJ/m+NeH+AyIc8IH RAbex5oJwvRD33v9in6N3MwjPMQd/2tJAtC87PVOLkkwGETibdbTIgH90621gy8kD6AR 90JyGifTGqTFOkcnvA3rdQY3hNUok2/O9AbwiifAhie5mrohF7jvirSucFuKwUxDdNHn qSkLOGzIsFBmCvfzdVVXuA+Vc+VSSt7kjjqWQO7OAfh/8VuEwBpaaTmqqJQVFKkAqhb6 dL3uPVqnf8mFmvsM9z4kYZLQLXxtbvHi1rz8dCsaCuDFppwjC9MX+Jc9GyZgEUdhAZpF x89g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uJajvPe9ilG6Fw3YANChPsLWSoYVpkS46sMZxS+1HnE=; b=3iisJiX5ar7uH2P1nA+UCJmfvrWMCMdGrMZBR/9QMRAoy7Xw8cZcNMq5WIawbwryP0 JWNb5NyK0iXZMeWTyounyGiqUu7pOI/UJhxKvIv+WGHg9HFSSnmMv7PRqij1hfdEjJ+v aLSAzyb0P7sJVtp8MM+kRYU9+HZoic3KDk938qBk9mABi0wzxUNUORp1xilScqSAgKAM EC8T+h6eM9VHn7763GEdFWlQrISc8fKNSPoSnEideKRBalyb0bz20qgHy6sARbMJirDY uVVT/pZZ4DJpDMFzolZveQyweqxOvjjHaOdnOZtUj/Ak8mHgh9W+TP5yx8r0aA7u2uYO kwGw== X-Gm-Message-State: ACrzQf14jj7r00Ae3Bo8XC98uTXwgj+fJwaHBXtPrJxUEUzpZQXvVpr6 Lf/v+rsLYWm6UWRhOJtOZaXLM88mjzhIisCS X-Google-Smtp-Source: AMsMyM7qcFEMG/d9RmxhGsKDBCZX8Iw/S8FWZMrmKA5VIBKzBJsRb2oJAm4nGUH5Q2uWtONXL0tMIw== X-Received: by 2002:a17:906:8a6c:b0:7a8:2f09:d88d with SMTP id hy12-20020a1709068a6c00b007a82f09d88dmr11051426ejc.49.1666736920419; Tue, 25 Oct 2022 15:28:40 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:40 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 02/12] selftests/bpf: Tests for standalone forward BTF declarations deduplication Date: Wed, 26 Oct 2022 01:27:51 +0300 Message-Id: <20221025222802.2295103-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Tests to verify the following behavior of `btf_dedup_standalone_fwds`: - remapping for struct forward declarations; - remapping for union forward declarations; - no remapping if forward declaration kind does not match similarly named struct or union declaration; - no remapping if forward declaration name is ambiguous. Signed-off-by: Eduard Zingerman --- tools/testing/selftests/bpf/prog_tests/btf.c | 152 +++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c index 127b8caa3dc1..f14020d51ab9 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf.c +++ b/tools/testing/selftests/bpf/prog_tests/btf.c @@ -7598,6 +7598,158 @@ static struct btf_dedup_test dedup_tests[] = { BTF_STR_SEC("\0e1\0e1_val"), }, }, +{ + .descr = "dedup: standalone fwd declaration struct", + /* + * // CU 1: + * struct foo { int x; }; + * struct foo *a; + * + * // CU 2: + * struct foo; + * struct foo *b; + */ + .input = { + .raw_types = { + BTF_STRUCT_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_FWD_ENC(NAME_TBD, 0), /* [4] */ + BTF_PTR_ENC(4), /* [5] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x"), + }, + .expect = { + .raw_types = { + BTF_STRUCT_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x"), + }, +}, +{ + .descr = "dedup: standalone fwd declaration union", + /* + * // CU 1: + * union foo { int x; }; + * union foo *another_global; + * + * // CU 2: + * union foo; + * union foo *some_global; + */ + .input = { + .raw_types = { + BTF_UNION_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_FWD_ENC(NAME_TBD, 1), /* [4] */ + BTF_PTR_ENC(4), /* [5] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x"), + }, + .expect = { + .raw_types = { + BTF_UNION_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x"), + }, +}, +{ + .descr = "dedup: standalone fwd declaration wrong kind", + /* + * // CU 1: + * struct foo { int x; }; + * struct foo *b; + * + * // CU 2: + * union foo; + * union foo *a; + */ + .input = { + .raw_types = { + BTF_STRUCT_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_FWD_ENC(NAME_TBD, 1), /* [4] */ + BTF_PTR_ENC(4), /* [5] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x"), + }, + .expect = { + .raw_types = { + BTF_STRUCT_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_FWD_ENC(NAME_TBD, 1), /* [4] */ + BTF_PTR_ENC(4), /* [5] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x"), + }, +}, +{ + .descr = "dedup: standalone fwd declaration name conflict", + /* + * // CU 1: + * struct foo { int x; }; + * struct foo *a; + * + * // CU 2: + * struct foo; + * struct foo *b; + * + * // CU 3: + * struct foo { int x; int y; }; + * struct foo *c; + */ + .input = { + .raw_types = { + BTF_STRUCT_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_FWD_ENC(NAME_TBD, 0), /* [4] */ + BTF_PTR_ENC(4), /* [5] */ + BTF_STRUCT_ENC(NAME_NTH(1), 2, 8), /* [6] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_MEMBER_ENC(NAME_NTH(3), 2, 0), + BTF_PTR_ENC(6), /* [7] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x\0y"), + }, + .expect = { + .raw_types = { + BTF_STRUCT_ENC(NAME_NTH(1), 1, 4), /* [1] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */ + BTF_PTR_ENC(1), /* [3] */ + BTF_FWD_ENC(NAME_TBD, 0), /* [4] */ + BTF_PTR_ENC(4), /* [5] */ + BTF_STRUCT_ENC(NAME_NTH(1), 2, 8), /* [6] */ + BTF_MEMBER_ENC(NAME_NTH(2), 2, 0), + BTF_MEMBER_ENC(NAME_NTH(3), 2, 0), + BTF_PTR_ENC(6), /* [7] */ + BTF_END_RAW, + }, + BTF_STR_SEC("\0foo\0x\0y"), + }, +}, }; From patchwork Tue Oct 25 22:27:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019952 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C7BBFA373D for ; Tue, 25 Oct 2022 22:28:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229497AbiJYW2p (ORCPT ); Tue, 25 Oct 2022 18:28:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229544AbiJYW2n (ORCPT ); Tue, 25 Oct 2022 18:28:43 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15789205FF for ; Tue, 25 Oct 2022 15:28:42 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id t25so11005240ejb.8 for ; Tue, 25 Oct 2022 15:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=Pr9TrkVKhYPYCcTPL4D6/3GF5o1ebBiH+ZuikBgJUeE=; b=hxZSFIGX8eJsTlxcCo+zCfwGGxAa9qi+IyX8cNWN55VJRqSkU+MSYPi+5ec8Qd6+ts yMKLxGabskcXlxFHm9l3VrDpVuVvCK62o7mzz4znW8O8ulLwgw3oyGV2ucAvzFJC3Z7I dZ+BeMc6Y1r017zz3gIbUUpqwzo7kbRayIJi0ISagKJRRmUYH7hA3utJAyzEIuqndhJl 8D0k4KKL2zoQz+hHkJEWno6HrwGsmgB2LA4wzcIAItyDDQP8elcL8dwKbF6AyjRMIpoR 9oalE9aOGa+TCXfKHj/78fmHTiA1ontJVlW/1zMSGs7V87ERrQDTiN43swZi7melQL6O dp3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pr9TrkVKhYPYCcTPL4D6/3GF5o1ebBiH+ZuikBgJUeE=; b=x7R94ejXeG4/l8wEdoR/3bwMAC1jumrhBc4ID+eki72nwsxH5q0kXVS5YEpp3MZgfp TTOdcPH3cwdn/FTrBdpOzSABd/pGXZBtDqi4WaO1pj1SJSpUbLseoSliUuEl5ZJHip4v aZeO4fJH1ZXjJAFYan8Dd/VAcR1RYS7sC+2lTMtWS86RWsj9OQo0mNsxvhx6ELGk/UTk 2HjTIm+LAz32u0xLcijvoxiehjkmqN6xJIo4XKMl2X6185TRW0M3/BsviBKGNuWIWDCJ jvWLJaMN2+G5YKneK9uErE1KA4CDqD65H+SvVSaFE2oTGxh4evN7jOCQ9ke+NlRiieX3 E1Aw== X-Gm-Message-State: ACrzQf0MbRg/Y3s6rvRx6TCKdR48ADkVKPjtqfS7W96kfuu+nYvkI3OX +UemgDa9PkhHc3iaPIMUoZO307S4BtDDvZRh X-Google-Smtp-Source: AMsMyM7BwqkETyU1jEmAgT/SYfsUHJtbDc0uTfUJiZlkQw4hsNZeMoOPMrPzU4knF03HdeU9GdklCw== X-Received: by 2002:a17:906:ee89:b0:73d:70c5:1a4e with SMTP id wt9-20020a170906ee8900b0073d70c51a4emr33131847ejb.683.1666736921434; Tue, 25 Oct 2022 15:28:41 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:41 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 03/12] libbpf: Support for BTF_DECL_TAG dump in C format Date: Wed, 26 Oct 2022 01:27:52 +0300 Message-Id: <20221025222802.2295103-4-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC At C level BTF_DECL_TAGs are represented as __attribute__ declarations, e.g.: struct foo { ...; } __attribute__((btf_decl_tag("bar"))); This commit covers only decl tags attached to structs and unions. BTF doc says that BTF_DECL_TAGs should follow a target type but this is not enforced and tests don't honor this restriction. This commit uses hash table to map types to the list of decl tags. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/btf_dump.c | 143 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index bf0cc0e986dd..9bfe2a4ae277 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -75,6 +75,15 @@ struct btf_dump_data { bool is_array_char; }; +/* + * An array of ids of BTF_DECL_TAG objects associated with a specific type. + */ +struct decl_tag_array { + __u16 cnt; + __u16 cap; + __u32 tag_ids[0]; +}; + struct btf_dump { const struct btf *btf; btf_dump_printf_fn_t printf_fn; @@ -111,6 +120,11 @@ struct btf_dump { * name occurrences */ struct hashmap *ident_names; + /* + * maps type id to decl_tag_array, assume that relatively small + * fraction of types has btf_decl_tag's attached + */ + struct hashmap *decl_tags; /* * data for typed display; allocated if needed. */ @@ -127,6 +141,26 @@ static bool str_equal_fn(const void *a, const void *b, void *ctx) return strcmp(a, b) == 0; } +static size_t int_hash_fn(const void *key, void *ctx) +{ + int i; + size_t h = 0; + char *bytes = (char *)key; + + for (i = 0; i < 4; ++i) + h = h * 31 + bytes[i]; + + return h; +} + +static bool int_equal_fn(const void *a, const void *b, void *ctx) +{ + int *ia = (int *)a; + int *ib = (int *)b; + + return *ia == *ib; +} + static const char *btf_name_of(const struct btf_dump *d, __u32 name_off) { return btf__name_by_offset(d->btf, name_off); @@ -143,6 +177,7 @@ static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...) static int btf_dump_mark_referenced(struct btf_dump *d); static int btf_dump_resize(struct btf_dump *d); +static int btf_dump_assign_decl_tags(struct btf_dump *d); struct btf_dump *btf_dump__new(const struct btf *btf, btf_dump_printf_fn_t printf_fn, @@ -179,11 +214,24 @@ struct btf_dump *btf_dump__new(const struct btf *btf, d->ident_names = NULL; goto err; } + d->decl_tags = hashmap__new(int_hash_fn, int_equal_fn, NULL); + if (IS_ERR(d->decl_tags)) { + err = PTR_ERR(d->decl_tags); + d->decl_tags = NULL; + goto err; + } err = btf_dump_resize(d); if (err) goto err; + err = btf_dump_assign_decl_tags(d); + if (err) + goto err; + + if (err) + goto err; + return d; err: btf_dump__free(d); @@ -232,7 +280,8 @@ static void btf_dump_free_names(struct hashmap *map) void btf_dump__free(struct btf_dump *d) { - int i; + int i, bkt; + struct hashmap_entry *cur; if (IS_ERR_OR_NULL(d)) return; @@ -250,6 +299,9 @@ void btf_dump__free(struct btf_dump *d) free(d->decl_stack); btf_dump_free_names(d->type_names); btf_dump_free_names(d->ident_names); + hashmap__for_each_entry(d->decl_tags, cur, bkt) + free(cur->value); + hashmap__free(d->decl_tags); free(d); } @@ -373,6 +425,77 @@ static int btf_dump_mark_referenced(struct btf_dump *d) return 0; } +static struct decl_tag_array *btf_dump_find_decl_tags(struct btf_dump *d, __u32 id) +{ + struct decl_tag_array *decl_tags = NULL; + + hashmap__find(d->decl_tags, &id, (void **)&decl_tags); + + return decl_tags; +} + +static struct decl_tag_array *realloc_decl_tags(struct decl_tag_array *tags, __u16 new_cap) +{ + size_t new_size = sizeof(struct decl_tag_array) + new_cap * sizeof(__u32); + struct decl_tag_array *new_tags = (tags + ? realloc(tags, new_size) + : calloc(1, new_size)); + + if (!new_tags) + return NULL; + + new_tags->cap = new_cap; + + return new_tags; +} + +/* + * Scans all BTF objects looking for BTF_KIND_DECL_TAG entries. + * The id's of the entries are stored in the `btf_dump.decl_tags` table, + * grouped by a target type. + */ +static int btf_dump_assign_decl_tags(struct btf_dump *d) +{ + int err; + __u32 id; + __u32 n = btf__type_cnt(d->btf); + __u32 new_capacity; + const struct btf_type *t; + struct decl_tag_array *decl_tags; + + for (id = 0; id < n; id++) { + t = btf__type_by_id(d->btf, id); + + if (btf_kind(t) != BTF_KIND_DECL_TAG) + continue; + + decl_tags = btf_dump_find_decl_tags(d, t->type); + if (!decl_tags) { + decl_tags = realloc_decl_tags(NULL, 1); + if (!decl_tags) + return -ENOMEM; + err = hashmap__insert(d->decl_tags, &t->type, decl_tags, + HASHMAP_SET, NULL, NULL); + if (err) + return err; + } else if (decl_tags->cnt == decl_tags->cap) { + new_capacity = decl_tags->cap * 2; + if (new_capacity > 0xffff) + return -ERANGE; + decl_tags = realloc_decl_tags(decl_tags, new_capacity); + if (!decl_tags) + return -ENOMEM; + decl_tags->cap = new_capacity; + err = hashmap__update(d->decl_tags, &t->type, decl_tags, NULL, NULL); + if (err) + return err; + } + decl_tags->tag_ids[decl_tags->cnt++] = id; + } + + return 0; +} + static int btf_dump_add_emit_queue_id(struct btf_dump *d, __u32 id) { __u32 *new_queue; @@ -899,6 +1022,23 @@ static void btf_dump_emit_bit_padding(const struct btf_dump *d, } } +static void btf_dump_emit_decl_tags(struct btf_dump *d, __u32 id) +{ + struct decl_tag_array *decl_tags = btf_dump_find_decl_tags(d, id); + struct btf_type *decl_tag_t; + const char *decl_tag_text; + __u32 i; + + if (!decl_tags) + return; + + for (i = 0; i < decl_tags->cnt; ++i) { + decl_tag_t = btf_type_by_id(d->btf, decl_tags->tag_ids[i]); + decl_tag_text = btf__name_by_offset(d->btf, decl_tag_t->name_off); + btf_dump_printf(d, " __attribute__((btf_decl_tag(\"%s\")))", decl_tag_text); + } +} + static void btf_dump_emit_struct_fwd(struct btf_dump *d, __u32 id, const struct btf_type *t) { @@ -964,6 +1104,7 @@ static void btf_dump_emit_struct_def(struct btf_dump *d, btf_dump_printf(d, "%s}", pfx(lvl)); if (packed) btf_dump_printf(d, " __attribute__((packed))"); + btf_dump_emit_decl_tags(d, id); } static const char *missing_base_types[][2] = { From patchwork Tue Oct 25 22:27:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019953 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA134FA3742 for ; Tue, 25 Oct 2022 22:28:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232169AbiJYW2q (ORCPT ); Tue, 25 Oct 2022 18:28:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232332AbiJYW2p (ORCPT ); Tue, 25 Oct 2022 18:28:45 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 414C8205FF for ; Tue, 25 Oct 2022 15:28:44 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id fy4so16540373ejc.5 for ; Tue, 25 Oct 2022 15:28:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=Ma0BF2cRyOQRhSe+pUioeCpF+s2k/1XYgu3BKKXbb8w=; b=FV2j8PwgRQFxEaOo6no+YWQ0AsCTtGAsxETnG/6KWF+pIc4xzIPWYs/04uxiETJqe8 +TxDdTq1eV2mFEG1M4hGtOE3Wn15qizaG54MXOmPhGQukY2ygOvty2xRs2NXmKw0RGQp a1QSlX7WbNs3cCAQVDX6XCuyTkX/DEdFuOqXBWXceY0ioH607pjH4/BW5Hr1/A3rgdKv rqJI5MvRgPtdNR1UC4pOSzmzhZo/R9Q3ezycshQbmHRA/+Q7nGSZTw9wzUA9K+ghV19f OLhEm76vahjhWimFC1pPqUS2lfp90LDcK+UDvq0N29atBLPOkuN3hZiCgn1Xaoqy02NH 5IWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ma0BF2cRyOQRhSe+pUioeCpF+s2k/1XYgu3BKKXbb8w=; b=jvKxLlCbzOwnceGpRMrkq+Qu0E3iYPsMz28vfs1u7F7JREAe9d7Y7UoPS0+pA2nK6l w3+/o9h3AJ5lYaAgIsFIPDWT9YI5eyuyPQm9TuH5UjEpbgwGMpZrfVd7/92Ni7SpHx/7 3dnEJOdZhWEB2xV7obLg29qdtI8k0byWdVnixk2VpovbHj1uIajFGTZPhHes1Rzf408/ B3U5VXsDESL/X+VgJ9fJ9M+NC6SVKEkCgrMUIhxkKkh9kSiCRBd4oJsONu/+xqWhAUTM qGNgI442WluBdDIFE7h4KraqBrR48xZnwdwvCPMJPJ0i2eBhDyWWyF1HEU0Rfsrs1OYP OcrQ== X-Gm-Message-State: ACrzQf0hSkw6rgk9zAsx5YjZaWSnuYZAqGjSeMRJTi3ImfQlfylhhr/R SUA6JQxSOv6CIdIW+0tNgEHbqg8dT5ZK54W3 X-Google-Smtp-Source: AMsMyM5eFL0FWDPYZvGs+nIYzyixEzb33H2uGRNzpUjU/wHk+lc1jxR3bIHtJybMtLo1Nhmse5yYvg== X-Received: by 2002:a17:906:fd84:b0:730:acee:d067 with SMTP id xa4-20020a170906fd8400b00730aceed067mr34763920ejb.206.1666736922530; Tue, 25 Oct 2022 15:28:42 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:42 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 04/12] selftests/bpf: Tests for BTF_DECL_TAG dump in C format Date: Wed, 26 Oct 2022 01:27:53 +0300 Message-Id: <20221025222802.2295103-5-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Covers the following cases: - `__atribute__((btf_decl_tag("...")))` could be applied to structs and unions; - decl tag applied to an empty struct is printed on a single line; - decl tags with the same name could be applied to several structs; - several decl tags could be applied to the same struct; - attribute `packed` works fine with decl tags (it is a separate branch in `tools/lib/bpf/btf_dump.c:btf_dump_emit_attributes`. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/btf_dump.c | 1 + .../bpf/progs/btf_dump_test_case_decl_tag.c | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_decl_tag.c diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c index 24da335482d4..5f6ce7f1a801 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c @@ -21,6 +21,7 @@ static struct btf_dump_test_case { {"btf_dump: bitfields", "btf_dump_test_case_bitfields", true}, {"btf_dump: multidim", "btf_dump_test_case_multidim", false}, {"btf_dump: namespacing", "btf_dump_test_case_namespacing", false}, + {"btf_dump: decl_tag", "btf_dump_test_case_decl_tag", true}, }; static int btf_dump_all_types(const struct btf *btf, void *ctx) diff --git a/tools/testing/selftests/bpf/progs/btf_dump_test_case_decl_tag.c b/tools/testing/selftests/bpf/progs/btf_dump_test_case_decl_tag.c new file mode 100644 index 000000000000..470bbbb530dc --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf_dump_test_case_decl_tag.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) + +/* + * BTF-to-C dumper test for __atribute__((btf_decl_tag("..."))). + */ +/* ----- START-EXPECTED-OUTPUT ----- */ +struct empty_with_tag {} __attribute__((btf_decl_tag("a"))); + +struct one_tag { + int x; +} __attribute__((btf_decl_tag("b"))); + +struct same_tag { + int x; +} __attribute__((btf_decl_tag("b"))); + +struct two_tags { + int x; +} __attribute__((btf_decl_tag("a"))) __attribute__((btf_decl_tag("b"))); + +struct packed { + int x; + short y; +} __attribute__((packed)) __attribute__((btf_decl_tag("another_name"))); + +struct root_struct { + struct empty_with_tag a; + struct one_tag b; + struct same_tag c; + struct two_tags d; + struct packed e; +}; + +/* ------ END-EXPECTED-OUTPUT ------ */ + +int f(struct root_struct *s) +{ + return 0; +} From patchwork Tue Oct 25 22:27:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019955 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4EF8FA373D for ; Tue, 25 Oct 2022 22:28:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232665AbiJYW2s (ORCPT ); Tue, 25 Oct 2022 18:28:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232280AbiJYW2q (ORCPT ); Tue, 25 Oct 2022 18:28:46 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AD6713D2F for ; Tue, 25 Oct 2022 15:28:45 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id kt23so10977258ejc.7 for ; Tue, 25 Oct 2022 15:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=UOHxE87HEjX67Sgs/cOkPKwgGjyuyfVIaFsiIyCh2h0=; b=b0NE/+6MxKyMVSh6/jiNOubuLsDn3nGv3kHo6vUvBI9NJJXgc4HVWrbuCP61HZHyxM nM7rQ34mKof9PbwAjuU4osElYdqlYF22SpGYkaNlu5k8T1kVVcVrZ5u70URRg1ZXC4aE G3xi7M9bztBIR0kUivgCIXfY0iohDROGlMVz26nIUFJ/i/QBpziZjZ8zmlOgisz38Eo4 wnw6otHQqLi8YR347gNKb+XB5RcBf4X+MxZYfnpuTOlWeRAbiutrnINikj+w/0Nw965t W/w6k3JptGgeDaAa+JZhDKY3ER+C39bR8l/m+zZy6qbo7e+jDfmGc1eL02OKqB5kXcsh LpFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UOHxE87HEjX67Sgs/cOkPKwgGjyuyfVIaFsiIyCh2h0=; b=HA8PghOOJWMY7gYqBspfPGoTrjifeF68gYYGknRW33mkbqxepFmWMQ9ObEmKUXDzU2 DuSWt3P9Or/I5EKyqDdOMG6vXLEUaJ36XutsTGcGEQmB3IkPZ9J7UhAeQ2Xc/en0IrWY e5lI9QpV2xtVtUyD6GBRWx1Qf7Vdh08FccjM/tT1RAb5KuuLNrRBfc2wFMGZ5JlVja9P nuR4M2MVJHQ7Ns6lE6oMiUP2ucYqIdAEHhcuJb1Gq1WMO7+U6wLotwm9XmquZ3rGD6UU b4Lj87Bz4FQJgQtEdq65zqn3RQwQRsTEJevGXG6yRLXqt2tIcsp9YnzgLUJCar5YuiZ/ pbOw== X-Gm-Message-State: ACrzQf2Cfxs+K33i15Nxtpb/JJHr1RNhXBtZgNUUqg3p7oc6fue95CTP hPQFdnOjdO7do5p5wSVIT3u3g07NQJ2qq4W6 X-Google-Smtp-Source: AMsMyM5YtWWLU2YkePMwk5HISJEfy9Uj1Q8ncQ+79H7q8kAwdUcjW1JWv3X7foDkrqFo3UW85qb/dQ== X-Received: by 2002:a17:906:ef8c:b0:78d:96b9:a0ad with SMTP id ze12-20020a170906ef8c00b0078d96b9a0admr35010946ejb.529.1666736923551; Tue, 25 Oct 2022 15:28:43 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:43 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 05/12] libbpf: Header guards for selected data structures in vmlinux.h Date: Wed, 26 Oct 2022 01:27:54 +0300 Message-Id: <20221025222802.2295103-6-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC The goal of the patch is to allow usage of header files from `include/uapi` alongside with `vmlinux.h`. E.g. as follows: #include #include "vmlinux.h" This goal is achieved by adding #ifndef / #endif guards in vmlinux.h around definitions that originate from the `include/uapi` headers. The guards emitted match the guards used in the original headers. E.g. as follows: include/uapi/linux/tcp.h: #ifndef _UAPI_LINUX_TCP_H #define _UAPI_LINUX_TCP_H ... union tcp_word_hdr { struct tcphdr hdr; __be32 words[5]; }; ... #endif /* _UAPI_LINUX_TCP_H */ vmlinux.h: ... #ifndef _UAPI_LINUX_TCP_H union tcp_word_hdr { struct tcphdr hdr; __be32 words[5]; }; #endif /* _UAPI_LINUX_TCP_H */ ... The problem of identifying data structures from uapi and selecting proper guard names is delegated to pahole. When configured pahole generates fake `BTF_DECL_TAG` records with header guards information. The fake tag is distinguished from a real tag by a prefix "header_guard:" in its value. These tags could be present for unions, structures, enums and typedefs, e.g.: [24139] STRUCT 'tcphdr' size=20 vlen=17 ... [24296] DECL_TAG 'header_guard:_UAPI_LINUX_TCP_H' type_id=24139 ... This patch adds An option `emit_header_guards` to `struct btf_dump_opts`. When this option is present the function `btf_dump__dump_type` emits header guards for top-level declarations. The header guards are identified by inspecting fake `BTF_DECL_TAG` records described above. Signed-off-by: Eduard Zingerman --- tools/lib/bpf/btf.h | 7 +++- tools/lib/bpf/btf_dump.c | 89 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 8e6880d91c84..dcfb3f168750 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -235,8 +235,13 @@ struct btf_dump; struct btf_dump_opts { size_t sz; + /* + * if set to true #ifndef X ... #endif brackets would be printed + * for types marked by BTF_DECL_TAG with "header_guard:" prefix + */ + bool emit_header_guards; }; -#define btf_dump_opts__last_field sz +#define btf_dump_opts__last_field emit_header_guards typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args); diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 9bfe2a4ae277..30b995d209c0 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -113,6 +113,8 @@ struct btf_dump { int decl_stack_cap; int decl_stack_cnt; + bool emit_header_guards; + /* maps struct/union/enum name to a number of name occurrences */ struct hashmap *type_names; /* @@ -202,6 +204,8 @@ struct btf_dump *btf_dump__new(const struct btf *btf, d->cb_ctx = ctx; d->ptr_sz = btf__pointer_size(btf) ? : sizeof(void *); + d->emit_header_guards = OPTS_GET(opts, emit_header_guards, false); + d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL); if (IS_ERR(d->type_names)) { err = PTR_ERR(d->type_names); @@ -347,6 +351,8 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id) return 0; } +static const char *btf_dump_is_header_guard_tag(struct btf_dump *d, const struct btf_type *t); + /* * Mark all types that are referenced from any other type. This is used to * determine top-level anonymous enums that need to be emitted as an @@ -384,11 +390,15 @@ static int btf_dump_mark_referenced(struct btf_dump *d) case BTF_KIND_TYPEDEF: case BTF_KIND_FUNC: case BTF_KIND_VAR: - case BTF_KIND_DECL_TAG: case BTF_KIND_TYPE_TAG: d->type_states[t->type].referenced = 1; break; + case BTF_KIND_DECL_TAG: + if (!btf_dump_is_header_guard_tag(d, t)) + d->type_states[t->type].referenced = 1; + break; + case BTF_KIND_ARRAY: { const struct btf_array *a = btf_array(t); @@ -449,6 +459,40 @@ static struct decl_tag_array *realloc_decl_tags(struct decl_tag_array *tags, __u return new_tags; } +#define HEADER_GUARD_TAG "header_guard:" + +static const char *btf_dump_is_header_guard_tag(struct btf_dump *d, const struct btf_type *t) +{ + const char *tag_value; + int tag_len = strlen(HEADER_GUARD_TAG); + + tag_value = btf__str_by_offset(d->btf, t->name_off); + if (strncmp(tag_value, HEADER_GUARD_TAG, tag_len)) + return NULL; + + return &tag_value[tag_len]; +} + +static const char *btf_dump_find_header_guard(struct btf_dump *d, __u32 id) +{ + struct decl_tag_array *decl_tags = btf_dump_find_decl_tags(d, id); + const struct btf_type *t; + const char *guard; + int i; + + if (!decl_tags) + return NULL; + + for (i = 0; i < decl_tags->cnt; ++i) { + t = btf__type_by_id(d->btf, decl_tags->tag_ids[i]); + guard = btf_dump_is_header_guard_tag(d, t); + if (guard) + return guard; + } + + return NULL; +} + /* * Scans all BTF objects looking for BTF_KIND_DECL_TAG entries. * The id's of the entries are stored in the `btf_dump.decl_tags` table, @@ -770,6 +814,8 @@ static const char *btf_dump_type_name(struct btf_dump *d, __u32 id); static const char *btf_dump_ident_name(struct btf_dump *d, __u32 id); static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map, const char *orig_name); +static void btf_dump_emit_guard_start(struct btf_dump *d, __u32 id); +static void btf_dump_emit_guard_end(struct btf_dump *d, __u32 id); static bool btf_dump_is_blacklisted(struct btf_dump *d, __u32 id) { @@ -835,8 +881,10 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) id); return; } + btf_dump_emit_guard_start(d, id); btf_dump_emit_struct_fwd(d, id, t); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); tstate->fwd_emitted = 1; break; case BTF_KIND_TYPEDEF: @@ -846,8 +894,10 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) * references through pointer only, not for embedding */ if (!btf_dump_is_blacklisted(d, id)) { + btf_dump_emit_guard_start(d, id); btf_dump_emit_typedef_def(d, id, t, 0); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); } tstate->fwd_emitted = 1; break; @@ -868,8 +918,10 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) case BTF_KIND_ENUM: case BTF_KIND_ENUM64: if (top_level_def) { + btf_dump_emit_guard_start(d, id); btf_dump_emit_enum_def(d, id, t, 0); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); } tstate->emit_state = EMITTED; break; @@ -884,8 +936,10 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) btf_dump_emit_type(d, btf_array(t)->type, cont_id); break; case BTF_KIND_FWD: + btf_dump_emit_guard_start(d, id); btf_dump_emit_fwd_def(d, id, t); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); tstate->emit_state = EMITTED; break; case BTF_KIND_TYPEDEF: @@ -899,8 +953,10 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) * emit typedef as a forward declaration */ if (!tstate->fwd_emitted && !btf_dump_is_blacklisted(d, id)) { + btf_dump_emit_guard_start(d, id); btf_dump_emit_typedef_def(d, id, t, 0); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); } tstate->emit_state = EMITTED; break; @@ -923,14 +979,18 @@ static void btf_dump_emit_type(struct btf_dump *d, __u32 id, __u32 cont_id) for (i = 0; i < vlen; i++, m++) btf_dump_emit_type(d, m->type, new_cont_id); } else if (!tstate->fwd_emitted && id != cont_id) { + btf_dump_emit_guard_start(d, id); btf_dump_emit_struct_fwd(d, id, t); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); tstate->fwd_emitted = 1; } if (top_level_def) { + btf_dump_emit_guard_start(d, id); btf_dump_emit_struct_def(d, id, t, 0); btf_dump_printf(d, ";\n\n"); + btf_dump_emit_guard_end(d, id); tstate->emit_state = EMITTED; } else { tstate->emit_state = NOT_EMITTED; @@ -1034,6 +1094,8 @@ static void btf_dump_emit_decl_tags(struct btf_dump *d, __u32 id) for (i = 0; i < decl_tags->cnt; ++i) { decl_tag_t = btf_type_by_id(d->btf, decl_tags->tag_ids[i]); + if (btf_dump_is_header_guard_tag(d, decl_tag_t)) + continue; decl_tag_text = btf__name_by_offset(d->btf, decl_tag_t->name_off); btf_dump_printf(d, " __attribute__((btf_decl_tag(\"%s\")))", decl_tag_text); } @@ -1672,6 +1734,31 @@ static void btf_dump_emit_type_cast(struct btf_dump *d, __u32 id, btf_dump_printf(d, ")"); } +static void btf_dump_emit_guard_start(struct btf_dump *d, __u32 id) +{ + const char *header_guard; + + if (!d->emit_header_guards) + return; + + header_guard = btf_dump_find_header_guard(d, id); + if (!header_guard) + return; + + btf_dump_printf(d, "#ifndef %s\n\n", header_guard); +} + +static void btf_dump_emit_guard_end(struct btf_dump *d, __u32 id) +{ + if (!d->emit_header_guards) + return; + + if (!btf_dump_find_header_guard(d, id)) + return; + + btf_dump_printf(d, "#endif\n\n"); +} + /* return number of duplicates (occurrences) of a given name */ static size_t btf_dump_name_dups(struct btf_dump *d, struct hashmap *name_map, const char *orig_name) From patchwork Tue Oct 25 22:27:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019954 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41487C38A2D for ; Tue, 25 Oct 2022 22:28:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232332AbiJYW2r (ORCPT ); Tue, 25 Oct 2022 18:28:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230327AbiJYW2q (ORCPT ); Tue, 25 Oct 2022 18:28:46 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D843638F for ; Tue, 25 Oct 2022 15:28:45 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id ud5so12984519ejc.4 for ; Tue, 25 Oct 2022 15:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=rQBR3N4ypelebWkIW3P1nYGuZKygRIlddf/fNdCwan4=; b=OO25d2aY0ZgQIEcz5xnQJptHgbeG4bju0cpSYCe5VMHLYi5tDTMez0FYp6B3Vn7ZDr 7KP09DIWlGKDjUpFM9w11jSEyEEppzy9zmFCwxi/Duj7N+bbdX/5RhMgjhkDsi2fNLza x9NYZWYPS7o5KA4MmrTIUvjNRnYV9ZzpCBjn2VJSoHoVWB56mwcCL/F33+zvYWODakIN zImZyG12rrQN3bWMkMv0IuwFvNHlgIinkkQSX8L5lPirXSnNILjgJkC637VfbqJCgo2y z/I8CR/7yhn85at+kDnRxBR0H4X2Mx8yyN5Ji11IilwAO91sZxJsTzA5xrDZDzkL1EYA ydiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rQBR3N4ypelebWkIW3P1nYGuZKygRIlddf/fNdCwan4=; b=YVDvBCSUdsps9nW8Yb2uLZICT6SAazABGJLZNC4anKzVyrc+3Y3Z5fdoj2QJXek0sG j27DfVgSFYotCW8GFuTovD/eUQJIV6kTiIAxiYKJh/HOnIgO7PhFOwJzVLrUrO4UDp+Q 39svrrHF8f1IvGQnkQH9U9bypwgx0+/BCpqxP8LLNB9baOOv1dcyg5fNlQ0c/rcISvQh /eBiq1dOcuIN4h1F9E0YVmD5M4lGV9i8R9rySif5BcKrkiiuxPeXJoV9WlM9o5v2haOD AqBFD4D7PjVMKkhY+9ygpXRt+3KhzBEJPgoBAC9CmDXRfzA08bC0oik+KKuuE0QWvgLt lHjQ== X-Gm-Message-State: ACrzQf1nA+78B/eh83visLM2q97kkVODrOa5GHXLLMCwPB83qxWKcA5d Od9DitXE+sMIpkQsl+R04rJGYoo8KXKKsUXk X-Google-Smtp-Source: AMsMyM48FYPlTjPyFlgIXjJzeou1KpQCMnB10z8IdeWvnp/CTid3Rqu5L3UFriJtOaRUbRk5m3GJVQ== X-Received: by 2002:a17:907:97d5:b0:7ac:5f72:6c1a with SMTP id js21-20020a17090797d500b007ac5f726c1amr4753969ejc.126.1666736924611; Tue, 25 Oct 2022 15:28:44 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:44 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 06/12] selftests/bpf: Tests for header guards printing in BTF dump Date: Wed, 26 Oct 2022 01:27:55 +0300 Message-Id: <20221025222802.2295103-7-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Verify that `btf_dump__dump_type` emits header guard brackets for various types when `btf_dump_opts.emit_header_guards` is set to true. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/btf_dump.c | 10 +- .../progs/btf_dump_test_case_header_guards.c | 94 +++++++++++++++++++ 2 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c index 5f6ce7f1a801..a3db352e61c7 100644 --- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c +++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c @@ -13,6 +13,7 @@ static struct btf_dump_test_case { const char *name; const char *file; bool known_ptr_sz; + bool emit_header_guards; } btf_dump_test_cases[] = { {"btf_dump: syntax", "btf_dump_test_case_syntax", true}, {"btf_dump: ordering", "btf_dump_test_case_ordering", false}, @@ -22,15 +23,18 @@ static struct btf_dump_test_case { {"btf_dump: multidim", "btf_dump_test_case_multidim", false}, {"btf_dump: namespacing", "btf_dump_test_case_namespacing", false}, {"btf_dump: decl_tag", "btf_dump_test_case_decl_tag", true}, + {"btf_dump: header guards", "btf_dump_test_case_header_guards", true, true}, }; -static int btf_dump_all_types(const struct btf *btf, void *ctx) +static int btf_dump_all_types(const struct btf *btf, void *ctx, struct btf_dump_test_case *t) { size_t type_cnt = btf__type_cnt(btf); + LIBBPF_OPTS(btf_dump_opts, opts); struct btf_dump *d; int err = 0, id; - d = btf_dump__new(btf, btf_dump_printf, ctx, NULL); + opts.emit_header_guards = t->emit_header_guards; + d = btf_dump__new(btf, btf_dump_printf, ctx, &opts); err = libbpf_get_error(d); if (err) return err; @@ -87,7 +91,7 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t) goto done; } - err = btf_dump_all_types(btf, f); + err = btf_dump_all_types(btf, f, t); fclose(f); close(fd); if (CHECK(err, "btf_dump", "failure during C dumping: %d\n", err)) { diff --git a/tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c b/tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c new file mode 100644 index 000000000000..3ee8aaba9e0a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) + +/* + * BTF-to-C dumper test for header guards. + */ +struct hg_struct { + int x; +} __attribute__((btf_decl_tag("header_guard:S"))); + +union hg_union { + int x; +} __attribute__((btf_decl_tag("header_guard:U"))); + +typedef int hg_typedef __attribute__((btf_decl_tag("header_guard:T"))); + +struct hg_fwd_a; + +struct hg_fwd_b { + struct hg_fwd_a *loop; +} __attribute__((btf_decl_tag("header_guard:FWD"))); + +struct hg_fwd_a { + struct hg_fwd_b *loop; +} __attribute__((btf_decl_tag("header_guard:FWD"))); + +struct root_struct { + struct hg_struct a; + union hg_union b; + hg_typedef c; + struct hg_fwd_a d; + struct hg_fwd_b e; +}; + +/* ----- START-EXPECTED-OUTPUT ----- */ +/* + *#ifndef S + * + *struct hg_struct { + * int x; + *}; + * + *#endif + * + *#ifndef U + * + *union hg_union { + * int x; + *}; + * + *#endif + * + *#ifndef T + * + *typedef int hg_typedef; + * + *#endif + * + *#ifndef FWD + * + *struct hg_fwd_b; + * + *#endif + * + *#ifndef FWD + * + *struct hg_fwd_a { + * struct hg_fwd_b *loop; + *}; + * + *#endif + * + *#ifndef FWD + * + *struct hg_fwd_b { + * struct hg_fwd_a *loop; + *}; + * + *#endif + * + *struct root_struct { + * struct hg_struct a; + * union hg_union b; + * hg_typedef c; + * struct hg_fwd_a d; + * struct hg_fwd_b e; + *}; + * + */ +/* ------ END-EXPECTED-OUTPUT ------ */ + +int f(struct root_struct *s) +{ + return 0; +} From patchwork Tue Oct 25 22:27:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019956 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2E3DECDFA1 for ; Tue, 25 Oct 2022 22:28:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232724AbiJYW2u (ORCPT ); Tue, 25 Oct 2022 18:28:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232673AbiJYW2s (ORCPT ); Tue, 25 Oct 2022 18:28:48 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5AA08785A7 for ; Tue, 25 Oct 2022 15:28:47 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id y12so18326020edc.9 for ; Tue, 25 Oct 2022 15:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=fJ6mjJpQHVqvM/2ly6yDyYOtGPBKn15dyRGLuLQdpPY=; b=dNw6cd1RlQ/5PFpbVBmlkqJYq9b2vpk4SaxipQrjTSVnrHpESorAhYE8heA8ReA6Ig 1xHWmbRUeHlsgLOrxDimg+VEo7AsaFVAnHSC+iXq5Zs26XRfTBThJXQcAGa711P0sdIe 0JcEJtBIdHaBYotYfqvdc6LjwVUDiMPCeFC2Rlj7/i2waWjWY+pbik2u/gtnx8NJEzco nG7yF9TduSlAJ1mTyR2Bggjye4bu7/X/MmoibogihL2OM6/mM4aZ0UpcfC4E9L+7cOWr efrJQx4eURf9L4yzW3Q/ICHdUvHrYS6SYIDSUg2TRcnkl7LLQLJ4Oh3Hd9P7eK16u6f5 /nug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fJ6mjJpQHVqvM/2ly6yDyYOtGPBKn15dyRGLuLQdpPY=; b=uqOsRS3+GfOObcs8jb14WenDQH42EfDRIbAWKnOkQaN+1z+QkYC9zrZKu0hq4S8e6H 4Ve/WtnMetl2FY1eC+vtoFgD03bW2zgItPgMTlLAhboPekYsm4Wu2aZ7oi6lnulrzsnp WZnVY3E4EvYLVFyQxOX1Cp71bY3wy6jT06Ib3gmZR5URc1xEYAanUJbDXfR+bbCQUDPP FUaEmEVj5Ue9SNl1FDtG6WF5uIg210I4tVaTYWdPkMKPEm7RE5Da/gK6OMcTR5MAVMBc g7zehAJbBNqusSblX1oLW0fN5jPgoeO/BoxHnn9cddcu9yxxSmww8vXUzdEqK2BUQgx/ kzFA== X-Gm-Message-State: ACrzQf3N1ET/GZs24kst6OugQCDoWqo6WfzhJIzLtdprMsGVxKjNeFmn 7kIOf4DUsZbyCTEb1xOrODjenVDsdqIplcrq X-Google-Smtp-Source: AMsMyM56pJfEQYEViLvI2h/WOMoSv3s6yVaaE1r+PRyx7DLSaAhkJW251N3vqsaHkjNFFWZ0WEzP1A== X-Received: by 2002:a05:6402:5114:b0:45d:b850:a4e2 with SMTP id m20-20020a056402511400b0045db850a4e2mr36956348edd.316.1666736925725; Tue, 25 Oct 2022 15:28:45 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:45 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 07/12] bpftool: Enable header guards generation Date: Wed, 26 Oct 2022 01:27:56 +0300 Message-Id: <20221025222802.2295103-8-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Enables header guards generation for BTF dumps in C format, e.g. vmlinux.h would be printed as follows: ... #ifndef _UAPI_LINUX_TCP_H enum { TCP_NO_QUEUE = 0, TCP_RECV_QUEUE = 1, TCP_SEND_QUEUE = 2, TCP_QUEUES_NR = 3, }; #endif /* _UAPI_LINUX_TCP_H */ ... Signed-off-by: Eduard Zingerman --- tools/bpf/bpftool/btf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 68a70ac03c80..f8e8946b61a7 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -466,7 +466,9 @@ static int dump_btf_c(const struct btf *btf, struct btf_dump *d; int err = 0, i; - d = btf_dump__new(btf, btf_dump_printf, NULL, NULL); + LIBBPF_OPTS(btf_dump_opts, opts); + opts.emit_header_guards = true; + d = btf_dump__new(btf, btf_dump_printf, NULL, &opts); err = libbpf_get_error(d); if (err) return err; From patchwork Tue Oct 25 22:27:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019957 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86F71C38A2D for ; Tue, 25 Oct 2022 22:28:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232619AbiJYW2w (ORCPT ); Tue, 25 Oct 2022 18:28:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232447AbiJYW2t (ORCPT ); Tue, 25 Oct 2022 18:28:49 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 779597D785 for ; Tue, 25 Oct 2022 15:28:47 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id fy4so16540763ejc.5 for ; Tue, 25 Oct 2022 15:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=Y888JDclK3ghVot9Xo2XQze7CjHGsTQvc0cMj3gqRXc=; b=HlTMsqxGKpzRv97N22t03nlacgceAaER0elc3LVgLR5FxAdLLlJu+liAXYfE7JX3do jk9wXyYbTf0uOR3faxlpw3jKt+bF7cGHqWGBulSA3J6VeMi0led/sMvmibVcj26Z38hB sqFVshVIRED6KryKy+HknDnQiFqVMs2gkvaUY2oCz99QYEX7ApqoDhBwx1iAndW5aNZT 67HRVIPbZJxKJmwz1lXjjQIUkVwoQcS8OE3n67uLV7zDBXbO7kvJ/BQ9O3AQmJTDVG8N GTgAMF5eEj3Ru2glatZOXyOeCPwiMGEQ/ganbBHCNMmVRJ6GSc/cPRd4QUth95bIOsmf MVzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y888JDclK3ghVot9Xo2XQze7CjHGsTQvc0cMj3gqRXc=; b=PIbxft2mPjhwJ6yHaxGl/Bl+zYXEbs79ayV3XGFrD5HLf7eytZ2TjAI6vHWVwUoOoZ MZlJY1ZisJN7CS2kI1vDRa5GMNKsG3KdS2lhceRPuxEETafkpDH0Ojc01RiaQ0KwQOJW zr88gZyGgQqJYC7dRxo8G9TZPdOibuWCmXohGptNSWJzcecTtlLim1x4sHbLDn88+s6w zYxhGNdGlZ44+BLPndat8BnN5EnvoHBd/YGcdH8VTVUFql1Fvn1BRwqsA394qgnE8U5Y aqB+qUbUC6u8Qge3CoZFDVwINJMMhX7Lca9EUECuZOMOWOBCZ0GoTGLTMtxCi2m/uJgL Y05g== X-Gm-Message-State: ACrzQf2YHF0JNf8cu9j73xHB1CEmjxZA5IMUVP7UllBmgEM1NvVUfBq+ LVTUPTjDqedfSc/d3r6aiGl7sdQJJbrM92hm X-Google-Smtp-Source: AMsMyM5PcyZFfZEAYeeHWK1NEQ0p5PddV04O42vqQ1zo6EB1bCZX4YeKqRyt/pp4neFCwoQtQURRNQ== X-Received: by 2002:a17:907:72c5:b0:798:1c8f:5bc7 with SMTP id du5-20020a17090772c500b007981c8f5bc7mr26591770ejc.119.1666736926853; Tue, 25 Oct 2022 15:28:46 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:46 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 08/12] kbuild: Script to infer header guard values for uapi headers Date: Wed, 26 Oct 2022 01:27:57 +0300 Message-Id: <20221025222802.2295103-9-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC The script infers header guard defines in headers from include/uapi/**/*.h . E.g. header guard for the `include/uapi/linux/tcp.h` is `_UAPI_LINUX_TCP_H`: include/uapi/linux/tcp.h: #ifndef _UAPI_LINUX_TCP_H #define _UAPI_LINUX_TCP_H ... union tcp_word_hdr { struct tcphdr hdr; __be32 words[5]; }; ... #endif /* _UAPI_LINUX_TCP_H */ The output of the script could be used as an input to pahole's `--header_guards_db` parameter. This information is necessary to repeat the same header guards in the `vmlinux.h` generated from BTF. It is not possible to infer the guard names from header file names alone, the file content has to be analyzed. The following heuristic is used to infer guard for a specific file: - All pairs `#ifndef ` / `#define ` are collected; - If a unique candidate matching regex `${headername}.*_H(EADER)?` it is selected; - If a unique candidate matching regex `_H(EADER)?_` it is selected; - If a unique candidate matching regex `_H(EADER)?$` it is selected; There is also a small list of headers that can't be caught by the rules above, 15 in total. These headers and corresponding guard values are listed in the `%OVERRIDES` hash table. Signed-off-by: Eduard Zingerman --- scripts/infer_header_guards.pl | 191 +++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100755 scripts/infer_header_guards.pl diff --git a/scripts/infer_header_guards.pl b/scripts/infer_header_guards.pl new file mode 100755 index 000000000000..201008ac83f3 --- /dev/null +++ b/scripts/infer_header_guards.pl @@ -0,0 +1,191 @@ +#!/usr/bin/env perl +# SPDX-License-Identifier: GPL-2.0 + +# This script scans the passed directory for header files (files ending with ".h"). +# For each header file it tries to infer the name of a C pre-processor +# variable used as a double include guard (dubbed as "header guard"). +# For example: +# +# #ifdef __MY_HEADER__ // <-- "header guard" +# ... +# #endif +# +# The inferred guards are printed to stdout in the following format: +# +# +# +# This is an expected format for pahole --header_guards_db parameter. +# Intended usage is to infer header guards for Linux UAPI headers. +# The collected information is further used in BTF embedded into kernel. +# +# The following inference logic is used for each file: +# - find all pairs `#ifndef #define ` +# - if there is a unique that matches a pattern - use this +# as a header guard, (see subroutine `select_guard` for the +# list of the patterns); +# - files containing only #include directives are safe to ignore. +# +# There are a few UAPI header files that don't fit in such logic, +# header guards for these files are hard-coded in %OVERRIDES hash. +# +# The script reports inference only when --report-failures flag is +# passed. This flag is intended for BPF tests. +# +# See subroutine `help` for usage info. + +use strict; +use warnings; +use File::Basename; +use File::Find; +use Getopt::Long; + +sub help { + my $message = << "EOM"; +Usage: + $0 [--report-failures] directory-or-file... + $0 --help + +For a specific file or for each .h file in a directory infer the name +of a C pre-processor variable used as a double include guard. + +Options: + --report-failures Report inference errors to stderr, + exit with non-zero code if guards were not inferred + for some files. + --help Print this message and exit. +EOM + print $message; +} + +my %OVERRIDES = ( + # Header guards that don't follow common naming rules + "include/uapi/linux/cciss_ioctl.h" => "_UAPICCISS_IOCTLH", + "include/uapi/linux/hpet.h" => "_UAPI__HPET__", + "include/uapi/linux/if_ppp.h" => "_PPP_IOCTL_H", + "include/uapi/linux/netfilter/xt_NFLOG.h" => "_XT_NFLOG_TARGET", + "include/uapi/linux/netfilter_ipv6/ip6t_NPT.h" => "__NETFILTER_IP6T_NPT", + "include/uapi/linux/quota.h" => "_UAPI_LINUX_QUOTA_", + "include/uapi/linux/v4l2-common.h" => "__V4L2_COMMON__", + # Headers that should be ignored + "arch/x86/include/uapi/asm/hw_breakpoint.h" => undef, + "arch/x86/include/uapi/asm/posix_types.h" => undef, + "arch/x86/include/uapi/asm/setup.h" => undef, + "include/generated/uapi/linux/version.h" => undef, + "include/uapi/asm-generic/bitsperlong.h" => undef, + "include/uapi/asm-generic/kvm_para.h" => undef, + "include/uapi/asm-generic/unistd.h" => undef, + "include/uapi/linux/irqnr.h" => undef, + "include/uapi/linux/zorro_ids.h" => undef, + ); + +sub get_basename { + my ($filename) = @_; + my $basename = fileparse($filename, qr/\.[^.]*/); + return $basename; +} + +sub find_bracket_candidates { + my ($filename) = @_; + my @candidates = (); + my $guard_candidate = undef; + my $safe_to_ignore = 1; + + open my $file, $filename or die "Can't open file $filename: $!"; + while (my $line = <$file>) { + if (not($line =~ "^#include")) { + $safe_to_ignore = 0; + } + if ($line =~ "^#ifndef[ \t]+([a-zA-Z0-9_]+)") { + $guard_candidate = $1; + } elsif ($guard_candidate && $line =~ "^#define[ \t]+${guard_candidate}") { + push(@candidates, $guard_candidate); + $guard_candidate = undef; + } + } + close $file; + + return ($safe_to_ignore, @candidates); +} + +sub select_guard { + my ($filename, @candidates) = @_; + my $basename = get_basename($filename); + my @regexes = ("$basename.*_H(EADER)?", + "_H(EADER)?_", + "_H(EADER)?\$"); + foreach my $re (@regexes) { + my @filtered = grep(/$re/i, @candidates); + if (scalar(@filtered) == 1) { + return $filtered[0]; + } + } + + return undef; +} + +sub collect_headers { + my ($dir) = @_; + my @headers = (); + + find(sub { /\.h$/ && push(@headers, $File::Find::name); }, $dir); + + return @headers; +} + +my $report_failures = 0; +my $options_parsed = GetOptions( + "report-failures" => \$report_failures, + "help" => sub { help(); exit 0; }, + ); + +if (!$options_parsed || scalar @ARGV == 0) { + help(); + exit 1; +} + +my @headers; + +foreach my $dir_or_file (@ARGV) { + if (-f $dir_or_file) { + push(@headers, $dir_or_file); + } elsif (-d $dir_or_file) { + push(@headers, collect_headers($dir_or_file)); + } else { + print("'$dir_or_file' is not a file or directory.\n"); + help(); + exit 1; + } +} + +my $rc = 0; + +foreach my $header (@headers) { + my $basename = get_basename($header); + my $guard; + + if (exists $OVERRIDES{$header}) { + $guard = $OVERRIDES{$basename}; + } else { + my ($safe_to_ignore, @candidates) = find_bracket_candidates($header); + $guard = select_guard($header, @candidates); + if ((not $guard) && (not $safe_to_ignore) && $report_failures) { + print STDERR "Can't select guard for $header, candidates:\n"; + print STDERR " "; + if (scalar(@candidates)) { + print STDERR join(", ", @candidates); + } else { + print STDERR "" + } + print STDERR "\n"; + $rc = 1; + } + } + if ($guard) { + # Remove the _UAPI prefix/suffix the same way + # scripts/headers_install.sh does it. + $guard =~ s/_UAPI//; + print("$header $guard\n"); + } +} + +exit $rc; From patchwork Tue Oct 25 22:27:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019958 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76BCDFA3740 for ; Tue, 25 Oct 2022 22:28:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232741AbiJYW2x (ORCPT ); Tue, 25 Oct 2022 18:28:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232280AbiJYW2v (ORCPT ); Tue, 25 Oct 2022 18:28:51 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71F4B76977 for ; Tue, 25 Oct 2022 15:28:49 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id q9so16695479ejd.0 for ; Tue, 25 Oct 2022 15:28:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=d3kHPOaGpPAJ0oeh2OhuYbH6g3Kz5mjKU8RpDZhAE6o=; b=gqfBc+LzJh01HvJnsphcv8sQ/dWjW56uFFkx4/2R2x6DwB7eMsLMCNnRHCiazCK6sI y/bN7SeHux+Sg5YVzmUbJZnAqW9kiy0TKv7Cehsmd2o6tXv/XFrXuJ1yFA5B9a4KB96Z y8L8KvViMjt1XQZLyivgdsTZpVpFi6gXjQ0kgHShUAdirYdAcygAvCvtqr9Khax3qJvo 2fSaOOg4pd0mpgsAUZf5kcdfLxHXycVvQWaUoICjz8Xi0CHYQ0Oh391XxTjPcBOpSRS+ cqBRwCn8lYBLzyhUPJRgx3QdG6WNkfdUCjnSCCB2rMypJ7FUehZP7zVCsuIiB6SAyXU+ +i2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d3kHPOaGpPAJ0oeh2OhuYbH6g3Kz5mjKU8RpDZhAE6o=; b=K/SPwm42RSXmTuNyoRNU4EP7bZb6gnqtpjbNVUDZFTKdj2hSDlvG3Wao/lKU9l5aVk uZJPQE7GS6/Ci8Pqney11haeIAwy9Y2F+wRKqf5dlUK+4spb45NdkjtGmLoqxf7cB7QY j9zEQFyCHvk0hJA4oD0oaZWyAsJSLQnU2PaUXTD1CmgT/QQEM97Hp5KVDQhUrXq0Vkab Ro/rAG0BHrcUo+/po2u/TiXyQTkf5xCMO4LyvuSmzUkvip1jYPRA1YxdOfIeyCi2x+d/ Su7wrSsTYU9Nqyxw5jw291txncCSO5L3/7AR8MR0EEyy608QFRh2ZqZ/wI0Z+4keM8Js 4WZw== X-Gm-Message-State: ACrzQf3YDO7LO1F14/gmyjwBiIwEFZO9+nhK98GspSrwwZb5EIvkDMQf /oCPZAAsEtQP1qflambjvzhI/4H2AX6ukv5b X-Google-Smtp-Source: AMsMyM49phYRFztQVzP3oK3I7YnfH0F7CTkJQfG//ET1r7UAGKGSYVJ1onoLcfAQpkuokFRy0jngig== X-Received: by 2002:a17:906:eec1:b0:782:6384:76be with SMTP id wu1-20020a170906eec100b00782638476bemr34661780ejb.756.1666736927871; Tue, 25 Oct 2022 15:28:47 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:47 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 09/12] kbuild: Header guards for types from include/uapi/*.h in kernel BTF Date: Wed, 26 Oct 2022 01:27:58 +0300 Message-Id: <20221025222802.2295103-10-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Use pahole --header_guards_db flag to enable encoding of header guard information in kernel BTF. The actual correspondence between header file and guard string is computed by the scripts/infer_header_guards.pl. The encoded header guard information could be used to restore the original guards in the vmlinux.h, e.g.: include/uapi/linux/tcp.h: #ifndef _UAPI_LINUX_TCP_H #define _UAPI_LINUX_TCP_H ... union tcp_word_hdr { struct tcphdr hdr; __be32 words[5]; }; ... #endif /* _UAPI_LINUX_TCP_H */ vmlinux.h: ... #ifndef _UAPI_LINUX_TCP_H union tcp_word_hdr { struct tcphdr hdr; __be32 words[5]; }; #endif /* _UAPI_LINUX_TCP_H */ ... Signed-off-by: Eduard Zingerman --- scripts/link-vmlinux.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 918470d768e9..f57f621eda1f 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -110,6 +110,7 @@ vmlinux_link() gen_btf() { local pahole_ver + local extra_flags if ! [ -x "$(command -v ${PAHOLE})" ]; then echo >&2 "BTF: ${1}: pahole (${PAHOLE}) is not available" @@ -122,10 +123,20 @@ gen_btf() return 1 fi + if [ "${pahole_ver}" -ge "124" ]; then + scripts/infer_header_guards.pl \ + include/uapi \ + include/generated/uapi \ + arch/${SRCARCH}/include/uapi \ + arch/${SRCARCH}/include/generated/uapi \ + > .btf.uapi_header_guards || return 1; + extra_flags="--header_guards_db .btf.uapi_header_guards" + fi + vmlinux_link ${1} info "BTF" ${2} - LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${1} + LLVM_OBJCOPY="${OBJCOPY}" ${PAHOLE} -J ${PAHOLE_FLAGS} ${extra_flags} ${1} # Create ${2} which contains just .BTF section but no symbols. Add # SHF_ALLOC because .BTF will be part of the vmlinux image. --strip-all From patchwork Tue Oct 25 22:27:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019959 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD5A2ECDFA1 for ; Tue, 25 Oct 2022 22:28:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230327AbiJYW2y (ORCPT ); Tue, 25 Oct 2022 18:28:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232731AbiJYW2v (ORCPT ); Tue, 25 Oct 2022 18:28:51 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 849679E0F4 for ; Tue, 25 Oct 2022 15:28:50 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id 13so16566922ejn.3 for ; Tue, 25 Oct 2022 15:28:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=KJcaGBfkjdWGPkwuuaxd3CxPiCHKa/iDfVgs/xqPdsY=; b=qfl1ZDtY8efvhv0piwxS+sZm69wrJtJRmY+cI6x/zsjtkjOPolKnUO2w/RN6PsqxWB dfu52r22Y5L4vXE1vjQyttWRBPBaCr8DKfRRp0/0+OelXClWcI+so6qGUzRYgWX+SLV1 H6fOddqWYPak8TXpY7x7wOFX8WaqhPnmlX0hiffzZV0bgOdU2eXPV/3q0wBAghB/HIQQ VWVguxm7LSiySakXgk3Kwt8PEELhHgRQl8SEkwScGJIn7axKi4DoLLOa4torslSLIYBX pXB3oxVyb5rjFA2eI3VrMV7tFOFKi+dU+Xs4jSJUmtcHb/QFD9TyyUiQIIfGEb1lGUk7 ZXeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KJcaGBfkjdWGPkwuuaxd3CxPiCHKa/iDfVgs/xqPdsY=; b=pBKlQkkgdBQle73N6pXAkXZoCmKZXL5RO4L7OUW7+xQhXldOCAO6MuMuNxcj5zycfB gLV5COn7XIohRu2sfr9WBivaI+bU8lflSVsTum5mD7iP4qctMQ89rOrSSofK35W8Jqpu nxHiplOZHt4IVnwuyJI7Zc9gN4iBXYdg2k6mlqTeyws4kaYiMR/pQ9klRD+G/GH2wb4G oQTLVXD1HpiVmYMSI4obebOUTrV1M+VENtkklnDAywU/YLC6pm5zAiyX+2LSbPTDrvLD egyVdJ/8lGxtT2kQBQPBsYkjWnIV4HN5Lm7GHcGJ8oK33YYFPXeQX8GMzkjHQ/rkLCEv Z7zQ== X-Gm-Message-State: ACrzQf0S2rioqKG5Q6a08iOymEviGQEM3g44TzVYpxVyh7iDs/lxiUcn D9P45hT345KU9ou1IRHdRwl5kE7NhVC/Ltr6 X-Google-Smtp-Source: AMsMyM6BrEPcTDDS3VpM3LvnEKW9wkNcotNQobdJ3DqDIUHp+CqjNEM1oT8sDoFRh0I44xA+F2vS/w== X-Received: by 2002:a17:907:c27:b0:791:81f2:f2b1 with SMTP id ga39-20020a1709070c2700b0079181f2f2b1mr34843934ejc.436.1666736928891; Tue, 25 Oct 2022 15:28:48 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:48 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 10/12] selftests/bpf: Script to verify uapi headers usage with vmlinux.h Date: Wed, 26 Oct 2022 01:27:59 +0300 Message-Id: <20221025222802.2295103-11-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC A script to test header guards in vmlinux.h by compiling a simple C snippet for a set of selected UAPI headers. The snippet being compiled looks as follows: #include #include "vmlinux.h" __attribute__((section("tc"), used)) int syncookie_tc(struct __sk_buff *skb) { return 0; } If header guards are placed correctly in vmlinux.h the snippet should compile w/o errors. The list of known good headers is supposed to be located in `tools/testing/selftests/bpf/good_uapi_headers.txt` added as a separate commit. Signed-off-by: Eduard Zingerman --- .../selftests/bpf/test_uapi_headers.py | 197 ++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100755 tools/testing/selftests/bpf/test_uapi_headers.py diff --git a/tools/testing/selftests/bpf/test_uapi_headers.py b/tools/testing/selftests/bpf/test_uapi_headers.py new file mode 100755 index 000000000000..1740c4fe0625 --- /dev/null +++ b/tools/testing/selftests/bpf/test_uapi_headers.py @@ -0,0 +1,197 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) + +# A script to test header guards in vmlinux.h by compiling a simple C +# snippet for a set of selected UAPI headers. The snippet being +# compiled looks as follows: +# +# #include +# #include "vmlinux.h" +# +# __attribute__((section("tc"), used)) +# int syncookie_tc(struct __sk_buff *skb) { return 0; } +# +# If header guards are placed correctly in vmlinux.h the snippet +# should compile w/o errors. +# +# The script could be used in two modes: +# - interactive BPF testing and CI; +# - debug mode. +# +# * Interactive BPF testing and CI +# +# Run script as follows: +# +# ./test_uapi_headers.py +# +# In this mode the following actions are performed: +# - kernel headers are installed to a temporary directory; +# - a list of known good uapi headers is read from ./good_uapi_headers.txt; +# - the snippet above is compiled by clang using BPF target for each header; +# - if shell is interactive the progress / ETA are reported during execution; +# - pass / fail statistics is reported in the end; +# - headers temporary directory is deleted; +# - script exit code is 0 if snippet could be compiled for all headers. +# +# The vmlinux.h processing time is significant (~700ms using Intel i7-4710HQ), +# thus the headers are processed in parallel. +# +# * Debug mode +# +# The following parameters are available for debugging: +# +# test_uapi_headers.py \ +# [-h] [--kheaders KHEADERS] [--vmlinuxh VMLINUXH] [--test TEST] +# +# options: +# -h, --help show this help message and exit +# --kheaders KHEADERS path to exported kernel headers +# --vmlinuxh VMLINUXH path to vmlinux.h +# --test TEST name of the header -or- +# file with header names -or- +# special value '*' +# +# When --kheaders is specified the temporary directory is not created +# and KHEADERS is used instead. It is assumed that headers are already +# installed to KHEADERS. +# +# When TEST names a header (e.g. 'linux/tcp.h') it is the to test. +# When TEST names a file this file should contain a list of +# headers to test one per line. +# When TEST is '*' all exported headers are tested. +# +# The simplest way to debug an issue with a single header is: +# +# ./test_uapi_headers.py --test linux/tcp.h + +import subprocess +import concurrent.futures +import pathlib +import time +import os +import sys +import argparse +import tempfile +import shutil +import atexit +from dataclasses import dataclass + +@dataclass +class Result: + header: pathlib.Path + returncode: int + stderr: str + +def run_one(header, kheaders, vmlinuxh): + code=f''' +#include <{header}> +#include "{vmlinuxh}" + +__attribute__((section("tc"), used)) +int syncookie_tc(struct __sk_buff *skb) +{{ + return 0; +}} + ''' + command = f''' +{os.getenv('CLANG', 'clang')} \ + -g -Werror -mlittle-endian \ + -D__x86_64__ \ + -Xclang -fwchar-type=short \ + -Xclang -fno-signed-wchar \ + -I{kheaders}/include/ \ + -Wno-compare-distinct-pointer-types \ + -mcpu=v3 \ + -O2 \ + -target bpf \ + -x c \ + -o /dev/null \ + -fsyntax-only \ + - +''' + proc = subprocess.run(command, input=code, capture_output=True, + shell=True, encoding='utf8') + return Result(header=header, + returncode=proc.returncode, + stderr=proc.stderr) + +def run_all(headers, kheaders, vmlinuxh): + start_time = time.time() + ok = 0 + fail = 0 + failures = [] + remain = len(headers) + print_progress = sys.stdout.isatty() + print(f'Processing {remain} headers.') + with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor: + for result in executor.map(lambda header: run_one(header, kheaders, vmlinuxh), + headers): + if result.returncode == 0: + print(f"{result.header:<60} ok") + ok += 1 + else: + print(f"{result.header:<60} fail") + fail += 1 + failures.append(result) + remain -= 1 + if print_progress: + elapsed = time.time() - start_time + processed = ok + fail + time_per_header = elapsed / processed + eta = int(remain * time_per_header) + # keep this shorter than header ok/fail line + line = f"Ok {ok: >4} Fail {fail: >4} Remain {remain: >4} ETA {eta: >4}s" + print(line, end="\r") + if print_progress: + print('') + elapsed = int(time.time() - start_time) + if fail == 0: + print(f"Done in {elapsed}s, all {len(headers)} ok.") + else: + print('----- Failure details -----') + for result in failures: + print(f'{result.header}: rc = {result.returncode}') + for line in result.stderr.split('\n'): + print(f"{result.header}: {line}") + print(f"Done in {elapsed}s, {fail} out of {len(headers)} failed.") + return fail == 0 + +def main(argv): + bpf_test_dir = pathlib.Path(__file__).resolve().parent + default_vmlinuxh = bpf_test_dir / './tools/include/vmlinux.h' + parser = argparse.ArgumentParser() + parser.add_argument("--kheaders", type=str, help='path to exported kernel headers') + parser.add_argument("--vmlinuxh", type=str, default=default_vmlinuxh, + help='path to vmlinux.h') + parser.add_argument("--test", type=str, + default='./good_uapi_headers.txt', + help="name of the header | file with header names | special value '*'") + args = parser.parse_args(argv) + + if args.kheaders is None: + kheaders = tempfile.mkdtemp(prefix='kheaders') + atexit.register(lambda: shutil.rmtree(kheaders)) + kernel_dir = bpf_test_dir / '../../../../' + # Capture both stdout and stderr as stdout to simplify CI logging + subprocess.run(f'make -C {kernel_dir} INSTALL_HDR_PATH={kheaders} headers_install', + stdout=sys.stdout, stderr=sys.stdout, + check=True, shell=True) + else: + kheaders = args.kheaders + + if os.path.exists(args.test): + with open(args.test, 'r') as list_file: + headers = [line.strip() for line in list_file] + elif args.test == '*': + headers = [p.relative_to(f'{kheaders}/include').as_posix() + for p in pathlib.Path(kheaders).rglob("*.h")] + else: + headers = [args.test] + + if run_all(headers, kheaders, args.vmlinuxh): + sys.exit(0) + else: + sys.exit(1) + +if __name__ == '__main__': + main(sys.argv[1:]) From patchwork Tue Oct 25 22:28:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019960 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB281C38A2D for ; Tue, 25 Oct 2022 22:28:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232681AbiJYW25 (ORCPT ); Tue, 25 Oct 2022 18:28:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232725AbiJYW2y (ORCPT ); Tue, 25 Oct 2022 18:28:54 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C0A6A3AA4 for ; Tue, 25 Oct 2022 15:28:50 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id fy4so16541089ejc.5 for ; Tue, 25 Oct 2022 15:28:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=08kZlFDgZD+VRJ8iQgCdtJCRxCKAdy9mN/ipbZWjkxo=; b=nQv5ouBG6Uqe2vEsqpMVJO7aCC2EXc8HYCsb7rYNGGexI1MI5vDyG0O0W6OePwH0sK amwqzIDeMwwMpKBYYBKvt5hdOGb8Xnmdagu34Z5CazKu8VVknw9UbfbIlrSLSUVhSfiD XeaXZ9DjPxRzwQiMukrcAekknmIWFxlh449CxAyrf9C6BzKi/m/lIr2FyI8XkvGcXWaZ 2e/SbYzjcgc18EY/MvwUJEHiB4y4hTDJo3U19+j0XFNBieagdEIKBsYxySi/KRmUf7H3 XFksAQCBRnef4uO4OwaEBZ17PaUnmKY3TfbJsZwbgLbq1hp+zbaf3jlgBNNpQ7vhtiiE Z7Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=08kZlFDgZD+VRJ8iQgCdtJCRxCKAdy9mN/ipbZWjkxo=; b=o+WAotA4KxFENTJijutFhcwlrdSE06qQFsT4WyWRO/31JNufIqKBsdLP41BAdJqwAm K7hG/yRS5l0xRgzRXLau8el0gUFbfPNRUKwINu+/iXtWmAQnuC7CoX7im3vei9RliD5P 0TK7mu4FNkCtkGbqLRnO9wc7hji8eus4L+UigIcMTsj8LN8ogZuOUa7PxsbajyDjr5Pi Slb0IsxOENkdBMA3requXG/gqdw3em8Odm8ICAdsZ9mNdv2HbNddUvO+iwBffRsmRxAv dBpTSmHl8pefFF/zwpuZyi0TnG7/hJ10gLf4xdZmSAyA5c6J3p4ssFewY59XuHH7g4wC LFQg== X-Gm-Message-State: ACrzQf3TXMXaRD6wkOdmkW3HK1e1fsm3EwHfk9aPuiyonQyYXFPxf9pH KekoMGP7HmAkOBPh4YNjjXinv1P5hacvzCY2 X-Google-Smtp-Source: AMsMyM5UGIE+tG5o3zJFyUKn3UeOjFmahlu9vwEMnv9lGziACW9PgQJ460QMo3xwfVUPpFhhKg55Bg== X-Received: by 2002:a17:907:a43:b0:77b:ba98:d2f with SMTP id be3-20020a1709070a4300b0077bba980d2fmr35031036ejc.270.1666736930048; Tue, 25 Oct 2022 15:28:50 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:49 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 11/12] selftests/bpf: Known good uapi headers for test_uapi_headers.py Date: Wed, 26 Oct 2022 01:28:00 +0300 Message-Id: <20221025222802.2295103-12-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC The list of uapi headers that could be included alongside vmlinux.h. The headers are peeked from the following locations: - /linux/*.h - /linux/**/*.h Signed-off-by: Eduard Zingerman --- .../selftests/bpf/good_uapi_headers.txt | 677 ++++++++++++++++++ 1 file changed, 677 insertions(+) create mode 100644 tools/testing/selftests/bpf/good_uapi_headers.txt diff --git a/tools/testing/selftests/bpf/good_uapi_headers.txt b/tools/testing/selftests/bpf/good_uapi_headers.txt new file mode 100644 index 000000000000..d2a7a6e3de74 --- /dev/null +++ b/tools/testing/selftests/bpf/good_uapi_headers.txt @@ -0,0 +1,677 @@ +linux/a.out.h +linux/acct.h +linux/acrn.h +linux/adb.h +linux/adfs_fs.h +linux/affs_hardblocks.h +linux/aio_abi.h +linux/amt.h +linux/android/binder.h +linux/android/binderfs.h +linux/apm_bios.h +linux/arcfb.h +linux/arm_sdei.h +linux/aspeed-lpc-ctrl.h +linux/aspeed-p2a-ctrl.h +linux/atalk.h +linux/atm.h +linux/atm_eni.h +linux/atm_he.h +linux/atm_idt77105.h +linux/atm_nicstar.h +linux/atm_tcp.h +linux/atm_zatm.h +linux/atmapi.h +linux/atmarp.h +linux/atmclip.h +linux/atmdev.h +linux/atmioc.h +linux/atmlec.h +linux/atmmpc.h +linux/atmppp.h +linux/atmsap.h +linux/atmsvc.h +linux/audit.h +linux/auxvec.h +linux/ax25.h +linux/batadv_packet.h +linux/batman_adv.h +linux/baycom.h +linux/bcm933xx_hcs.h +linux/bfs_fs.h +linux/binfmts.h +linux/blkpg.h +linux/blktrace_api.h +linux/blkzoned.h +linux/bpf.h +linux/bpf_common.h +linux/bpqether.h +linux/bsg.h +linux/bt-bmc.h +linux/btf.h +linux/btrfs.h +linux/btrfs_tree.h +linux/byteorder/big_endian.h +linux/byteorder/little_endian.h +linux/cachefiles.h +linux/caif/caif_socket.h +linux/caif/if_caif.h +linux/can.h +linux/can/bcm.h +linux/can/error.h +linux/can/gw.h +linux/can/isotp.h +linux/can/j1939.h +linux/can/netlink.h +linux/can/raw.h +linux/can/vxcan.h +linux/capability.h +linux/capi.h +linux/cciss_defs.h +linux/cciss_ioctl.h +linux/ccs.h +linux/cdrom.h +linux/cfm_bridge.h +linux/cgroupstats.h +linux/chio.h +linux/cifs/cifs_mount.h +linux/cifs/cifs_netlink.h +linux/close_range.h +linux/cm4000_cs.h +linux/cn_proc.h +linux/coff.h +linux/comedi.h +linux/connector.h +linux/const.h +linux/coresight-stm.h +linux/counter.h +linux/cramfs_fs.h +linux/cryptouser.h +linux/cuda.h +linux/cxl_mem.h +linux/cycx_cfm.h +linux/dcbnl.h +linux/dccp.h +linux/devlink.h +linux/dlm.h +linux/dlm_device.h +linux/dlm_netlink.h +linux/dlm_plock.h +linux/dlmconstants.h +linux/dm-ioctl.h +linux/dm-log-userspace.h +linux/dma-heap.h +linux/dns_resolver.h +linux/dqblk_xfs.h +linux/dvb/audio.h +linux/dvb/ca.h +linux/dvb/frontend.h +linux/dvb/net.h +linux/dvb/osd.h +linux/dvb/version.h +linux/dw100.h +linux/edd.h +linux/efs_fs_sb.h +linux/elf-em.h +linux/elf-fdpic.h +linux/elf.h +linux/errno.h +linux/erspan.h +linux/eventpoll.h +linux/f2fs.h +linux/fadvise.h +linux/falloc.h +linux/fanotify.h +linux/fb.h +linux/fcntl.h +linux/fd.h +linux/fdreg.h +linux/fib_rules.h +linux/fiemap.h +linux/filter.h +linux/firewire-cdev.h +linux/firewire-constants.h +linux/fou.h +linux/fpga-dfl.h +linux/fs.h +linux/fscrypt.h +linux/fsi.h +linux/fsl_hypervisor.h +linux/fsl_mc.h +linux/fsmap.h +linux/fsverity.h +linux/futex.h +linux/gameport.h +linux/gen_stats.h +linux/genetlink.h +linux/genwqe/genwqe_card.h +linux/gfs2_ondisk.h +linux/gpio.h +linux/gtp.h +linux/hash_info.h +linux/hdlc.h +linux/hdlcdrv.h +linux/hdreg.h +linux/hid.h +linux/hiddev.h +linux/hidraw.h +linux/hsi/cs-protocol.h +linux/hsi/hsi_char.h +linux/hsr_netlink.h +linux/hw_breakpoint.h +linux/hyperv.h +linux/i2c-dev.h +linux/i2c.h +linux/i2o-dev.h +linux/i8k.h +linux/icmpv6.h +linux/if_addr.h +linux/if_addrlabel.h +linux/if_alg.h +linux/if_arcnet.h +linux/if_bridge.h +linux/if_cablemodem.h +linux/if_eql.h +linux/if_ether.h +linux/if_fc.h +linux/if_fddi.h +linux/if_hippi.h +linux/if_infiniband.h +linux/if_link.h +linux/if_ltalk.h +linux/if_macsec.h +linux/if_packet.h +linux/if_phonet.h +linux/if_plip.h +linux/if_ppp.h +linux/if_pppol2tp.h +linux/if_slip.h +linux/if_team.h +linux/if_tun.h +linux/if_vlan.h +linux/if_x25.h +linux/if_xdp.h +linux/ife.h +linux/igmp.h +linux/iio/buffer.h +linux/iio/events.h +linux/iio/types.h +linux/ila.h +linux/in.h +linux/in6.h +linux/in_route.h +linux/inet_diag.h +linux/inotify.h +linux/input-event-codes.h +linux/io_uring.h +linux/ioam6.h +linux/ioam6_genl.h +linux/ioam6_iptunnel.h +linux/ioctl.h +linux/iommu.h +linux/ioprio.h +linux/ip.h +linux/ip_vs.h +linux/ipc.h +linux/ipmi.h +linux/ipmi_bmc.h +linux/ipmi_msgdefs.h +linux/ipsec.h +linux/ipv6.h +linux/ipv6_route.h +linux/irqnr.h +linux/isdn/capicmd.h +linux/iso_fs.h +linux/isst_if.h +linux/ivtvfb.h +linux/jffs2.h +linux/kcm.h +linux/kcmp.h +linux/kcov.h +linux/kd.h +linux/kdev_t.h +linux/kernel-page-flags.h +linux/kernel.h +linux/kernelcapi.h +linux/keyboard.h +linux/keyctl.h +linux/kfd_sysfs.h +linux/kvm_para.h +linux/l2tp.h +linux/landlock.h +linux/libc-compat.h +linux/limits.h +linux/lirc.h +linux/loadpin.h +linux/loop.h +linux/lp.h +linux/lwtunnel.h +linux/magic.h +linux/major.h +linux/map_to_14segment.h +linux/map_to_7segment.h +linux/max2175.h +linux/media-bus-format.h +linux/media.h +linux/mei.h +linux/membarrier.h +linux/memfd.h +linux/mempolicy.h +linux/meye.h +linux/minix_fs.h +linux/misc/bcm_vk.h +linux/mman.h +linux/mmc/ioctl.h +linux/mmtimer.h +linux/module.h +linux/mount.h +linux/mpls.h +linux/mpls_iptunnel.h +linux/mqueue.h +linux/mroute.h +linux/mroute6.h +linux/mrp_bridge.h +linux/msdos_fs.h +linux/msg.h +linux/mtio.h +linux/nbd-netlink.h +linux/nbd.h +linux/ncsi.h +linux/ndctl.h +linux/neighbour.h +linux/net.h +linux/net_dropmon.h +linux/net_namespace.h +linux/net_tstamp.h +linux/netconf.h +linux/netfilter.h +linux/netfilter/ipset/ip_set.h +linux/netfilter/ipset/ip_set_bitmap.h +linux/netfilter/ipset/ip_set_hash.h +linux/netfilter/ipset/ip_set_list.h +linux/netfilter/nf_conntrack_common.h +linux/netfilter/nf_conntrack_ftp.h +linux/netfilter/nf_conntrack_sctp.h +linux/netfilter/nf_conntrack_tcp.h +linux/netfilter/nf_conntrack_tuple_common.h +linux/netfilter/nf_log.h +linux/netfilter/nf_nat.h +linux/netfilter/nf_synproxy.h +linux/netfilter/nf_tables.h +linux/netfilter/nf_tables_compat.h +linux/netfilter/nfnetlink.h +linux/netfilter/nfnetlink_acct.h +linux/netfilter/nfnetlink_compat.h +linux/netfilter/nfnetlink_conntrack.h +linux/netfilter/nfnetlink_cthelper.h +linux/netfilter/nfnetlink_cttimeout.h +linux/netfilter/nfnetlink_hook.h +linux/netfilter/nfnetlink_log.h +linux/netfilter/nfnetlink_osf.h +linux/netfilter/nfnetlink_queue.h +linux/netfilter/x_tables.h +linux/netfilter/xt_AUDIT.h +linux/netfilter/xt_CHECKSUM.h +linux/netfilter/xt_CLASSIFY.h +linux/netfilter/xt_CONNMARK.h +linux/netfilter/xt_CONNSECMARK.h +linux/netfilter/xt_CT.h +linux/netfilter/xt_DSCP.h +linux/netfilter/xt_HMARK.h +linux/netfilter/xt_IDLETIMER.h +linux/netfilter/xt_LED.h +linux/netfilter/xt_LOG.h +linux/netfilter/xt_MARK.h +linux/netfilter/xt_NFQUEUE.h +linux/netfilter/xt_SECMARK.h +linux/netfilter/xt_SYNPROXY.h +linux/netfilter/xt_TCPMSS.h +linux/netfilter/xt_TCPOPTSTRIP.h +linux/netfilter/xt_TEE.h +linux/netfilter/xt_TPROXY.h +linux/netfilter/xt_addrtype.h +linux/netfilter/xt_bpf.h +linux/netfilter/xt_cgroup.h +linux/netfilter/xt_cluster.h +linux/netfilter/xt_comment.h +linux/netfilter/xt_connbytes.h +linux/netfilter/xt_connlabel.h +linux/netfilter/xt_connlimit.h +linux/netfilter/xt_connmark.h +linux/netfilter/xt_conntrack.h +linux/netfilter/xt_cpu.h +linux/netfilter/xt_dccp.h +linux/netfilter/xt_devgroup.h +linux/netfilter/xt_dscp.h +linux/netfilter/xt_ecn.h +linux/netfilter/xt_esp.h +linux/netfilter/xt_helper.h +linux/netfilter/xt_ipcomp.h +linux/netfilter/xt_iprange.h +linux/netfilter/xt_ipvs.h +linux/netfilter/xt_l2tp.h +linux/netfilter/xt_length.h +linux/netfilter/xt_limit.h +linux/netfilter/xt_mac.h +linux/netfilter/xt_mark.h +linux/netfilter/xt_multiport.h +linux/netfilter/xt_nfacct.h +linux/netfilter/xt_osf.h +linux/netfilter/xt_owner.h +linux/netfilter/xt_pkttype.h +linux/netfilter/xt_policy.h +linux/netfilter/xt_quota.h +linux/netfilter/xt_realm.h +linux/netfilter/xt_recent.h +linux/netfilter/xt_rpfilter.h +linux/netfilter/xt_sctp.h +linux/netfilter/xt_set.h +linux/netfilter/xt_socket.h +linux/netfilter/xt_state.h +linux/netfilter/xt_statistic.h +linux/netfilter/xt_string.h +linux/netfilter/xt_tcpmss.h +linux/netfilter/xt_tcpudp.h +linux/netfilter/xt_time.h +linux/netfilter/xt_u32.h +linux/netfilter_arp.h +linux/netfilter_bridge/ebt_among.h +linux/netfilter_bridge/ebt_arp.h +linux/netfilter_bridge/ebt_arpreply.h +linux/netfilter_bridge/ebt_ip.h +linux/netfilter_bridge/ebt_ip6.h +linux/netfilter_bridge/ebt_limit.h +linux/netfilter_bridge/ebt_log.h +linux/netfilter_bridge/ebt_mark_m.h +linux/netfilter_bridge/ebt_mark_t.h +linux/netfilter_bridge/ebt_nat.h +linux/netfilter_bridge/ebt_nflog.h +linux/netfilter_bridge/ebt_pkttype.h +linux/netfilter_bridge/ebt_redirect.h +linux/netfilter_bridge/ebt_stp.h +linux/netfilter_bridge/ebt_vlan.h +linux/netfilter_ipv4.h +linux/netfilter_ipv4/ipt_CLUSTERIP.h +linux/netfilter_ipv4/ipt_ECN.h +linux/netfilter_ipv4/ipt_LOG.h +linux/netfilter_ipv4/ipt_REJECT.h +linux/netfilter_ipv4/ipt_TTL.h +linux/netfilter_ipv4/ipt_ah.h +linux/netfilter_ipv4/ipt_ecn.h +linux/netfilter_ipv4/ipt_ttl.h +linux/netfilter_ipv6.h +linux/netfilter_ipv6/ip6t_HL.h +linux/netfilter_ipv6/ip6t_LOG.h +linux/netfilter_ipv6/ip6t_NPT.h +linux/netfilter_ipv6/ip6t_REJECT.h +linux/netfilter_ipv6/ip6t_ah.h +linux/netfilter_ipv6/ip6t_frag.h +linux/netfilter_ipv6/ip6t_hl.h +linux/netfilter_ipv6/ip6t_ipv6header.h +linux/netfilter_ipv6/ip6t_mh.h +linux/netfilter_ipv6/ip6t_opts.h +linux/netfilter_ipv6/ip6t_rt.h +linux/netfilter_ipv6/ip6t_srh.h +linux/netlink.h +linux/netlink_diag.h +linux/netrom.h +linux/nexthop.h +linux/nfc.h +linux/nfs.h +linux/nfs2.h +linux/nfs3.h +linux/nfs4.h +linux/nfs4_mount.h +linux/nfs_fs.h +linux/nfs_idmap.h +linux/nfs_mount.h +linux/nfsacl.h +linux/nfsd/cld.h +linux/nfsd/debug.h +linux/nfsd/export.h +linux/nfsd/stats.h +linux/nilfs2_api.h +linux/nilfs2_ondisk.h +linux/nitro_enclaves.h +linux/nl80211-vnd-intel.h +linux/nl80211.h +linux/nsfs.h +linux/nubus.h +linux/nvme_ioctl.h +linux/nvram.h +linux/oom.h +linux/openat2.h +linux/openvswitch.h +linux/packet_diag.h +linux/param.h +linux/parport.h +linux/pci.h +linux/pci_regs.h +linux/pcitest.h +linux/perf_event.h +linux/personality.h +linux/pfkeyv2.h +linux/pfrut.h +linux/pg.h +linux/phantom.h +linux/pidfd.h +linux/pkt_cls.h +linux/pkt_sched.h +linux/pktcdvd.h +linux/pmu.h +linux/poll.h +linux/posix_acl.h +linux/posix_types.h +linux/ppdev.h +linux/ppp-comp.h +linux/ppp-ioctl.h +linux/ppp_defs.h +linux/pps.h +linux/pr.h +linux/prctl.h +linux/psample.h +linux/psci.h +linux/psp-sev.h +linux/ptp_clock.h +linux/qemu_fw_cfg.h +linux/qnx4_fs.h +linux/qnxtypes.h +linux/qrtr.h +linux/radeonfb.h +linux/raid/md_p.h +linux/raid/md_u.h +linux/random.h +linux/rds.h +linux/reboot.h +linux/reiserfs_fs.h +linux/reiserfs_xattr.h +linux/remoteproc_cdev.h +linux/resource.h +linux/rfkill.h +linux/rio_cm_cdev.h +linux/rio_mport_cdev.h +linux/rkisp1-config.h +linux/romfs_fs.h +linux/rose.h +linux/rpl.h +linux/rpl_iptunnel.h +linux/rpmsg.h +linux/rpmsg_types.h +linux/rseq.h +linux/rtc.h +linux/rtnetlink.h +linux/rxrpc.h +linux/scc.h +linux/sched.h +linux/sched/types.h +linux/scif_ioctl.h +linux/screen_info.h +linux/seccomp.h +linux/securebits.h +linux/sed-opal.h +linux/seg6.h +linux/seg6_genl.h +linux/seg6_hmac.h +linux/seg6_iptunnel.h +linux/seg6_local.h +linux/selinux_netlink.h +linux/sem.h +linux/serial_reg.h +linux/serio.h +linux/sev-guest.h +linux/shm.h +linux/signalfd.h +linux/smc.h +linux/smc_diag.h +linux/smiapp.h +linux/snmp.h +linux/socket.h +linux/sockios.h +linux/sonet.h +linux/sonypi.h +linux/sound.h +linux/soundcard.h +linux/spi/spi.h +linux/spi/spidev.h +linux/stat.h +linux/stddef.h +linux/stm.h +linux/string.h +linux/sunrpc/debug.h +linux/surface_aggregator/cdev.h +linux/surface_aggregator/dtx.h +linux/suspend_ioctls.h +linux/swab.h +linux/switchtec_ioctl.h +linux/sync_file.h +linux/sysinfo.h +linux/target_core_user.h +linux/taskstats.h +linux/tc_act/tc_bpf.h +linux/tc_act/tc_csum.h +linux/tc_act/tc_ct.h +linux/tc_act/tc_defact.h +linux/tc_act/tc_gact.h +linux/tc_act/tc_gate.h +linux/tc_act/tc_ife.h +linux/tc_act/tc_ipt.h +linux/tc_act/tc_mirred.h +linux/tc_act/tc_mpls.h +linux/tc_act/tc_nat.h +linux/tc_act/tc_pedit.h +linux/tc_act/tc_sample.h +linux/tc_act/tc_skbedit.h +linux/tc_act/tc_skbmod.h +linux/tc_act/tc_tunnel_key.h +linux/tc_act/tc_vlan.h +linux/tc_ematch/tc_em_cmp.h +linux/tc_ematch/tc_em_ipt.h +linux/tc_ematch/tc_em_meta.h +linux/tc_ematch/tc_em_nbyte.h +linux/tc_ematch/tc_em_text.h +linux/tcp.h +linux/tcp_metrics.h +linux/tee.h +linux/termios.h +linux/thermal.h +linux/time.h +linux/time_types.h +linux/timerfd.h +linux/times.h +linux/timex.h +linux/tiocl.h +linux/tipc.h +linux/tipc_config.h +linux/tipc_netlink.h +linux/tls.h +linux/toshiba.h +linux/tty.h +linux/tty_flags.h +linux/types.h +linux/ublk_cmd.h +linux/udf_fs_i.h +linux/udmabuf.h +linux/udp.h +linux/uio.h +linux/uleds.h +linux/ultrasound.h +linux/um_timetravel.h +linux/un.h +linux/unistd.h +linux/unix_diag.h +linux/usb/cdc-wdm.h +linux/usb/ch11.h +linux/usb/ch9.h +linux/usb/charger.h +linux/usb/functionfs.h +linux/usb/g_printer.h +linux/usb/g_uvc.h +linux/usb/gadgetfs.h +linux/usb/midi.h +linux/usb/raw_gadget.h +linux/usb/tmc.h +linux/usb/video.h +linux/usbdevice_fs.h +linux/usbip.h +linux/userfaultfd.h +linux/userio.h +linux/utime.h +linux/utsname.h +linux/uuid.h +linux/uvcvideo.h +linux/v4l2-common.h +linux/v4l2-controls.h +linux/v4l2-dv-timings.h +linux/vbox_err.h +linux/vbox_vmmdev_types.h +linux/vboxguest.h +linux/vdpa.h +linux/vduse.h +linux/veth.h +linux/vfio.h +linux/vfio_ccw.h +linux/vfio_zdev.h +linux/virtio_9p.h +linux/virtio_balloon.h +linux/virtio_blk.h +linux/virtio_bt.h +linux/virtio_config.h +linux/virtio_console.h +linux/virtio_crypto.h +linux/virtio_fs.h +linux/virtio_gpio.h +linux/virtio_gpu.h +linux/virtio_i2c.h +linux/virtio_ids.h +linux/virtio_input.h +linux/virtio_iommu.h +linux/virtio_mem.h +linux/virtio_mmio.h +linux/virtio_net.h +linux/virtio_pci.h +linux/virtio_pcidev.h +linux/virtio_pmem.h +linux/virtio_rng.h +linux/virtio_scmi.h +linux/virtio_scsi.h +linux/virtio_snd.h +linux/virtio_types.h +linux/virtio_vsock.h +linux/vm_sockets_diag.h +linux/vmcore.h +linux/vsockmon.h +linux/vt.h +linux/vtpm_proxy.h +linux/wait.h +linux/watch_queue.h +linux/watchdog.h +linux/wireguard.h +linux/wmi.h +linux/wwan.h +linux/x25.h +linux/xattr.h +linux/xdp_diag.h +linux/xfrm.h +linux/xilinx-v4l2-controls.h +linux/zorro.h +linux/zorro_ids.h From patchwork Tue Oct 25 22:28:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13019961 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85279C38A2D for ; Tue, 25 Oct 2022 22:29:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232678AbiJYW27 (ORCPT ); Tue, 25 Oct 2022 18:28:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232731AbiJYW24 (ORCPT ); Tue, 25 Oct 2022 18:28:56 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 204EF7D79F for ; Tue, 25 Oct 2022 15:28:52 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id x2so11349463edd.2 for ; Tue, 25 Oct 2022 15:28:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=guCllocMNmw80ZyG53CeyhOkNL1cE+oBzRCN8SKnxmQ=; b=Vpy7pm91Pv7q+ly8gpSnv6R4YYFk+HzGU9GMH2n300sYNkjxunYodJzaw9dnxs5Bmt 8v6AXGhq2KGsYkXDczNkFWmoT/3kvNqYnLzK30sE7hSGSx7To0ytQ7Y8wQAxwwOlVdKS BwS8Tz73i+59NIKb0gEAgrViQNkbwZalx9OAvAWwlgACIhebkR01C/tSfW5EnoX+4CTp CH8cgY5z8HxtMC8k52bh79angCdRWo/+UdOSDLXVSE3sJaVeC/7IPpVoOgrtXT+KKJd6 GBQUX8M48U/wtOiQA8zetOtjs835DfBfOXD+8QuuvxmwCB9eW0pSl9Vj+qdGv86tah3I UiFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=guCllocMNmw80ZyG53CeyhOkNL1cE+oBzRCN8SKnxmQ=; b=7Y1e1c/VfyZOQOY2fRX0Qh6q44Xcxo9Z6RUo73J0k1A+f6HcORxHSWiNAHD/2jpglU qb5J25zwf6w483ZDgPo/EvQ2rq81PtjDu0KfGVyGBRmoVxZmGhZ4XuEqE31YC+7fyCP5 yRf194iMq7OVurIIr6LlLNnb63JktG5QE1XO6WkLrceFx/Qj5sc8yHnEIlbnaht8oL42 pMd5lUu9oIL1zeNt7zgfkT97ZDLJi3fQK19GEd4oUA/aYC2xygGsjAFtdNIehCku5ZjY S8PeW9gRrSy3M4JVkQRquMCA4Ozhu4LL7yEU2qXyetIg2jDeRAM7HMwY9xolv2HV+WfN qklA== X-Gm-Message-State: ACrzQf25ZZAp2WhV5ObeLKMxQmW8owgF5QHkKFhJ+IN5gWTr/WjHWTaS tQYtt07KkzStPISGu/qB2Ps93mG3iGqI/jgi X-Google-Smtp-Source: AMsMyM5EKeSmZdER5rKxCyebm8bVn64TXd/nuStRKMIAAiT5Wpdl1V1HWwjddWVxddNhIeVB1zsEwQ== X-Received: by 2002:aa7:c61a:0:b0:461:c48d:effe with SMTP id h26-20020aa7c61a000000b00461c48deffemr11772170edq.7.1666736931110; Tue, 25 Oct 2022 15:28:51 -0700 (PDT) Received: from pluto.. (boundsly.muster.volia.net. [93.72.16.93]) by smtp.gmail.com with ESMTPSA id ks23-20020a170906f85700b0078d175d6dc5sm1993119ejb.201.2022.10.25.15.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 15:28:50 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com, arnaldo.melo@gmail.com, Eduard Zingerman Subject: [RFC bpf-next 12/12] selftests/bpf: script for infer_header_guards.pl testing Date: Wed, 26 Oct 2022 01:28:01 +0300 Message-Id: <20221025222802.2295103-13-eddyz87@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221025222802.2295103-1-eddyz87@gmail.com> References: <20221025222802.2295103-1-eddyz87@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This script verifies that patterns for header guard inference specified in scripts/infer_header_guards.pl cover all uapi headers. To achieve this the infer_header_guards.pl is invoked the same way it is invoked from link-vmlinux.sh but with --report-failures flag. Signed-off-by: Eduard Zingerman --- .../bpf/test_uapi_header_guards_infer.sh | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 tools/testing/selftests/bpf/test_uapi_header_guards_infer.sh diff --git a/tools/testing/selftests/bpf/test_uapi_header_guards_infer.sh b/tools/testing/selftests/bpf/test_uapi_header_guards_infer.sh new file mode 100755 index 000000000000..bd332db100f3 --- /dev/null +++ b/tools/testing/selftests/bpf/test_uapi_header_guards_infer.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) + +# This script verifies that patterns for header guard inference +# specified in scripts/infer_header_guards.pl cover all uapi headers. +# To achieve this the infer_header_guards.pl is invoked the same way +# it is invoked from link-vmlinux.sh but with --report-failures flag. + +kernel_dir=$(dirname $0)/../../../../ + +# The SRCARCH is defined in tools/scripts/Makefile.arch, thus use a +# temporary makefile to get access to this variable. +fake_makefile=$(cat </dev/null +EOF +) + +# The infer_header_guards.pl script prints inferred guards to stdout, +# redirecting stdout to /dev/null to see only error messages. +echo "$fake_makefile" | make -C $kernel_dir -f - 1>/dev/null +if [ "$?" == "0" ]; then + echo "all good" + exit 0 +fi + +# Failures are already reported by infer_header_guards.pl +exit 1