summaryrefslogtreecommitdiff
path: root/lib/raid/raid6/algos.c
blob: 6f5c89ab2b17be4b84f49cca03d440e5c5464cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2002 H. Peter Anvin - All Rights Reserved
 *
 * Algorithm list and algorithm selection for RAID-6
 */

#include <linux/module.h>
#include <linux/gfp.h>
#include <linux/raid/pq.h>
#include <linux/slab.h>
#include <linux/static_call.h>
#include <kunit/visibility.h>
#include "algos.h"

#define RAID6_MAX_ALGOS		16
static const struct raid6_calls *raid6_algos[RAID6_MAX_ALGOS];
static unsigned int raid6_nr_algos;
static const struct raid6_recov_calls *raid6_recov_algo;

/* Selected algorithm */
DEFINE_STATIC_CALL_NULL(raid6_gen_syndrome_impl, *raid6_intx1.gen_syndrome);
DEFINE_STATIC_CALL_NULL(raid6_xor_syndrome_impl, *raid6_intx1.xor_syndrome);
DEFINE_STATIC_CALL_NULL(raid6_recov_2data_impl, *raid6_recov_intx1.data2);
DEFINE_STATIC_CALL_NULL(raid6_recov_datap_impl, *raid6_recov_intx1.datap);

/**
 * raid6_gen_syndrome - generate RAID6 P/Q parity
 * @disks:	number of "disks" to operate on including parity
 * @bytes:	length in bytes of each vector
 * @ptrs:	@disks size array of memory pointers
 *
 * Generate @bytes worth of RAID6 P and Q parity in @ptrs[@disks - 2] and
 * @ptrs[@disks - 1] respectively from the memory pointed to by @ptrs[0] to
 * @ptrs[@disks - 3].
 *
 * @disks must be at least 4, and the memory pointed to by each member of @ptrs
 * must be at least 64-byte aligned.  @bytes must be non-zero and a multiple of
 * 512.
 *
 * See https://kernel.org/pub/linux/kernel/people/hpa/raid6.pdf for underlying
 * algorithm.
 */
void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs)
{
	WARN_ON_ONCE(!in_task() || irqs_disabled() || softirq_count());
	WARN_ON_ONCE(bytes & 511);
	WARN_ON_ONCE(disks < RAID6_MIN_DISKS);

	static_call(raid6_gen_syndrome_impl)(disks, bytes, ptrs);
}
EXPORT_SYMBOL_GPL(raid6_gen_syndrome);

/**
 * raid6_xor_syndrome - update RAID6 P/Q parity
 * @disks:	number of "disks" to operate on including parity
 * @start:	first index into @disk to update
 * @stop:	last index into @disk to update
 * @bytes:	length in bytes of each vector
 * @ptrs:	@disks size array of memory pointers
 *
 * Update @bytes worth of RAID6 P and Q parity in @ptrs[@disks - 2] and
 * @ptrs[@disks - 1] respectively for the memory pointed to by
 * @ptrs[@start..@stop].
 *
 * This is used to update parity in place using the following sequence:
 *
 * 1) call raid6_xor_syndrome(disk, start, stop, ...) for the existing data.
 * 2) update the the data in @ptrs[@start..@stop].
 * 3) call raid6_xor_syndrome(disk, start, stop, ...) for the new data.
 *
 * Data between @start and @stop that is not changed should be filled
 * with a pointer to the kernel zero page.
 *
 * @disks must be at least 4, and the memory pointed to by each member of @ptrs
 * must be at least 64-byte aligned.  @bytes must be non-zero and a multiple of
 * 512.  @stop must be larger or equal to @start.
 */
void raid6_xor_syndrome(int disks, int start, int stop, size_t bytes,
		void **ptrs)
{
	WARN_ON_ONCE(!in_task() || irqs_disabled() || softirq_count());
	WARN_ON_ONCE(bytes & 511);
	WARN_ON_ONCE(disks < RAID6_MIN_DISKS);
	WARN_ON_ONCE(stop < start);

	static_call(raid6_xor_syndrome_impl)(disks, start, stop, bytes, ptrs);
}
EXPORT_SYMBOL_GPL(raid6_xor_syndrome);

/*
 * raid6_can_xor_syndrome - check if raid6_xor_syndrome() can be used
 *
 * Returns %true if raid6_can_xor_syndrome() can be used, else %false.
 */
bool raid6_can_xor_syndrome(void)
{
	return !!static_call_query(raid6_xor_syndrome_impl);
}
EXPORT_SYMBOL_GPL(raid6_can_xor_syndrome);

/**
 * raid6_recov_2data - recover two missing data disks
 * @disks:	number of "disks" to operate on including parity
 * @bytes:	length in bytes of each vector
 * @faila:	first failed data disk index
 * @failb:	second failed data disk index
 * @ptrs:	@disks size array of memory pointers
 *
 * Rebuild @bytes of missing data in @ptrs[@faila] and @ptrs[@failb] from the
 * data in the remaining disks and the two parities pointed to by the other
 * indices between 0 and @disks - 1 in @ptrs.  @disks includes the data disks
 * and the two parities.  @faila must be smaller than @failb.
 *
 * Memory pointed to by each pointer in @ptrs must be page aligned and is
 * limited to %PAGE_SIZE.
 */
void raid6_recov_2data(int disks, size_t bytes, int faila, int failb,
		void **ptrs)
{
	WARN_ON_ONCE(!in_task() || irqs_disabled() || softirq_count());
	WARN_ON_ONCE(bytes & 511);
	WARN_ON_ONCE(bytes > PAGE_SIZE);
	WARN_ON_ONCE(failb <= faila);

	static_call(raid6_recov_2data_impl)(disks, bytes, faila, failb, ptrs);
}
EXPORT_SYMBOL_GPL(raid6_recov_2data);

/**
 * raid6_recov_datap - recover a missing data disk and missing P-parity
 * @disks:	number of "disks" to operate on including parity
 * @bytes:	length in bytes of each vector
 * @faila:	failed data disk index
 * @ptrs:	@disks size array of memory pointers
 *
 * Rebuild @bytes of missing data in @ptrs[@faila] and the missing P-parity in
 * @ptrs[@disks - 2] from the data in the remaining disks and the Q-parity
 * pointed to by the other indices between 0 and @disks - 1 in @ptrs.  @disks
 * includes the data disks and the two parities.
 *
 * Memory pointed to by each pointer in @ptrs must be page aligned and is
 * limited to %PAGE_SIZE.
 */
void raid6_recov_datap(int disks, size_t bytes, int faila, void **ptrs)
{
	WARN_ON_ONCE(!in_task() || irqs_disabled() || softirq_count());
	WARN_ON_ONCE(bytes & 511);
	WARN_ON_ONCE(bytes > PAGE_SIZE);

	static_call(raid6_recov_datap_impl)(disks, bytes, faila, ptrs);
}
EXPORT_SYMBOL_GPL(raid6_recov_datap);

#define RAID6_TIME_JIFFIES_LG2	4
#define RAID6_TEST_DISKS	8

static int raid6_choose_gen(void *(*const dptrs)[RAID6_TEST_DISKS],
		const int disks)
{
	/* work on the second half of the disks */
	int start = (disks >> 1) - 1, stop = disks - 3;
	const struct raid6_calls *best = NULL;
	unsigned long bestgenperf = 0;
	unsigned int i;

	for (i = 0; i < raid6_nr_algos; i++) {
		const struct raid6_calls *algo = raid6_algos[i];
		unsigned long perf = 0, j0, j1;

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

		if (perf > bestgenperf) {
			bestgenperf = perf;
			best = algo;
		}
		pr_info("raid6: %-8s gen() %5ld MB/s\n", algo->name,
			(perf * HZ * (disks-2)) >>
			(20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));
	}

	if (!best) {
		pr_err("raid6: Yikes! No algorithm found!\n");
		return -EINVAL;
	}

	static_call_update(raid6_gen_syndrome_impl, best->gen_syndrome);
	static_call_update(raid6_xor_syndrome_impl, best->xor_syndrome);

	pr_info("raid6: using algorithm %s gen() %ld MB/s\n",
		best->name,
		(bestgenperf * HZ * (disks - 2)) >>
		(20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2));

	if (best->xor_syndrome) {
		unsigned long perf = 0, j0, j1;

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

		pr_info("raid6: .... xor() %ld MB/s, rmw enabled\n",
			(perf * HZ * (disks - 2)) >>
			(20 - PAGE_SHIFT + RAID6_TIME_JIFFIES_LG2 + 1));
	}

	return 0;
}


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

static int __init raid6_select_algo(void)
{
	const int disks = RAID6_TEST_DISKS;
	char *disk_ptr, *p;
	void *dptrs[RAID6_TEST_DISKS];
	int i, cycle;
	int error;

	if (!IS_ENABLED(CONFIG_RAID6_PQ_BENCHMARK) || raid6_nr_algos == 1) {
		pr_info("raid6: skipped pq benchmark and selected %s\n",
			raid6_algos[raid6_nr_algos - 1]->name);
		static_call_update(raid6_gen_syndrome_impl,
				raid6_algos[raid6_nr_algos - 1]->gen_syndrome);
		static_call_update(raid6_xor_syndrome_impl,
				raid6_algos[raid6_nr_algos - 1]->xor_syndrome);
		return 0;
	}

	/* prepare the buffer and fill it circularly with gfmul table */
	disk_ptr = kmalloc(PAGE_SIZE * RAID6_TEST_DISKS, GFP_KERNEL);
	if (!disk_ptr) {
		pr_err("raid6: Yikes!  No memory available.\n");
		return -ENOMEM;
	}

	p = disk_ptr;
	for (i = 0; i < disks; i++)
		dptrs[i] = p + PAGE_SIZE * i;

	cycle = ((disks - 2) * PAGE_SIZE) / 65536;
	for (i = 0; i < cycle; i++) {
		memcpy(p, raid6_gfmul, 65536);
		p += 65536;
	}

	if ((disks - 2) * PAGE_SIZE % 65536)
		memcpy(p, raid6_gfmul, (disks - 2) * PAGE_SIZE % 65536);

	/* select raid gen_syndrome function */
	error = raid6_choose_gen(&dptrs, disks);

	kfree(disk_ptr);

	return error;
}

/*
 * Register a RAID6 P/Q generation algorithm.  The most optimized/unrolled
 * implementation should be registered last so it will be selected when the
 * boot-time benchmark is disabled.
 */
void __init raid6_algo_add(const struct raid6_calls *algo)
{
	if (WARN_ON_ONCE(raid6_nr_algos == RAID6_MAX_ALGOS))
		return;
	raid6_algos[raid6_nr_algos++] = algo;
}

void __init raid6_algo_add_default(void)
{
	raid6_algo_add(&raid6_intx1);
	raid6_algo_add(&raid6_intx2);
	raid6_algo_add(&raid6_intx4);
	raid6_algo_add(&raid6_intx8);
}

void __init raid6_recov_algo_add(const struct raid6_recov_calls *algo)
{
	if (WARN_ON_ONCE(raid6_recov_algo))
		return;
	raid6_recov_algo = algo;
}

#ifdef CONFIG_RAID6_PQ_ARCH
#include "pq_arch.h"
#else
static inline void arch_raid6_init(void)
{
	raid6_algo_add_default();
}
#endif /* CONFIG_RAID6_PQ_ARCH */

static int __init raid6_init(void)
{
	/*
	 * Architectures providing arch_raid6_init must add all PQ generation
	 * algorithms they want to consider in arch_raid6_init(), including
	 * the generic ones using raid6_algo_add_default() if wanted.
	 */
	arch_raid6_init();

	/*
	 * Architectures don't have to set a recovery algorithm, we'll just pick
	 * the generic integer one if none was set.
	 */
	if (!raid6_recov_algo)
		raid6_recov_algo = &raid6_recov_intx1;
	static_call_update(raid6_recov_2data_impl, raid6_recov_algo->data2);
	static_call_update(raid6_recov_datap_impl, raid6_recov_algo->datap);
	pr_info("raid6: using %s recovery algorithm\n", raid6_recov_algo->name);

	return raid6_select_algo();
}

static void __exit raid6_exit(void)
{
}

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

#if IS_ENABLED(CONFIG_RAID6_PQ_KUNIT_TEST)
const struct raid6_calls *raid6_algo_find(unsigned int idx)
{
	if (idx >= raid6_nr_algos) {
		/*
		 * Always include the simplest generic integer implementation in
		 * the unit tests as a baseline.
		 */
		if (idx == raid6_nr_algos &&
		    raid6_algos[0] != &raid6_intx1)
			return &raid6_intx1;
		return NULL;
	}
	return raid6_algos[idx];
}
EXPORT_SYMBOL_IF_KUNIT(raid6_algo_find);

const struct raid6_recov_calls *raid6_recov_algo_find(unsigned int idx)
{
	switch (idx) {
	case 0:
		/* always test the generic integer implementation */
		return &raid6_recov_intx1;
	case 1:
		/* test the optimized implementation if there is one */
		if (raid6_recov_algo != &raid6_recov_intx1)
			return raid6_recov_algo;
		return NULL;
	default:
		return NULL;
	}
}
EXPORT_SYMBOL_IF_KUNIT(raid6_recov_algo_find);
#endif /* CONFIG_RAID6_PQ_KUNIT_TEST */