1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/backing-dev.h>
5 #include <linux/bio.h>
6 #include <linux/blkdev.h>
7 #include <linux/mm.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/workqueue.h>
11 #include <linux/smp.h>
12
13 #include <linux/blk-mq.h>
14 #include "blk.h"
15 #include "blk-mq.h"
16 #include "blk-mq-tag.h"
17
blk_mq_sysfs_release(struct kobject * kobj)18 static void blk_mq_sysfs_release(struct kobject *kobj)
19 {
20 struct blk_mq_ctxs *ctxs = container_of(kobj, struct blk_mq_ctxs, kobj);
21
22 free_percpu(ctxs->queue_ctx);
23 kfree(ctxs);
24 }
25
blk_mq_ctx_sysfs_release(struct kobject * kobj)26 static void blk_mq_ctx_sysfs_release(struct kobject *kobj)
27 {
28 struct blk_mq_ctx *ctx = container_of(kobj, struct blk_mq_ctx, kobj);
29
30 /* ctx->ctxs won't be released until all ctx are freed */
31 kobject_put(&ctx->ctxs->kobj);
32 }
33
blk_mq_hw_sysfs_release(struct kobject * kobj)34 static void blk_mq_hw_sysfs_release(struct kobject *kobj)
35 {
36 struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
37 kobj);
38
39 if (hctx->flags & BLK_MQ_F_BLOCKING)
40 cleanup_srcu_struct(hctx->srcu);
41 blk_free_flush_queue(hctx->fq);
42 sbitmap_free(&hctx->ctx_map);
43 free_cpumask_var(hctx->cpumask);
44 kfree(hctx->ctxs);
45 kfree(hctx);
46 }
47
48 struct blk_mq_hw_ctx_sysfs_entry {
49 struct attribute attr;
50 ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
51 ssize_t (*store)(struct blk_mq_hw_ctx *, const char *, size_t);
52 };
53
blk_mq_hw_sysfs_show(struct kobject * kobj,struct attribute * attr,char * page)54 static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
55 struct attribute *attr, char *page)
56 {
57 struct blk_mq_hw_ctx_sysfs_entry *entry;
58 struct blk_mq_hw_ctx *hctx;
59 struct request_queue *q;
60 ssize_t res;
61
62 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
63 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
64 q = hctx->queue;
65
66 if (!entry->show)
67 return -EIO;
68
69 mutex_lock(&q->sysfs_lock);
70 res = entry->show(hctx, page);
71 mutex_unlock(&q->sysfs_lock);
72 return res;
73 }
74
blk_mq_hw_sysfs_store(struct kobject * kobj,struct attribute * attr,const char * page,size_t length)75 static ssize_t blk_mq_hw_sysfs_store(struct kobject *kobj,
76 struct attribute *attr, const char *page,
77 size_t length)
78 {
79 struct blk_mq_hw_ctx_sysfs_entry *entry;
80 struct blk_mq_hw_ctx *hctx;
81 struct request_queue *q;
82 ssize_t res;
83
84 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
85 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
86 q = hctx->queue;
87
88 if (!entry->store)
89 return -EIO;
90
91 mutex_lock(&q->sysfs_lock);
92 res = entry->store(hctx, page, length);
93 mutex_unlock(&q->sysfs_lock);
94 return res;
95 }
96
blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx * hctx,char * page)97 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
98 char *page)
99 {
100 return sprintf(page, "%u\n", hctx->tags->nr_tags);
101 }
102
blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx * hctx,char * page)103 static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
104 char *page)
105 {
106 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
107 }
108
blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx * hctx,char * page)109 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
110 {
111 const size_t size = PAGE_SIZE - 1;
112 unsigned int i, first = 1;
113 int ret = 0, pos = 0;
114
115 for_each_cpu(i, hctx->cpumask) {
116 if (first)
117 ret = snprintf(pos + page, size - pos, "%u", i);
118 else
119 ret = snprintf(pos + page, size - pos, ", %u", i);
120
121 if (ret >= size - pos)
122 break;
123
124 first = 0;
125 pos += ret;
126 }
127
128 ret = snprintf(pos + page, size + 1 - pos, "\n");
129 return pos + ret;
130 }
131
132 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
133 .attr = {.name = "nr_tags", .mode = 0444 },
134 .show = blk_mq_hw_sysfs_nr_tags_show,
135 };
136 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
137 .attr = {.name = "nr_reserved_tags", .mode = 0444 },
138 .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
139 };
140 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
141 .attr = {.name = "cpu_list", .mode = 0444 },
142 .show = blk_mq_hw_sysfs_cpus_show,
143 };
144
145 static struct attribute *default_hw_ctx_attrs[] = {
146 &blk_mq_hw_sysfs_nr_tags.attr,
147 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
148 &blk_mq_hw_sysfs_cpus.attr,
149 NULL,
150 };
151 ATTRIBUTE_GROUPS(default_hw_ctx);
152
153 static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
154 .show = blk_mq_hw_sysfs_show,
155 .store = blk_mq_hw_sysfs_store,
156 };
157
158 static struct kobj_type blk_mq_ktype = {
159 .release = blk_mq_sysfs_release,
160 };
161
162 static struct kobj_type blk_mq_ctx_ktype = {
163 .release = blk_mq_ctx_sysfs_release,
164 };
165
166 static struct kobj_type blk_mq_hw_ktype = {
167 .sysfs_ops = &blk_mq_hw_sysfs_ops,
168 .default_groups = default_hw_ctx_groups,
169 .release = blk_mq_hw_sysfs_release,
170 };
171
blk_mq_unregister_hctx(struct blk_mq_hw_ctx * hctx)172 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
173 {
174 struct blk_mq_ctx *ctx;
175 int i;
176
177 if (!hctx->nr_ctx)
178 return;
179
180 hctx_for_each_ctx(hctx, ctx, i)
181 kobject_del(&ctx->kobj);
182
183 kobject_del(&hctx->kobj);
184 }
185
blk_mq_register_hctx(struct blk_mq_hw_ctx * hctx)186 static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
187 {
188 struct request_queue *q = hctx->queue;
189 struct blk_mq_ctx *ctx;
190 int i, j, ret;
191
192 if (!hctx->nr_ctx)
193 return 0;
194
195 ret = kobject_add(&hctx->kobj, q->mq_kobj, "%u", hctx->queue_num);
196 if (ret)
197 return ret;
198
199 hctx_for_each_ctx(hctx, ctx, i) {
200 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
201 if (ret)
202 goto out;
203 }
204
205 return 0;
206 out:
207 hctx_for_each_ctx(hctx, ctx, j) {
208 if (j < i)
209 kobject_del(&ctx->kobj);
210 }
211 kobject_del(&hctx->kobj);
212 return ret;
213 }
214
blk_mq_unregister_dev(struct device * dev,struct request_queue * q)215 void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
216 {
217 struct blk_mq_hw_ctx *hctx;
218 int i;
219
220 lockdep_assert_held(&q->sysfs_dir_lock);
221
222 queue_for_each_hw_ctx(q, hctx, i)
223 blk_mq_unregister_hctx(hctx);
224
225 kobject_uevent(q->mq_kobj, KOBJ_REMOVE);
226 kobject_del(q->mq_kobj);
227 kobject_put(&dev->kobj);
228
229 q->mq_sysfs_init_done = false;
230 }
231
blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx * hctx)232 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
233 {
234 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
235 }
236
blk_mq_sysfs_deinit(struct request_queue * q)237 void blk_mq_sysfs_deinit(struct request_queue *q)
238 {
239 struct blk_mq_ctx *ctx;
240 int cpu;
241
242 for_each_possible_cpu(cpu) {
243 ctx = per_cpu_ptr(q->queue_ctx, cpu);
244 kobject_put(&ctx->kobj);
245 }
246 kobject_put(q->mq_kobj);
247 }
248
blk_mq_sysfs_init(struct request_queue * q)249 void blk_mq_sysfs_init(struct request_queue *q)
250 {
251 struct blk_mq_ctx *ctx;
252 int cpu;
253
254 kobject_init(q->mq_kobj, &blk_mq_ktype);
255
256 for_each_possible_cpu(cpu) {
257 ctx = per_cpu_ptr(q->queue_ctx, cpu);
258
259 kobject_get(q->mq_kobj);
260 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
261 }
262 }
263
__blk_mq_register_dev(struct device * dev,struct request_queue * q)264 int __blk_mq_register_dev(struct device *dev, struct request_queue *q)
265 {
266 struct blk_mq_hw_ctx *hctx;
267 int ret, i;
268
269 WARN_ON_ONCE(!q->kobj.parent);
270 lockdep_assert_held(&q->sysfs_dir_lock);
271
272 ret = kobject_add(q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
273 if (ret < 0)
274 goto out;
275
276 kobject_uevent(q->mq_kobj, KOBJ_ADD);
277
278 queue_for_each_hw_ctx(q, hctx, i) {
279 ret = blk_mq_register_hctx(hctx);
280 if (ret)
281 goto unreg;
282 }
283
284 q->mq_sysfs_init_done = true;
285
286 out:
287 return ret;
288
289 unreg:
290 while (--i >= 0)
291 blk_mq_unregister_hctx(q->queue_hw_ctx[i]);
292
293 kobject_uevent(q->mq_kobj, KOBJ_REMOVE);
294 kobject_del(q->mq_kobj);
295 kobject_put(&dev->kobj);
296 return ret;
297 }
298
blk_mq_sysfs_unregister(struct request_queue * q)299 void blk_mq_sysfs_unregister(struct request_queue *q)
300 {
301 struct blk_mq_hw_ctx *hctx;
302 int i;
303
304 mutex_lock(&q->sysfs_dir_lock);
305 if (!q->mq_sysfs_init_done)
306 goto unlock;
307
308 queue_for_each_hw_ctx(q, hctx, i)
309 blk_mq_unregister_hctx(hctx);
310
311 unlock:
312 mutex_unlock(&q->sysfs_dir_lock);
313 }
314
blk_mq_sysfs_register(struct request_queue * q)315 int blk_mq_sysfs_register(struct request_queue *q)
316 {
317 struct blk_mq_hw_ctx *hctx;
318 int i, ret = 0;
319
320 mutex_lock(&q->sysfs_dir_lock);
321 if (!q->mq_sysfs_init_done)
322 goto unlock;
323
324 queue_for_each_hw_ctx(q, hctx, i) {
325 ret = blk_mq_register_hctx(hctx);
326 if (ret)
327 break;
328 }
329
330 unlock:
331 mutex_unlock(&q->sysfs_dir_lock);
332
333 return ret;
334 }
335