Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp747715imm; Wed, 18 Jul 2018 09:59:16 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdVCNhVm6MZ8N1i6YSzS+sM2U1Qf0eF5kxXPwuqjTv2pbCFW2FPqLNvYrrPOAbI0ze1pvPN X-Received: by 2002:a17:902:780d:: with SMTP id p13-v6mr6524985pll.119.1531933156418; Wed, 18 Jul 2018 09:59:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531933156; cv=none; d=google.com; s=arc-20160816; b=hI5QHlq6ekuhW+uTHlt5UUDhuSFBUzvVu6g/m7pAeINSLBo9IUGbypUed95TST0+gV 7QOCKolYA7f3GGhRAXJ3GbBVQFN9lLopWuvwOaEshPy1xxG5qJc9lnsH51pfbuVJsT5m rhakgAPtwWl/GS1cUUiWrqG5qgjmV4jHmDlSHSLWk3Jv4hjMyf2vBmnLdpUadVrJGC7e G2vhxJnYCASpd6JJXLsGCehP5KeDYYItX6f0N/VaU9/QaXM65naIz6UdH+Sx9EesRRxP 67V8kr3XpB3d/quEqGb9TxsOVVmZcpyhfPHO6B2OKAtvFhpMYB5OvlO8xopKNS34Suv8 DN9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=buIeh080eRaI6IJXetz6iR807acPruTpWDY4MctqLXg=; b=PzvPr/f6vAUhgcyw8OjVusxwYP21F004h24wMwVsG1daRAwAigpRaJ0pZP9VA0tMwx I3RKKHTRRZr5mpYghJbaWrZ4tUwH8uMhTrgU4zo2wceNXAoGbXal87+DcLpvboaaZyZl wNhMODFk3Wr9Z+0iKNJW/wQ+9IYZDnVAy3BwmmsXALZmIi49hPh/yS5qZLCpq7w5MxUW JN9QUQ5Vg6JQo4AF7Fwg5EfwR/2Af0mbXNKqkiQhFWPv/glclb9U7qVHqHdMkD2PjEoZ V+C7r2omTtRhCUaN5M0bem4a+70gOZEsuZ3uFKI7dPQUovf/FL1FTvMZ2lpzImpuyEvs kAnQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l63-v6si3779173pfg.326.2018.07.18.09.59.01; Wed, 18 Jul 2018 09:59:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731653AbeGRRgC (ORCPT + 99 others); Wed, 18 Jul 2018 13:36:02 -0400 Received: from mx2.suse.de ([195.135.220.15]:48054 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731327AbeGRRgC (ORCPT ); Wed, 18 Jul 2018 13:36:02 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 60583ADEC; Wed, 18 Jul 2018 16:57:13 +0000 (UTC) From: Coly Li To: linux-kernel@vger.kernel.org Cc: linux-bcache@vger.kernel.org, linux-block@vger.kernel.org, Coly Li , Greg Kroah-Hartman , Linus Torvalds , Thomas Gleixner , Kate Stewart , Andrew Morton , Randy Dunlap , Andy Shevchenko , Noah Massey Subject: [PATCH v4 3/3] lib/test_crc: Add test cases for crc calculation Date: Thu, 19 Jul 2018 00:55:45 +0800 Message-Id: <20180718165545.1622-4-colyli@suse.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180718165545.1622-1-colyli@suse.de> References: <20180718165545.1622-1-colyli@suse.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds a kernel module to test the consistency of multiple crc calculation in Linux kernel. It is enabled with CONFIG_TEST_CRC enabled. The test results are printed into kernel message, which look like, test_crc: crc64_be: FAILED (0x03d4d0d85685d9a1, expected 0x3d4d0d85685d9a1f) kernel 0day system has framework to check kernel message, then the above result can be handled by 0day system. If crc calculation inconsistency happens, it can be detected quite soon. lib/test_crc.c is a testing frame work for many crc consistency testings. For now, there is only one test caes for crc64_be(). Signed-off-by: Coly Li Reviewed-by: Hannes Reinecke Cc: Greg Kroah-Hartman Cc: Linus Torvalds Cc: Thomas Gleixner Cc: Kate Stewart Cc: Andrew Morton Cc: Randy Dunlap Cc: Andy Shevchenko Cc: Noah Massey --- Changelog: v4: Improve error statistic and failure message display by suggestion from Andy Shevchenko and Noah Massey. Fixes for review commennts of v3 v3: Add test cases passed/failed statistic More fixes for review comments of v2 v2: Fixes for review comments of v1 v1: Initial version. lib/Kconfig.debug | 10 +++++ lib/Makefile | 1 + lib/test_crc.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 lib/test_crc.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 8838d1158d19..a9c1de0c2a7d 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1911,6 +1911,16 @@ config TEST_SYSCTL If unsure, say N. +config TEST_CRC + tristate "CRC calculation test driver" + depends on CRC64 + help + This builds the "test_crc" module. This driver enables to test the + CRC calculation consistency to make sure new modification does not + break existing checksum calculation. + + if unsure, say N. + config TEST_UDELAY tristate "udelay test driver" default n diff --git a/lib/Makefile b/lib/Makefile index 40c215181687..224d047d026a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -49,6 +49,7 @@ obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o obj-$(CONFIG_TEST_BPF) += test_bpf.o obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o +obj-$(CONFIG_TEST_CRC) += test_crc.o obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o obj-$(CONFIG_TEST_KASAN) += test_kasan.o CFLAGS_test_kasan.o += -fno-builtin diff --git a/lib/test_crc.c b/lib/test_crc.c new file mode 100644 index 000000000000..324e8ad419d7 --- /dev/null +++ b/lib/test_crc.c @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * CRC test driver + * + * Copyright (C) 2018 Coly Li + * + * This module provides an simple framework to check the consistency of + * Linux kernel CRC calculation routines in lib/crc*.c. This driver + * requires CONFIG_CRC* items to be enabled if the associated routines are + * tested here. The test results will be printed to kernel message + * when this test driver is loaded. + * + * Current test routines are, + * - crc64_be() + */ + +#include +#include + +struct crc_test_record { + char *name; + u64 data[4]; + u64 initval; + u64 expval; + void (*handler)(struct crc_test_record *rec); +}; + +int failed_tests; +int total_tests; + +static void chk_and_msg(const char *name, u64 crc, u64 expval) +{ + total_tests++; + if (crc == expval) + return; + + pr_err("test_crc: %s: FAILED:(0x%016llx, expected 0x%016llx)\n", + name, crc, expval); + failed_tests++; +} + +/* Add your crc test cases here */ +static void test_crc64_be(struct crc_test_record *rec) +{ + u64 crc; + + crc = crc64_be(rec->initval, rec->data, sizeof(rec->data)); + chk_and_msg(rec->name, crc, rec->expval); +} + +/* + * Set up your crc test initial data here. + * Do not change the existing items, they are hard coded with + * pre-calculated values. + */ +static struct crc_test_record test_data[] = { + { .name = "crc64_be", + .data = { 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, + 0xC711223CFA3E5BB5, 0x493366450E42ECDF }, + .initval = 0x61C8864680B583EB, + .expval = 0xb2c863673f4292bf, + .handler = test_crc64_be, + }, + {} +}; + +static int __init test_crc_init(void) +{ + int i; + + failed_tests = 0; + total_tests = 0; + + pr_info("Kernel CRC consistency testing:\n"); + for (i = 0; test_data[i].name; i++) + test_data[i].handler(&test_data[i]); + + if (failed_tests == 0) + pr_info("test_crc: all %d tests passed\n", i); + else + pr_err("test_crc: %d cases tested, %d passed, %d failed\n", + total_tests, total_tests - failed_tests, failed_tests); + + return (failed_tests == 0) ? 0 : -EINVAL; +} +late_initcall(test_crc_init); + +static void __exit test_crc_exit(void) { } +module_exit(test_crc_exit); + +MODULE_DESCRIPTION("CRC consistency testing driver"); +MODULE_AUTHOR("Coly Li "); +MODULE_LICENSE("GPL v2"); -- 2.17.1