USB: serial: keyspan: fix NULL-derefs on open() and write()
[platform/kernel/linux-rpi.git] / block / blk-mq-sysfs.c
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.h"
14 #include "blk-mq.h"
15 #include "blk-mq-tag.h"
16
17 static void blk_mq_sysfs_release(struct kobject *kobj)
18 {
19 }
20
21 static 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);
25
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);
31         kfree(hctx->ctxs);
32         kfree(hctx);
33 }
34
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);
39 };
40
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);
45 };
46
47 static 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
70 static 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
93 static 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
116 static 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
140 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
141                                             char *page)
142 {
143         return sprintf(page, "%u\n", hctx->tags->nr_tags);
144 }
145
146 static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
147                                                      char *page)
148 {
149         return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
150 }
151
152 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
153 {
154         unsigned int i, first = 1;
155         ssize_t ret = 0;
156
157         for_each_cpu(i, hctx->cpumask) {
158                 if (first)
159                         ret += sprintf(ret + page, "%u", i);
160                 else
161                         ret += sprintf(ret + page, ", %u", i);
162
163                 first = 0;
164         }
165
166         ret += sprintf(ret + page, "\n");
167         return ret;
168 }
169
170 static struct attribute *default_ctx_attrs[] = {
171         NULL,
172 };
173
174 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
175         .attr = {.name = "nr_tags", .mode = 0444 },
176         .show = blk_mq_hw_sysfs_nr_tags_show,
177 };
178 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
179         .attr = {.name = "nr_reserved_tags", .mode = 0444 },
180         .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
181 };
182 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
183         .attr = {.name = "cpu_list", .mode = 0444 },
184         .show = blk_mq_hw_sysfs_cpus_show,
185 };
186
187 static struct attribute *default_hw_ctx_attrs[] = {
188         &blk_mq_hw_sysfs_nr_tags.attr,
189         &blk_mq_hw_sysfs_nr_reserved_tags.attr,
190         &blk_mq_hw_sysfs_cpus.attr,
191         NULL,
192 };
193
194 static const struct sysfs_ops blk_mq_sysfs_ops = {
195         .show   = blk_mq_sysfs_show,
196         .store  = blk_mq_sysfs_store,
197 };
198
199 static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
200         .show   = blk_mq_hw_sysfs_show,
201         .store  = blk_mq_hw_sysfs_store,
202 };
203
204 static struct kobj_type blk_mq_ktype = {
205         .sysfs_ops      = &blk_mq_sysfs_ops,
206         .release        = blk_mq_sysfs_release,
207 };
208
209 static struct kobj_type blk_mq_ctx_ktype = {
210         .sysfs_ops      = &blk_mq_sysfs_ops,
211         .default_attrs  = default_ctx_attrs,
212         .release        = blk_mq_sysfs_release,
213 };
214
215 static struct kobj_type blk_mq_hw_ktype = {
216         .sysfs_ops      = &blk_mq_hw_sysfs_ops,
217         .default_attrs  = default_hw_ctx_attrs,
218         .release        = blk_mq_hw_sysfs_release,
219 };
220
221 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
222 {
223         struct blk_mq_ctx *ctx;
224         int i;
225
226         if (!hctx->nr_ctx)
227                 return;
228
229         hctx_for_each_ctx(hctx, ctx, i)
230                 kobject_del(&ctx->kobj);
231
232         kobject_del(&hctx->kobj);
233 }
234
235 static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
236 {
237         struct request_queue *q = hctx->queue;
238         struct blk_mq_ctx *ctx;
239         int i, ret;
240
241         if (!hctx->nr_ctx)
242                 return 0;
243
244         ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
245         if (ret)
246                 return ret;
247
248         hctx_for_each_ctx(hctx, ctx, i) {
249                 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
250                 if (ret)
251                         break;
252         }
253
254         return ret;
255 }
256
257 void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
258 {
259         struct blk_mq_hw_ctx *hctx;
260         int i;
261
262         lockdep_assert_held(&q->sysfs_lock);
263
264         queue_for_each_hw_ctx(q, hctx, i)
265                 blk_mq_unregister_hctx(hctx);
266
267         kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
268         kobject_del(&q->mq_kobj);
269         kobject_put(&dev->kobj);
270
271         q->mq_sysfs_init_done = false;
272 }
273
274 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
275 {
276         kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
277 }
278
279 void blk_mq_sysfs_deinit(struct request_queue *q)
280 {
281         struct blk_mq_ctx *ctx;
282         int cpu;
283
284         for_each_possible_cpu(cpu) {
285                 ctx = per_cpu_ptr(q->queue_ctx, cpu);
286                 kobject_put(&ctx->kobj);
287         }
288         kobject_put(&q->mq_kobj);
289 }
290
291 void blk_mq_sysfs_init(struct request_queue *q)
292 {
293         struct blk_mq_ctx *ctx;
294         int cpu;
295
296         kobject_init(&q->mq_kobj, &blk_mq_ktype);
297
298         for_each_possible_cpu(cpu) {
299                 ctx = per_cpu_ptr(q->queue_ctx, cpu);
300                 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
301         }
302 }
303
304 int __blk_mq_register_dev(struct device *dev, struct request_queue *q)
305 {
306         struct blk_mq_hw_ctx *hctx;
307         int ret, i;
308
309         WARN_ON_ONCE(!q->kobj.parent);
310         lockdep_assert_held(&q->sysfs_lock);
311
312         ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
313         if (ret < 0)
314                 goto out;
315
316         kobject_uevent(&q->mq_kobj, KOBJ_ADD);
317
318         queue_for_each_hw_ctx(q, hctx, i) {
319                 ret = blk_mq_register_hctx(hctx);
320                 if (ret)
321                         goto unreg;
322         }
323
324         q->mq_sysfs_init_done = true;
325
326 out:
327         return ret;
328
329 unreg:
330         while (--i >= 0)
331                 blk_mq_unregister_hctx(q->queue_hw_ctx[i]);
332
333         kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
334         kobject_del(&q->mq_kobj);
335         kobject_put(&dev->kobj);
336         return ret;
337 }
338
339 int blk_mq_register_dev(struct device *dev, struct request_queue *q)
340 {
341         int ret;
342
343         mutex_lock(&q->sysfs_lock);
344         ret = __blk_mq_register_dev(dev, q);
345         mutex_unlock(&q->sysfs_lock);
346
347         return ret;
348 }
349 EXPORT_SYMBOL_GPL(blk_mq_register_dev);
350
351 void blk_mq_sysfs_unregister(struct request_queue *q)
352 {
353         struct blk_mq_hw_ctx *hctx;
354         int i;
355
356         mutex_lock(&q->sysfs_lock);
357         if (!q->mq_sysfs_init_done)
358                 goto unlock;
359
360         queue_for_each_hw_ctx(q, hctx, i)
361                 blk_mq_unregister_hctx(hctx);
362
363 unlock:
364         mutex_unlock(&q->sysfs_lock);
365 }
366
367 int blk_mq_sysfs_register(struct request_queue *q)
368 {
369         struct blk_mq_hw_ctx *hctx;
370         int i, ret = 0;
371
372         mutex_lock(&q->sysfs_lock);
373         if (!q->mq_sysfs_init_done)
374                 goto unlock;
375
376         queue_for_each_hw_ctx(q, hctx, i) {
377                 ret = blk_mq_register_hctx(hctx);
378                 if (ret)
379                         break;
380         }
381
382 unlock:
383         mutex_unlock(&q->sysfs_lock);
384
385         return ret;
386 }