1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
5 #include "test_pe_preserve_elems.skel.h"
9 static void test_one_map(struct bpf_map
*map
, struct bpf_program
*prog
,
12 int err
, key
= 0, pfd
= -1, mfd
= bpf_map__fd(map
);
13 DECLARE_LIBBPF_OPTS(bpf_test_run_opts
, opts
);
14 struct perf_event_attr attr
= {
15 .size
= sizeof(struct perf_event_attr
),
16 .type
= PERF_TYPE_SOFTWARE
,
17 .config
= PERF_COUNT_SW_CPU_CLOCK
,
20 pfd
= syscall(__NR_perf_event_open
, &attr
, 0 /* pid */,
21 -1 /* cpu 0 */, -1 /* group id */, 0 /* flags */);
22 if (CHECK(pfd
< 0, "perf_event_open", "failed\n"))
25 err
= bpf_map_update_elem(mfd
, &key
, &pfd
, BPF_ANY
);
27 if (CHECK(err
< 0, "bpf_map_update_elem", "failed\n"))
30 err
= bpf_prog_test_run_opts(bpf_program__fd(prog
), &opts
);
31 if (CHECK(err
< 0, "bpf_prog_test_run_opts", "failed\n"))
33 if (CHECK(opts
.retval
!= 0, "bpf_perf_event_read_value",
34 "failed with %d\n", opts
.retval
))
37 /* closing mfd, prog still holds a reference on map */
40 err
= bpf_prog_test_run_opts(bpf_program__fd(prog
), &opts
);
41 if (CHECK(err
< 0, "bpf_prog_test_run_opts", "failed\n"))
45 CHECK(opts
.retval
!= 0, "bpf_perf_event_read_value",
46 "failed with %d\n", opts
.retval
);
48 CHECK(opts
.retval
!= -ENOENT
, "bpf_perf_event_read_value",
49 "should have failed with %d, but got %d\n", -ENOENT
,
54 void test_pe_preserve_elems(void)
56 struct test_pe_preserve_elems
*skel
;
58 skel
= test_pe_preserve_elems__open_and_load();
59 if (CHECK(!skel
, "skel_open", "failed to open skeleton\n"))
62 test_one_map(skel
->maps
.array_1
, skel
->progs
.read_array_1
, false);
63 test_one_map(skel
->maps
.array_2
, skel
->progs
.read_array_2
, true);
65 test_pe_preserve_elems__destroy(skel
);