1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/backing-dev.h>
6 #include <linux/blkdev.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/workqueue.h>
11 #include <linux/smp.h>
13 #include <linux/blk-mq.h>
16 #include "blk-mq-tag.h"
18 static void blk_mq_sysfs_release(struct kobject *kobj)
20 struct blk_mq_ctxs *ctxs = container_of(kobj, struct blk_mq_ctxs, kobj);
22 free_percpu(ctxs->queue_ctx);
26 static void blk_mq_ctx_sysfs_release(struct kobject *kobj)
28 struct blk_mq_ctx *ctx = container_of(kobj, struct blk_mq_ctx, kobj);
30 /* ctx->ctxs won't be released until all ctx are freed */
31 kobject_put(&ctx->ctxs->kobj);
34 static void blk_mq_hw_sysfs_release(struct kobject *kobj)
36 struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
39 blk_free_flush_queue(hctx->fq);
40 sbitmap_free(&hctx->ctx_map);
41 free_cpumask_var(hctx->cpumask);
46 struct blk_mq_hw_ctx_sysfs_entry {
47 struct attribute attr;
48 ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
51 static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
52 struct attribute *attr, char *page)
54 struct blk_mq_hw_ctx_sysfs_entry *entry;
55 struct blk_mq_hw_ctx *hctx;
56 struct request_queue *q;
59 entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
60 hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
66 mutex_lock(&q->sysfs_lock);
67 res = entry->show(hctx, page);
68 mutex_unlock(&q->sysfs_lock);
72 static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
75 return sprintf(page, "%u\n", hctx->tags->nr_tags);
78 static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
81 return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
84 static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
86 const size_t size = PAGE_SIZE - 1;
87 unsigned int i, first = 1;
90 for_each_cpu(i, hctx->cpumask) {
92 ret = snprintf(pos + page, size - pos, "%u", i);
94 ret = snprintf(pos + page, size - pos, ", %u", i);
96 if (ret >= size - pos)
103 ret = snprintf(pos + page, size + 1 - pos, "\n");
107 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
108 .attr = {.name = "nr_tags", .mode = 0444 },
109 .show = blk_mq_hw_sysfs_nr_tags_show,
111 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
112 .attr = {.name = "nr_reserved_tags", .mode = 0444 },
113 .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
115 static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
116 .attr = {.name = "cpu_list", .mode = 0444 },
117 .show = blk_mq_hw_sysfs_cpus_show,
120 static struct attribute *default_hw_ctx_attrs[] = {
121 &blk_mq_hw_sysfs_nr_tags.attr,
122 &blk_mq_hw_sysfs_nr_reserved_tags.attr,
123 &blk_mq_hw_sysfs_cpus.attr,
126 ATTRIBUTE_GROUPS(default_hw_ctx);
128 static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
129 .show = blk_mq_hw_sysfs_show,
132 static const struct kobj_type blk_mq_ktype = {
133 .release = blk_mq_sysfs_release,
136 static const struct kobj_type blk_mq_ctx_ktype = {
137 .release = blk_mq_ctx_sysfs_release,
140 static const struct kobj_type blk_mq_hw_ktype = {
141 .sysfs_ops = &blk_mq_hw_sysfs_ops,
142 .default_groups = default_hw_ctx_groups,
143 .release = blk_mq_hw_sysfs_release,
146 static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
148 struct blk_mq_ctx *ctx;
154 hctx_for_each_ctx(hctx, ctx, i)
155 kobject_del(&ctx->kobj);
157 kobject_del(&hctx->kobj);
160 static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
162 struct request_queue *q = hctx->queue;
163 struct blk_mq_ctx *ctx;
169 ret = kobject_add(&hctx->kobj, q->mq_kobj, "%u", hctx->queue_num);
173 hctx_for_each_ctx(hctx, ctx, i) {
174 ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
181 hctx_for_each_ctx(hctx, ctx, j) {
183 kobject_del(&ctx->kobj);
185 kobject_del(&hctx->kobj);
189 void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
191 kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
194 void blk_mq_sysfs_deinit(struct request_queue *q)
196 struct blk_mq_ctx *ctx;
199 for_each_possible_cpu(cpu) {
200 ctx = per_cpu_ptr(q->queue_ctx, cpu);
201 kobject_put(&ctx->kobj);
203 kobject_put(q->mq_kobj);
206 void blk_mq_sysfs_init(struct request_queue *q)
208 struct blk_mq_ctx *ctx;
211 kobject_init(q->mq_kobj, &blk_mq_ktype);
213 for_each_possible_cpu(cpu) {
214 ctx = per_cpu_ptr(q->queue_ctx, cpu);
216 kobject_get(q->mq_kobj);
217 kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
221 int blk_mq_sysfs_register(struct gendisk *disk)
223 struct request_queue *q = disk->queue;
224 struct blk_mq_hw_ctx *hctx;
228 lockdep_assert_held(&q->sysfs_dir_lock);
230 ret = kobject_add(q->mq_kobj, &disk_to_dev(disk)->kobj, "mq");
234 kobject_uevent(q->mq_kobj, KOBJ_ADD);
236 queue_for_each_hw_ctx(q, hctx, i) {
237 ret = blk_mq_register_hctx(hctx);
242 q->mq_sysfs_init_done = true;
248 queue_for_each_hw_ctx(q, hctx, j) {
250 blk_mq_unregister_hctx(hctx);
253 kobject_uevent(q->mq_kobj, KOBJ_REMOVE);
254 kobject_del(q->mq_kobj);
258 void blk_mq_sysfs_unregister(struct gendisk *disk)
260 struct request_queue *q = disk->queue;
261 struct blk_mq_hw_ctx *hctx;
264 lockdep_assert_held(&q->sysfs_dir_lock);
266 queue_for_each_hw_ctx(q, hctx, i)
267 blk_mq_unregister_hctx(hctx);
269 kobject_uevent(q->mq_kobj, KOBJ_REMOVE);
270 kobject_del(q->mq_kobj);
272 q->mq_sysfs_init_done = false;
275 void blk_mq_sysfs_unregister_hctxs(struct request_queue *q)
277 struct blk_mq_hw_ctx *hctx;
280 mutex_lock(&q->sysfs_dir_lock);
281 if (!q->mq_sysfs_init_done)
284 queue_for_each_hw_ctx(q, hctx, i)
285 blk_mq_unregister_hctx(hctx);
288 mutex_unlock(&q->sysfs_dir_lock);
291 int blk_mq_sysfs_register_hctxs(struct request_queue *q)
293 struct blk_mq_hw_ctx *hctx;
297 mutex_lock(&q->sysfs_dir_lock);
298 if (!q->mq_sysfs_init_done)
301 queue_for_each_hw_ctx(q, hctx, i) {
302 ret = blk_mq_register_hctx(hctx);
308 mutex_unlock(&q->sysfs_dir_lock);