blob: cb19ec16a7fc03d1b07c6d85bea5fcc6b88ea4b4 [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{
18}
19
Ming Lei6c8b2322017-02-22 18:14:01 +080020static void blk_mq_hw_sysfs_release(struct kobject *kobj)
21{
22 struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
23 kobj);
24 kfree(hctx->ctxs);
25 kfree(hctx);
26}
27
Jens Axboe320ae512013-10-24 09:20:05 +010028struct blk_mq_ctx_sysfs_entry {
29 struct attribute attr;
30 ssize_t (*show)(struct blk_mq_ctx *, char *);
31 ssize_t (*store)(struct blk_mq_ctx *, const char *, size_t);
32};
33
34struct blk_mq_hw_ctx_sysfs_entry {
35 struct attribute attr;
36 ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
37 ssize_t (*store)(struct blk_mq_hw_ctx *, const char *, size_t);
38};
39
40static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
41 char *page)
42{
43 struct blk_mq_ctx_sysfs_entry *entry;
44 struct blk_mq_ctx *ctx;
45 struct request_queue *q;
46 ssize_t res;
47
48 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
49 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
50 q = ctx->queue;
51
52 if (!entry->show)
53 return -EIO;
54
55 res = -ENOENT;
56 mutex_lock(&q->sysfs_lock);
57 if (!blk_queue_dying(q))
58 res = entry->show(ctx, page);
59 mutex_unlock(&q->sysfs_lock);
60 return res;
61}
62
63static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
64 const char *page, size_t length)
65{
66 struct blk_mq_ctx_sysfs_entry *entry;
67 struct blk_mq_ctx *ctx;
68 struct request_queue *q;
69 ssize_t res;
70
71 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
72 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
73 q = ctx->queue;
74
75 if (!entry->store)
76 return -EIO;
77
78 res = -ENOENT;
79 mutex_lock(&q->sysfs_lock);
80 if (!blk_queue_dying(q))
81 res = entry->store(ctx, page, length);
82 mutex_unlock(&q->sysfs_lock);
83 return res;
84}
85
86static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
87 struct attribute *attr, char *page)
88{
89 struct blk_mq_hw_ctx_sysfs_entry *entry;
90 struct blk_mq_hw_ctx *hctx;
91 struct request_queue *q;
92 ssize_t res;
93
94 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
95 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
96 q = hctx->queue;
97
98 if (!entry->show)
99 return -EIO;
100
101 res = -ENOENT;
102 mutex_lock(&q->sysfs_lock);
103 if (!blk_queue_dying(q))
104 res = entry->show(hctx, page);
105 mutex_unlock(&q->sysfs_lock);
106 return res;
107}
108
109static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
110 struct attribute *attr, const char *page,
111 size_t length)
112{
113 struct blk_mq_hw_ctx_sysfs_entry *entry;
114 struct blk_mq_hw_ctx *hctx;
115 struct request_queue *q;
116 ssize_t res;
117
118 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
119 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
120 q = hctx->queue;
121
122 if (!entry->store)
123 return -EIO;
124
125 res = -ENOENT;
126 mutex_lock(&q->sysfs_lock);
127 if (!blk_queue_dying(q))
128 res = entry->store(hctx, page, length);
129 mutex_unlock(&q->sysfs_lock);
130 return res;
131}
132
Omar Sandovald96b37c2017-01-25 08:06:46 -0800133static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
134 char *page)
Jens Axboebd166ef2017-01-17 06:03:22 -0700135{
Omar Sandovald96b37c2017-01-25 08:06:46 -0800136 return sprintf(page, "%u\n", hctx->tags->nr_tags);
Jens Axboebd166ef2017-01-17 06:03:22 -0700137}
138
Omar Sandovald96b37c2017-01-25 08:06:46 -0800139static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
140 char *page)
Jens Axboe320ae512013-10-24 09:20:05 +0100141{
Omar Sandovald96b37c2017-01-25 08:06:46 -0800142 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
Jens Axboe320ae512013-10-24 09:20:05 +0100143}
144
Jens Axboe676141e2014-03-20 13:29:18 -0600145static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
146{
Jens Axboecb2da432014-04-09 10:53:21 -0600147 unsigned int i, first = 1;
Jens Axboe676141e2014-03-20 13:29:18 -0600148 ssize_t ret = 0;
149
Jens Axboecb2da432014-04-09 10:53:21 -0600150 for_each_cpu(i, hctx->cpumask) {
Jens Axboe676141e2014-03-20 13:29:18 -0600151 if (first)
152 ret += sprintf(ret + page, "%u", i);
153 else
154 ret += sprintf(ret + page, ", %u", i);
155
156 first = 0;
157 }
158
Jens Axboe676141e2014-03-20 13:29:18 -0600159 ret += sprintf(ret + page, "\n");
160 return ret;
161}
162
Jens Axboe320ae512013-10-24 09:20:05 +0100163static struct attribute *default_ctx_attrs[] = {
Jens Axboe320ae512013-10-24 09:20:05 +0100164 NULL,
165};
166
Omar Sandovald96b37c2017-01-25 08:06:46 -0800167static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
168 .attr = {.name = "nr_tags", .mode = S_IRUGO },
169 .show = blk_mq_hw_sysfs_nr_tags_show,
170};
171static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
172 .attr = {.name = "nr_reserved_tags", .mode = S_IRUGO },
173 .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
174};
Jens Axboe676141e2014-03-20 13:29:18 -0600175static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
176 .attr = {.name = "cpu_list", .mode = S_IRUGO },
177 .show = blk_mq_hw_sysfs_cpus_show,
178};
Jens Axboe320ae512013-10-24 09:20:05 +0100179
180static struct attribute *default_hw_ctx_attrs[] = {
Omar Sandovald96b37c2017-01-25 08:06:46 -0800181 &blk_mq_hw_sysfs_nr_tags.attr,
182 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
Jens Axboe676141e2014-03-20 13:29:18 -0600183 &blk_mq_hw_sysfs_cpus.attr,
Jens Axboe320ae512013-10-24 09:20:05 +0100184 NULL,
185};
186
187static const struct sysfs_ops blk_mq_sysfs_ops = {
188 .show = blk_mq_sysfs_show,
189 .store = blk_mq_sysfs_store,
190};
191
192static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
193 .show = blk_mq_hw_sysfs_show,
194 .store = blk_mq_hw_sysfs_store,
195};
196
197static struct kobj_type blk_mq_ktype = {
198 .sysfs_ops = &blk_mq_sysfs_ops,
199 .release = blk_mq_sysfs_release,
200};
201
202static struct kobj_type blk_mq_ctx_ktype = {
203 .sysfs_ops = &blk_mq_sysfs_ops,
204 .default_attrs = default_ctx_attrs,
Ming Lei74170112015-01-29 20:17:26 +0800205 .release = blk_mq_sysfs_release,
Jens Axboe320ae512013-10-24 09:20:05 +0100206};
207
208static struct kobj_type blk_mq_hw_ktype = {
209 .sysfs_ops = &blk_mq_hw_sysfs_ops,
210 .default_attrs = default_hw_ctx_attrs,
Ming Lei6c8b2322017-02-22 18:14:01 +0800211 .release = blk_mq_hw_sysfs_release,
Jens Axboe320ae512013-10-24 09:20:05 +0100212};
213
Fengguang Wuee3c5db2014-05-30 10:31:13 -0600214static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
Jens Axboe67aec142014-05-30 08:25:36 -0600215{
216 struct blk_mq_ctx *ctx;
217 int i;
218
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900219 if (!hctx->nr_ctx)
Jens Axboe67aec142014-05-30 08:25:36 -0600220 return;
221
222 hctx_for_each_ctx(hctx, ctx, i)
223 kobject_del(&ctx->kobj);
224
225 kobject_del(&hctx->kobj);
226}
227
Fengguang Wuee3c5db2014-05-30 10:31:13 -0600228static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
Jens Axboe67aec142014-05-30 08:25:36 -0600229{
230 struct request_queue *q = hctx->queue;
231 struct blk_mq_ctx *ctx;
232 int i, ret;
233
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900234 if (!hctx->nr_ctx)
Jens Axboe67aec142014-05-30 08:25:36 -0600235 return 0;
236
237 ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
238 if (ret)
239 return ret;
240
241 hctx_for_each_ctx(hctx, ctx, i) {
242 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
243 if (ret)
244 break;
245 }
246
247 return ret;
248}
249
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200250static void __blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100251{
Andrey Vagin85157362013-12-06 09:06:41 +0400252 struct blk_mq_hw_ctx *hctx;
Ming Lei7ea5fe32017-02-22 18:14:00 +0800253 int i;
Andrey Vagin85157362013-12-06 09:06:41 +0400254
Ming Lei6c8b2322017-02-22 18:14:01 +0800255 queue_for_each_hw_ctx(q, hctx, i)
Jens Axboe67aec142014-05-30 08:25:36 -0600256 blk_mq_unregister_hctx(hctx);
257
Omar Sandoval62ebce12017-01-31 14:53:21 -0800258 blk_mq_debugfs_unregister_hctxs(q);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800259
Jens Axboe320ae512013-10-24 09:20:05 +0100260 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
261 kobject_del(&q->mq_kobj);
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200262 kobject_put(&dev->kobj);
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900263
264 q->mq_sysfs_init_done = false;
Jens Axboec0f3fd22016-08-02 08:45:44 -0600265}
266
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200267void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
Jens Axboec0f3fd22016-08-02 08:45:44 -0600268{
269 blk_mq_disable_hotplug();
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200270 __blk_mq_unregister_dev(dev, q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900271 blk_mq_enable_hotplug();
Jens Axboe320ae512013-10-24 09:20:05 +0100272}
273
Keith Busch868f2f02015-12-17 17:08:14 -0700274void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
275{
276 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
277}
278
Ming Lei7ea5fe32017-02-22 18:14:00 +0800279void blk_mq_sysfs_deinit(struct request_queue *q)
280{
281 struct blk_mq_ctx *ctx;
282 int cpu;
283
284 for_each_possible_cpu(cpu) {
285 ctx = per_cpu_ptr(q->queue_ctx, cpu);
286 kobject_put(&ctx->kobj);
287 }
288 kobject_put(&q->mq_kobj);
289}
290
Ming Lei737f98c2017-02-22 18:13:59 +0800291void blk_mq_sysfs_init(struct request_queue *q)
Jens Axboe67aec142014-05-30 08:25:36 -0600292{
Jens Axboe67aec142014-05-30 08:25:36 -0600293 struct blk_mq_ctx *ctx;
Thomas Gleixner897bb0c2016-03-19 11:30:33 +0100294 int cpu;
Jens Axboe67aec142014-05-30 08:25:36 -0600295
296 kobject_init(&q->mq_kobj, &blk_mq_ktype);
297
Thomas Gleixner897bb0c2016-03-19 11:30:33 +0100298 for_each_possible_cpu(cpu) {
299 ctx = per_cpu_ptr(q->queue_ctx, cpu);
Takashi Iwai06a41a92014-12-10 16:38:30 +0100300 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
Thomas Gleixner897bb0c2016-03-19 11:30:33 +0100301 }
Jens Axboe67aec142014-05-30 08:25:36 -0600302}
303
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200304int blk_mq_register_dev(struct device *dev, struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100305{
Jens Axboe320ae512013-10-24 09:20:05 +0100306 struct blk_mq_hw_ctx *hctx;
Jens Axboe67aec142014-05-30 08:25:36 -0600307 int ret, i;
Jens Axboe320ae512013-10-24 09:20:05 +0100308
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900309 blk_mq_disable_hotplug();
310
Jens Axboe320ae512013-10-24 09:20:05 +0100311 ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
312 if (ret < 0)
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900313 goto out;
Jens Axboe320ae512013-10-24 09:20:05 +0100314
315 kobject_uevent(&q->mq_kobj, KOBJ_ADD);
316
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800317 blk_mq_debugfs_register(q, kobject_name(&dev->kobj));
318
Jens Axboe320ae512013-10-24 09:20:05 +0100319 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe67aec142014-05-30 08:25:36 -0600320 ret = blk_mq_register_hctx(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100321 if (ret)
322 break;
Jens Axboe320ae512013-10-24 09:20:05 +0100323 }
324
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900325 if (ret)
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200326 __blk_mq_unregister_dev(dev, q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900327 else
328 q->mq_sysfs_init_done = true;
329out:
330 blk_mq_enable_hotplug();
Jens Axboe320ae512013-10-24 09:20:05 +0100331
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900332 return ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100333}
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200334EXPORT_SYMBOL_GPL(blk_mq_register_dev);
Jens Axboe67aec142014-05-30 08:25:36 -0600335
336void blk_mq_sysfs_unregister(struct request_queue *q)
337{
338 struct blk_mq_hw_ctx *hctx;
339 int i;
340
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900341 if (!q->mq_sysfs_init_done)
342 return;
343
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800344 blk_mq_debugfs_unregister_hctxs(q);
345
Jens Axboe67aec142014-05-30 08:25:36 -0600346 queue_for_each_hw_ctx(q, hctx, i)
347 blk_mq_unregister_hctx(hctx);
348}
349
350int blk_mq_sysfs_register(struct request_queue *q)
351{
352 struct blk_mq_hw_ctx *hctx;
353 int i, ret = 0;
354
Akinobu Mita4593fdb2015-09-27 02:09:20 +0900355 if (!q->mq_sysfs_init_done)
356 return ret;
357
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800358 blk_mq_debugfs_register_hctxs(q);
359
Jens Axboe67aec142014-05-30 08:25:36 -0600360 queue_for_each_hw_ctx(q, hctx, i) {
361 ret = blk_mq_register_hctx(hctx);
362 if (ret)
363 break;
364 }
365
366 return ret;
367}