2015-03-26 03:49:26 +08:00
|
|
|
/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/version.h>
|
|
|
|
#include <uapi/linux/bpf.h>
|
2020-01-20 21:06:49 +08:00
|
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
#include <bpf/bpf_tracing.h>
|
2015-03-26 03:49:26 +08:00
|
|
|
|
|
|
|
struct pair {
|
|
|
|
u64 val;
|
|
|
|
u64 ip;
|
|
|
|
};
|
|
|
|
|
2020-05-16 12:06:08 +08:00
|
|
|
struct {
|
|
|
|
__uint(type, BPF_MAP_TYPE_HASH);
|
|
|
|
__type(key, long);
|
|
|
|
__type(value, struct pair);
|
|
|
|
__uint(max_entries, 1000000);
|
|
|
|
} my_map SEC(".maps");
|
2015-03-26 03:49:26 +08:00
|
|
|
|
|
|
|
/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
|
|
|
|
* example will no longer be meaningful
|
|
|
|
*/
|
|
|
|
SEC("kprobe/kmem_cache_free")
|
|
|
|
int bpf_prog1(struct pt_regs *ctx)
|
|
|
|
{
|
2015-07-06 22:20:07 +08:00
|
|
|
long ptr = PT_REGS_PARM2(ctx);
|
2015-03-26 03:49:26 +08:00
|
|
|
|
|
|
|
bpf_map_delete_elem(&my_map, &ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("kretprobe/kmem_cache_alloc_node")
|
|
|
|
int bpf_prog2(struct pt_regs *ctx)
|
|
|
|
{
|
2015-07-06 22:20:07 +08:00
|
|
|
long ptr = PT_REGS_RC(ctx);
|
2015-03-26 03:49:26 +08:00
|
|
|
long ip = 0;
|
|
|
|
|
|
|
|
/* get ip address of kmem_cache_alloc_node() caller */
|
2016-04-05 01:01:34 +08:00
|
|
|
BPF_KRETPROBE_READ_RET_IP(ip, ctx);
|
2015-03-26 03:49:26 +08:00
|
|
|
|
|
|
|
struct pair v = {
|
|
|
|
.val = bpf_ktime_get_ns(),
|
|
|
|
.ip = ip,
|
|
|
|
};
|
|
|
|
|
|
|
|
bpf_map_update_elem(&my_map, &ptr, &v, BPF_ANY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
u32 _version SEC("version") = LINUX_VERSION_CODE;
|