From nobody Wed Oct 1 23:35:06 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 746F130C11E for ; Mon, 29 Sep 2025 15:50:13 +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=1759161015; cv=none; b=b1vuOM1zzZZ0lnb96xzeBy+j2oSScOM66nWoPG3gGBtEbyYMsLJBxHz5hN3nV8YrqCJIU+C4ov5xMCnBBzxbbnNJycbQplEwQBhles/FxnsbB/+KZ5mxnH/qxaw2EL2SN3VyRt6mDZCXr9nEa8WRTaeDmsZJXltyfRNxyPwHIJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759161015; c=relaxed/simple; bh=e+dO3TdTPUtOJ9w/cxH12JvpOvQM+TDsOm9CNC0cxF8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a9bFeFX0p+qaStOEynzjCayt2mrdbdO7Obq3bdNBNwXerd0QDX1Sq6Jy5Qim7ush87lQ5+TrZx5WdVcMZ+fC/B8vSbprJgXpALsSrDhXv+S1GReIFL4bGagKD+A/kkq3n7HgITJNGA8fQKNmdS8ue9nk1mdTmyjjt0yqGRpPMMA= 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=DU1rq/+z; 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="DU1rq/+z" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-46e4fa584e7so9390415e9.0 for ; Mon, 29 Sep 2025 08:50:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759161012; x=1759765812; 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=BEgYRP9pjGI8MH0ERrMYDYhvE9nlc9YIovovBCSH5M4=; b=DU1rq/+z8d/oARHf4Hx8GqgKe6W6R7TUA/hjtV7WqT0ecmPsVuESzj75aNwAi8jLdt 8nAIxmD6MjZeookQbffaGOjUfeivsevAYek3TgvEe+6aNgVP9ZFzbxvmu27zxPS5gMSb 4wA6hzx74bQeZUsO+kBrkEUThQOCvJQj4Q0v+9Bozcck8E1h++NdSsfxLSgzx2H46d7J eq0pgUIBcdcjhmZrNlx3zoeNF5uaZzP7z4tnawQxmsytNlSiFZtmdfRWOjG9XloPZTx/ MMg86MLuc/Dap4UWO1f8n/voy8k9i+uNCxx2++vkTHWKOnDXJbNiIo+drYRKz4btuRjn jokw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759161012; x=1759765812; 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=BEgYRP9pjGI8MH0ERrMYDYhvE9nlc9YIovovBCSH5M4=; b=DUt/AoCmsG9a49UVN6IJ0kyPhbbKROZ5PIa24x/FYWeENKLlgcGAvUO0JSWUrXPHit VZwxKSVLModQDywDwWJubYoxWSc+MfwrGAuZm1PGNZwUlZ3lmAmrl2pfp/H9QvGO7s28 M8wwo5Uc3WaK0nQvfdc8mPzg9ddteR3TxNO5V/bO0r2D+HwOTaxWu3y6ablOOjpyE0M5 0EHd1y7uyZoughN+VbJ3hdK/N+nuOUjmxknv6fOkQ26vbLpGZOwOJblxEmaE8jxRNCTD DXm2q9JOhx+ysT+EpWO+XTXZaVoQuefpw+NkQzectL56VNOa+/dMtUX+XcJsOt8+YW6L +eqA== X-Forwarded-Encrypted: i=1; AJvYcCXHkKtxXTVr8LY8R8z0i5E1sdpoPVxAcrjTFE6WDu/ta315GQ8hj1T/h6czfiW9UiafBvQgcuIfeIxkEso=@vger.kernel.org X-Gm-Message-State: AOJu0YxHigxKkplnqXE8NmOPrn1PXwTiBTasDuh89kfIWiEZ2g1WeuiX 5ZSKBjljF535kG/ZLpVyjqne9bfAjZMj4KITgdNT/kMI35r60UcozfcWwrBO6bozFMcXaHKA7uq 5IgsQyyldgyn5Ow== X-Google-Smtp-Source: AGHT+IH02TVx87JZNG4/PbnW2pW4B2Qn/p6OdDDTJVwjoPTklfJoNYTo0+sAfcLhrmO/WJXH64U2N03jAr6p1g== X-Received: from wmbep21.prod.google.com ([2002:a05:600c:8415:b0:45d:d881:33a7]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d16:b0:45d:84ca:8a7 with SMTP id 5b1f17b1804b1-46e329bb51cmr138280925e9.14.1759161011598; Mon, 29 Sep 2025 08:50:11 -0700 (PDT) Date: Mon, 29 Sep 2025 15:49:59 +0000 In-Reply-To: <20250929155001.3287719-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: <20250929155001.3287719-1-smostafa@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250929155001.3287719-5-smostafa@google.com> Subject: [PATCH v5 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 Reviewed-by: Pranjal Shrivastava Signed-off-by: Mostafa Saleh --- 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 32c6a5c7af53..b61849a8a685 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 =20 @@ -47,13 +48,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, @@ -69,7 +71,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 @@ -78,13 +80,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. @@ -97,16 +99,16 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cf= g) 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; } @@ -117,18 +119,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; } @@ -144,11 +146,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); } @@ -156,7 +158,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, @@ -169,18 +171,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) { @@ -189,9 +192,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++; @@ -199,17 +202,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.536.g15c5d4f767-goog