dm-ps-service-time.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2009 NEC Corporation. All Rights Reserved.
  4. *
  5. * Module Author: Kiyoshi Ueda
  6. *
  7. * This file is released under the GPL.
  8. *
  9. * Throughput oriented path selector.
  10. */
  11. #include "dm.h"
  12. #include "dm-path-selector.h"
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #define DM_MSG_PREFIX "multipath service-time"
  16. #define ST_MIN_IO 1
  17. #define ST_MAX_RELATIVE_THROUGHPUT 100
  18. #define ST_MAX_RELATIVE_THROUGHPUT_SHIFT 7
  19. #define ST_MAX_INFLIGHT_SIZE ((size_t)-1 >> ST_MAX_RELATIVE_THROUGHPUT_SHIFT)
  20. #define ST_VERSION "0.3.0"
  21. struct selector {
  22. struct list_head valid_paths;
  23. struct list_head failed_paths;
  24. spinlock_t lock;
  25. };
  26. struct path_info {
  27. struct list_head list;
  28. struct dm_path *path;
  29. unsigned int repeat_count;
  30. unsigned int relative_throughput;
  31. atomic_t in_flight_size; /* Total size of in-flight I/Os */
  32. };
  33. static struct selector *alloc_selector(void)
  34. {
  35. struct selector *s = kmalloc_obj(*s);
  36. if (s) {
  37. INIT_LIST_HEAD(&s->valid_paths);
  38. INIT_LIST_HEAD(&s->failed_paths);
  39. spin_lock_init(&s->lock);
  40. }
  41. return s;
  42. }
  43. static int st_create(struct path_selector *ps, unsigned int argc, char **argv)
  44. {
  45. struct selector *s = alloc_selector();
  46. if (!s)
  47. return -ENOMEM;
  48. ps->context = s;
  49. return 0;
  50. }
  51. static void free_paths(struct list_head *paths)
  52. {
  53. struct path_info *pi, *next;
  54. list_for_each_entry_safe(pi, next, paths, list) {
  55. list_del(&pi->list);
  56. kfree(pi);
  57. }
  58. }
  59. static void st_destroy(struct path_selector *ps)
  60. {
  61. struct selector *s = ps->context;
  62. free_paths(&s->valid_paths);
  63. free_paths(&s->failed_paths);
  64. kfree(s);
  65. ps->context = NULL;
  66. }
  67. static int st_status(struct path_selector *ps, struct dm_path *path,
  68. status_type_t type, char *result, unsigned int maxlen)
  69. {
  70. unsigned int sz = 0;
  71. struct path_info *pi;
  72. if (!path)
  73. DMEMIT("0 ");
  74. else {
  75. pi = path->pscontext;
  76. switch (type) {
  77. case STATUSTYPE_INFO:
  78. DMEMIT("%d %u ", atomic_read(&pi->in_flight_size),
  79. pi->relative_throughput);
  80. break;
  81. case STATUSTYPE_TABLE:
  82. DMEMIT("%u %u ", pi->repeat_count,
  83. pi->relative_throughput);
  84. break;
  85. case STATUSTYPE_IMA:
  86. result[0] = '\0';
  87. break;
  88. }
  89. }
  90. return sz;
  91. }
  92. static int st_add_path(struct path_selector *ps, struct dm_path *path,
  93. int argc, char **argv, char **error)
  94. {
  95. struct selector *s = ps->context;
  96. struct path_info *pi;
  97. unsigned int repeat_count = ST_MIN_IO;
  98. unsigned int relative_throughput = 1;
  99. char dummy;
  100. unsigned long flags;
  101. /*
  102. * Arguments: [<repeat_count> [<relative_throughput>]]
  103. * <repeat_count>: The number of I/Os before switching path.
  104. * If not given, default (ST_MIN_IO) is used.
  105. * <relative_throughput>: The relative throughput value of
  106. * the path among all paths in the path-group.
  107. * The valid range: 0-<ST_MAX_RELATIVE_THROUGHPUT>
  108. * If not given, minimum value '1' is used.
  109. * If '0' is given, the path isn't selected while
  110. * other paths having a positive value are available.
  111. */
  112. if (argc > 2) {
  113. *error = "service-time ps: incorrect number of arguments";
  114. return -EINVAL;
  115. }
  116. if (argc && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
  117. *error = "service-time ps: invalid repeat count";
  118. return -EINVAL;
  119. }
  120. if (repeat_count > 1) {
  121. DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
  122. repeat_count = 1;
  123. }
  124. if ((argc == 2) &&
  125. (sscanf(argv[1], "%u%c", &relative_throughput, &dummy) != 1 ||
  126. relative_throughput > ST_MAX_RELATIVE_THROUGHPUT)) {
  127. *error = "service-time ps: invalid relative_throughput value";
  128. return -EINVAL;
  129. }
  130. /* allocate the path */
  131. pi = kmalloc_obj(*pi);
  132. if (!pi) {
  133. *error = "service-time ps: Error allocating path context";
  134. return -ENOMEM;
  135. }
  136. pi->path = path;
  137. pi->repeat_count = repeat_count;
  138. pi->relative_throughput = relative_throughput;
  139. atomic_set(&pi->in_flight_size, 0);
  140. path->pscontext = pi;
  141. spin_lock_irqsave(&s->lock, flags);
  142. list_add_tail(&pi->list, &s->valid_paths);
  143. spin_unlock_irqrestore(&s->lock, flags);
  144. return 0;
  145. }
  146. static void st_fail_path(struct path_selector *ps, struct dm_path *path)
  147. {
  148. struct selector *s = ps->context;
  149. struct path_info *pi = path->pscontext;
  150. unsigned long flags;
  151. spin_lock_irqsave(&s->lock, flags);
  152. list_move(&pi->list, &s->failed_paths);
  153. spin_unlock_irqrestore(&s->lock, flags);
  154. }
  155. static int st_reinstate_path(struct path_selector *ps, struct dm_path *path)
  156. {
  157. struct selector *s = ps->context;
  158. struct path_info *pi = path->pscontext;
  159. unsigned long flags;
  160. spin_lock_irqsave(&s->lock, flags);
  161. list_move_tail(&pi->list, &s->valid_paths);
  162. spin_unlock_irqrestore(&s->lock, flags);
  163. return 0;
  164. }
  165. /*
  166. * Compare the estimated service time of 2 paths, pi1 and pi2,
  167. * for the incoming I/O.
  168. *
  169. * Returns:
  170. * < 0 : pi1 is better
  171. * 0 : no difference between pi1 and pi2
  172. * > 0 : pi2 is better
  173. *
  174. * Description:
  175. * Basically, the service time is estimated by:
  176. * ('pi->in-flight-size' + 'incoming') / 'pi->relative_throughput'
  177. * To reduce the calculation, some optimizations are made.
  178. * (See comments inline)
  179. */
  180. static int st_compare_load(struct path_info *pi1, struct path_info *pi2,
  181. size_t incoming)
  182. {
  183. size_t sz1, sz2, st1, st2;
  184. sz1 = atomic_read(&pi1->in_flight_size);
  185. sz2 = atomic_read(&pi2->in_flight_size);
  186. /*
  187. * Case 1: Both have same throughput value. Choose less loaded path.
  188. */
  189. if (pi1->relative_throughput == pi2->relative_throughput)
  190. return sz1 - sz2;
  191. /*
  192. * Case 2a: Both have same load. Choose higher throughput path.
  193. * Case 2b: One path has no throughput value. Choose the other one.
  194. */
  195. if (sz1 == sz2 ||
  196. !pi1->relative_throughput || !pi2->relative_throughput)
  197. return pi2->relative_throughput - pi1->relative_throughput;
  198. /*
  199. * Case 3: Calculate service time. Choose faster path.
  200. * Service time using pi1:
  201. * st1 = (sz1 + incoming) / pi1->relative_throughput
  202. * Service time using pi2:
  203. * st2 = (sz2 + incoming) / pi2->relative_throughput
  204. *
  205. * To avoid the division, transform the expression to use
  206. * multiplication.
  207. * Because ->relative_throughput > 0 here, if st1 < st2,
  208. * the expressions below are the same meaning:
  209. * (sz1 + incoming) / pi1->relative_throughput <
  210. * (sz2 + incoming) / pi2->relative_throughput
  211. * (sz1 + incoming) * pi2->relative_throughput <
  212. * (sz2 + incoming) * pi1->relative_throughput
  213. * So use the later one.
  214. */
  215. sz1 += incoming;
  216. sz2 += incoming;
  217. if (unlikely(sz1 >= ST_MAX_INFLIGHT_SIZE ||
  218. sz2 >= ST_MAX_INFLIGHT_SIZE)) {
  219. /*
  220. * Size may be too big for multiplying pi->relative_throughput
  221. * and overflow.
  222. * To avoid the overflow and mis-selection, shift down both.
  223. */
  224. sz1 >>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT;
  225. sz2 >>= ST_MAX_RELATIVE_THROUGHPUT_SHIFT;
  226. }
  227. st1 = sz1 * pi2->relative_throughput;
  228. st2 = sz2 * pi1->relative_throughput;
  229. if (st1 != st2)
  230. return st1 - st2;
  231. /*
  232. * Case 4: Service time is equal. Choose higher throughput path.
  233. */
  234. return pi2->relative_throughput - pi1->relative_throughput;
  235. }
  236. static struct dm_path *st_select_path(struct path_selector *ps, size_t nr_bytes)
  237. {
  238. struct selector *s = ps->context;
  239. struct path_info *pi = NULL, *best = NULL;
  240. struct dm_path *ret = NULL;
  241. unsigned long flags;
  242. spin_lock_irqsave(&s->lock, flags);
  243. if (list_empty(&s->valid_paths))
  244. goto out;
  245. list_for_each_entry(pi, &s->valid_paths, list)
  246. if (!best || (st_compare_load(pi, best, nr_bytes) < 0))
  247. best = pi;
  248. if (!best)
  249. goto out;
  250. /* Move most recently used to least preferred to evenly balance. */
  251. list_move_tail(&best->list, &s->valid_paths);
  252. ret = best->path;
  253. out:
  254. spin_unlock_irqrestore(&s->lock, flags);
  255. return ret;
  256. }
  257. static int st_start_io(struct path_selector *ps, struct dm_path *path,
  258. size_t nr_bytes)
  259. {
  260. struct path_info *pi = path->pscontext;
  261. atomic_add(nr_bytes, &pi->in_flight_size);
  262. return 0;
  263. }
  264. static int st_end_io(struct path_selector *ps, struct dm_path *path,
  265. size_t nr_bytes, u64 start_time)
  266. {
  267. struct path_info *pi = path->pscontext;
  268. atomic_sub(nr_bytes, &pi->in_flight_size);
  269. return 0;
  270. }
  271. static struct path_selector_type st_ps = {
  272. .name = "service-time",
  273. .module = THIS_MODULE,
  274. .table_args = 2,
  275. .info_args = 2,
  276. .create = st_create,
  277. .destroy = st_destroy,
  278. .status = st_status,
  279. .add_path = st_add_path,
  280. .fail_path = st_fail_path,
  281. .reinstate_path = st_reinstate_path,
  282. .select_path = st_select_path,
  283. .start_io = st_start_io,
  284. .end_io = st_end_io,
  285. };
  286. static int __init dm_st_init(void)
  287. {
  288. int r = dm_register_path_selector(&st_ps);
  289. if (r < 0) {
  290. DMERR("register failed %d", r);
  291. return r;
  292. }
  293. DMINFO("version " ST_VERSION " loaded");
  294. return r;
  295. }
  296. static void __exit dm_st_exit(void)
  297. {
  298. dm_unregister_path_selector(&st_ps);
  299. }
  300. module_init(dm_st_init);
  301. module_exit(dm_st_exit);
  302. MODULE_DESCRIPTION(DM_NAME " throughput oriented path selector");
  303. MODULE_AUTHOR("Kiyoshi Ueda <k-ueda@ct.jp.nec.com>");
  304. MODULE_LICENSE("GPL");