dm-ps-queue-length.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
  4. * Copyright (C) 2006-2009 NEC Corporation.
  5. *
  6. * dm-queue-length.c
  7. *
  8. * Module Author: Stefan Bader, IBM
  9. * Modified by: Kiyoshi Ueda, NEC
  10. *
  11. * This file is released under the GPL.
  12. *
  13. * queue-length path selector - choose a path with the least number of
  14. * in-flight I/Os.
  15. */
  16. #include "dm.h"
  17. #include "dm-path-selector.h"
  18. #include <linux/slab.h>
  19. #include <linux/ctype.h>
  20. #include <linux/errno.h>
  21. #include <linux/module.h>
  22. #include <linux/atomic.h>
  23. #define DM_MSG_PREFIX "multipath queue-length"
  24. #define QL_MIN_IO 1
  25. #define QL_VERSION "0.2.0"
  26. struct selector {
  27. struct list_head valid_paths;
  28. struct list_head failed_paths;
  29. spinlock_t lock;
  30. };
  31. struct path_info {
  32. struct list_head list;
  33. struct dm_path *path;
  34. unsigned int repeat_count;
  35. atomic_t qlen; /* the number of in-flight I/Os */
  36. };
  37. static struct selector *alloc_selector(void)
  38. {
  39. struct selector *s = kmalloc_obj(*s);
  40. if (s) {
  41. INIT_LIST_HEAD(&s->valid_paths);
  42. INIT_LIST_HEAD(&s->failed_paths);
  43. spin_lock_init(&s->lock);
  44. }
  45. return s;
  46. }
  47. static int ql_create(struct path_selector *ps, unsigned int argc, char **argv)
  48. {
  49. struct selector *s = alloc_selector();
  50. if (!s)
  51. return -ENOMEM;
  52. ps->context = s;
  53. return 0;
  54. }
  55. static void ql_free_paths(struct list_head *paths)
  56. {
  57. struct path_info *pi, *next;
  58. list_for_each_entry_safe(pi, next, paths, list) {
  59. list_del(&pi->list);
  60. kfree(pi);
  61. }
  62. }
  63. static void ql_destroy(struct path_selector *ps)
  64. {
  65. struct selector *s = ps->context;
  66. ql_free_paths(&s->valid_paths);
  67. ql_free_paths(&s->failed_paths);
  68. kfree(s);
  69. ps->context = NULL;
  70. }
  71. static int ql_status(struct path_selector *ps, struct dm_path *path,
  72. status_type_t type, char *result, unsigned int maxlen)
  73. {
  74. unsigned int sz = 0;
  75. struct path_info *pi;
  76. /* When called with NULL path, return selector status/args. */
  77. if (!path)
  78. DMEMIT("0 ");
  79. else {
  80. pi = path->pscontext;
  81. switch (type) {
  82. case STATUSTYPE_INFO:
  83. DMEMIT("%d ", atomic_read(&pi->qlen));
  84. break;
  85. case STATUSTYPE_TABLE:
  86. DMEMIT("%u ", pi->repeat_count);
  87. break;
  88. case STATUSTYPE_IMA:
  89. *result = '\0';
  90. break;
  91. }
  92. }
  93. return sz;
  94. }
  95. static int ql_add_path(struct path_selector *ps, struct dm_path *path,
  96. int argc, char **argv, char **error)
  97. {
  98. struct selector *s = ps->context;
  99. struct path_info *pi;
  100. unsigned int repeat_count = QL_MIN_IO;
  101. char dummy;
  102. unsigned long flags;
  103. /*
  104. * Arguments: [<repeat_count>]
  105. * <repeat_count>: The number of I/Os before switching path.
  106. * If not given, default (QL_MIN_IO) is used.
  107. */
  108. if (argc > 1) {
  109. *error = "queue-length ps: incorrect number of arguments";
  110. return -EINVAL;
  111. }
  112. if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
  113. *error = "queue-length ps: invalid repeat count";
  114. return -EINVAL;
  115. }
  116. if (repeat_count > 1) {
  117. DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
  118. repeat_count = 1;
  119. }
  120. /* Allocate the path information structure */
  121. pi = kmalloc_obj(*pi);
  122. if (!pi) {
  123. *error = "queue-length ps: Error allocating path information";
  124. return -ENOMEM;
  125. }
  126. pi->path = path;
  127. pi->repeat_count = repeat_count;
  128. atomic_set(&pi->qlen, 0);
  129. path->pscontext = pi;
  130. spin_lock_irqsave(&s->lock, flags);
  131. list_add_tail(&pi->list, &s->valid_paths);
  132. spin_unlock_irqrestore(&s->lock, flags);
  133. return 0;
  134. }
  135. static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
  136. {
  137. struct selector *s = ps->context;
  138. struct path_info *pi = path->pscontext;
  139. unsigned long flags;
  140. spin_lock_irqsave(&s->lock, flags);
  141. list_move(&pi->list, &s->failed_paths);
  142. spin_unlock_irqrestore(&s->lock, flags);
  143. }
  144. static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
  145. {
  146. struct selector *s = ps->context;
  147. struct path_info *pi = path->pscontext;
  148. unsigned long flags;
  149. spin_lock_irqsave(&s->lock, flags);
  150. list_move_tail(&pi->list, &s->valid_paths);
  151. spin_unlock_irqrestore(&s->lock, flags);
  152. return 0;
  153. }
  154. /*
  155. * Select a path having the minimum number of in-flight I/Os
  156. */
  157. static struct dm_path *ql_select_path(struct path_selector *ps, size_t nr_bytes)
  158. {
  159. struct selector *s = ps->context;
  160. struct path_info *pi = NULL, *best = NULL;
  161. struct dm_path *ret = NULL;
  162. unsigned long flags;
  163. spin_lock_irqsave(&s->lock, flags);
  164. if (list_empty(&s->valid_paths))
  165. goto out;
  166. list_for_each_entry(pi, &s->valid_paths, list) {
  167. if (!best ||
  168. (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
  169. best = pi;
  170. if (!atomic_read(&best->qlen))
  171. break;
  172. }
  173. if (!best)
  174. goto out;
  175. /* Move most recently used to least preferred to evenly balance. */
  176. list_move_tail(&best->list, &s->valid_paths);
  177. ret = best->path;
  178. out:
  179. spin_unlock_irqrestore(&s->lock, flags);
  180. return ret;
  181. }
  182. static int ql_start_io(struct path_selector *ps, struct dm_path *path,
  183. size_t nr_bytes)
  184. {
  185. struct path_info *pi = path->pscontext;
  186. atomic_inc(&pi->qlen);
  187. return 0;
  188. }
  189. static int ql_end_io(struct path_selector *ps, struct dm_path *path,
  190. size_t nr_bytes, u64 start_time)
  191. {
  192. struct path_info *pi = path->pscontext;
  193. atomic_dec(&pi->qlen);
  194. return 0;
  195. }
  196. static struct path_selector_type ql_ps = {
  197. .name = "queue-length",
  198. .module = THIS_MODULE,
  199. .table_args = 1,
  200. .info_args = 1,
  201. .create = ql_create,
  202. .destroy = ql_destroy,
  203. .status = ql_status,
  204. .add_path = ql_add_path,
  205. .fail_path = ql_fail_path,
  206. .reinstate_path = ql_reinstate_path,
  207. .select_path = ql_select_path,
  208. .start_io = ql_start_io,
  209. .end_io = ql_end_io,
  210. };
  211. static int __init dm_ql_init(void)
  212. {
  213. int r = dm_register_path_selector(&ql_ps);
  214. if (r < 0) {
  215. DMERR("register failed %d", r);
  216. return r;
  217. }
  218. DMINFO("version " QL_VERSION " loaded");
  219. return r;
  220. }
  221. static void __exit dm_ql_exit(void)
  222. {
  223. dm_unregister_path_selector(&ql_ps);
  224. }
  225. module_init(dm_ql_init);
  226. module_exit(dm_ql_exit);
  227. MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
  228. MODULE_DESCRIPTION(
  229. "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
  230. DM_NAME " path selector to balance the number of in-flight I/Os"
  231. );
  232. MODULE_LICENSE("GPL");