Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  */
0007 #include <linux/ptrace.h>
0008 #include <linux/version.h>
0009 #include <uapi/linux/bpf.h>
0010 #include <bpf/bpf_helpers.h>
0011 #include <bpf/bpf_tracing.h>
0012 
0013 struct pair {
0014     u64 val;
0015     u64 ip;
0016 };
0017 
0018 struct {
0019     __uint(type, BPF_MAP_TYPE_HASH);
0020     __type(key, long);
0021     __type(value, struct pair);
0022     __uint(max_entries, 1000000);
0023 } my_map SEC(".maps");
0024 
0025 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
0026  * example will no longer be meaningful
0027  */
0028 SEC("kprobe/kmem_cache_free")
0029 int bpf_prog1(struct pt_regs *ctx)
0030 {
0031     long ptr = PT_REGS_PARM2(ctx);
0032 
0033     bpf_map_delete_elem(&my_map, &ptr);
0034     return 0;
0035 }
0036 
0037 SEC("kretprobe/kmem_cache_alloc_node")
0038 int bpf_prog2(struct pt_regs *ctx)
0039 {
0040     long ptr = PT_REGS_RC(ctx);
0041     long ip = 0;
0042 
0043     /* get ip address of kmem_cache_alloc_node() caller */
0044     BPF_KRETPROBE_READ_RET_IP(ip, ctx);
0045 
0046     struct pair v = {
0047         .val = bpf_ktime_get_ns(),
0048         .ip = ip,
0049     };
0050 
0051     bpf_map_update_elem(&my_map, &ptr, &v, BPF_ANY);
0052     return 0;
0053 }
0054 char _license[] SEC("license") = "GPL";
0055 u32 _version SEC("version") = LINUX_VERSION_CODE;