1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/backing-dev.h>
5 #include <linux/blkdev.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 #include <linux/smp.h>
12 #include <linux/blk-mq.h>
14 #include "blk-mq-tag.h"
16 static void blk_mq_sysfs_release(struct kobject *kobj)
20 struct blk_mq_ctx_sysfs_entry {
21 struct attribute attr;
22 ssize_t (*show)(struct blk_mq_ctx *, char *);
23 ssize_t (*store)(struct blk_mq_ctx *, const char *, size_t);
26 struct blk_mq_hw_ctx_sysfs_entry {
27 struct attribute attr;
28 ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
29 ssize_t (*store)(struct blk_mq_hw_ctx *, const char *, size_t);
32 static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
35 struct blk_mq_ctx_sysfs_entry *entry;
36 struct blk_mq_ctx *ctx;
37 struct request_queue *q;
40 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
41 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
48 mutex_lock(&q->sysfs_lock);
49 if (!blk_queue_dying(q))
50 res = entry->show(ctx, page);
51 mutex_unlock(&q->sysfs_lock);
55 static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
56 const char *page, size_t length)
58 struct blk_mq_ctx_sysfs_entry *entry;
59 struct blk_mq_ctx *ctx;
60 struct request_queue *q;
63 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
64 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
71 mutex_lock(&q->sysfs_lock);
72 if (!blk_queue_dying(q))
73 res = entry->store(ctx, page, length);
74 mutex_unlock(&q->sysfs_lock);
78 static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
79 struct attribute *attr, char *page)
81 struct blk_mq_hw_ctx_sysfs_entry *entry;
82 struct blk_mq_hw_ctx *hctx;
83 struct request_queue *q;
86 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
87 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
94 mutex_lock(&q->sysfs_lock);
95 if (!blk_queue_dying(q))
96 res = entry->show(hctx, page);
97 mutex_unlock(&q->sysfs_lock);
101 static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
102 struct attribute *attr, const char *page,
105 struct blk_mq_hw_ctx_sysfs_entry *entry;
106 struct blk_mq_hw_ctx *hctx;
107 struct request_queue *q;
110 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
111 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
118 mutex_lock(&q->sysfs_lock);
119 if (!blk_queue_dying(q))
120 res = entry->store(hctx, page, length);
121 mutex_unlock(&q->sysfs_lock);
125 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
128 return sprintf(page, "%u\n", hctx->tags->nr_tags);
131 static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
134 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
137 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
139 unsigned int i, first = 1;
142 for_each_cpu(i, hctx->cpumask) {
144 ret += sprintf(ret + page, "%u", i);
146 ret += sprintf(ret + page, ", %u", i);
151 ret += sprintf(ret + page, "\n");
155 static struct attribute *default_ctx_attrs[] = {
159 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
160 .attr = {.name = "nr_tags", .mode = S_IRUGO },
161 .show = blk_mq_hw_sysfs_nr_tags_show,
163 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
164 .attr = {.name = "nr_reserved_tags", .mode = S_IRUGO },
165 .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
167 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
168 .attr = {.name = "cpu_list", .mode = S_IRUGO },
169 .show = blk_mq_hw_sysfs_cpus_show,
172 static struct attribute *default_hw_ctx_attrs[] = {
173 &blk_mq_hw_sysfs_nr_tags.attr,
174 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
175 &blk_mq_hw_sysfs_cpus.attr,
179 static const struct sysfs_ops blk_mq_sysfs_ops = {
180 .show = blk_mq_sysfs_show,
181 .store = blk_mq_sysfs_store,
184 static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
185 .show = blk_mq_hw_sysfs_show,
186 .store = blk_mq_hw_sysfs_store,
189 static struct kobj_type blk_mq_ktype = {
190 .sysfs_ops = &blk_mq_sysfs_ops,
191 .release = blk_mq_sysfs_release,
194 static struct kobj_type blk_mq_ctx_ktype = {
195 .sysfs_ops = &blk_mq_sysfs_ops,
196 .default_attrs = default_ctx_attrs,
197 .release = blk_mq_sysfs_release,
200 static struct kobj_type blk_mq_hw_ktype = {
201 .sysfs_ops = &blk_mq_hw_sysfs_ops,
202 .default_attrs = default_hw_ctx_attrs,
203 .release = blk_mq_sysfs_release,
206 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
208 struct blk_mq_ctx *ctx;
214 hctx_for_each_ctx(hctx, ctx, i)
215 kobject_del(&ctx->kobj);
217 kobject_del(&hctx->kobj);
220 static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
222 struct request_queue *q = hctx->queue;
223 struct blk_mq_ctx *ctx;
229 ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
233 hctx_for_each_ctx(hctx, ctx, i) {
234 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
242 static void __blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
244 struct blk_mq_hw_ctx *hctx;
245 struct blk_mq_ctx *ctx;
248 queue_for_each_hw_ctx(q, hctx, i) {
249 blk_mq_unregister_hctx(hctx);
251 hctx_for_each_ctx(hctx, ctx, j)
252 kobject_put(&ctx->kobj);
254 kobject_put(&hctx->kobj);
257 blk_mq_debugfs_unregister_hctxs(q);
259 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
260 kobject_del(&q->mq_kobj);
261 kobject_put(&q->mq_kobj);
263 kobject_put(&dev->kobj);
265 q->mq_sysfs_init_done = false;
268 void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
270 blk_mq_disable_hotplug();
271 __blk_mq_unregister_dev(dev, q);
272 blk_mq_enable_hotplug();
275 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
277 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
280 static void blk_mq_sysfs_init(struct request_queue *q)
282 struct blk_mq_ctx *ctx;
285 kobject_init(&q->mq_kobj, &blk_mq_ktype);
287 for_each_possible_cpu(cpu) {
288 ctx = per_cpu_ptr(q->queue_ctx, cpu);
289 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
293 int blk_mq_register_dev(struct device *dev, struct request_queue *q)
295 struct blk_mq_hw_ctx *hctx;
298 blk_mq_disable_hotplug();
300 blk_mq_sysfs_init(q);
302 ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
306 kobject_uevent(&q->mq_kobj, KOBJ_ADD);
308 blk_mq_debugfs_register(q, kobject_name(&dev->kobj));
310 queue_for_each_hw_ctx(q, hctx, i) {
311 ret = blk_mq_register_hctx(hctx);
317 __blk_mq_unregister_dev(dev, q);
319 q->mq_sysfs_init_done = true;
321 blk_mq_enable_hotplug();
325 EXPORT_SYMBOL_GPL(blk_mq_register_dev);
327 void blk_mq_sysfs_unregister(struct request_queue *q)
329 struct blk_mq_hw_ctx *hctx;
332 if (!q->mq_sysfs_init_done)
335 blk_mq_debugfs_unregister_hctxs(q);
337 queue_for_each_hw_ctx(q, hctx, i)
338 blk_mq_unregister_hctx(hctx);
341 int blk_mq_sysfs_register(struct request_queue *q)
343 struct blk_mq_hw_ctx *hctx;
346 if (!q->mq_sysfs_init_done)
349 blk_mq_debugfs_register_hctxs(q);
351 queue_for_each_hw_ctx(q, hctx, i) {
352 ret = blk_mq_register_hctx(hctx);