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 113C0C433EF for ; Wed, 1 Dec 2021 03:55:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241681AbhLAD6d (ORCPT ); Tue, 30 Nov 2021 22:58:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231927AbhLAD6X (ORCPT ); Tue, 30 Nov 2021 22:58:23 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 308DAC061746 for ; Tue, 30 Nov 2021 19:55:03 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id j11so12239563pgs.2 for ; Tue, 30 Nov 2021 19:55:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=YaeKFsX1KofNItzE7OLhurInohiFOUCQOEHbA1sAEqI=; b=3onNIi9mMlCHbRc8pIiWSdbZzQ8bSTlCWcl/1Pm4RpVXT+eR3gK6MiBNvoUmicZO2k 66rFF1/d+vKp/9a9Vlk3Sjav/WR+tm1daMeX8nbKKigFNW/VF3lmTmf1PgO5hZFgW5vB 6Lfq0dK3eB7FxfowyvXCFp23SEu5gwBo4DPV1gHEXuHU+TpEZlc4Sn0JN6ZzCP95al6+ KcWn/hIPpo6OlXDiLD4YCDdAXX8Wyl9y3ayLxEP+Ve4NBmIgr90t4jhU5ddtMwOeuRCC w9HNj4d7Bn8GaY1UN8YFOuChjhqYlyHHJDiJpS1iqCWoR9xzes6IN0xvbIUM536Y9P3/ JJ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=YaeKFsX1KofNItzE7OLhurInohiFOUCQOEHbA1sAEqI=; b=IrMb6wnRVaGcWhKMmIQbIqDYhrBRzPZ3fGaiSxKI3zQqRfdQW2kX8AupMwiNOzL55d HiwACr9Rg47hIcj/WD5NyowIyogX8Aww9hX0V9BvnNCIREtmKp65Ht45IwR6syiFQWfD tKc27Se1bkh/E9eoHnWVqwlWIC6zvY0Ykt+NwSbgTOXwklobYXnZzezMARTva2gZeMpU ePVUYnDsLWlb8nVojxftAvj2KU9340eU7/fgo6Tm946j+hY0+W3SF87pIb7oFONRRLi/ CI5eHybZpT5zzstJjI25UiTBu/UKUveZt0l/2DgibHCyjDvxZEh7g0OgaBi4C3XTuz+J gZ3w== X-Gm-Message-State: AOAM530r7/dWSNyTS2Qnimdoe5T5jRS8ozn0oBN5L1hSdwJveVRuzM3t JE4tLZgiT5SpF50mQycF9N9fvQ== X-Google-Smtp-Source: ABdhPJytno4BKM7xrhKQupoYZLnpjJGyxc7I60G48S15ovrjedr1oSfP6RAn6nfrdDL6eOljCajehg== X-Received: by 2002:a05:6a00:2387:b0:49f:af00:d5d0 with SMTP id f7-20020a056a00238700b0049faf00d5d0mr3721734pfc.1.1638330902605; Tue, 30 Nov 2021 19:55:02 -0800 (PST) Received: from C02F52LSML85.bytedance.net ([139.177.225.236]) by smtp.gmail.com with ESMTPSA id f3sm21679043pfg.167.2021.11.30.19.54.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Nov 2021 19:55:01 -0800 (PST) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next] libbpf: Let any two INT/UNION compatible if their names and sizes match Date: Wed, 1 Dec 2021 11:54:50 +0800 Message-Id: <20211201035450.31083-1-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Feng Zhou commit:67c0496e87d193b8356d2af49ab95e8a1b954b3c(kernfs: convert kernfs_node->id from union kernfs_node_id to u64). The bpf program compiles on the kernel version after this commit and then tries to run on the kernel before this commit, libbpf will report an error. The reverse is also same. libbpf: prog 'tcp_retransmit_synack_tp': relo #4: kind (0), spec is [342] struct kernfs_node.id (0:9 @ offset 104) libbpf: prog 'tcp_retransmit_synack_tp': relo #4: non-matching candidate libbpf: prog 'tcp_retransmit_synack_tp': relo #4: non-matching candidate libbpf: prog 'tcp_retransmit_synack_tp': relo #4: no matching targets found The type before this commit: union kernfs_node_id id; union kernfs_node_id { struct { u32 ino; u32 generation; }; u64 id; }; The type after this commit: u64 id; We can find that the variable name and size have not changed except for the type change. So I added some judgment to let any two INT/UNION are compatible, if their names and sizes match. Reported-by: Chengming Zhou Tested-by: Chengming Zhou Signed-off-by: Feng Zhou --- tools/lib/bpf/relo_core.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c index b5b8956a1be8..ff7f4e97bafb 100644 --- a/tools/lib/bpf/relo_core.c +++ b/tools/lib/bpf/relo_core.c @@ -294,6 +294,7 @@ static int bpf_core_parse_spec(const struct btf *btf, * - any two FLOATs are always compatible; * - for ARRAY, dimensionality is ignored, element types are checked for * compatibility recursively; + * - any two INT/UNION are compatible, if their names and sizes match; * - everything else shouldn't be ever a target of relocation. * These rules are not set in stone and probably will be adjusted as we get * more experience with using BPF CO-RE relocations. @@ -313,8 +314,14 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf, if (btf_is_composite(local_type) && btf_is_composite(targ_type)) return 1; - if (btf_kind(local_type) != btf_kind(targ_type)) - return 0; + if (btf_kind(local_type) != btf_kind(targ_type)) { + if (local_type->size == targ_type->size && + (btf_is_union(local_type) || btf_is_union(targ_type)) && + (btf_is_int(local_type) || btf_is_int(targ_type))) + return 1; + else + return 0; + } switch (btf_kind(local_type)) { case BTF_KIND_PTR: @@ -384,11 +391,17 @@ static int bpf_core_match_member(const struct btf *local_btf, targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id); if (!targ_type) return -EINVAL; - if (!btf_is_composite(targ_type)) - return 0; local_id = local_acc->type_id; local_type = btf__type_by_id(local_btf, local_id); + if (!btf_is_composite(targ_type)) { + if (local_type->size == targ_type->size && + btf_is_union(local_type) && btf_is_int(targ_type)) + return 1; + else + return 0; + } + local_member = btf_members(local_type) + local_acc->idx; local_name = btf__name_by_offset(local_btf, local_member->name_off); -- 2.11.0