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