From nobody Thu Oct 2 09:19:14 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.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 1D7533126A7 for ; Fri, 19 Sep 2025 13:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758288810; cv=none; b=gU3sakKUcGnDQL2qwvwZiBDB1uD+rljhEgvuJr7iw04Ry2Bj4ia991GsG1UJmjrONSULOYXli4APyR5wPUk8p+lWhyZc6PdUPtkhTN2D9oK1YENB1INXPT89n3huBrIml9ineT61rU3A9cVlR/AMf2SX27j30YMr1QjxphX4zXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758288810; c=relaxed/simple; bh=TyW+eoxhdQX3K1kZzdswB4GMFpfu8E/GUU5PJAgxrb0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i+e3lIgoLUhZGowa4tkGAw3isWnPBtZY6CQriGIZ2KAW4gWcRlLsGcJlYlP7Fjw5Rbq3d3Cbze2yyqu6QHugsM1d3H9trdSUyPM4ZohmPISjH1AJ1wV5+XpcbL3uEU+0pm5ZtJWzfdn15Xs8R+t+/tJeJsyzZGXU/9DVp4mrxfk= 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=AbQywyRX; arc=none smtp.client-ip=209.85.221.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="AbQywyRX" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3eae869cb3fso1532265f8f.0 for ; Fri, 19 Sep 2025 06:33:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758288806; x=1758893606; 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=DP/Bm+5e3mSkOukBBhCRTkrnvruqWsZSDU04xkfK8rg=; b=AbQywyRXmWloyWT8nXFcs+QqICkFIjSUcHQ8gZYkvEjvkIj9mHEh7YNsvXEomKVai9 rLGN68yeqxsR3csl3Hut1I9cm/rLA3Xcdocfy73wVJLI3pEmqFRmqOmkKCeWroTbzC4K JKgCt1usP+iyxqAdsr2Yvs8Fci3mm/nQff2rGHNVtA07ZxatC4ylNOiHBsQDn85kEVid lZEwm6Sebt1kgx/Lt5nn7WhwY8HyX3u4cbjou4fc0x5Gs+d3Izr9w/XQrB6y+wFTTcWa tQbH0wSFGGoDDfbi3NyLSQebtWnaYX6lZqj7WbJipH0A3JLuoCuiKKYXboatCxyKCnZq L2Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758288806; x=1758893606; 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=DP/Bm+5e3mSkOukBBhCRTkrnvruqWsZSDU04xkfK8rg=; b=bwBnSgTEh2YOEiAMi09qrBk7nZUyRY59Pqn8nFJ0fG8tzYsRsVxs0lJX7LQxCdGR0y Tox6i2e/CkHhaMrE+kJH4IjuduRUIs39ZTwDVwiBhQO7ryFLPCDqGhLg5+I/S5CnyJ7N Pm2w0DNu4yba3pdQuDlbp6B/SnleWcMakICLgRwzZ09Zdg/CqjO1pW2sGgWyhJOdtk1L zKvcMLuGVc5m6NojkqZlddnsZef/yT/GlZluNWAc6oaymawqVAOk/kgSWp9dtjpLFXI/ SRgHny1kog2K0umEUj3Gs7gbqYn5yPPWQPCoGqqLJkYihJH7mqEhaEaQv7LrofB0wgCx fXBg== X-Forwarded-Encrypted: i=1; AJvYcCWKlTd4JFl/ennG28EHpk9GanSfePL+jHWa8eGFbms4T+QpXon14qkE0HHmUzAo6MuTeEu5PS1BwRTVTcM=@vger.kernel.org X-Gm-Message-State: AOJu0Ywan8HanGQi2woIk6+g+H0+L5Yu8Ebq7Gc8vaJ9bQa51j9UnEZa dDrih5Df6Atu6DAhPNFNreG0PF4fCLCC1bPdbxliLzofuyntKr6ciJGXvRhJa4snU+Qm1LDuH/6 UlF9yd7gFeyJ0vg== X-Google-Smtp-Source: AGHT+IGfd9H6PFBnWGC9u0o/DJ1VDVi6YMq0gft0vsd6mTOo7SapURN04FyPK7q0T/gUAS8fpfu5CNP10jZbcQ== X-Received: from wrbgv25.prod.google.com ([2002:a05:6000:4619:b0:3ec:df9b:56a4]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:400c:b0:3ea:9042:e682 with SMTP id ffacd0b85a97d-3ee7c55430amr2660786f8f.11.1758288806436; Fri, 19 Sep 2025 06:33:26 -0700 (PDT) Date: Fri, 19 Sep 2025 13:33:13 +0000 In-Reply-To: <20250919133316.2741279-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: <20250919133316.2741279-1-smostafa@google.com> X-Mailer: git-send-email 2.51.0.534.gc79095c0ca-goog Message-ID: <20250919133316.2741279-5-smostafa@google.com> Subject: [PATCH v3 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 --- drivers/iommu/Kconfig | 10 +-- drivers/iommu/Makefile | 2 +- drivers/iommu/io-pgtable-arm-selftests.c | 82 +++++++++++++----------- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 553522ef3ca9..f7e80c3c1f2a 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -40,12 +40,14 @@ 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 +config IOMMU_IO_PGTABLE_LPAE_KUNIT_TEST tristate "LPAE selftests" - depends on IOMMU_IO_PGTABLE_LPAE + select IOMMU_IO_PGTABLE_LPAE + depends on 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 4c3dee4ba895..9d9acb91d823 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 __init arm_lpae_dump_ops(struct io_pgtable_= ops *ops) 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