From patchwork Mon Oct 9 12:04:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Schnelle X-Patchwork-Id: 149990 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1817718vqo; Mon, 9 Oct 2023 05:05:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/X6vaOHDZhwwZApfLt8ZM57Cn2+MSbYOo1YSBe63C+N3M6cYgBQ31jOG8XC4jwygsijuo X-Received: by 2002:a17:90b:4387:b0:270:b961:1c3d with SMTP id in7-20020a17090b438700b00270b9611c3dmr12489591pjb.22.1696853124873; Mon, 09 Oct 2023 05:05:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696853124; cv=none; d=google.com; s=arc-20160816; b=agiF2CO8a7gI8mncKmqg4Sjr7ODHhiFpIiGTvW9c6/sqqD0GH+Tbbr6ldO45ZzdVO9 ATXLKH0hwntJJY7/QrvOUPbTmpSzxFpG8HwgKezRBLkLBHsDjmtiORBnSUutek8OW3gA rS82H6HdZnkm4gw5g/7c3fwFgj1DXTtXJxj8Nl64kaey3oNiC5y4GyyWGhbNZhCxhrv/ eiEmv+TjcFjwOvkd5MxH+j7u764um6DEJJq+1Cgl2PYFWA1dgTn+DoQxmNksQH23iu/C iaH6DHJpVzHL8Qz5of6jdOzr5Yp5AIrjoZqvoJ3OR1z2o0n8jo88sVyVhj1qw2F+eJ35 FJWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=06dSpSZxXR07VbbFIqBIzQpuBT3BvOOtnKrMUNjBk2E=; fh=NBqO6JLFhqgkgG3dBfrBBv178gCbgmktx4QiPPECsoA=; b=vd/wlk60OGhovU9NV15zGz+cEiufP+cELUaxK+0WlNb4NA6UT1CvKoSP535MSI8ms6 SCdZ3ZsFvVCd/vN1zdEmVM2OO734p4yXRXjOgoO9r8Mk7F4rOcCCVPIilHnMACenT3Y+ EOg72WD152YbuE4fTo19OTwMNzjxgfuFw/mEK95gKiYLS89rrlo5MfnylAFgIChd58G8 533BqOrV9nKhb2tEpLiU3G4RvzPG/7gwb5BOV1layabFOOehwk/EvvQ45D3xUyIvf08Z 3jc2tYl/ojivkZYD/cTbdMdvsBQxTtG2YL+4snkK1yv6bsyOySyH/OoD51S7a/RriHt/ SIgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=smNS8VqU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id h22-20020a17090aa89600b00278fe6b74c1si4928241pjq.25.2023.10.09.05.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 05:05:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=smNS8VqU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id BD189802A222; Mon, 9 Oct 2023 05:05:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346428AbjJIMFR (ORCPT + 18 others); Mon, 9 Oct 2023 08:05:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346308AbjJIMFQ (ORCPT ); Mon, 9 Oct 2023 08:05:16 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9A1DA6 for ; Mon, 9 Oct 2023 05:05:11 -0700 (PDT) Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 399BqMTn006664; Mon, 9 Oct 2023 12:05:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=pp1; bh=06dSpSZxXR07VbbFIqBIzQpuBT3BvOOtnKrMUNjBk2E=; b=smNS8VqUD38Wz4DpIUtb6EMnI90JqYMerGGaOkzSUHKK7tsW5fzL4EPXB6A82Fjcncgo GgEhOJs5RaqINkXmM3hrJ0ai0V7plBXXhev9j0NfI4qVpqNomcycTL/d19e6s539pPPo EOXgglXqykWNB6vOTNN7gaVkQ4zEkzIlIOaScqzabKwCinodYea8JqXqFrVnA8B2sPqp 95hA2B/26lZ4Ak0MkLL2B2txPajYyUZVazN13iqe5pTFdJme1ip0UVeR4PZBygrO36CT HG/0Qh5JW/iQm81YD+Gq/rPybCrtTbyv6yO5LWEPBFJxdZlNOcRmGCH6miE8aMJMMeiO bw== Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3tmh198dqp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 09 Oct 2023 12:05:02 +0000 Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 399BI40R024439; Mon, 9 Oct 2023 12:05:01 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 3tkhns91sn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 09 Oct 2023 12:05:01 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 399C4xl425100894 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 9 Oct 2023 12:04:59 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8BEC72004B; Mon, 9 Oct 2023 12:04:59 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7B62820040; Mon, 9 Oct 2023 12:04:59 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTPS; Mon, 9 Oct 2023 12:04:59 +0000 (GMT) Received: by tuxmaker.boeblingen.de.ibm.com (Postfix, from userid 55390) id 156B6E040D; Mon, 9 Oct 2023 14:04:59 +0200 (CEST) From: Sven Schnelle To: Arnd Bergmann , Kees Cook , Andy Shevchenko Cc: linux-kernel@vger.kernel.org Subject: [PATCH] tests: provide a word-at-a-time test implementation Date: Mon, 9 Oct 2023 14:04:55 +0200 Message-Id: <20231009120455.173862-1-svens@linux.ibm.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: cxv9pQOwCe66oRUI4meEk7GLM0DnYZFh X-Proofpoint-ORIG-GUID: cxv9pQOwCe66oRUI4meEk7GLM0DnYZFh X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-09_11,2023-10-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 mlxscore=0 phishscore=0 adultscore=0 spamscore=0 impostorscore=0 malwarescore=0 clxscore=1011 mlxlogscore=999 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2310090098 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 09 Oct 2023 05:05:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779279462500274411 X-GMAIL-MSGID: 1779279462500274411 Add some basic tests to test the correctness of has_zero() and find_zero(). Signed-off-by: Sven Schnelle Reviewed-by: Kees Cook --- lib/Kconfig.debug | 11 +++++++ lib/Makefile | 1 + lib/test_word-at-a-time.c | 62 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 lib/test_word-at-a-time.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index fa307f93fa2e..f7a4df3054ac 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2791,6 +2791,17 @@ config SIPHASH_KUNIT_TEST This is intended to help people writing architecture-specific optimized versions. If unsure, say N. +config WORDATATIME_KUNIT_TEST + tristate "KUnit test word-at-a-time implementation at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Enable this option to test the kernel's word-at-a-time () + functions on boot (or module load). + + This is intended to help people writing architecture-specific + optimized versions. If unsure, say N. + config TEST_UDELAY tristate "udelay test driver" help diff --git a/lib/Makefile b/lib/Makefile index 740109b6e2c8..b3bc6a698896 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -402,6 +402,7 @@ obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o obj-$(CONFIG_STRCAT_KUNIT_TEST) += strcat_kunit.o obj-$(CONFIG_STRSCPY_KUNIT_TEST) += strscpy_kunit.o obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o +obj-$(CONFIG_WORDATATIME_KUNIT_TEST) += test_word-at-a-time.o obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o diff --git a/lib/test_word-at-a-time.c b/lib/test_word-at-a-time.c new file mode 100644 index 000000000000..574303a4f913 --- /dev/null +++ b/lib/test_word-at-a-time.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include + +static void test_wordatatime_has_zero(struct kunit *test) +{ + const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; + unsigned long val, data; + + val = -1UL; + KUNIT_ASSERT_FALSE(test, has_zero(val, &data, &constants)); + + for (int i = 0; i < BITS_PER_LONG; i += 8) { + val = ~(0xffUL << i); + KUNIT_ASSERT_TRUE(test, has_zero(val, &data, &constants)); + } + + for (int i = 0; i < BITS_PER_LONG; i++) { + val = ~(0x1UL << i); + KUNIT_ASSERT_FALSE(test, has_zero(val, &data, &constants)); + } + + for (int i = 0; i < BITS_PER_LONG; i++) { + val = 0x1UL << i; + KUNIT_ASSERT_TRUE(test, has_zero(val, &data, &constants)); + } +} + +static void test_wordatatime_find_zero(struct kunit *test) +{ + const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; + unsigned long val, data; + + for (int i = 0; i < BITS_PER_LONG; i += 8) { + val = ~(0xffUL << i); + KUNIT_ASSERT_TRUE(test, has_zero(val, &data, &constants)); + data = prep_zero_mask(val, data, &constants); + data = create_zero_mask(data); +#ifdef CONFIG_CPU_BIG_ENDIAN + KUNIT_ASSERT_EQ(test, find_zero(data), (BITS_PER_LONG / 8 - 1) - (i / 8)); +#else + KUNIT_ASSERT_EQ(test, find_zero(data), i / 8); +#endif + } +} +static struct kunit_case wordatatime_test_cases[] = { + KUNIT_CASE(test_wordatatime_has_zero), + KUNIT_CASE(test_wordatatime_find_zero), + {} +}; + +static struct kunit_suite wordatatime_test_suite = { + .name = "wordatatime_test", + .test_cases = wordatatime_test_cases, +}; + +kunit_test_suites(&wordatatime_test_suite); +MODULE_LICENSE("GPL");