Christoph Hellwig | 3dcf60b | 2019-04-30 14:42:43 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Block stat tracking code |
| 4 | * |
| 5 | * Copyright (C) 2016 Jens Axboe |
| 6 | */ |
| 7 | #include <linux/kernel.h> |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 8 | #include <linux/rculist.h> |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 9 | #include <linux/blk-mq.h> |
| 10 | |
| 11 | #include "blk-stat.h" |
| 12 | #include "blk-mq.h" |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 13 | #include "blk.h" |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 14 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 15 | struct blk_queue_stats { |
| 16 | struct list_head callbacks; |
| 17 | spinlock_t lock; |
Jens Axboe | 6849709 | 2021-12-14 17:23:05 -0700 | [diff] [blame] | 18 | int accounting; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 21 | void blk_rq_stat_init(struct blk_rq_stat *stat) |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 22 | { |
| 23 | stat->min = -1ULL; |
| 24 | stat->max = stat->nr_samples = stat->mean = 0; |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 25 | stat->batch = 0; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 28 | /* src is a per-cpu stat, mean isn't initialized */ |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 29 | void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 30 | { |
| 31 | if (!src->nr_samples) |
| 32 | return; |
| 33 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 34 | dst->min = min(dst->min, src->min); |
| 35 | dst->max = max(dst->max, src->max); |
| 36 | |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 37 | dst->mean = div_u64(src->batch + dst->mean * dst->nr_samples, |
| 38 | dst->nr_samples + src->nr_samples); |
| 39 | |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 40 | dst->nr_samples += src->nr_samples; |
| 41 | } |
| 42 | |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 43 | void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 44 | { |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 45 | stat->min = min(stat->min, value); |
| 46 | stat->max = max(stat->max, value); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 47 | stat->batch += value; |
Shaohua Li | eca8b53 | 2017-10-06 17:55:59 -0700 | [diff] [blame] | 48 | stat->nr_samples++; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Omar Sandoval | 522a777 | 2018-05-09 02:08:53 -0700 | [diff] [blame] | 51 | void blk_stat_add(struct request *rq, u64 now) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 52 | { |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 53 | struct request_queue *q = rq->q; |
| 54 | struct blk_stat_callback *cb; |
| 55 | struct blk_rq_stat *stat; |
Pavel Begunkov | 8148f0b | 2019-10-08 00:16:51 +0300 | [diff] [blame] | 56 | int bucket, cpu; |
Omar Sandoval | 522a777 | 2018-05-09 02:08:53 -0700 | [diff] [blame] | 57 | u64 value; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 58 | |
Omar Sandoval | 544ccc8d | 2018-05-09 02:08:50 -0700 | [diff] [blame] | 59 | value = (now >= rq->io_start_time_ns) ? now - rq->io_start_time_ns : 0; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 60 | |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 61 | blk_throtl_stat_add(rq, value); |
| 62 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 63 | rcu_read_lock(); |
Pavel Begunkov | 8148f0b | 2019-10-08 00:16:51 +0300 | [diff] [blame] | 64 | cpu = get_cpu(); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 65 | list_for_each_entry_rcu(cb, &q->stats->callbacks, list) { |
Jens Axboe | d373812 | 2017-05-09 11:39:56 -0600 | [diff] [blame] | 66 | if (!blk_stat_is_active(cb)) |
| 67 | continue; |
| 68 | |
| 69 | bucket = cb->bucket_fn(rq); |
| 70 | if (bucket < 0) |
| 71 | continue; |
| 72 | |
Pavel Begunkov | 8148f0b | 2019-10-08 00:16:51 +0300 | [diff] [blame] | 73 | stat = &per_cpu_ptr(cb->cpu_stat, cpu)[bucket]; |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 74 | blk_rq_stat_add(stat, value); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 75 | } |
Pavel Begunkov | 8148f0b | 2019-10-08 00:16:51 +0300 | [diff] [blame] | 76 | put_cpu(); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 77 | rcu_read_unlock(); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 80 | static void blk_stat_timer_fn(struct timer_list *t) |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 81 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 82 | struct blk_stat_callback *cb = from_timer(cb, t, timer); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 83 | unsigned int bucket; |
| 84 | int cpu; |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 85 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 86 | for (bucket = 0; bucket < cb->buckets; bucket++) |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 87 | blk_rq_stat_init(&cb->stat[bucket]); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 88 | |
| 89 | for_each_online_cpu(cpu) { |
| 90 | struct blk_rq_stat *cpu_stat; |
| 91 | |
| 92 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
| 93 | for (bucket = 0; bucket < cb->buckets; bucket++) { |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 94 | blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]); |
| 95 | blk_rq_stat_init(&cpu_stat[bucket]); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 96 | } |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 99 | cb->timer_fn(cb); |
| 100 | } |
| 101 | |
| 102 | struct blk_stat_callback * |
| 103 | blk_stat_alloc_callback(void (*timer_fn)(struct blk_stat_callback *), |
Stephen Bates | a37244e | 2017-04-20 15:29:16 -0600 | [diff] [blame] | 104 | int (*bucket_fn)(const struct request *), |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 105 | unsigned int buckets, void *data) |
| 106 | { |
| 107 | struct blk_stat_callback *cb; |
| 108 | |
| 109 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); |
| 110 | if (!cb) |
| 111 | return NULL; |
| 112 | |
| 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 | } |
| 126 | |
| 127 | cb->timer_fn = timer_fn; |
| 128 | cb->bucket_fn = bucket_fn; |
| 129 | cb->data = data; |
| 130 | cb->buckets = buckets; |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 131 | timer_setup(&cb->timer, blk_stat_timer_fn, 0); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 132 | |
| 133 | return cb; |
| 134 | } |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 135 | |
| 136 | void blk_stat_add_callback(struct request_queue *q, |
| 137 | struct blk_stat_callback *cb) |
| 138 | { |
| 139 | unsigned int bucket; |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 140 | unsigned long flags; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 141 | int cpu; |
| 142 | |
| 143 | for_each_possible_cpu(cpu) { |
| 144 | struct blk_rq_stat *cpu_stat; |
| 145 | |
| 146 | cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); |
| 147 | for (bucket = 0; bucket < cb->buckets; bucket++) |
Josef Bacik | 2ecbf45 | 2018-07-03 11:14:57 -0400 | [diff] [blame] | 148 | blk_rq_stat_init(&cpu_stat[bucket]); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 151 | spin_lock_irqsave(&q->stats->lock, flags); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 152 | list_add_tail_rcu(&cb->list, &q->stats->callbacks); |
Bart Van Assche | 7dfdbc7 | 2018-03-07 17:10:05 -0800 | [diff] [blame] | 153 | blk_queue_flag_set(QUEUE_FLAG_STATS, q); |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 154 | spin_unlock_irqrestore(&q->stats->lock, flags); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 155 | } |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 156 | |
| 157 | void blk_stat_remove_callback(struct request_queue *q, |
| 158 | struct blk_stat_callback *cb) |
| 159 | { |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 160 | unsigned long flags; |
| 161 | |
| 162 | spin_lock_irqsave(&q->stats->lock, flags); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 163 | list_del_rcu(&cb->list); |
Jens Axboe | 6849709 | 2021-12-14 17:23:05 -0700 | [diff] [blame] | 164 | if (list_empty(&q->stats->callbacks) && !q->stats->accounting) |
Bart Van Assche | 7dfdbc7 | 2018-03-07 17:10:05 -0800 | [diff] [blame] | 165 | blk_queue_flag_clear(QUEUE_FLAG_STATS, q); |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 166 | spin_unlock_irqrestore(&q->stats->lock, flags); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 167 | |
| 168 | del_timer_sync(&cb->timer); |
| 169 | } |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 170 | |
| 171 | static void blk_stat_free_callback_rcu(struct rcu_head *head) |
| 172 | { |
| 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); |
| 179 | } |
| 180 | |
| 181 | void blk_stat_free_callback(struct blk_stat_callback *cb) |
| 182 | { |
Jens Axboe | a83b576 | 2017-03-21 17:20:01 -0600 | [diff] [blame] | 183 | if (cb) |
| 184 | call_rcu(&cb->rcu, blk_stat_free_callback_rcu); |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 185 | } |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 186 | |
Jens Axboe | 6849709 | 2021-12-14 17:23:05 -0700 | [diff] [blame] | 187 | void blk_stat_disable_accounting(struct request_queue *q) |
| 188 | { |
| 189 | unsigned long flags; |
| 190 | |
| 191 | spin_lock_irqsave(&q->stats->lock, flags); |
| 192 | if (!--q->stats->accounting) |
| 193 | blk_queue_flag_clear(QUEUE_FLAG_STATS, q); |
| 194 | spin_unlock_irqrestore(&q->stats->lock, flags); |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(blk_stat_disable_accounting); |
| 197 | |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 198 | void blk_stat_enable_accounting(struct request_queue *q) |
| 199 | { |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 200 | unsigned long flags; |
| 201 | |
| 202 | spin_lock_irqsave(&q->stats->lock, flags); |
Jens Axboe | 6849709 | 2021-12-14 17:23:05 -0700 | [diff] [blame] | 203 | if (!q->stats->accounting++) |
| 204 | blk_queue_flag_set(QUEUE_FLAG_STATS, q); |
Tejun Heo | e11d80a | 2020-09-01 14:52:32 -0400 | [diff] [blame] | 205 | spin_unlock_irqrestore(&q->stats->lock, flags); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 206 | } |
Omar Sandoval | f8232f2 | 2018-09-27 15:55:52 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(blk_stat_enable_accounting); |
Shaohua Li | b9147dd | 2017-03-27 15:19:42 -0700 | [diff] [blame] | 208 | |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 209 | struct blk_queue_stats *blk_alloc_queue_stats(void) |
| 210 | { |
| 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); |
Jens Axboe | 6849709 | 2021-12-14 17:23:05 -0700 | [diff] [blame] | 219 | stats->accounting = 0; |
Omar Sandoval | 34dbad5 | 2017-03-21 08:56:08 -0700 | [diff] [blame] | 220 | |
| 221 | return stats; |
| 222 | } |
| 223 | |
| 224 | void blk_free_queue_stats(struct blk_queue_stats *stats) |
| 225 | { |
| 226 | if (!stats) |
| 227 | return; |
| 228 | |
| 229 | WARN_ON(!list_empty(&stats->callbacks)); |
| 230 | |
| 231 | kfree(stats); |
Jens Axboe | cf43e6b | 2016-11-07 21:32:37 -0700 | [diff] [blame] | 232 | } |
Jens Axboe | 48b5c1f | 2021-11-13 14:03:26 -0700 | [diff] [blame] | 233 | |
| 234 | bool blk_stats_alloc_enable(struct request_queue *q) |
| 235 | { |
| 236 | struct blk_rq_stat *poll_stat; |
| 237 | |
| 238 | poll_stat = kcalloc(BLK_MQ_POLL_STATS_BKTS, sizeof(*poll_stat), |
| 239 | GFP_ATOMIC); |
| 240 | if (!poll_stat) |
| 241 | return false; |
| 242 | |
| 243 | if (cmpxchg(&q->poll_stat, NULL, poll_stat) != NULL) { |
| 244 | kfree(poll_stat); |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | blk_stat_add_callback(q, q->poll_cb); |
| 249 | return false; |
| 250 | } |