dm-ps-io-affinity.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Oracle Corporation
  4. *
  5. * Module Author: Mike Christie
  6. */
  7. #include "dm-path-selector.h"
  8. #include <linux/device-mapper.h>
  9. #include <linux/module.h>
  10. #define DM_MSG_PREFIX "multipath io-affinity"
  11. struct path_info {
  12. struct dm_path *path;
  13. cpumask_var_t cpumask;
  14. refcount_t refcount;
  15. bool failed;
  16. };
  17. struct selector {
  18. struct path_info **path_map;
  19. cpumask_var_t path_mask;
  20. atomic_t map_misses;
  21. };
  22. static void ioa_free_path(struct selector *s, unsigned int cpu)
  23. {
  24. struct path_info *pi = s->path_map[cpu];
  25. if (!pi)
  26. return;
  27. if (refcount_dec_and_test(&pi->refcount)) {
  28. cpumask_clear_cpu(cpu, s->path_mask);
  29. free_cpumask_var(pi->cpumask);
  30. kfree(pi);
  31. s->path_map[cpu] = NULL;
  32. }
  33. }
  34. static int ioa_add_path(struct path_selector *ps, struct dm_path *path,
  35. int argc, char **argv, char **error)
  36. {
  37. struct selector *s = ps->context;
  38. struct path_info *pi = NULL;
  39. unsigned int cpu;
  40. int ret;
  41. if (argc != 1) {
  42. *error = "io-affinity ps: invalid number of arguments";
  43. return -EINVAL;
  44. }
  45. pi = kzalloc(sizeof(*pi), GFP_KERNEL);
  46. if (!pi) {
  47. *error = "io-affinity ps: Error allocating path context";
  48. return -ENOMEM;
  49. }
  50. pi->path = path;
  51. path->pscontext = pi;
  52. refcount_set(&pi->refcount, 1);
  53. if (!zalloc_cpumask_var(&pi->cpumask, GFP_KERNEL)) {
  54. *error = "io-affinity ps: Error allocating cpumask context";
  55. ret = -ENOMEM;
  56. goto free_pi;
  57. }
  58. ret = cpumask_parse(argv[0], pi->cpumask);
  59. if (ret) {
  60. *error = "io-affinity ps: invalid cpumask";
  61. ret = -EINVAL;
  62. goto free_mask;
  63. }
  64. for_each_cpu(cpu, pi->cpumask) {
  65. if (cpu >= nr_cpu_ids) {
  66. DMWARN_LIMIT("Ignoring mapping for CPU %u. Max CPU is %u",
  67. cpu, nr_cpu_ids);
  68. break;
  69. }
  70. if (s->path_map[cpu]) {
  71. DMWARN("CPU mapping for %u exists. Ignoring.", cpu);
  72. continue;
  73. }
  74. cpumask_set_cpu(cpu, s->path_mask);
  75. s->path_map[cpu] = pi;
  76. refcount_inc(&pi->refcount);
  77. }
  78. if (refcount_dec_and_test(&pi->refcount)) {
  79. *error = "io-affinity ps: No new/valid CPU mapping found";
  80. ret = -EINVAL;
  81. goto free_mask;
  82. }
  83. return 0;
  84. free_mask:
  85. free_cpumask_var(pi->cpumask);
  86. free_pi:
  87. kfree(pi);
  88. return ret;
  89. }
  90. static int ioa_create(struct path_selector *ps, unsigned int argc, char **argv)
  91. {
  92. struct selector *s;
  93. s = kmalloc(sizeof(*s), GFP_KERNEL);
  94. if (!s)
  95. return -ENOMEM;
  96. s->path_map = kzalloc(nr_cpu_ids * sizeof(struct path_info *),
  97. GFP_KERNEL);
  98. if (!s->path_map)
  99. goto free_selector;
  100. if (!zalloc_cpumask_var(&s->path_mask, GFP_KERNEL))
  101. goto free_map;
  102. atomic_set(&s->map_misses, 0);
  103. ps->context = s;
  104. return 0;
  105. free_map:
  106. kfree(s->path_map);
  107. free_selector:
  108. kfree(s);
  109. return -ENOMEM;
  110. }
  111. static void ioa_destroy(struct path_selector *ps)
  112. {
  113. struct selector *s = ps->context;
  114. unsigned int cpu;
  115. for_each_cpu(cpu, s->path_mask)
  116. ioa_free_path(s, cpu);
  117. free_cpumask_var(s->path_mask);
  118. kfree(s->path_map);
  119. kfree(s);
  120. ps->context = NULL;
  121. }
  122. static int ioa_status(struct path_selector *ps, struct dm_path *path,
  123. status_type_t type, char *result, unsigned int maxlen)
  124. {
  125. struct selector *s = ps->context;
  126. struct path_info *pi;
  127. int sz = 0;
  128. if (!path) {
  129. DMEMIT("0 ");
  130. return sz;
  131. }
  132. switch(type) {
  133. case STATUSTYPE_INFO:
  134. DMEMIT("%d ", atomic_read(&s->map_misses));
  135. break;
  136. case STATUSTYPE_TABLE:
  137. pi = path->pscontext;
  138. DMEMIT("%*pb ", cpumask_pr_args(pi->cpumask));
  139. break;
  140. case STATUSTYPE_IMA:
  141. *result = '\0';
  142. break;
  143. }
  144. return sz;
  145. }
  146. static void ioa_fail_path(struct path_selector *ps, struct dm_path *p)
  147. {
  148. struct path_info *pi = p->pscontext;
  149. pi->failed = true;
  150. }
  151. static int ioa_reinstate_path(struct path_selector *ps, struct dm_path *p)
  152. {
  153. struct path_info *pi = p->pscontext;
  154. pi->failed = false;
  155. return 0;
  156. }
  157. static struct dm_path *ioa_select_path(struct path_selector *ps,
  158. size_t nr_bytes)
  159. {
  160. unsigned int cpu, node;
  161. struct selector *s = ps->context;
  162. const struct cpumask *cpumask;
  163. struct path_info *pi;
  164. int i;
  165. cpu = get_cpu();
  166. pi = s->path_map[cpu];
  167. if (pi && !pi->failed)
  168. goto done;
  169. /*
  170. * Perf is not optimal, but we at least try the local node then just
  171. * try not to fail.
  172. */
  173. if (!pi)
  174. atomic_inc(&s->map_misses);
  175. node = cpu_to_node(cpu);
  176. cpumask = cpumask_of_node(node);
  177. for_each_cpu(i, cpumask) {
  178. pi = s->path_map[i];
  179. if (pi && !pi->failed)
  180. goto done;
  181. }
  182. for_each_cpu(i, s->path_mask) {
  183. pi = s->path_map[i];
  184. if (pi && !pi->failed)
  185. goto done;
  186. }
  187. pi = NULL;
  188. done:
  189. put_cpu();
  190. return pi ? pi->path : NULL;
  191. }
  192. static struct path_selector_type ioa_ps = {
  193. .name = "io-affinity",
  194. .module = THIS_MODULE,
  195. .table_args = 1,
  196. .info_args = 1,
  197. .create = ioa_create,
  198. .destroy = ioa_destroy,
  199. .status = ioa_status,
  200. .add_path = ioa_add_path,
  201. .fail_path = ioa_fail_path,
  202. .reinstate_path = ioa_reinstate_path,
  203. .select_path = ioa_select_path,
  204. };
  205. static int __init dm_ioa_init(void)
  206. {
  207. int ret = dm_register_path_selector(&ioa_ps);
  208. if (ret < 0)
  209. DMERR("register failed %d", ret);
  210. return ret;
  211. }
  212. static void __exit dm_ioa_exit(void)
  213. {
  214. int ret = dm_unregister_path_selector(&ioa_ps);
  215. if (ret < 0)
  216. DMERR("unregister failed %d", ret);
  217. }
  218. module_init(dm_ioa_init);
  219. module_exit(dm_ioa_exit);
  220. MODULE_DESCRIPTION(DM_NAME " multipath path selector that selects paths based on the CPU IO is being executed on");
  221. MODULE_AUTHOR("Mike Christie <[email protected]>");
  222. MODULE_LICENSE("GPL");