xref: /linux/samples/bpf/test_current_task_under_cgroup.bpf.c (revision d4fffba4d04b8d605ff07f1ed987399f6af0ad5b)
1 /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 
8 #include "vmlinux.h"
9 #include <linux/version.h>
10 #include <bpf/bpf_helpers.h>
11 
12 struct {
13 	__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
14 	__uint(key_size, sizeof(u32));
15 	__uint(value_size, sizeof(u32));
16 	__uint(max_entries, 1);
17 } cgroup_map SEC(".maps");
18 
19 struct {
20 	__uint(type, BPF_MAP_TYPE_ARRAY);
21 	__type(key, u32);
22 	__type(value, u64);
23 	__uint(max_entries, 1);
24 } perf_map SEC(".maps");
25 
26 /* Writes the last PID that called sync to a map at index 0 */
27 SEC("ksyscall/sync")
28 int bpf_prog1(struct pt_regs *ctx)
29 {
30 	u64 pid = bpf_get_current_pid_tgid();
31 	int idx = 0;
32 
33 	if (!bpf_current_task_under_cgroup(&cgroup_map, 0))
34 		return 0;
35 
36 	bpf_map_update_elem(&perf_map, &idx, &pid, BPF_ANY);
37 	return 0;
38 }
39 
40 char _license[] SEC("license") = "GPL";
41 u32 _version SEC("version") = LINUX_VERSION_CODE;
42