]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq-sysfs.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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>
13#include "blk-mq.h"
14#include "blk-mq-tag.h"
15
16static void blk_mq_sysfs_release(struct kobject *kobj)
17{
18}
19
20struct 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);
24};
25
26struct 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);
30};
31
32static ssize_t blk_mq_sysfs_show(struct kobject *kobj, struct attribute *attr,
33 char *page)
34{
35 struct blk_mq_ctx_sysfs_entry *entry;
36 struct blk_mq_ctx *ctx;
37 struct request_queue *q;
38 ssize_t res;
39
40 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
41 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
42 q = ctx->queue;
43
44 if (!entry->show)
45 return -EIO;
46
47 res = -ENOENT;
48 mutex_lock(&q->sysfs_lock);
49 if (!blk_queue_dying(q))
50 res = entry->show(ctx, page);
51 mutex_unlock(&q->sysfs_lock);
52 return res;
53}
54
55static ssize_t blk_mq_sysfs_store(struct kobject *kobj, struct attribute *attr,
56 const char *page, size_t length)
57{
58 struct blk_mq_ctx_sysfs_entry *entry;
59 struct blk_mq_ctx *ctx;
60 struct request_queue *q;
61 ssize_t res;
62
63 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
64 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
65 q = ctx->queue;
66
67 if (!entry->store)
68 return -EIO;
69
70 res = -ENOENT;
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);
75 return res;
76}
77
78static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
79 struct attribute *attr, char *page)
80{
81 struct blk_mq_hw_ctx_sysfs_entry *entry;
82 struct blk_mq_hw_ctx *hctx;
83 struct request_queue *q;
84 ssize_t res;
85
86 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
87 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
88 q = hctx->queue;
89
90 if (!entry->show)
91 return -EIO;
92
93 res = -ENOENT;
94 mutex_lock(&q->sysfs_lock);
95 if (!blk_queue_dying(q))
96 res = entry->show(hctx, page);
97 mutex_unlock(&q->sysfs_lock);
98 return res;
99}
100
101static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
102 struct attribute *attr, const char *page,
103 size_t length)
104{
105 struct blk_mq_hw_ctx_sysfs_entry *entry;
106 struct blk_mq_hw_ctx *hctx;
107 struct request_queue *q;
108 ssize_t res;
109
110 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
111 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
112 q = hctx->queue;
113
114 if (!entry->store)
115 return -EIO;
116
117 res = -ENOENT;
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);
122 return res;
123}
124
d96b37c0
OS
125static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
126 char *page)
bd166ef1 127{
d96b37c0 128 return sprintf(page, "%u\n", hctx->tags->nr_tags);
bd166ef1
JA
129}
130
d96b37c0
OS
131static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
132 char *page)
320ae51f 133{
d96b37c0 134 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
320ae51f
JA
135}
136
676141e4
JA
137static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
138{
cb2da43e 139 unsigned int i, first = 1;
676141e4
JA
140 ssize_t ret = 0;
141
cb2da43e 142 for_each_cpu(i, hctx->cpumask) {
676141e4
JA
143 if (first)
144 ret += sprintf(ret + page, "%u", i);
145 else
146 ret += sprintf(ret + page, ", %u", i);
147
148 first = 0;
149 }
150
676141e4
JA
151 ret += sprintf(ret + page, "\n");
152 return ret;
153}
154
320ae51f 155static struct attribute *default_ctx_attrs[] = {
320ae51f
JA
156 NULL,
157};
158
d96b37c0
OS
159static 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,
162};
163static 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,
166};
676141e4
JA
167static 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,
170};
320ae51f
JA
171
172static struct attribute *default_hw_ctx_attrs[] = {
d96b37c0
OS
173 &blk_mq_hw_sysfs_nr_tags.attr,
174 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
676141e4 175 &blk_mq_hw_sysfs_cpus.attr,
320ae51f
JA
176 NULL,
177};
178
179static const struct sysfs_ops blk_mq_sysfs_ops = {
180 .show = blk_mq_sysfs_show,
181 .store = blk_mq_sysfs_store,
182};
183
184static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
185 .show = blk_mq_hw_sysfs_show,
186 .store = blk_mq_hw_sysfs_store,
187};
188
189static struct kobj_type blk_mq_ktype = {
190 .sysfs_ops = &blk_mq_sysfs_ops,
191 .release = blk_mq_sysfs_release,
192};
193
194static struct kobj_type blk_mq_ctx_ktype = {
195 .sysfs_ops = &blk_mq_sysfs_ops,
196 .default_attrs = default_ctx_attrs,
74170118 197 .release = blk_mq_sysfs_release,
320ae51f
JA
198};
199
200static struct kobj_type blk_mq_hw_ktype = {
201 .sysfs_ops = &blk_mq_hw_sysfs_ops,
202 .default_attrs = default_hw_ctx_attrs,
74170118 203 .release = blk_mq_sysfs_release,
320ae51f
JA
204};
205
ee3c5db0 206static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
67aec14c
JA
207{
208 struct blk_mq_ctx *ctx;
209 int i;
210
4593fdbe 211 if (!hctx->nr_ctx)
67aec14c
JA
212 return;
213
214 hctx_for_each_ctx(hctx, ctx, i)
215 kobject_del(&ctx->kobj);
216
217 kobject_del(&hctx->kobj);
218}
219
ee3c5db0 220static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
67aec14c
JA
221{
222 struct request_queue *q = hctx->queue;
223 struct blk_mq_ctx *ctx;
224 int i, ret;
225
4593fdbe 226 if (!hctx->nr_ctx)
67aec14c
JA
227 return 0;
228
229 ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
230 if (ret)
231 return ret;
232
233 hctx_for_each_ctx(hctx, ctx, i) {
234 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
235 if (ret)
236 break;
237 }
238
239 return ret;
240}
241
b21d5b30 242static void __blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
320ae51f 243{
85157366
AV
244 struct blk_mq_hw_ctx *hctx;
245 struct blk_mq_ctx *ctx;
246 int i, j;
247
248 queue_for_each_hw_ctx(q, hctx, i) {
67aec14c
JA
249 blk_mq_unregister_hctx(hctx);
250
251 hctx_for_each_ctx(hctx, ctx, j)
85157366 252 kobject_put(&ctx->kobj);
67aec14c 253
85157366
AV
254 kobject_put(&hctx->kobj);
255 }
320ae51f 256
62ebce16 257 blk_mq_debugfs_unregister_hctxs(q);
07e4fead 258
320ae51f
JA
259 kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
260 kobject_del(&q->mq_kobj);
85157366 261 kobject_put(&q->mq_kobj);
320ae51f 262
b21d5b30 263 kobject_put(&dev->kobj);
4593fdbe
AM
264
265 q->mq_sysfs_init_done = false;
c0f3fd2b
JA
266}
267
b21d5b30 268void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
c0f3fd2b
JA
269{
270 blk_mq_disable_hotplug();
b21d5b30 271 __blk_mq_unregister_dev(dev, q);
4593fdbe 272 blk_mq_enable_hotplug();
320ae51f
JA
273}
274
868f2f0b
KB
275void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
276{
277 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
278}
279
67aec14c
JA
280static void blk_mq_sysfs_init(struct request_queue *q)
281{
67aec14c 282 struct blk_mq_ctx *ctx;
897bb0c7 283 int cpu;
67aec14c
JA
284
285 kobject_init(&q->mq_kobj, &blk_mq_ktype);
286
897bb0c7
TG
287 for_each_possible_cpu(cpu) {
288 ctx = per_cpu_ptr(q->queue_ctx, cpu);
06a41a99 289 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
897bb0c7 290 }
67aec14c
JA
291}
292
b21d5b30 293int blk_mq_register_dev(struct device *dev, struct request_queue *q)
320ae51f 294{
320ae51f 295 struct blk_mq_hw_ctx *hctx;
67aec14c 296 int ret, i;
320ae51f 297
4593fdbe
AM
298 blk_mq_disable_hotplug();
299
67aec14c 300 blk_mq_sysfs_init(q);
320ae51f
JA
301
302 ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
303 if (ret < 0)
4593fdbe 304 goto out;
320ae51f
JA
305
306 kobject_uevent(&q->mq_kobj, KOBJ_ADD);
307
07e4fead
OS
308 blk_mq_debugfs_register(q, kobject_name(&dev->kobj));
309
320ae51f 310 queue_for_each_hw_ctx(q, hctx, i) {
67aec14c 311 ret = blk_mq_register_hctx(hctx);
320ae51f
JA
312 if (ret)
313 break;
320ae51f
JA
314 }
315
4593fdbe 316 if (ret)
b21d5b30 317 __blk_mq_unregister_dev(dev, q);
4593fdbe
AM
318 else
319 q->mq_sysfs_init_done = true;
320out:
321 blk_mq_enable_hotplug();
320ae51f 322
4593fdbe 323 return ret;
320ae51f 324}
b21d5b30 325EXPORT_SYMBOL_GPL(blk_mq_register_dev);
67aec14c
JA
326
327void blk_mq_sysfs_unregister(struct request_queue *q)
328{
329 struct blk_mq_hw_ctx *hctx;
330 int i;
331
4593fdbe
AM
332 if (!q->mq_sysfs_init_done)
333 return;
334
07e4fead
OS
335 blk_mq_debugfs_unregister_hctxs(q);
336
67aec14c
JA
337 queue_for_each_hw_ctx(q, hctx, i)
338 blk_mq_unregister_hctx(hctx);
339}
340
341int blk_mq_sysfs_register(struct request_queue *q)
342{
343 struct blk_mq_hw_ctx *hctx;
344 int i, ret = 0;
345
4593fdbe
AM
346 if (!q->mq_sysfs_init_done)
347 return ret;
348
07e4fead
OS
349 blk_mq_debugfs_register_hctxs(q);
350
67aec14c
JA
351 queue_for_each_hw_ctx(q, hctx, i) {
352 ret = blk_mq_register_hctx(hctx);
353 if (ret)
354 break;
355 }
356
357 return ret;
358}