algos.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <linux/module.h>
  19. #ifndef __KERNEL__
  20. #include <sys/mman.h>
  21. #include <stdio.h>
  22. #else
  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. #ifdef __KERNEL__
  63. #define RAID6_TIME_JIFFIES_LG2 4
  64. #else
  65. /* Need more time to be stable in userspace */
  66. #define RAID6_TIME_JIFFIES_LG2 9
  67. #define time_before(x, y) ((x) < (y))
  68. #endif
  69. /* Try to pick the best algorithm */
  70. /* This code uses the gfmul table as convenient data set to abuse */
  71. int __init raid6_select_algo(void)
  72. {
  73. const struct raid6_calls * const * algo;
  74. const struct raid6_calls * best;
  75. char *syndromes;
  76. void *dptrs[(65536/PAGE_SIZE)+2];
  77. int i, disks;
  78. unsigned long perf, bestperf;
  79. int bestprefer;
  80. unsigned long j0, j1;
  81. disks = (65536/PAGE_SIZE)+2;
  82. for ( i = 0 ; i < disks-2 ; i++ ) {
  83. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  84. }
  85. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  86. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  87. if ( !syndromes ) {
  88. printk("raid6: Yikes! No memory available.\n");
  89. return -ENOMEM;
  90. }
  91. dptrs[disks-2] = syndromes;
  92. dptrs[disks-1] = syndromes + PAGE_SIZE;
  93. bestperf = 0; bestprefer = 0; best = NULL;
  94. for ( algo = raid6_algos ; *algo ; algo++ ) {
  95. if ( !(*algo)->valid || (*algo)->valid() ) {
  96. perf = 0;
  97. preempt_disable();
  98. j0 = jiffies;
  99. while ( (j1 = jiffies) == j0 )
  100. cpu_relax();
  101. while (time_before(jiffies,
  102. j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
  103. (*algo)->gen_syndrome(disks, PAGE_SIZE, dptrs);
  104. perf++;
  105. }
  106. preempt_enable();
  107. if ( (*algo)->prefer > bestprefer ||
  108. ((*algo)->prefer == bestprefer &&
  109. perf > bestperf) ) {
  110. best = *algo;
  111. bestprefer = best->prefer;
  112. bestperf = perf;
  113. }
  114. printk("raid6: %-8s %5ld MB/s\n", (*algo)->name,
  115. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  116. }
  117. }
  118. if (best) {
  119. printk("raid6: using algorithm %s (%ld MB/s)\n",
  120. best->name,
  121. (bestperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  122. raid6_call = *best;
  123. } else
  124. printk("raid6: Yikes! No algorithm found!\n");
  125. free_pages((unsigned long)syndromes, 1);
  126. return best ? 0 : -EINVAL;
  127. }
  128. static void raid6_exit(void)
  129. {
  130. do { } while (0);
  131. }
  132. subsys_initcall(raid6_select_algo);
  133. module_exit(raid6_exit);
  134. MODULE_LICENSE("GPL");
  135. MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");