raid6algos.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * raid6algos.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. #if !RAID6_USE_EMPTY_ZERO_PAGE
  23. /* In .bss so it's zeroed */
  24. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  25. EXPORT_SYMBOL(raid6_empty_zero_page);
  26. #endif
  27. #endif
  28. struct raid6_calls raid6_call;
  29. EXPORT_SYMBOL_GPL(raid6_call);
  30. /* Various routine sets */
  31. extern const struct raid6_calls raid6_intx1;
  32. extern const struct raid6_calls raid6_intx2;
  33. extern const struct raid6_calls raid6_intx4;
  34. extern const struct raid6_calls raid6_intx8;
  35. extern const struct raid6_calls raid6_intx16;
  36. extern const struct raid6_calls raid6_intx32;
  37. extern const struct raid6_calls raid6_mmxx1;
  38. extern const struct raid6_calls raid6_mmxx2;
  39. extern const struct raid6_calls raid6_sse1x1;
  40. extern const struct raid6_calls raid6_sse1x2;
  41. extern const struct raid6_calls raid6_sse2x1;
  42. extern const struct raid6_calls raid6_sse2x2;
  43. extern const struct raid6_calls raid6_sse2x4;
  44. extern const struct raid6_calls raid6_altivec1;
  45. extern const struct raid6_calls raid6_altivec2;
  46. extern const struct raid6_calls raid6_altivec4;
  47. extern const struct raid6_calls raid6_altivec8;
  48. const struct raid6_calls * const raid6_algos[] = {
  49. &raid6_intx1,
  50. &raid6_intx2,
  51. &raid6_intx4,
  52. &raid6_intx8,
  53. #if defined(__ia64__)
  54. &raid6_intx16,
  55. &raid6_intx32,
  56. #endif
  57. #if defined(__i386__) && !defined(__arch_um__)
  58. &raid6_mmxx1,
  59. &raid6_mmxx2,
  60. &raid6_sse1x1,
  61. &raid6_sse1x2,
  62. &raid6_sse2x1,
  63. &raid6_sse2x2,
  64. #endif
  65. #if defined(__x86_64__) && !defined(__arch_um__)
  66. &raid6_sse2x1,
  67. &raid6_sse2x2,
  68. &raid6_sse2x4,
  69. #endif
  70. #ifdef CONFIG_ALTIVEC
  71. &raid6_altivec1,
  72. &raid6_altivec2,
  73. &raid6_altivec4,
  74. &raid6_altivec8,
  75. #endif
  76. NULL
  77. };
  78. #ifdef __KERNEL__
  79. #define RAID6_TIME_JIFFIES_LG2 4
  80. #else
  81. /* Need more time to be stable in userspace */
  82. #define RAID6_TIME_JIFFIES_LG2 9
  83. #define time_before(x, y) ((x) < (y))
  84. #endif
  85. /* Try to pick the best algorithm */
  86. /* This code uses the gfmul table as convenient data set to abuse */
  87. int __init raid6_select_algo(void)
  88. {
  89. const struct raid6_calls * const * algo;
  90. const struct raid6_calls * best;
  91. char *syndromes;
  92. void *dptrs[(65536/PAGE_SIZE)+2];
  93. int i, disks;
  94. unsigned long perf, bestperf;
  95. int bestprefer;
  96. unsigned long j0, j1;
  97. disks = (65536/PAGE_SIZE)+2;
  98. for ( i = 0 ; i < disks-2 ; i++ ) {
  99. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  100. }
  101. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  102. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  103. if ( !syndromes ) {
  104. printk("raid6: Yikes! No memory available.\n");
  105. return -ENOMEM;
  106. }
  107. dptrs[disks-2] = syndromes;
  108. dptrs[disks-1] = syndromes + PAGE_SIZE;
  109. bestperf = 0; bestprefer = 0; best = NULL;
  110. for ( algo = raid6_algos ; *algo ; algo++ ) {
  111. if ( !(*algo)->valid || (*algo)->valid() ) {
  112. perf = 0;
  113. preempt_disable();
  114. j0 = jiffies;
  115. while ( (j1 = jiffies) == j0 )
  116. cpu_relax();
  117. while (time_before(jiffies,
  118. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  119. (*algo)->gen_syndrome(disks, PAGE_SIZE, dptrs);
  120. perf++;
  121. }
  122. preempt_enable();
  123. if ( (*algo)->prefer > bestprefer ||
  124. ((*algo)->prefer == bestprefer &&
  125. perf > bestperf) ) {
  126. best = *algo;
  127. bestprefer = best->prefer;
  128. bestperf = perf;
  129. }
  130. printk("raid6: %-8s %5ld MB/s\n", (*algo)->name,
  131. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  132. }
  133. }
  134. if (best) {
  135. printk("raid6: using algorithm %s (%ld MB/s)\n",
  136. best->name,
  137. (bestperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  138. raid6_call = *best;
  139. } else
  140. printk("raid6: Yikes! No algorithm found!\n");
  141. free_pages((unsigned long)syndromes, 1);
  142. return best ? 0 : -EINVAL;
  143. }
  144. static void raid6_exit(void)
  145. {
  146. do { } while (0);
  147. }
  148. subsys_initcall(raid6_select_algo);
  149. module_exit(raid6_exit);
  150. MODULE_LICENSE("GPL");