dm-ps-io-affinity.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_obj(*pi);
  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_obj(*s);
  94. if (!s)
  95. return -ENOMEM;
  96. s->path_map = kzalloc_objs(struct path_info *, nr_cpu_ids);
  97. if (!s->path_map)
  98. goto free_selector;
  99. if (!zalloc_cpumask_var(&s->path_mask, GFP_KERNEL))
  100. goto free_map;
  101. atomic_set(&s->map_misses, 0);
  102. ps->context = s;
  103. return 0;
  104. free_map:
  105. kfree(s->path_map);
  106. free_selector:
  107. kfree(s);
  108. return -ENOMEM;
  109. }
  110. static void ioa_destroy(struct path_selector *ps)
  111. {
  112. struct selector *s = ps->context;
  113. unsigned int cpu;
  114. for_each_cpu(cpu, s->path_mask)
  115. ioa_free_path(s, cpu);
  116. free_cpumask_var(s->path_mask);
  117. kfree(s->path_map);
  118. kfree(s);
  119. ps->context = NULL;
  120. }
  121. static int ioa_status(struct path_selector *ps, struct dm_path *path,
  122. status_type_t type, char *result, unsigned int maxlen)
  123. {
  124. struct selector *s = ps->context;
  125. struct path_info *pi;
  126. int sz = 0;
  127. if (!path) {
  128. DMEMIT("0 ");
  129. return sz;
  130. }
  131. switch (type) {
  132. case STATUSTYPE_INFO:
  133. DMEMIT("%d ", atomic_read(&s->map_misses));
  134. break;
  135. case STATUSTYPE_TABLE:
  136. pi = path->pscontext;
  137. DMEMIT("%*pb ", cpumask_pr_args(pi->cpumask));
  138. break;
  139. case STATUSTYPE_IMA:
  140. *result = '\0';
  141. break;
  142. }
  143. return sz;
  144. }
  145. static void ioa_fail_path(struct path_selector *ps, struct dm_path *p)
  146. {
  147. struct path_info *pi = p->pscontext;
  148. pi->failed = true;
  149. }
  150. static int ioa_reinstate_path(struct path_selector *ps, struct dm_path *p)
  151. {
  152. struct path_info *pi = p->pscontext;
  153. pi->failed = false;
  154. return 0;
  155. }
  156. static struct dm_path *ioa_select_path(struct path_selector *ps,
  157. size_t nr_bytes)
  158. {
  159. unsigned int cpu, node;
  160. struct selector *s = ps->context;
  161. const struct cpumask *cpumask;
  162. struct path_info *pi;
  163. int i;
  164. cpu = get_cpu();
  165. pi = s->path_map[cpu];
  166. if (pi && !pi->failed)
  167. goto done;
  168. /*
  169. * Perf is not optimal, but we at least try the local node then just
  170. * try not to fail.
  171. */
  172. if (!pi)
  173. atomic_inc(&s->map_misses);
  174. node = cpu_to_node(cpu);
  175. cpumask = cpumask_of_node(node);
  176. for_each_cpu(i, cpumask) {
  177. pi = s->path_map[i];
  178. if (pi && !pi->failed)
  179. goto done;
  180. }
  181. for_each_cpu(i, s->path_mask) {
  182. pi = s->path_map[i];
  183. if (pi && !pi->failed)
  184. goto done;
  185. }
  186. pi = NULL;
  187. done:
  188. put_cpu();
  189. return pi ? pi->path : NULL;
  190. }
  191. static struct path_selector_type ioa_ps = {
  192. .name = "io-affinity",
  193. .module = THIS_MODULE,
  194. .table_args = 1,
  195. .info_args = 1,
  196. .create = ioa_create,
  197. .destroy = ioa_destroy,
  198. .status = ioa_status,
  199. .add_path = ioa_add_path,
  200. .fail_path = ioa_fail_path,
  201. .reinstate_path = ioa_reinstate_path,
  202. .select_path = ioa_select_path,
  203. };
  204. static int __init dm_ioa_init(void)
  205. {
  206. int ret = dm_register_path_selector(&ioa_ps);
  207. if (ret < 0)
  208. DMERR("register failed %d", ret);
  209. return ret;
  210. }
  211. static void __exit dm_ioa_exit(void)
  212. {
  213. dm_unregister_path_selector(&ioa_ps);
  214. }
  215. module_init(dm_ioa_init);
  216. module_exit(dm_ioa_exit);
  217. MODULE_DESCRIPTION(DM_NAME " multipath path selector that selects paths based on the CPU IO is being executed on");
  218. MODULE_AUTHOR("Mike Christie <michael.christie@oracle.com>");
  219. MODULE_LICENSE("GPL");