Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2556810imm; Mon, 16 Jul 2018 09:57:35 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcytH0KawNXPY1CpgauY2rneicd/nfQBMWEy1oNkQXnGmjk6AYlBiBUhMvdMg3DArZcjVqN X-Received: by 2002:a63:b213:: with SMTP id x19-v6mr16190683pge.393.1531760255691; Mon, 16 Jul 2018 09:57:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531760255; cv=none; d=google.com; s=arc-20160816; b=HKN7x6nvAGJeforuLg2kqUHt5ZWc21a77eIsbJCqP5MsoF7UauL1PyD+P9o2AtXVbC 5fmiQaX/RxUF1l8BmNhDtQSBlkASeQMngyEj+y1/chHz8a1N7EUzEG/Nai6bLek7HxBB 0iq1zDX/4/20ZFyhspe7q2WfZF5QvXEiQ6FOaBeNNbpd8pXAUVxd77Vaq+lcxPgns7tX jJblEyvVTIYv41LMUlqIJdYmP2h8ExIeyZ6/P2xeDl3nhssTT3/4R3AU+Jm6P40ZADjf KZ2/4wH+iX9Irg21Y4G99HdlCSX17AN9UqFA4BPrisFrCZDJ3N2Qt7T044wupFgOODrV kH9w== 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=bp4KPAEtc1gUEshMcPKhIYvqPr8zVT6X+0Q4tNBA6cM=; b=nMhkYDfNWvNtnEAH9ReQWZlo/AJOyqt8wInPPkMMknNNnlVd1TRJLTlB6IdPR0Orns BD7XLtYK+m+3CedBl9fS4YYtUM5VwoVsb+B4hTwP9PFYm/sHeSHlmjc1HOYvfxdefUkR I/eVM3sm4FViPS9U4Rn+ybYo/1KhViAkvEIg9effr0FJ/eJDu5JuY135KMpkWqHano86 i1jQUYDR10AQesCgJW4yf+FcJzsBljkfrQhNPVoh4t8ZxpWz/mXzpv+G7R9OicElzKZA Ma2H4tD/BAyeOFxl5YFf8sPqX1yYmnHH8vbaTwQXSUmoyX0bagM+hHgLPdqf4nVVRsAk 5lZA== 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 v1-v6si29547064pgf.515.2018.07.16.09.57.20; Mon, 16 Jul 2018 09:57:35 -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 S1729967AbeGPRYC (ORCPT + 99 others); Mon, 16 Jul 2018 13:24:02 -0400 Received: from mx2.suse.de ([195.135.220.15]:54410 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727843AbeGPRYB (ORCPT ); Mon, 16 Jul 2018 13:24:01 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 6576DAF4E; Mon, 16 Jul 2018 16:55:43 +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 , "Luis R . Rodriguez" , Linus Torvalds , Thomas Gleixner , Kate Stewart Subject: [PATCH 4/4] lib/test_crc: Add test cases for crc calculation Date: Tue, 17 Jul 2018 00:55:07 +0800 Message-Id: <20180716165507.23100-5-colyli@suse.de> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180716165507.23100-1-colyli@suse.de> References: <20180716165507.23100-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_le: PASSED (0x4e6b1ff972fa8c55, expval 0x4e6b1ff972fa8c55) test_crc: crc64_le_bch: PASSED (0x0e4f1391d7a4a62e, expval 0x0e4f1391d7a4a62e) test_crc: crc64_le_update: FAILED (0x03d4d0d85685d9a1, expval 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 can is a testing frame work for all crc consistency testings. For now, there are only test caes for 3 crc routines, - crc64_le() - crc64_le_bch() - crc64_le_update() Signed-off-by: Coly Li Cc: Greg Kroah-Hartman Cc: Luis R. Rodriguez Cc: Linus Torvalds Cc: Thomas Gleixner Cc: Kate Stewart --- lib/Kconfig.debug | 11 ++++ lib/Makefile | 1 + lib/test_crc.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 lib/test_crc.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 8838d1158d19..c7deb4e2e4eb 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1911,6 +1911,17 @@ config TEST_SYSCTL If unsure, say N. +config TEST_CRC + tristate "CRC calculation test driver" + default n + 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..3a9442252de5 --- /dev/null +++ b/lib/test_crc.c @@ -0,0 +1,136 @@ +// 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_le() + * - crc64_le_bch() + * - crc64_le_update() + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct crc_test_record { + + char *name; + __le64 data[4]; + __le64 initval; + __le64 expval; + int (*handler)(struct crc_test_record *rec); +}; + +static int chk_and_msg(const char *name, __le64 crc, __le64 expval) +{ + int ret = 0; + + if (crc == expval) { + pr_info("test_crc: %s: PASSED:(0x%016llx, expval 0x%016llx)", + name, crc, expval); + } else { + pr_err("test_crc: %s: FAILED:(0x%016llx, expval 0x%016llx)", + name, crc, expval); + ret = -EINVAL; + } + + return ret; +} + +/* Add your crc test caese here */ +static int test_crc64_le(struct crc_test_record *rec) +{ + __le64 crc; + + crc = crc64_le(rec->data, sizeof(rec->data)); + return chk_and_msg(rec->name, crc, rec->expval); + +} + +static int test_crc64_le_bch(struct crc_test_record *rec) +{ + __le64 crc; + + crc = crc64_le_bch(rec->data, sizeof(rec->data)); + return chk_and_msg(rec->name, crc, rec->expval); +} + +static int test_crc64_le_update(struct crc_test_record *rec) +{ + __le64 crc = rec->initval; + + crc = crc64_le_update(crc, rec->data, sizeof(rec->data)); + return 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_le", + .data = { 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, + 0xC711223CFA3E5BB5, 0x493366450E42ECDF }, + .initval = 0, + .expval = 0xe2b9911e7b997201, + .handler = test_crc64_le, + }, + { .name = "crc64_le_bch", + .data = { 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, + 0xC711223CFA3E5BB5, 0x493366450E42ECDF }, + .initval = 0, + .expval = 0xd2753a20fd862892, + .handler = test_crc64_le_bch, + }, + { .name = "crc64_le_update", + .data = { 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, + 0xC711223CFA3E5BB5, 0x493366450E42ECDF }, + .initval = 0x61C8864680B583EB, + .expval = 0xb2c863673f4292bf, + .handler = test_crc64_le_update, + }, + { .name = NULL, } +}; + + +static int __init test_crc_init(void) +{ + int i; + int v, ret = 0; + + pr_info("Kernel crc consitency testing:"); + for (i = 0; test_data[i].name; i++) { + v = test_data[i].handler(&test_data[i]); + if (v < 0 && ret == 0) + ret = -EINVAL; + } + + return ret; +} +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"); -- 2.17.1