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