From patchwork Tue Sep 29 08:47:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 259975 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B88E4C4727F for ; Tue, 29 Sep 2020 08:48:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 696132067D for ; Tue, 29 Sep 2020 08:48:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="iuH70HO4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727731AbgI2IsP (ORCPT ); Tue, 29 Sep 2020 04:48:15 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:3356 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725372AbgI2IsP (ORCPT ); Tue, 29 Sep 2020 04:48:15 -0400 Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 08T8dmgo020572 for ; Tue, 29 Sep 2020 01:48:14 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=f1DRt8DSh7xvwsbLwZtA/reljbesfZWQwqhLxzP0MJY=; b=iuH70HO4HlPxk8dd7EZ+wP1NRWtlHsW+aXTzKUjr99w2M9RnwVTKHGj2jAycU09EUsau MIv8SdtzeNHf82riPBxfN46hx5X0wWg6vU6zC6mwWrbl9p0Fj8zy6W1TKAUY2FMNLRiz /3PB7tuTtesLoAqU5AN839e5cjkNDCR/4cQ= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 33t3cpbv3w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 29 Sep 2020 01:48:14 -0700 Received: from intmgw004.08.frc2.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Tue, 29 Sep 2020 01:48:13 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 165DB62E5765; Tue, 29 Sep 2020 01:48:06 -0700 (PDT) From: Song Liu To: , CC: , , , , , Song Liu Subject: [PATCH bpf-next 2/2] selftests/bpf: add tests for BPF_F_SHARE_PE Date: Tue, 29 Sep 2020 01:47:50 -0700 Message-ID: <20200929084750.419168-3-songliubraving@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200929084750.419168-1-songliubraving@fb.com> References: <20200929084750.419168-1-songliubraving@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-09-29_01:2020-09-29,2020-09-29 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 malwarescore=0 clxscore=1015 impostorscore=0 mlxlogscore=999 suspectscore=0 priorityscore=1501 lowpriorityscore=0 spamscore=0 adultscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2009290080 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add tests for perf event array with and without BPF_F_SHARE_PE. Add a perf event to array via fd mfd. Without BPF_F_SHARE_PE, the perf event is removed when mfd is closed. With BPF_F_SHARE_PE, the perf event is removed when the map is freed. Signed-off-by: Song Liu --- .../bpf/prog_tests/perf_event_share.c | 68 +++++++++++++++++++ .../bpf/progs/test_perf_event_share.c | 44 ++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_event_share.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_event_share.c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_event_share.c b/tools/testing/selftests/bpf/prog_tests/perf_event_share.c new file mode 100644 index 0000000000000..a37cfdd047ea6 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_event_share.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2019 Facebook */ +#include +#include +#include "test_perf_event_share.skel.h" + +static int duration; + +static void test_one_map(struct bpf_map *map, struct bpf_program *prog, + bool has_share_pe) +{ + int err, key = 0, pfd = -1, mfd = bpf_map__fd(map); + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts); + struct perf_event_attr attr = { + .size = sizeof(struct perf_event_attr), + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_CPU_CLOCK, + }; + + pfd = syscall(__NR_perf_event_open, &attr, 0 /* pid */, + -1 /* cpu 0 */, -1 /* group id */, 0 /* flags */); + if (CHECK(pfd < 0, "perf_event_open", "failed\n")) + return; + + err = bpf_map_update_elem(mfd, &key, &pfd, BPF_ANY); + if (CHECK(err < 0, "bpf_map_update_elem", "failed\n")) + goto cleanup; + + err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts); + if (CHECK(err < 0, "bpf_prog_test_run_opts", "failed\n")) + goto cleanup; + if (CHECK(opts.retval != 0, "bpf_perf_event_read_value", + "failed with %d\n", opts.retval)) + goto cleanup; + + /* closing mfd, prog still holds a reference on map */ + close(mfd); + + err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts); + if (CHECK(err < 0, "bpf_prog_test_run_opts", "failed\n")) + goto cleanup; + + if (has_share_pe) { + CHECK(opts.retval != 0, "bpf_perf_event_read_value", + "failed with %d\n", opts.retval); + } else { + CHECK(opts.retval != -ENOENT, "bpf_perf_event_read_value", + "should have failed with %d, but got %d\n", -ENOENT, + opts.retval); + } + +cleanup: + close(pfd); +} + +void test_perf_event_share(void) +{ + struct test_perf_event_share *skel; + + skel = test_perf_event_share__open_and_load(); + if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + return; + + test_one_map(skel->maps.array_1, skel->progs.read_array_1, false); + test_one_map(skel->maps.array_2, skel->progs.read_array_2, true); + + test_perf_event_share__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_perf_event_share.c b/tools/testing/selftests/bpf/progs/test_perf_event_share.c new file mode 100644 index 0000000000000..005bfe375352f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_perf_event_share.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Facebook +#include "vmlinux.h" +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); +} array_1 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(map_flags, BPF_F_SHARE_PE); /* array_2 has BPF_F_SHARE_PE */ +} array_2 SEC(".maps"); + +SEC("raw_tp/sched_switch") +int BPF_PROG(read_array_1) +{ + struct bpf_perf_event_value val; + long ret; + + ret = bpf_perf_event_read_value(&array_1, 0, &val, sizeof(val)); + bpf_printk("read_array_1 returns %ld", ret); + return ret; +} + +SEC("raw_tp/task_rename") +int BPF_PROG(read_array_2) +{ + struct bpf_perf_event_value val; + long ret; + + ret = bpf_perf_event_read_value(&array_2, 0, &val, sizeof(val)); + bpf_printk("read_array_2 returns %ld", ret); + return ret; +} + +char LICENSE[] SEC("license") = "GPL";