From patchwork Fri May 12 10:33:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13239099 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C15FAD57 for ; Fri, 12 May 2023 10:34:54 +0000 (UTC) Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 832FE132B5 for ; Fri, 12 May 2023 03:34:24 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-305f0491e62so9376791f8f.3 for ; Fri, 12 May 2023 03:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1683887647; x=1686479647; 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=F8+XuKxjMAeHAGdlI1NVxBOcoZ+cnUWPAn3g63YYSyQ=; b=VmjqalVY7z5kg8NT8Dsrxvhv4ZWc+QmcqO5vi1U++6dL4gaE0dP6DzSiXs+R+J7eIl 5VGSYqPuosVwKnP8gJ3wXLSIdL4Nz+WMdePxw5RiAeto/9IwpzE/h77Ym+QhmHgeaTJG XLMal0O7iEcrQItuXo34JqgTO0HSuk38CO9P7TliGxJk4uFywf983znYiJnMMd2GIxPX r0V14yQ+dCAVeGLB5hcpNBjvaPj536S5rTDiTZQKoi4/stFkaDz69HfVjf/AAvMPnZXw ddw+HhldMKGH3Er8V0aSIk1I6JH5Nl+LnUghqcZXyuoA+emvVx89WFJG3z5Q3SYyObDg NSeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683887647; x=1686479647; 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=F8+XuKxjMAeHAGdlI1NVxBOcoZ+cnUWPAn3g63YYSyQ=; b=cMJsB6ErPRjVnXCmk5dGTsNI9ipkb92nHWVly8sueF0tuCl/clar4TavEphkQmqnV1 NWyQGGDNuctzBOoxXeSv+wpGxz+7UDtb5vw0CTtGFgyvrmOuGYkB1i06ZKejwAam0fCR P+cuRC4FGvxrwJqGQgrJsSXJvmYVxau4eqGQ2WMhOcqmOmbGBE4omtADhq0/XLuJq6q5 /IZQchZMCMeKLVLrY3tNELKqnc/xDom8YiA986v8/eSg8+PNPX9AxtgCtbEM4++gEadq l5hn7ukjU/TEXS3L3t/3Uu8ddZIhOwFskT78yZM/HmNbl+Le8nvuH8eyR2iKKxqyj7Z4 j6hQ== X-Gm-Message-State: AC+VfDz/9EaeythX2lMj5F6zlIXQ78eR7TCm7nVfRGxrG7SgRRB+o1xf c/51I1+As4a9TVGDETaevPo7lQ== X-Google-Smtp-Source: ACHHUZ4/fs/YQPZVoJ+tJ8HsBI1l4D+8Pk6QqzO/BZV4CjziW8p2SGnNZvehICuFJaVDwUERKdVfig== X-Received: by 2002:a5d:42ca:0:b0:306:3b78:fe31 with SMTP id t10-20020a5d42ca000000b003063b78fe31mr15443375wrr.69.1683887646768; Fri, 12 May 2023 03:34:06 -0700 (PDT) Received: from harfang.fritz.box ([2a02:8011:e80c:0:a162:20e4:626a:dd]) by smtp.gmail.com with ESMTPSA id q6-20020adff946000000b003078cd719ffsm17946320wrr.95.2023.05.12.03.34.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 03:34:06 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, Quentin Monnet , Alexander Lobakin , =?utf-8?q?Michal_Such?= =?utf-8?q?=C3=A1nek?= , Alexander Lobakin Subject: [PATCH bpf-next 1/4] bpftool: use a local copy of perf_event to fix accessing ::bpf_cookie Date: Fri, 12 May 2023 11:33:51 +0100 Message-Id: <20230512103354.48374-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230512103354.48374-1-quentin@isovalent.com> References: <20230512103354.48374-1-quentin@isovalent.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Lobakin When CONFIG_PERF_EVENTS is not set, struct perf_event remains empty. However, the structure is being used by bpftool indirectly via BTF. This leads to: skeleton/pid_iter.bpf.c:49:30: error: no member named 'bpf_cookie' in 'struct perf_event' return BPF_CORE_READ(event, bpf_cookie); ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~ ... skeleton/pid_iter.bpf.c:49:9: error: returning 'void' from a function with incompatible result type '__u64' (aka 'unsigned long long') return BPF_CORE_READ(event, bpf_cookie); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tools and samples can't use any CONFIG_ definitions, so the fields used there should always be present. Define struct perf_event___local with the `preserve_access_index` attribute inside the pid_iter BPF prog to allow compiling on any configs. CO-RE will substitute it with the real struct perf_event accesses later on. Fixes: cbdaf71f7e65 ("bpftool: Add bpf_cookie to link output") Suggested-by: Andrii Nakryiko Signed-off-by: Alexander Lobakin Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c index eb05ea53afb1..e2af8e5fb29e 100644 --- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c +++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c @@ -15,6 +15,10 @@ enum bpf_obj_type { BPF_OBJ_BTF, }; +struct perf_event___local { + u64 bpf_cookie; +} __attribute__((preserve_access_index)); + extern const void bpf_link_fops __ksym; extern const void bpf_map_fops __ksym; extern const void bpf_prog_fops __ksym; @@ -41,8 +45,8 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type) /* could be used only with BPF_LINK_TYPE_PERF_EVENT links */ static __u64 get_bpf_cookie(struct bpf_link *link) { + struct perf_event___local *event; struct bpf_perf_link *perf_link; - struct perf_event *event; perf_link = container_of(link, struct bpf_perf_link, link); event = BPF_CORE_READ(perf_link, perf_file, private_data); From patchwork Fri May 12 10:33:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13239100 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEAF1AD57 for ; Fri, 12 May 2023 10:34:54 +0000 (UTC) Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9067311B5C for ; Fri, 12 May 2023 03:34:25 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3f4ec041fc3so11693705e9.1 for ; Fri, 12 May 2023 03:34:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1683887648; x=1686479648; 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=2NPNRPg3iC1S44ccs7eA5h09oCu2rts6Gsygb3/XLG4=; b=KU2TN68d5PEpMwlSBYS/XvHlC8aafie5kwDmui2hmBQVyiBlNj6n9dtieF5l34izcD 3JaaIr8H9bgEKw+i/EPW29I/Ua73PAqHdwwQveaWvkRiUns4DM22mofYLbVch384U66Z 1KVqmj6jU6I+mrvx5nFFn/FVOK4EWMSoOldG9T7Xr15l8UcREJuTgOi4SUFWn1YtblAG nBfPNbO0V1N4CV1G0RacqHfpzjJrL8nsCkVvmzoBzWViJhCzLPew+hdtXvyP5h2f1/S/ aBRSpGuJS3ax+rePCU8qr0CDZjQCrKQKmJr4KjS2eqscByaecPGpoiwkapmPkkTzHYQB fpmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683887648; x=1686479648; 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=2NPNRPg3iC1S44ccs7eA5h09oCu2rts6Gsygb3/XLG4=; b=S9ysq3k90a0cP98azM6YeOl0r1Cx+ZoCR9C68gZa23CjjpjF8Vb9mEc/+T/K/isylV kvh7vbi5LDusut+P1nwFxUenp2/y6nnDfyBLpnoHezQO36T7z+dHb23z0S/Kc/rjs+zJ RsAd7yILMhZs1KZpOwFrcqeONVTp7BN5JUafcToBX+GTz6SHwWYC1MhgfVELNAbsRPe8 DbOzYQOMpw7sHFwvcYacCCAV8NZ8L6VwxEGs/PkHYwGIJoBuSNRPKmxTQEL9tnbEQhqk sfNPwOWrdeFJczMY5P8Q4mMGSbKzsnpsRiDeSQ6ixipO0+Zrdy7MdsJseDmEcz0f572G o3TQ== X-Gm-Message-State: AC+VfDwtA75jKDGwA5Ois4fcb0dFvU6F3j8BxvyuUy0bq57uBgnNGYKj 2JeA4NM8xBixS6xty5itqTV/Zw== X-Google-Smtp-Source: ACHHUZ6bRZz+6d8UGMUqDavEDR7++W9B8vc3Qup73zLqIy9pJJxQ7vNEgxEVvV45MXguGKfjpKKeLQ== X-Received: by 2002:adf:f58f:0:b0:306:b3f9:e2c9 with SMTP id f15-20020adff58f000000b00306b3f9e2c9mr18224105wro.6.1683887647687; Fri, 12 May 2023 03:34:07 -0700 (PDT) Received: from harfang.fritz.box ([2a02:8011:e80c:0:a162:20e4:626a:dd]) by smtp.gmail.com with ESMTPSA id q6-20020adff946000000b003078cd719ffsm17946320wrr.95.2023.05.12.03.34.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 03:34:07 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, Quentin Monnet , Alexander Lobakin , =?utf-8?q?Michal_Such?= =?utf-8?q?=C3=A1nek?= , Alexander Lobakin Subject: [PATCH bpf-next 2/4] bpftool: define a local bpf_perf_link to fix accessing its fields Date: Fri, 12 May 2023 11:33:52 +0100 Message-Id: <20230512103354.48374-3-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230512103354.48374-1-quentin@isovalent.com> References: <20230512103354.48374-1-quentin@isovalent.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Lobakin When building bpftool with !CONFIG_PERF_EVENTS: skeleton/pid_iter.bpf.c:47:14: error: incomplete definition of type 'struct bpf_perf_link' perf_link = container_of(link, struct bpf_perf_link, link); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:74:22: note: expanded from macro 'container_of' ((type *)(__mptr - offsetof(type, member))); \ ^~~~~~~~~~~~~~~~~~~~~~ tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:68:60: note: expanded from macro 'offsetof' #define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) ~~~~~~~~~~~^ skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link' struct bpf_perf_link *perf_link; ^ &bpf_perf_link is being defined and used only under the ifdef. Define struct bpf_perf_link___local with the `preserve_access_index` attribute inside the pid_iter BPF prog to allow compiling on any configs. CO-RE will substitute it with the real struct bpf_perf_link accesses later on. container_of() is not CO-REd, but it is a noop for bpf_perf_link <-> bpf_link and the local copy is a full mirror of the original structure. Fixes: cbdaf71f7e65 ("bpftool: Add bpf_cookie to link output") Suggested-by: Andrii Nakryiko Signed-off-by: Alexander Lobakin Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c index e2af8e5fb29e..3a4c4f7d83d8 100644 --- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c +++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c @@ -15,6 +15,11 @@ enum bpf_obj_type { BPF_OBJ_BTF, }; +struct bpf_perf_link___local { + struct bpf_link link; + struct file *perf_file; +} __attribute__((preserve_access_index)); + struct perf_event___local { u64 bpf_cookie; } __attribute__((preserve_access_index)); @@ -45,10 +50,10 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type) /* could be used only with BPF_LINK_TYPE_PERF_EVENT links */ static __u64 get_bpf_cookie(struct bpf_link *link) { + struct bpf_perf_link___local *perf_link; struct perf_event___local *event; - struct bpf_perf_link *perf_link; - perf_link = container_of(link, struct bpf_perf_link, link); + perf_link = container_of(link, struct bpf_perf_link___local, link); event = BPF_CORE_READ(perf_link, perf_file, private_data); return BPF_CORE_READ(event, bpf_cookie); } From patchwork Fri May 12 10:33:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13239101 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C034AD57 for ; Fri, 12 May 2023 10:34:55 +0000 (UTC) Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9717124B3 for ; Fri, 12 May 2023 03:34:25 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3f4c6c4b425so19250615e9.2 for ; Fri, 12 May 2023 03:34:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1683887648; x=1686479648; 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=LqgT86uPEyXHdOLiPS9BK7uUMEWYpDHmxeePdcp8lT0=; b=RmA2o2Za3fPhiNxz1gDJOfd0UmL8yx109vVF32j4Ogt7bY1Za5pLJ66I9zaH2aq5LK R9x4fPxwk0wPMCYREJcXBKcc+qxpmxrFCRNjLeT+6lkxBTy7LuyjAqpAxfvpdP3D5JzD gsmZYwia5PL0ilfRUF4pfDWXh+pmnd0DXHFRvRBOdyx0OEWGUDWaMLDlxdxr31OCsTUe eYEDuIcab0KBd1urK+Lv6/VQSWrlk45hN9u6qgWO3pcK05DiSE191Ju96ENKa8MnCHlt Hk9zqBtGWSVPosgClo0XFqjfsKUZSgcNYM6bXl1n6wIvPsvC0uQBkGqKBZa35Kr4oa7C AZsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683887648; x=1686479648; 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=LqgT86uPEyXHdOLiPS9BK7uUMEWYpDHmxeePdcp8lT0=; b=CvzgSLXeV5rFU/FnwdOPhxwbWwhA/tbRVnppbqmZTsl4i4MxOlHGk4vjvHfV/KCNUc OzcHCHE6xn1Nk8Z5m3NPkVH5Hs9mRH7EAvMmhE+vYWdny3+5DFreeoDnw3xi1WKcdqtq CD3rnpU6pcUE02nx6WPdCY1z6R4hs1zc+VZEqureBxG6tIv8zbJWzno8a/lboExeWB2h xObTQX9rR7f8mXvJ6k51Pn82MIfVEn+IC4dZIUt6D0ubXzXvAVibzwGxdEy2cBdsteef rXsOU265dtIw2/k0TznlMs9EY1WGZ5tYUhFwJRrhzwZdQaWiemj8AMx3S1XjdS3PJOhO Br8w== X-Gm-Message-State: AC+VfDwKJQzMWUggOaEbo0rluICu0BO2DaKQhRLKZSnsKcnDqByFgvJi 4IYfvSSsKVYFnclAIw68+Ne1Ag== X-Google-Smtp-Source: ACHHUZ4YfrpXcM9nC0GJaw+1zgmFPocYiKWdsbjHnZXaMbrUeQLjWnr3kYxm6s3KdQkAP/SO0yp/Lw== X-Received: by 2002:a5d:428f:0:b0:2f7:e3aa:677a with SMTP id k15-20020a5d428f000000b002f7e3aa677amr17107257wrq.46.1683887648611; Fri, 12 May 2023 03:34:08 -0700 (PDT) Received: from harfang.fritz.box ([2a02:8011:e80c:0:a162:20e4:626a:dd]) by smtp.gmail.com with ESMTPSA id q6-20020adff946000000b003078cd719ffsm17946320wrr.95.2023.05.12.03.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 03:34:08 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, Quentin Monnet , Alexander Lobakin , =?utf-8?q?Michal_Such?= =?utf-8?q?=C3=A1nek?= Subject: [PATCH bpf-next 3/4] bpftool: Use a local copy of BPF_LINK_TYPE_PERF_EVENT in pid_iter.bpf.c Date: Fri, 12 May 2023 11:33:53 +0100 Message-Id: <20230512103354.48374-4-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230512103354.48374-1-quentin@isovalent.com> References: <20230512103354.48374-1-quentin@isovalent.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net In order to allow the BPF program in bpftool's pid_iter.bpf.c to compile correctly on hosts where vmlinux.h does not define BPF_LINK_TYPE_PERF_EVENT (running kernel versions lower than 5.15, for example), define and use a local copy of the enum value. This requires LLVM 12 or newer to build the BPF program. Fixes: cbdaf71f7e65 ("bpftool: Add bpf_cookie to link output") Signed-off-by: Quentin Monnet --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c index 3a4c4f7d83d8..26004f0c5a6a 100644 --- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c +++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c @@ -24,6 +24,10 @@ struct perf_event___local { u64 bpf_cookie; } __attribute__((preserve_access_index)); +enum bpf_link_type___local { + BPF_LINK_TYPE_PERF_EVENT___local = 7, +}; + extern const void bpf_link_fops __ksym; extern const void bpf_map_fops __ksym; extern const void bpf_prog_fops __ksym; @@ -93,10 +97,13 @@ int iter(struct bpf_iter__task_file *ctx) e.pid = task->tgid; e.id = get_obj_id(file->private_data, obj_type); - if (obj_type == BPF_OBJ_LINK) { + if (obj_type == BPF_OBJ_LINK && + bpf_core_enum_value_exists(enum bpf_link_type___local, + BPF_LINK_TYPE_PERF_EVENT___local)) { struct bpf_link *link = (struct bpf_link *) file->private_data; - if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) { + if (link->type == bpf_core_enum_value(enum bpf_link_type___local, + BPF_LINK_TYPE_PERF_EVENT___local)) { e.has_bpf_cookie = true; e.bpf_cookie = get_bpf_cookie(link); } From patchwork Fri May 12 10:33:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Monnet X-Patchwork-Id: 13239102 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDAB6AD57 for ; Fri, 12 May 2023 10:34:55 +0000 (UTC) Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FEBD13848 for ; Fri, 12 May 2023 03:34:26 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-3063208beedso9313871f8f.1 for ; Fri, 12 May 2023 03:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1683887649; x=1686479649; 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=OMbB3rUKr4BJ9Y1bRCvT0+9HIM59fAt1h5Xo/nKUPv4=; b=Uj3pso/Eayo7KRB5BgEY/pQdMWehWmqU7frzwSVuYUMrofP//oKvuC5zzOh0uBxMKB mBdE08WGIYHWG4NjdozigFfh64aq91VEd+nMboC13Yt87cBVJ9faurkpRG8xR+4DYk2M LsycLsHCwzhQujivFZvJuOz/8OamwSEaAGaR+Z2oWlbE3e3hzTDnuuc21lkYV1XLxcW8 KgJr0YtzsjqMRPXdHe1yRlXNyBthedZYcDUiMUSmg48hTkC53p8s1zXvf3bPmbik39Xj tV2nmCJSIhBiBYrbVF3c5QqyUBjtzb0V0Z94jjieToHo/qiLdXqmQyd9lW+7B1MClSmI pNgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683887649; x=1686479649; 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=OMbB3rUKr4BJ9Y1bRCvT0+9HIM59fAt1h5Xo/nKUPv4=; b=BbNHWicSnXrcC60pvWYKtX1FAJmRknID+u6zR2sVzLG2YX0gvBhurZRBikf2wKghyP HHFVUt3bOlo1cgKBQgK5qUUlsEC/YtqCr4umCKh0SvoR1uWRqHHZATkVMRdb8nB81U0t cPVJ13UbOqxdmtzZFWYycGu4nRls1+zQuiuvTXE1HxnXuf5c1Y0qX586TJ/C9VOgi2ea QhjWhVfb0CPP84KAHT6AWD7X+QGdHhDPjuQ6Fo9wAf/aK3WSAZuOMZgXQAA9Uj+6LkMJ LHPBzyDDF+HEvUjB5nLgPNJy2f3NtYt3uAsqR57XRjle8Vr5VGp0FnA9pJYtwQEu7svj TSAw== X-Gm-Message-State: AC+VfDw8ukWNvV7BOzsuM9rsMhlu3xAOWx+XJW11IB98IvL5b59K/VD/ CVC3qUeQMn0EtQTlYRDsNrTxmw== X-Google-Smtp-Source: ACHHUZ6eqgFzzfyx/ebDX6KljV6e6EeFX865Nmj1frO4+NFhJumo1ClpWmA6lyn7LT36n89NnJDzmg== X-Received: by 2002:adf:fccb:0:b0:306:3899:ccbf with SMTP id f11-20020adffccb000000b003063899ccbfmr17483186wrs.14.1683887649515; Fri, 12 May 2023 03:34:09 -0700 (PDT) Received: from harfang.fritz.box ([2a02:8011:e80c:0:a162:20e4:626a:dd]) by smtp.gmail.com with ESMTPSA id q6-20020adff946000000b003078cd719ffsm17946320wrr.95.2023.05.12.03.34.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 03:34:09 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, Quentin Monnet , Alexander Lobakin , =?utf-8?q?Michal_Such?= =?utf-8?q?=C3=A1nek?= , Alexander Lobakin Subject: [PATCH bpf-next 4/4] bpftool: use a local bpf_perf_event_value to fix accessing its fields Date: Fri, 12 May 2023 11:33:54 +0100 Message-Id: <20230512103354.48374-5-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230512103354.48374-1-quentin@isovalent.com> References: <20230512103354.48374-1-quentin@isovalent.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Lobakin 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: expanded 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 Signed-off-by: Quentin Monnet --- 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 { + __u64 counter; + __u64 enabled; + __u64 running; +} __attribute__((preserve_access_index)); + /* map of perf event fds, num_cpu * num_metric entries */ struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); @@ -15,14 +21,14 @@ struct { struct { __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); __uint(key_size, sizeof(u32)); - __uint(value_size, sizeof(struct bpf_perf_event_value)); + __uint(value_size, sizeof(struct bpf_perf_event_value___local)); } fentry_readings SEC(".maps"); /* accumulated readings */ struct { __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); __uint(key_size, sizeof(u32)); - __uint(value_size, sizeof(struct bpf_perf_event_value)); + __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 = 1; SEC("fentry/XXX") int BPF_PROG(fentry_XXX) { - struct bpf_perf_event_value *ptrs[MAX_NUM_MATRICS]; + struct bpf_perf_event_value___local *ptrs[MAX_NUM_MATRICS]; u32 key = bpf_get_smp_processor_id(); u32 i; @@ -53,10 +59,10 @@ int BPF_PROG(fentry_XXX) } for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) { - struct bpf_perf_event_value reading; + struct bpf_perf_event_value___local reading; int err; - err = bpf_perf_event_read_value(&events, key, &reading, + err = bpf_perf_event_read_value(&events, key, (void *)&reading, sizeof(reading)); if (err) return 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) { - struct bpf_perf_event_value *before, diff; + struct bpf_perf_event_value___local *before, diff; before = bpf_map_lookup_elem(&fentry_readings, &id); /* only account samples with a valid fentry_reading */ if (before && before->counter) { - struct bpf_perf_event_value *accum; + struct bpf_perf_event_value___local *accum; diff.counter = after->counter - before->counter; diff.enabled = after->enabled - before->enabled; @@ -93,7 +99,7 @@ fexit_update_maps(u32 id, struct bpf_perf_event_value *after) SEC("fexit/XXX") int BPF_PROG(fexit_XXX) { - struct bpf_perf_event_value readings[MAX_NUM_MATRICS]; + struct bpf_perf_event_value___local readings[MAX_NUM_MATRICS]; u32 cpu = bpf_get_smp_processor_id(); u32 i, zero = 0; int err; @@ -102,7 +108,8 @@ int BPF_PROG(fexit_XXX) /* read all events before updating the maps, to reduce error */ for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) { err = bpf_perf_event_read_value(&events, cpu + i * num_cpu, - readings + i, sizeof(*readings)); + (void *)(readings + i), + sizeof(*readings)); if (err) return 0; }