mtd: mtd_nandecctest: ensure alignment requirement for bitops

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Sat Sep 29 10:59:13 EDT 2012


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=1749c00ffc909db4ebf1b2f17fd52cdb6e7b149c
Commit:     1749c00ffc909db4ebf1b2f17fd52cdb6e7b149c
Parent:     c5b8384abc11fd566a3633b7bd7d476ff04c31af
Author:     Akinobu Mita <akinobu.mita at gmail.com>
AuthorDate: Mon Sep 3 22:00:01 2012 +0900
Committer:  David Woodhouse <David.Woodhouse at intel.com>
CommitDate: Sat Sep 29 15:34:30 2012 +0100

    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 at gmail.com>
    Signed-off-by: Artem Bityutskiy <artem.bityutskiy at linux.intel.com>
    Signed-off-by: David Woodhouse <David.Woodhouse at intel.com>
---
 drivers/mtd/tests/mtd_nandecctest.c |   43 +++++++++++++++++++++++-----------
 1 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/drivers/mtd/tests/mtd_nandecctest.c b/drivers/mtd/tests/mtd_nandecctest.c
index 2b2d1a9..d3e8873 100644
--- a/drivers/mtd/tests/mtd_nandecctest.c
+++ b/drivers/mtd/tests/mtd_nandecctest.c
@@ -4,6 +4,7 @@
 #include <linux/random.h>
 #include <linux/string.h>
 #include <linux/bitops.h>
+#include <linux/slab.h>
 #include <linux/mtd/nand_ecc.h>
 
 #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,
 			DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
 }
 
-static unsigned char correct_data[512];
-static unsigned char error_data[512];
-
 static int nand_ecc_test(const size_t size)
 {
-	unsigned char correct_ecc[3];
-	unsigned char error_ecc[3];
+	int err = 0;
+	void *error_data;
+	void *error_ecc;
+	void *correct_data;
+	void *correct_ecc;
 	char testname[30];
 
-	BUG_ON(sizeof(correct_data) < size);
+	error_data = kmalloc(size, GFP_KERNEL);
+	error_ecc = kmalloc(3, GFP_KERNEL);
+	correct_data = kmalloc(size, GFP_KERNEL);
+	correct_ecc = kmalloc(3, GFP_KERNEL);
+
+	if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
+		err = -ENOMEM;
+		goto error;
+	}
 
 	sprintf(testname, "nand-ecc-%zu", size);
 
@@ -53,15 +62,21 @@ static int nand_ecc_test(const size_t size)
 	__nand_calculate_ecc(error_data, size, error_ecc);
 	__nand_correct_data(error_data, correct_ecc, error_ecc, size);
 
-	if (!memcmp(correct_data, error_data, size)) {
-		pr_info("mtd_nandecctest: ok - %s\n", testname);
-		return 0;
+	if (memcmp(correct_data, error_data, size)) {
+		pr_err("mtd_nandecctest: not ok - %s\n", testname);
+		dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc,
+				size);
+		err = -EINVAL;
+		goto error;
 	}
-
-	pr_err("mtd_nandecctest: not ok - %s\n", testname);
-	dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc, size);
-
-	return -EINVAL;
+	pr_info("mtd_nandecctest: ok - %s\n", testname);
+error:
+	kfree(error_data);
+	kfree(error_ecc);
+	kfree(correct_data);
+	kfree(correct_ecc);
+
+	return err;
 }
 
 #else



More information about the linux-mtd-cvs mailing list