From nobody Thu Oct 2 06:32:27 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D000E287273 for ; Mon, 22 Sep 2025 09:00:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758531612; cv=none; b=LdwuLzKBD9viaVWxGLXLd7dhS+b+M+0geIJzQV78G45MvwU8Uhaz+60CRSIpW4pyfyaPURt0tm8jvQRv5iziBNKAZ+8FtHe/05sMoX9l0nSzSqRXNQVE9wSL3HZzErznmbyGnc4XvOhNypOTjhfqsxCYc39x9DaQ8rD2fuzVNNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758531612; c=relaxed/simple; bh=G27qEXWQqpyS77HZ4Ggj3iTnX1m9X4z98mMc0f/SONs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KRYXrCGab35PUQNJjurmNdR7tzkQQufLRclrzh2CA8Hdsa5fAliqTGSb6S89xxeAQikj9JILcL58xSS8qfBVQm89Kvhg1P7gUcWgd15Tm+2r0ITIBpi/vmsU1FAtArulczKJwtYPVfGEDnuCadN/bLU+SlGvB53CRhuIAJIoM8s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ECijtvv6; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ECijtvv6" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-45e037fd142so38257085e9.3 for ; Mon, 22 Sep 2025 02:00:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758531609; x=1759136409; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mi6TD8VyLf+L2Nc6qgjtgNBQkwWi+DuvkEzgMSAI6jo=; b=ECijtvv6hEvisEe3IlLsEGdsUEdGWbCX45KPX8K0WsX86xVEv6O/tIR2VqKWa8zDrO Gq0JfTM2eZW3Ge8in47d9N3pNn5H5MhmciR6NHmaM9bp8g96ms2EJx0YV+pjrV97TQQs fOH47IfBC4I+V3QOiY0Tj8TVjDxa1oQStPd7CPtOa1Y/mAUysT/1hoODRBhI0K6pNja4 1PB2DF7MI5l8W/22G8k0VDQPjvUOVcpm8TB8x4rr7AJBCrwwsmq8alosofQypDrUXOJB YZEt+yEaM5OmgScbV30LCms8geOsCMq4lKCJ6gWNpq1RW2UYd+JWb8vK/r4//EBkt7NS Yjog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758531609; x=1759136409; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mi6TD8VyLf+L2Nc6qgjtgNBQkwWi+DuvkEzgMSAI6jo=; b=cCGFO/RCG38XPqx9LIFF+bRSeJls659h3Xysb0TXjv15K12dxKSq1LgjDILAtiu0mU EmQEBBB0i7/qMoxw/8cwofnkLhLZ7IjxtT5kNmcog27QdCDf4d8xCLjZx3mkyfjjT9jT dQv9rQO5qc2ZQy5LS7r6pATQelq9L9TGS8R4VKqGFTk+GYKGlNcTZqd9Tfpu7SqKePBd 5Jq+YneIjjSeniDOwEcCbgiWsoigUdwRiduehbJozSw/cSM6bPB+OzVfUzajFvlcPGsy EvOU8MLQbMiFbr82Ypf4IfEo2ZFhWuSNe8ZdJvjhYIqsnPE+cEY8OdAOeuzuh+/4rmw9 IPfQ== X-Forwarded-Encrypted: i=1; AJvYcCWQcHQ2RVndkDBBVBPsU+na2nSgbXLWoO/rqvCvrkD+TbxWl7neFiJ/vD6OKKFABzbyv8vCOQZghkVWjzE=@vger.kernel.org X-Gm-Message-State: AOJu0YyBNCNi/+x22aJF31f3a2/j0T9Qk9+la2EC/0Fe0wM0LdPb1vhN gE7tkXAfruC99YRq47aFZs0ziEF7vq2O6daFQ0M0LvT7Xoi8yRoN+gC/Upck0cbvtMZ1i0qinzC CvW3vdN5QpCJjmw== X-Google-Smtp-Source: AGHT+IHEl5s+QPaPzGfgCuRqhCMt6KCfe3mA0A/82HtqGagB0IP7AhZDyC3DDVBt3JAEbplLYzuZt2vDDFw8ww== X-Received: from wmtk8.prod.google.com ([2002:a05:600c:c4a8:b0:45b:885a:402d]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f44:b0:46d:996b:8291 with SMTP id 5b1f17b1804b1-46d996b8960mr13252115e9.34.1758531609366; Mon, 22 Sep 2025 02:00:09 -0700 (PDT) Date: Mon, 22 Sep 2025 08:59:28 +0000 In-Reply-To: <20250922090003.686704-1-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250922090003.686704-1-smostafa@google.com> X-Mailer: git-send-email 2.51.0.534.gc79095c0ca-goog Message-ID: <20250922090003.686704-5-smostafa@google.com> Subject: [PATCH v4 4/4] iommu/io-pgtable-arm-selftests: Use KUnit From: Mostafa Saleh To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: robin.murphy@arm.com, will@kernel.org, joro@8bytes.org, jgg@ziepe.ca, praan@google.com, Mostafa Saleh , Jason Gunthorpe Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Integrate the selftests as part of kunit. Now instead of the test only being run at boot, it can run: - With CONFIG_IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST=3Dy It will automatically run at boot as before. - Otherwise with CONFIG_IOMMU_IO_PGTABLE_KUNIT_TEST=3Dm: 1) on module load: Once the module load the self test will run # modprobe io-pgtable-arm-selftests 2) debugfs With CONFIG_KUNIT_DEBUGFS=3Dy You can run the test with # echo 1 > /sys/kernel/debug/kunit/io-pgtable-arm-test/run 3) Using kunit.py You can also use the helper script which uses Qemu in the background # ./tools/testing/kunit/kunit.py run --build_dir build_kunit_arm64 --a= rch arm64 \ --make_options LLVM=3D1 --kunitconfig ./kunit/kunitconfig [18:01:09] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D io-pgtable-arm-tes= t (1 subtest) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [18:01:09] [PASSED] arm_lpae_do_selftests [18:01:09] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [PASSED] io-= pgtable-arm-test =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [18:01:09] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Signed-off-by: Mostafa Saleh Reviewed-by: Pranjal Shrivastava --- drivers/iommu/Kconfig | 11 ++-- drivers/iommu/Makefile | 2 +- drivers/iommu/io-pgtable-arm-selftests.c | 82 +++++++++++++----------- 3 files changed, 51 insertions(+), 44 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 553522ef3ca9..d50685433347 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -40,12 +40,13 @@ config IOMMU_IO_PGTABLE_LPAE sizes at both stage-1 and stage-2, as well as address spaces up to 48-bits in size. =20 -config IOMMU_IO_PGTABLE_LPAE_SELFTEST - tristate "LPAE selftests" - depends on IOMMU_IO_PGTABLE_LPAE +config IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST + tristate "KUnit tests for LPAE" + depends on IOMMU_IO_PGTABLE_LPAE && KUNIT + default KUNIT_ALL_TESTS help - Enable self-tests for LPAE page table allocator. This performs - a series of page-table consistency checks during boot. + Enable kunit tests for LPAE page table allocator. This performs + a series of page-table consistency checks. =20 If unsure, say N here. =20 diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile index 5250a2eea13f..ac3851570303 100644 --- a/drivers/iommu/Makefile +++ b/drivers/iommu/Makefile @@ -12,7 +12,7 @@ obj-$(CONFIG_IOMMU_DMA) +=3D dma-iommu.o obj-$(CONFIG_IOMMU_IO_PGTABLE) +=3D io-pgtable.o obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) +=3D io-pgtable-arm-v7s.o obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) +=3D io-pgtable-arm.o -obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST) +=3D io-pgtable-arm-selftests= .o +obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST) +=3D io-pgtable-arm-selftes= ts.o obj-$(CONFIG_IOMMU_IO_PGTABLE_DART) +=3D io-pgtable-dart.o obj-$(CONFIG_IOMMU_IOVA) +=3D iova.o obj-$(CONFIG_OF_IOMMU) +=3D of_iommu.o diff --git a/drivers/iommu/io-pgtable-arm-selftests.c b/drivers/iommu/io-pg= table-arm-selftests.c index 50350e88d9b4..fc18bd54a315 100644 --- a/drivers/iommu/io-pgtable-arm-selftests.c +++ b/drivers/iommu/io-pgtable-arm-selftests.c @@ -6,7 +6,8 @@ * * Author: Will Deacon */ -#include +#include +#include #include #include #include @@ -48,13 +49,14 @@ static void arm_lpae_dump_ops(struct io_pgtable_ops *op= s) cfg->pgsize_bitmap, cfg->ias, cfg->oas); } =20 -#define __FAIL(ops, i) ({ \ - WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ - arm_lpae_dump_ops(ops); \ - -EFAULT; \ +#define __FAIL(test, ops, i) ({ \ + KUNIT_FAIL(test, ""); \ + kunit_err(test, "selftest: test failed for fmt idx %d\n", (i)); \ + arm_lpae_dump_ops(ops); \ + -EFAULT; \ }) =20 -static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg) +static int arm_lpae_run_tests(struct kunit *test, struct io_pgtable_cfg *c= fg) { static const enum io_pgtable_fmt fmts[] =3D { ARM_64_LPAE_S1, @@ -70,7 +72,7 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg) cfg_cookie =3D cfg; ops =3D alloc_io_pgtable_ops(fmts[i], cfg, cfg); if (!ops) { - pr_err("selftest: failed to allocate io pgtable ops\n"); + kunit_err(test, "selftest: failed to allocate io pgtable ops\n"); return -ENOMEM; } =20 @@ -79,13 +81,13 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cf= g) * Empty page tables shouldn't provide any translations. */ if (ops->iova_to_phys(ops, 42)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 if (ops->iova_to_phys(ops, SZ_1G + 42)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 if (ops->iova_to_phys(ops, SZ_2G + 42)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 /* * Distinct mappings of different granule sizes. @@ -98,16 +100,16 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *c= fg) IOMMU_READ | IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_CACHE, GFP_KERNEL, &mapped)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 /* Overlapping mappings */ if (!ops->map_pages(ops, iova, iova + size, size, 1, IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL, &mapped)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 if (ops->iova_to_phys(ops, iova + 42) !=3D (iova + 42)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 iova +=3D SZ_1G; } @@ -118,18 +120,18 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *= cfg) size =3D 1UL << j; =20 if (ops->unmap_pages(ops, iova, size, 1, NULL) !=3D size) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 if (ops->iova_to_phys(ops, iova + 42)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 /* Remap full block */ if (ops->map_pages(ops, iova, iova, size, 1, IOMMU_WRITE, GFP_KERNEL, &mapped)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 if (ops->iova_to_phys(ops, iova + 42) !=3D (iova + 42)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 iova +=3D SZ_1G; } @@ -145,11 +147,11 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *= cfg) IOMMU_READ | IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_CACHE, GFP_KERNEL, &mapped)) - return __FAIL(ops, i); + return __FAIL(test, ops, i); if (mapped !=3D size) - return __FAIL(ops, i); + return __FAIL(test, ops, i); if (ops->unmap_pages(ops, iova, size, 1, NULL) !=3D size) - return __FAIL(ops, i); + return __FAIL(test, ops, i); =20 free_io_pgtable_ops(ops); } @@ -157,7 +159,7 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cf= g) return 0; } =20 -static int arm_lpae_do_selftests(void) +static void arm_lpae_do_selftests(struct kunit *test) { static const unsigned long pgsize[] =3D { SZ_4K | SZ_2M | SZ_1G, @@ -170,18 +172,19 @@ static int arm_lpae_do_selftests(void) }; =20 int i, j, k, pass =3D 0, fail =3D 0; - struct faux_device *dev; + struct device *dev; struct io_pgtable_cfg cfg =3D { .tlb =3D &dummy_tlb_ops, .coherent_walk =3D true, .quirks =3D IO_PGTABLE_QUIRK_NO_WARN, }; =20 - dev =3D faux_device_create("io-pgtable-test", NULL, 0); - if (!dev) - return -ENOMEM; + dev =3D kunit_device_register(test, "io-pgtable-test"); + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev); + if (IS_ERR_OR_NULL(dev)) + return; =20 - cfg.iommu_dev =3D &dev->dev; + cfg.iommu_dev =3D dev; =20 for (i =3D 0; i < ARRAY_SIZE(pgsize); ++i) { for (j =3D 0; j < ARRAY_SIZE(address_size); ++j) { @@ -190,9 +193,9 @@ static int arm_lpae_do_selftests(void) cfg.pgsize_bitmap =3D pgsize[i]; cfg.ias =3D address_size[k]; cfg.oas =3D address_size[j]; - pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u OAS %u\n", - pgsize[i], cfg.ias, cfg.oas); - if (arm_lpae_run_tests(&cfg)) + kunit_info(test, "selftest: pgsize_bitmap 0x%08lx, IAS %u OAS %u\n", + pgsize[i], cfg.ias, cfg.oas); + if (arm_lpae_run_tests(test, &cfg)) fail++; else pass++; @@ -200,17 +203,20 @@ static int arm_lpae_do_selftests(void) } } =20 - pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); - faux_device_destroy(dev); - - return fail ? -EFAULT : 0; + kunit_info(test, "selftest: completed with %d PASS %d FAIL\n", pass, fail= ); } =20 -static void arm_lpae_exit_selftests(void) -{ -} +static struct kunit_case io_pgtable_arm_test_cases[] =3D { + KUNIT_CASE(arm_lpae_do_selftests), + {}, +}; + +static struct kunit_suite io_pgtable_arm_test =3D { + .name =3D "io-pgtable-arm-test", + .test_cases =3D io_pgtable_arm_test_cases, +}; + +kunit_test_suite(io_pgtable_arm_test); =20 -subsys_initcall(arm_lpae_do_selftests); -module_exit(arm_lpae_exit_selftests); MODULE_DESCRIPTION("io-pgtable-arm library selftest"); MODULE_LICENSE("GPL"); --=20 2.51.0.534.gc79095c0ca-goog