cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

pe_preserve_elems.c (1933B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/* Copyright (c) 2019 Facebook */
      3#include <test_progs.h>
      4#include <linux/bpf.h>
      5#include "test_pe_preserve_elems.skel.h"
      6
      7static int duration;
      8
      9static void test_one_map(struct bpf_map *map, struct bpf_program *prog,
     10			 bool has_share_pe)
     11{
     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,
     18	};
     19
     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"))
     23		return;
     24
     25	err = bpf_map_update_elem(mfd, &key, &pfd, BPF_ANY);
     26	close(pfd);
     27	if (CHECK(err < 0, "bpf_map_update_elem", "failed\n"))
     28		return;
     29
     30	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
     31	if (CHECK(err < 0, "bpf_prog_test_run_opts", "failed\n"))
     32		return;
     33	if (CHECK(opts.retval != 0, "bpf_perf_event_read_value",
     34		  "failed with %d\n", opts.retval))
     35		return;
     36
     37	/* closing mfd, prog still holds a reference on map */
     38	close(mfd);
     39
     40	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
     41	if (CHECK(err < 0, "bpf_prog_test_run_opts", "failed\n"))
     42		return;
     43
     44	if (has_share_pe) {
     45		CHECK(opts.retval != 0, "bpf_perf_event_read_value",
     46		      "failed with %d\n", opts.retval);
     47	} else {
     48		CHECK(opts.retval != -ENOENT, "bpf_perf_event_read_value",
     49		      "should have failed with %d, but got %d\n", -ENOENT,
     50		      opts.retval);
     51	}
     52}
     53
     54void test_pe_preserve_elems(void)
     55{
     56	struct test_pe_preserve_elems *skel;
     57
     58	skel = test_pe_preserve_elems__open_and_load();
     59	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
     60		return;
     61
     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);
     64
     65	test_pe_preserve_elems__destroy(skel);
     66}