blk-mq-sysfs.c 8.1 KB

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