blk-mq-sysfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. const size_t size = PAGE_SIZE - 1;
  125. unsigned int i, first = 1;
  126. int ret = 0, pos = 0;
  127. for_each_cpu(i, hctx->cpumask) {
  128. if (first)
  129. ret = snprintf(pos + page, size - pos, "%u", i);
  130. else
  131. ret = snprintf(pos + page, size - pos, ", %u", i);
  132. if (ret >= size - pos)
  133. break;
  134. first = 0;
  135. pos += ret;
  136. }
  137. ret = snprintf(pos + page, size + 1 - pos, "\n");
  138. return pos + ret;
  139. }
  140. static struct attribute *default_ctx_attrs[] = {
  141. NULL,
  142. };
  143. static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {
  144. .attr = {.name = "nr_tags", .mode = S_IRUGO },
  145. .show = blk_mq_hw_sysfs_nr_tags_show,
  146. };
  147. static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_reserved_tags = {
  148. .attr = {.name = "nr_reserved_tags", .mode = S_IRUGO },
  149. .show = blk_mq_hw_sysfs_nr_reserved_tags_show,
  150. };
  151. static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_cpus = {
  152. .attr = {.name = "cpu_list", .mode = S_IRUGO },
  153. .show = blk_mq_hw_sysfs_cpus_show,
  154. };
  155. static struct attribute *default_hw_ctx_attrs[] = {
  156. &blk_mq_hw_sysfs_nr_tags.attr,
  157. &blk_mq_hw_sysfs_nr_reserved_tags.attr,
  158. &blk_mq_hw_sysfs_cpus.attr,
  159. NULL,
  160. };
  161. static const struct sysfs_ops blk_mq_sysfs_ops = {
  162. .show = blk_mq_sysfs_show,
  163. .store = blk_mq_sysfs_store,
  164. };
  165. static const struct sysfs_ops blk_mq_hw_sysfs_ops = {
  166. .show = blk_mq_hw_sysfs_show,
  167. .store = blk_mq_hw_sysfs_store,
  168. };
  169. static struct kobj_type blk_mq_ktype = {
  170. .sysfs_ops = &blk_mq_sysfs_ops,
  171. .release = blk_mq_sysfs_release,
  172. };
  173. static struct kobj_type blk_mq_ctx_ktype = {
  174. .sysfs_ops = &blk_mq_sysfs_ops,
  175. .default_attrs = default_ctx_attrs,
  176. .release = blk_mq_sysfs_release,
  177. };
  178. static struct kobj_type blk_mq_hw_ktype = {
  179. .sysfs_ops = &blk_mq_hw_sysfs_ops,
  180. .default_attrs = default_hw_ctx_attrs,
  181. .release = blk_mq_hw_sysfs_release,
  182. };
  183. static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
  184. {
  185. struct blk_mq_ctx *ctx;
  186. int i;
  187. if (!hctx->nr_ctx)
  188. return;
  189. hctx_for_each_ctx(hctx, ctx, i)
  190. kobject_del(&ctx->kobj);
  191. kobject_del(&hctx->kobj);
  192. }
  193. static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
  194. {
  195. struct request_queue *q = hctx->queue;
  196. struct blk_mq_ctx *ctx;
  197. int i, ret;
  198. if (!hctx->nr_ctx)
  199. return 0;
  200. ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
  201. if (ret)
  202. return ret;
  203. hctx_for_each_ctx(hctx, ctx, i) {
  204. ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
  205. if (ret)
  206. break;
  207. }
  208. return ret;
  209. }
  210. static void __blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
  211. {
  212. struct blk_mq_hw_ctx *hctx;
  213. int i;
  214. lockdep_assert_held(&q->sysfs_lock);
  215. queue_for_each_hw_ctx(q, hctx, i)
  216. blk_mq_unregister_hctx(hctx);
  217. kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
  218. kobject_del(&q->mq_kobj);
  219. kobject_put(&dev->kobj);
  220. q->mq_sysfs_init_done = false;
  221. }
  222. void blk_mq_unregister_dev(struct device *dev, struct request_queue *q)
  223. {
  224. mutex_lock(&q->sysfs_lock);
  225. __blk_mq_unregister_dev(dev, q);
  226. mutex_unlock(&q->sysfs_lock);
  227. }
  228. void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx)
  229. {
  230. kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
  231. }
  232. void blk_mq_sysfs_deinit(struct request_queue *q)
  233. {
  234. struct blk_mq_ctx *ctx;
  235. int cpu;
  236. for_each_possible_cpu(cpu) {
  237. ctx = per_cpu_ptr(q->queue_ctx, cpu);
  238. kobject_put(&ctx->kobj);
  239. }
  240. kobject_put(&q->mq_kobj);
  241. }
  242. void blk_mq_sysfs_init(struct request_queue *q)
  243. {
  244. struct blk_mq_ctx *ctx;
  245. int cpu;
  246. kobject_init(&q->mq_kobj, &blk_mq_ktype);
  247. for_each_possible_cpu(cpu) {
  248. ctx = per_cpu_ptr(q->queue_ctx, cpu);
  249. kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
  250. }
  251. }
  252. int __blk_mq_register_dev(struct device *dev, struct request_queue *q)
  253. {
  254. struct blk_mq_hw_ctx *hctx;
  255. int ret, i;
  256. WARN_ON_ONCE(!q->kobj.parent);
  257. lockdep_assert_held(&q->sysfs_lock);
  258. ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
  259. if (ret < 0)
  260. goto out;
  261. kobject_uevent(&q->mq_kobj, KOBJ_ADD);
  262. queue_for_each_hw_ctx(q, hctx, i) {
  263. ret = blk_mq_register_hctx(hctx);
  264. if (ret)
  265. goto unreg;
  266. }
  267. q->mq_sysfs_init_done = true;
  268. out:
  269. return ret;
  270. unreg:
  271. while (--i >= 0)
  272. blk_mq_unregister_hctx(q->queue_hw_ctx[i]);
  273. kobject_uevent(&q->mq_kobj, KOBJ_REMOVE);
  274. kobject_del(&q->mq_kobj);
  275. kobject_put(&dev->kobj);
  276. return ret;
  277. }
  278. int blk_mq_register_dev(struct device *dev, struct request_queue *q)
  279. {
  280. int ret;
  281. mutex_lock(&q->sysfs_lock);
  282. ret = __blk_mq_register_dev(dev, q);
  283. mutex_unlock(&q->sysfs_lock);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL_GPL(blk_mq_register_dev);
  287. void blk_mq_sysfs_unregister(struct request_queue *q)
  288. {
  289. struct blk_mq_hw_ctx *hctx;
  290. int i;
  291. mutex_lock(&q->sysfs_lock);
  292. if (!q->mq_sysfs_init_done)
  293. goto unlock;
  294. queue_for_each_hw_ctx(q, hctx, i)
  295. blk_mq_unregister_hctx(hctx);
  296. unlock:
  297. mutex_unlock(&q->sysfs_lock);
  298. }
  299. int blk_mq_sysfs_register(struct request_queue *q)
  300. {
  301. struct blk_mq_hw_ctx *hctx;
  302. int i, ret = 0;
  303. mutex_lock(&q->sysfs_lock);
  304. if (!q->mq_sysfs_init_done)
  305. goto unlock;
  306. queue_for_each_hw_ctx(q, hctx, i) {
  307. ret = blk_mq_register_hctx(hctx);
  308. if (ret)
  309. break;
  310. }
  311. unlock:
  312. mutex_unlock(&q->sysfs_lock);
  313. return ret;
  314. }