algos.c 3.6 KB

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