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