Skip to contentSkip to conversation

x86/pv: Move compat_set_gdt() to be beside do_set_gdt()

PATCH In review Andrew Cooper 2 participants 6 messages Oct 19, 2017 – Oct 23, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 x86/pv: Move compat_set_gdt() to be beside do_set_gdt()

This also makes the do_update_descriptor() pair of functions adjacent.

Purely code motion; no functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/pv/descriptor-tables.c | 69 +++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 33 deletions(-)
xen/arch/x86/pv/descriptor-tables.c +36 -33
index 81973af..9c8de1d 100644
@@ -126,6 +126,42 @@ long do_set_gdt(XEN_GUEST_HANDLE_PARAM(xen_ulong_t) frame_list,
     return ret;
 }
 
+int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list,
+                   unsigned int entries)
+{
+    struct vcpu *curr = current;
+    unsigned int i, nr_pages = (entries + 511) / 512;
+    unsigned long frames[16];
+    int ret;
+
+    /* Rechecked in set_gdt, but ensures a sane limit for copy_from_user(). */
+    if ( entries > FIRST_RESERVED_GDT_ENTRY )
+        return -EINVAL;
+
+    if ( !guest_handle_okay(frame_list, nr_pages) )
+        return -EFAULT;
+
+    for ( i = 0; i < nr_pages; ++i )
+    {
+        unsigned int frame;
+
+        if ( __copy_from_guest(&frame, frame_list, 1) )
+            return -EFAULT;
+
+        frames[i] = frame;
+        guest_handle_add_offset(frame_list, 1);
+    }
+
+    domain_lock(curr->domain);
+
+    if ( (ret = pv_set_gdt(curr, frames, entries)) == 0 )
+        flush_tlb_local();
+
+    domain_unlock(curr->domain);
+
+    return ret;
+}
+
 long do_update_descriptor(uint64_t pa, uint64_t desc)
 {
     struct domain *currd = current->domain;
@@ -181,39 +217,6 @@ long do_update_descriptor(uint64_t pa, uint64_t desc)
     return ret;
 }
 
-int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list, unsigned int entries)
-{
-    unsigned int i, nr_pages = (entries + 511) / 512;
-    unsigned long frames[16];
-    int ret;
-
-    /* Rechecked in set_gdt, but ensures a sane limit for copy_from_user(). */
-    if ( entries > FIRST_RESERVED_GDT_ENTRY )
-        return -EINVAL;
-
-    if ( !guest_handle_okay(frame_list, nr_pages) )
-        return -EFAULT;
-
-    for ( i = 0; i < nr_pages; ++i )
-    {
-        unsigned int frame;
-
-        if ( __copy_from_guest(&frame, frame_list, 1) )
-            return -EFAULT;
-        frames[i] = frame;
-        guest_handle_add_offset(frame_list, 1);
-    }
-
-    domain_lock(current->domain);
-
-    if ( (ret = pv_set_gdt(current, frames, entries)) == 0 )
-        flush_tlb_local();
-
-    domain_unlock(current->domain);
-
-    return ret;
-}
-
 int compat_update_descriptor(uint32_t pa_lo, uint32_t pa_hi,
                              uint32_t desc_lo, uint32_t desc_hi)
 {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 2/3 x86/pv: Use DIV_ROUND_UP() when converting between GDT entries and frames

Also consistently use use nr_frames, rather than mixing nr_pages with a frames[] array.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/domain.c               |  8 +++++---
 xen/arch/x86/pv/descriptor-tables.c | 17 ++++++++---------
 2 files changed, 13 insertions(+), 12 deletions(-)
xen/arch/x86/domain.c +5 -3
index c28ac38..98bfb35 100644
@@ -1017,12 +1017,14 @@ int arch_set_info_guest(
     else
     {
         unsigned long gdt_frames[ARRAY_SIZE(v->arch.pv_vcpu.gdt_frames)];
-        unsigned int n = (c.cmp->gdt_ents + 511) / 512;
+        unsigned int nr_frames = DIV_ROUND_UP(c.cmp->gdt_ents, 512);
 
-        if ( n > ARRAY_SIZE(v->arch.pv_vcpu.gdt_frames) )
+        if ( nr_frames > ARRAY_SIZE(v->arch.pv_vcpu.gdt_frames) )
             return -EINVAL;
-        for ( i = 0; i < n; ++i )
+
+        for ( i = 0; i < nr_frames; ++i )
             gdt_frames[i] = c.cmp->gdt_frames[i];
+
         rc = (int)pv_set_gdt(v, gdt_frames, c.cmp->gdt_ents);
     }
     if ( rc != 0 )
xen/arch/x86/pv/descriptor-tables.c +8 -9
index 9c8de1d..e31c97e 100644
@@ -57,14 +57,13 @@ long pv_set_gdt(struct vcpu *v, unsigned long *frames, unsigned int entries)
 {
     struct domain *d = v->domain;
     l1_pgentry_t *pl1e;
-    /* NB. There are 512 8-byte entries per GDT page. */
-    unsigned int i, nr_pages = (entries + 511) / 512;
+    unsigned int i, nr_frames = DIV_ROUND_UP(entries, 512);
 
     if ( entries > FIRST_RESERVED_GDT_ENTRY )
         return -EINVAL;
 
     /* Check the pages in the new GDT. */
-    for ( i = 0; i < nr_pages; i++ )
+    for ( i = 0; i < nr_frames; i++ )
     {
         struct page_info *page;
 
@@ -85,7 +84,7 @@ long pv_set_gdt(struct vcpu *v, unsigned long *frames, unsigned int entries)
     /* Install the new GDT. */
     v->arch.pv_vcpu.gdt_ents = entries;
     pl1e = pv_gdt_ptes(v);
-    for ( i = 0; i < nr_pages; i++ )
+    for ( i = 0; i < nr_frames; i++ )
     {
         v->arch.pv_vcpu.gdt_frames[i] = frames[i];
         l1e_write(&pl1e[i], l1e_from_pfn(frames[i], __PAGE_HYPERVISOR_RW));
@@ -104,7 +103,7 @@ long pv_set_gdt(struct vcpu *v, unsigned long *frames, unsigned int entries)
 long do_set_gdt(XEN_GUEST_HANDLE_PARAM(xen_ulong_t) frame_list,
                 unsigned int entries)
 {
-    int nr_pages = (entries + 511) / 512;
+    unsigned int nr_frames = DIV_ROUND_UP(entries, 512);
     unsigned long frames[16];
     struct vcpu *curr = current;
     long ret;
@@ -113,7 +112,7 @@ long do_set_gdt(XEN_GUEST_HANDLE_PARAM(xen_ulong_t) frame_list,
     if ( entries > FIRST_RESERVED_GDT_ENTRY )
         return -EINVAL;
 
-    if ( copy_from_guest(frames, frame_list, nr_pages) )
+    if ( copy_from_guest(frames, frame_list, nr_frames) )
         return -EFAULT;
 
     domain_lock(curr->domain);
@@ -130,7 +129,7 @@ int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list,
                    unsigned int entries)
 {
     struct vcpu *curr = current;
-    unsigned int i, nr_pages = (entries + 511) / 512;
+    unsigned int i, nr_frames = DIV_ROUND_UP(entries, 512);
     unsigned long frames[16];
     int ret;
 
@@ -138,10 +137,10 @@ int compat_set_gdt(XEN_GUEST_HANDLE_PARAM(uint) frame_list,
     if ( entries > FIRST_RESERVED_GDT_ENTRY )
         return -EINVAL;
 
-    if ( !guest_handle_okay(frame_list, nr_pages) )
+    if ( !guest_handle_okay(frame_list, nr_frames) )
         return -EFAULT;
 
-    for ( i = 0; i < nr_pages; ++i )
+    for ( i = 0; i < nr_frames; ++i )
     {
         unsigned int frame;
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH for-next 2/3] x86/pv: Use DIV_ROUND_UP() when converting between GDT entries and frames

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 17:47, <[email protected]> wrote:

Also consistently use use nr_frames, rather than mixing nr_pages with a frames[] array.

No functional change.

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

#3 lore raw

PATCH 3/3 x86/pv: Misc improvements to pv_destroy_gdt()

In reply to Andrew Cooper

Hoist the l1e_from_pfn(zero_pfn, __PAGE_HYPERVISOR_RO) calculation out of the loop, and switch the code over to using mfn_t.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/pv/descriptor-tables.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
xen/arch/x86/pv/descriptor-tables.c +10 -7
index e31c97e..d1c4296 100644
@@ -37,18 +37,21 @@
 
 void pv_destroy_gdt(struct vcpu *v)
 {
-    l1_pgentry_t *pl1e;
+    l1_pgentry_t *pl1e = pv_gdt_ptes(v);
+    mfn_t zero_mfn = _mfn(virt_to_mfn(zero_page));
+    l1_pgentry_t zero_l1e = l1e_from_mfn(zero_mfn, __PAGE_HYPERVISOR_RO);
     unsigned int i;
-    unsigned long pfn, zero_pfn = PFN_DOWN(__pa(zero_page));
 
     v->arch.pv_vcpu.gdt_ents = 0;
-    pl1e = pv_gdt_ptes(v);
     for ( i = 0; i < FIRST_RESERVED_GDT_PAGE; i++ )
     {
-        pfn = l1e_get_pfn(pl1e[i]);
-        if ( (l1e_get_flags(pl1e[i]) & _PAGE_PRESENT) && pfn != zero_pfn )
-            put_page_and_type(mfn_to_page(_mfn(pfn)));
-        l1e_write(&pl1e[i], l1e_from_pfn(zero_pfn, __PAGE_HYPERVISOR_RO));
+        mfn_t mfn = l1e_get_mfn(pl1e[i]);
+
+        if ( (l1e_get_flags(pl1e[i]) & _PAGE_PRESENT) &&
+             !mfn_eq(mfn, zero_mfn) )
+            put_page_and_type(mfn_to_page(mfn));
+
+        l1e_write(&pl1e[i], zero_l1e);
         v->arch.pv_vcpu.gdt_frames[i] = 0;
     }
 }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH for-next 3/3] x86/pv: Misc improvements to pv_destroy_gdt()

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 17:47, <[email protected]> wrote:

Hoist the l1e_from_pfn(zero_pfn, __PAGE_HYPERVISOR_RO) calculation out of the loop, and switch the code over to using mfn_t.

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

Acked-by on v1 1/3
#5 lore raw
In reply to Andrew Cooper
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.10.17 at 17:47, <[email protected]> wrote:

This also makes the do_update_descriptor() pair of functions adjacent.

Purely code motion; no functional change.

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