mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: samarth.colleges@gmail.com
To: Jason Gunthorpe <jgg@nvidia.com>, Joerg Roedel <joro@8bytes.org>,
	Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	Samarth Narang <samarth.colleges@gmail.com>
Subject: [PATCH] iommupt: report the whole contiguous group in read_and_clear_dirty
Date: Tue, 13 Oct 2026 09:58:14 -0700	[thread overview]
Message-ID: <20261013165814.3092559-1-samarth.colleges@gmail.com> (raw)

From: Samarth Narang <samarth.colleges@gmail.com>

The record_dirty() function computes the reported dirty length of a contiguous
(multi-item) leaf group as:

	end_index = log2_set_mod_max_t(unsigned int, pts->index, num_contig_lg2);
	end_index = min(end_index, pts->end_index);
	dirty_len = (end_index - index) * log2_to_int(pt_table_item_lg2sz(pts));

Here, log2_set_mod_max_t() sets the low num_contig_lg2 bits of the index to all
ones, i.e. it returns the *inclusive* last index of the contiguous group.
However, pts->end_index is an *exclusive* bound, this can be seen from the
definition of pt_range_to_end_index() which returns of the form "index + 1".
Also, the iterator stops on index >= end_index. Therefore, subtracting an
inclusive end from an exclusive bound will yield one less item than the actual
length of the contiguous group.

For IOMMU dirty tracking used by VFIO/iommufd live migration this silently
drops the last page of a contiguous dirty mapping from the migration
bitmap.

This patch makes end_index exclusive to match pts->end_index and the span cleared by
pt_entry_make_write_clean(). Also, the same length is passed to
iommu_iotlb_gather_add_range() so this fix would correct that as well.

Fixes: 4a00f9434891 ("iommupt: Add read_and_clear_dirty op")
Signed-off-by: Samarth Narang <samarth.colleges@gmail.com>
---
 drivers/iommu/generic_pt/iommu_pt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iommu/generic_pt/iommu_pt.h b/drivers/iommu/generic_pt/iommu_pt.h
--- a/drivers/iommu/generic_pt/iommu_pt.h
+++ b/drivers/iommu/generic_pt/iommu_pt.h
@@ -248,7 +248,7 @@ static void record_dirty(struct pt_state *pts,
 	if (num_contig_lg2 != ilog2(1)) {
 		unsigned int index = pts->index;
 		unsigned int end_index = log2_set_mod_max_t(
-			unsigned int, pts->index, num_contig_lg2);
+			unsigned int, pts->index, num_contig_lg2) + 1;
 
 		/* Adjust for being contained inside a contiguous page */
 		end_index = min(end_index, pts->end_index);


                 reply	other threads:[~2026-10-13 17:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261013165814.3092559-1-samarth.colleges@gmail.com \
    --to=samarth.colleges@gmail.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®