1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* -*- linux-c -*- ------------------------------------------------------- * 3 * 4 * Copyright 2002 H. Peter Anvin - All Rights Reserved 5 * 6 * ----------------------------------------------------------------------- */ 7 8 /* 9 * raid6/algos.c 10 * 11 * Algorithm list and algorithm selection for RAID-6 12 */ 13 14 #include <linux/raid/pq.h> 15 #ifndef __KERNEL__ 16 #include <sys/mman.h> 17 #include <stdio.h> 18 #else 19 #include <linux/module.h> 20 #include <linux/gfp.h> 21 /* In .bss so it's zeroed */ 22 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256))); 23 EXPORT_SYMBOL(raid6_empty_zero_page); 24 #endif 25 26 struct raid6_calls raid6_call; 27 EXPORT_SYMBOL_GPL(raid6_call); 28 29 const struct raid6_calls * const raid6_algos[] = { 30 #if defined(__i386__) && !defined(__arch_um__) 31 &raid6_avx512x2, 32 &raid6_avx512x1, 33 &raid6_avx2x2, 34 &raid6_avx2x1, 35 &raid6_sse2x2, 36 &raid6_sse2x1, 37 &raid6_sse1x2, 38 &raid6_sse1x1, 39 &raid6_mmxx2, 40 &raid6_mmxx1, 41 #endif 42 #if defined(__x86_64__) && !defined(__arch_um__) 43 &raid6_avx512x4, 44 &raid6_avx512x2, 45 &raid6_avx512x1, 46 &raid6_avx2x4, 47 &raid6_avx2x2, 48 &raid6_avx2x1, 49 &raid6_sse2x4, 50 &raid6_sse2x2, 51 &raid6_sse2x1, 52 #endif 53 #ifdef CONFIG_ALTIVEC 54 &raid6_vpermxor8, 55 &raid6_vpermxor4, 56 &raid6_vpermxor2, 57 &raid6_vpermxor1, 58 &raid6_altivec8, 59 &raid6_altivec4, 60 &raid6_altivec2, 61 &raid6_altivec1, 62 #endif 63 #if defined(CONFIG_S390) 64 &raid6_s390vx8, 65 #endif 66 #ifdef CONFIG_KERNEL_MODE_NEON 67 &raid6_neonx8, 68 &raid6_neonx4, 69 &raid6_neonx2, 70 &raid6_neonx1, 71 #endif 72 #ifdef CONFIG_LOONGARCH 73 #ifdef CONFIG_CPU_HAS_LASX 74 &raid6_lasx, 75 #endif 76 #ifdef CONFIG_CPU_HAS_LSX 77 &raid6_lsx, 78 #endif 79 #endif 80 #ifdef CONFIG_RISCV_ISA_V 81 &raid6_rvvx1, 82 &raid6_rvvx2, 83 &raid6_rvvx4, 84 &raid6_rvvx8, 85 #endif 86 &raid6_intx8, 87 &raid6_intx4, 88 &raid6_intx2, 89 &raid6_intx1, 90 NULL 91 }; 92 93 void (*raid6_2data_recov)(int, size_t, int, int, void **); 94 EXPORT_SYMBOL_GPL(raid6_2data_recov); 95 96 void (*raid6_datap_recov)(int, size_t, int, void **); 97 EXPORT_SYMBOL_GPL(raid6_datap_recov); 98 99 const struct raid6_recov_calls *const raid6_recov_algos[] = { 100 #ifdef CONFIG_X86 101 &raid6_recov_avx512, 102 &raid6_recov_avx2, 103 &raid6_recov_ssse3, 104 #endif 105 #ifdef CONFIG_S390 106 &raid6_recov_s390xc, 107 #endif 108 #if defined(CONFIG_KERNEL_MODE_NEON) 109 &raid6_recov_neon, 110 #endif 111 #ifdef CONFIG_LOONGARCH 112 #ifdef CONFIG_CPU_HAS_LASX 113 &raid6_recov_lasx, 114 #endif 115 #ifdef CONFIG_CPU_HAS_LSX 116 &raid6_recov_lsx, 117 #endif 118 #endif 119 #ifdef CONFIG_RISCV_ISA_V 120 &raid6_recov_rvv, 121 #endif 122 &raid6_recov_intx1, 123 NULL 124 }; 125 126 #ifdef __KERNEL__ 127 #define RAID6_TIME_JIFFIES_LG2 4 128 #else 129 /* Need more time to be stable in userspace */ 130 #define RAID6_TIME_JIFFIES_LG2 9 131 #define time_before(x, y) ((x) < (y)) 132 #endif 133 134 #define RAID6_TEST_DISKS 8 135 #define RAID6_TEST_DISKS_ORDER 3 136 137 static inline const struct raid6_recov_calls *raid6_choose_recov(void) 138 { 139 const struct raid6_recov_calls *const *algo; 140 const struct raid6_recov_calls *best; 141 142 for (best = NULL, algo = raid6_recov_algos; *algo; algo++) 143 if (!best || (*algo)->priority > best->priority) 144 if (!(*algo)->valid || (*algo)->valid()) 145 best = *algo; 146 147 if (best) { 148 raid6_2data_recov = best->data2; 149 raid6_datap_recov = best->datap; 150 151 pr_info("raid6: using %s recovery algorithm\n", best->name); 152 } else 153 pr_err("raid6: Yikes! No recovery algorithm found!\n"); 154 155 return best; 156 } 157 158 static inline const struct raid6_calls *raid6_choose_gen( 159 void *(*const dptrs)[RAID6_TEST_DISKS], const int disks) 160 { 161 unsigned long perf, bestgenperf, j0, j1; 162 int start = (disks>>1)-1, stop = disks-3; /* work on the second half of the disks */ 163 const struct raid6_calls *const *algo; 164 const struct raid6_calls *best; 165 166 for (bestgenperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) { 167 if (!best || (*algo)->priority >= best->priority) { 168 if ((*algo)->valid && !(*algo)->valid()) 169 continue; 170 171 if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) { 172 best = *algo; 173 break; 174 } 175 176 perf = 0; 177 178 preempt_disable(); 179 j0 = jiffies; 180 while ((j1 = jiffies) == j0) 181 cpu_relax(); 182 while (time_before(jiffies, 183 j1 + (1<<RAID6_TIME_JIFFIES_LG2))) { 184 (*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs); 185 perf++; 186 } 187 preempt_enable(); 188 189 if (perf > bestgenperf) { 190 bestgenperf = perf; 191 best = *algo; 192 } 193 pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name, 194 (perf * HZ * (disks-2)) >> 195 (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2)); 196 } 197 } 198 199 if (!best) { 200 pr_err("raid6: Yikes! No algorithm found!\n"); 201 goto out; 202 } 203 204 raid6_call = *best; 205 206 if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK)) { 207 pr_info("raid6: skipped pq benchmark and selected %s\n", 208 best->name); 209 goto out; 210 } 211 212 pr_info("raid6: using algorithm %s gen() %ld MB/s\n", 213 best->name, 214 (bestgenperf * HZ * (disks - 2)) >> 215 (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2)); 216 217 if (best->xor_syndrome) { 218 perf = 0; 219 220 preempt_disable(); 221 j0 = jiffies; 222 while ((j1 = jiffies) == j0) 223 cpu_relax(); 224 while (time_before(jiffies, 225 j1 + (1 << RAID6_TIME_JIFFIES_LG2))) { 226 best->xor_syndrome(disks, start, stop, 227 PAGE_SIZE, *dptrs); 228 perf++; 229 } 230 preempt_enable(); 231 232 pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n", 233 (perf * HZ * (disks - 2)) >> 234 (20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2 + 1)); 235 } 236 237 out: 238 return best; 239 } 240 241 242 /* Try to pick the best algorithm */ 243 /* This code uses the gfmul table as convenient data set to abuse */ 244 245 int __init raid6_select_algo(void) 246 { 247 const int disks = RAID6_TEST_DISKS; 248 249 const struct raid6_calls *gen_best; 250 const struct raid6_recov_calls *rec_best; 251 char *disk_ptr, *p; 252 void *dptrs[RAID6_TEST_DISKS]; 253 int i, cycle; 254 255 /* prepare the buffer and fill it circularly with gfmul table */ 256 disk_ptr = (char *)__get_free_pages(GFP_KERNEL, RAID6_TEST_DISKS_ORDER); 257 if (!disk_ptr) { 258 pr_err("raid6: Yikes! No memory available.\n"); 259 return -ENOMEM; 260 } 261 262 p = disk_ptr; 263 for (i = 0; i < disks; i++) 264 dptrs[i] = p + PAGE_SIZE * i; 265 266 cycle = ((disks - 2) * PAGE_SIZE) / 65536; 267 for (i = 0; i < cycle; i++) { 268 memcpy(p, raid6_gfmul, 65536); 269 p += 65536; 270 } 271 272 if ((disks - 2) * PAGE_SIZE % 65536) 273 memcpy(p, raid6_gfmul, (disks - 2) * PAGE_SIZE % 65536); 274 275 /* select raid gen_syndrome function */ 276 gen_best = raid6_choose_gen(&dptrs, disks); 277 278 /* select raid recover functions */ 279 rec_best = raid6_choose_recov(); 280 281 free_pages((unsigned long)disk_ptr, RAID6_TEST_DISKS_ORDER); 282 283 return gen_best && rec_best ? 0 : -EINVAL; 284 } 285 286 static void raid6_exit(void) 287 { 288 do { } while (0); 289 } 290 291 subsys_initcall(raid6_select_algo); 292 module_exit(raid6_exit); 293 MODULE_LICENSE("GPL"); 294 MODULE_DESCRIPTION("RAID6 Q-syndrome calculations"); 295