[RFC PATCH 1/5] iommu/virtio-iommu: Correct the values of granule and nr_pages

Tina Zhang
2023-11-06T07:12:22Z

The value of granule is ilog2(pgsize). When the value of pgsize isn't
a power of two, granule would make pgsize less than the actual size of
pgsize. E.g., if pgsize = 0x6000 and granule = ilog2(gather->pgsize), then
granule = 0xe. 2^0xe = 0x4000 makes the pgsize (0x4000) smaller than the
actual pgsize (0x6000). Invalidating IOTLB with smaller range would lead
to cache incoherence. So, roundup pgsize value to the nearest power of 2
to make sure the granule won't make pgsize less than the actual size. The
value of "gather->end - gather->start + 1" also needs similar
adjustment.

Signed-off-by: Tina Zhang <tina.zhang at intel.com>
---
 drivers/iommu/virtio-iommu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
index 08e310672e57..b1ceaac974e2 100644
--- a/drivers/iommu/virtio-iommu.c
+++ b/drivers/iommu/virtio-iommu.c
@@ -1289,8 +1289,8 @@ static void viommu_iotlb_sync(struct iommu_domain *domain,
 		if (!gather->pgsize)
 			return;

-		granule = ilog2(gather->pgsize);
-		nr_pages = (gather->end - gather->start + 1) >> granule;
+		granule = ilog2(__roundup_pow_of_two(gather->pgsize));
+		nr_pages = __roundup_pow_of_two(gather->end - gather->start + 1)
>> granule;
 		req = (struct virtio_iommu_req_invalidate) {
 			.head.type	= VIRTIO_IOMMU_T_INVALIDATE,
 			.inv_gran	= cpu_to_le16(VIRTIO_IOMMU_INVAL_G_VA),
--
2.39.3