All of lore.kernel.org
 help / color / mirror / Atom feed
From: YeeLi <seven.yi.lee@gmail.com>
To: joro@8bytes.org, will@kernel.org
Cc: robin.murphy@arm.com, jgg@ziepe.ca, iommu@lists.linux.dev
Subject: [PATCH 2/2] iommupt: add a KUnit test for unmapping an empty range
Date: Thu,  5 Nov 2026 20:12:27 +0800	[thread overview]
Message-ID: <20261105121227.622631-3-seven.yi.lee@gmail.com> (raw)
In-Reply-To: <20261105121227.622631-1-seven.yi.lee@gmail.com>

From: yeeli <seven.yi.lee@gmail.com>

Unmapping a range that holds no mappings must not record anything in
the IOTLB gather. Otherwise iova + len - 1 underflows and the flush
covers the whole domain, or widens a range that was already gathered.

Check that the gather stays empty for a hole at IOVA 0 and at a
non-zero IOVA. Without the previous fix the first case turns the gather
into [0, ULONG_MAX] and the second one flips it to [iova, iova - 1].

Signed-off-by: yeeli <seven.yi.lee@gmail.com>
---
 drivers/iommu/generic_pt/kunit_iommu_pt.h | 31 +++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/iommu/generic_pt/kunit_iommu_pt.h b/drivers/iommu/generic_pt/kunit_iommu_pt.h
index 9fc71db74234..59a9386f1474 100644
--- a/drivers/iommu/generic_pt/kunit_iommu_pt.h
+++ b/drivers/iommu/generic_pt/kunit_iommu_pt.h
@@ -427,6 +427,36 @@ static void test_mixed(struct kunit *test)
 	check_iova(test, start, oa, len);
 }
 
+/*
+ * Unmapping a range that holds no mappings must not record anything in
+ * the IOTLB gather. Otherwise iova + len - 1 underflows and the flush
+ * covers the whole domain, or widens a range that was already gathered.
+ */
+static void test_unmap_hole(struct kunit *test)
+{
+	struct kunit_iommu_priv *priv = test->priv;
+	struct iommu_iotlb_gather gather;
+
+	/* A hole at IOVA 0 must not turn the gather into [0, ULONG_MAX]. */
+	iommu_iotlb_gather_init(&gather);
+	KUNIT_ASSERT_EQ(
+		test,
+		iommu_unmap_fast(&priv->domain, 0, priv->smallest_pgsz, &gather),
+		0);
+	KUNIT_EXPECT_EQ(test, gather.start, ULONG_MAX);
+	KUNIT_EXPECT_EQ(test, gather.end, 0);
+
+	/* Neither may a hole at a non-zero IOVA widen the gather. */
+	iommu_iotlb_gather_init(&gather);
+	KUNIT_ASSERT_EQ(
+		test,
+		iommu_unmap_fast(&priv->domain, priv->smallest_pgsz,
+				 priv->smallest_pgsz, &gather),
+		0);
+	KUNIT_EXPECT_EQ(test, gather.start, ULONG_MAX);
+	KUNIT_EXPECT_EQ(test, gather.end, 0);
+}
+
 static struct kunit_case iommu_test_cases[] = {
 	KUNIT_CASE_FMT(test_increase_level),
 	KUNIT_CASE_FMT(test_map_simple),
@@ -435,6 +465,7 @@ static struct kunit_case iommu_test_cases[] = {
 	KUNIT_CASE_FMT(test_random_map),
 	KUNIT_CASE_FMT(test_pgsize_boundary),
 	KUNIT_CASE_FMT(test_mixed),
+	KUNIT_CASE_FMT(test_unmap_hole),
 	{},
 };
 
-- 
2.34.1


      parent reply	other threads:[~2026-11-05 12:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 12:12 [PATCH 0/2] iommupt: fix zero-length unmap causing a full IOTLB flush YeeLi
2026-11-05 12:12 ` [PATCH 1/2] iommupt: fix full IOTLB flush from a zero length unmap YeeLi
2026-11-09  6:59   ` Baolu Lu
2026-11-05 12:12 ` YeeLi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261105121227.622631-3-seven.yi.lee@gmail.com \
    --to=seven.yi.lee@gmail.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=robin.murphy@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.