]> Gentwo Git Trees - linux/.git/commitdiff
iommu/io-pgtable-arm-selftests: Use KUnit
authorMostafa Saleh <smostafa@google.com>
Mon, 3 Nov 2025 12:33:52 +0000 (12:33 +0000)
committerJoerg Roedel <joerg.roedel@amd.com>
Thu, 13 Nov 2025 15:25:32 +0000 (16:25 +0100)
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=y
  It will automatically run at boot as before.

- Otherwise with CONFIG_IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST=m:
  1) on module load:
     Once the module load the self test will run
     # modprobe io-pgtable-arm-selftests

  2) debugfs
     With CONFIG_KUNIT_DEBUGFS=y 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 --arch arm64 \
       --make_options LLVM=1 --kunitconfig ./kunit/kunitconfig
      [18:01:09] ============= io-pgtable-arm-test (1 subtest) ==============
      [18:01:09] [PASSED] arm_lpae_do_selftests
      [18:01:09] =============== [PASSED] io-pgtable-arm-test ===============
      [18:01:09] ============================================================

Suggested-by: Jason Gunthorpe <jgg@ziepe.ca>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Mostafa Saleh <smostafa@google.com>
Acked-by: Will Deacon <will@kernel.org>
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
drivers/iommu/Kconfig
drivers/iommu/Makefile
drivers/iommu/io-pgtable-arm-selftests.c

index 6eca8b3a17e992a2b43587aa4617601f5ca827bc..acce340c7bc739ab98a7679423cad16edee28e34 100644 (file)
@@ -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.
 
-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.
 
          If unsure, say N here.
 
index b5be5f811e83ba4ff48f30ccc091117e45a46468..8e8843316c4bf811a1eb92b9f326c3d092702d77 100644 (file)
@@ -13,7 +13,7 @@ obj-$(CONFIG_IOMMU_DMA) += dma-iommu.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) += io-pgtable-arm-v7s.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o
-obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST) += io-pgtable-arm-selftests.o
+obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST) += io-pgtable-arm-selftests.o
 obj-$(CONFIG_IOMMU_IO_PGTABLE_DART) += io-pgtable-dart.o
 obj-$(CONFIG_IOMMU_IOVA) += iova.o
 obj-$(CONFIG_OF_IOMMU) += of_iommu.o
index 8361d01b49c515a4fa79d14e5969d0673ced7479..334e70350924072185a14f3dbf2b523bb3a5df26 100644 (file)
@@ -9,7 +9,8 @@
 
 #define pr_fmt(fmt)    "arm-lpae io-pgtable: " fmt
 
-#include <linux/device/faux.h>
+#include <kunit/device.h>
+#include <kunit/test.h>
 #include <linux/io-pgtable.h>
 #include <linux/kernel.h>
 
@@ -42,12 +43,12 @@ static const struct iommu_flush_ops dummy_tlb_ops = {
        .tlb_add_page   = dummy_tlb_add_page,
 };
 
-#define __FAIL(i) ({                                                   \
-               WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
-               -EFAULT;                                                \
+#define __FAIL(test, i) ({                                                     \
+               KUNIT_FAIL(test, "test failed for fmt idx %d\n", (i));          \
+               -EFAULT;                                                        \
 })
 
-static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
+static int arm_lpae_run_tests(struct kunit *test, struct io_pgtable_cfg *cfg)
 {
        static const enum io_pgtable_fmt fmts[] = {
                ARM_64_LPAE_S1,
@@ -63,7 +64,7 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
                cfg_cookie = cfg;
                ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
                if (!ops) {
-                       pr_err("selftest: failed to allocate io pgtable ops\n");
+                       kunit_err(test, "failed to allocate io pgtable ops\n");
                        return -ENOMEM;
                }
 
@@ -72,13 +73,13 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
                 * Empty page tables shouldn't provide any translations.
                 */
                if (ops->iova_to_phys(ops, 42))
-                       return __FAIL(i);
+                       return __FAIL(test, i);
 
                if (ops->iova_to_phys(ops, SZ_1G + 42))
-                       return __FAIL(i);
+                       return __FAIL(test, i);
 
                if (ops->iova_to_phys(ops, SZ_2G + 42))
-                       return __FAIL(i);
+                       return __FAIL(test, i);
 
                /*
                 * Distinct mappings of different granule sizes.
@@ -91,16 +92,16 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
                                           IOMMU_READ | IOMMU_WRITE |
                                           IOMMU_NOEXEC | IOMMU_CACHE,
                                           GFP_KERNEL, &mapped))
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        /* Overlapping mappings */
                        if (!ops->map_pages(ops, iova, iova + size, size, 1,
                                            IOMMU_READ | IOMMU_NOEXEC,
                                            GFP_KERNEL, &mapped))
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        iova += SZ_1G;
                }
@@ -111,18 +112,18 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
                        size = 1UL << j;
 
                        if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        if (ops->iova_to_phys(ops, iova + 42))
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        /* Remap full block */
                        if (ops->map_pages(ops, iova, iova, size, 1,
                                           IOMMU_WRITE, GFP_KERNEL, &mapped))
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
-                               return __FAIL(i);
+                               return __FAIL(test, i);
 
                        iova += SZ_1G;
                }
@@ -138,11 +139,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(i);
+                       return __FAIL(test, i);
                if (mapped != size)
-                       return __FAIL(i);
+                       return __FAIL(test, i);
                if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
-                       return __FAIL(i);
+                       return __FAIL(test, i);
 
                free_io_pgtable_ops(ops);
        }
@@ -150,7 +151,7 @@ static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
        return 0;
 }
 
-static int arm_lpae_do_selftests(void)
+static void arm_lpae_do_selftests(struct kunit *test)
 {
        static const unsigned long pgsize[] = {
                SZ_4K | SZ_2M | SZ_1G,
@@ -163,18 +164,19 @@ static int arm_lpae_do_selftests(void)
        };
 
        int i, j, k, pass = 0, fail = 0;
-       struct faux_device *dev;
+       struct device *dev;
        struct io_pgtable_cfg cfg = {
                .tlb = &dummy_tlb_ops,
                .coherent_walk = true,
                .quirks = IO_PGTABLE_QUIRK_NO_WARN,
        };
 
-       dev = faux_device_create("io-pgtable-test", NULL, 0);
-       if (!dev)
-               return -ENOMEM;
+       dev = kunit_device_register(test, "io-pgtable-test");
+       KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
+       if (IS_ERR_OR_NULL(dev))
+               return;
 
-       cfg.iommu_dev = &dev->dev;
+       cfg.iommu_dev = dev;
 
        for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
                for (j = 0; j < ARRAY_SIZE(address_size); ++j) {
@@ -183,9 +185,9 @@ static int arm_lpae_do_selftests(void)
                                cfg.pgsize_bitmap = pgsize[i];
                                cfg.ias = address_size[k];
                                cfg.oas = 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, "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++;
@@ -193,17 +195,20 @@ static int arm_lpae_do_selftests(void)
                }
        }
 
-       pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
-       faux_device_destroy(dev);
-
-       return fail ? -EFAULT : 0;
+       kunit_info(test, "completed with %d PASS %d FAIL\n", pass, fail);
 }
 
-static void arm_lpae_exit_selftests(void)
-{
-}
+static struct kunit_case io_pgtable_arm_test_cases[] = {
+       KUNIT_CASE(arm_lpae_do_selftests),
+       {},
+};
+
+static struct kunit_suite io_pgtable_arm_test = {
+       .name = "io-pgtable-arm-test",
+       .test_cases = io_pgtable_arm_test_cases,
+};
+
+kunit_test_suite(io_pgtable_arm_test);
 
-subsys_initcall(arm_lpae_do_selftests);
-module_exit(arm_lpae_exit_selftests);
-MODULE_DESCRIPTION("io-pgtable-arm library selftest");
+MODULE_DESCRIPTION("io-pgtable-arm library kunit tests");
 MODULE_LICENSE("GPL");