Linux 5.1.5
[linux/fpc-iii.git] / block / blk-mq-sysfs.c
blob4040e62c3737a2f852ce5a8fb0042ca0c899b1d3
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>
12 #include <linux/blk-mq.h>
13 #include "blk.h"
14 #include "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);
22 kfree(ctxs);
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,
36 kobj);
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);
43 kfree(hctx->ctxs);
44 kfree(hctx);
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,
60 char *page)
62 struct blk_mq_ctx_sysfs_entry *entry;
63 struct blk_mq_ctx *ctx;
64 struct request_queue *q;
65 ssize_t res;
67 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
68 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
69 q = ctx->queue;
71 if (!entry->show)
72 return -EIO;
74 res = -ENOENT;
75 mutex_lock(&q->sysfs_lock);
76 if (!blk_queue_dying(q))
77 res = entry->show(ctx, page);
78 mutex_unlock(&q->sysfs_lock);
79 return res;
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;
88 ssize_t res;
90 entry = container_of(attr, struct blk_mq_ctx_sysfs_entry, attr);
91 ctx = container_of(kobj, struct blk_mq_ctx, kobj);
92 q = ctx->queue;
94 if (!entry->store)
95 return -EIO;
97 res = -ENOENT;
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);
102 return res;
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;
111 ssize_t res;
113 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
114 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
115 q = hctx->queue;
117 if (!entry->show)
118 return -EIO;
120 res = -ENOENT;
121 mutex_lock(&q->sysfs_lock);
122 if (!blk_queue_dying(q))
123 res = entry->show(hctx, page);
124 mutex_unlock(&q->sysfs_lock);
125 return res;
128 static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
129 struct attribute *attr, const char *page,
130 size_t length)
132 struct blk_mq_hw_ctx_sysfs_entry *entry;
133 struct blk_mq_hw_ctx *hctx;
134 struct request_queue *q;
135 ssize_t res;
137 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
138 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
139 q = hctx->queue;
141 if (!entry->store)
142 return -EIO;
144 res = -ENOENT;
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);
149 return res;
152 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
153 char *page)
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,
159 char *page)
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;
167 ssize_t ret = 0;
169 for_each_cpu(i, hctx->cpumask) {
170 if (first)
171 ret += sprintf(ret + page, "%u", i);
172 else
173 ret += sprintf(ret + page, ", %u", i);
175 first = 0;
178 ret += sprintf(ret + page, "\n");
179 return ret;
182 static struct attribute *default_ctx_attrs[] = {
183 NULL,
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,
203 NULL,
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;
236 int i;
238 if (!hctx->nr_ctx)
239 return;
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;
251 int i, ret;
253 if (!hctx->nr_ctx)
254 return 0;
256 ret = kobject_add(&hctx->kobj, q->mq_kobj, "%u", hctx->queue_num);
257 if (ret)
258 return ret;
260 hctx_for_each_ctx(hctx, ctx, i) {
261 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
262 if (ret)
263 break;
266 return ret;
269 void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
271 struct blk_mq_hw_ctx *hctx;
272 int i;
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;
294 int cpu;
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;
306 int cpu;
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;
321 int ret, i;
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");
327 if (ret < 0)
328 goto out;
330 kobject_uevent(q->mq_kobj, KOBJ_ADD);
332 queue_for_each_hw_ctx(q, hctx, i) {
333 ret = blk_mq_register_hctx(hctx);
334 if (ret)
335 goto unreg;
338 q->mq_sysfs_init_done = true;
340 out:
341 return ret;
343 unreg:
344 while (--i >= 0)
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);
350 return ret;
353 int blk_mq_register_dev(struct device *dev, struct request_queue *q)
355 int ret;
357 mutex_lock(&q->sysfs_lock);
358 ret = __blk_mq_register_dev(dev, q);
359 mutex_unlock(&q->sysfs_lock);
361 return ret;
364 void blk_mq_sysfs_unregister(struct request_queue *q)
366 struct blk_mq_hw_ctx *hctx;
367 int i;
369 mutex_lock(&q->sysfs_lock);
370 if (!q->mq_sysfs_init_done)
371 goto unlock;
373 queue_for_each_hw_ctx(q, hctx, i)
374 blk_mq_unregister_hctx(hctx);
376 unlock:
377 mutex_unlock(&q->sysfs_lock);
380 int blk_mq_sysfs_register(struct request_queue *q)
382 struct blk_mq_hw_ctx *hctx;
383 int i, ret = 0;
385 mutex_lock(&q->sysfs_lock);
386 if (!q->mq_sysfs_init_done)
387 goto unlock;
389 queue_for_each_hw_ctx(q, hctx, i) {
390 ret = blk_mq_register_hctx(hctx);
391 if (ret)
392 break;
395 unlock:
396 mutex_unlock(&q->sysfs_lock);
398 return ret;