algos.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- linux-c -*- ------------------------------------------------------- *
  3. *
  4. * Copyright 2002 H. Peter Anvin - All Rights Reserved
  5. *
  6. * ----------------------------------------------------------------------- */
  7. /*
  8. * raid6/algos.c
  9. *
  10. * Algorithm list and algorithm selection for RAID-6
  11. */
  12. #include <linux/raid/pq.h>
  13. #ifndef __KERNEL__
  14. #include <sys/mman.h>
  15. #include <stdio.h>
  16. #else
  17. #include <linux/module.h>
  18. #include <linux/gfp.h>
  19. #endif
  20. struct raid6_calls raid6_call;
  21. EXPORT_SYMBOL_GPL(raid6_call);
  22. const struct raid6_calls * const raid6_algos[] = {
  23. #if defined(__i386__) && !defined(__arch_um__)
  24. &raid6_avx512x2,
  25. &raid6_avx512x1,
  26. &raid6_avx2x2,
  27. &raid6_avx2x1,
  28. &raid6_sse2x2,
  29. &raid6_sse2x1,
  30. &raid6_sse1x2,
  31. &raid6_sse1x1,
  32. &raid6_mmxx2,
  33. &raid6_mmxx1,
  34. #endif
  35. #if defined(__x86_64__) && !defined(__arch_um__)
  36. &raid6_avx512x4,
  37. &raid6_avx512x2,
  38. &raid6_avx512x1,
  39. &raid6_avx2x4,
  40. &raid6_avx2x2,
  41. &raid6_avx2x1,
  42. &raid6_sse2x4,
  43. &raid6_sse2x2,
  44. &raid6_sse2x1,
  45. #endif
  46. #ifdef CONFIG_ALTIVEC
  47. &raid6_vpermxor8,
  48. &raid6_vpermxor4,
  49. &raid6_vpermxor2,
  50. &raid6_vpermxor1,
  51. &raid6_altivec8,
  52. &raid6_altivec4,
  53. &raid6_altivec2,
  54. &raid6_altivec1,
  55. #endif
  56. #if defined(CONFIG_S390)
  57. &raid6_s390vx8,
  58. #endif
  59. #ifdef CONFIG_KERNEL_MODE_NEON
  60. &raid6_neonx8,
  61. &raid6_neonx4,
  62. &raid6_neonx2,
  63. &raid6_neonx1,
  64. #endif
  65. #ifdef CONFIG_LOONGARCH
  66. #ifdef CONFIG_CPU_HAS_LASX
  67. &raid6_lasx,
  68. #endif
  69. #ifdef CONFIG_CPU_HAS_LSX
  70. &raid6_lsx,
  71. #endif
  72. #endif
  73. #ifdef CONFIG_RISCV_ISA_V
  74. &raid6_rvvx1,
  75. &raid6_rvvx2,
  76. &raid6_rvvx4,
  77. &raid6_rvvx8,
  78. #endif
  79. &raid6_intx8,
  80. &raid6_intx4,
  81. &raid6_intx2,
  82. &raid6_intx1,
  83. NULL
  84. };
  85. void (*raid6_2data_recov)(int, size_t, int, int, void **);
  86. EXPORT_SYMBOL_GPL(raid6_2data_recov);
  87. void (*raid6_datap_recov)(int, size_t, int, void **);
  88. EXPORT_SYMBOL_GPL(raid6_datap_recov);
  89. const struct raid6_recov_calls *const raid6_recov_algos[] = {
  90. #ifdef CONFIG_X86
  91. &raid6_recov_avx512,
  92. &raid6_recov_avx2,
  93. &raid6_recov_ssse3,
  94. #endif
  95. #ifdef CONFIG_S390
  96. &raid6_recov_s390xc,
  97. #endif
  98. #if defined(CONFIG_KERNEL_MODE_NEON)
  99. &raid6_recov_neon,
  100. #endif
  101. #ifdef CONFIG_LOONGARCH
  102. #ifdef CONFIG_CPU_HAS_LASX
  103. &raid6_recov_lasx,
  104. #endif
  105. #ifdef CONFIG_CPU_HAS_LSX
  106. &raid6_recov_lsx,
  107. #endif
  108. #endif
  109. #ifdef CONFIG_RISCV_ISA_V
  110. &raid6_recov_rvv,
  111. #endif
  112. &raid6_recov_intx1,
  113. NULL
  114. };
  115. #ifdef __KERNEL__
  116. #define RAID6_TIME_JIFFIES_LG2 4
  117. #else
  118. /* Need more time to be stable in userspace */
  119. #define RAID6_TIME_JIFFIES_LG2 9
  120. #define time_before(x, y) ((x) < (y))
  121. #endif
  122. #define RAID6_TEST_DISKS 8
  123. #define RAID6_TEST_DISKS_ORDER 3
  124. static inline const struct raid6_recov_calls *raid6_choose_recov(void)
  125. {
  126. const struct raid6_recov_calls *const *algo;
  127. const struct raid6_recov_calls *best;
  128. for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
  129. if (!best || (*algo)->priority > best->priority)
  130. if (!(*algo)->valid || (*algo)->valid())
  131. best = *algo;
  132. if (best) {
  133. raid6_2data_recov = best->data2;
  134. raid6_datap_recov = best->datap;
  135. pr_info("raid6: using %s recovery algorithm\n", best->name);
  136. } else
  137. pr_err("raid6: Yikes! No recovery algorithm found!\n");
  138. return best;
  139. }
  140. static inline const struct raid6_calls *raid6_choose_gen(
  141. void *(*const dptrs)[RAID6_TEST_DISKS], const int disks)
  142. {
  143. unsigned long perf, bestgenperf, j0, j1;
  144. int start = (disks>>1)-1, stop = disks-3; /* work on the second half of the disks */
  145. const struct raid6_calls *const *algo;
  146. const struct raid6_calls *best;
  147. for (bestgenperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
  148. if (!best || (*algo)->priority >= best->priority) {
  149. if ((*algo)->valid && !(*algo)->valid())
  150. continue;
  151. if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) {
  152. best = *algo;
  153. break;
  154. }
  155. perf = 0;
  156. preempt_disable();
  157. j0 = jiffies;
  158. while ((j1 = jiffies) == j0)
  159. cpu_relax();
  160. while (time_before(jiffies,
  161. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  162. (*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
  163. perf++;
  164. }
  165. preempt_enable();
  166. if (perf > bestgenperf) {
  167. bestgenperf = perf;
  168. best = *algo;
  169. }
  170. pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name,
  171. (perf * HZ * (disks-2)) >>
  172. (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));
  173. }
  174. }
  175. if (!best) {
  176. pr_err("raid6: Yikes! No algorithm found!\n");
  177. goto out;
  178. }
  179. raid6_call = *best;
  180. if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) {
  181. pr_info("raid6: skipped pq benchmark and selected %s\n",
  182. best->name);
  183. goto out;
  184. }
  185. pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
  186. best->name,
  187. (bestgenperf * HZ * (disks - 2)) >>
  188. (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));
  189. if (best->xor_syndrome) {
  190. perf = 0;
  191. preempt_disable();
  192. j0 = jiffies;
  193. while ((j1 = jiffies) == j0)
  194. cpu_relax();
  195. while (time_before(jiffies,
  196. j1 + (1 << RAID6_TIME_JIFFIES_LG2))) {
  197. best->xor_syndrome(disks, start, stop,
  198. PAGE_SIZE, *dptrs);
  199. perf++;
  200. }
  201. preempt_enable();
  202. pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
  203. (perf * HZ * (disks - 2)) >>
  204. (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2 + 1));
  205. }
  206. out:
  207. return best;
  208. }
  209. /* Try to pick the best algorithm */
  210. /* This code uses the gfmul table as convenient data set to abuse */
  211. int __init raid6_select_algo(void)
  212. {
  213. const int disks = RAID6_TEST_DISKS;
  214. const struct raid6_calls *gen_best;
  215. const struct raid6_recov_calls *rec_best;
  216. char *disk_ptr, *p;
  217. void *dptrs[RAID6_TEST_DISKS];
  218. int i, cycle;
  219. /* prepare the buffer and fill it circularly with gfmul table */
  220. disk_ptr = (char *)__get_free_pages(GFP_KERNEL, RAID6_TEST_DISKS_ORDER);
  221. if (!disk_ptr) {
  222. pr_err("raid6: Yikes! No memory available.\n");
  223. return -ENOMEM;
  224. }
  225. p = disk_ptr;
  226. for (i = 0; i < disks; i++)
  227. dptrs[i] = p + PAGE_SIZE * i;
  228. cycle = ((disks - 2) * PAGE_SIZE) / 65536;
  229. for (i = 0; i < cycle; i++) {
  230. memcpy(p, raid6_gfmul, 65536);
  231. p += 65536;
  232. }
  233. if ((disks - 2) * PAGE_SIZE % 65536)
  234. memcpy(p, raid6_gfmul, (disks - 2) * PAGE_SIZE % 65536);
  235. /* select raid gen_syndrome function */
  236. gen_best = raid6_choose_gen(&dptrs, disks);
  237. /* select raid recover functions */
  238. rec_best = raid6_choose_recov();
  239. free_pages((unsigned long)disk_ptr, RAID6_TEST_DISKS_ORDER);
  240. return gen_best && rec_best ? 0 : -EINVAL;
  241. }
  242. static void raid6_exit(void)
  243. {
  244. do { } while (0);
  245. }
  246. subsys_initcall(raid6_select_algo);
  247. module_exit(raid6_exit);
  248. MODULE_LICENSE("GPL");
  249. MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");