Skip to contentSkip to conversation

x86/iommu: fix wrong iterator type in arch_iommu_hwdom_init()

PATCH In review Roger Pau Monne 2 participants 5 messages May 9, 2023 – May 10, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/iommu: fix wrong iterator type in arch_iommu_hwdom_init()

The 'i' iterator index stores a pdx, not a pfn, and hence the initial assignation of start (which stores a pfn) needs a conversion from pfn to pdx.

Fixes 6b4f6a31ace1 ('x86/PVH: de-duplicate mappings for first Mb of Dom0 memory')
---
 xen/drivers/passthrough/x86/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/x86/iommu.c +1 -1
index cb0788960a08..6bc79e7ec843 100644
@@ -406,7 +406,7 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
      */
     start = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0;
 
-    for ( i = start, count = 0; i < top; )
+    for ( i = pfn_to_pdx(start), count = 0; i < top; )
     {
         unsigned long pfn = pdx_to_pfn(i);
         unsigned int perms = hwdom_iommu_map(d, pfn, max_pfn);
Signature · 1 line
2.40.0
#1 lore raw
Quoted 3 lines · Roger Pau Monne

The 'i' iterator index stores a pdx, not a pfn, and hence the initial assignation of start (which stores a pfn) needs a conversion from pfn to pdx.

Strictly speaking: Yes. But pdx compression skips the bottom MAX_ORDER bits, so ...

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 407

Quoted 5 lines
xen/drivers/passthrough/x86/iommu.c +0 -0
@@ -406,7 +406,7 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
      */
     start = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0;

... with this, ...

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 409

Quoted 2 lines
xen/drivers/passthrough/x86/iommu.c +1 -1
-    for ( i = start, count = 0; i < top; )
+    for ( i = pfn_to_pdx(start), count = 0; i < top; )

... this is an expensive identity transformation. Could I talk you into adding

    ASSERT(start == pfn_to_pdx(start));

instead (or the corresponding BUG_ON() if you'd prefer that, albeit then the expensive identity transformation will still be there even in release builds; not that it matters all that much right here, but still)?

In any event, with no real bug fixed (unless I'm overlooking something), I would suggest to drop the Fixes: tag.

Jan

#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Roger Pau Monne

The 'i' iterator index stores a pdx, not a pfn, and hence the initial assignation of start (which stores a pfn) needs a conversion from pfn to pdx.

Strictly speaking: Yes. But pdx compression skips the bottom MAX_ORDER bits, so ...

Oh, that wasn't obvious to me.

Quoted 11 lines

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 407

Quoted 5 lines
xen/drivers/passthrough/x86/iommu.c +0 -0
@@ -406,7 +406,7 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
      */
     start = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0;

... with this, ...

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 409

Quoted 2 lines
xen/drivers/passthrough/x86/iommu.c +1 -1
-    for ( i = start, count = 0; i < top; )
+    for ( i = pfn_to_pdx(start), count = 0; i < top; )
Quoted 8 lines

... this is an expensive identity transformation. Could I talk you into adding

    ASSERT(start == pfn_to_pdx(start));

instead (or the corresponding BUG_ON() if you'd prefer that, albeit then the expensive identity transformation will still be there even in release builds; not that it matters all that much right here, but still)?

So far the value of start is not influenced by hardware, so having an assert should be fine.

Given that the assignation is just done once at the start of the loop I don't see it being that relevant to the performance of this piece of code TBH, the more that we do a pdx_to_pfn() for each loop iteration, so my preference would be to use the proposed change.

Quoted 2 lines

In any event, with no real bug fixed (unless I'm overlooking something), I would suggest to drop the Fixes: tag.

Right, I could drop that.

Thanks, Roger.

#3 lore raw
Quoted 31 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Roger Pau Monne

The 'i' iterator index stores a pdx, not a pfn, and hence the initial assignation of start (which stores a pfn) needs a conversion from pfn to pdx.

Strictly speaking: Yes. But pdx compression skips the bottom MAX_ORDER bits, so ...

Oh, that wasn't obvious to me.

Quoted 11 lines

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 407

Quoted 5 lines
xen/drivers/passthrough/x86/iommu.c +0 -0
@@ -406,7 +406,7 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
      */
     start = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0;

... with this, ...

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 409

Quoted 2 lines
xen/drivers/passthrough/x86/iommu.c +1 -1
-    for ( i = start, count = 0; i < top; )
+    for ( i = pfn_to_pdx(start), count = 0; i < top; )
Quoted 8 lines

... this is an expensive identity transformation. Could I talk you into adding

    ASSERT(start == pfn_to_pdx(start));

instead (or the corresponding BUG_ON() if you'd prefer that, albeit then the expensive identity transformation will still be there even in release builds; not that it matters all that much right here, but still)?

Quoted 7 lines

So far the value of start is not influenced by hardware, so having an assert should be fine.

Given that the assignation is just done once at the start of the loop I don't see it being that relevant to the performance of this piece of code TBH, the more that we do a pdx_to_pfn() for each loop iteration, so my preference would be to use the proposed change.

Well, okay, but then please with the description also "softened" a little (it isn't really "needs", but e.g. "better would undergo"), alongside ...

Quoted 4 lines
Quoted 2 lines

In any event, with no real bug fixed (unless I'm overlooking something), I would suggest to drop the Fixes: tag.

Right, I could drop that.

... this.

Jan

#4 lore raw
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Roger Pau Monne

The 'i' iterator index stores a pdx, not a pfn, and hence the initial assignation of start (which stores a pfn) needs a conversion from pfn to pdx.

Strictly speaking: Yes. But pdx compression skips the bottom MAX_ORDER bits, so ...

Oh, that wasn't obvious to me.

Quoted 11 lines

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 407

Quoted 5 lines
xen/drivers/passthrough/x86/iommu.c +0 -0
@@ -406,7 +406,7 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
      */
     start = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0;

... with this, ...

xen/drivers/passthrough/x86/iommu.c void __hwdom_init arch_iommu_hwdom_init(struct domain *d) · line 409

Quoted 2 lines
xen/drivers/passthrough/x86/iommu.c +1 -1
-    for ( i = start, count = 0; i < top; )
+    for ( i = pfn_to_pdx(start), count = 0; i < top; )
Quoted 8 lines

... this is an expensive identity transformation. Could I talk you into adding

    ASSERT(start == pfn_to_pdx(start));

instead (or the corresponding BUG_ON() if you'd prefer that, albeit then the expensive identity transformation will still be there even in release builds; not that it matters all that much right here, but still)?

So far the value of start is not influenced by hardware, so having an assert should be fine.

Quoted 7 lines
Quoted 4 lines

Given that the assignation is just done once at the start of the loop I don't see it being that relevant to the performance of this piece of code TBH, the more that we do a pdx_to_pfn() for each loop iteration, so my preference would be to use the proposed change.

Well, okay, but then please with the description also "softened" a little (it isn't really "needs", but e.g. "better would undergo"),

And in the title then perhaps s/fix wrong/adjust/.

Jan

Quoted 11 lines

alongside ...

Quoted 4 lines
Quoted 2 lines

In any event, with no real bug fixed (unless I'm overlooking something), I would suggest to drop the Fixes: tag.

Right, I could drop that.

... this.

Jan