From nobody Fri Dec 19 17:17:30 2025 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 828C216EC18; Fri, 21 Jun 2024 22:24:00 +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=1719008640; cv=none; b=dmWzqZfYDXsQx9oWJ+smZmK1iNNtGXqPyXSir/FRpjmYm2PseV6ALicEhmai7Gtq3mCSmlnkpkGYG5j/Hq0kMLOj/aTSgLn0n0/fgLG3d6f73K2O+S0iEVxELtL38/RRGesyo1VWYe17nUeokbWMdj7QzzktX7BuxOEhHgosjb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719008640; c=relaxed/simple; bh=W5GZEcIk9twAOrRsPhPKS2FUMDyJH0v8fpPh0A03qEk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nObo4rmCtp7oM5mI3WgqP3fDxYBFZxgBT8jqMuC6XI3U78ddty0lPkMdtZkflRoQkIeqhE2TPoa+4d4VnphOoIwGWl4glFdO8+q6QZIcWJvbIwuodEgHL4QBruetZ3rM3+wlQfR5yOIe+8JRPkJvYx0TS4WQgh/5R2PYbNcf9mw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pdvMT9Xs; 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="pdvMT9Xs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1E18C4AF12; Fri, 21 Jun 2024 22:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719008640; bh=W5GZEcIk9twAOrRsPhPKS2FUMDyJH0v8fpPh0A03qEk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pdvMT9XsB/8qClLenFuK8U2NqL4qKJlpMKh56rQgp1nuugQ4PyvmIh3ISOMwQ+udC E1cFLiRYSiJ+0p6Ttsk5LAIdPTweHOE/w07FHknLbPeQgSx/jsCF0CWmrdO9E5IXiV 9mAM2kUBf5/VtJ8je2QV8/LzHU2jJ8+NgEO9zOU7jOcGDxBFgBbbtVECiiyO6ttG+T ToaiBDUBgzExx1Tq7L0NvoddUH2UeNmSOIr7Z8v6YYcK7GnT7tA2nCC6vXkBbZpIX0 bFNUnibpDXLfhbbIP7PGGYsOHyIqanIOKdYKgM98y2YaOGt45Xhki/XQkssGen4kWE Y3TLPboyStMLg== 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 Subject: [PATCH 3/8] perf bpf-filter: Split per-task filter use case Date: Fri, 21 Jun 2024 15:23:52 -0700 Message-ID: <20240621222357.717374-4-namhyung@kernel.org> X-Mailer: git-send-email 2.45.2.741.gdbec12cfda-goog In-Reply-To: <20240621222357.717374-1-namhyung@kernel.org> References: <20240621222357.717374-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If the target is a list of tasks, it can use a shared hash map for filter expressions. The key of the filter map is an integer index like in an array. A separate pid_hash map is added to get the index for the filter map using the tgid. For system-wide mode including per-cpu or per-user targets are handled by the single entry map like before. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf-filter.c | 186 +++++++++++++++---- tools/perf/util/bpf_skel/sample-filter.h | 1 + tools/perf/util/bpf_skel/sample_filter.bpf.c | 21 +++ 3 files changed, 168 insertions(+), 40 deletions(-) diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index 0b2eca56aa10..5ec0e0955ec4 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -3,10 +3,13 @@ =20 #include #include +#include #include +#include =20 #include "util/debug.h" #include "util/evsel.h" +#include "util/target.h" =20 #include "util/bpf-filter.h" #include @@ -91,38 +94,17 @@ static int check_sample_flags(struct evsel *evsel, stru= ct perf_bpf_filter_expr * return -1; } =20 -int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target __= maybe_unused) +static int get_filter_entries(struct evsel *evsel, struct perf_bpf_filter_= entry *entry) { - int i, x, y, fd, ret; - struct sample_filter_bpf *skel; - struct bpf_program *prog; - struct bpf_link *link; + int i =3D 0; struct perf_bpf_filter_expr *expr; - struct perf_bpf_filter_entry *entry; - - entry =3D calloc(MAX_FILTERS, sizeof(*entry)); - if (entry =3D=3D NULL) - return -1; - - skel =3D sample_filter_bpf__open_and_load(); - if (!skel) { - pr_err("Failed to load perf sample-filter BPF skeleton\n"); - ret =3D -EPERM; - goto err; - } =20 - i =3D 0; - fd =3D bpf_map__fd(skel->maps.filters); list_for_each_entry(expr, &evsel->bpf_filters, list) { - if (check_sample_flags(evsel, expr) < 0) { - ret =3D -EINVAL; - goto err; - } + if (check_sample_flags(evsel, expr) < 0) + return -EINVAL; =20 - if (i =3D=3D MAX_FILTERS) { - ret =3D -E2BIG; - goto err; - } + if (i =3D=3D MAX_FILTERS) + return -E2BIG; =20 entry[i].op =3D expr->op; entry[i].part =3D expr->part; @@ -134,10 +116,8 @@ int perf_bpf_filter__prepare(struct evsel *evsel, stru= ct target *target __maybe_ struct perf_bpf_filter_expr *group; =20 list_for_each_entry(group, &expr->groups, list) { - if (i =3D=3D MAX_FILTERS) { - ret =3D -E2BIG; - goto err; - } + if (i =3D=3D MAX_FILTERS) + return -E2BIG; =20 entry[i].op =3D group->op; entry[i].part =3D group->part; @@ -146,10 +126,8 @@ int perf_bpf_filter__prepare(struct evsel *evsel, stru= ct target *target __maybe_ i++; } =20 - if (i =3D=3D MAX_FILTERS) { - ret =3D -E2BIG; - goto err; - } + if (i =3D=3D MAX_FILTERS) + return -E2BIG; =20 entry[i].op =3D PBF_OP_GROUP_END; i++; @@ -161,15 +139,143 @@ int perf_bpf_filter__prepare(struct evsel *evsel, st= ruct target *target __maybe_ entry[i].op =3D PBF_OP_DONE; i++; } + return 0; +} + +static int convert_to_tgid(int tid) +{ + char path[128]; + char *buf, *p, *q; + int tgid; + size_t len; + + scnprintf(path, sizeof(path), "%d/status", tid); + if (procfs__read_str(path, &buf, &len) < 0) + return -1; =20 - /* The filters map has only one entry for now */ - i =3D 0; - if (bpf_map_update_elem(fd, &i, entry, BPF_ANY) < 0) { - ret =3D -errno; - pr_err("Failed to update the filter map\n"); + p =3D strstr(buf, "Tgid:"); + if (p =3D=3D NULL) { + free(buf); + return -1; + } + + tgid =3D strtol(p + 6, &q, 0); + free(buf); + if (*q !=3D '\n') + return -1; + + return tgid; +} + +static int update_pid_hash(struct sample_filter_bpf *skel, struct evsel *e= vsel, + struct perf_bpf_filter_entry *entry) +{ + int filter_idx; + int nr, last; + int fd =3D bpf_map__fd(skel->maps.filters); + struct perf_thread_map *threads; + + /* Find the first available entry in the filters map */ + for (filter_idx =3D 0; filter_idx < MAX_FILTERS; filter_idx++) { + if (bpf_map_update_elem(fd, &filter_idx, entry, BPF_NOEXIST) =3D=3D 0) + break; + } + + if (filter_idx =3D=3D MAX_FILTERS) { + pr_err("Too many users for the filter map\n"); + return -EBUSY; + } + + threads =3D perf_evsel__threads(&evsel->core); + if (threads =3D=3D NULL) { + pr_err("Cannot get the thread list of the event\n"); + return -EINVAL; + } + + /* save the index to a hash map */ + fd =3D bpf_map__fd(skel->maps.pid_hash); + + last =3D -1; + nr =3D perf_thread_map__nr(threads); + for (int i =3D 0; i < nr; i++) { + int pid =3D perf_thread_map__pid(threads, i); + int tgid; + + /* it actually needs tgid, let's get tgid from /proc. */ + tgid =3D convert_to_tgid(pid); + if (tgid < 0) { + /* the thread may be dead, ignore. */ + continue; + } + + if (tgid =3D=3D last) + continue; + last =3D tgid; + + if (bpf_map_update_elem(fd, &tgid, &filter_idx, BPF_ANY) < 0) { + pr_err("Failed to update the pid hash\n"); + return -errno; + } + pr_debug("pid hash: %d -> %d\n", tgid, filter_idx); + } + return 0; +} + +int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target) +{ + int i, x, y, fd, ret; + struct sample_filter_bpf *skel =3D NULL; + struct bpf_program *prog; + struct bpf_link *link; + struct perf_bpf_filter_entry *entry; + bool needs_pid_hash =3D !target__has_cpu(target) && !target->uid_str; + + entry =3D calloc(MAX_FILTERS, sizeof(*entry)); + if (entry =3D=3D NULL) + return -1; + + ret =3D get_filter_entries(evsel, entry); + if (ret < 0) { + pr_err("Failed to process filter entries\n"); + goto err; + } + + skel =3D sample_filter_bpf__open(); + if (!skel) { + pr_err("Failed to open perf sample-filter BPF skeleton\n"); + ret =3D -EPERM; goto err; } =20 + if (needs_pid_hash) { + bpf_map__set_max_entries(skel->maps.filters, MAX_FILTERS); + bpf_map__set_max_entries(skel->maps.pid_hash, MAX_PIDS); + skel->rodata->use_pid_hash =3D 1; + } + + if (sample_filter_bpf__load(skel) < 0) { + pr_err("Failed to load perf sample-filter BPF skeleton\n"); + ret =3D -EPERM; + goto err; + } + + if (needs_pid_hash) { + /* The filters map is shared among other processes */ + ret =3D update_pid_hash(skel, evsel, entry); + if (ret < 0) + goto err; + } else { + i =3D 0; + fd =3D bpf_map__fd(skel->maps.filters); + + /* The filters map has only one entry in this case */ + if (bpf_map_update_elem(fd, &i, entry, BPF_ANY) < 0) { + ret =3D -errno; + pr_err("Failed to update the filter map\n"); + goto err; + } + } + prog =3D skel->progs.perf_sample_filter; for (x =3D 0; x < xyarray__max_x(evsel->core.fd); x++) { for (y =3D 0; y < xyarray__max_y(evsel->core.fd); y++) { diff --git a/tools/perf/util/bpf_skel/sample-filter.h b/tools/perf/util/bpf= _skel/sample-filter.h index bb6a1b91f1df..e666bfd5fbdd 100644 --- a/tools/perf/util/bpf_skel/sample-filter.h +++ b/tools/perf/util/bpf_skel/sample-filter.h @@ -2,6 +2,7 @@ #define PERF_UTIL_BPF_SKEL_SAMPLE_FILTER_H =20 #define MAX_FILTERS 64 +#define MAX_PIDS (16 * 1024) =20 /* supported filter operations */ enum perf_bpf_filter_op { diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util= /bpf_skel/sample_filter.bpf.c index 0d56e52b922c..c5273f06fa45 100644 --- a/tools/perf/util/bpf_skel/sample_filter.bpf.c +++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c @@ -15,7 +15,16 @@ struct filters { __uint(max_entries, 1); } filters SEC(".maps"); =20 +/* tgid to filter index */ +struct pid_hash { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, int); + __type(value, int); + __uint(max_entries, 1); +} pid_hash SEC(".maps"); + int dropped; +volatile const int use_pid_hash; =20 void *bpf_cast_to_kern_ctx(void *) __ksym; =20 @@ -184,6 +193,18 @@ int perf_sample_filter(void *ctx) kctx =3D bpf_cast_to_kern_ctx(ctx); =20 k =3D 0; + + if (use_pid_hash) { + int tgid =3D bpf_get_current_pid_tgid() >> 32; + int *idx; + + idx =3D bpf_map_lookup_elem(&pid_hash, &tgid); + if (idx) + k =3D *idx; + else + goto drop; + } + entry =3D bpf_map_lookup_elem(&filters, &k); if (entry =3D=3D NULL) goto drop; --=20 2.45.2.741.gdbec12cfda-goog