algos.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #endif
  49. #if defined(__x86_64__) && !defined(__arch_um__)
  50. &raid6_sse2x1,
  51. &raid6_sse2x2,
  52. &raid6_sse2x4,
  53. #ifdef CONFIG_AS_AVX2
  54. &raid6_avx2x1,
  55. &raid6_avx2x2,
  56. &raid6_avx2x4,
  57. #endif
  58. #endif
  59. #ifdef CONFIG_ALTIVEC
  60. &raid6_altivec1,
  61. &raid6_altivec2,
  62. &raid6_altivec4,
  63. &raid6_altivec8,
  64. #endif
  65. #if defined(CONFIG_TILEGX)
  66. &raid6_tilegx8,
  67. #endif
  68. &raid6_intx1,
  69. &raid6_intx2,
  70. &raid6_intx4,
  71. &raid6_intx8,
  72. #ifdef CONFIG_KERNEL_MODE_NEON
  73. &raid6_neonx1,
  74. &raid6_neonx2,
  75. &raid6_neonx4,
  76. &raid6_neonx8,
  77. #endif
  78. NULL
  79. };
  80. void (*raid6_2data_recov)(int, size_t, int, int, void **);
  81. EXPORT_SYMBOL_GPL(raid6_2data_recov);
  82. void (*raid6_datap_recov)(int, size_t, int, void **);
  83. EXPORT_SYMBOL_GPL(raid6_datap_recov);
  84. const struct raid6_recov_calls *const raid6_recov_algos[] = {
  85. #ifdef CONFIG_AS_AVX2
  86. &raid6_recov_avx2,
  87. #endif
  88. #ifdef CONFIG_AS_SSSE3
  89. &raid6_recov_ssse3,
  90. #endif
  91. &raid6_recov_intx1,
  92. NULL
  93. };
  94. #ifdef __KERNEL__
  95. #define RAID6_TIME_JIFFIES_LG2 4
  96. #else
  97. /* Need more time to be stable in userspace */
  98. #define RAID6_TIME_JIFFIES_LG2 9
  99. #define time_before(x, y) ((x) < (y))
  100. #endif
  101. static inline const struct raid6_recov_calls *raid6_choose_recov(void)
  102. {
  103. const struct raid6_recov_calls *const *algo;
  104. const struct raid6_recov_calls *best;
  105. for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
  106. if (!best || (*algo)->priority > best->priority)
  107. if (!(*algo)->valid || (*algo)->valid())
  108. best = *algo;
  109. if (best) {
  110. raid6_2data_recov = best->data2;
  111. raid6_datap_recov = best->datap;
  112. pr_info("raid6: using %s recovery algorithm\n", best->name);
  113. } else
  114. pr_err("raid6: Yikes! No recovery algorithm found!\n");
  115. return best;
  116. }
  117. static inline const struct raid6_calls *raid6_choose_gen(
  118. void *(*const dptrs)[(65536/PAGE_SIZE)+2], const int disks)
  119. {
  120. unsigned long perf, bestgenperf, bestxorperf, j0, j1;
  121. int start = (disks>>1)-1, stop = disks-3; /* work on the second half of the disks */
  122. const struct raid6_calls *const *algo;
  123. const struct raid6_calls *best;
  124. for (bestgenperf = 0, bestxorperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
  125. if (!best || (*algo)->prefer >= best->prefer) {
  126. if ((*algo)->valid && !(*algo)->valid())
  127. continue;
  128. perf = 0;
  129. preempt_disable();
  130. j0 = jiffies;
  131. while ((j1 = jiffies) == j0)
  132. cpu_relax();
  133. while (time_before(jiffies,
  134. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  135. (*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
  136. perf++;
  137. }
  138. preempt_enable();
  139. if (perf > bestgenperf) {
  140. bestgenperf = perf;
  141. best = *algo;
  142. }
  143. pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name,
  144. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  145. if (!(*algo)->xor_syndrome)
  146. continue;
  147. perf = 0;
  148. preempt_disable();
  149. j0 = jiffies;
  150. while ((j1 = jiffies) == j0)
  151. cpu_relax();
  152. while (time_before(jiffies,
  153. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  154. (*algo)->xor_syndrome(disks, start, stop,
  155. PAGE_SIZE, *dptrs);
  156. perf++;
  157. }
  158. preempt_enable();
  159. if (best == *algo)
  160. bestxorperf = perf;
  161. pr_info("raid6: %-8s xor() %5ld MB/s\n", (*algo)->name,
  162. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2+1));
  163. }
  164. }
  165. if (best) {
  166. pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
  167. best->name,
  168. (bestgenperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  169. if (best->xor_syndrome)
  170. pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
  171. (bestxorperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2+1));
  172. raid6_call = *best;
  173. } else
  174. pr_err("raid6: Yikes! No algorithm found!\n");
  175. return best;
  176. }
  177. /* Try to pick the best algorithm */
  178. /* This code uses the gfmul table as convenient data set to abuse */
  179. int __init raid6_select_algo(void)
  180. {
  181. const int disks = (65536/PAGE_SIZE)+2;
  182. const struct raid6_calls *gen_best;
  183. const struct raid6_recov_calls *rec_best;
  184. char *syndromes;
  185. void *dptrs[(65536/PAGE_SIZE)+2];
  186. int i;
  187. for (i = 0; i < disks-2; i++)
  188. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  189. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  190. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  191. if (!syndromes) {
  192. pr_err("raid6: Yikes! No memory available.\n");
  193. return -ENOMEM;
  194. }
  195. dptrs[disks-2] = syndromes;
  196. dptrs[disks-1] = syndromes + PAGE_SIZE;
  197. /* select raid gen_syndrome function */
  198. gen_best = raid6_choose_gen(&dptrs, disks);
  199. /* select raid recover functions */
  200. rec_best = raid6_choose_recov();
  201. free_pages((unsigned long)syndromes, 1);
  202. return gen_best && rec_best ? 0 : -EINVAL;
  203. }
  204. static void raid6_exit(void)
  205. {
  206. do { } while (0);
  207. }
  208. subsys_initcall(raid6_select_algo);
  209. module_exit(raid6_exit);
  210. MODULE_LICENSE("GPL");
  211. MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");