]>
Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | // SPDX-License-Identifier: GPL-2.0 |
320ae51f JA |
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> | |
c7e2d94b | 14 | #include "blk.h" |
320ae51f JA |
15 | #include "blk-mq.h" |
16 | #include "blk-mq-tag.h" | |
17 | ||
18 | static void blk_mq_sysfs_release(struct kobject *kobj) | |
19 | { | |
1db4909e ML |
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 | ||
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); | |
320ae51f JA |
32 | } |
33 | ||
6c8b232e ML |
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); | |
c7e2d94b ML |
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); | |
01388df3 | 43 | free_cpumask_var(hctx->cpumask); |
6c8b232e ML |
44 | kfree(hctx->ctxs); |
45 | kfree(hctx); | |
46 | } | |
47 | ||
320ae51f JA |
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 | ||
320ae51f JA |
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 | ||
320ae51f | 69 | mutex_lock(&q->sysfs_lock); |
bae85c15 | 70 | res = entry->show(hctx, page); |
320ae51f JA |
71 | mutex_unlock(&q->sysfs_lock); |
72 | return res; | |
73 | } | |
74 | ||
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 | ||
320ae51f | 91 | mutex_lock(&q->sysfs_lock); |
bae85c15 | 92 | res = entry->store(hctx, page, length); |
320ae51f JA |
93 | mutex_unlock(&q->sysfs_lock); |
94 | return res; | |
95 | } | |
96 | ||
d96b37c0 OS |
97 | static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx, |
98 | char *page) | |
bd166ef1 | 99 | { |
d96b37c0 | 100 | return sprintf(page, "%u\n", hctx->tags->nr_tags); |
bd166ef1 JA |
101 | } |
102 | ||
d96b37c0 OS |
103 | static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx, |
104 | char *page) | |
320ae51f | 105 | { |
d96b37c0 | 106 | return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags); |
320ae51f JA |
107 | } |
108 | ||
676141e4 JA |
109 | static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page) |
110 | { | |
8962842c | 111 | const size_t size = PAGE_SIZE - 1; |
cb2da43e | 112 | unsigned int i, first = 1; |
8962842c | 113 | int ret = 0, pos = 0; |
676141e4 | 114 | |
cb2da43e | 115 | for_each_cpu(i, hctx->cpumask) { |
676141e4 | 116 | if (first) |
8962842c | 117 | ret = snprintf(pos + page, size - pos, "%u", i); |
676141e4 | 118 | else |
8962842c ML |
119 | ret = snprintf(pos + page, size - pos, ", %u", i); |
120 | ||
121 | if (ret >= size - pos) | |
122 | break; | |
676141e4 JA |
123 | |
124 | first = 0; | |
8962842c | 125 | pos += ret; |
676141e4 JA |
126 | } |
127 | ||
d2c9be89 | 128 | ret = snprintf(pos + page, size + 1 - pos, "\n"); |
8962842c | 129 | return pos + ret; |
676141e4 JA |
130 | } |
131 | ||
d96b37c0 | 132 | static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = { |
5657a819 | 133 | .attr = {.name = "nr_tags", .mode = 0444 }, |
d96b37c0 OS |
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 = { | |
5657a819 | 137 | .attr = {.name = "nr_reserved_tags", .mode = 0444 }, |
d96b37c0 OS |
138 | .show = blk_mq_hw_sysfs_nr_reserved_tags_show, |
139 | }; | |
676141e4 | 140 | static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = { |
5657a819 | 141 | .attr = {.name = "cpu_list", .mode = 0444 }, |
676141e4 JA |
142 | .show = blk_mq_hw_sysfs_cpus_show, |
143 | }; | |
320ae51f JA |
144 | |
145 | static struct attribute *default_hw_ctx_attrs[] = { | |
d96b37c0 OS |
146 | &blk_mq_hw_sysfs_nr_tags.attr, |
147 | &blk_mq_hw_sysfs_nr_reserved_tags.attr, | |
676141e4 | 148 | &blk_mq_hw_sysfs_cpus.attr, |
320ae51f JA |
149 | NULL, |
150 | }; | |
800f5aa1 | 151 | ATTRIBUTE_GROUPS(default_hw_ctx); |
320ae51f | 152 | |
320ae51f JA |
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 = { | |
320ae51f JA |
159 | .release = blk_mq_sysfs_release, |
160 | }; | |
161 | ||
162 | static struct kobj_type blk_mq_ctx_ktype = { | |
1db4909e | 163 | .release = blk_mq_ctx_sysfs_release, |
320ae51f JA |
164 | }; |
165 | ||
166 | static struct kobj_type blk_mq_hw_ktype = { | |
167 | .sysfs_ops = &blk_mq_hw_sysfs_ops, | |
800f5aa1 | 168 | .default_groups = default_hw_ctx_groups, |
6c8b232e | 169 | .release = blk_mq_hw_sysfs_release, |
320ae51f JA |
170 | }; |
171 | ||
ee3c5db0 | 172 | static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx) |
67aec14c JA |
173 | { |
174 | struct blk_mq_ctx *ctx; | |
175 | int i; | |
176 | ||
4593fdbe | 177 | if (!hctx->nr_ctx) |
67aec14c JA |
178 | return; |
179 | ||
180 | hctx_for_each_ctx(hctx, ctx, i) | |
181 | kobject_del(&ctx->kobj); | |
182 | ||
183 | kobject_del(&hctx->kobj); | |
184 | } | |
185 | ||
ee3c5db0 | 186 | static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx) |
67aec14c JA |
187 | { |
188 | struct request_queue *q = hctx->queue; | |
189 | struct blk_mq_ctx *ctx; | |
190 | int i, ret; | |
191 | ||
4593fdbe | 192 | if (!hctx->nr_ctx) |
67aec14c JA |
193 | return 0; |
194 | ||
1db4909e | 195 | ret = kobject_add(&hctx->kobj, q->mq_kobj, "%u", hctx->queue_num); |
67aec14c JA |
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 | break; | |
203 | } | |
204 | ||
205 | return ret; | |
206 | } | |
207 | ||
667257e8 | 208 | void blk_mq_unregister_dev(struct device *dev, struct request_queue *q) |
320ae51f | 209 | { |
85157366 | 210 | struct blk_mq_hw_ctx *hctx; |
7ea5fe31 | 211 | int i; |
85157366 | 212 | |
cecf5d87 | 213 | lockdep_assert_held(&q->sysfs_dir_lock); |
2d0364c8 | 214 | |
6c8b232e | 215 | queue_for_each_hw_ctx(q, hctx, i) |
67aec14c JA |
216 | blk_mq_unregister_hctx(hctx); |
217 | ||
1db4909e ML |
218 | kobject_uevent(q->mq_kobj, KOBJ_REMOVE); |
219 | kobject_del(q->mq_kobj); | |
b21d5b30 | 220 | kobject_put(&dev->kobj); |
4593fdbe AM |
221 | |
222 | q->mq_sysfs_init_done = false; | |
c0f3fd2b JA |
223 | } |
224 | ||
868f2f0b KB |
225 | void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx) |
226 | { | |
227 | kobject_init(&hctx->kobj, &blk_mq_hw_ktype); | |
228 | } | |
229 | ||
7ea5fe31 ML |
230 | void blk_mq_sysfs_deinit(struct request_queue *q) |
231 | { | |
232 | struct blk_mq_ctx *ctx; | |
233 | int cpu; | |
234 | ||
235 | for_each_possible_cpu(cpu) { | |
236 | ctx = per_cpu_ptr(q->queue_ctx, cpu); | |
237 | kobject_put(&ctx->kobj); | |
238 | } | |
1db4909e | 239 | kobject_put(q->mq_kobj); |
7ea5fe31 ML |
240 | } |
241 | ||
737f98cf | 242 | void blk_mq_sysfs_init(struct request_queue *q) |
67aec14c | 243 | { |
67aec14c | 244 | struct blk_mq_ctx *ctx; |
897bb0c7 | 245 | int cpu; |
67aec14c | 246 | |
1db4909e | 247 | kobject_init(q->mq_kobj, &blk_mq_ktype); |
67aec14c | 248 | |
897bb0c7 TG |
249 | for_each_possible_cpu(cpu) { |
250 | ctx = per_cpu_ptr(q->queue_ctx, cpu); | |
1db4909e ML |
251 | |
252 | kobject_get(q->mq_kobj); | |
06a41a99 | 253 | kobject_init(&ctx->kobj, &blk_mq_ctx_ktype); |
897bb0c7 | 254 | } |
67aec14c JA |
255 | } |
256 | ||
2d0364c8 | 257 | int __blk_mq_register_dev(struct device *dev, struct request_queue *q) |
320ae51f | 258 | { |
320ae51f | 259 | struct blk_mq_hw_ctx *hctx; |
67aec14c | 260 | int ret, i; |
320ae51f | 261 | |
2d0364c8 | 262 | WARN_ON_ONCE(!q->kobj.parent); |
cecf5d87 | 263 | lockdep_assert_held(&q->sysfs_dir_lock); |
4593fdbe | 264 | |
1db4909e | 265 | ret = kobject_add(q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq"); |
320ae51f | 266 | if (ret < 0) |
4593fdbe | 267 | goto out; |
320ae51f | 268 | |
1db4909e | 269 | kobject_uevent(q->mq_kobj, KOBJ_ADD); |
320ae51f JA |
270 | |
271 | queue_for_each_hw_ctx(q, hctx, i) { | |
67aec14c | 272 | ret = blk_mq_register_hctx(hctx); |
320ae51f | 273 | if (ret) |
f05d1ba7 | 274 | goto unreg; |
320ae51f JA |
275 | } |
276 | ||
f05d1ba7 | 277 | q->mq_sysfs_init_done = true; |
2d0364c8 | 278 | |
4593fdbe | 279 | out: |
2d0364c8 | 280 | return ret; |
f05d1ba7 BVA |
281 | |
282 | unreg: | |
283 | while (--i >= 0) | |
284 | blk_mq_unregister_hctx(q->queue_hw_ctx[i]); | |
285 | ||
1db4909e ML |
286 | kobject_uevent(q->mq_kobj, KOBJ_REMOVE); |
287 | kobject_del(q->mq_kobj); | |
f05d1ba7 BVA |
288 | kobject_put(&dev->kobj); |
289 | return ret; | |
2d0364c8 BVA |
290 | } |
291 | ||
67aec14c JA |
292 | void blk_mq_sysfs_unregister(struct request_queue *q) |
293 | { | |
294 | struct blk_mq_hw_ctx *hctx; | |
295 | int i; | |
296 | ||
cecf5d87 | 297 | mutex_lock(&q->sysfs_dir_lock); |
4593fdbe | 298 | if (!q->mq_sysfs_init_done) |
2d0364c8 | 299 | goto unlock; |
4593fdbe | 300 | |
67aec14c JA |
301 | queue_for_each_hw_ctx(q, hctx, i) |
302 | blk_mq_unregister_hctx(hctx); | |
2d0364c8 BVA |
303 | |
304 | unlock: | |
cecf5d87 | 305 | mutex_unlock(&q->sysfs_dir_lock); |
67aec14c JA |
306 | } |
307 | ||
308 | int blk_mq_sysfs_register(struct request_queue *q) | |
309 | { | |
310 | struct blk_mq_hw_ctx *hctx; | |
311 | int i, ret = 0; | |
312 | ||
cecf5d87 | 313 | mutex_lock(&q->sysfs_dir_lock); |
4593fdbe | 314 | if (!q->mq_sysfs_init_done) |
2d0364c8 | 315 | goto unlock; |
4593fdbe | 316 | |
67aec14c JA |
317 | queue_for_each_hw_ctx(q, hctx, i) { |
318 | ret = blk_mq_register_hctx(hctx); | |
319 | if (ret) | |
320 | break; | |
321 | } | |
322 | ||
2d0364c8 | 323 | unlock: |
cecf5d87 | 324 | mutex_unlock(&q->sysfs_dir_lock); |
2d0364c8 | 325 | |
67aec14c JA |
326 | return ret; |
327 | } |