blk-mq: fix two misuses on RQF_USE_SCHED
[linux-block.git] / block / blk-stat.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
cf43e6be
JA
2/*
3 * Block stat tracking code
4 *
5 * Copyright (C) 2016 Jens Axboe
6 */
7#include <linux/kernel.h>
34dbad5d 8#include <linux/rculist.h>
cf43e6be
JA
9
10#include "blk-stat.h"
11#include "blk-mq.h"
b9147dd1 12#include "blk.h"
cf43e6be 13
34dbad5d
OS
14struct blk_queue_stats {
15 struct list_head callbacks;
16 spinlock_t lock;
68497092 17 int accounting;
34dbad5d
OS
18};
19
2ecbf456 20void blk_rq_stat_init(struct blk_rq_stat *stat)
34dbad5d
OS
21{
22 stat->min = -1ULL;
23 stat->max = stat->nr_samples = stat->mean = 0;
eca8b53a 24 stat->batch = 0;
cf43e6be
JA
25}
26
eca8b53a 27/* src is a per-cpu stat, mean isn't initialized */
2ecbf456 28void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
cf43e6be
JA
29{
30 if (!src->nr_samples)
31 return;
32
cf43e6be
JA
33 dst->min = min(dst->min, src->min);
34 dst->max = max(dst->max, src->max);
35
eca8b53a
SL
36 dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples,
37 dst->nr_samples + src->nr_samples);
38
cf43e6be
JA
39 dst->nr_samples += src->nr_samples;
40}
41
2ecbf456 42void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value)
cf43e6be 43{
34dbad5d
OS
44 stat->min = min(stat->min, value);
45 stat->max = max(stat->max, value);
34dbad5d 46 stat->batch += value;
eca8b53a 47 stat->nr_samples++;
cf43e6be
JA
48}
49
522a7775 50void blk_stat_add(struct request *rq, u64 now)
cf43e6be 51{
34dbad5d
OS
52 struct request_queue *q = rq->q;
53 struct blk_stat_callback *cb;
54 struct blk_rq_stat *stat;
8148f0b5 55 int bucket, cpu;
522a7775 56 u64 value;
34dbad5d 57
544ccc8d 58 value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0;
34dbad5d 59
a886001c
CH
60 if (req_op(rq) == REQ_OP_READ || req_op(rq) == REQ_OP_WRITE)
61 blk_throtl_stat_add(rq, value);
b9147dd1 62
34dbad5d 63 rcu_read_lock();
8148f0b5 64 cpu = get_cpu();
34dbad5d 65 list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
d3738123
JA
66 if (!blk_stat_is_active(cb))
67 continue;
68
69 bucket = cb->bucket_fn(rq);
70 if (bucket < 0)
71 continue;
72
8148f0b5 73 stat = &per_cpu_ptr(cb->cpu_stat, cpu)[bucket];
2ecbf456 74 blk_rq_stat_add(stat, value);
cf43e6be 75 }
8148f0b5 76 put_cpu();
34dbad5d 77 rcu_read_unlock();
cf43e6be
JA
78}
79
e99e88a9 80static void blk_stat_timer_fn(struct timer_list *t)
cf43e6be 81{
e99e88a9 82 struct blk_stat_callback *cb = from_timer(cb, t, timer);
34dbad5d
OS
83 unsigned int bucket;
84 int cpu;
cf43e6be 85
34dbad5d 86 for (bucket = 0; bucket < cb->buckets; bucket++)
2ecbf456 87 blk_rq_stat_init(&cb->stat[bucket]);
cf43e6be 88
34dbad5d
OS
89 for_each_online_cpu(cpu) {
90 struct blk_rq_stat *cpu_stat;
7cd54aa8 91
34dbad5d
OS
92 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
93 for (bucket = 0; bucket < cb->buckets; bucket++) {
2ecbf456
JB
94 blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]);
95 blk_rq_stat_init(&cpu_stat[bucket]);
cf43e6be 96 }
34dbad5d 97 }
cf43e6be 98
34dbad5d 99 cb->timer_fn(cb);
cf43e6be
JA
100}
101
34dbad5d
OS
102struct blk_stat_callback *
103blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *),
a37244e4 104 int (*bucket_fn)(const struct request *),
34dbad5d 105 unsigned int buckets, void *data)
cf43e6be 106{
34dbad5d 107 struct blk_stat_callback *cb;
cf43e6be 108
34dbad5d
OS
109 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
110 if (!cb)
111 return NULL;
cf43e6be 112
34dbad5d
OS
113 cb->stat = kmalloc_array(buckets, sizeof(struct blk_rq_stat),
114 GFP_KERNEL);
115 if (!cb->stat) {
116 kfree(cb);
117 return NULL;
118 }
119 cb->cpu_stat = __alloc_percpu(buckets * sizeof(struct blk_rq_stat),
120 __alignof__(struct blk_rq_stat));
121 if (!cb->cpu_stat) {
122 kfree(cb->stat);
123 kfree(cb);
124 return NULL;
125 }
cf43e6be 126
34dbad5d
OS
127 cb->timer_fn = timer_fn;
128 cb->bucket_fn = bucket_fn;
129 cb->data = data;
130 cb->buckets = buckets;
e99e88a9 131 timer_setup(&cb->timer, blk_stat_timer_fn, 0);
34dbad5d
OS
132
133 return cb;
cf43e6be
JA
134}
135
34dbad5d
OS
136void blk_stat_add_callback(struct request_queue *q,
137 struct blk_stat_callback *cb)
cf43e6be 138{
34dbad5d 139 unsigned int bucket;
e11d80a8 140 unsigned long flags;
34dbad5d 141 int cpu;
cf43e6be 142
34dbad5d
OS
143 for_each_possible_cpu(cpu) {
144 struct blk_rq_stat *cpu_stat;
cf43e6be 145
34dbad5d
OS
146 cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu);
147 for (bucket = 0; bucket < cb->buckets; bucket++)
2ecbf456 148 blk_rq_stat_init(&cpu_stat[bucket]);
34dbad5d 149 }
cf43e6be 150
e11d80a8 151 spin_lock_irqsave(&q->stats->lock, flags);
34dbad5d 152 list_add_tail_rcu(&cb->list, &q->stats->callbacks);
7dfdbc73 153 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
e11d80a8 154 spin_unlock_irqrestore(&q->stats->lock, flags);
34dbad5d 155}
cf43e6be 156
34dbad5d
OS
157void blk_stat_remove_callback(struct request_queue *q,
158 struct blk_stat_callback *cb)
159{
e11d80a8
TH
160 unsigned long flags;
161
162 spin_lock_irqsave(&q->stats->lock, flags);
34dbad5d 163 list_del_rcu(&cb->list);
68497092 164 if (list_empty(&q->stats->callbacks) && !q->stats->accounting)
7dfdbc73 165 blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
e11d80a8 166 spin_unlock_irqrestore(&q->stats->lock, flags);
cf43e6be 167
34dbad5d 168 del_timer_sync(&cb->timer);
cf43e6be
JA
169}
170
34dbad5d 171static void blk_stat_free_callback_rcu(struct rcu_head *head)
cf43e6be 172{
34dbad5d
OS
173 struct blk_stat_callback *cb;
174
175 cb = container_of(head, struct blk_stat_callback, rcu);
176 free_percpu(cb->cpu_stat);
177 kfree(cb->stat);
178 kfree(cb);
cf43e6be
JA
179}
180
34dbad5d 181void blk_stat_free_callback(struct blk_stat_callback *cb)
cf43e6be 182{
a83b576c
JA
183 if (cb)
184 call_rcu(&cb->rcu, blk_stat_free_callback_rcu);
cf43e6be
JA
185}
186
68497092
JA
187void blk_stat_disable_accounting(struct request_queue *q)
188{
189 unsigned long flags;
190
191 spin_lock_irqsave(&q->stats->lock, flags);
20de765f 192 if (!--q->stats->accounting && list_empty(&q->stats->callbacks))
68497092
JA
193 blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
194 spin_unlock_irqrestore(&q->stats->lock, flags);
195}
196EXPORT_SYMBOL_GPL(blk_stat_disable_accounting);
197
b9147dd1
SL
198void blk_stat_enable_accounting(struct request_queue *q)
199{
e11d80a8
TH
200 unsigned long flags;
201
202 spin_lock_irqsave(&q->stats->lock, flags);
20de765f 203 if (!q->stats->accounting++ && list_empty(&q->stats->callbacks))
68497092 204 blk_queue_flag_set(QUEUE_FLAG_STATS, q);
e11d80a8 205 spin_unlock_irqrestore(&q->stats->lock, flags);
b9147dd1 206}
f8232f29 207EXPORT_SYMBOL_GPL(blk_stat_enable_accounting);
b9147dd1 208
34dbad5d 209struct blk_queue_stats *blk_alloc_queue_stats(void)
cf43e6be 210{
34dbad5d
OS
211 struct blk_queue_stats *stats;
212
213 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
214 if (!stats)
215 return NULL;
216
217 INIT_LIST_HEAD(&stats->callbacks);
218 spin_lock_init(&stats->lock);
68497092 219 stats->accounting = 0;
34dbad5d
OS
220
221 return stats;
222}
223
224void blk_free_queue_stats(struct blk_queue_stats *stats)
225{
226 if (!stats)
227 return;
228
229 WARN_ON(!list_empty(&stats->callbacks));
cf43e6be 230
34dbad5d 231 kfree(stats);
cf43e6be 232}