From nobody Thu Oct 2 05:03:34 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 E4B8B283FE4 for ; Mon, 22 Sep 2025 09:00:07 +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=1758531609; cv=none; b=JgKZACRM3aCeReXcb4Bn2J1eeXaN1/c9kBemhan4hS3Y94rVHegSaR30qMvd+lUj9FBSEk5r8yZoDvnBIj0HWj5mhOLXFIyxtlAYjImszZrUad6//NbV33LHR6yYUA6W6IBOTrnTSZV7VqeLBizfaImBtAER3+wQtUwBqwTpspQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758531609; c=relaxed/simple; bh=IkvFohIT4Wev3CJlOkrieSRHtDFSjhkdcyilBtxGyoQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZuB/va9OM5+Iomo9y7fndOhBYBSEDm0bVpeUCyRate5Wb+BTiyzv3RPAXqLAvStiswVz6ZKXhh7hPcpKfbnnmb7BaZXBWyzf/k1blJBtkwmNDyxx8N8i0BcuXO5RQQA6K+vX8VtwPGXIvo9cusCAYMbKeJw3OQqqcuGod84Kydc= 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=T5efvkB3; 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="T5efvkB3" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-45b98de0e34so36653375e9.0 for ; Mon, 22 Sep 2025 02:00:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758531606; x=1759136406; 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=dta3w9x0Kzr2/YC3aAI2cLcS4FTvsApb1bOklp+8AkE=; b=T5efvkB3e9XLqlhyb/XKnM6NZNWA3wIxaVT9ze3TZdGJ7ILCHqILOIHrIG0qkbmumU XpII4vofk5P+EDKr7HewHbuBhiBTSq6NO4vW6q+DA7es/4N4q5Jwytkk53j+i5ELFHwI A+C8T/nKQMBZbDM+ZgeNXQl0KQTfj2J07JIKhuV/kK03qKapnvfRpQvVy7C6RKPZNQFl BUASWw7e4XvkCo5tF6Ng7vA+MdT/0erG8DMsXsMc9d2Y4kfI4Jud232p+/dEjAlb/nGs kPivSeYbZ6da/qr+0R/mYCoQjbsRvi73H8PpR0zV3NOw55U4SAFc5GacS0k5dNHvb0cC k1tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758531606; x=1759136406; 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=dta3w9x0Kzr2/YC3aAI2cLcS4FTvsApb1bOklp+8AkE=; b=F/IP5SzUwt9CycWYsoBZymn76HI9D5ZZBbnOSu5pMNFAkUOF02vKqelNv4BVt6QRHk Zh/DEn1ZEl25gZyJGa5SDE3C7DthohBv+S2BFsuInhvJabYkQ7BTm2uMhDQY7Fh7Zow1 q5lCNTa4jmNY3zuUKdgqxS79wcZgfdzGGtKUYZNzDiv1mSNAl7KMe9V4JaW9rYVS3RaP Qx4BiROvFYO5kaS2XgGkKzJMIOu8cf9lXy4zfk36SmmLCP3IJB11NCgwt99VT+W5MyeR 57UxNDgObVNv5gTJ90yGbqYMmy9Pf6QGMbkznqCAs4eHZGkseGcClMv0+v37XOw4iHGu fNYg== X-Forwarded-Encrypted: i=1; AJvYcCUMA3SUO+lV/5fTLIhAjql+Y47M68Bief6XdYmUYxTwhro0FtvjHJkbIg4fNtGgVzYVCV6XoPcIm4NV1Jw=@vger.kernel.org X-Gm-Message-State: AOJu0YzCIKfEMxF16G9Mbyal6fEGgRHX3ZgChI+MaM6d8WONPbtJ6Nim ckfsSNUhOtBkgza3lvl5M3KMA/RbuKAcvWRwCGioCOj0gr4w8UzZsUvCcmkbuYcg/RGQKvxhnSx SIh8BTMskPzMing== X-Google-Smtp-Source: AGHT+IEqtivvAp1LSR1yfY3OPo8H0NhHHFJcQpxkNow9ciOTTfMvMKV71pcO7sOl6lYxK9wepfDZCsUMSsF6Iw== X-Received: from wmbep17.prod.google.com ([2002:a05:600c:8411:b0:45d:d179:882f]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fc6:b0:46e:1aa8:44a3 with SMTP id 5b1f17b1804b1-46e1aa845c9mr2814975e9.37.1758531606327; Mon, 22 Sep 2025 02:00:06 -0700 (PDT) Date: Mon, 22 Sep 2025 08:59:25 +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-2-smostafa@google.com> Subject: [PATCH v4 1/4] iommu/io-pgtable-arm: Simplify error prints for selftests 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" At the moment, if the self test fails it prints a lot of information about the page table (size, levels...) this requires access to many internals, which has to be exposed in the next patch moving the tests out. Instead, we can simplify the print, using ias, oas, pgsize_bitmap and fmt is enough to identify the failed case, and the rest can be deduced from the code. Signed-off-by: Mostafa Saleh Reviewed-by: Jason Gunthorpe Reviewed-by: Pranjal Shrivastava --- drivers/iommu/io-pgtable-arm.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 7e8e2216c294..00218af5d5f7 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -1299,14 +1299,10 @@ static const struct iommu_flush_ops dummy_tlb_ops _= _initconst =3D { =20 static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) { - struct arm_lpae_io_pgtable *data =3D io_pgtable_ops_to_data(ops); - struct io_pgtable_cfg *cfg =3D &data->iop.cfg; + struct io_pgtable_cfg *cfg =3D &io_pgtable_ops_to_pgtable(ops)->cfg; =20 - pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", - cfg->pgsize_bitmap, cfg->ias); - pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, = pgd @ %p\n", - ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), - ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); + pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit oas %u-bit\n", + cfg->pgsize_bitmap, cfg->ias, cfg->oas); } =20 #define __FAIL(ops, i) ({ \ --=20 2.51.0.534.gc79095c0ca-goog From nobody Thu Oct 2 05:03:34 2025 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) (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 F375E284892 for ; Mon, 22 Sep 2025 09:00:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758531611; cv=none; b=XyW2wLVXFuLYOBaXGxp2Ql8JAScF9yRuWXOxNQhVCk5VVtmqTOLMA2PUjSdEoSsaxMU/wfUJL5Evc8Qqmn9OOAp4uHvNoVwxMa5FyHM6c/pa0htVoj1JYT44lupxVzEzmkepJkFzIfKOm1ZNuiBcGdXUcP44kmpNcQcDFU2/sEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758531611; c=relaxed/simple; bh=vYUS3dI9XgT7DB9opuiZ1Byrj34GtobBqgcS9r1zFWc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aTkEFCgO5QTjw0XkN/J1g1p8GzdGPSRQjwkG6tQ7/Qt03NEUchI4+447cmvNLFeG4u2W4mm1offfQRwRTybIrZ1AISkC4ApY9igV1svrSuZi8OILNDwm6G9FFZPhUxiFSTMSk/AZW/0PmBK5bcLC7Xdu5VqbFmgwtkX4QXsGn6M= 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=vHfvLbRP; arc=none smtp.client-ip=209.85.208.74 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="vHfvLbRP" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-63267bb00cfso537195a12.2 for ; Mon, 22 Sep 2025 02:00:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758531607; x=1759136407; 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=gDG1lzvpcDhJhbEFYfIq80tGXsnz1MaPskEEqdr6vp0=; b=vHfvLbRPnR2KFCZ2nCkQZng0L1gDXdcBGGMJEB0Db0jhSCqK0dId2KcSO9tniwQjfa Utz1/oD6fo5sD5VGLzea7aF0ixsALWJUNYtKjTbxiQV/PJ9KURGuMDt1UdCAwKbJKA9x pPeLAVf9z2iDsr7sfwp3Vtw76fdw+COFp7cTFICEAG52dsE1EQxjKpbEZqIKAxY5YJ3h Km7mCGmmLDvpl3mBgNrvxhQsfghnWF5iOKIgfkF0o1YolGsmkbm+XTia05NA6ywZAebe TIJBU3QPUPtly3bI2TAEOeI0jNCMVxktNoIjpU/bmdvw6UvWsGi4FZtcdhbnp9FwedsR t8AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758531607; x=1759136407; 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=gDG1lzvpcDhJhbEFYfIq80tGXsnz1MaPskEEqdr6vp0=; b=q2KYnuwZnvEB7J3BhkmlzLIFuUpElbty8d2JuLGTK9rakhv8pHEotXNxMORuE9KM1K Z8pZaObJ4bDsM4x7z44tb3FXn7JF4bB4IXPL96QYjVt7S1Bl0P5O7yMJVZHnfP93TUJm AlmCBxfxANk6BCqjaAOarPfYWLNuHmJRFkzYB+En540/+8rk70n9FxuBckfErlUzfBLj PiAc5cE3UgKRE0/1BcU6BK2QwvRtcm026t738XRTbwFUOqfNieJ+8VKUGMWD19f0CQm8 VzQkxEm3gNT7knPSvNeEkjQqamlSlfbsm1r6f2fyjdsMqTWLDiykG59iRdsrjR3PhVT2 9Udw== X-Forwarded-Encrypted: i=1; AJvYcCWoMG1TXEN4tO5PfeRSgpZk0IESzgVulXfS194pVMFKZLnBnZ5Bw1hoiJYxiIsCw/JD9ocf8J83EAXVbu4=@vger.kernel.org X-Gm-Message-State: AOJu0YyLbDH4w6/UfiOR+8pnQAuCMFnatIJP99AeFRUT7arWvYuwt1TL 3EuxInmbeRL4lznjEW2M1R9ZJWFIUjBxvPgFEvGez+3S7pMpU7T9pxI3DGvCkyNqZnqCYyGRDIx D19EMmQ1qHmoHjw== X-Google-Smtp-Source: AGHT+IFiMhubF9mPGZW4Iq/ZLmvAvFqkuB7YZjfWlyHTX+KKNWX2mhXCrsmmBTOg3JTSkNqtfG8Ggrp1jMj0CA== X-Received: from edi4.prod.google.com ([2002:a05:6402:3044:b0:632:b93e:8a58]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:4308:b0:62f:4dbc:6785 with SMTP id 4fb4d7f45d1cf-62fc0960641mr10635774a12.19.1758531607318; Mon, 22 Sep 2025 02:00:07 -0700 (PDT) Date: Mon, 22 Sep 2025 08:59:26 +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-3-smostafa@google.com> Subject: [PATCH v4 2/4] iommu/io-pgtable-arm: Move selftests to a separate file 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Clean up the io-pgtable-arm library by moving the selftests out. Next the tests will be registered with kunit. This is useful also to factor out kernel specific code out, so it can compiled as part of the hypervisor object. Signed-off-by: Mostafa Saleh Reviewed-by: Jason Gunthorpe Reviewed-by: Pranjal Shrivastava --- drivers/iommu/Makefile | 1 + drivers/iommu/io-pgtable-arm-selftests.c | 208 +++++++++++++++++++++++ drivers/iommu/io-pgtable-arm.c | 200 ---------------------- 3 files changed, 209 insertions(+), 200 deletions(-) create mode 100644 drivers/iommu/io-pgtable-arm-selftests.c diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile index 355294fa9033..5250a2eea13f 100644 --- a/drivers/iommu/Makefile +++ b/drivers/iommu/Makefile @@ -12,6 +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_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 new file mode 100644 index 000000000000..17f48216647c --- /dev/null +++ b/drivers/iommu/io-pgtable-arm-selftests.c @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * CPU-agnostic ARM page table allocator. + * + * Copyright (C) 2014 ARM Limited + * + * Author: Will Deacon + */ +#include +#include +#include +#include + +#include "io-pgtable-arm.h" + +static struct io_pgtable_cfg *cfg_cookie __initdata; + +static void __init dummy_tlb_flush_all(void *cookie) +{ + WARN_ON(cookie !=3D cfg_cookie); +} + +static void __init dummy_tlb_flush(unsigned long iova, size_t size, + size_t granule, void *cookie) +{ + WARN_ON(cookie !=3D cfg_cookie); + WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); +} + +static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, + unsigned long iova, size_t granule, + void *cookie) +{ + dummy_tlb_flush(iova, granule, granule, cookie); +} + +static const struct iommu_flush_ops dummy_tlb_ops __initconst =3D { + .tlb_flush_all =3D dummy_tlb_flush_all, + .tlb_flush_walk =3D dummy_tlb_flush, + .tlb_add_page =3D dummy_tlb_add_page, +}; + +static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) +{ + struct io_pgtable_cfg *cfg =3D &io_pgtable_ops_to_pgtable(ops)->cfg; + + pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit oas %u-bit\n", + cfg->pgsize_bitmap, cfg->ias, cfg->oas); +} + +#define __FAIL(ops, i) ({ \ + WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ + arm_lpae_dump_ops(ops); \ + -EFAULT; \ +}) + +static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) +{ + static const enum io_pgtable_fmt fmts[] __initconst =3D { + ARM_64_LPAE_S1, + ARM_64_LPAE_S2, + }; + + int i, j; + unsigned long iova; + size_t size, mapped; + struct io_pgtable_ops *ops; + + for (i =3D 0; i < ARRAY_SIZE(fmts); ++i) { + 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"); + return -ENOMEM; + } + + /* + * Initial sanity checks. + * Empty page tables shouldn't provide any translations. + */ + if (ops->iova_to_phys(ops, 42)) + return __FAIL(ops, i); + + if (ops->iova_to_phys(ops, SZ_1G + 42)) + return __FAIL(ops, i); + + if (ops->iova_to_phys(ops, SZ_2G + 42)) + return __FAIL(ops, i); + + /* + * Distinct mappings of different granule sizes. + */ + iova =3D 0; + for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { + size =3D 1UL << j; + + if (ops->map_pages(ops, iova, iova, size, 1, + IOMMU_READ | IOMMU_WRITE | + IOMMU_NOEXEC | IOMMU_CACHE, + GFP_KERNEL, &mapped)) + return __FAIL(ops, i); + + /* Overlapping mappings */ + if (!ops->map_pages(ops, iova, iova + size, size, 1, + IOMMU_READ | IOMMU_NOEXEC, + GFP_KERNEL, &mapped)) + return __FAIL(ops, i); + + if (ops->iova_to_phys(ops, iova + 42) !=3D (iova + 42)) + return __FAIL(ops, i); + + iova +=3D SZ_1G; + } + + /* Full unmap */ + iova =3D 0; + for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { + size =3D 1UL << j; + + if (ops->unmap_pages(ops, iova, size, 1, NULL) !=3D size) + return __FAIL(ops, i); + + if (ops->iova_to_phys(ops, iova + 42)) + return __FAIL(ops, i); + + /* Remap full block */ + if (ops->map_pages(ops, iova, iova, size, 1, + IOMMU_WRITE, GFP_KERNEL, &mapped)) + return __FAIL(ops, i); + + if (ops->iova_to_phys(ops, iova + 42) !=3D (iova + 42)) + return __FAIL(ops, i); + + iova +=3D SZ_1G; + } + + /* + * Map/unmap the last largest supported page of the IAS, this can + * trigger corner cases in the concatednated page tables. + */ + mapped =3D 0; + size =3D 1UL << __fls(cfg->pgsize_bitmap); + iova =3D (1UL << cfg->ias) - size; + if (ops->map_pages(ops, iova, iova, size, 1, + IOMMU_READ | IOMMU_WRITE | + IOMMU_NOEXEC | IOMMU_CACHE, + GFP_KERNEL, &mapped)) + return __FAIL(ops, i); + if (mapped !=3D size) + return __FAIL(ops, i); + if (ops->unmap_pages(ops, iova, size, 1, NULL) !=3D size) + return __FAIL(ops, i); + + free_io_pgtable_ops(ops); + } + + return 0; +} + +static int __init arm_lpae_do_selftests(void) +{ + static const unsigned long pgsize[] __initconst =3D { + SZ_4K | SZ_2M | SZ_1G, + SZ_16K | SZ_32M, + SZ_64K | SZ_512M, + }; + + static const unsigned int address_size[] __initconst =3D { + 32, 36, 40, 42, 44, 48, + }; + + int i, j, k, pass =3D 0, fail =3D 0; + struct faux_device *dev; + struct io_pgtable_cfg cfg =3D { + .tlb =3D &dummy_tlb_ops, + .coherent_walk =3D true, + .quirks =3D IO_PGTABLE_QUIRK_NO_WARN, + }; + + dev =3D faux_device_create("io-pgtable-test", NULL, 0); + if (!dev) + return -ENOMEM; + + cfg.iommu_dev =3D &dev->dev; + + for (i =3D 0; i < ARRAY_SIZE(pgsize); ++i) { + for (j =3D 0; j < ARRAY_SIZE(address_size); ++j) { + /* Don't use ias > oas as it is not valid for stage-2. */ + for (k =3D 0; k <=3D j; ++k) { + 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)) + fail++; + else + pass++; + } + } + } + + pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); + faux_device_destroy(dev); + + return fail ? -EFAULT : 0; +} +subsys_initcall(arm_lpae_do_selftests); diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c index 00218af5d5f7..aed18b3d277f 100644 --- a/drivers/iommu/io-pgtable-arm.c +++ b/drivers/iommu/io-pgtable-arm.c @@ -12,10 +12,7 @@ #include #include #include -#include -#include #include -#include #include #include =20 @@ -1267,200 +1264,3 @@ struct io_pgtable_init_fns io_pgtable_arm_mali_lpae= _init_fns =3D { .alloc =3D arm_mali_lpae_alloc_pgtable, .free =3D arm_lpae_free_pgtable, }; - -#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST - -static struct io_pgtable_cfg *cfg_cookie __initdata; - -static void __init dummy_tlb_flush_all(void *cookie) -{ - WARN_ON(cookie !=3D cfg_cookie); -} - -static void __init dummy_tlb_flush(unsigned long iova, size_t size, - size_t granule, void *cookie) -{ - WARN_ON(cookie !=3D cfg_cookie); - WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); -} - -static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, - unsigned long iova, size_t granule, - void *cookie) -{ - dummy_tlb_flush(iova, granule, granule, cookie); -} - -static const struct iommu_flush_ops dummy_tlb_ops __initconst =3D { - .tlb_flush_all =3D dummy_tlb_flush_all, - .tlb_flush_walk =3D dummy_tlb_flush, - .tlb_add_page =3D dummy_tlb_add_page, -}; - -static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) -{ - struct io_pgtable_cfg *cfg =3D &io_pgtable_ops_to_pgtable(ops)->cfg; - - pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit oas %u-bit\n", - cfg->pgsize_bitmap, cfg->ias, cfg->oas); -} - -#define __FAIL(ops, i) ({ \ - WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ - arm_lpae_dump_ops(ops); \ - -EFAULT; \ -}) - -static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) -{ - static const enum io_pgtable_fmt fmts[] __initconst =3D { - ARM_64_LPAE_S1, - ARM_64_LPAE_S2, - }; - - int i, j; - unsigned long iova; - size_t size, mapped; - struct io_pgtable_ops *ops; - - for (i =3D 0; i < ARRAY_SIZE(fmts); ++i) { - 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"); - return -ENOMEM; - } - - /* - * Initial sanity checks. - * Empty page tables shouldn't provide any translations. - */ - if (ops->iova_to_phys(ops, 42)) - return __FAIL(ops, i); - - if (ops->iova_to_phys(ops, SZ_1G + 42)) - return __FAIL(ops, i); - - if (ops->iova_to_phys(ops, SZ_2G + 42)) - return __FAIL(ops, i); - - /* - * Distinct mappings of different granule sizes. - */ - iova =3D 0; - for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { - size =3D 1UL << j; - - if (ops->map_pages(ops, iova, iova, size, 1, - IOMMU_READ | IOMMU_WRITE | - IOMMU_NOEXEC | IOMMU_CACHE, - GFP_KERNEL, &mapped)) - return __FAIL(ops, i); - - /* Overlapping mappings */ - if (!ops->map_pages(ops, iova, iova + size, size, 1, - IOMMU_READ | IOMMU_NOEXEC, - GFP_KERNEL, &mapped)) - return __FAIL(ops, i); - - if (ops->iova_to_phys(ops, iova + 42) !=3D (iova + 42)) - return __FAIL(ops, i); - - iova +=3D SZ_1G; - } - - /* Full unmap */ - iova =3D 0; - for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { - size =3D 1UL << j; - - if (ops->unmap_pages(ops, iova, size, 1, NULL) !=3D size) - return __FAIL(ops, i); - - if (ops->iova_to_phys(ops, iova + 42)) - return __FAIL(ops, i); - - /* Remap full block */ - if (ops->map_pages(ops, iova, iova, size, 1, - IOMMU_WRITE, GFP_KERNEL, &mapped)) - return __FAIL(ops, i); - - if (ops->iova_to_phys(ops, iova + 42) !=3D (iova + 42)) - return __FAIL(ops, i); - - iova +=3D SZ_1G; - } - - /* - * Map/unmap the last largest supported page of the IAS, this can - * trigger corner cases in the concatednated page tables. - */ - mapped =3D 0; - size =3D 1UL << __fls(cfg->pgsize_bitmap); - iova =3D (1UL << cfg->ias) - size; - if (ops->map_pages(ops, iova, iova, size, 1, - IOMMU_READ | IOMMU_WRITE | - IOMMU_NOEXEC | IOMMU_CACHE, - GFP_KERNEL, &mapped)) - return __FAIL(ops, i); - if (mapped !=3D size) - return __FAIL(ops, i); - if (ops->unmap_pages(ops, iova, size, 1, NULL) !=3D size) - return __FAIL(ops, i); - - free_io_pgtable_ops(ops); - } - - return 0; -} - -static int __init arm_lpae_do_selftests(void) -{ - static const unsigned long pgsize[] __initconst =3D { - SZ_4K | SZ_2M | SZ_1G, - SZ_16K | SZ_32M, - SZ_64K | SZ_512M, - }; - - static const unsigned int address_size[] __initconst =3D { - 32, 36, 40, 42, 44, 48, - }; - - int i, j, k, pass =3D 0, fail =3D 0; - struct faux_device *dev; - struct io_pgtable_cfg cfg =3D { - .tlb =3D &dummy_tlb_ops, - .coherent_walk =3D true, - .quirks =3D IO_PGTABLE_QUIRK_NO_WARN, - }; - - dev =3D faux_device_create("io-pgtable-test", NULL, 0); - if (!dev) - return -ENOMEM; - - cfg.iommu_dev =3D &dev->dev; - - for (i =3D 0; i < ARRAY_SIZE(pgsize); ++i) { - for (j =3D 0; j < ARRAY_SIZE(address_size); ++j) { - /* Don't use ias > oas as it is not valid for stage-2. */ - for (k =3D 0; k <=3D j; ++k) { - 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)) - fail++; - else - pass++; - } - } - } - - pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); - faux_device_destroy(dev); - - return fail ? -EFAULT : 0; -} -subsys_initcall(arm_lpae_do_selftests); -#endif --=20 2.51.0.534.gc79095c0ca-goog From nobody Thu Oct 2 05:03:34 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 1583E286893 for ; Mon, 22 Sep 2025 09:00:09 +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=1758531611; cv=none; b=A1Z+lTAVgvD+StduTfnFcmaxy4LvJTgZeIoHdug+nQNYnrdG+nCXELNgy56ymmiYRXLxlBZvSdxTOQvLlLC1O2RoyBdqdwu8YoqmuIfJlcYiT95vJpnBC+rTd0fVmaSMQJLFWMggImk6VZLhJn1L3w4QNWbgYsojOW3S2hELD4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758531611; c=relaxed/simple; bh=EcV5Ud4O7dddqV6D+cceP3QOprSQAHvyeSV8revOdpQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dQBlHx4cLucTcVpytDfPolS4tCzWw6QdCXrljEXz5vXWpBlSTVxm/h7EOuM6OMwB7ILBav/wBP+xwaPAbaMaty/j8JltVhMtYeW/vKfYKfw/9v3PfQSms8MJbyfqUFX1UykhuzidBH4B72iZiq7kDP4ayDOy+q0Pzbw8QAJTULk= 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=e5+lHPAH; 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="e5+lHPAH" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-46b15e6f227so5218645e9.1 for ; Mon, 22 Sep 2025 02:00:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758531608; x=1759136408; 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=fxsHa5a/TnumwuS8wGt++Dj39uNGV0RG0Oz4S0LjiTY=; b=e5+lHPAHWwzQUcvPJUTVszNIPXnz1WzdlCrN9ONVepReVEdD3qbwfpfaFFzOU1SH2e zVCgKDXY6R4jtK8IObfkNM00hmAaMjS8i3itKEG7eTfETCGcVoM8hUxxFvBPvD+7srYL XnaW/uQNQ/GODkPYHxOUqEsXYVKXyyR7fK/YD6wM8bB/F/RjxGM9//aUscWSihBq0MQj Mm8BvNJ7vJZ8/56l5O9o73re9l5CgOSp9DwYtaJITEJaSyXrWrOWsK6vG5uiL/oGZAsE izGOTUxRGJpYYLsnQyXyvzTyAwihnp6QQVh6fk7qvpF+fniesLd4DpFaU9nQfCr+Ez+Z SDgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758531608; x=1759136408; 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=fxsHa5a/TnumwuS8wGt++Dj39uNGV0RG0Oz4S0LjiTY=; b=ra1fqGotkwmnpoBmBjZIIAjMZxuLY/8CDyjnyo9NY1AJ5vE7aAvEk29hAeE4W5Qjw5 jnl/xnuObQppPAouWRFaIhj7ecwztcQsCv6SGVyLW+nwh8ok5DbdK5X9EsA9UHn5jGeX 4xR4vdkXUJBT4cGifoxxufKs8wrCqxEogfKFdF1exLbSeDRnhehtrdHvMcduaxiGfiOS wlgLS7bA+4mmJZP8bd97WUw6H1Dxy5I3DYwvbGzCKOQcfa1dnasEprrWnF6xsAMIcr11 bkQ1OYgvlVi/xgrq9B3tp4ofOa/qOkFo33HKPDpuFlsrW33uZXuxJK5aUHQYdi5PKW1W K73A== X-Forwarded-Encrypted: i=1; AJvYcCX/khMGnb8cdZNilOnaKn0Cw/ThNsEc+9D+4YF0AVIW3Nl7bac7/suZ7brEQnNaM1fTqzjsIRQF5sSdSl0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/7hwepfnxKxNpaQkAZceb9Af+17+/jXOWhPZr52EGAxoq3UDU n1bf5zD1D9/CDIIkxO0rJxpyO7rKMokIturNMcQgj8sMsj+IDusl9virZcCaN90qeo8RcGnMPV6 AsQw5Z5/PeIbyJg== X-Google-Smtp-Source: AGHT+IHget1O4BPqdwANeAahSQkK78lrpcEF2/nHFiBWfVpUCmbTfdZneqa7756vBxWSeKVk9F/QEVfsPjcQ4A== X-Received: from wmcn18-n1.prod.google.com ([2002:a05:600c:c0d2:10b0:45b:7262:8426]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5491:b0:45d:d5df:ab39 with SMTP id 5b1f17b1804b1-467f2cc83afmr101921075e9.26.1758531608493; Mon, 22 Sep 2025 02:00:08 -0700 (PDT) Date: Mon, 22 Sep 2025 08:59:27 +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-4-smostafa@google.com> Subject: [PATCH v4 3/4] iommu/io-pgtable-arm-selftests: Modularize the test 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" Remove the __init constraint, as the test will be converted to KUnit, it can run on-demand after later. Also, as KUnit can be a module, make this test modular. Reviewed-by: Jason Gunthorpe Signed-off-by: Mostafa Saleh Reviewed-by: Pranjal Shrivastava --- drivers/iommu/Kconfig | 2 +- drivers/iommu/io-pgtable-arm-selftests.c | 36 +++++++++++++++--------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 70d29b14d851..553522ef3ca9 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -41,7 +41,7 @@ config IOMMU_IO_PGTABLE_LPAE up to 48-bits in size. =20 config IOMMU_IO_PGTABLE_LPAE_SELFTEST - bool "LPAE selftests" + tristate "LPAE selftests" depends on IOMMU_IO_PGTABLE_LPAE help Enable self-tests for LPAE page table allocator. This performs diff --git a/drivers/iommu/io-pgtable-arm-selftests.c b/drivers/iommu/io-pg= table-arm-selftests.c index 17f48216647c..50350e88d9b4 100644 --- a/drivers/iommu/io-pgtable-arm-selftests.c +++ b/drivers/iommu/io-pgtable-arm-selftests.c @@ -13,34 +13,34 @@ =20 #include "io-pgtable-arm.h" =20 -static struct io_pgtable_cfg *cfg_cookie __initdata; +static struct io_pgtable_cfg *cfg_cookie; =20 -static void __init dummy_tlb_flush_all(void *cookie) +static void dummy_tlb_flush_all(void *cookie) { WARN_ON(cookie !=3D cfg_cookie); } =20 -static void __init dummy_tlb_flush(unsigned long iova, size_t size, - size_t granule, void *cookie) +static void dummy_tlb_flush(unsigned long iova, size_t size, + size_t granule, void *cookie) { WARN_ON(cookie !=3D cfg_cookie); WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); } =20 -static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, - unsigned long iova, size_t granule, - void *cookie) +static void dummy_tlb_add_page(struct iommu_iotlb_gather *gather, + unsigned long iova, size_t granule, + void *cookie) { dummy_tlb_flush(iova, granule, granule, cookie); } =20 -static const struct iommu_flush_ops dummy_tlb_ops __initconst =3D { +static const struct iommu_flush_ops dummy_tlb_ops =3D { .tlb_flush_all =3D dummy_tlb_flush_all, .tlb_flush_walk =3D dummy_tlb_flush, .tlb_add_page =3D dummy_tlb_add_page, }; =20 -static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) +static void arm_lpae_dump_ops(struct io_pgtable_ops *ops) { struct io_pgtable_cfg *cfg =3D &io_pgtable_ops_to_pgtable(ops)->cfg; =20 @@ -54,9 +54,9 @@ static void __init arm_lpae_dump_ops(struct io_pgtable_op= s *ops) -EFAULT; \ }) =20 -static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) +static int arm_lpae_run_tests(struct io_pgtable_cfg *cfg) { - static const enum io_pgtable_fmt fmts[] __initconst =3D { + static const enum io_pgtable_fmt fmts[] =3D { ARM_64_LPAE_S1, ARM_64_LPAE_S2, }; @@ -157,15 +157,15 @@ static int __init arm_lpae_run_tests(struct io_pgtabl= e_cfg *cfg) return 0; } =20 -static int __init arm_lpae_do_selftests(void) +static int arm_lpae_do_selftests(void) { - static const unsigned long pgsize[] __initconst =3D { + static const unsigned long pgsize[] =3D { SZ_4K | SZ_2M | SZ_1G, SZ_16K | SZ_32M, SZ_64K | SZ_512M, }; =20 - static const unsigned int address_size[] __initconst =3D { + static const unsigned int address_size[] =3D { 32, 36, 40, 42, 44, 48, }; =20 @@ -205,4 +205,12 @@ static int __init arm_lpae_do_selftests(void) =20 return fail ? -EFAULT : 0; } + +static void arm_lpae_exit_selftests(void) +{ +} + 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 From nobody Thu Oct 2 05:03:34 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