Skip to contentSkip to conversation

iommu/vt-d: Fix aligned pages in calculate_psi_aligned_address()

PATCH In review Lu Baolu 2 participants 3 messages Jul 8, 2024 – Jul 9, 2024

Subsystems (2)
lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

PATCH 1/1 iommu/vt-d: Fix aligned pages in calculate_psi_aligned_address()

The helper calculate_psi_aligned_address() is used to convert an arbitrary range into a size-aligned one.

The aligned_pages variable is calculated from input start and end, but is not adjusted when the start pfn is not aligned and the mask is adjusted, which results in an incorrect number of pages returned.

The number of pages is used by qi_flush_piotlb() to flush caches for the first-stage translation. With the wrong number of pages, the cache is not synchronized, leading to inconsistencies in some cases.

Fixes c4d27ffaa8eb ("iommu/vt-d: Add cache tag invalidation helpers")
---
 drivers/iommu/intel/cache.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/intel/cache.c +1 -0
index e8418cdd8331..113834742107 100644
@@ -246,6 +246,7 @@ static unsigned long calculate_psi_aligned_address(unsigned long start,
 		 */
 		shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
 		mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG;
+		aligned_pages = 1UL << mask;
 	}
 
 	*_pages = aligned_pages;
Signature · 1 line
2.34.1
author maintainer
#1 lore raw
Quoted 22 lines · Lu Baolu

The helper calculate_psi_aligned_address() is used to convert an arbitrary range into a size-aligned one.

The aligned_pages variable is calculated from input start and end, but is not adjusted when the start pfn is not aligned and the mask is adjusted, which results in an incorrect number of pages returned.

The number of pages is used by qi_flush_piotlb() to flush caches for the first-stage translation. With the wrong number of pages, the cache is not synchronized, leading to inconsistencies in some cases.

Fixes c4d27ffaa8eb ("iommu/vt-d: Add cache tag invalidation helpers")
---
  drivers/iommu/intel/cache.c | 1 +
  1 file changed, 1 insertion(+)
drivers/iommu/intel/cache.c +0 -0
index e8418cdd8331..113834742107 100644

drivers/iommu/intel/cache.c static unsigned long calculate_psi_aligned_address(unsigned long start, · line 249

Quoted 4 lines
drivers/iommu/intel/cache.c +1 -0
@@ -246,6 +246,7 @@ static unsigned long calculate_psi_aligned_address(unsigned long start,
  		 */
  		shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
  		mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG;
+		aligned_pages = 1UL << mask;

Hmm, it appears that if mask is equal to BITS_PER_LONG (which is typically 64), the left shift operation will overflow.

So perhaps we need another line of change:

drivers/iommu/intel/cache.c +1 -1
index 113834742107..44e92638c0cd 100644
@@ -245,7 +245,7 @@ static unsigned long calculate_psi_aligned_address(unsigned long start,
                 * shared_bits are all equal in both pfn and end_pfn.
                 */
                shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
-               mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG;
+               mask = shared_bits ? __ffs(shared_bits) : MAX_AGAW_PFN_WIDTH;
                aligned_pages = 1UL << mask;
        }
 

I will make above another fix as it already causes overflow in another path.

Kevin, sound good to you?

Thanks,
baolu

Reviewed-by
#2 lore raw
Quoted 52 lines

From: Baolu Lu <[email protected]>
Sent: Tuesday, July 9, 2024 10:54 AM

drivers/iommu/intel/cache.c static unsigned long

Quoted 22 lines · Lu Baolu

The helper calculate_psi_aligned_address() is used to convert an arbitrary range into a size-aligned one.

The aligned_pages variable is calculated from input start and end, but is not adjusted when the start pfn is not aligned and the mask is adjusted, which results in an incorrect number of pages returned.

The number of pages is used by qi_flush_piotlb() to flush caches for the first-stage translation. With the wrong number of pages, the cache is not synchronized, leading to inconsistencies in some cases.

Fixes c4d27ffaa8eb ("iommu/vt-d: Add cache tag invalidation helpers")
---
  drivers/iommu/intel/cache.c | 1 +
  1 file changed, 1 insertion(+)
drivers/iommu/intel/cache.c +0 -0
index e8418cdd8331..113834742107 100644
@@ -246,6 +246,7 @@ static unsigned long

calculate_psi_aligned_address(unsigned long start,

drivers/iommu/intel/cache.c static unsigned long calculate_psi_aligned_address(unsigned long start,

Quoted 4 lines
drivers/iommu/intel/cache.c +1 -0
  		 */
  		shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
  		mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG;
+		aligned_pages = 1UL << mask;

Hmm, it appears that if mask is equal to BITS_PER_LONG (which is typically 64), the left shift operation will overflow.

So perhaps we need another line of change:

drivers/iommu/intel/cache.c +1 -1
index 113834742107..44e92638c0cd 100644
@@ -245,7 +245,7 @@ static unsigned long
 calculate_psi_aligned_address(unsigned long start,
                  * shared_bits are all equal in both pfn and end_pfn.
                  */
                 shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
-               mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG;
+               mask = shared_bits ? __ffs(shared_bits) :
 MAX_AGAW_PFN_WIDTH;
                 aligned_pages = 1UL << mask;
         }
Quoted 5 lines

I will make above another fix as it already causes overflow in another path.

Kevin, sound good to you?

yes. for both: