raid6algos.c 3.6 KB

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