xref: /aosp_15_r20/external/bcc/libbpf-tools/bitesize.bpf.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Wenbo Zhang
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/bpf_core_read.h>
7 #include "bitesize.h"
8 #include "bits.bpf.h"
9 #include "core_fixes.bpf.h"
10 
11 const volatile char targ_comm[TASK_COMM_LEN] = {};
12 const volatile bool filter_dev = false;
13 const volatile __u32 targ_dev = 0;
14 
15 extern __u32 LINUX_KERNEL_VERSION __kconfig;
16 
17 struct {
18 	__uint(type, BPF_MAP_TYPE_HASH);
19 	__uint(max_entries, 10240);
20 	__type(key, struct hist_key);
21 	__type(value, struct hist);
22 } hists SEC(".maps");
23 
24 static struct hist initial_hist;
25 
comm_allowed(const char * comm)26 static __always_inline bool comm_allowed(const char *comm)
27 {
28 	int i;
29 
30 	for (i = 0; targ_comm[i] != '\0' && i < TASK_COMM_LEN; i++) {
31 		if (comm[i] != targ_comm[i])
32 			return false;
33 	}
34 	return true;
35 }
36 
trace_rq_issue(struct request * rq)37 static int trace_rq_issue(struct request *rq)
38 {
39 	struct hist_key hkey;
40 	struct hist *histp;
41 	u64 slot;
42 
43 	if (filter_dev) {
44 		struct gendisk *disk = get_disk(rq);
45 		u32 dev;
46 
47 		dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
48 				BPF_CORE_READ(disk, first_minor)) : 0;
49 		if (targ_dev != dev)
50 			return 0;
51 	}
52 	bpf_get_current_comm(&hkey.comm, sizeof(hkey.comm));
53 	if (!comm_allowed(hkey.comm))
54 		return 0;
55 
56 	histp = bpf_map_lookup_elem(&hists, &hkey);
57 	if (!histp) {
58 		bpf_map_update_elem(&hists, &hkey, &initial_hist, 0);
59 		histp = bpf_map_lookup_elem(&hists, &hkey);
60 		if (!histp)
61 			return 0;
62 	}
63 	slot = log2l(rq->__data_len / 1024);
64 	if (slot >= MAX_SLOTS)
65 		slot = MAX_SLOTS - 1;
66 	__sync_fetch_and_add(&histp->slots[slot], 1);
67 
68 	return 0;
69 }
70 
71 SEC("tp_btf/block_rq_issue")
BPF_PROG(block_rq_issue)72 int BPF_PROG(block_rq_issue)
73 {
74 	/**
75 	 * commit a54895fa (v5.11-rc1) changed tracepoint argument list
76 	 * from TP_PROTO(struct request_queue *q, struct request *rq)
77 	 * to TP_PROTO(struct request *rq)
78 	 */
79 	if (LINUX_KERNEL_VERSION >= KERNEL_VERSION(5, 11, 0))
80 		return trace_rq_issue((void *)ctx[0]);
81 	else
82 		return trace_rq_issue((void *)ctx[1]);
83 }
84 
85 char LICENSE[] SEC("license") = "GPL";
86