Skip to contentSkip to conversation

x86/pv: Clean up cr3 handling in arch_set_info_guest()

PATCH In review Andrew Cooper 5 participants 7 messages Dec 21, 2018 – Jan 24, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/pv: Clean up cr3 handling in arch_set_info_guest()

All of this code lives inside CONFIG_PV which means gfn == mfn, and the fill_ro_mpt() calls clearly show that the value is used untranslated.

Change cr3_gfn to a suitably typed cr3_mfn, and replace get_page_from_gfn() with a straight mfn_to_page/get_page sequence, to avoid the implication that translation is going on.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Julien: This should simplify your "xen: Switch parameter in get_page_from_gfn to use typesafe gfn" patch. In particular, I did a doubletake at fill_ro_mpt(_mfn(gfn_x(cr3_gfn))); when reviewing it.

---
 xen/arch/x86/domain.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
xen/arch/x86/domain.c +11 -11
index 32dc4253..da94ab4 100644
@@ -827,8 +827,8 @@ int arch_set_info_guest(
     unsigned long flags;
     bool compat;
 #ifdef CONFIG_PV
-    unsigned long cr3_gfn;
-    struct page_info *cr3_page;
+    mfn_t cr3_mfn;
+    struct page_info *cr3_page = NULL;
     unsigned long cr4;
     int rc = 0;
 #endif
@@ -1091,12 +1091,12 @@ int arch_set_info_guest(
     set_bit(_VPF_in_reset, &v->pause_flags);
 
     if ( !compat )
-        cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[3]);
+        cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[3]));
     else
-        cr3_gfn = compat_cr3_to_pfn(c.cmp->ctrlreg[3]);
-    cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+        cr3_mfn = _mfn(compat_cr3_to_pfn(c.cmp->ctrlreg[3]));
 
-    if ( !cr3_page )
+    if ( !mfn_valid(cr3_mfn) ||
+         !(cr3_page = mfn_to_page(cr3_mfn), get_page(cr3_page, d)) )
         rc = -EINVAL;
     else if ( paging_mode_refcounts(d) )
         /* nothing */;
@@ -1122,7 +1122,7 @@ int arch_set_info_guest(
         case 0:
             if ( !compat && !VM_ASSIST(d, m2p_strict) &&
                  !paging_mode_refcounts(d) )
-                fill_ro_mpt(_mfn(cr3_gfn));
+                fill_ro_mpt(cr3_mfn);
             break;
         default:
             if ( cr3_page == current->arch.old_guest_table )
@@ -1137,10 +1137,10 @@ int arch_set_info_guest(
         v->arch.guest_table = pagetable_from_page(cr3_page);
         if ( c.nat->ctrlreg[1] )
         {
-            cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[1]);
-            cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+            cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[1]));
 
-            if ( !cr3_page )
+            if ( !mfn_valid(cr3_mfn) ||
+                 !(cr3_page = mfn_to_page(cr3_mfn), get_page(cr3_page, d)) )
                 rc = -EINVAL;
             else if ( !paging_mode_refcounts(d) )
             {
@@ -1162,7 +1162,7 @@ int arch_set_info_guest(
                     break;
                 case 0:
                     if ( VM_ASSIST(d, m2p_strict) )
-                        zap_ro_mpt(_mfn(cr3_gfn));
+                        zap_ro_mpt(cr3_mfn);
                     break;
                 }
             }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 43 lines · Andrew Cooper

All of this code lives inside CONFIG_PV which means gfn == mfn, and the fill_ro_mpt() calls clearly show that the value is used untranslated.

Change cr3_gfn to a suitably typed cr3_mfn, and replace get_page_from_gfn() with a straight mfn_to_page/get_page sequence, to avoid the implication that translation is going on.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Julien: This should simplify your "xen: Switch parameter in get_page_from_gfn to use typesafe gfn" patch. In particular, I did a doubletake at fill_ro_mpt(_mfn(gfn_x(cr3_gfn))); when reviewing it.

---
 xen/arch/x86/domain.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
xen/arch/x86/domain.c +2 -2
index 32dc4253..da94ab4 100644
@@ -827,8 +827,8 @@ int arch_set_info_guest(
     unsigned long flags;
     bool compat;
 #ifdef CONFIG_PV
-    unsigned long cr3_gfn;
-    struct page_info *cr3_page;
+    mfn_t cr3_mfn;
+    struct page_info *cr3_page = NULL;
     unsigned long cr4;
     int rc = 0;
 #endif
@@ -1091,12 +1091,12 @@ int arch_set_info_guest(
     set_bit(_VPF_in_reset, &v->pause_flags);
 
     if ( !compat )

xen/arch/x86/domain.c int arch_set_info_guest( · line 1099

Quoted 10 lines
xen/arch/x86/domain.c +4 -4
-        cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[3]);
+        cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[3]));
     else
-        cr3_gfn = compat_cr3_to_pfn(c.cmp->ctrlreg[3]);
-    cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+        cr3_mfn = _mfn(compat_cr3_to_pfn(c.cmp->ctrlreg[3]));
 
-    if ( !cr3_page )
+    if ( !mfn_valid(cr3_mfn) ||
+         !(cr3_page = mfn_to_page(cr3_mfn), get_page(cr3_page, d)) )

This is kind of an open-coded version of get_page_from_gfn with just the non-paging bits, IMO I would use get_page_from_gfn, or introduce a get_page_from_mfn helper?

The more that you use the same construct below.

Quoted 13 lines
xen/arch/x86/domain.c +1 -1
         rc = -EINVAL;
     else if ( paging_mode_refcounts(d) )
         /* nothing */;
@@ -1122,7 +1122,7 @@ int arch_set_info_guest(
         case 0:
             if ( !compat && !VM_ASSIST(d, m2p_strict) &&
                  !paging_mode_refcounts(d) )
-                fill_ro_mpt(_mfn(cr3_gfn));
+                fill_ro_mpt(cr3_mfn);
             break;
         default:
             if ( cr3_page == current->arch.old_guest_table )

xen/arch/x86/domain.c int arch_set_info_guest( · line 1140

Quoted 6 lines
xen/arch/x86/domain.c +1 -2
@@ -1137,10 +1137,10 @@ int arch_set_info_guest(
         v->arch.guest_table = pagetable_from_page(cr3_page);
         if ( c.nat->ctrlreg[1] )
         {
-            cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[1]);
-            cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+            cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[1]));

I assume this is something PV specific, but calling xen_cr3_to_pfn on ctrlreg[1] seems wrong at first sight. And the xen_cr3_to_pfn and xen_pfn_to_cr3 helpers seem quite pointless, since it's just PFN_DOWN or pfn_to_paddr.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 12 lines · Roger Pau Monné
Quoted 11 lines · Andrew Cooper
xen/arch/x86/domain.c +2 -3
@@ -1091,12 +1091,12 @@ int arch_set_info_guest(
     set_bit(_VPF_in_reset, &v->pause_flags);
 
     if ( !compat )
-        cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[3]);
+        cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[3]));
     else
-        cr3_gfn = compat_cr3_to_pfn(c.cmp->ctrlreg[3]);
-    cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+        cr3_mfn = _mfn(compat_cr3_to_pfn(c.cmp->ctrlreg[3]));
 
Quoted 6 lines

xen/arch/x86/domain.c int arch_set_info_guest( · line 1099

Quoted 3 lines
xen/arch/x86/domain.c +2 -1
-    if ( !cr3_page )
+    if ( !mfn_valid(cr3_mfn) ||
+         !(cr3_page = mfn_to_page(cr3_mfn), get_page(cr3_page, d)) )

This is kind of an open-coded version of get_page_from_gfn with just the non-paging bits, IMO I would use get_page_from_gfn, or introduce a get_page_from_mfn helper?

Turns out that we already have one of those.  I'll tweak it for purpose and use it.

Quoted 18 lines

The more that you use the same construct below.

Quoted 15 lines
xen/arch/x86/domain.c +1 -1
         rc = -EINVAL;
     else if ( paging_mode_refcounts(d) )
         /* nothing */;
@@ -1122,7 +1122,7 @@ int arch_set_info_guest(
         case 0:
             if ( !compat && !VM_ASSIST(d, m2p_strict) &&
                  !paging_mode_refcounts(d) )
-                fill_ro_mpt(_mfn(cr3_gfn));
+                fill_ro_mpt(cr3_mfn);
             break;
         default:
             if ( cr3_page == current->arch.old_guest_table )
@@ -1137,10 +1137,10 @@ int arch_set_info_guest(
         v->arch.guest_table = pagetable_from_page(cr3_page);
         if ( c.nat->ctrlreg[1] )
Quoted 8 lines

xen/arch/x86/domain.c int arch_set_info_guest( · line 1140

Quoted 4 lines
xen/arch/x86/domain.c +1 -2
         {
-            cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[1]);
-            cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+            cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[1]));

I assume this is something PV specific, but calling xen_cr3_to_pfn on ctrlreg[1] seems wrong at first sight. And the xen_cr3_to_pfn and xen_pfn_to_cr3 helpers seem quite pointless, since it's just PFN_DOWN or pfn_to_paddr.

It is one of the more peculiar pieces of PV magic.

For reasons best explained by whomever wrote the code, the 64bit ABI, having separate kernel and user %cr3's, stashes the user %cr3 in ctrlreg[1].

This code is correct, but I do accept that it is very confusing to read without knowing the PV ABI inside out.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v1 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

All of this code lives inside CONFIG_PV which means gfn == mfn, and the fill_ro_mpt() calls clearly show that the value is used untranslated.

Change cr3_gfn to a suitably typed cr3_mfn, and replace get_page_from_gfn() with a straight mfn_to_page/get_page sequence, to avoid the implication that translation is going on.

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v1 1/1
#4 lore raw
In reply to Andrew Cooper
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 21.12.18 at 14:46, <[email protected]> wrote:

All of this code lives inside CONFIG_PV which means gfn == mfn, and the fill_ro_mpt() calls clearly show that the value is used untranslated.

Change cr3_gfn to a suitably typed cr3_mfn, and replace get_page_from_gfn() with a straight mfn_to_page/get_page sequence, to avoid the implication that translation is going on.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Julien: This should simplify your "xen: Switch parameter in get_page_from_gfn
to use typesafe gfn" patch. In particular, I did a doubletake at fill_ro_mpt(_mfn(gfn_x(cr3_gfn))); when reviewing it.

---
 xen/arch/x86/domain.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
xen/arch/x86/domain.c +2 -2
index 32dc4253..da94ab4 100644
@@ -827,8 +827,8 @@ int arch_set_info_guest(
     unsigned long flags;
     bool compat;
 #ifdef CONFIG_PV
-    unsigned long cr3_gfn;
-    struct page_info *cr3_page;
+    mfn_t cr3_mfn;
+    struct page_info *cr3_page = NULL;
     unsigned long cr4;
     int rc = 0;
 #endif
@@ -1091,12 +1091,12 @@ int arch_set_info_guest(
     set_bit(_VPF_in_reset, &v->pause_flags);
 
     if ( !compat )

xen/arch/x86/domain.c int arch_set_info_guest( · line 1099

Quoted 10 lines
xen/arch/x86/domain.c +4 -4
-        cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[3]);
+        cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[3]));
     else
-        cr3_gfn = compat_cr3_to_pfn(c.cmp->ctrlreg[3]);
-    cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+        cr3_mfn = _mfn(compat_cr3_to_pfn(c.cmp->ctrlreg[3]));
 
-    if ( !cr3_page )
+    if ( !mfn_valid(cr3_mfn) ||
+         !(cr3_page = mfn_to_page(cr3_mfn), get_page(cr3_page, d)) )

I'd prefer if the comma operator was used really only when there's no alternative. Here I think

    if ( !mfn_valid(cr3_mfn) ||
         !get_page(cr3_page = mfn_to_page(cr3_mfn), d) )

is quite a bit easier to read.

xen/arch/x86/domain.c int arch_set_info_guest( · line 1143

Quoted 11 lines
xen/arch/x86/domain.c +3 -3
@@ -1137,10 +1137,10 @@ int arch_set_info_guest(
         v->arch.guest_table = pagetable_from_page(cr3_page);
         if ( c.nat->ctrlreg[1] )
         {
-            cr3_gfn = xen_cr3_to_pfn(c.nat->ctrlreg[1]);
-            cr3_page = get_page_from_gfn(d, cr3_gfn, NULL, P2M_ALLOC);
+            cr3_mfn = _mfn(xen_cr3_to_pfn(c.nat->ctrlreg[1]));
 
-            if ( !cr3_page )
+            if ( !mfn_valid(cr3_mfn) ||
+                 !(cr3_page = mfn_to_page(cr3_mfn), get_page(cr3_page, d)) )

Same here and then

Or wait - in the discussion with Roger you've indicated you'd switch to get_page_from_mfn() anyway, which would eliminate the comma expression altogether, and the need for the newly added initializer as well. The R-b above applies to that adjustment as well.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
In reply to Andrew Cooper

Hi Andrew,

Quoted 17 lines · Andrew Cooper

All of this code lives inside CONFIG_PV which means gfn == mfn, and the fill_ro_mpt() calls clearly show that the value is used untranslated.

Change cr3_gfn to a suitably typed cr3_mfn, and replace get_page_from_gfn() with a straight mfn_to_page/get_page sequence, to avoid the implication that translation is going on.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Quoted 3 lines

Julien: This should simplify your "xen: Switch parameter in get_page_from_gfn to use typesafe gfn" patch. In particular, I did a doubletake at fill_ro_mpt(_mfn(gfn_x(cr3_gfn))); when reviewing it.

I was looking at updating my patch and remembered you suggested to rebase on top of this. Do you have any plan to resend the patch?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw
Quoted 22 lines · Julien Grall

Hi Andrew,

Quoted 19 lines · Andrew Cooper

All of this code lives inside CONFIG_PV which means gfn == mfn, and the fill_ro_mpt() calls clearly show that the value is used untranslated.

Change cr3_gfn to a suitably typed cr3_mfn, and replace get_page_from_gfn()
with a straight mfn_to_page/get_page sequence, to avoid the implication that
translation is going on.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>

Quoted 7 lines
Quoted 4 lines

Julien: This should simplify your "xen: Switch parameter in get_page_from_gfn
to use typesafe gfn" patch.  In particular, I did a doubletake at fill_ro_mpt(_mfn(gfn_x(cr3_gfn))); when reviewing it.

I was looking at updating my patch and remembered you suggested to rebase on top of this. Do you have any plan to resend the patch?

I'll see if I can find some time tomorrow.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel