]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq-sysfs.c
blk-mq: move hctx lock/unlock into a helper
[mirror_ubuntu-bionic-kernel.git] / block / blk-mq-sysfs.c
CommitLineData
320ae51f
JA
1#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>
84c8ff7f 13#include "blk.h"
320ae51f
JA
14#include "blk-mq.h"
15#include "blk-mq-tag.h"
16
17static void blk_mq_sysfs_release(struct kobject *kobj)
18{
19}
20
6c8b232e
ML
21static void blk_mq_hw_sysfs_release(struct kobject *kobj)
22{
23 struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
24 kobj);
84c8ff7f
ML
25
26 if (hctx->flags & BLK_MQ_F_BLOCKING)
27 cleanup_srcu_struct(hctx->queue_rq_srcu);
28 blk_free_flush_queue(hctx->fq);
29 sbitmap_free(&hctx->ctx_map);
01388df3 30 free_cpumask_var(hctx->cpumask);
6c8b232e
ML
31 kfree(hctx->ctxs);
32 kfree(hctx);
33}
34
320ae51f
JA
35struct 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);
39};
40
41struct 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);
45};
46
47static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
48 char *page)
49{
50 struct blk_mq_ctx_sysfs_entry *entry;
51 struct blk_mq_ctx *ctx;
52 struct request_queue *q;
53 ssize_t res;
54
55 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
56 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
57 q = ctx->queue;
58
59 if (!entry->show)
60 return -EIO;
61
62 res = -ENOENT;
63 mutex_lock(&q->sysfs_lock);
64 if (!blk_queue_dying(q))
65 res = entry->show(ctx, page);
66 mutex_unlock(&q->sysfs_lock);
67 return res;
68}
69
70static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
71 const char *page, size_t length)
72{
73 struct blk_mq_ctx_sysfs_entry *entry;
74 struct blk_mq_ctx *ctx;
75 struct request_queue *q;
76 ssize_t res;
77
78 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
79 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
80 q = ctx->queue;
81
82 if (!entry->store)
83 return -EIO;
84
85 res = -ENOENT;
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);
90 return res;
91}
92
93static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
94 struct attribute *attr, char *page)
95{
96 struct blk_mq_hw_ctx_sysfs_entry *entry;
97 struct blk_mq_hw_ctx *hctx;
98 struct request_queue *q;
99 ssize_t res;
100
101 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
102 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
103 q = hctx->queue;
104
105 if (!entry->show)
106 return -EIO;
107
108 res = -ENOENT;
109 mutex_lock(&q->sysfs_lock);
110 if (!blk_queue_dying(q))
111 res = entry->show(hctx, page);
112 mutex_unlock(&q->sysfs_lock);
113 return res;
114}
115
116static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
117 struct attribute *attr, const char *page,
118 size_t length)
119{
120 struct blk_mq_hw_ctx_sysfs_entry *entry;
121 struct blk_mq_hw_ctx *hctx;
122 struct request_queue *q;
123 ssize_t res;
124
125 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
126 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
127 q = hctx->queue;
128
129 if (!entry->store)
130 return -EIO;
131
132 res = -ENOENT;
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);
137 return res;
138}
139
d96b37c0
OS
140static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
141 char *page)
bd166ef1 142{
d96b37c0 143 return sprintf(page, "%u\n", hctx->tags->nr_tags);
bd166ef1
JA
144}
145
d96b37c0
OS
146static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
147 char *page)
320ae51f 148{
d96b37c0 149 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
320ae51f
JA
150}
151
676141e4
JA
152static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
153{
d00dc13c 154 const size_t size = PAGE_SIZE - 1;
cb2da43e 155 unsigned int i, first = 1;
d00dc13c 156 int ret = 0, pos = 0;
676141e4 157
cb2da43e 158 for_each_cpu(i, hctx->cpumask) {
676141e4 159 if (first)
d00dc13c 160 ret = snprintf(pos + page, size - pos, "%u", i);
676141e4 161 else
d00dc13c
ML
162 ret = snprintf(pos + page, size - pos, ", %u", i);
163
164 if (ret >= size - pos)
165 break;
676141e4
JA
166
167 first = 0;
d00dc13c 168 pos += ret;
676141e4
JA
169 }
170
61fcd9bc 171 ret = snprintf(pos + page, size + 1 - pos, "\n");
d00dc13c 172 return pos + ret;
676141e4
JA
173}
174
320ae51f 175static struct attribute *default_ctx_attrs[] = {
320ae51f
JA
176 NULL,
177};
178
d96b37c0
OS
179static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
180 .attr = {.name = "nr_tags", .mode = S_IRUGO },
181 .show = blk_mq_hw_sysfs_nr_tags_show,
182};
183static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
184 .attr = {.name = "nr_reserved_tags", .mode = S_IRUGO },
185 .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
186};
676141e4
JA
187static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
188 .attr = {.name = "cpu_list", .mode = S_IRUGO },
189 .show = blk_mq_hw_sysfs_cpus_show,
190};
320ae51f
JA
191
192static struct attribute *default_hw_ctx_attrs[] = {
d96b37c0
OS
193 &blk_mq_hw_sysfs_nr_tags.attr,
194 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
676141e4 195 &blk_mq_hw_sysfs_cpus.attr,
320ae51f
JA
196 NULL,
197};
198
199static const struct sysfs_ops blk_mq_sysfs_ops = {
200 .show = blk_mq_sysfs_show,
201 .store = blk_mq_sysfs_store,
202};
203
204static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
205 .show = blk_mq_hw_sysfs_show,
206 .store = blk_mq_hw_sysfs_store,
207};
208
209static struct kobj_type blk_mq_ktype = {
210 .sysfs_ops = &blk_mq_sysfs_ops,
211 .release = blk_mq_sysfs_release,
212};
213
214static struct kobj_type blk_mq_ctx_ktype = {
215 .sysfs_ops = &blk_mq_sysfs_ops,
216 .default_attrs = default_ctx_attrs,
74170118 217 .release = blk_mq_sysfs_release,
320ae51f
JA
218};
219
220static struct kobj_type blk_mq_hw_ktype = {
221 .sysfs_ops = &blk_mq_hw_sysfs_ops,
222 .default_attrs = default_hw_ctx_attrs,
6c8b232e 223 .release = blk_mq_hw_sysfs_release,
320ae51f
JA
224};
225
ee3c5db0 226static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
67aec14c
JA
227{
228 struct blk_mq_ctx *ctx;
229 int i;
230
4593fdbe 231 if (!hctx->nr_ctx)
67aec14c
JA
232 return;
233
234 hctx_for_each_ctx(hctx, ctx, i)
235 kobject_del(&ctx->kobj);
236
237 kobject_del(&hctx->kobj);
238}
239
ee3c5db0 240static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
67aec14c
JA
241{
242 struct request_queue *q = hctx->queue;
243 struct blk_mq_ctx *ctx;
244 int i, ret;
245
4593fdbe 246 if (!hctx->nr_ctx)
67aec14c
JA
247 return 0;
248
249 ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
250 if (ret)
251 return ret;
252
253 hctx_for_each_ctx(hctx, ctx, i) {
254 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
255 if (ret)
256 break;
257 }
258
259 return ret;
260}
261
b21d5b30 262static void __blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
320ae51f 263{
85157366 264 struct blk_mq_hw_ctx *hctx;
7ea5fe31 265 int i;
85157366 266
2d0364c8
BVA
267 lockdep_assert_held(&q->sysfs_lock);
268
6c8b232e 269 queue_for_each_hw_ctx(q, hctx, i)
67aec14c
JA
270 blk_mq_unregister_hctx(hctx);
271
320ae51f
JA
272 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
273 kobject_del(&q->mq_kobj);
b21d5b30 274 kobject_put(&dev->kobj);
4593fdbe
AM
275
276 q->mq_sysfs_init_done = false;
c0f3fd2b
JA
277}
278
b21d5b30 279void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
c0f3fd2b 280{
2d0364c8 281 mutex_lock(&q->sysfs_lock);
b21d5b30 282 __blk_mq_unregister_dev(dev, q);
2d0364c8 283 mutex_unlock(&q->sysfs_lock);
320ae51f
JA
284}
285
868f2f0b
KB
286void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
287{
288 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
289}
290
7ea5fe31
ML
291void blk_mq_sysfs_deinit(struct request_queue *q)
292{
293 struct blk_mq_ctx *ctx;
294 int cpu;
295
296 for_each_possible_cpu(cpu) {
297 ctx = per_cpu_ptr(q->queue_ctx, cpu);
298 kobject_put(&ctx->kobj);
299 }
300 kobject_put(&q->mq_kobj);
301}
302
737f98cf 303void blk_mq_sysfs_init(struct request_queue *q)
67aec14c 304{
67aec14c 305 struct blk_mq_ctx *ctx;
897bb0c7 306 int cpu;
67aec14c
JA
307
308 kobject_init(&q->mq_kobj, &blk_mq_ktype);
309
897bb0c7
TG
310 for_each_possible_cpu(cpu) {
311 ctx = per_cpu_ptr(q->queue_ctx, cpu);
06a41a99 312 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
897bb0c7 313 }
67aec14c
JA
314}
315
2d0364c8 316int __blk_mq_register_dev(struct device *dev, struct request_queue *q)
320ae51f 317{
320ae51f 318 struct blk_mq_hw_ctx *hctx;
67aec14c 319 int ret, i;
320ae51f 320
2d0364c8
BVA
321 WARN_ON_ONCE(!q->kobj.parent);
322 lockdep_assert_held(&q->sysfs_lock);
4593fdbe 323
320ae51f
JA
324 ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
325 if (ret < 0)
4593fdbe 326 goto out;
320ae51f
JA
327
328 kobject_uevent(&q->mq_kobj, KOBJ_ADD);
329
330 queue_for_each_hw_ctx(q, hctx, i) {
67aec14c 331 ret = blk_mq_register_hctx(hctx);
320ae51f 332 if (ret)
f05d1ba7 333 goto unreg;
320ae51f
JA
334 }
335
f05d1ba7 336 q->mq_sysfs_init_done = true;
2d0364c8 337
4593fdbe 338out:
2d0364c8 339 return ret;
f05d1ba7
BVA
340
341unreg:
342 while (--i >= 0)
343 blk_mq_unregister_hctx(q->queue_hw_ctx[i]);
344
f05d1ba7
BVA
345 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
346 kobject_del(&q->mq_kobj);
347 kobject_put(&dev->kobj);
348 return ret;
2d0364c8
BVA
349}
350
351int blk_mq_register_dev(struct device *dev, struct request_queue *q)
352{
353 int ret;
354
355 mutex_lock(&q->sysfs_lock);
356 ret = __blk_mq_register_dev(dev, q);
357 mutex_unlock(&q->sysfs_lock);
320ae51f 358
4593fdbe 359 return ret;
320ae51f 360}
b21d5b30 361EXPORT_SYMBOL_GPL(blk_mq_register_dev);
67aec14c
JA
362
363void blk_mq_sysfs_unregister(struct request_queue *q)
364{
365 struct blk_mq_hw_ctx *hctx;
366 int i;
367
2d0364c8 368 mutex_lock(&q->sysfs_lock);
4593fdbe 369 if (!q->mq_sysfs_init_done)
2d0364c8 370 goto unlock;
4593fdbe 371
67aec14c
JA
372 queue_for_each_hw_ctx(q, hctx, i)
373 blk_mq_unregister_hctx(hctx);
2d0364c8
BVA
374
375unlock:
376 mutex_unlock(&q->sysfs_lock);
67aec14c
JA
377}
378
379int blk_mq_sysfs_register(struct request_queue *q)
380{
381 struct blk_mq_hw_ctx *hctx;
382 int i, ret = 0;
383
2d0364c8 384 mutex_lock(&q->sysfs_lock);
4593fdbe 385 if (!q->mq_sysfs_init_done)
2d0364c8 386 goto unlock;
4593fdbe 387
67aec14c
JA
388 queue_for_each_hw_ctx(q, hctx, i) {
389 ret = blk_mq_register_hctx(hctx);
390 if (ret)
391 break;
392 }
393
2d0364c8
BVA
394unlock:
395 mutex_unlock(&q->sysfs_lock);
396
67aec14c
JA
397 return ret;
398}