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