On 9/29/20 10:47 AM, Song Liu wrote:
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 <songliubraving@xxxxxx>
[...]
+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);
Why holding pfd until the end? The map should already hold a ref after update. So you should be able to just do ... err = bpf_map_update_elem(mfd, &key, &pfd, BPF_ANY); close(pfd); ... and simplify cleanup.
+} + +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); +}