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
)
19 struct blk_mq_ctxs
*ctxs
= container_of(kobj
, struct blk_mq_ctxs
, kobj
);
21 free_percpu(ctxs
->queue_ctx
);
25 static void blk_mq_ctx_sysfs_release(struct kobject
*kobj
)
27 struct blk_mq_ctx
*ctx
= container_of(kobj
, struct blk_mq_ctx
, kobj
);
29 /* ctx->ctxs won't be released until all ctx are freed */
30 kobject_put(&ctx
->ctxs
->kobj
);
33 static void blk_mq_hw_sysfs_release(struct kobject
*kobj
)
35 struct blk_mq_hw_ctx
*hctx
= container_of(kobj
, struct blk_mq_hw_ctx
,
38 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
39 cleanup_srcu_struct(hctx
->srcu
);
40 blk_free_flush_queue(hctx
->fq
);
41 sbitmap_free(&hctx
->ctx_map
);
42 free_cpumask_var(hctx
->cpumask
);
47 struct blk_mq_ctx_sysfs_entry
{
48 struct attribute attr
;
49 ssize_t (*show
)(struct blk_mq_ctx
*, char *);
50 ssize_t (*store
)(struct blk_mq_ctx
*, const char *, size_t);
53 struct blk_mq_hw_ctx_sysfs_entry
{
54 struct attribute attr
;
55 ssize_t (*show
)(struct blk_mq_hw_ctx
*, char *);
56 ssize_t (*store
)(struct blk_mq_hw_ctx
*, const char *, size_t);
59 static ssize_t
blk_mq_sysfs_show(struct kobject
*kobj
, struct attribute
*attr
,
62 struct blk_mq_ctx_sysfs_entry
*entry
;
63 struct blk_mq_ctx
*ctx
;
64 struct request_queue
*q
;
67 entry
= container_of(attr
, struct blk_mq_ctx_sysfs_entry
, attr
);
68 ctx
= container_of(kobj
, struct blk_mq_ctx
, kobj
);
75 mutex_lock(&q
->sysfs_lock
);
76 if (!blk_queue_dying(q
))
77 res
= entry
->show(ctx
, page
);
78 mutex_unlock(&q
->sysfs_lock
);
82 static ssize_t
blk_mq_sysfs_store(struct kobject
*kobj
, struct attribute
*attr
,
83 const char *page
, size_t length
)
85 struct blk_mq_ctx_sysfs_entry
*entry
;
86 struct blk_mq_ctx
*ctx
;
87 struct request_queue
*q
;
90 entry
= container_of(attr
, struct blk_mq_ctx_sysfs_entry
, attr
);
91 ctx
= container_of(kobj
, struct blk_mq_ctx
, kobj
);
98 mutex_lock(&q
->sysfs_lock
);
99 if (!blk_queue_dying(q
))
100 res
= entry
->store(ctx
, page
, length
);
101 mutex_unlock(&q
->sysfs_lock
);
105 static ssize_t
blk_mq_hw_sysfs_show(struct kobject
*kobj
,
106 struct attribute
*attr
, char *page
)
108 struct blk_mq_hw_ctx_sysfs_entry
*entry
;
109 struct blk_mq_hw_ctx
*hctx
;
110 struct request_queue
*q
;
113 entry
= container_of(attr
, struct blk_mq_hw_ctx_sysfs_entry
, attr
);
114 hctx
= container_of(kobj
, struct blk_mq_hw_ctx
, kobj
);
121 mutex_lock(&q
->sysfs_lock
);
122 if (!blk_queue_dying(q
))
123 res
= entry
->show(hctx
, page
);
124 mutex_unlock(&q
->sysfs_lock
);
128 static ssize_t
blk_mq_hw_sysfs_store(struct kobject
*kobj
,
129 struct attribute
*attr
, const char *page
,
132 struct blk_mq_hw_ctx_sysfs_entry
*entry
;
133 struct blk_mq_hw_ctx
*hctx
;
134 struct request_queue
*q
;
137 entry
= container_of(attr
, struct blk_mq_hw_ctx_sysfs_entry
, attr
);
138 hctx
= container_of(kobj
, struct blk_mq_hw_ctx
, kobj
);
145 mutex_lock(&q
->sysfs_lock
);
146 if (!blk_queue_dying(q
))
147 res
= entry
->store(hctx
, page
, length
);
148 mutex_unlock(&q
->sysfs_lock
);
152 static ssize_t
blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx
*hctx
,
155 return sprintf(page
, "%u\n", hctx
->tags
->nr_tags
);
158 static ssize_t
blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx
*hctx
,
161 return sprintf(page
, "%u\n", hctx
->tags
->nr_reserved_tags
);
164 static ssize_t
blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx
*hctx
, char *page
)
166 unsigned int i
, first
= 1;
169 for_each_cpu(i
, hctx
->cpumask
) {
171 ret
+= sprintf(ret
+ page
, "%u", i
);
173 ret
+= sprintf(ret
+ page
, ", %u", i
);
178 ret
+= sprintf(ret
+ page
, "\n");
182 static struct attribute
*default_ctx_attrs
[] = {
186 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags
= {
187 .attr
= {.name
= "nr_tags", .mode
= 0444 },
188 .show
= blk_mq_hw_sysfs_nr_tags_show
,
190 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags
= {
191 .attr
= {.name
= "nr_reserved_tags", .mode
= 0444 },
192 .show
= blk_mq_hw_sysfs_nr_reserved_tags_show
,
194 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus
= {
195 .attr
= {.name
= "cpu_list", .mode
= 0444 },
196 .show
= blk_mq_hw_sysfs_cpus_show
,
199 static struct attribute
*default_hw_ctx_attrs
[] = {
200 &blk_mq_hw_sysfs_nr_tags
.attr
,
201 &blk_mq_hw_sysfs_nr_reserved_tags
.attr
,
202 &blk_mq_hw_sysfs_cpus
.attr
,
206 static const struct sysfs_ops blk_mq_sysfs_ops
= {
207 .show
= blk_mq_sysfs_show
,
208 .store
= blk_mq_sysfs_store
,
211 static const struct sysfs_ops blk_mq_hw_sysfs_ops
= {
212 .show
= blk_mq_hw_sysfs_show
,
213 .store
= blk_mq_hw_sysfs_store
,
216 static struct kobj_type blk_mq_ktype
= {
217 .sysfs_ops
= &blk_mq_sysfs_ops
,
218 .release
= blk_mq_sysfs_release
,
221 static struct kobj_type blk_mq_ctx_ktype
= {
222 .sysfs_ops
= &blk_mq_sysfs_ops
,
223 .default_attrs
= default_ctx_attrs
,
224 .release
= blk_mq_ctx_sysfs_release
,
227 static struct kobj_type blk_mq_hw_ktype
= {
228 .sysfs_ops
= &blk_mq_hw_sysfs_ops
,
229 .default_attrs
= default_hw_ctx_attrs
,
230 .release
= blk_mq_hw_sysfs_release
,
233 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx
*hctx
)
235 struct blk_mq_ctx
*ctx
;
241 hctx_for_each_ctx(hctx
, ctx
, i
)
242 kobject_del(&ctx
->kobj
);
244 kobject_del(&hctx
->kobj
);
247 static int blk_mq_register_hctx(struct blk_mq_hw_ctx
*hctx
)
249 struct request_queue
*q
= hctx
->queue
;
250 struct blk_mq_ctx
*ctx
;
256 ret
= kobject_add(&hctx
->kobj
, q
->mq_kobj
, "%u", hctx
->queue_num
);
260 hctx_for_each_ctx(hctx
, ctx
, i
) {
261 ret
= kobject_add(&ctx
->kobj
, &hctx
->kobj
, "cpu%u", ctx
->cpu
);
269 void blk_mq_unregister_dev(struct device
*dev
, struct request_queue
*q
)
271 struct blk_mq_hw_ctx
*hctx
;
274 lockdep_assert_held(&q
->sysfs_lock
);
276 queue_for_each_hw_ctx(q
, hctx
, i
)
277 blk_mq_unregister_hctx(hctx
);
279 kobject_uevent(q
->mq_kobj
, KOBJ_REMOVE
);
280 kobject_del(q
->mq_kobj
);
281 kobject_put(&dev
->kobj
);
283 q
->mq_sysfs_init_done
= false;
286 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx
*hctx
)
288 kobject_init(&hctx
->kobj
, &blk_mq_hw_ktype
);
291 void blk_mq_sysfs_deinit(struct request_queue
*q
)
293 struct blk_mq_ctx
*ctx
;
296 for_each_possible_cpu(cpu
) {
297 ctx
= per_cpu_ptr(q
->queue_ctx
, cpu
);
298 kobject_put(&ctx
->kobj
);
300 kobject_put(q
->mq_kobj
);
303 void blk_mq_sysfs_init(struct request_queue
*q
)
305 struct blk_mq_ctx
*ctx
;
308 kobject_init(q
->mq_kobj
, &blk_mq_ktype
);
310 for_each_possible_cpu(cpu
) {
311 ctx
= per_cpu_ptr(q
->queue_ctx
, cpu
);
313 kobject_get(q
->mq_kobj
);
314 kobject_init(&ctx
->kobj
, &blk_mq_ctx_ktype
);
318 int __blk_mq_register_dev(struct device
*dev
, struct request_queue
*q
)
320 struct blk_mq_hw_ctx
*hctx
;
323 WARN_ON_ONCE(!q
->kobj
.parent
);
324 lockdep_assert_held(&q
->sysfs_lock
);
326 ret
= kobject_add(q
->mq_kobj
, kobject_get(&dev
->kobj
), "%s", "mq");
330 kobject_uevent(q
->mq_kobj
, KOBJ_ADD
);
332 queue_for_each_hw_ctx(q
, hctx
, i
) {
333 ret
= blk_mq_register_hctx(hctx
);
338 q
->mq_sysfs_init_done
= true;
345 blk_mq_unregister_hctx(q
->queue_hw_ctx
[i
]);
347 kobject_uevent(q
->mq_kobj
, KOBJ_REMOVE
);
348 kobject_del(q
->mq_kobj
);
349 kobject_put(&dev
->kobj
);
353 int blk_mq_register_dev(struct device
*dev
, struct request_queue
*q
)
357 mutex_lock(&q
->sysfs_lock
);
358 ret
= __blk_mq_register_dev(dev
, q
);
359 mutex_unlock(&q
->sysfs_lock
);
364 void blk_mq_sysfs_unregister(struct request_queue
*q
)
366 struct blk_mq_hw_ctx
*hctx
;
369 mutex_lock(&q
->sysfs_lock
);
370 if (!q
->mq_sysfs_init_done
)
373 queue_for_each_hw_ctx(q
, hctx
, i
)
374 blk_mq_unregister_hctx(hctx
);
377 mutex_unlock(&q
->sysfs_lock
);
380 int blk_mq_sysfs_register(struct request_queue
*q
)
382 struct blk_mq_hw_ctx
*hctx
;
385 mutex_lock(&q
->sysfs_lock
);
386 if (!q
->mq_sysfs_init_done
)
389 queue_for_each_hw_ctx(q
, hctx
, i
) {
390 ret
= blk_mq_register_hctx(hctx
);
396 mutex_unlock(&q
->sysfs_lock
);