Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp886614pxb; Fri, 22 Apr 2022 13:27:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCafYkJNy03q75JKW1mnKBvmEksHUPJhjdbPnNlJ6VSasbiUAPnlXupQrqwcwTdoRmDAsB X-Received: by 2002:a17:902:6bc2:b0:158:a26b:5884 with SMTP id m2-20020a1709026bc200b00158a26b5884mr6119938plt.38.1650659256689; Fri, 22 Apr 2022 13:27:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650659256; cv=none; d=google.com; s=arc-20160816; b=Cc+CobsVNKyZU7BRcGnSSdryUMWT7m0KhXtaPlxBF1uqPgya617awaL78Os0PRGl4J 2B6iIXy9q4sPINXN/pKnsN8/0VIsikYb00t+0eiujLHnKKUlh8ikTlQEMTdsvgBDjVV2 vT2n9QtxINmyPMSwyggsJeKt1XcgEeYYFSvhLps6YboRYazbG1yAjypYXUVp4By8jAWp qZsC+IIdzIPZLI79jm5Goglcv+cXBKI8RzyRf2a43JOhTued1diCJVs5jM+9J46zlZTK GYpbUxaZMtLbztCKejDvmrMHKlwZGLWCnzHVD+6V/EGB9GzxntB934JZqTZAAlg/3iNv Ptug== 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 :feedback-id:references:in-reply-to:message-id:subject:reply-to:cc :from:to:dkim-signature:date; bh=/g4pmmtM4Oo/NDEXoriizu4gAbFoM5evYeKkWecSlPo=; b=j9idPdt3hs9gOJJr5974RnCiFk+MCc3jn5X4JwgTFFz3Oo9W58gr6Njfzb1wABRJ4V Gk61r+xoyz/gAAOERRLMz4UfLusW9dP6LhxhGJo+uH2KROtaLqQzBb6Payvt5x31Mr38 8uAYFA79F+h4ypok12+98VW1hsRdapprjYv540b0vvnPlHFYxgVaERSF2ey42PHXnxLP LRE85r0k5C2Dfvj0Mekd/kJ5+Ev5PJEqVCWN+xOLbtFcdNGiZawfIBLXB1IzbEoY2xkA XABHd/pOBjzz0QQmlUcEReJR2Xg8a4hXXFmO3/KKijpGGyUDqUPXexOIkE5curT7HagZ uw5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail2 header.b="k/WI8gCg"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pm.me Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m19-20020a056a00081300b004fa3a8e000fsi9642520pfk.198.2022.04.22.13.27.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 13:27:36 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail2 header.b="k/WI8gCg"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pm.me Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EB5602783C7; Fri, 22 Apr 2022 12:14:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383483AbiDUAmC (ORCPT + 99 others); Wed, 20 Apr 2022 20:42:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383467AbiDUAl5 (ORCPT ); Wed, 20 Apr 2022 20:41:57 -0400 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A78D62181F for ; Wed, 20 Apr 2022 17:39:09 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501548; bh=/g4pmmtM4Oo/NDEXoriizu4gAbFoM5evYeKkWecSlPo=; h=Date:To:From:Cc:Reply-To:Subject:Message-ID:In-Reply-To: References:Feedback-ID:From:To:Cc:Date:Subject:Reply-To: Feedback-ID:Message-ID; b=k/WI8gCgbA/7H7mcN1b6NuIjFe0vc31u1hLtl8ksxXxNWMUBpPV5i7+qGZgZDfOMb ZJJtN9LFRURpWzTf7z1pHMoE+x+8GcvXhM7910qM83oEd2Gv8RWCqv5wMgPINl8aIP YZobarZReKbUDhIXsN0Yieq/ylDE5hbIyA+2WlIv6h3pEshLaIpfzUVkhgn0ZN8iSU gc/LruJoeWTqcYNoNLYXsz4ZJfrGja8GC6KzYjv5FSZ9Wc6SJ33IIyqixSsrfySm9D 2j5uOuMIIslSFFlzkSyjeYdGGn2NAc84b01sXRDRpBoEBEY8FvrRTpBnjAVd2C+/2s bgGUZtREiDHbQ== To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko From: Alexander Lobakin Cc: Alexander Lobakin , Maciej Fijalkowski , Song Liu , Kumar Kartikeya Dwivedi , bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Reply-To: Alexander Lobakin Subject: [PATCH v2 bpf 03/11] bpftool: use a local bpf_perf_event_value to fix accessing its fields Message-ID: <20220421003152.339542-4-alobakin@pm.me> In-Reply-To: <20220421003152.339542-1-alobakin@pm.me> References: <20220421003152.339542-1-alobakin@pm.me> Feedback-ID: 22809121:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=no 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 Fix the following error when building bpftool: CLANG profiler.bpf.o CLANG pid_iter.bpf.o skeleton/profiler.bpf.c:18:21: error: invalid application of 'sizeof' to an= incomplete type 'struct bpf_perf_event_value' __uint(value_size, sizeof(struct bpf_perf_event_value)); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:13:39: note: e= xpanded from macro '__uint' tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helper_defs.h:7:8: note:= forward declaration of 'struct bpf_perf_event_value' struct bpf_perf_event_value; ^ struct bpf_perf_event_value is being used in the kernel only when CONFIG_BPF_EVENTS is enabled, so it misses a BTF entry then. Define struct bpf_perf_event_value___local with the `preserve_access_index` attribute inside the pid_iter BPF prog to allow compiling on any configs. It is a full mirror of a UAPI structure, so is compatible both with and w/o CO-RE. bpf_perf_event_read_value() requires a pointer of the original type, so a cast is needed. Fixes: 47c09d6a9f67 ("bpftool: Introduce "prog profile" command") Suggested-by: Andrii Nakryiko Signed-off-by: Alexander Lobakin --- tools/bpf/bpftool/skeleton/profiler.bpf.c | 27 ++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tools/bpf/bpftool/skeleton/profiler.bpf.c b/tools/bpf/bpftool/= skeleton/profiler.bpf.c index ce5b65e07ab1..2f80edc682f1 100644 --- a/tools/bpf/bpftool/skeleton/profiler.bpf.c +++ b/tools/bpf/bpftool/skeleton/profiler.bpf.c @@ -4,6 +4,12 @@ #include #include +struct bpf_perf_event_value___local { +=09__u64 counter; +=09__u64 enabled; +=09__u64 running; +} __attribute__((preserve_access_index)); + /* map of perf event fds, num_cpu * num_metric entries */ struct { =09__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); @@ -15,14 +21,14 @@ struct { struct { =09__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); =09__uint(key_size, sizeof(u32)); -=09__uint(value_size, sizeof(struct bpf_perf_event_value)); +=09__uint(value_size, sizeof(struct bpf_perf_event_value___local)); } fentry_readings SEC(".maps"); /* accumulated readings */ struct { =09__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); =09__uint(key_size, sizeof(u32)); -=09__uint(value_size, sizeof(struct bpf_perf_event_value)); +=09__uint(value_size, sizeof(struct bpf_perf_event_value___local)); } accum_readings SEC(".maps"); /* sample counts, one per cpu */ @@ -39,7 +45,7 @@ const volatile __u32 num_metric =3D 1; SEC("fentry/XXX") int BPF_PROG(fentry_XXX) { -=09struct bpf_perf_event_value *ptrs[MAX_NUM_MATRICS]; +=09struct bpf_perf_event_value___local *ptrs[MAX_NUM_MATRICS]; =09u32 key =3D bpf_get_smp_processor_id(); =09u32 i; @@ -53,10 +59,10 @@ int BPF_PROG(fentry_XXX) =09} =09for (i =3D 0; i < num_metric && i < MAX_NUM_MATRICS; i++) { -=09=09struct bpf_perf_event_value reading; +=09=09struct bpf_perf_event_value___local reading; =09=09int err; -=09=09err =3D bpf_perf_event_read_value(&events, key, &reading, +=09=09err =3D bpf_perf_event_read_value(&events, key, (void *)&reading, =09=09=09=09=09=09sizeof(reading)); =09=09if (err) =09=09=09return 0; @@ -68,14 +74,14 @@ int BPF_PROG(fentry_XXX) } static inline void -fexit_update_maps(u32 id, struct bpf_perf_event_value *after) +fexit_update_maps(u32 id, struct bpf_perf_event_value___local *after) { -=09struct bpf_perf_event_value *before, diff; +=09struct bpf_perf_event_value___local *before, diff; =09before =3D bpf_map_lookup_elem(&fentry_readings, &id); =09/* only account samples with a valid fentry_reading */ =09if (before && before->counter) { -=09=09struct bpf_perf_event_value *accum; +=09=09struct bpf_perf_event_value___local *accum; =09=09diff.counter =3D after->counter - before->counter; =09=09diff.enabled =3D after->enabled - before->enabled; @@ -93,7 +99,7 @@ fexit_update_maps(u32 id, struct bpf_perf_event_value *af= ter) SEC("fexit/XXX") int BPF_PROG(fexit_XXX) { -=09struct bpf_perf_event_value readings[MAX_NUM_MATRICS]; +=09struct bpf_perf_event_value___local readings[MAX_NUM_MATRICS]; =09u32 cpu =3D bpf_get_smp_processor_id(); =09u32 i, zero =3D 0; =09int err; @@ -102,7 +108,8 @@ int BPF_PROG(fexit_XXX) =09/* read all events before updating the maps, to reduce error */ =09for (i =3D 0; i < num_metric && i < MAX_NUM_MATRICS; i++) { =09=09err =3D bpf_perf_event_read_value(&events, cpu + i * num_cpu, -=09=09=09=09=09=09readings + i, sizeof(*readings)); +=09=09=09=09=09=09(void *)(readings + i), +=09=09=09=09=09=09sizeof(*readings)); =09=09if (err) =09=09=09return 0; =09} -- 2.36.0