algos.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright 2002 H. Peter Anvin - All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
  8. * Boston MA 02111-1307, USA; either version 2 of the License, or
  9. * (at your option) any later version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * raid6/algos.c
  14. *
  15. * Algorithm list and algorithm selection for RAID-6
  16. */
  17. #include <linux/raid/pq.h>
  18. #ifndef __KERNEL__
  19. #include <sys/mman.h>
  20. #include <stdio.h>
  21. #else
  22. #include <linux/module.h>
  23. #include <linux/gfp.h>
  24. #if !RAID6_USE_EMPTY_ZERO_PAGE
  25. /* In .bss so it's zeroed */
  26. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  27. EXPORT_SYMBOL(raid6_empty_zero_page);
  28. #endif
  29. #endif
  30. struct raid6_calls raid6_call;
  31. EXPORT_SYMBOL_GPL(raid6_call);
  32. const struct raid6_calls * const raid6_algos[] = {
  33. #if defined(__ia64__)
  34. &raid6_intx16,
  35. &raid6_intx32,
  36. #endif
  37. #if defined(__i386__) && !defined(__arch_um__)
  38. &raid6_mmxx1,
  39. &raid6_mmxx2,
  40. &raid6_sse1x1,
  41. &raid6_sse1x2,
  42. &raid6_sse2x1,
  43. &raid6_sse2x2,
  44. #ifdef CONFIG_AS_AVX2
  45. &raid6_avx2x1,
  46. &raid6_avx2x2,
  47. #endif
  48. #ifdef CONFIG_AS_AVX512
  49. &raid6_avx512x1,
  50. &raid6_avx512x2,
  51. #endif
  52. #endif
  53. #if defined(__x86_64__) && !defined(__arch_um__)
  54. &raid6_sse2x1,
  55. &raid6_sse2x2,
  56. &raid6_sse2x4,
  57. #ifdef CONFIG_AS_AVX2
  58. &raid6_avx2x1,
  59. &raid6_avx2x2,
  60. &raid6_avx2x4,
  61. #endif
  62. #ifdef CONFIG_AS_AVX512
  63. &raid6_avx512x1,
  64. &raid6_avx512x2,
  65. &raid6_avx512x4,
  66. #endif
  67. #endif
  68. #ifdef CONFIG_ALTIVEC
  69. &raid6_altivec1,
  70. &raid6_altivec2,
  71. &raid6_altivec4,
  72. &raid6_altivec8,
  73. &raid6_vpermxor1,
  74. &raid6_vpermxor2,
  75. &raid6_vpermxor4,
  76. &raid6_vpermxor8,
  77. #endif
  78. #if defined(CONFIG_S390)
  79. &raid6_s390vx8,
  80. #endif
  81. &raid6_intx1,
  82. &raid6_intx2,
  83. &raid6_intx4,
  84. &raid6_intx8,
  85. #ifdef CONFIG_KERNEL_MODE_NEON
  86. &raid6_neonx1,
  87. &raid6_neonx2,
  88. &raid6_neonx4,
  89. &raid6_neonx8,
  90. #endif
  91. NULL
  92. };
  93. void (*raid6_2data_recov)(int, size_t, int, int, void **);
  94. EXPORT_SYMBOL_GPL(raid6_2data_recov);
  95. void (*raid6_datap_recov)(int, size_t, int, void **);
  96. EXPORT_SYMBOL_GPL(raid6_datap_recov);
  97. const struct raid6_recov_calls *const raid6_recov_algos[] = {
  98. #ifdef CONFIG_AS_AVX512
  99. &raid6_recov_avx512,
  100. #endif
  101. #ifdef CONFIG_AS_AVX2
  102. &raid6_recov_avx2,
  103. #endif
  104. #ifdef CONFIG_AS_SSSE3
  105. &raid6_recov_ssse3,
  106. #endif
  107. #ifdef CONFIG_S390
  108. &raid6_recov_s390xc,
  109. #endif
  110. #if defined(CONFIG_KERNEL_MODE_NEON)
  111. &raid6_recov_neon,
  112. #endif
  113. &raid6_recov_intx1,
  114. NULL
  115. };
  116. #ifdef __KERNEL__
  117. #define RAID6_TIME_JIFFIES_LG2 4
  118. #else
  119. /* Need more time to be stable in userspace */
  120. #define RAID6_TIME_JIFFIES_LG2 9
  121. #define time_before(x, y) ((x) < (y))
  122. #endif
  123. static inline const struct raid6_recov_calls *raid6_choose_recov(void)
  124. {
  125. const struct raid6_recov_calls *const *algo;
  126. const struct raid6_recov_calls *best;
  127. for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
  128. if (!best || (*algo)->priority > best->priority)
  129. if (!(*algo)->valid || (*algo)->valid())
  130. best = *algo;
  131. if (best) {
  132. raid6_2data_recov = best->data2;
  133. raid6_datap_recov = best->datap;
  134. pr_info("raid6: using %s recovery algorithm\n", best->name);
  135. } else
  136. pr_err("raid6: Yikes! No recovery algorithm found!\n");
  137. return best;
  138. }
  139. static inline const struct raid6_calls *raid6_choose_gen(
  140. void *(*const dptrs)[(65536/PAGE_SIZE)+2], const int disks)
  141. {
  142. unsigned long perf, bestgenperf, bestxorperf, j0, j1;
  143. int start = (disks>>1)-1, stop = disks-3; /* work on the second half of the disks */
  144. const struct raid6_calls *const *algo;
  145. const struct raid6_calls *best;
  146. for (bestgenperf = 0, bestxorperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
  147. if (!best || (*algo)->prefer >= best->prefer) {
  148. if ((*algo)->valid && !(*algo)->valid())
  149. continue;
  150. perf = 0;
  151. preempt_disable();
  152. j0 = jiffies;
  153. while ((j1 = jiffies) == j0)
  154. cpu_relax();
  155. while (time_before(jiffies,
  156. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  157. (*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
  158. perf++;
  159. }
  160. preempt_enable();
  161. if (perf > bestgenperf) {
  162. bestgenperf = perf;
  163. best = *algo;
  164. }
  165. pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name,
  166. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  167. if (!(*algo)->xor_syndrome)
  168. continue;
  169. perf = 0;
  170. preempt_disable();
  171. j0 = jiffies;
  172. while ((j1 = jiffies) == j0)
  173. cpu_relax();
  174. while (time_before(jiffies,
  175. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  176. (*algo)->xor_syndrome(disks, start, stop,
  177. PAGE_SIZE, *dptrs);
  178. perf++;
  179. }
  180. preempt_enable();
  181. if (best == *algo)
  182. bestxorperf = perf;
  183. pr_info("raid6: %-8s xor() %5ld MB/s\n", (*algo)->name,
  184. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2+1));
  185. }
  186. }
  187. if (best) {
  188. pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
  189. best->name,
  190. (bestgenperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  191. if (best->xor_syndrome)
  192. pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
  193. (bestxorperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2+1));
  194. raid6_call = *best;
  195. } else
  196. pr_err("raid6: Yikes! No algorithm found!\n");
  197. return best;
  198. }
  199. /* Try to pick the best algorithm */
  200. /* This code uses the gfmul table as convenient data set to abuse */
  201. int __init raid6_select_algo(void)
  202. {
  203. const int disks = (65536/PAGE_SIZE)+2;
  204. const struct raid6_calls *gen_best;
  205. const struct raid6_recov_calls *rec_best;
  206. char *syndromes;
  207. void *dptrs[(65536/PAGE_SIZE)+2];
  208. int i;
  209. for (i = 0; i < disks-2; i++)
  210. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  211. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  212. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  213. if (!syndromes) {
  214. pr_err("raid6: Yikes! No memory available.\n");
  215. return -ENOMEM;
  216. }
  217. dptrs[disks-2] = syndromes;
  218. dptrs[disks-1] = syndromes + PAGE_SIZE;
  219. /* select raid gen_syndrome function */
  220. gen_best = raid6_choose_gen(&dptrs, disks);
  221. /* select raid recover functions */
  222. rec_best = raid6_choose_recov();
  223. free_pages((unsigned long)syndromes, 1);
  224. return gen_best && rec_best ? 0 : -EINVAL;
  225. }
  226. static void raid6_exit(void)
  227. {
  228. do { } while (0);
  229. }
  230. subsys_initcall(raid6_select_algo);
  231. module_exit(raid6_exit);
  232. MODULE_LICENSE("GPL");
  233. MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");