algos.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #endif
  45. #if defined(__x86_64__) && !defined(__arch_um__)
  46. &raid6_sse2x1,
  47. &raid6_sse2x2,
  48. &raid6_sse2x4,
  49. #endif
  50. #ifdef CONFIG_ALTIVEC
  51. &raid6_altivec1,
  52. &raid6_altivec2,
  53. &raid6_altivec4,
  54. &raid6_altivec8,
  55. #endif
  56. &raid6_intx1,
  57. &raid6_intx2,
  58. &raid6_intx4,
  59. &raid6_intx8,
  60. NULL
  61. };
  62. void (*raid6_2data_recov)(int, size_t, int, int, void **);
  63. EXPORT_SYMBOL_GPL(raid6_2data_recov);
  64. void (*raid6_datap_recov)(int, size_t, int, void **);
  65. EXPORT_SYMBOL_GPL(raid6_datap_recov);
  66. const struct raid6_recov_calls *const raid6_recov_algos[] = {
  67. #if (defined(__i386__) || defined(__x86_64__)) && !defined(__arch_um__)
  68. #ifdef CONFIG_AS_AVX2
  69. &raid6_recov_avx2,
  70. #endif
  71. &raid6_recov_ssse3,
  72. #endif
  73. &raid6_recov_intx1,
  74. NULL
  75. };
  76. #ifdef __KERNEL__
  77. #define RAID6_TIME_JIFFIES_LG2 4
  78. #else
  79. /* Need more time to be stable in userspace */
  80. #define RAID6_TIME_JIFFIES_LG2 9
  81. #define time_before(x, y) ((x) < (y))
  82. #endif
  83. static inline const struct raid6_recov_calls *raid6_choose_recov(void)
  84. {
  85. const struct raid6_recov_calls *const *algo;
  86. const struct raid6_recov_calls *best;
  87. for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
  88. if (!best || (*algo)->priority > best->priority)
  89. if (!(*algo)->valid || (*algo)->valid())
  90. best = *algo;
  91. if (best) {
  92. raid6_2data_recov = best->data2;
  93. raid6_datap_recov = best->datap;
  94. printk("raid6: using %s recovery algorithm\n", best->name);
  95. } else
  96. printk("raid6: Yikes! No recovery algorithm found!\n");
  97. return best;
  98. }
  99. static inline const struct raid6_calls *raid6_choose_gen(
  100. void *(*const dptrs)[(65536/PAGE_SIZE)+2], const int disks)
  101. {
  102. unsigned long perf, bestperf, j0, j1;
  103. const struct raid6_calls *const *algo;
  104. const struct raid6_calls *best;
  105. for (bestperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
  106. if (!best || (*algo)->prefer >= best->prefer) {
  107. if ((*algo)->valid && !(*algo)->valid())
  108. continue;
  109. perf = 0;
  110. preempt_disable();
  111. j0 = jiffies;
  112. while ((j1 = jiffies) == j0)
  113. cpu_relax();
  114. while (time_before(jiffies,
  115. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  116. (*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
  117. perf++;
  118. }
  119. preempt_enable();
  120. if (perf > bestperf) {
  121. bestperf = perf;
  122. best = *algo;
  123. }
  124. printk("raid6: %-8s %5ld MB/s\n", (*algo)->name,
  125. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  126. }
  127. }
  128. if (best) {
  129. printk("raid6: using algorithm %s (%ld MB/s)\n",
  130. best->name,
  131. (bestperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  132. raid6_call = *best;
  133. } else
  134. printk("raid6: Yikes! No algorithm found!\n");
  135. return best;
  136. }
  137. /* Try to pick the best algorithm */
  138. /* This code uses the gfmul table as convenient data set to abuse */
  139. int __init raid6_select_algo(void)
  140. {
  141. const int disks = (65536/PAGE_SIZE)+2;
  142. const struct raid6_calls *gen_best;
  143. const struct raid6_recov_calls *rec_best;
  144. char *syndromes;
  145. void *dptrs[(65536/PAGE_SIZE)+2];
  146. int i;
  147. for (i = 0; i < disks-2; i++)
  148. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  149. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  150. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  151. if (!syndromes) {
  152. printk("raid6: Yikes! No memory available.\n");
  153. return -ENOMEM;
  154. }
  155. dptrs[disks-2] = syndromes;
  156. dptrs[disks-1] = syndromes + PAGE_SIZE;
  157. /* select raid gen_syndrome function */
  158. gen_best = raid6_choose_gen(&dptrs, disks);
  159. /* select raid recover functions */
  160. rec_best = raid6_choose_recov();
  161. free_pages((unsigned long)syndromes, 1);
  162. return gen_best && rec_best ? 0 : -EINVAL;
  163. }
  164. static void raid6_exit(void)
  165. {
  166. do { } while (0);
  167. }
  168. subsys_initcall(raid6_select_algo);
  169. module_exit(raid6_exit);
  170. MODULE_LICENSE("GPL");
  171. MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");