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>
15 #include "blk-mq-tag.h"
17 static void blk_mq_sysfs_release(struct kobject *kobj)
21 static void blk_mq_hw_sysfs_release(struct kobject *kobj)
23 struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
26 if (hctx->flags & BLK_MQ_F_BLOCKING)
27 cleanup_srcu_struct(hctx->srcu);
28 blk_free_flush_queue(hctx->fq);
29 sbitmap_free(&hctx->ctx_map);
30 free_cpumask_var(hctx->cpumask);
35 struct blk_mq_ctx_sysfs_entry {
36 struct attribute attr;
37 ssize_t (*show)(struct blk_mq_ctx *, char *);
38 ssize_t (*store)(struct blk_mq_ctx *, const char *, size_t);
41 struct blk_mq_hw_ctx_sysfs_entry {
42 struct attribute attr;
43 ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
44 ssize_t (*store)(struct blk_mq_hw_ctx *, const char *, size_t);
47 static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
50 struct blk_mq_ctx_sysfs_entry *entry;
51 struct blk_mq_ctx *ctx;
52 struct request_queue *q;
55 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
56 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
63 mutex_lock(&q->sysfs_lock);
64 if (!blk_queue_dying(q))
65 res = entry->show(ctx, page);
66 mutex_unlock(&q->sysfs_lock);
70 static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
71 const char *page, size_t length)
73 struct blk_mq_ctx_sysfs_entry *entry;
74 struct blk_mq_ctx *ctx;
75 struct request_queue *q;
78 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
79 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
86 mutex_lock(&q->sysfs_lock);
87 if (!blk_queue_dying(q))
88 res = entry->store(ctx, page, length);
89 mutex_unlock(&q->sysfs_lock);
93 static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
94 struct attribute *attr, char *page)
96 struct blk_mq_hw_ctx_sysfs_entry *entry;
97 struct blk_mq_hw_ctx *hctx;
98 struct request_queue *q;
101 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
102 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
109 mutex_lock(&q->sysfs_lock);
110 if (!blk_queue_dying(q))
111 res = entry->show(hctx, page);
112 mutex_unlock(&q->sysfs_lock);
116 static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
117 struct attribute *attr, const char *page,
120 struct blk_mq_hw_ctx_sysfs_entry *entry;
121 struct blk_mq_hw_ctx *hctx;
122 struct request_queue *q;
125 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
126 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
133 mutex_lock(&q->sysfs_lock);
134 if (!blk_queue_dying(q))
135 res = entry->store(hctx, page, length);
136 mutex_unlock(&q->sysfs_lock);
140 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
143 return sprintf(page, "%u\n", hctx->tags->nr_tags);
146 static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
149 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
152 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
154 const size_t size = PAGE_SIZE - 1;
155 unsigned int i, first = 1;
156 int ret = 0, pos = 0;
158 for_each_cpu(i, hctx->cpumask) {
160 ret = snprintf(pos + page, size - pos, "%u", i);
162 ret = snprintf(pos + page, size - pos, ", %u", i);
164 if (ret >= size - pos)
171 ret = snprintf(pos + page, size + 1 - pos, "\n");
175 static struct attribute *default_ctx_attrs[] = {
179 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
180 .attr = {.name = "nr_tags", .mode = 0444 },
181 .show = blk_mq_hw_sysfs_nr_tags_show,
183 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
184 .attr = {.name = "nr_reserved_tags", .mode = 0444 },
185 .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
187 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
188 .attr = {.name = "cpu_list", .mode = 0444 },
189 .show = blk_mq_hw_sysfs_cpus_show,
192 static struct attribute *default_hw_ctx_attrs[] = {
193 &blk_mq_hw_sysfs_nr_tags.attr,
194 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
195 &blk_mq_hw_sysfs_cpus.attr,
199 static const struct sysfs_ops blk_mq_sysfs_ops = {
200 .show = blk_mq_sysfs_show,
201 .store = blk_mq_sysfs_store,
204 static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
205 .show = blk_mq_hw_sysfs_show,
206 .store = blk_mq_hw_sysfs_store,
209 static struct kobj_type blk_mq_ktype = {
210 .sysfs_ops = &blk_mq_sysfs_ops,
211 .release = blk_mq_sysfs_release,
214 static struct kobj_type blk_mq_ctx_ktype = {
215 .sysfs_ops = &blk_mq_sysfs_ops,
216 .default_attrs = default_ctx_attrs,
217 .release = blk_mq_sysfs_release,
220 static struct kobj_type blk_mq_hw_ktype = {
221 .sysfs_ops = &blk_mq_hw_sysfs_ops,
222 .default_attrs = default_hw_ctx_attrs,
223 .release = blk_mq_hw_sysfs_release,
226 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
228 struct blk_mq_ctx *ctx;
234 hctx_for_each_ctx(hctx, ctx, i)
235 kobject_del(&ctx->kobj);
237 kobject_del(&hctx->kobj);
240 static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
242 struct request_queue *q = hctx->queue;
243 struct blk_mq_ctx *ctx;
249 ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
253 hctx_for_each_ctx(hctx, ctx, i) {
254 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
262 void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
264 struct blk_mq_hw_ctx *hctx;
267 lockdep_assert_held(&q->sysfs_dir_lock);
269 queue_for_each_hw_ctx(q, hctx, i)
270 blk_mq_unregister_hctx(hctx);
272 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
273 kobject_del(&q->mq_kobj);
274 kobject_put(&dev->kobj);
276 q->mq_sysfs_init_done = false;
279 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
281 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
284 void blk_mq_sysfs_deinit(struct request_queue *q)
286 struct blk_mq_ctx *ctx;
289 for_each_possible_cpu(cpu) {
290 ctx = per_cpu_ptr(q->queue_ctx, cpu);
291 kobject_put(&ctx->kobj);
293 kobject_put(&q->mq_kobj);
296 void blk_mq_sysfs_init(struct request_queue *q)
298 struct blk_mq_ctx *ctx;
301 kobject_init(&q->mq_kobj, &blk_mq_ktype);
303 for_each_possible_cpu(cpu) {
304 ctx = per_cpu_ptr(q->queue_ctx, cpu);
305 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
309 int __blk_mq_register_dev(struct device *dev, struct request_queue *q)
311 struct blk_mq_hw_ctx *hctx;
314 WARN_ON_ONCE(!q->kobj.parent);
315 lockdep_assert_held(&q->sysfs_dir_lock);
317 ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
321 kobject_uevent(&q->mq_kobj, KOBJ_ADD);
323 queue_for_each_hw_ctx(q, hctx, i) {
324 ret = blk_mq_register_hctx(hctx);
329 q->mq_sysfs_init_done = true;
336 blk_mq_unregister_hctx(q->queue_hw_ctx[i]);
338 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
339 kobject_del(&q->mq_kobj);
340 kobject_put(&dev->kobj);
344 int blk_mq_register_dev(struct device *dev, struct request_queue *q)
348 mutex_lock(&q->sysfs_lock);
349 ret = __blk_mq_register_dev(dev, q);
350 mutex_unlock(&q->sysfs_lock);
354 EXPORT_SYMBOL_GPL(blk_mq_register_dev);
356 void blk_mq_sysfs_unregister(struct request_queue *q)
358 struct blk_mq_hw_ctx *hctx;
361 mutex_lock(&q->sysfs_dir_lock);
362 if (!q->mq_sysfs_init_done)
365 queue_for_each_hw_ctx(q, hctx, i)
366 blk_mq_unregister_hctx(hctx);
369 mutex_unlock(&q->sysfs_dir_lock);
372 int blk_mq_sysfs_register(struct request_queue *q)
374 struct blk_mq_hw_ctx *hctx;
377 mutex_lock(&q->sysfs_dir_lock);
378 if (!q->mq_sysfs_init_done)
381 queue_for_each_hw_ctx(q, hctx, i) {
382 ret = blk_mq_register_hctx(hctx);
388 mutex_unlock(&q->sysfs_dir_lock);