Skip to contentSkip to conversation

iommu/vtd: fix address translation for superpages

PATCH Superseded Roger Pau Monne 2 participants 8 messages May 9, 2023 – May 11, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/vtd: fix address translation for superpages

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

Fix this by re-using the value of pte_maddr which is obtained using dma_pte_addr(). Such change requires adjusting the previous error path to zero pte_maddr.

Fixes c71e55501a61 ('VT-d: have callers specify the target level for page table walks')
---
 xen/drivers/passthrough/vtd/iommu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +4 -2
index 130a159cde07..819e996e6269 100644
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);
                 if ( !target )
Signature · 1 line
2.40.0
#1 lore raw
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 12 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 10 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
 
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Note also the following code:

                if ( !target )
                    break;
            }

            pte_maddr = level - 1;

IOW the local variable is overwritten right away unless target == 0.

Jan

#2 lore raw
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 13 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
Quoted 14 lines

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 9 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Hm, but the code in intel_iommu_lookup_page() is now wrong as it takes the bits in DMA_PTE_CONTIG_MASK as part of the physical address when doing the conversion to mfn? maddr_to_mfn() doesn't perform a any masking to remove the bits above PADDR_BITS.

Thanks, Roger.

#3 lore raw
Quoted 37 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 13 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
Quoted 14 lines

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 9 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Quoted 4 lines

Hm, but the code in intel_iommu_lookup_page() is now wrong as it takes the bits in DMA_PTE_CONTIG_MASK as part of the physical address when doing the conversion to mfn? maddr_to_mfn() doesn't perform a any masking to remove the bits above PADDR_BITS.

Oh, right. But that's a missing dma_pte_addr() in intel_iommu_lookup_page() then. (It would likely be better anyway to switch "uint64_t val" to "struct dma_pte pte" there, to make more visible that it's a PTE we're dealing with.) I indeed overlooked this aspect when doing the earlier change.

Jan

#4 lore raw
Quoted 43 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 13 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
Quoted 14 lines

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 9 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Quoted 4 lines

Hm, but the code in intel_iommu_lookup_page() is now wrong as it takes the bits in DMA_PTE_CONTIG_MASK as part of the physical address when doing the conversion to mfn? maddr_to_mfn() doesn't perform a any masking to remove the bits above PADDR_BITS.

Quoted 5 lines

Oh, right. But that's a missing dma_pte_addr() in intel_iommu_lookup_page() then. (It would likely be better anyway to switch "uint64_t val" to "struct dma_pte pte" there, to make more visible that it's a PTE we're dealing with.) I indeed overlooked this aspect when doing the earlier change.

I guess I'm still confused, as the other return value for target == 0 (when the address is not part of a superpage) does return dma_pte_addr(pte). I think that needs further fixing then.

Thanks, Roger.

#5 lore raw
Quoted 50 lines · Roger Pau Monné
Quoted 43 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 13 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
Quoted 14 lines

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 9 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Quoted 4 lines

Hm, but the code in intel_iommu_lookup_page() is now wrong as it takes the bits in DMA_PTE_CONTIG_MASK as part of the physical address when doing the conversion to mfn? maddr_to_mfn() doesn't perform a any masking to remove the bits above PADDR_BITS.

Quoted 5 lines

Oh, right. But that's a missing dma_pte_addr() in intel_iommu_lookup_page() then. (It would likely be better anyway to switch "uint64_t val" to "struct dma_pte pte" there, to make more visible that it's a PTE we're dealing with.) I indeed overlooked this aspect when doing the earlier change.

Quoted 3 lines

I guess I'm still confused, as the other return value for target == 0 (when the address is not part of a superpage) does return dma_pte_addr(pte). I think that needs further fixing then.

Hmm, indeed. But I think it's worse than this: addr_to_dma_page_maddr() also does one too many iterations in that case. All "normal" callers supply a positive "target". We need to terminate the walk at level 1 also when target == 0.

Jan

#6 lore raw
Quoted 51 lines · Jan Beulich
Quoted 50 lines · Roger Pau Monné
Quoted 48 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 13 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
Quoted 14 lines

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 9 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Quoted 4 lines

Hm, but the code in intel_iommu_lookup_page() is now wrong as it takes the bits in DMA_PTE_CONTIG_MASK as part of the physical address when doing the conversion to mfn? maddr_to_mfn() doesn't perform a any masking to remove the bits above PADDR_BITS.

Oh, right. But that's a missing dma_pte_addr() in intel_iommu_lookup_page() then. (It would likely be better anyway to switch "uint64_t val" to "struct dma_pte pte" there, to make more visible that it's a PTE we're dealing with.) I indeed overlooked this aspect when doing the earlier change.

Quoted 8 lines
Quoted 3 lines

I guess I'm still confused, as the other return value for target == 0 (when the address is not part of a superpage) does return dma_pte_addr(pte). I think that needs further fixing then.

Hmm, indeed. But I think it's worse than this: addr_to_dma_page_maddr() also does one too many iterations in that case. All "normal" callers supply a positive "target". We need to terminate the walk at level 1 also when target == 0.

Don't we do that already due to the following check:

if ( --level == target )

    break;

Which prevents mapping the PTE address as a page table directory?

Thanks, Roger.

#7 lore raw
Quoted 61 lines · Roger Pau Monné
Quoted 51 lines · Jan Beulich
Quoted 50 lines · Roger Pau Monné
Quoted 48 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne

When translating an address that falls inside of a superpage in the IOMMU page tables the fetching of the PTE physical address field wasn't using dma_pte_addr(), which caused the returned data to be corrupt as it would contain bits not related to the address field.

I'm afraid I don't understand:

Quoted 13 lines
xen/drivers/passthrough/vtd/iommu.c +3 -1
@@ -359,16 +359,18 @@ static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr,
 
             if ( !alloc )
             {
-                pte_maddr = 0;
                 if ( !dma_pte_present(*pte) )
+                {
+                    pte_maddr = 0;
                     break;
+                }
 
Quoted 14 lines

xen/drivers/passthrough/vtd/iommu.c static uint64_t addr_to_dma_page_maddr(struct domain *domain, daddr_t addr, · line 375

Quoted 9 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
                 /*
                  * When the leaf entry was requested, pass back the full PTE,
                  * with the address adjusted to account for the residual of
                  * the walk.
                  */
-                pte_maddr = pte->val +
+                pte_maddr +=
                     (addr & ((1UL << level_to_offset_bits(level)) - 1) &
                      PAGE_MASK);

With this change you're now violating what the comment says (plus what the comment ahead of the function says). And it says what it says for a reason - see intel_iommu_lookup_page(), which I think your change is breaking.

Quoted 4 lines

Hm, but the code in intel_iommu_lookup_page() is now wrong as it takes the bits in DMA_PTE_CONTIG_MASK as part of the physical address when doing the conversion to mfn? maddr_to_mfn() doesn't perform a any masking to remove the bits above PADDR_BITS.

Oh, right. But that's a missing dma_pte_addr() in intel_iommu_lookup_page() then. (It would likely be better anyway to switch "uint64_t val" to "struct dma_pte pte" there, to make more visible that it's a PTE we're dealing with.) I indeed overlooked this aspect when doing the earlier change.

Quoted 8 lines
Quoted 3 lines

I guess I'm still confused, as the other return value for target == 0 (when the address is not part of a superpage) does return dma_pte_addr(pte). I think that needs further fixing then.

Hmm, indeed. But I think it's worse than this: addr_to_dma_page_maddr() also does one too many iterations in that case. All "normal" callers supply a positive "target". We need to terminate the walk at level 1 also when target == 0.

Quoted 6 lines

Don't we do that already due to the following check:

if ( --level == target )

    break;

Which prevents mapping the PTE address as a page table directory?

I don't think this is enough - this code, afaict right now, is only sufficient when target >= 1.

Jan