blob: 6774a0e698675927be5c78dc34b0087d873b5ebe [file] [log] [blame]
Jens Axboe320ae512013-10-24 09:20:05 +01001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/backing-dev.h>
4#include <linux/bio.h>
5#include <linux/blkdev.h>
6#include <linux/mm.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/workqueue.h>
10#include <linux/smp.h>
11
12#include <linux/blk-mq.h>
13#include "blk-mq.h"
14#include "blk-mq-tag.h"
15
16static void blk_mq_sysfs_release(struct kobject *kobj)
17{
Ming Lei76d697d2015-01-20 11:00:56 +080018 struct request_queue *q;
19
20 q = container_of(kobj, struct request_queue, mq_kobj);
21 free_percpu(q->queue_ctx);
22}
23
24static void blk_mq_ctx_release(struct kobject *kobj)
25{
26 struct blk_mq_ctx *ctx;
27
28 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
29 kobject_put(&ctx->queue->mq_kobj);
30}
31
32static void blk_mq_hctx_release(struct kobject *kobj)
33{
34 struct blk_mq_hw_ctx *hctx;
35
36 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
37 kfree(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010038}
39
40struct blk_mq_ctx_sysfs_entry {
41 struct attribute attr;
42 ssize_t (*show)(struct blk_mq_ctx *, char *);
43 ssize_t (*store)(struct blk_mq_ctx *, const char *, size_t);
44};
45
46struct blk_mq_hw_ctx_sysfs_entry {
47 struct attribute attr;
48 ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
49 ssize_t (*store)(struct blk_mq_hw_ctx *, const char *, size_t);
50};
51
52static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
53 char *page)
54{
55 struct blk_mq_ctx_sysfs_entry *entry;
56 struct blk_mq_ctx *ctx;
57 struct request_queue *q;
58 ssize_t res;
59
60 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
61 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
62 q = ctx->queue;
63
64 if (!entry->show)
65 return -EIO;
66
67 res = -ENOENT;
68 mutex_lock(&q->sysfs_lock);
69 if (!blk_queue_dying(q))
70 res = entry->show(ctx, page);
71 mutex_unlock(&q->sysfs_lock);
72 return res;
73}
74
75static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
76 const char *page, size_t length)
77{
78 struct blk_mq_ctx_sysfs_entry *entry;
79 struct blk_mq_ctx *ctx;
80 struct request_queue *q;
81 ssize_t res;
82
83 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
84 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
85 q = ctx->queue;
86
87 if (!entry->store)
88 return -EIO;
89
90 res = -ENOENT;
91 mutex_lock(&q->sysfs_lock);
92 if (!blk_queue_dying(q))
93 res = entry->store(ctx, page, length);
94 mutex_unlock(&q->sysfs_lock);
95 return res;
96}
97
98static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
99 struct attribute *attr, char *page)
100{
101 struct blk_mq_hw_ctx_sysfs_entry *entry;
102 struct blk_mq_hw_ctx *hctx;
103 struct request_queue *q;
104 ssize_t res;
105
106 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
107 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
108 q = hctx->queue;
109
110 if (!entry->show)
111 return -EIO;
112
113 res = -ENOENT;
114 mutex_lock(&q->sysfs_lock);
115 if (!blk_queue_dying(q))
116 res = entry->show(hctx, page);
117 mutex_unlock(&q->sysfs_lock);
118 return res;
119}
120
121static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
122 struct attribute *attr, const char *page,
123 size_t length)
124{
125 struct blk_mq_hw_ctx_sysfs_entry *entry;
126 struct blk_mq_hw_ctx *hctx;
127 struct request_queue *q;
128 ssize_t res;
129
130 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
131 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
132 q = hctx->queue;
133
134 if (!entry->store)
135 return -EIO;
136
137 res = -ENOENT;
138 mutex_lock(&q->sysfs_lock);
139 if (!blk_queue_dying(q))
140 res = entry->store(hctx, page, length);
141 mutex_unlock(&q->sysfs_lock);
142 return res;
143}
144
145static ssize_t blk_mq_sysfs_dispatched_show(struct blk_mq_ctx *ctx, char *page)
146{
147 return sprintf(page, "%lu %lu\n", ctx->rq_dispatched[1],
148 ctx->rq_dispatched[0]);
149}
150
151static ssize_t blk_mq_sysfs_merged_show(struct blk_mq_ctx *ctx, char *page)
152{
153 return sprintf(page, "%lu\n", ctx->rq_merged);
154}
155
156static ssize_t blk_mq_sysfs_completed_show(struct blk_mq_ctx *ctx, char *page)
157{
158 return sprintf(page, "%lu %lu\n", ctx->rq_completed[1],
159 ctx->rq_completed[0]);
160}
161
162static ssize_t sysfs_list_show(char *page, struct list_head *list, char *msg)
163{
164 char *start_page = page;
165 struct request *rq;
166
167 page += sprintf(page, "%s:\n", msg);
168
169 list_for_each_entry(rq, list, queuelist)
170 page += sprintf(page, "\t%p\n", rq);
171
172 return page - start_page;
173}
174
175static ssize_t blk_mq_sysfs_rq_list_show(struct blk_mq_ctx *ctx, char *page)
176{
177 ssize_t ret;
178
179 spin_lock(&ctx->lock);
180 ret = sysfs_list_show(page, &ctx->rq_list, "CTX pending");
181 spin_unlock(&ctx->lock);
182
183 return ret;
184}
185
186static ssize_t blk_mq_hw_sysfs_queued_show(struct blk_mq_hw_ctx *hctx,
187 char *page)
188{
189 return sprintf(page, "%lu\n", hctx->queued);
190}
191
192static ssize_t blk_mq_hw_sysfs_run_show(struct blk_mq_hw_ctx *hctx, char *page)
193{
194 return sprintf(page, "%lu\n", hctx->run);
195}
196
197static ssize_t blk_mq_hw_sysfs_dispatched_show(struct blk_mq_hw_ctx *hctx,
198 char *page)
199{
200 char *start_page = page;
201 int i;
202
203 page += sprintf(page, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
204
205 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER; i++) {
206 unsigned long d = 1U << (i - 1);
207
208 page += sprintf(page, "%8lu\t%lu\n", d, hctx->dispatched[i]);
209 }
210
211 return page - start_page;
212}
213
214static ssize_t blk_mq_hw_sysfs_rq_list_show(struct blk_mq_hw_ctx *hctx,
215 char *page)
216{
217 ssize_t ret;
218
219 spin_lock(&hctx->lock);
220 ret = sysfs_list_show(page, &hctx->dispatch, "HCTX pending");
221 spin_unlock(&hctx->lock);
222
223 return ret;
224}
225
Jens Axboe320ae512013-10-24 09:20:05 +0100226static ssize_t blk_mq_hw_sysfs_tags_show(struct blk_mq_hw_ctx *hctx, char *page)
227{
228 return blk_mq_tag_sysfs_show(hctx->tags, page);
229}
230
Jens Axboe0d2602c2014-05-13 15:10:52 -0600231static ssize_t blk_mq_hw_sysfs_active_show(struct blk_mq_hw_ctx *hctx, char *page)
232{
233 return sprintf(page, "%u\n", atomic_read(&hctx->nr_active));
234}
235
Jens Axboe676141e2014-03-20 13:29:18 -0600236static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
237{
Jens Axboecb2da432014-04-09 10:53:21 -0600238 unsigned int i, first = 1;
Jens Axboe676141e2014-03-20 13:29:18 -0600239 ssize_t ret = 0;
240
241 blk_mq_disable_hotplug();
242
Jens Axboecb2da432014-04-09 10:53:21 -0600243 for_each_cpu(i, hctx->cpumask) {
Jens Axboe676141e2014-03-20 13:29:18 -0600244 if (first)
245 ret += sprintf(ret + page, "%u", i);
246 else
247 ret += sprintf(ret + page, ", %u", i);
248
249 first = 0;
250 }
251
252 blk_mq_enable_hotplug();
253
254 ret += sprintf(ret + page, "\n");
255 return ret;
256}
257
Jens Axboe320ae512013-10-24 09:20:05 +0100258static struct blk_mq_ctx_sysfs_entry blk_mq_sysfs_dispatched = {
259 .attr = {.name = "dispatched", .mode = S_IRUGO },
260 .show = blk_mq_sysfs_dispatched_show,
261};
262static struct blk_mq_ctx_sysfs_entry blk_mq_sysfs_merged = {
263 .attr = {.name = "merged", .mode = S_IRUGO },
264 .show = blk_mq_sysfs_merged_show,
265};
266static struct blk_mq_ctx_sysfs_entry blk_mq_sysfs_completed = {
267 .attr = {.name = "completed", .mode = S_IRUGO },
268 .show = blk_mq_sysfs_completed_show,
269};
270static struct blk_mq_ctx_sysfs_entry blk_mq_sysfs_rq_list = {
271 .attr = {.name = "rq_list", .mode = S_IRUGO },
272 .show = blk_mq_sysfs_rq_list_show,
273};
274
275static struct attribute *default_ctx_attrs[] = {
276 &blk_mq_sysfs_dispatched.attr,
277 &blk_mq_sysfs_merged.attr,
278 &blk_mq_sysfs_completed.attr,
279 &blk_mq_sysfs_rq_list.attr,
280 NULL,
281};
282
283static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_queued = {
284 .attr = {.name = "queued", .mode = S_IRUGO },
285 .show = blk_mq_hw_sysfs_queued_show,
286};
287static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_run = {
288 .attr = {.name = "run", .mode = S_IRUGO },
289 .show = blk_mq_hw_sysfs_run_show,
290};
291static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_dispatched = {
292 .attr = {.name = "dispatched", .mode = S_IRUGO },
293 .show = blk_mq_hw_sysfs_dispatched_show,
294};
Jens Axboe0d2602c2014-05-13 15:10:52 -0600295static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_active = {
296 .attr = {.name = "active", .mode = S_IRUGO },
297 .show = blk_mq_hw_sysfs_active_show,
298};
Jens Axboe320ae512013-10-24 09:20:05 +0100299static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_pending = {
300 .attr = {.name = "pending", .mode = S_IRUGO },
301 .show = blk_mq_hw_sysfs_rq_list_show,
302};
Jens Axboe320ae512013-10-24 09:20:05 +0100303static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_tags = {
304 .attr = {.name = "tags", .mode = S_IRUGO },
305 .show = blk_mq_hw_sysfs_tags_show,
306};
Jens Axboe676141e2014-03-20 13:29:18 -0600307static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
308 .attr = {.name = "cpu_list", .mode = S_IRUGO },
309 .show = blk_mq_hw_sysfs_cpus_show,
310};
Jens Axboe320ae512013-10-24 09:20:05 +0100311
312static struct attribute *default_hw_ctx_attrs[] = {
313 &blk_mq_hw_sysfs_queued.attr,
314 &blk_mq_hw_sysfs_run.attr,
315 &blk_mq_hw_sysfs_dispatched.attr,
316 &blk_mq_hw_sysfs_pending.attr,
Jens Axboe320ae512013-10-24 09:20:05 +0100317 &blk_mq_hw_sysfs_tags.attr,
Jens Axboe676141e2014-03-20 13:29:18 -0600318 &blk_mq_hw_sysfs_cpus.attr,
Jens Axboe0d2602c2014-05-13 15:10:52 -0600319 &blk_mq_hw_sysfs_active.attr,
Jens Axboe320ae512013-10-24 09:20:05 +0100320 NULL,
321};
322
323static const struct sysfs_ops blk_mq_sysfs_ops = {
324 .show = blk_mq_sysfs_show,
325 .store = blk_mq_sysfs_store,
326};
327
328static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
329 .show = blk_mq_hw_sysfs_show,
330 .store = blk_mq_hw_sysfs_store,
331};
332
333static struct kobj_type blk_mq_ktype = {
334 .sysfs_ops = &blk_mq_sysfs_ops,
335 .release = blk_mq_sysfs_release,
336};
337
338static struct kobj_type blk_mq_ctx_ktype = {
339 .sysfs_ops = &blk_mq_sysfs_ops,
340 .default_attrs = default_ctx_attrs,
Ming Lei76d697d2015-01-20 11:00:56 +0800341 .release = blk_mq_ctx_release,
Jens Axboe320ae512013-10-24 09:20:05 +0100342};
343
344static struct kobj_type blk_mq_hw_ktype = {
345 .sysfs_ops = &blk_mq_hw_sysfs_ops,
346 .default_attrs = default_hw_ctx_attrs,
Ming Lei76d697d2015-01-20 11:00:56 +0800347 .release = blk_mq_hctx_release,
Jens Axboe320ae512013-10-24 09:20:05 +0100348};
349
Fengguang Wuee3c5db2014-05-30 10:31:13 -0600350static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
Jens Axboe67aec142014-05-30 08:25:36 -0600351{
352 struct blk_mq_ctx *ctx;
353 int i;
354
355 if (!hctx->nr_ctx || !(hctx->flags & BLK_MQ_F_SYSFS_UP))
356 return;
357
358 hctx_for_each_ctx(hctx, ctx, i)
359 kobject_del(&ctx->kobj);
360
361 kobject_del(&hctx->kobj);
362}
363
Fengguang Wuee3c5db2014-05-30 10:31:13 -0600364static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
Jens Axboe67aec142014-05-30 08:25:36 -0600365{
366 struct request_queue *q = hctx->queue;
367 struct blk_mq_ctx *ctx;
368 int i, ret;
369
370 if (!hctx->nr_ctx || !(hctx->flags & BLK_MQ_F_SYSFS_UP))
371 return 0;
372
373 ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
374 if (ret)
375 return ret;
376
377 hctx_for_each_ctx(hctx, ctx, i) {
Ming Lei76d697d2015-01-20 11:00:56 +0800378 kobject_get(&q->mq_kobj);
Jens Axboe67aec142014-05-30 08:25:36 -0600379 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
380 if (ret)
381 break;
382 }
383
384 return ret;
385}
386
Jens Axboe320ae512013-10-24 09:20:05 +0100387void blk_mq_unregister_disk(struct gendisk *disk)
388{
389 struct request_queue *q = disk->queue;
Andrey Vagin85157362013-12-06 09:06:41 +0400390 struct blk_mq_hw_ctx *hctx;
391 struct blk_mq_ctx *ctx;
392 int i, j;
393
394 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe67aec142014-05-30 08:25:36 -0600395 blk_mq_unregister_hctx(hctx);
396
397 hctx_for_each_ctx(hctx, ctx, j)
Andrey Vagin85157362013-12-06 09:06:41 +0400398 kobject_put(&ctx->kobj);
Jens Axboe67aec142014-05-30 08:25:36 -0600399
Andrey Vagin85157362013-12-06 09:06:41 +0400400 kobject_put(&hctx->kobj);
401 }
Jens Axboe320ae512013-10-24 09:20:05 +0100402
403 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
404 kobject_del(&q->mq_kobj);
Andrey Vagin85157362013-12-06 09:06:41 +0400405 kobject_put(&q->mq_kobj);
Jens Axboe320ae512013-10-24 09:20:05 +0100406
407 kobject_put(&disk_to_dev(disk)->kobj);
408}
409
Jens Axboe67aec142014-05-30 08:25:36 -0600410static void blk_mq_sysfs_init(struct request_queue *q)
411{
412 struct blk_mq_hw_ctx *hctx;
413 struct blk_mq_ctx *ctx;
Takashi Iwai06a41a92014-12-10 16:38:30 +0100414 int i;
Jens Axboe67aec142014-05-30 08:25:36 -0600415
416 kobject_init(&q->mq_kobj, &blk_mq_ktype);
417
Takashi Iwai06a41a92014-12-10 16:38:30 +0100418 queue_for_each_hw_ctx(q, hctx, i)
Jens Axboe67aec142014-05-30 08:25:36 -0600419 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
420
Takashi Iwai06a41a92014-12-10 16:38:30 +0100421 queue_for_each_ctx(q, ctx, i)
422 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
Jens Axboe67aec142014-05-30 08:25:36 -0600423}
424
Tejun Heo17497ac2014-09-24 13:31:50 -0400425/* see blk_register_queue() */
426void blk_mq_finish_init(struct request_queue *q)
427{
428 percpu_ref_switch_to_percpu(&q->mq_usage_counter);
429}
430
Jens Axboe320ae512013-10-24 09:20:05 +0100431int blk_mq_register_disk(struct gendisk *disk)
432{
433 struct device *dev = disk_to_dev(disk);
434 struct request_queue *q = disk->queue;
435 struct blk_mq_hw_ctx *hctx;
Jens Axboe67aec142014-05-30 08:25:36 -0600436 int ret, i;
Jens Axboe320ae512013-10-24 09:20:05 +0100437
Jens Axboe67aec142014-05-30 08:25:36 -0600438 blk_mq_sysfs_init(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100439
440 ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
441 if (ret < 0)
442 return ret;
443
444 kobject_uevent(&q->mq_kobj, KOBJ_ADD);
445
446 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe67aec142014-05-30 08:25:36 -0600447 hctx->flags |= BLK_MQ_F_SYSFS_UP;
448 ret = blk_mq_register_hctx(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100449 if (ret)
450 break;
Jens Axboe320ae512013-10-24 09:20:05 +0100451 }
452
453 if (ret) {
454 blk_mq_unregister_disk(disk);
455 return ret;
456 }
457
458 return 0;
459}
Jens Axboe67aec142014-05-30 08:25:36 -0600460
461void blk_mq_sysfs_unregister(struct request_queue *q)
462{
463 struct blk_mq_hw_ctx *hctx;
464 int i;
465
466 queue_for_each_hw_ctx(q, hctx, i)
467 blk_mq_unregister_hctx(hctx);
468}
469
470int blk_mq_sysfs_register(struct request_queue *q)
471{
472 struct blk_mq_hw_ctx *hctx;
473 int i, ret = 0;
474
475 queue_for_each_hw_ctx(q, hctx, i) {
476 ret = blk_mq_register_hctx(hctx);
477 if (ret)
478 break;
479 }
480
481 return ret;
482}