linux/tools/testing/selftests/bpf/progs/test_mmap.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2// Copyright (c) 2019 Facebook
   3
   4#include <linux/bpf.h>
   5#include <stdint.h>
   6#include <bpf/bpf_helpers.h>
   7
   8char _license[] SEC("license") = "GPL";
   9
  10struct {
  11        __uint(type, BPF_MAP_TYPE_ARRAY);
  12        __uint(max_entries, 4096);
  13        __uint(map_flags, BPF_F_MMAPABLE | BPF_F_RDONLY_PROG);
  14        __type(key, __u32);
  15        __type(value, char);
  16} rdonly_map SEC(".maps");
  17
  18struct {
  19        __uint(type, BPF_MAP_TYPE_ARRAY);
  20        __uint(max_entries, 512 * 4); /* at least 4 pages of data */
  21        __uint(map_flags, BPF_F_MMAPABLE);
  22        __type(key, __u32);
  23        __type(value, __u64);
  24} data_map SEC(".maps");
  25
  26__u64 in_val = 0;
  27__u64 out_val = 0;
  28
  29SEC("raw_tracepoint/sys_enter")
  30int test_mmap(void *ctx)
  31{
  32        int zero = 0, one = 1, two = 2, far = 1500;
  33        __u64 val, *p;
  34
  35        out_val = in_val;
  36
  37        /* data_map[2] = in_val; */
  38        bpf_map_update_elem(&data_map, &two, (const void *)&in_val, 0);
  39
  40        /* data_map[1] = data_map[0] * 2; */
  41        p = bpf_map_lookup_elem(&data_map, &zero);
  42        if (p) {
  43                val = (*p) * 2;
  44                bpf_map_update_elem(&data_map, &one, &val, 0);
  45        }
  46
  47        /* data_map[far] = in_val * 3; */
  48        val = in_val * 3;
  49        bpf_map_update_elem(&data_map, &far, &val, 0);
  50
  51        return 0;
  52}
  53
  54