algos.c 6.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/* -*- linux-c -*- ------------------------------------------------------- *
 *
 *   Copyright 2002 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
A
Atsushi SAKAI 已提交
8
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
L
Linus Torvalds 已提交
9 10 11 12 13
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
14
 * raid6/algos.c
L
Linus Torvalds 已提交
15 16 17 18
 *
 * Algorithm list and algorithm selection for RAID-6
 */

19
#include <linux/raid/pq.h>
L
Linus Torvalds 已提交
20 21
#ifndef __KERNEL__
#include <sys/mman.h>
H
H. Peter Anvin 已提交
22
#include <stdio.h>
23
#else
J
Jim Kukunas 已提交
24
#include <linux/module.h>
N
NeilBrown 已提交
25
#include <linux/gfp.h>
26 27 28 29 30
#if !RAID6_USE_EMPTY_ZERO_PAGE
/* In .bss so it's zeroed */
const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
EXPORT_SYMBOL(raid6_empty_zero_page);
#endif
L
Linus Torvalds 已提交
31 32 33
#endif

struct raid6_calls raid6_call;
34
EXPORT_SYMBOL_GPL(raid6_call);
L
Linus Torvalds 已提交
35 36 37 38 39 40

const struct raid6_calls * const raid6_algos[] = {
#if defined(__ia64__)
	&raid6_intx16,
	&raid6_intx32,
#endif
A
Al Viro 已提交
41
#if defined(__i386__) && !defined(__arch_um__)
L
Linus Torvalds 已提交
42 43 44 45 46 47
	&raid6_mmxx1,
	&raid6_mmxx2,
	&raid6_sse1x1,
	&raid6_sse1x2,
	&raid6_sse2x1,
	&raid6_sse2x2,
48 49 50 51
#ifdef CONFIG_AS_AVX2
	&raid6_avx2x1,
	&raid6_avx2x2,
#endif
L
Linus Torvalds 已提交
52
#endif
A
Al Viro 已提交
53
#if defined(__x86_64__) && !defined(__arch_um__)
L
Linus Torvalds 已提交
54 55 56
	&raid6_sse2x1,
	&raid6_sse2x2,
	&raid6_sse2x4,
57 58 59 60 61
#ifdef CONFIG_AS_AVX2
	&raid6_avx2x1,
	&raid6_avx2x2,
	&raid6_avx2x4,
#endif
L
Linus Torvalds 已提交
62 63 64 65 66 67
#endif
#ifdef CONFIG_ALTIVEC
	&raid6_altivec1,
	&raid6_altivec2,
	&raid6_altivec4,
	&raid6_altivec8,
68 69 70
#endif
#if defined(CONFIG_TILEGX)
	&raid6_tilegx8,
71 72 73
#endif
#if defined(CONFIG_S390)
	&raid6_s390vx8,
L
Linus Torvalds 已提交
74
#endif
75 76 77 78
	&raid6_intx1,
	&raid6_intx2,
	&raid6_intx4,
	&raid6_intx8,
79 80 81 82 83 84
#ifdef CONFIG_KERNEL_MODE_NEON
	&raid6_neonx1,
	&raid6_neonx2,
	&raid6_neonx4,
	&raid6_neonx8,
#endif
L
Linus Torvalds 已提交
85 86 87
	NULL
};

88 89 90 91 92 93 94
void (*raid6_2data_recov)(int, size_t, int, int, void **);
EXPORT_SYMBOL_GPL(raid6_2data_recov);

void (*raid6_datap_recov)(int, size_t, int, void **);
EXPORT_SYMBOL_GPL(raid6_datap_recov);

const struct raid6_recov_calls *const raid6_recov_algos[] = {
95 96 97
#ifdef CONFIG_AS_AVX2
	&raid6_recov_avx2,
#endif
98
#ifdef CONFIG_AS_SSSE3
99 100 101 102 103 104
	&raid6_recov_ssse3,
#endif
	&raid6_recov_intx1,
	NULL
};

L
Linus Torvalds 已提交
105 106 107 108 109
#ifdef __KERNEL__
#define RAID6_TIME_JIFFIES_LG2	4
#else
/* Need more time to be stable in userspace */
#define RAID6_TIME_JIFFIES_LG2	9
110
#define time_before(x, y) ((x) < (y))
L
Linus Torvalds 已提交
111 112
#endif

113
static inline const struct raid6_recov_calls *raid6_choose_recov(void)
114 115 116 117 118 119 120 121 122 123 124 125 126
{
	const struct raid6_recov_calls *const *algo;
	const struct raid6_recov_calls *best;

	for (best = NULL, algo = raid6_recov_algos; *algo; algo++)
		if (!best || (*algo)->priority > best->priority)
			if (!(*algo)->valid || (*algo)->valid())
				best = *algo;

	if (best) {
		raid6_2data_recov = best->data2;
		raid6_datap_recov = best->datap;

127
		pr_info("raid6: using %s recovery algorithm\n", best->name);
128
	} else
129
		pr_err("raid6: Yikes! No recovery algorithm found!\n");
130

131 132
	return best;
}
L
Linus Torvalds 已提交
133

134 135
static inline const struct raid6_calls *raid6_choose_gen(
	void *(*const dptrs)[(65536/PAGE_SIZE)+2], const int disks)
L
Linus Torvalds 已提交
136
{
137 138
	unsigned long perf, bestgenperf, bestxorperf, j0, j1;
	int start = (disks>>1)-1, stop = disks-3;	/* work on the second half of the disks */
139 140
	const struct raid6_calls *const *algo;
	const struct raid6_calls *best;
L
Linus Torvalds 已提交
141

142
	for (bestgenperf = 0, bestxorperf = 0, best = NULL, algo = raid6_algos; *algo; algo++) {
143 144 145
		if (!best || (*algo)->prefer >= best->prefer) {
			if ((*algo)->valid && !(*algo)->valid())
				continue;
L
Linus Torvalds 已提交
146 147 148 149 150

			perf = 0;

			preempt_disable();
			j0 = jiffies;
151
			while ((j1 = jiffies) == j0)
L
Linus Torvalds 已提交
152
				cpu_relax();
153 154
			while (time_before(jiffies,
					    j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
155
				(*algo)->gen_syndrome(disks, PAGE_SIZE, *dptrs);
L
Linus Torvalds 已提交
156 157 158 159
				perf++;
			}
			preempt_enable();

160 161
			if (perf > bestgenperf) {
				bestgenperf = perf;
162
				best = *algo;
L
Linus Torvalds 已提交
163
			}
164
			pr_info("raid6: %-8s gen() %5ld MB/s\n", (*algo)->name,
L
Linus Torvalds 已提交
165
			       (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

			if (!(*algo)->xor_syndrome)
				continue;

			perf = 0;

			preempt_disable();
			j0 = jiffies;
			while ((j1 = jiffies) == j0)
				cpu_relax();
			while (time_before(jiffies,
					    j1 + (1<<RAID6_TIME_JIFFIES_LG2))) {
				(*algo)->xor_syndrome(disks, start, stop,
						      PAGE_SIZE, *dptrs);
				perf++;
			}
			preempt_enable();

			if (best == *algo)
				bestxorperf = perf;

			pr_info("raid6: %-8s xor() %5ld MB/s\n", (*algo)->name,
				(perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2+1));
L
Linus Torvalds 已提交
189 190 191
		}
	}

192
	if (best) {
193
		pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
L
Linus Torvalds 已提交
194
		       best->name,
195 196 197 198
		       (bestgenperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
		if (best->xor_syndrome)
			pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
			       (bestxorperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2+1));
199 200
		raid6_call = *best;
	} else
201
		pr_err("raid6: Yikes!  No algorithm found!\n");
L
Linus Torvalds 已提交
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	return best;
}


/* Try to pick the best algorithm */
/* This code uses the gfmul table as convenient data set to abuse */

int __init raid6_select_algo(void)
{
	const int disks = (65536/PAGE_SIZE)+2;

	const struct raid6_calls *gen_best;
	const struct raid6_recov_calls *rec_best;
	char *syndromes;
	void *dptrs[(65536/PAGE_SIZE)+2];
	int i;

	for (i = 0; i < disks-2; i++)
		dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;

	/* Normal code - use a 2-page allocation to avoid D$ conflict */
	syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);

	if (!syndromes) {
227
		pr_err("raid6: Yikes!  No memory available.\n");
228 229 230 231 232 233 234 235
		return -ENOMEM;
	}

	dptrs[disks-2] = syndromes;
	dptrs[disks-1] = syndromes + PAGE_SIZE;

	/* select raid gen_syndrome function */
	gen_best = raid6_choose_gen(&dptrs, disks);
L
Linus Torvalds 已提交
236

237
	/* select raid recover functions */
238 239 240
	rec_best = raid6_choose_recov();

	free_pages((unsigned long)syndromes, 1);
241

242
	return gen_best && rec_best ? 0 : -EINVAL;
L
Linus Torvalds 已提交
243
}
244 245 246 247 248 249 250 251 252

static void raid6_exit(void)
{
	do { } while (0);
}

subsys_initcall(raid6_select_algo);
module_exit(raid6_exit);
MODULE_LICENSE("GPL");
253
MODULE_DESCRIPTION("RAID6 Q-syndrome calculations");