From patchwork Wed Oct 19 08:26:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5053 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp247787wrs; Wed, 19 Oct 2022 03:41:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6z9rbJ8M8rK3P/fnc2PSxOa3yGnhmUVIGMDeDe78VuUnv6GHgx/tgsq2Mylyk5z9pcmPEi X-Received: by 2002:a17:907:6e9e:b0:78c:5533:4158 with SMTP id sh30-20020a1709076e9e00b0078c55334158mr5857191ejc.417.1666176111488; Wed, 19 Oct 2022 03:41:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666176111; cv=none; d=google.com; s=arc-20160816; b=e12ctKZqORYhpX2OgvkEFprvW+JtQTRe4gfejoUr5bxa9ONl7aqzaMMp1dYQs59n1q +2EDZmKA5IoEU002dLdM+11fo6AjTAr47YC5Yvps9K+d+wmX5QSK5HiE2oicRuwA5Z6u +Ib2yBNnOiH+/41VN7FwgtIBdPUxYbM2BexC/vN6NSr8Xqb/8ZlJeLlvJkit5zrPUPv/ RXlAra4GfM5xR47LzpmVTetiKCkjVnmKDff4Mr1L/2G41eiGpyJ5CwbVzLd42K1qDIXG 6y/lM2/+FQpJ/1oa7xiUw74tx+w2VY/lJmTygt7tvX06BwVkJ++fmKuwC0A1nG+ZQoRl moqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wxxECDAQGzigeSeZLPf7PBJXIf8D8jHRjdGKI7sgdY0=; b=Ly8475swc60UHOC2mtxjW5sRm7GIqQxv0Ysn7I18/Sm5tzBLvufT0g3WxXl09978IM MofDQXGYibK1t86wWMQQdMZ9pX7BkdwoFEbIZQSokg6DVGTveE65BUsX+JuMyghzPDty CiyBRagKD6vO6aH0A+t7hxB1nEkpc6Hvl9zRltt8sArPK6xMnqsOwUfVjmS+6HNiiyca HG4c84B/ZDeH8N9gjcUyv1zo0q4hcnkjRnGeinqBpRjopPNVtzu39rIIigeWTDAa1ayt 6+OuPivH4682+Kpw/+m+Da9l7mEG3nECyQ1hO8eacEzXc2CbijPJvkSyuvdSAEejSpBU 8Mdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="p3ddpY/w"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a50aa84000000b00458b87a0919si13082049edc.114.2022.10.19.03.41.25; Wed, 19 Oct 2022 03:41:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="p3ddpY/w"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231864AbiJSKkp (ORCPT + 99 others); Wed, 19 Oct 2022 06:40:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231855AbiJSKjt (ORCPT ); Wed, 19 Oct 2022 06:39:49 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1D8E15745E; Wed, 19 Oct 2022 03:18:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 354FAB823B2; Wed, 19 Oct 2022 08:52:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 81200C433C1; Wed, 19 Oct 2022 08:52:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169549; bh=LoFmpQ3886X+NWnu6AizYWHdAbOt4zyiwxpdLya/P7g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p3ddpY/wQ+mwcN54Lvk0gfa+CEaQu6qV2pokV8j1sRAfVDa9n1Ey3oBeD8k96tOHI /kGeqAG54j6LOqtPGGfc5oVyTKfEn8qsWjJl8cbQzpwg5J4n/WzF3CSB9ogw+uTNNo RRy2UAom/LwwgZOzCwXqcnthdC2CPYIgOLQGhl+A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= , Andrii Nakryiko , Daniel Borkmann , Sasha Levin Subject: [PATCH 6.0 319/862] libbpf: Dont require full struct enum64 in UAPI headers Date: Wed, 19 Oct 2022 10:26:46 +0200 Message-Id: <20221019083304.107157180@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747112282201218910?= X-GMAIL-MSGID: =?utf-8?q?1747112282201218910?= From: Andrii Nakryiko [ Upstream commit 87dbdc230d162bf9ee1ac77c8ade178b6b1e199e ] Drop the requirement for system-wide kernel UAPI headers to provide full struct btf_enum64 definition. This is an unexpected requirement that slipped in libbpf 1.0 and put unnecessary pressure ([0]) on users to have a bleeding-edge kernel UAPI header from unreleased Linux 6.0. To achieve this, we forward declare struct btf_enum64. But that's not enough as there is btf_enum64_value() helper that expects to know the layout of struct btf_enum64. So we get a bit creative with reinterpreting memory layout as array of __u32 and accesing lo32/hi32 fields as array elements. Alternative way would be to have a local pointer variable for anonymous struct with exactly the same layout as struct btf_enum64, but that gets us into C++ compiler errors complaining about invalid type casts. So play it safe, if ugly. [0] Closes: https://github.com/libbpf/libbpf/issues/562 Fixes: d90ec262b35b ("libbpf: Add enum64 support for btf_dump") Reported-by: Toke Høiland-Jørgensen Signed-off-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Acked-by: Toke Høiland-Jørgensen Link: https://lore.kernel.org/bpf/20220927042940.147185-1-andrii@kernel.org Signed-off-by: Sasha Levin --- tools/lib/bpf/btf.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 583760df83b4..d421d656a076 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -487,6 +487,8 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t) return (struct btf_enum *)(t + 1); } +struct btf_enum64; + static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) { return (struct btf_enum64 *)(t + 1); @@ -494,7 +496,28 @@ static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) static inline __u64 btf_enum64_value(const struct btf_enum64 *e) { - return ((__u64)e->val_hi32 << 32) | e->val_lo32; + /* struct btf_enum64 is introduced in Linux 6.0, which is very + * bleeding-edge. Here we are avoiding relying on struct btf_enum64 + * definition coming from kernel UAPI headers to support wider range + * of system-wide kernel headers. + * + * Given this header can be also included from C++ applications, that + * further restricts C tricks we can use (like using compatible + * anonymous struct). So just treat struct btf_enum64 as + * a three-element array of u32 and access second (lo32) and third + * (hi32) elements directly. + * + * For reference, here is a struct btf_enum64 definition: + * + * const struct btf_enum64 { + * __u32 name_off; + * __u32 val_lo32; + * __u32 val_hi32; + * }; + */ + const __u32 *e64 = (const __u32 *)e; + + return ((__u64)e64[2] << 32) | e64[1]; } static inline struct btf_member *btf_members(const struct btf_type *t)