From patchwork Mon Sep 2 20:05:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13787683 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 542CC1D6792; Mon, 2 Sep 2024 20:05:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307517; cv=none; b=AroO1w7ahw5SYuk67yud3sCedOlEqAPkPy3LimYz+5TxU/d9iGiQupG6VtycfHvHYT0AFvdR5HO6b4/sUhZ5G1AakGz3FGQ+mSyESpqMstgx1PLhw/JR0bziXkrm4+CpTl1rO+Eny+Zjd1740ALpA7yY3X8vl7Zh3QBo5RFya9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307517; c=relaxed/simple; bh=uh4VtjUUr5NjwAo/DUboGTevNunfbEJudmUWdFmEXu4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NHzgbQb9GF+idIfoiYpm4Gm96S4QdGZeDlZlJDj6+fDIN+0fjcUSG/5ANbAYzOeiXsyXNPFS+AXzQ/ItFUpJZ9eqo5QSHEI622SVK/1sLlIem/SLrVR76lNNEnrRKS2W1B7pMXu59hSGq5F7mW76NBfk94seJIzlblp+fHSjMq0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hc24pqBx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hc24pqBx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BFDFC4CEC8; Mon, 2 Sep 2024 20:05:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725307516; bh=uh4VtjUUr5NjwAo/DUboGTevNunfbEJudmUWdFmEXu4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hc24pqBxda9cs7V7huGhzYzUgIL+hpKrNG2OvPgCnuOg1CdWJbxQVSIaYUFqZh1y+ cQ1IIQDF9ya2liIrham/nuxdIeAJHhDOWpt4FAWQQ13vxTd4IodlqQVoYqEa+CiKYJ GBZGGRib4yVM/BQ7rucK3tT4jwOo1PErhIpSQcs0rWi6Pjzdc8hX7zwzCPd6amSYX2 R4N4jURNlduwhUUue6EcLxp7Epy//G5zURTjbPCjro7bVMgy1NVB90a4arlz6Q8FiL lZlFiezLSfMvqrOw6DLBOTqDuRj2TX+iLul+mJirUAUvOPF0vg5U/Q2yQt8pHtnsEs SrIn5A3UTJHcA== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 1/5] perf stat: Constify control data for BPF Date: Mon, 2 Sep 2024 13:05:11 -0700 Message-ID: <20240902200515.2103769-2-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240902200515.2103769-1-namhyung@kernel.org> References: <20240902200515.2103769-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The control knobs set before loading BPF programs should be declared as 'const volatile' so that it can be optimized by the BPF core. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_counter_cgroup.c | 6 +++--- tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c index ea29c372f339705d..6ff42619de12bddf 100644 --- a/tools/perf/util/bpf_counter_cgroup.c +++ b/tools/perf/util/bpf_counter_cgroup.c @@ -61,6 +61,9 @@ static int bperf_load_program(struct evlist *evlist) skel->rodata->num_cpus = total_cpus; skel->rodata->num_events = evlist->core.nr_entries / nr_cgroups; + if (cgroup_is_v2("perf_event") > 0) + skel->rodata->use_cgroup_v2 = 1; + BUG_ON(evlist->core.nr_entries % nr_cgroups != 0); /* we need one copy of events per cpu for reading */ @@ -82,9 +85,6 @@ static int bperf_load_program(struct evlist *evlist) goto out; } - if (cgroup_is_v2("perf_event") > 0) - skel->bss->use_cgroup_v2 = 1; - err = -1; cgrp_switch = evsel__new(&cgrp_switch_attr); diff --git a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c index 6a438e0102c5a2cb..57cab7647a9ad766 100644 --- a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c +++ b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c @@ -57,9 +57,9 @@ struct cgroup___old { const volatile __u32 num_events = 1; const volatile __u32 num_cpus = 1; +const volatile int use_cgroup_v2 = 0; int enabled = 0; -int use_cgroup_v2 = 0; int perf_subsys_id = -1; static inline __u64 get_cgroup_v1_ancestor_id(struct cgroup *cgrp, int level) From patchwork Mon Sep 2 20:05:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13787684 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F3B9B2101A3; Mon, 2 Sep 2024 20:05:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307518; cv=none; b=R+JAOHvwH4nzrePIsWV9xnDIbKHqfP0Zm9PIPJMXAfyiLw2RlnfMj9ClZv3AuJko/cMEEuO8CZH/zNVJR3MwBa9gDCf2AfgwbDMDvLqeGZ4dlkdO4kcPM5L2YmCT7fS6Y4RaYsgCwEiglgtUSB0kNeDj1si3pGEQI6Q6VkPwIbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307518; c=relaxed/simple; bh=X0ek1mkNREyiAUXg/Zefjuh2UjFk41svoWx7X7V/gXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OxvLSiXgLVMn2+Vs0FnwCfjQOXITJ4z7WLAhp9nIvGOyWInzqtxa7/98FgP7QkeDL1fcuZQxJg2ucTjVOVmTS4H9JAPBEehjdEgYG5Akg1kMBSjurJiSrZLLa5ZN88lkmvWhjTuz7Vly/zFTqe/6Zs+mxWcSwVfwJEZUvVp6i8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NfWFcmaX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NfWFcmaX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFC3FC4CEC6; Mon, 2 Sep 2024 20:05:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725307517; bh=X0ek1mkNREyiAUXg/Zefjuh2UjFk41svoWx7X7V/gXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NfWFcmaXkqXOyiKyuLySUQCBSTLT+vaUa/Wf34esFu3T7u6/0cfcV6VMC5UDUM6NE 4iCtLLrCh6P3kK+WYLqAd/bi2FzWOExK7Icm5359ajYT/f0+hG3x2G1hwEEPFa5fQv hIh/khzaaOiQelWCV2j9YayG5On6hkyQBH8JFB4achqGvxMne24aQ1I8tMbrH/+0M8 UTmJQCPTe+f7PBNWkQ1kvjqnILaxaD5Rqei6W/N0QT/gGAwnlG1XAuY3af3YDh82Tg ajS+DZDEcGe71IuaLO9938rmPjW1Eyy9CxDDZC3/lIpGDNNSea1xgWVbtyfDRXZDGW 4z1TNq8TBM1qQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 2/5] perf ftrace latency: Constify control data for BPF Date: Mon, 2 Sep 2024 13:05:12 -0700 Message-ID: <20240902200515.2103769-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240902200515.2103769-1-namhyung@kernel.org> References: <20240902200515.2103769-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The control knobs set before loading BPF programs should be declared as 'const volatile' so that it can be optimized by the BPF core. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_ftrace.c | 8 ++++---- tools/perf/util/bpf_skel/func_latency.bpf.c | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/bpf_ftrace.c b/tools/perf/util/bpf_ftrace.c index 7a4297d8fd2ce925..06d1c4018407a265 100644 --- a/tools/perf/util/bpf_ftrace.c +++ b/tools/perf/util/bpf_ftrace.c @@ -40,13 +40,17 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) if (ftrace->target.cpu_list) { ncpus = perf_cpu_map__nr(ftrace->evlist->core.user_requested_cpus); bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); + skel->rodata->has_cpu = 1; } if (target__has_task(&ftrace->target) || target__none(&ftrace->target)) { ntasks = perf_thread_map__nr(ftrace->evlist->core.threads); bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + skel->rodata->has_task = 1; } + skel->rodata->use_nsec = ftrace->use_nsec; + set_max_rlimit(); err = func_latency_bpf__load(skel); @@ -59,7 +63,6 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) u32 cpu; u8 val = 1; - skel->bss->has_cpu = 1; fd = bpf_map__fd(skel->maps.cpu_filter); for (i = 0; i < ncpus; i++) { @@ -72,7 +75,6 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) u32 pid; u8 val = 1; - skel->bss->has_task = 1; fd = bpf_map__fd(skel->maps.task_filter); for (i = 0; i < ntasks; i++) { @@ -81,8 +83,6 @@ int perf_ftrace__latency_prepare_bpf(struct perf_ftrace *ftrace) } } - skel->bss->use_nsec = ftrace->use_nsec; - skel->links.func_begin = bpf_program__attach_kprobe(skel->progs.func_begin, false, func->name); if (IS_ERR(skel->links.func_begin)) { diff --git a/tools/perf/util/bpf_skel/func_latency.bpf.c b/tools/perf/util/bpf_skel/func_latency.bpf.c index 9d01e3af747922ca..f613dc9cb123480c 100644 --- a/tools/perf/util/bpf_skel/func_latency.bpf.c +++ b/tools/perf/util/bpf_skel/func_latency.bpf.c @@ -37,9 +37,10 @@ struct { int enabled = 0; -int has_cpu = 0; -int has_task = 0; -int use_nsec = 0; + +const volatile int has_cpu = 0; +const volatile int has_task = 0; +const volatile int use_nsec = 0; SEC("kprobe/func") int BPF_PROG(func_begin) From patchwork Mon Sep 2 20:05:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13787685 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 18CC82101AD; Mon, 2 Sep 2024 20:05:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307518; cv=none; b=kvMmVU5UzJ36jr7asExvzElDtf7UK/5xLer2eIZs3u80KIV1PKgvaAfVp5/YIYLMm658XMdyJvu7RcJMOsGtFrHt+oRRSOAiPRGLuxaNbkiiHaKVmCZj7IS/FuQvJb6rgZyG0MCk92bnDi4gHwn1H3ZLLrJ+Qm05BPzm/T7W4KM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307518; c=relaxed/simple; bh=l9WvYqLtg7S11PYIe7m0qJ7EWnF5FzWibwNh2ZfGfh8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i2VokN/U5INcvvTk4j0c6s69zYfakl5IucDQZ5b43HlqHCL2MSaES20A7BY0teAdpchOQbyJVkR8ylScvm70deA22akYh/jCAiIQ1Pd5qLh0Ye5NCJy6in4eyWfBwGJTwcrLOAchFgszQn7nqHg3diRzH6oVOfCnk1cxchf1K+M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Xcw4QedY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Xcw4QedY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 877A3C4CEC2; Mon, 2 Sep 2024 20:05:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725307518; bh=l9WvYqLtg7S11PYIe7m0qJ7EWnF5FzWibwNh2ZfGfh8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xcw4QedY8Hsw3AEiWp2y/ghitLVzgHDnnTnLlMit3qi4rOInt5+SX62KHGVcxO4mv VHHs8TTJ8jBYUtKKWgx8uYdaRI6E7m5V3VVDlYXtzIrI2L9WB9GeJQH74XdLC+F0L6 F+NZXi2bIWdWTTrpGfMI+J2GGNDCbxlYaUhSPUX3R2GUUvHP9yC1saw5y+rPzX/33h g4NsZRGchgJdnyQirsd9kzxYmOECyUxLu9MBmYEbNBepu0qRjI3r0VsFAqUy7yLzsi doGHAiLXZtSh3CMObS1dmAmpeGE52af6veKcpxDpokHW4PMP9OJ+vM7R7qCerwhkg7 07oTXI+N3BD4g== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org, Yang Jihong Subject: [PATCH 3/5] perf kwork: Constify control data for BPF Date: Mon, 2 Sep 2024 13:05:13 -0700 Message-ID: <20240902200515.2103769-4-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240902200515.2103769-1-namhyung@kernel.org> References: <20240902200515.2103769-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The control knobs set before loading BPF programs should be declared as 'const volatile' so that it can be optimized by the BPF core. Cc: Yang Jihong Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_kwork.c | 9 +++++---- tools/perf/util/bpf_kwork_top.c | 7 ++++--- tools/perf/util/bpf_skel/kwork_top.bpf.c | 2 +- tools/perf/util/bpf_skel/kwork_trace.bpf.c | 5 +++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/bpf_kwork.c b/tools/perf/util/bpf_kwork.c index 44f0f708a15d6a14..6c7126b7670dd0c9 100644 --- a/tools/perf/util/bpf_kwork.c +++ b/tools/perf/util/bpf_kwork.c @@ -176,8 +176,6 @@ static int setup_filters(struct perf_kwork *kwork) bpf_map_update_elem(fd, &cpu.cpu, &val, BPF_ANY); } perf_cpu_map__put(map); - - skel->bss->has_cpu_filter = 1; } if (kwork->profile_name != NULL) { @@ -197,8 +195,6 @@ static int setup_filters(struct perf_kwork *kwork) key = 0; bpf_map_update_elem(fd, &key, kwork->profile_name, BPF_ANY); - - skel->bss->has_name_filter = 1; } return 0; @@ -239,6 +235,11 @@ int perf_kwork__trace_prepare_bpf(struct perf_kwork *kwork) class_bpf->load_prepare(kwork); } + if (kwork->cpu_list != NULL) + skel->rodata->has_cpu_filter = 1; + if (kwork->profile_name != NULL) + skel->rodata->has_name_filter = 1; + if (kwork_trace_bpf__load(skel)) { pr_debug("Failed to load kwork trace skeleton\n"); goto out; diff --git a/tools/perf/util/bpf_kwork_top.c b/tools/perf/util/bpf_kwork_top.c index 22a3b00a1e23f929..7261cad43468d7a4 100644 --- a/tools/perf/util/bpf_kwork_top.c +++ b/tools/perf/util/bpf_kwork_top.c @@ -151,14 +151,12 @@ static int setup_filters(struct perf_kwork *kwork) bpf_map_update_elem(fd, &cpu.cpu, &val, BPF_ANY); } perf_cpu_map__put(map); - - skel->bss->has_cpu_filter = 1; } return 0; } -int perf_kwork__top_prepare_bpf(struct perf_kwork *kwork __maybe_unused) +int perf_kwork__top_prepare_bpf(struct perf_kwork *kwork) { struct bpf_program *prog; struct kwork_class *class; @@ -193,6 +191,9 @@ int perf_kwork__top_prepare_bpf(struct perf_kwork *kwork __maybe_unused) class_bpf->load_prepare(); } + if (kwork->cpu_list) + skel->rodata->has_cpu_filter = 1; + if (kwork_top_bpf__load(skel)) { pr_debug("Failed to load kwork top skeleton\n"); goto out; diff --git a/tools/perf/util/bpf_skel/kwork_top.bpf.c b/tools/perf/util/bpf_skel/kwork_top.bpf.c index 84c15ccbab44ccc9..594da91965a2f6c9 100644 --- a/tools/perf/util/bpf_skel/kwork_top.bpf.c +++ b/tools/perf/util/bpf_skel/kwork_top.bpf.c @@ -84,7 +84,7 @@ struct { int enabled = 0; -int has_cpu_filter = 0; +const volatile int has_cpu_filter = 0; __u64 from_timestamp = 0; __u64 to_timestamp = 0; diff --git a/tools/perf/util/bpf_skel/kwork_trace.bpf.c b/tools/perf/util/bpf_skel/kwork_trace.bpf.c index 063c124e099938ed..cbd79bc4b3302d81 100644 --- a/tools/perf/util/bpf_skel/kwork_trace.bpf.c +++ b/tools/perf/util/bpf_skel/kwork_trace.bpf.c @@ -68,8 +68,9 @@ struct { } perf_kwork_name_filter SEC(".maps"); int enabled = 0; -int has_cpu_filter = 0; -int has_name_filter = 0; + +const volatile int has_cpu_filter = 0; +const volatile int has_name_filter = 0; static __always_inline int local_strncmp(const char *s1, unsigned int sz, const char *s2) From patchwork Mon Sep 2 20:05:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13787686 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 01F1D1C62DB; Mon, 2 Sep 2024 20:05:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307519; cv=none; b=M823azHNlIwcvrEMQ7YB+VAfW/5pQnv5pkWXKLT/+T/oDAm+apAxH4ifu0vJdS1Kx/W0bSugXxN81S9TZKcAzk4ZZ9Yu2Drvp+fg3eZARRVZtNCynKPJyzW8ue/1p5Tw4nQgcIo4fFODRopGnc9GLCKa/yGFLUeL7GRSepgSjpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307519; c=relaxed/simple; bh=vbXiJUW44MYn4zKNicBPcWvvEQsLojbALFnxDDCzRRM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p1JjEwBV8fSwf4WER1Y3xe60qgyZddsqgy7kw9L22sfRbKVM0yBFVnKG4eZmebXO9V+6uDG36jR7xmWwby+y3V5/KyVA6UEZxMSr9kGqlbz2vf5Iu1e1tKAo+bNDKCHDsyW9I+dWeS8qPv0eJqIH9PT6BgIys0TxLZGSHjl9/S4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SxFojMMn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SxFojMMn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21931C4CED8; Mon, 2 Sep 2024 20:05:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725307518; bh=vbXiJUW44MYn4zKNicBPcWvvEQsLojbALFnxDDCzRRM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SxFojMMnaxyrEJG1fjJldXz3VQfQtCEhqEd9/XyRgPQfPoeb7e+3eqA7MRPgXDbgB qcCGpmOT6At+kOhRbaGhvMuWBFM/hE1i78Sf5PcOoCGqkKpDw73mPN6x+2yjS4BhAN N0gjLgEOIzaxh4KamQxCKawXDc770muvYd/PH4GNSTE3ZSOR/NfKmYZcKnE8mSt0MZ m+7+f1U37xV7u14MgBbf11n9C9rPmR5UwoKQq2seqxB/rDEat4FgcELJV1jZSJ3MxU AsPvnq9euqHRgxYXh7fhfPQjDLt2qOo3zMQ72Yx3OtvvLDnpGgI9pv5sXDrQb8S4JQ 2F5s62IIXeiLA== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 4/5] perf lock contention: Constify control data for BPF Date: Mon, 2 Sep 2024 13:05:14 -0700 Message-ID: <20240902200515.2103769-5-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240902200515.2103769-1-namhyung@kernel.org> References: <20240902200515.2103769-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The control knobs set before loading BPF programs should be declared as 'const volatile' so that it can be optimized by the BPF core. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_lock_contention.c | 45 ++++++++++--------- .../perf/util/bpf_skel/lock_contention.bpf.c | 27 +++++------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index bc4e92c0c08b8b20..41a1ad08789511c3 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -46,14 +46,22 @@ int lock_contention_prepare(struct lock_contention *con) else bpf_map__set_max_entries(skel->maps.stacks, 1); - if (target__has_cpu(target)) + if (target__has_cpu(target)) { + skel->rodata->has_cpu = 1; ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus); - if (target__has_task(target)) + } + if (target__has_task(target)) { + skel->rodata->has_task = 1; ntasks = perf_thread_map__nr(evlist->core.threads); - if (con->filters->nr_types) + } + if (con->filters->nr_types) { + skel->rodata->has_type = 1; ntypes = con->filters->nr_types; - if (con->filters->nr_cgrps) + } + if (con->filters->nr_cgrps) { + skel->rodata->has_cgroup = 1; ncgrps = con->filters->nr_cgrps; + } /* resolve lock name filters to addr */ if (con->filters->nr_syms) { @@ -82,6 +90,7 @@ int lock_contention_prepare(struct lock_contention *con) con->filters->addrs = addrs; } naddrs = con->filters->nr_addrs; + skel->rodata->has_addr = 1; } bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); @@ -90,6 +99,16 @@ int lock_contention_prepare(struct lock_contention *con) bpf_map__set_max_entries(skel->maps.addr_filter, naddrs); bpf_map__set_max_entries(skel->maps.cgroup_filter, ncgrps); + skel->rodata->stack_skip = con->stack_skip; + skel->rodata->aggr_mode = con->aggr_mode; + skel->rodata->needs_callstack = con->save_callstack; + skel->rodata->lock_owner = con->owner; + + if (con->aggr_mode == LOCK_AGGR_CGROUP || con->filters->nr_cgrps) { + if (cgroup_is_v2("perf_event")) + skel->rodata->use_cgroup_v2 = 1; + } + if (lock_contention_bpf__load(skel) < 0) { pr_err("Failed to load lock-contention BPF skeleton\n"); return -1; @@ -99,7 +118,6 @@ int lock_contention_prepare(struct lock_contention *con) u32 cpu; u8 val = 1; - skel->bss->has_cpu = 1; fd = bpf_map__fd(skel->maps.cpu_filter); for (i = 0; i < ncpus; i++) { @@ -112,7 +130,6 @@ int lock_contention_prepare(struct lock_contention *con) u32 pid; u8 val = 1; - skel->bss->has_task = 1; fd = bpf_map__fd(skel->maps.task_filter); for (i = 0; i < ntasks; i++) { @@ -125,7 +142,6 @@ int lock_contention_prepare(struct lock_contention *con) u32 pid = evlist->workload.pid; u8 val = 1; - skel->bss->has_task = 1; fd = bpf_map__fd(skel->maps.task_filter); bpf_map_update_elem(fd, &pid, &val, BPF_ANY); } @@ -133,7 +149,6 @@ int lock_contention_prepare(struct lock_contention *con) if (con->filters->nr_types) { u8 val = 1; - skel->bss->has_type = 1; fd = bpf_map__fd(skel->maps.type_filter); for (i = 0; i < con->filters->nr_types; i++) @@ -143,7 +158,6 @@ int lock_contention_prepare(struct lock_contention *con) if (con->filters->nr_addrs) { u8 val = 1; - skel->bss->has_addr = 1; fd = bpf_map__fd(skel->maps.addr_filter); for (i = 0; i < con->filters->nr_addrs; i++) @@ -153,25 +167,14 @@ int lock_contention_prepare(struct lock_contention *con) if (con->filters->nr_cgrps) { u8 val = 1; - skel->bss->has_cgroup = 1; fd = bpf_map__fd(skel->maps.cgroup_filter); for (i = 0; i < con->filters->nr_cgrps; i++) bpf_map_update_elem(fd, &con->filters->cgrps[i], &val, BPF_ANY); } - /* these don't work well if in the rodata section */ - skel->bss->stack_skip = con->stack_skip; - skel->bss->aggr_mode = con->aggr_mode; - skel->bss->needs_callstack = con->save_callstack; - skel->bss->lock_owner = con->owner; - - if (con->aggr_mode == LOCK_AGGR_CGROUP) { - if (cgroup_is_v2("perf_event")) - skel->bss->use_cgroup_v2 = 1; - + if (con->aggr_mode == LOCK_AGGR_CGROUP) read_all_cgroups(&con->cgroups); - } bpf_program__set_autoload(skel->progs.collect_lock_syms, false); diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c index 52a876b4269917fe..1069bda5d733887f 100644 --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c @@ -117,21 +117,22 @@ struct mm_struct___new { } __attribute__((preserve_access_index)); /* control flags */ -int enabled; -int has_cpu; -int has_task; -int has_type; -int has_addr; -int has_cgroup; -int needs_callstack; -int stack_skip; -int lock_owner; - -int use_cgroup_v2; -int perf_subsys_id = -1; +const volatile int has_cpu; +const volatile int has_task; +const volatile int has_type; +const volatile int has_addr; +const volatile int has_cgroup; +const volatile int needs_callstack; +const volatile int stack_skip; +const volatile int lock_owner; +const volatile int use_cgroup_v2; /* determine the key of lock stat */ -int aggr_mode; +const volatile int aggr_mode; + +int enabled; + +int perf_subsys_id = -1; __u64 end_ts; From patchwork Mon Sep 2 20:05:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13787687 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8C9B621C19E; Mon, 2 Sep 2024 20:05:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307519; cv=none; b=OLcrY3yPFGBxBho3TSP3EEH997ZM6vKhUKFHfpYUU8x0Gepa3zVCBYhNbKNSBi6xlinKHxGlICEwR8kNOl2/Olemqgjwq1rrEnJTZkYfnhxs3rS7CokiQGza6rnApRVF0Mj29AFanjYuncb5uMGT0i5S4QtqjH0D/f0IzW+685I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725307519; c=relaxed/simple; bh=+Pwd5GaA9knREy8lwTTeOVCpbI0xDH9FKh/p825ZiW4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QBq006awsAbS+2e6xiZNAPZQ3/J+R90KtY1UrKpLEGgWdfEN3eQDwi/rAovVblrzGhX8mqjhRxMhUsxoF/Yo5nod8ihNjragtRquYb0+ZNLfxz1pANqA+GnEikV0yHXTwfqRSNR+VLnbwjFy5m14Zf5jLIi3Xp6kvYLqUnHLqas= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MHKAT3vc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MHKAT3vc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8CE1C4CED4; Mon, 2 Sep 2024 20:05:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725307519; bh=+Pwd5GaA9knREy8lwTTeOVCpbI0xDH9FKh/p825ZiW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MHKAT3vcR/3ZyLfB90CSL3UldTyRqP/Z4jDLAbdHrop+kFB6gZFw9TBniv0PORXHJ eUKgiWz3MS1acbIFVyApmYtjT6pS5ZEP9bKx2eE+Y3mKF4eFMoOUH1875B2QRkFhRz 2Ktd/b6xnzr6IK32MUcxs4ribt2JWoLh5VRhZWxLnERn2Xfxm8d06yZs4OHCGMZBcy N+35ssm00BaE2gtjPEsRNnJpH2jlnzn4jOrRDemLVXxIrWj3ru17T6cKy3laY0Y+di d1+AVY6tx0GOiE1wBWZug0OzLat/WP+Dj3efcDQ4udm5CldeqToRaQXBwREQj+vL6t USohxECgf5Tpg== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Subject: [PATCH 5/5] perf record offcpu: Constify control data for BPF Date: Mon, 2 Sep 2024 13:05:15 -0700 Message-ID: <20240902200515.2103769-6-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240902200515.2103769-1-namhyung@kernel.org> References: <20240902200515.2103769-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The control knobs set before loading BPF programs should be declared as 'const volatile' so that it can be optimized by the BPF core. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_off_cpu.c | 16 ++++++++-------- tools/perf/util/bpf_skel/off_cpu.bpf.c | 9 +++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 6af36142dc5a7fd0..a590a8ac1f9d42f0 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -73,14 +73,12 @@ static void off_cpu_start(void *arg) struct evlist *evlist = arg; /* update task filter for the given workload */ - if (!skel->bss->has_cpu && !skel->bss->has_task && + if (skel->rodata->has_task && skel->rodata->uses_tgid && perf_thread_map__pid(evlist->core.threads, 0) != -1) { int fd; u32 pid; u8 val = 1; - skel->bss->has_task = 1; - skel->bss->uses_tgid = 1; fd = bpf_map__fd(skel->maps.task_filter); pid = perf_thread_map__pid(evlist->core.threads, 0); bpf_map_update_elem(fd, &pid, &val, BPF_ANY); @@ -148,6 +146,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, if (target->cpu_list) { ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus); bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); + skel->rodata->has_cpu = 1; } if (target->pid) { @@ -173,11 +172,16 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, ntasks = MAX_PROC; bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + skel->rodata->has_task = 1; + skel->rodata->uses_tgid = 1; } else if (target__has_task(target)) { ntasks = perf_thread_map__nr(evlist->core.threads); bpf_map__set_max_entries(skel->maps.task_filter, ntasks); + skel->rodata->has_task = 1; } else if (target__none(target)) { bpf_map__set_max_entries(skel->maps.task_filter, MAX_PROC); + skel->rodata->has_task = 1; + skel->rodata->uses_tgid = 1; } if (evlist__first(evlist)->cgrp) { @@ -186,6 +190,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, if (!cgroup_is_v2("perf_event")) skel->rodata->uses_cgroup_v1 = true; + skel->rodata->has_cgroup = 1; } if (opts->record_cgroup) { @@ -208,7 +213,6 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, u32 cpu; u8 val = 1; - skel->bss->has_cpu = 1; fd = bpf_map__fd(skel->maps.cpu_filter); for (i = 0; i < ncpus; i++) { @@ -220,8 +224,6 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, if (target->pid) { u8 val = 1; - skel->bss->has_task = 1; - skel->bss->uses_tgid = 1; fd = bpf_map__fd(skel->maps.task_filter); strlist__for_each_entry(pos, pid_slist) { @@ -240,7 +242,6 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, u32 pid; u8 val = 1; - skel->bss->has_task = 1; fd = bpf_map__fd(skel->maps.task_filter); for (i = 0; i < ntasks; i++) { @@ -253,7 +254,6 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target, struct evsel *evsel; u8 val = 1; - skel->bss->has_cgroup = 1; fd = bpf_map__fd(skel->maps.cgroup_filter); evlist__for_each_entry(evlist, evsel) { diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c index d877a0a9731f9f2f..c152116df72f9bc1 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -85,10 +85,11 @@ struct task_struct___old { } __attribute__((preserve_access_index)); int enabled = 0; -int has_cpu = 0; -int has_task = 0; -int has_cgroup = 0; -int uses_tgid = 0; + +const volatile int has_cpu = 0; +const volatile int has_task = 0; +const volatile int has_cgroup = 0; +const volatile int uses_tgid = 0; const volatile bool has_prev_state = false; const volatile bool needs_cgroup = false;