From patchwork Thu Apr 21 00:38:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820957 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 32657C4332F for ; Thu, 21 Apr 2022 00:38:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383455AbiDUAlj (ORCPT ); Wed, 20 Apr 2022 20:41:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383448AbiDUAlh (ORCPT ); Wed, 20 Apr 2022 20:41:37 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 089F6205C7; Wed, 20 Apr 2022 17:38:49 -0700 (PDT) Date: Thu, 21 Apr 2022 00:38:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501528; bh=ieYkeey10FAhoCbWrOd1isttftjkFsISmPppYe5m5sM=; 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=hGIu1MujrFCCpRDxpmF8ugG8/VtqzXXj2hTW0lqgEu/pD4OBH65i2iXXJbcVS8c0+ VVNIJXEOJyZaF3/4rH3OmWMbvhT68ti9JsvLjFpoPzUguuLp5jsIosFJqsuW0SO8Qu OOPyBfe58wvB/pc9fLlhkXC8h4uThZFq0n5rWWw3XJWONZSGlEdQPyDN91Kd33jUFM jDKcD0YZNm+b1AENqdkC1HIMoeOR5w5T31xBj/W+J0lMIOFrn25DDGpj25/3jwF6fu 9PDia2fmA8HM5jTmXPlG4STMdDjaRk3xEcjSDbs1MvwL+UQGPfBQ6hATa32xmO1sA3 9oHwpaxvQ0BuA== 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 01/11] bpftool: use a local copy of perf_event to fix accessing ::bpf_cookie Message-ID: <20220421003152.339542-2-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) -- 2.36.0 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 Thu Apr 21 00:38:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820958 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 29322C433EF for ; Thu, 21 Apr 2022 00:39:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383469AbiDUAl5 (ORCPT ); Wed, 20 Apr 2022 20:41:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383457AbiDUAlz (ORCPT ); Wed, 20 Apr 2022 20:41:55 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B39B0220ED; Wed, 20 Apr 2022 17:39:07 -0700 (PDT) Date: Thu, 21 Apr 2022 00:38:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501546; bh=h36ggskq9XGdjBzPQnssVWN6Wf4iatLNUirRoRiZBiw=; 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=OPefelKWg3a3dc5CM+Llww0E8ejWJF79yrwH+/bMFAp0Vdv6ZMhndPKiLLkJhzfnc LvMJixUnblLiq/pjz3DQf+QJ/KQ/eUxdr242SV+sx5UKT9QwIXJekNU/04JIuPORn8 pa4tlEnFh2HIfI9h66mm4nrIQyX/XDOXaZ8qvB8r7YrjmvG5Kn0DJQzss47Zuj4buw Ve++O26D0XYWZ7AKDswIgatuWGqdnJGtuQJI8pXviQVdQJD97FlbWKR0OpX4Lvkjkc UHUm9ZGCjpyp6rWoew0zGtTUIlhR9rVqh8BbOJRu/FFQrWneE5Cw1fA2NqW4C7gBSi wjtnfMp8ViNAg== 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 02/11] bpftool: define a local bpf_perf_link to fix accessing its fields Message-ID: <20220421003152.339542-3-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) -- 2.36.0 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 Thu Apr 21 00:39:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820959 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 59999C4332F for ; Thu, 21 Apr 2022 00:39:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383480AbiDUAmA (ORCPT ); Wed, 20 Apr 2022 20:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383466AbiDUAl5 (ORCPT ); Wed, 20 Apr 2022 20:41:57 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B068921824; 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 --- tools/bpf/bpftool/skeleton/profiler.bpf.c | 27 ++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) -- 2.36.0 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; } From patchwork Thu Apr 21 00:39:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820960 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 13427C433FE for ; Thu, 21 Apr 2022 00:39:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383516AbiDUAmO (ORCPT ); Wed, 20 Apr 2022 20:42:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383491AbiDUAmK (ORCPT ); Wed, 20 Apr 2022 20:42:10 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79B8F245BC; Wed, 20 Apr 2022 17:39:18 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501556; bh=xsNYvkm5Qd125vyidZBd6wtSSydi8Pp6nb55LqTjBlw=; 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=dS9u56yR1Ee0jS8CgcSI9UUgtL7gjpQnm5XkfmIF0d21RxGa0Cll2OUQWgSywkX5k Q79O0HkfV9P+Xx3Cxkdsy1V6vPK3pM/ZywTMuNu30pJmtazMqkUs6n1KpMeM0yL3Rq D0FOZ8iP/D8oZLZbfNKiStHbRgv/aJW7Ff3f2/lFCtWzs03RUOIalGbthXSa8EB/UI qYSUR825wYkwEdZdTvXvBFYH0wYNp21C5NVMNlHoMeivOIwgBpbAbBSMUsVF35WDnB SlW9koZjJhR7ebCZRbO4bFpsV/zNbY36y6KvEAj2bG8Xy7kGzT+iXXIrnG+l/po9hi NnUdJFa3rw+JQ== 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 04/11] bpftool: fix fcntl.h include Message-ID: <20220421003152.339542-5-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Fix the following (on some libc implementations): CC tracelog.o In file included from tracelog.c:12: include/sys/fcntl.h:1:2: warning: #warning redirecting incorrect #include to [-Wcpp] 1 | #warning redirecting incorrect #include to | ^~~~~~~ is anyway just a wrapper over (backcomp stuff). Fixes: 30da46b5dc3a ("tools: bpftool: add a command to dump the trace pipe") Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- tools/bpf/bpftool/tracelog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.36.0 diff --git a/tools/bpf/bpftool/tracelog.c b/tools/bpf/bpftool/tracelog.c index e80a5c79b38f..bf1f02212797 100644 --- a/tools/bpf/bpftool/tracelog.c +++ b/tools/bpf/bpftool/tracelog.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include "main.h" From patchwork Thu Apr 21 00:39:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820966 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 9529DC433FE for ; Thu, 21 Apr 2022 00:41:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383506AbiDUAod (ORCPT ); Wed, 20 Apr 2022 20:44:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383503AbiDUAmU (ORCPT ); Wed, 20 Apr 2022 20:42:20 -0400 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B4CA23140 for ; Wed, 20 Apr 2022 17:39:32 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501571; bh=wuC1+vhay3ASZ7SmRAfdiwma2HlM/uaJNS3a/KTFZuQ=; 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=NIW25xfbt5y/meQ91SkENqxh9jDr3xHuEFfIe9oFetZeS9Esx9nq8D+f3xtwOA4R5 b8GP5uR12nhhBLrs6GCiJ9HDeL2dRxCDOeTJFvJrsvupR+G9tQBAfOuRy3NbmPIKQG yUsJCTvZzpOmbj9S6V4ErjLhrazOgUcobhQeqJXW4HwXhIeTgHVcnj+sK2WvRUkvNp dnrx3QM6E4YCMKtE3+EIDd8FW/HTImIihzbNRxk28a1+yBz0CpoF09lH7OXpmdDOl+ bWGk2DnVUw9kxrDqtPC+fMy3Eakf9VhoJseNmaf9Rycy1KX5opa+VHbFubileRA6jJ 71UbHrbwvhDsQ== 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 05/11] samples/bpf: add 'asm/mach-generic' include path for every MIPS Message-ID: <20220421003152.339542-6-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Fix the following: In file included from samples/bpf/tracex2_kern.c:7: In file included from ./include/linux/skbuff.h:13: In file included from ./include/linux/kernel.h:22: In file included from ./include/linux/bitops.h:33: In file included from ./arch/mips/include/asm/bitops.h:20: In file included from ./arch/mips/include/asm/barrier.h:11: ./arch/mips/include/asm/addrspace.h:13:10: fatal error: 'spaces.h' file not found #include ^~~~~~~~~~ 'arch/mips/include/asm/mach-generic' should always be included as many other MIPS include files rely on this. Move it from under CONFIG_MACH_LOONGSON64 to let it be included for every MIPS. Fixes: 058107abafc7 ("samples/bpf: Add include dir for MIPS Loongson64 to fix build errors") Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- samples/bpf/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.36.0 diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 38638845db9d..70323ac1114f 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -194,8 +194,8 @@ ifeq ($(ARCH), mips) TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__ ifdef CONFIG_MACH_LOONGSON64 BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64 -BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic endif +BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic endif TPROGS_CFLAGS += -Wall -O2 From patchwork Thu Apr 21 00:39:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820967 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 8AE8EC433EF for ; Thu, 21 Apr 2022 00:41:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383514AbiDUAog (ORCPT ); Wed, 20 Apr 2022 20:44:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383512AbiDUAmW (ORCPT ); Wed, 20 Apr 2022 20:42:22 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FC0627B23; Wed, 20 Apr 2022 17:39:34 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501572; bh=v6gaFkC1lQ5OvOGA5jP895MtmOBP8Tdf6LrASizShuo=; 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=PFdTuOoCFpxSp4uLdYHgSLDaZVZwi7hatwlWmCfdP56Da7ptv/UBE8hO/VLSl1xAn lOkh92ooqW1lM/YySkpa4nxE/nsmnRPHmkW/Rhd1ZuDTDvT2o5k0hmI+E1/DsA+O7O TDyzviRTM4/nByU25Ar99spr1lC7wo7phHzCAeKvcKcgpNBojGFUJqhalRb0WyzN6l oIRGiLBv5xH0tkPZJxCTDur070BWsA3iRt0dAYOOwnHlItFmjiGlkAbG5h7KcS05nB zkV9ViBIHO//ZKcyS+V2Q7NsHosR/zRyXrk/0VNx9rggxhcHxfwXC6BRowRIcFARbS mezfQjhZThggg== 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 06/11] samples/bpf: use host bpftool to generate vmlinux.h, not target Message-ID: <20220421003152.339542-7-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Use the host build of bpftool (bootstrap) instead of the target one to generate vmlinux.h/skeletons for the BPF samples. Otherwise, when host != target, samples compilation fails with: /bin/sh: line 1: samples/bpf/bpftool/bpftool: failed to exec: Exec format error Fixes: 384b6b3bbf0d ("samples: bpf: Add vmlinux.h generation support") Acked-by: Kumar Kartikeya Dwivedi Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- samples/bpf/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) -- 2.36.0 diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 70323ac1114f..2bb9088a8d91 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -291,12 +291,13 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool -BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool +BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool $(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT) $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \ OUTPUT=$(BPFTOOL_OUTPUT)/ \ LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \ - LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/ + LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/ \ + bootstrap $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT): $(call msg,MKDIR,$@) From patchwork Thu Apr 21 00:39:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820964 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 799B6C433EF for ; Thu, 21 Apr 2022 00:41:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346941AbiDUAob (ORCPT ); Wed, 20 Apr 2022 20:44:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383530AbiDUAmZ (ORCPT ); Wed, 20 Apr 2022 20:42:25 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37DB82A262; Wed, 20 Apr 2022 17:39:37 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501575; bh=0PStfJn/A30V4r63nD53fw6U9MRSxUFeg0SQxYCDl8o=; 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=ha5O7GsYjAxVIWUiwEWS6Wh5ksolr7RWStN0fdNIJ4VKKHWIRQCtr+M06ZWxebTuW vhPVsPi2kytoKb9p90Ziwj2ZT+KPMZ1jXcVqqHchqse4S/0jcOUPyM7PM5N+qZ4WkU 4k4dlSRssuF+OIoMhDXH4fKm/4l4HLB1pl+nLsAmFildlGsK4XKckGPjUQOve0rsk7 cwaEtCPHImlTgg6dDkMdKMls+EjdbjJZ/VD/AxWg1Wfauq2sy4zdW4DaNttbPEM5FV davbM8BIOnKyQl6s3wJgCNuNXlP1GHlHIehk2M4F60+Y0ixcbzaqgkz+G32ZZKp6AW Vg8LiqmlPGU7Q== 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 07/11] samples/bpf: fix uin64_t format literals Message-ID: <20220421003152.339542-8-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net There's a couple places where uin64_t is being passed as an %lu format argument. That type is defined as unsigned long on 64-bit systems and as unsigned long long on 32-bit, so neither %lu nor %llu are not universal. One of the options is %PRIu64, but since it's always 8-byte long, just cast it to the _proper_ __u64 and print as %llu. Fixes: 51570a5ab2b7 ("A Sample of using socket cookie and uid for traffic monitoring") Fixes: 00f660eaf378 ("Sample program using SO_COOKIE") Signed-off-by: Alexander Lobakin --- samples/bpf/cookie_uid_helper_example.c | 12 ++++++------ samples/bpf/lwt_len_hist_user.c | 7 ++++--- 2 files changed, 10 insertions(+), 9 deletions(-) -- 2.36.0 diff --git a/samples/bpf/cookie_uid_helper_example.c b/samples/bpf/cookie_uid_helper_example.c index f0df3dda4b1f..269fac58fd5c 100644 --- a/samples/bpf/cookie_uid_helper_example.c +++ b/samples/bpf/cookie_uid_helper_example.c @@ -207,9 +207,9 @@ static void print_table(void) error(1, errno, "fail to get entry value of Key: %u\n", curN); } else { - printf("cookie: %u, uid: 0x%x, Packet Count: %lu," - " Bytes Count: %lu\n", curN, curEntry.uid, - curEntry.packets, curEntry.bytes); + printf("cookie: %u, uid: 0x%x, Packet Count: %llu, Bytes Count: %llu\n", + curN, curEntry.uid, (__u64)curEntry.packets, + (__u64)curEntry.bytes); } } } @@ -265,9 +265,9 @@ static void udp_client(void) if (res < 0) error(1, errno, "lookup sk stat failed, cookie: %lu\n", cookie); - printf("cookie: %lu, uid: 0x%x, Packet Count: %lu," - " Bytes Count: %lu\n\n", cookie, dataEntry.uid, - dataEntry.packets, dataEntry.bytes); + printf("cookie: %llu, uid: 0x%x, Packet Count: %llu, Bytes Count: %llu\n\n", + (__u64)cookie, dataEntry.uid, (__u64)dataEntry.packets, + (__u64)dataEntry.bytes); } close(s_send); close(s_rcv); diff --git a/samples/bpf/lwt_len_hist_user.c b/samples/bpf/lwt_len_hist_user.c index 430a4b7e353e..c682faa75a2b 100644 --- a/samples/bpf/lwt_len_hist_user.c +++ b/samples/bpf/lwt_len_hist_user.c @@ -44,7 +44,8 @@ int main(int argc, char **argv) while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) { if (next_key >= MAX_INDEX) { - fprintf(stderr, "Key %lu out of bounds\n", next_key); + fprintf(stderr, "Key %llu out of bounds\n", + (__u64)next_key); continue; } @@ -66,8 +67,8 @@ int main(int argc, char **argv) for (i = 1; i <= max_key + 1; i++) { stars(starstr, data[i - 1], max_value, MAX_STARS); - printf("%8ld -> %-8ld : %-8ld |%-*s|\n", - (1l << i) >> 1, (1l << i) - 1, data[i - 1], + printf("%8ld -> %-8ld : %-8lld |%-*s|\n", + (1l << i) >> 1, (1l << i) - 1, (__u64)data[i - 1], MAX_STARS, starstr); } From patchwork Thu Apr 21 00:39:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820961 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 E8DF6C433F5 for ; Thu, 21 Apr 2022 00:40:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383488AbiDUAmv (ORCPT ); Wed, 20 Apr 2022 20:42:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383557AbiDUAmg (ORCPT ); Wed, 20 Apr 2022 20:42:36 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68239C15; Wed, 20 Apr 2022 17:39:48 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501586; bh=OfNKhPyskwu1Q+3O0NrPPcm3lyoLjqwkLF7TAXF+zrM=; 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=Gv76EDdwK9liHoTdSS5w4HVGCnBoPqoi3KllzNoPJOPyTIw/8Wv6ASNOOEnAaaiV7 USoT99Z/UgYq7jIIHonFsaWa295GFVDledCPqvv3mz7wzzRaM9oi7RZGOTMwP/80Lv +CJeuYfL8NoE16JL/srqKP5qB3g3DZShwM0RmOEnc7Si7lfo2mrbjnj9Dfrcjasi3W FIPRcZY2BdqZJo73Tjn/lEAio+PldaIxuztKQQ8UvgQ+ImRZ/HhuoMp9mWCKdRHTxt Fdfv7fMtgrfHze1/IHWmrEvY+i9eObYepH/YJIb/LcRSJGQI05eT+pPIjO35jDwWWK LSsqxigXJRV/w== 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 08/11] samples/bpf: fix false-positive right-shift underflow warnings Message-ID: <20220421003152.339542-9-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net On 32 bit systems, shifting an unsigned long by 32 positions yields the following warning: samples/bpf/tracex2_kern.c:60:23: warning: shift count >= width of type [-Wshift-count-overflow] unsigned int hi = v >> 32; ^ ~~ sizeof(long) is always 8 for the BPF architecture, so this is not correct, but the BPF samples Makefile still uses the Clang native + LLC combo which enforces that. Until the samples are switched to `-target bpf`, do it the usual way: shift by 16 two times (see upper_32_bits() macro in the kernel). Fixes: d822a1926849 ("samples/bpf: Add counting example for kfree_skb() function calls and the write() syscall") Fixes: 0fb1170ee68a ("bpf: BPF based latency tracing") Fixes: f74599f7c530 ("bpf: Add tests and samples for LWT-BPF") Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- samples/bpf/lathist_kern.c | 2 +- samples/bpf/lwt_len_hist_kern.c | 2 +- samples/bpf/tracex2_kern.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) -- 2.36.0 diff --git a/samples/bpf/lathist_kern.c b/samples/bpf/lathist_kern.c index 4adfcbbe6ef4..9744ed547abe 100644 --- a/samples/bpf/lathist_kern.c +++ b/samples/bpf/lathist_kern.c @@ -53,7 +53,7 @@ static unsigned int log2(unsigned int v) static unsigned int log2l(unsigned long v) { - unsigned int hi = v >> 32; + unsigned int hi = (v >> 16) >> 16; if (hi) return log2(hi) + 32; diff --git a/samples/bpf/lwt_len_hist_kern.c b/samples/bpf/lwt_len_hist_kern.c index 1fa14c54963a..bf32fa04c91f 100644 --- a/samples/bpf/lwt_len_hist_kern.c +++ b/samples/bpf/lwt_len_hist_kern.c @@ -49,7 +49,7 @@ static unsigned int log2(unsigned int v) static unsigned int log2l(unsigned long v) { - unsigned int hi = v >> 32; + unsigned int hi = (v >> 16) >> 16; if (hi) return log2(hi) + 32; else diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c index 5bc696bac27d..6bf22056ff95 100644 --- a/samples/bpf/tracex2_kern.c +++ b/samples/bpf/tracex2_kern.c @@ -57,7 +57,7 @@ static unsigned int log2(unsigned int v) static unsigned int log2l(unsigned long v) { - unsigned int hi = v >> 32; + unsigned int hi = (v >> 16) >> 16; if (hi) return log2(hi) + 32; else From patchwork Thu Apr 21 00:39:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820962 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 9FB23C433FE for ; Thu, 21 Apr 2022 00:40:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383555AbiDUAmx (ORCPT ); Wed, 20 Apr 2022 20:42:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383578AbiDUAmn (ORCPT ); Wed, 20 Apr 2022 20:42:43 -0400 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F024DFC8; Wed, 20 Apr 2022 17:39:55 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501593; bh=J9EMLBfyl6d0EbP75AQ2MnE0hjjzekwUZInxavmwZss=; 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=qJARHcrj6Hh60qCbLVmoEFR3CuENFViDy1rPraIGl/Az3GCz8Aq5S8KZV8rEkoWYR dJfIfBrVytWgcc6VZqf0UtjCpqkv6GIdxsYDouYrxXM4+3XoG+QEsL3uwZMsVK6PQw wYw9PVFhvkocayxI26r/CLtEAbjIRj9wJiMgnL1AS3kldqrU4k48s9n+FvYx9hPLs/ cdD9roNtSs5MYXw33n//IppafEWUhRy6dTwD07lLXeigM/5EwMVcAYxqbnajpFJhiQ b2yTZGosWBP4bfVmBEJY5FYXZ8WbirlecCte/6otmdjkan8EZPZ5HkNgElhVcKKQnq fUtpFql4HMDhg== 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 09/11] samples/bpf: fix include order for non-Glibc environments Message-ID: <20220421003152.339542-10-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Some standard C library implementations, e.g. Musl, ship the UAPI definitions themselves to not be dependent on the UAPI headers and their versions. Their kernel UAPI counterparts are usually guarded with some definitions which the formers set in order to avoid duplicate definitions. In such cases, include order matters. Change it in two samples: in the first, kernel UAPI ioctl definitions should go before the libc ones, and the opposite story with the second, where the kernel includes should go later to avoid struct redefinitions. Fixes: b4b8faa1ded7 ("samples/bpf: sample application and documentation for AF_XDP sockets") Fixes: e55190f26f92 ("samples/bpf: Fix build for task_fd_query_user.c") Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- samples/bpf/task_fd_query_user.c | 2 +- samples/bpf/xdpsock_user.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) -- 2.36.0 diff --git a/samples/bpf/task_fd_query_user.c b/samples/bpf/task_fd_query_user.c index c9a0ca8351fd..c0ecca01d890 100644 --- a/samples/bpf/task_fd_query_user.c +++ b/samples/bpf/task_fd_query_user.c @@ -9,11 +9,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include diff --git a/samples/bpf/xdpsock_user.c b/samples/bpf/xdpsock_user.c index 6f3fe30ad283..9747d47a0a8f 100644 --- a/samples/bpf/xdpsock_user.c +++ b/samples/bpf/xdpsock_user.c @@ -7,14 +7,15 @@ #include #include #include -#include #include #include +#include #include #include #include #include #include +#include #include #include #include From patchwork Thu Apr 21 00:39:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820965 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 ABA79C433F5 for ; Thu, 21 Apr 2022 00:41:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383494AbiDUAoc (ORCPT ); Wed, 20 Apr 2022 20:44:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383604AbiDUAmq (ORCPT ); Wed, 20 Apr 2022 20:42:46 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C87C9FC8 for ; Wed, 20 Apr 2022 17:39:58 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501597; bh=cRAvzrtALX1sJ8J18VhyO/LgCTRMCMTwVnkRTlhgHHY=; 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=bN7ztdhb3R7z9ftStNO+xmJOuI0eX/7lvHTuEM2FKXprSN+QoX55kjLXacGPAQZie Q6+6KndDKQXPh6LnZRm8KXOCmmuf9lTgJ6ZSleFF6gZhhY3RIb0/mIbjYr9otNNF9e p7R9alTrgJBCQpd9NR1SsnREpEiU/TBe0kJ3DMQZ+1Ra2+hijoJWupRVJnWpByn2V2 kv2KdfVYAjzWNQr6zmnO0AQotqWT7FYitJt0NzzPIceDSobra7m+xX2DwP3+Jm0D0v IDXVWOf56J/SRdfhggn4zDph4Brp15HVph3qnyIz/4a0xZHX0444Lqz97WjDRwRmGC Vk7flGjRnNrsA== 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 10/11] samples/bpf: fix -Wsequence-point Message-ID: <20220421003152.339542-11-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In some libc implementations, CPU_SET() may utilize its first argument several times. When combined with a post-increment, it leads to: samples/bpf/test_lru_dist.c:233:36: warning: operation on 'next_to_try' may be undefined [-Wsequence-point] 233 | CPU_SET(next_to_try++, &cpuset); | ^ Macros must always define local copies of arguments to avoid reusing, but since several libc versions already and still have that, split the sentence into two standalone operations to fix this. Fixes: 5db58faf989f ("bpf: Add tests for the LRU bpf_htab") Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- samples/bpf/test_lru_dist.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) -- 2.36.0 diff --git a/samples/bpf/test_lru_dist.c b/samples/bpf/test_lru_dist.c index 75e877853596..d09ccd5370e8 100644 --- a/samples/bpf/test_lru_dist.c +++ b/samples/bpf/test_lru_dist.c @@ -230,7 +230,8 @@ static int sched_next_online(int pid, int next_to_try) while (next_to_try < nr_cpus) { CPU_ZERO(&cpuset); - CPU_SET(next_to_try++, &cpuset); + CPU_SET(next_to_try, &cpuset); + next_to_try++; if (!sched_setaffinity(pid, sizeof(cpuset), &cpuset)) break; } From patchwork Thu Apr 21 00:39:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 12820963 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 E1815C433F5 for ; Thu, 21 Apr 2022 00:40:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1383548AbiDUAm4 (ORCPT ); Wed, 20 Apr 2022 20:42:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383552AbiDUAmw (ORCPT ); Wed, 20 Apr 2022 20:42:52 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42B291BEBF for ; Wed, 20 Apr 2022 17:40:04 -0700 (PDT) Date: Thu, 21 Apr 2022 00:39:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail2; t=1650501602; bh=W0VffryUE5RZwuv08QCVM2IU2VidejFL9tGRVW8qxIU=; 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=ZmwSroo+f/5b/WG1HKGylRZynSpIQ7KwWJXsPB35AcEjjQWgWLGzrrLTKHiJQ7mYT wzphf4JosMH0oJ6OOq5QI+pEO6tAU/smfTxKKI2n7mFUlmEEg+CD8SOOGxwCfdZKzP p1rBaXkzYRrAwc0OFrTOIK8SLRaPd34JorLslZzs+kVaRX1fiL5w6jDgHPQ/Jk364F dTC0WyV45BxGoQjD985X18M+2afDUl4jpRq9H9oYPr0jX/TzoCzNeL369xQanMyqb6 ck3tLF/yLpqjIn+RXHbFNveiB8nbS3dmPKyfMidq4O/TjC/jA+t97ZiJMjx5q8IKuj WEOC87ZPC22yA== 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 11/11] samples/bpf: xdpsock: fix -Wmaybe-uninitialized Message-ID: <20220421003152.339542-12-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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Fix two sort-of-false-positives in the xdpsock userspace part: samples/bpf/xdpsock_user.c: In function 'main': samples/bpf/xdpsock_user.c:1531:47: warning: 'tv_usec' may be used uninitialized in this function [-Wmaybe-uninitialized] 1531 | pktgen_hdr->tv_usec = htonl(tv_usec); | ^~~~~~~~~~~~~~ samples/bpf/xdpsock_user.c:1500:26: note: 'tv_usec' was declared here 1500 | u32 idx, tv_sec, tv_usec; | ^~~~~~~ samples/bpf/xdpsock_user.c:1530:46: warning: 'tv_sec' may be used uninitialized in this function [-Wmaybe-uninitialized] 1530 | pktgen_hdr->tv_sec = htonl(tv_sec); | ^~~~~~~~~~~~~ samples/bpf/xdpsock_user.c:1500:18: note: 'tv_sec' was declared here 1500 | u32 idx, tv_sec, tv_usec; | ^~~~~~ Both variables are always initialized when @opt_tstamp == true and they're being used also only when @opt_tstamp == true. However, that variable comes from the BSS and is being toggled from another function. They can't be executed simultaneously to actually trigger undefined behaviour, but purely technically it is a correct warning. Just initialize them with zeroes. Fixes: eb68db45b747 ("samples/bpf: xdpsock: Add timestamp for Tx-only operation") Acked-by: Maciej Fijalkowski Acked-by: Song Liu Signed-off-by: Alexander Lobakin --- samples/bpf/xdpsock_user.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.36.0 diff --git a/samples/bpf/xdpsock_user.c b/samples/bpf/xdpsock_user.c index 9747d47a0a8f..a6d8291c8b38 100644 --- a/samples/bpf/xdpsock_user.c +++ b/samples/bpf/xdpsock_user.c @@ -1497,7 +1497,7 @@ static void rx_drop_all(void) static int tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size, unsigned long tx_ns) { - u32 idx, tv_sec, tv_usec; + u32 idx, tv_sec = 0, tv_usec = 0; unsigned int i; while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <