blk-mq-sysfs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #include "blk.h"
  13. #include "blk-mq.h"
  14. static void blk_mq_sysfs_release(struct kobject *kobj)
  15. {
  16. struct blk_mq_ctxs *ctxs = container_of(kobj, struct blk_mq_ctxs, kobj);
  17. free_percpu(ctxs->queue_ctx);
  18. kfree(ctxs);
  19. }
  20. static void blk_mq_ctx_sysfs_release(struct kobject *kobj)
  21. {
  22. struct blk_mq_ctx *ctx = container_of(kobj, struct blk_mq_ctx, kobj);
  23. /* ctx->ctxs won't be released until all ctx are freed */
  24. kobject_put(&ctx->ctxs->kobj);
  25. }
  26. static void blk_mq_hw_sysfs_release(struct kobject *kobj)
  27. {
  28. struct blk_mq_hw_ctx *hctx = container_of(kobj, struct blk_mq_hw_ctx,
  29. kobj);
  30. sbitmap_free(&hctx->ctx_map);
  31. free_cpumask_var(hctx->cpumask);
  32. kfree(hctx->ctxs);
  33. kfree(hctx);
  34. }
  35. struct blk_mq_hw_ctx_sysfs_entry {
  36. struct attribute attr;
  37. ssize_t (*show)(struct blk_mq_hw_ctx *, char *);
  38. };
  39. static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
  40. struct attribute *attr, char *page)
  41. {
  42. struct blk_mq_hw_ctx_sysfs_entry *entry;
  43. struct blk_mq_hw_ctx *hctx;
  44. struct request_queue *q;
  45. ssize_t res;
  46. entry = container_of(attr, struct blk_mq_hw_ctx_sysfs_entry, attr);
  47. hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
  48. q = hctx->queue;
  49. if (!entry->show)
  50. return -EIO;
  51. mutex_lock(&q->elevator_lock);
  52. res = entry->show(hctx, page);
  53. mutex_unlock(&q->elevator_lock);
  54. return res;
  55. }
  56. static ssize_t blk_mq_hw_sysfs_nr_tags_show(struct blk_mq_hw_ctx *hctx,
  57. char *page)
  58. {
  59. return sprintf(page, "%u\n", hctx->tags->nr_tags);
  60. }
  61. static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
  62. char *page)
  63. {
  64. return sprintf(page, "%u\n", hctx->tags->nr_reserved_tags);
  65. }
  66. static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
  67. {
  68. const size_t size = PAGE_SIZE - 1;
  69. unsigned int i, first = 1;
  70. int ret = 0, pos = 0;
  71. for_each_cpu(i, hctx->cpumask) {
  72. if (first)
  73. ret = snprintf(pos + page, size - pos, "%u", i);
  74. else
  75. ret = snprintf(pos + page, size - pos, ", %u", i);
  76. if (ret >= size - pos)
  77. break;
  78. first = 0;
  79. pos += ret;
  80. }
  81. ret = snprintf(pos + page, size + 1 - pos, "\n");
  82. return pos + ret;
  83. }
  84. static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
  85. .attr = {.name = "nr_tags", .mode = 0444 },
  86. .show = blk_mq_hw_sysfs_nr_tags_show,
  87. };
  88. static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
  89. .attr = {.name = "nr_reserved_tags", .mode = 0444 },
  90. .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
  91. };
  92. static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
  93. .attr = {.name = "cpu_list", .mode = 0444 },
  94. .show = blk_mq_hw_sysfs_cpus_show,
  95. };
  96. static struct attribute *default_hw_ctx_attrs[] = {
  97. &blk_mq_hw_sysfs_nr_tags.attr,
  98. &blk_mq_hw_sysfs_nr_reserved_tags.attr,
  99. &blk_mq_hw_sysfs_cpus.attr,
  100. NULL,
  101. };
  102. ATTRIBUTE_GROUPS(default_hw_ctx);
  103. static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
  104. .show = blk_mq_hw_sysfs_show,
  105. };
  106. static const struct kobj_type blk_mq_ktype = {
  107. .release = blk_mq_sysfs_release,
  108. };
  109. static const struct kobj_type blk_mq_ctx_ktype = {
  110. .release = blk_mq_ctx_sysfs_release,
  111. };
  112. static const struct kobj_type blk_mq_hw_ktype = {
  113. .sysfs_ops = &blk_mq_hw_sysfs_ops,
  114. .default_groups = default_hw_ctx_groups,
  115. .release = blk_mq_hw_sysfs_release,
  116. };
  117. static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
  118. {
  119. struct blk_mq_ctx *ctx;
  120. int i;
  121. if (!hctx->nr_ctx)
  122. return;
  123. hctx_for_each_ctx(hctx, ctx, i)
  124. if (ctx->kobj.state_in_sysfs)
  125. kobject_del(&ctx->kobj);
  126. if (hctx->kobj.state_in_sysfs)
  127. kobject_del(&hctx->kobj);
  128. }
  129. static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
  130. {
  131. struct request_queue *q = hctx->queue;
  132. struct blk_mq_ctx *ctx;
  133. int i, j, ret;
  134. if (!hctx->nr_ctx)
  135. return 0;
  136. ret = kobject_add(&hctx->kobj, q->mq_kobj, "%u", hctx->queue_num);
  137. if (ret)
  138. return ret;
  139. hctx_for_each_ctx(hctx, ctx, i) {
  140. ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
  141. if (ret)
  142. goto out;
  143. }
  144. return 0;
  145. out:
  146. hctx_for_each_ctx(hctx, ctx, j) {
  147. if (j < i)
  148. kobject_del(&ctx->kobj);
  149. }
  150. kobject_del(&hctx->kobj);
  151. return ret;
  152. }
  153. void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
  154. {
  155. kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
  156. }
  157. void blk_mq_sysfs_deinit(struct request_queue *q)
  158. {
  159. struct blk_mq_ctx *ctx;
  160. int cpu;
  161. for_each_possible_cpu(cpu) {
  162. ctx = per_cpu_ptr(q->queue_ctx, cpu);
  163. kobject_put(&ctx->kobj);
  164. }
  165. kobject_put(q->mq_kobj);
  166. }
  167. void blk_mq_sysfs_init(struct request_queue *q)
  168. {
  169. struct blk_mq_ctx *ctx;
  170. int cpu;
  171. kobject_init(q->mq_kobj, &blk_mq_ktype);
  172. for_each_possible_cpu(cpu) {
  173. ctx = per_cpu_ptr(q->queue_ctx, cpu);
  174. kobject_get(q->mq_kobj);
  175. kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
  176. }
  177. }
  178. int blk_mq_sysfs_register(struct gendisk *disk)
  179. {
  180. struct request_queue *q = disk->queue;
  181. struct blk_mq_hw_ctx *hctx;
  182. unsigned long i, j;
  183. int ret;
  184. ret = kobject_add(q->mq_kobj, &disk_to_dev(disk)->kobj, "mq");
  185. if (ret < 0)
  186. return ret;
  187. kobject_uevent(q->mq_kobj, KOBJ_ADD);
  188. mutex_lock(&q->tag_set->tag_list_lock);
  189. queue_for_each_hw_ctx(q, hctx, i) {
  190. ret = blk_mq_register_hctx(hctx);
  191. if (ret)
  192. goto out_unreg;
  193. }
  194. mutex_unlock(&q->tag_set->tag_list_lock);
  195. return 0;
  196. out_unreg:
  197. queue_for_each_hw_ctx(q, hctx, j) {
  198. if (j < i)
  199. blk_mq_unregister_hctx(hctx);
  200. }
  201. mutex_unlock(&q->tag_set->tag_list_lock);
  202. kobject_uevent(q->mq_kobj, KOBJ_REMOVE);
  203. kobject_del(q->mq_kobj);
  204. return ret;
  205. }
  206. void blk_mq_sysfs_unregister(struct gendisk *disk)
  207. {
  208. struct request_queue *q = disk->queue;
  209. struct blk_mq_hw_ctx *hctx;
  210. unsigned long i;
  211. mutex_lock(&q->tag_set->tag_list_lock);
  212. queue_for_each_hw_ctx(q, hctx, i)
  213. blk_mq_unregister_hctx(hctx);
  214. mutex_unlock(&q->tag_set->tag_list_lock);
  215. kobject_uevent(q->mq_kobj, KOBJ_REMOVE);
  216. kobject_del(q->mq_kobj);
  217. }
  218. void blk_mq_sysfs_unregister_hctxs(struct request_queue *q)
  219. {
  220. struct blk_mq_hw_ctx *hctx;
  221. unsigned long i;
  222. if (!blk_queue_registered(q))
  223. return;
  224. queue_for_each_hw_ctx(q, hctx, i)
  225. blk_mq_unregister_hctx(hctx);
  226. }
  227. int blk_mq_sysfs_register_hctxs(struct request_queue *q)
  228. {
  229. struct blk_mq_hw_ctx *hctx;
  230. unsigned long i;
  231. int ret = 0;
  232. if (!blk_queue_registered(q))
  233. goto out;
  234. queue_for_each_hw_ctx(q, hctx, i) {
  235. ret = blk_mq_register_hctx(hctx);
  236. if (ret)
  237. break;
  238. }
  239. out:
  240. return ret;
  241. }