Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp627704lqp; Wed, 22 May 2024 14:56:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVSN1mxEQexGeLOvkXhsJhs06P0jejvGWTXl5EllROWYELCrfs0cDgkfF5wX4oGDCEXfGmskXJCDqVSS41p9xl+LzOOebxkeJdIg0lttA== X-Google-Smtp-Source: AGHT+IHv/h2gEfiFrnCFsDZa4XTYhluWLgjfbPB5cppQs2JahUsFsNDvSe2N16L5GFl4veKfbfC3 X-Received: by 2002:aa7:d755:0:b0:578:3335:f263 with SMTP id 4fb4d7f45d1cf-5783335f358mr2075661a12.0.1716415005037; Wed, 22 May 2024 14:56:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716415005; cv=pass; d=google.com; s=arc-20160816; b=B0f2bwmaysNOCKBxQjXD/iMVF+ElMAWDuQxmgv7H7vxs3EgrqLlxx34+WHNGIXO71P 0AM8OMC2DV5HoslpZ7SGfeZbxKotd15ZUbB44UxAgM1p8Ef0c50prLdm5l+LzEk/V1cv g+6HPIpbHwW7BSdQ8TDbuZSs4hZM1orsvXmPapcAcbi9/y+WdAArnAl1JKautQoBHkhe i2Uj1pqBeYRaEC7wdmpGAz02/agIHmXFpIHG4ZGJNbCjLwpsS/hLgkSYDYLT6FUGAdLd lB2eYGh3X7IqukH85M6DaNAB/kBakP6nw/cPssLjmOaDhsszQPDqTuBl3Bwrf2/CCqG/ cU7A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mz23gbYVK/vi61k0pCCGQJCWdFXIb0wcPupm9MY+Fhg=; fh=+1J0V14dKjMi8h4q7hKoOukbS74/BCx2naK7yok440s=; b=UYyNCUC09D9HJwCdnIEhnUJSBYV2J7G7Ga5beMX87RsW8S3PERswuGKuezAeoRhNAF o/gDq65EUYcOqeoACoUzWcQ5hBzgXXs9BX7Fifv+Rwl0jhYzcCI3z4+eyj0CNApBhubm cw6m1C7WX7NhsEUUtV5qZyg8e0x+vLAqaPJrl7ZXye3wHft5jQx1ECx/psYP42GYNsqC S4Uw3xHW8PPR4tJY0iBvpOuX2OciDGY7La1tqokycPXjHcAX86RSUIaKPLVgrVqJgaOo SY8HoQqGjjUDNDuhgyZ3TmEukyYPnY2Zpon6aDMr2BKIY5jRA03qDAXaH0kQip0ZY8HU 6MrQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iAudHEua; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-186750-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186750-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-574ea283027si9812649a12.223.2024.05.22.14.56.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 14:56:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-186750-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iAudHEua; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-186750-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186750-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9C1621F21437 for ; Wed, 22 May 2024 21:56:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 02E781474B6; Wed, 22 May 2024 21:56:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iAudHEua" 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 E920D146A84; Wed, 22 May 2024 21:56: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=1716414979; cv=none; b=iORrN7MASLscuqjGcFbxNrvhgSKue7dezSDqD0ImFwYhYnhEzG43TJ+tAjCRVG5R9VcBPS96lhNCLGp/Tu1rNPVegT5/EnnelNX5os/HwjxXy9SVs8hjPtTFO5XJCXcJCiAueKCOOeMRqc8cX5yzU1UPv9toRV0A0x2tiDTDZ7w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716414979; c=relaxed/simple; bh=JtuRxcCvglFcfjf50Tet8nXRDQxwrVBlbIJSMK5vXig=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E/KaW255hX/kcovZRFlE6V1kweG2xWztZlT2kw86HLJL3aXGKB3uXhv3L5fUfAmTHiLGTkfXQ2+wRKBzkmTvbJe2RfnWPT2ndJQ4iFUDvJ2807CM0jG/uSaAorpB1Zi9TtAD1Rqx4kEF6kiRLCZbXQekIbB6Er3MQ0MiORjOhWg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iAudHEua; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1ACD2C4AF08; Wed, 22 May 2024 21:56:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716414978; bh=JtuRxcCvglFcfjf50Tet8nXRDQxwrVBlbIJSMK5vXig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iAudHEua+RG/yoSgYXxU/MothI/bnwiRRNq1aeOcQOGHR0mK52i4cCJIJWbSit3vs yW0fEE6nOnnzYeLA0fqgHvEzMQEmdbCHLrN0/7h391fSk6pi8oJXramn3vBYU5Ih8/ W/7lS1pUaK5G59KTitSugn5wi9mVUHQItXdSfG6dDCCyvZ+t7PWVCcZo0wKd1kjzVs QPQhHAknv8xMqoJdoPKWB4huW/LdEDg927g9lyTXaThMW+tFRRdwXswWFxfvlxhr7I gKLHyL83KOB/MpYERZ/CCvYfUpTSvpMv0EjtpaVqR19k7VpYNhmUqR2oHw1fBYb/7T Logz6RQFc2k3Q== 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, KP Singh , Stephane Eranian , Song Liu , bpf@vger.kernel.org Subject: [PATCH 1/6] perf bpf-filter: Make filters map a single entry hashmap Date: Wed, 22 May 2024 14:56:11 -0700 Message-ID: <20240522215616.762195-2-namhyung@kernel.org> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog In-Reply-To: <20240522215616.762195-1-namhyung@kernel.org> References: <20240522215616.762195-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: 8bit And the value is now an array. This is to support multiple filter entries in the map later. No functional changes intended. Signed-off-by: Namhyung Kim --- tools/perf/util/bpf-filter.c | 81 ++++++++++++++------ tools/perf/util/bpf_skel/sample-filter.h | 3 +- tools/perf/util/bpf_skel/sample_filter.bpf.c | 37 +++++---- 3 files changed, 78 insertions(+), 43 deletions(-) diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index b51544996046..12e9c7dbb4dd 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -87,71 +87,102 @@ static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_expr * int perf_bpf_filter__prepare(struct evsel *evsel) { - int i, x, y, fd; + int i, x, y, fd, ret; struct sample_filter_bpf *skel; struct bpf_program *prog; struct bpf_link *link; struct perf_bpf_filter_expr *expr; + struct perf_bpf_filter_entry *entry; + + entry = calloc(MAX_FILTERS, sizeof(*entry)); + if (entry == NULL) + return -1; skel = sample_filter_bpf__open_and_load(); if (!skel) { pr_err("Failed to load perf sample-filter BPF skeleton\n"); - return -1; + ret = -EPERM; + goto err; } i = 0; fd = bpf_map__fd(skel->maps.filters); list_for_each_entry(expr, &evsel->bpf_filters, list) { - struct perf_bpf_filter_entry entry = { - .op = expr->op, - .part = expr->part, - .flags = expr->sample_flags, - .value = expr->val, - }; + if (check_sample_flags(evsel, expr) < 0) { + ret = -EINVAL; + goto err; + } - if (check_sample_flags(evsel, expr) < 0) - return -1; + if (i == MAX_FILTERS) { + ret = -E2BIG; + goto err; + } - bpf_map_update_elem(fd, &i, &entry, BPF_ANY); + entry[i].op = expr->op; + entry[i].part = expr->part; + entry[i].flags = expr->sample_flags; + entry[i].value = expr->val; i++; if (expr->op == PBF_OP_GROUP_BEGIN) { struct perf_bpf_filter_expr *group; list_for_each_entry(group, &expr->groups, list) { - struct perf_bpf_filter_entry group_entry = { - .op = group->op, - .part = group->part, - .flags = group->sample_flags, - .value = group->val, - }; - bpf_map_update_elem(fd, &i, &group_entry, BPF_ANY); + if (i == MAX_FILTERS) { + ret = -E2BIG; + goto err; + } + + entry[i].op = group->op; + entry[i].part = group->part; + entry[i].flags = group->sample_flags; + entry[i].value = group->val; i++; } - memset(&entry, 0, sizeof(entry)); - entry.op = PBF_OP_GROUP_END; - bpf_map_update_elem(fd, &i, &entry, BPF_ANY); + if (i == MAX_FILTERS) { + ret = -E2BIG; + goto err; + } + + entry[i].op = PBF_OP_GROUP_END; i++; } } - if (i > MAX_FILTERS) { - pr_err("Too many filters: %d (max = %d)\n", i, MAX_FILTERS); - return -1; + if (i < MAX_FILTERS) { + /* to terminate the loop early */ + entry[i].op = PBF_OP_DONE; + i++; + } + + /* The filters map has only one entry for now */ + i = 0; + if (bpf_map_update_elem(fd, &i, entry, BPF_ANY) < 0) { + ret = -errno; + pr_err("Failed to update the filter map\n"); + goto err; } + prog = skel->progs.perf_sample_filter; for (x = 0; x < xyarray__max_x(evsel->core.fd); x++) { for (y = 0; y < xyarray__max_y(evsel->core.fd); y++) { link = bpf_program__attach_perf_event(prog, FD(evsel, x, y)); if (IS_ERR(link)) { pr_err("Failed to attach perf sample-filter program\n"); - return PTR_ERR(link); + ret = PTR_ERR(link); + goto err; } } } + free(entry); evsel->bpf_skel = skel; return 0; + +err: + free(entry); + sample_filter_bpf__destroy(skel); + return ret; } int perf_bpf_filter__destroy(struct evsel *evsel) diff --git a/tools/perf/util/bpf_skel/sample-filter.h b/tools/perf/util/bpf_skel/sample-filter.h index 2e96e1ab084a..cf18f570eef4 100644 --- a/tools/perf/util/bpf_skel/sample-filter.h +++ b/tools/perf/util/bpf_skel/sample-filter.h @@ -14,6 +14,7 @@ enum perf_bpf_filter_op { PBF_OP_AND, PBF_OP_GROUP_BEGIN, PBF_OP_GROUP_END, + PBF_OP_DONE, }; /* BPF map entry for filtering */ @@ -24,4 +25,4 @@ struct perf_bpf_filter_entry { __u64 value; }; -#endif /* PERF_UTIL_BPF_SKEL_SAMPLE_FILTER_H */ \ No newline at end of file +#endif /* PERF_UTIL_BPF_SKEL_SAMPLE_FILTER_H */ diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util/bpf_skel/sample_filter.bpf.c index fb94f5280626..5f17cd6458b7 100644 --- a/tools/perf/util/bpf_skel/sample_filter.bpf.c +++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c @@ -9,10 +9,10 @@ /* BPF map that will be filled by user space */ struct filters { - __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(type, BPF_MAP_TYPE_HASH); __type(key, int); - __type(value, struct perf_bpf_filter_entry); - __uint(max_entries, MAX_FILTERS); + __type(value, struct perf_bpf_filter_entry[MAX_FILTERS]); + __uint(max_entries, 1); } filters SEC(".maps"); int dropped; @@ -144,35 +144,35 @@ int perf_sample_filter(void *ctx) kctx = bpf_cast_to_kern_ctx(ctx); - for (i = 0; i < MAX_FILTERS; i++) { - int key = i; /* needed for verifier :( */ + i = 0; + entry = bpf_map_lookup_elem(&filters, &i); + if (entry == NULL) + goto drop; - entry = bpf_map_lookup_elem(&filters, &key); - if (entry == NULL) - break; - sample_data = perf_get_sample(kctx, entry); + for (i = 0; i < MAX_FILTERS; i++) { + sample_data = perf_get_sample(kctx, &entry[i]); - switch (entry->op) { + switch (entry[i].op) { case PBF_OP_EQ: - CHECK_RESULT(sample_data, ==, entry->value) + CHECK_RESULT(sample_data, ==, entry[i].value) break; case PBF_OP_NEQ: - CHECK_RESULT(sample_data, !=, entry->value) + CHECK_RESULT(sample_data, !=, entry[i].value) break; case PBF_OP_GT: - CHECK_RESULT(sample_data, >, entry->value) + CHECK_RESULT(sample_data, >, entry[i].value) break; case PBF_OP_GE: - CHECK_RESULT(sample_data, >=, entry->value) + CHECK_RESULT(sample_data, >=, entry[i].value) break; case PBF_OP_LT: - CHECK_RESULT(sample_data, <, entry->value) + CHECK_RESULT(sample_data, <, entry[i].value) break; case PBF_OP_LE: - CHECK_RESULT(sample_data, <=, entry->value) + CHECK_RESULT(sample_data, <=, entry[i].value) break; case PBF_OP_AND: - CHECK_RESULT(sample_data, &, entry->value) + CHECK_RESULT(sample_data, &, entry[i].value) break; case PBF_OP_GROUP_BEGIN: in_group = 1; @@ -183,6 +183,9 @@ int perf_sample_filter(void *ctx) goto drop; in_group = 0; break; + case PBF_OP_DONE: + /* no failures so far, accept it */ + return 1; } } /* generate sample data */ -- 2.45.1.288.g0e0cd299f1-goog