• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_core_read.h>
6 #include <bpf/bpf_tracing.h>
7 #include "biolatency.h"
8 #include "bits.bpf.h"
9 
10 #define MAX_ENTRIES	10240
11 
12 extern int LINUX_KERNEL_VERSION __kconfig;
13 
14 const volatile bool filter_cg = false;
15 const volatile bool targ_per_disk = false;
16 const volatile bool targ_per_flag = false;
17 const volatile bool targ_queued = false;
18 const volatile bool targ_ms = false;
19 const volatile bool filter_dev = false;
20 const volatile __u32 targ_dev = 0;
21 
22 struct {
23 	__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
24 	__type(key, u32);
25 	__type(value, u32);
26 	__uint(max_entries, 1);
27 } cgroup_map SEC(".maps");
28 
29 struct {
30 	__uint(type, BPF_MAP_TYPE_HASH);
31 	__uint(max_entries, MAX_ENTRIES);
32 	__type(key, struct request *);
33 	__type(value, u64);
34 	__uint(map_flags, BPF_F_NO_PREALLOC);
35 } start SEC(".maps");
36 
37 static struct hist initial_hist;
38 
39 struct {
40 	__uint(type, BPF_MAP_TYPE_HASH);
41 	__uint(max_entries, MAX_ENTRIES);
42 	__type(key, struct hist_key);
43 	__type(value, struct hist);
44 	__uint(map_flags, BPF_F_NO_PREALLOC);
45 } hists SEC(".maps");
46 
47 static __always_inline
trace_rq_start(struct request * rq,int issue)48 int trace_rq_start(struct request *rq, int issue)
49 {
50 	if (issue && targ_queued && BPF_CORE_READ(rq->q, elevator))
51 		return 0;
52 
53 	u64 ts = bpf_ktime_get_ns();
54 
55 	if (filter_dev) {
56 		struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
57 		u32 dev;
58 
59 		dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
60 				BPF_CORE_READ(disk, first_minor)) : 0;
61 		if (targ_dev != dev)
62 			return 0;
63 	}
64 	bpf_map_update_elem(&start, &rq, &ts, 0);
65 	return 0;
66 }
67 
68 SEC("tp_btf/block_rq_insert")
block_rq_insert(u64 * ctx)69 int block_rq_insert(u64 *ctx)
70 {
71 	if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
72 		return 0;
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, 10, 0))
80 		return trace_rq_start((void *)ctx[1], false);
81 	else
82 		return trace_rq_start((void *)ctx[0], false);
83 }
84 
85 SEC("tp_btf/block_rq_issue")
block_rq_issue(u64 * ctx)86 int block_rq_issue(u64 *ctx)
87 {
88 	if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
89 		return 0;
90 
91 	/**
92 	 * commit a54895fa (v5.11-rc1) changed tracepoint argument list
93 	 * from TP_PROTO(struct request_queue *q, struct request *rq)
94 	 * to TP_PROTO(struct request *rq)
95 	 */
96 	if (LINUX_KERNEL_VERSION <= KERNEL_VERSION(5, 10, 0))
97 		return trace_rq_start((void *)ctx[1], true);
98 	else
99 		return trace_rq_start((void *)ctx[0], true);
100 }
101 
102 SEC("tp_btf/block_rq_complete")
BPF_PROG(block_rq_complete,struct request * rq,int error,unsigned int nr_bytes)103 int BPF_PROG(block_rq_complete, struct request *rq, int error,
104 	unsigned int nr_bytes)
105 {
106 	if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
107 		return 0;
108 
109 	u64 slot, *tsp, ts = bpf_ktime_get_ns();
110 	struct hist_key hkey = {};
111 	struct hist *histp;
112 	s64 delta;
113 
114 	tsp = bpf_map_lookup_elem(&start, &rq);
115 	if (!tsp)
116 		return 0;
117 	delta = (s64)(ts - *tsp);
118 	if (delta < 0)
119 		goto cleanup;
120 
121 	if (targ_per_disk) {
122 		struct gendisk *disk = BPF_CORE_READ(rq, rq_disk);
123 
124 		hkey.dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
125 					BPF_CORE_READ(disk, first_minor)) : 0;
126 	}
127 	if (targ_per_flag)
128 		hkey.cmd_flags = rq->cmd_flags;
129 
130 	histp = bpf_map_lookup_elem(&hists, &hkey);
131 	if (!histp) {
132 		bpf_map_update_elem(&hists, &hkey, &initial_hist, 0);
133 		histp = bpf_map_lookup_elem(&hists, &hkey);
134 		if (!histp)
135 			goto cleanup;
136 	}
137 
138 	if (targ_ms)
139 		delta /= 1000000U;
140 	else
141 		delta /= 1000U;
142 	slot = log2l(delta);
143 	if (slot >= MAX_SLOTS)
144 		slot = MAX_SLOTS - 1;
145 	__sync_fetch_and_add(&histp->slots[slot], 1);
146 
147 cleanup:
148 	bpf_map_delete_elem(&start, &rq);
149 	return 0;
150 }
151 
152 char LICENSE[] SEC("license") = "GPL";
153