algos.c 4.6 KB

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