Skip to content

Commit 1749c00

Browse files
mitaDavid Woodhouse
authored andcommitted
mtd: mtd_nandecctest: ensure alignment requirement for bitops
Currently the data blocks which is used to test single bit error correction is allocated statically and injecting single bit error is implemented by using __change_bit() which must operate on the memory aligned to the size of an "unsigned long". But there is no such guarantee for statically allocated array. This fix the issue by allocating the data block dynamically by kmalloc(). It also allocate the ecc code dynamically instead of allocating statically on stack. The reason to allocate the ecc code dynamically is that later change will add tests which inject bit errors into the ecc code by bitops. Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent c5b8384 commit 1749c00

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

‎drivers/mtd/tests/mtd_nandecctest.c‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/random.h>
55
#include <linux/string.h>
66
#include <linux/bitops.h>
7+
#include <linux/slab.h>
78
#include <linux/mtd/nand_ecc.h>
89

910
#if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)
@@ -31,16 +32,24 @@ static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
3132
DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
3233
}
3334

34-
static unsigned char correct_data[512];
35-
static unsigned char error_data[512];
36-
3735
static int nand_ecc_test(const size_t size)
3836
{
39-
unsigned char correct_ecc[3];
40-
unsigned char error_ecc[3];
37+
int err = 0;
38+
void *error_data;
39+
void *error_ecc;
40+
void *correct_data;
41+
void *correct_ecc;
4142
char testname[30];
4243

43-
BUG_ON(sizeof(correct_data) < size);
44+
error_data = kmalloc(size, GFP_KERNEL);
45+
error_ecc = kmalloc(3, GFP_KERNEL);
46+
correct_data = kmalloc(size, GFP_KERNEL);
47+
correct_ecc = kmalloc(3, GFP_KERNEL);
48+
49+
if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
50+
err = -ENOMEM;
51+
goto error;
52+
}
4453

4554
sprintf(testname, "nand-ecc-%zu", size);
4655

@@ -53,15 +62,21 @@ static int nand_ecc_test(const size_t size)
5362
__nand_calculate_ecc(error_data, size, error_ecc);
5463
__nand_correct_data(error_data, correct_ecc, error_ecc, size);
5564

56-
if (!memcmp(correct_data, error_data, size)) {
57-
pr_info("mtd_nandecctest: ok - %s\n", testname);
58-
return 0;
65+
if (memcmp(correct_data, error_data, size)) {
66+
pr_err("mtd_nandecctest: not ok - %s\n", testname);
67+
dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc,
68+
size);
69+
err = -EINVAL;
70+
goto error;
5971
}
60-
61-
pr_err("mtd_nandecctest: not ok - %s\n", testname);
62-
dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc, size);
63-
64-
return -EINVAL;
72+
pr_info("mtd_nandecctest: ok - %s\n", testname);
73+
error:
74+
kfree(error_data);
75+
kfree(error_ecc);
76+
kfree(correct_data);
77+
kfree(correct_ecc);
78+
79+
return err;
6580
}
6681

6782
#else

0 commit comments

Comments
 (0)