Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * asynchronous raid6 recovery self test
0004  * Copyright (c) 2009, Intel Corporation.
0005  *
0006  * based on drivers/md/raid6test/test.c:
0007  *  Copyright 2002-2007 H. Peter Anvin
0008  */
0009 #include <linux/async_tx.h>
0010 #include <linux/gfp.h>
0011 #include <linux/mm.h>
0012 #include <linux/random.h>
0013 #include <linux/module.h>
0014 
0015 #undef pr
0016 #define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
0017 
0018 #define NDISKS 64 /* Including P and Q */
0019 
0020 static struct page *dataptrs[NDISKS];
0021 unsigned int dataoffs[NDISKS];
0022 static addr_conv_t addr_conv[NDISKS];
0023 static struct page *data[NDISKS+3];
0024 static struct page *spare;
0025 static struct page *recovi;
0026 static struct page *recovj;
0027 
0028 static void callback(void *param)
0029 {
0030     struct completion *cmp = param;
0031 
0032     complete(cmp);
0033 }
0034 
0035 static void makedata(int disks)
0036 {
0037     int i;
0038 
0039     for (i = 0; i < disks; i++) {
0040         prandom_bytes(page_address(data[i]), PAGE_SIZE);
0041         dataptrs[i] = data[i];
0042         dataoffs[i] = 0;
0043     }
0044 }
0045 
0046 static char disk_type(int d, int disks)
0047 {
0048     if (d == disks - 2)
0049         return 'P';
0050     else if (d == disks - 1)
0051         return 'Q';
0052     else
0053         return 'D';
0054 }
0055 
0056 /* Recover two failed blocks. */
0057 static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb,
0058         struct page **ptrs, unsigned int *offs)
0059 {
0060     struct async_submit_ctl submit;
0061     struct completion cmp;
0062     struct dma_async_tx_descriptor *tx = NULL;
0063     enum sum_check_flags result = ~0;
0064 
0065     if (faila > failb)
0066         swap(faila, failb);
0067 
0068     if (failb == disks-1) {
0069         if (faila == disks-2) {
0070             /* P+Q failure.  Just rebuild the syndrome. */
0071             init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
0072             tx = async_gen_syndrome(ptrs, offs,
0073                     disks, bytes, &submit);
0074         } else {
0075             struct page *blocks[NDISKS];
0076             struct page *dest;
0077             int count = 0;
0078             int i;
0079 
0080             BUG_ON(disks > NDISKS);
0081 
0082             /* data+Q failure.  Reconstruct data from P,
0083              * then rebuild syndrome
0084              */
0085             for (i = disks; i-- ; ) {
0086                 if (i == faila || i == failb)
0087                     continue;
0088                 blocks[count++] = ptrs[i];
0089             }
0090             dest = ptrs[faila];
0091             init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
0092                       NULL, NULL, addr_conv);
0093             tx = async_xor(dest, blocks, 0, count, bytes, &submit);
0094 
0095             init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
0096             tx = async_gen_syndrome(ptrs, offs,
0097                     disks, bytes, &submit);
0098         }
0099     } else {
0100         if (failb == disks-2) {
0101             /* data+P failure. */
0102             init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
0103             tx = async_raid6_datap_recov(disks, bytes,
0104                     faila, ptrs, offs, &submit);
0105         } else {
0106             /* data+data failure. */
0107             init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
0108             tx = async_raid6_2data_recov(disks, bytes,
0109                     faila, failb, ptrs, offs, &submit);
0110         }
0111     }
0112     init_completion(&cmp);
0113     init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
0114     tx = async_syndrome_val(ptrs, offs,
0115             disks, bytes, &result, spare, 0, &submit);
0116     async_tx_issue_pending(tx);
0117 
0118     if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
0119         pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
0120            __func__, faila, failb, disks);
0121 
0122     if (result != 0)
0123         pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
0124            __func__, faila, failb, result);
0125 }
0126 
0127 static int test_disks(int i, int j, int disks)
0128 {
0129     int erra, errb;
0130 
0131     memset(page_address(recovi), 0xf0, PAGE_SIZE);
0132     memset(page_address(recovj), 0xba, PAGE_SIZE);
0133 
0134     dataptrs[i] = recovi;
0135     dataptrs[j] = recovj;
0136 
0137     raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs, dataoffs);
0138 
0139     erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
0140     errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
0141 
0142     pr("%s(%d, %d): faila=%3d(%c)  failb=%3d(%c)  %s\n",
0143        __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
0144        (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
0145 
0146     dataptrs[i] = data[i];
0147     dataptrs[j] = data[j];
0148 
0149     return erra || errb;
0150 }
0151 
0152 static int test(int disks, int *tests)
0153 {
0154     struct dma_async_tx_descriptor *tx;
0155     struct async_submit_ctl submit;
0156     struct completion cmp;
0157     int err = 0;
0158     int i, j;
0159 
0160     recovi = data[disks];
0161     recovj = data[disks+1];
0162     spare  = data[disks+2];
0163 
0164     makedata(disks);
0165 
0166     /* Nuke syndromes */
0167     memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
0168     memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
0169 
0170     /* Generate assumed good syndrome */
0171     init_completion(&cmp);
0172     init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
0173     tx = async_gen_syndrome(dataptrs, dataoffs, disks, PAGE_SIZE, &submit);
0174     async_tx_issue_pending(tx);
0175 
0176     if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
0177         pr("error: initial gen_syndrome(%d) timed out\n", disks);
0178         return 1;
0179     }
0180 
0181     pr("testing the %d-disk case...\n", disks);
0182     for (i = 0; i < disks-1; i++)
0183         for (j = i+1; j < disks; j++) {
0184             (*tests)++;
0185             err += test_disks(i, j, disks);
0186         }
0187 
0188     return err;
0189 }
0190 
0191 
0192 static int raid6_test(void)
0193 {
0194     int err = 0;
0195     int tests = 0;
0196     int i;
0197 
0198     for (i = 0; i < NDISKS+3; i++) {
0199         data[i] = alloc_page(GFP_KERNEL);
0200         if (!data[i]) {
0201             while (i--)
0202                 put_page(data[i]);
0203             return -ENOMEM;
0204         }
0205     }
0206 
0207     /* the 4-disk and 5-disk cases are special for the recovery code */
0208     if (NDISKS > 4)
0209         err += test(4, &tests);
0210     if (NDISKS > 5)
0211         err += test(5, &tests);
0212     /* the 11 and 12 disk cases are special for ioatdma (p-disabled
0213      * q-continuation without extended descriptor)
0214      */
0215     if (NDISKS > 12) {
0216         err += test(11, &tests);
0217         err += test(12, &tests);
0218     }
0219 
0220     /* the 24 disk case is special for ioatdma as it is the boundary point
0221      * at which it needs to switch from 8-source ops to 16-source
0222      * ops for continuation (assumes DMA_HAS_PQ_CONTINUE is not set)
0223      */
0224     if (NDISKS > 24)
0225         err += test(24, &tests);
0226 
0227     err += test(NDISKS, &tests);
0228 
0229     pr("\n");
0230     pr("complete (%d tests, %d failure%s)\n",
0231        tests, err, err == 1 ? "" : "s");
0232 
0233     for (i = 0; i < NDISKS+3; i++)
0234         put_page(data[i]);
0235 
0236     return 0;
0237 }
0238 
0239 static void raid6_test_exit(void)
0240 {
0241 }
0242 
0243 /* when compiled-in wait for drivers to load first (assumes dma drivers
0244  * are also compiled-in)
0245  */
0246 late_initcall(raid6_test);
0247 module_exit(raid6_test_exit);
0248 MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
0249 MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
0250 MODULE_LICENSE("GPL");