Skip to contentSkip to conversation

unmap_page_range optimisation

PATCH New Kevin Lampis 1 participant 5 messages Nov 25

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v6 cover unmap_page_range optimisation

https://lore.kernel.org/xen-devel/[email protected]/

Previous discussion:
RFC: unmap_page_range optimisation (avoiding emulation faults during VM migration)
https://lore.kernel.org/xen-devel/[email protected]/

This series adds a new MMU_PT_UPDATE_SWAP sub-op to the mmu_update hypercall which Linux can use from a new pte_get_and_clear pv-op to significantly improve the performance of unmap_page_range.

A microbenchmark[1] which allocates a large number of pages and then clears them shows a performance increase from 1100ms to 640ms using the new pte_get_and_clear pv-op.

Further profiling the microbenchmark with bpftrace[2] shows that Linux calls unmap_page_range() 23 times with an average execution time of 48ms dropping to 28ms when using the new operation.

Changes in v6:
- De-duplicate code in mm.c/mm.h
- No changes to Linux or XTF patches

Changes in v5:
- New Xen patch x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY
- New XTF patch (XTF) Add test mmu-pt-update-swap
- Review comments

Changes in v4:
- Review comments

Changes in v3:
- Extend swap to work with l2/l3/l4 PTEs and any value not just 0
- Other review comments

Changes in v2:
- Add a sub-op to mmu_update instead of new hypercall
- Add Linux patch
- Other review comments

[1] microbenchmark
#include <err.h>
#include <sys/mman.h>
#include <time.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

static uint64_t nsec(void)

{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec;
}

int main(int argc, char **argv)

{
    const size_t len = 1024UL * 1024 * 1024 * 4;
    const long pagesz = sysconf(_SC_PAGESIZE);

    char *p = mmap(NULL, len,
                   PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS,
                   -1, 0);
    if ( p == MAP_FAILED )
        err(1, "mmap");

    /* Fault every page in */
    for (size_t i = 0; i < len; i += pagesz)
        p[i] = 1;

    uint64_t start = nsec();

    if ( madvise(p, len, MADV_DONTNEED) )
        err(1, "madvise");

    uint64_t end = nsec();

    printf("MADV_DONTNEED on %zu MB took %.3f ms\n",
           len / 1024 / 1024,
           (end - start) / 1e6);

    munmap(p, len);
    return 0;
}

[2] bpftrace
bpftrace -e '
kprobe:unmap_page_range
/comm == "a.out"/

{
    @start[tid] = nsecs;
}

kretprobe:unmap_page_range
/@start[tid] && comm == "a.out"/

{
    $delta = nsecs - @start[tid];
    @count = count();
    @total = sum($delta);
    delete(@start[tid]);
}

interval:s:10

{
    printf("avg call time = %d ns (%d calls)\n",
           (@total / @count), (uint64)@count);
    exit();
}'

Kevin Lampis (4):
x86: extend mod_l*_entry() to optionally return the old PTE value
x86: extend do_mmu_update() to support returning the old PTE value (Linux) xen: Add new Xen pv-op pte_get_and_clear.
(XTF) Add test mmu-pt-update-swap

 xen/arch/x86/mm.c             | 95 ++++++++++++++++++++++++++---------
 xen/arch/x86/pv/mm.h          | 28 +++++++++--
 xen/common/kernel.c           |  1 +
 xen/include/public/features.h |  3 ++
 xen/include/public/xen.h      |  1 +
 5 files changed, 100 insertions(+), 28 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v6 1/4 x86: extend mod_l*_entry() to optionally return the old PTE value

Calling mod_l*_entry() with a valid ol*e_out pointer will do atomic CMPXCHG to set the new pte value and return the old pte value through the ol*e_out pointer. If the ol*e_out pointer is NULL then old behavior is preserved.

---
Changes in v6:
- Write of the output value moved into UPDATE_ENTRY().
- Use count_args() to keep call semantics the same for non-SWAP users
- The temporary out_ variable is not strictly necessary for upstream but we have something in our patchqueue that falls foul of -Werror=address without it.

Changes in v5:
- Refactoring the mod_l*_entry() functions was split out into a patch before this one

Changes in v4:
- Refactor how UPDATE_ENTRY is used
- Update commit message to specify CMPXCHG instead of xchg

Changes in v3:
- Add swap to mod_l{2,3,4}_entry()

Changes in v2:
- UPDATE_ENTRY now returns the old pte value instead of using an out pointer

---
 xen/arch/x86/mm.c    | 45 ++++++++++++++++++++++++++------------------
 xen/arch/x86/pv/mm.h | 28 ++++++++++++++++++++++-----
 2 files changed, 50 insertions(+), 23 deletions(-)
xen/arch/x86/mm.c +27 -18
index dee06828b3bc..7ee71e5cc61d 100644
@@ -2197,7 +2197,8 @@ static void l3t_unlock(struct page_info *page)
 /* Update the L1 entry at pl1e to new value nl1e. */
 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
                         mfn_t gl1mfn, unsigned int update_flags,
-                        struct vcpu *pt_vcpu, struct domain *pg_dom)
+                        struct vcpu *pt_vcpu, struct domain *pg_dom,
+                        l1_pgentry_t *ol1e_out)
 {
     l1_pgentry_t ol1e = l1e_read(pl1e);
     struct domain *pt_dom = pt_vcpu->domain;
@@ -2258,7 +2259,8 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
         /* Fast path for sufficiently-similar mappings. */
         if ( !l1e_has_changed(ol1e, nl1e, ~FASTPATH_FLAG_WHITELIST) )
         {
-            UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+            UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags,
+                         ol1e_out);
             if ( page )
                 put_page(page);
             return 0;
@@ -2284,7 +2286,7 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
     else if ( pv_l1tf_check_l1e(pt_dom, nl1e) )
         return -ERESTART;
 
-    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags, ol1e_out);
 
     put_page_from_l1e(ol1e, pt_dom);
     return 0;
@@ -2296,7 +2298,8 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
                         l2_pgentry_t nl2e,
                         mfn_t mfn,
                         unsigned int update_flags,
-                        struct vcpu *vcpu)
+                        struct vcpu *vcpu,
+                        l2_pgentry_t *ol2e_out)
 {
     l2_pgentry_t ol2e;
     struct domain *d = vcpu->domain;
@@ -2328,7 +2331,8 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
         /* Fast path for sufficiently-similar mappings. */
         if ( !l2e_has_changed(ol2e, nl2e, ~FASTPATH_PDE_FLAG_WHITELIST) )
         {
-            UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags);
+            UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags,
+                         ol2e_out);
             return 0;
         }
 
@@ -2340,7 +2344,7 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
     else if ( pv_l1tf_check_l2e(d, nl2e) )
         return -ERESTART;
 
-    UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags);
+    UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags, ol2e_out);
 
     put_page_from_l2e(ol2e, mfn, PTF_defer);
 
@@ -2352,7 +2356,8 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
                         l3_pgentry_t nl3e,
                         mfn_t mfn,
                         unsigned int update_flags,
-                        struct vcpu *vcpu)
+                        struct vcpu *vcpu,
+                        l3_pgentry_t *ol3e_out)
 {
     l3_pgentry_t ol3e;
     struct domain *d = vcpu->domain;
@@ -2382,7 +2387,8 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
         /* Fast path for sufficiently-similar mappings. */
         if ( !l3e_has_changed(ol3e, nl3e, ~FASTPATH_PDE_FLAG_WHITELIST) )
         {
-            UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags);
+            UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags,
+                         ol3e_out);
             return 0;
         }
 
@@ -2394,7 +2400,7 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
     else if ( pv_l1tf_check_l3e(d, nl3e) )
         return -ERESTART;
 
-    UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags);
+    UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags, ol3e_out);
 
     put_page_from_l3e(ol3e, mfn, PTF_defer);
     return 0;
@@ -2405,7 +2411,8 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
                         l4_pgentry_t nl4e,
                         mfn_t mfn,
                         unsigned int update_flags,
-                        struct vcpu *vcpu)
+                        struct vcpu *vcpu,
+                        l4_pgentry_t *ol4e_out)
 {
     struct domain *d = vcpu->domain;
     l4_pgentry_t ol4e;
@@ -2435,7 +2442,8 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
         /* Fast path for sufficiently-similar mappings. */
         if ( !l4e_has_changed(ol4e, nl4e, ~FASTPATH_PDE_FLAG_WHITELIST) )
         {
-            UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags);
+            UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags,
+                         ol4e_out);
             return 0;
         }
 
@@ -2447,7 +2455,7 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
     else if ( pv_l1tf_check_l4e(d, nl4e) )
         return -ERESTART;
 
-    UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags);
+    UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags, ol4e_out);
 
     put_page_from_l4e(ol4e, mfn, PTF_defer);
     return 0;
@@ -3325,7 +3333,7 @@ int new_guest_cr3(mfn_t mfn)
                           l4e_from_mfn(mfn,
                                        (_PAGE_PRESENT | _PAGE_RW |
                                         _PAGE_USER | _PAGE_ACCESSED)),
-                          gt_mfn, 0, curr);
+                          gt_mfn, 0, curr, NULL);
         unmap_domain_page(pl4e);
         switch ( rc )
         {
@@ -4163,14 +4171,14 @@ long do_mmu_update(
                 {
                 case PGT_l1_page_table:
                     rc = mod_l1_entry(va, l1e_from_intpte(req.val), mfn,
-                                      update_flags, v, pg_owner);
+                                      update_flags, v, pg_owner, NULL);
                     break;
 
                 case PGT_l2_page_table:
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
                     rc = mod_l2_entry(va, l2e_from_intpte(req.val), mfn,
-                                      update_flags, v);
+                                      update_flags, v, NULL);
                     if ( !rc )
                         flush_linear_pt = true;
                     break;
@@ -4179,7 +4187,7 @@ long do_mmu_update(
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
                     rc = mod_l3_entry(va, l3e_from_intpte(req.val), mfn,
-                                      update_flags, v);
+                                      update_flags, v, NULL);
                     if ( !rc )
                         flush_linear_pt = true;
                     break;
@@ -4188,7 +4196,7 @@ long do_mmu_update(
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
                     rc = mod_l4_entry(va, l4e_from_intpte(req.val), mfn,
-                                      update_flags, v);
+                                      update_flags, v, NULL);
                     if ( !rc )
                         flush_linear_pt = true;
                     if ( !rc && pt_owner->arch.pv.xpti )
@@ -4528,7 +4536,8 @@ static int __do_update_va_mapping(
         goto out;
     }
 
-    rc = mod_l1_entry(pl1e, val, gl1mfn, MMU_NORMAL_PT_UPDATE, v, pg_owner);
+    rc = mod_l1_entry(pl1e, val, gl1mfn, MMU_NORMAL_PT_UPDATE, v, pg_owner,
+                      NULL);
 
     page_unlock(gl1pg);
     put_page(gl1pg);
xen/arch/x86/pv/mm.h +23 -5
index 8ee776509c8b..eb099ac9dc33 100644
@@ -2,6 +2,7 @@
 #define __PV_MM_H__
 
 #include <asm/shadow.h>
+#include <xen/macros.h>
 
 l1_pgentry_t *map_guest_l1e(unsigned long linear, mfn_t *gl1mfn);
 
@@ -105,14 +106,31 @@ static inline intpte_t update_intpte(intpte_t *p, intpte_t old, intpte_t new,
     return old;
 }
 
+#define UPDATE_ENTRY0(_t ,_p ,_o ,_n ,_m ,_v , fl)                  \
+    (void)update_intpte(&_t ## e_get_intpte(*(_p)),                 \
+                                  _t ## e_get_intpte(_o),           \
+                                  _t ## e_get_intpte(_n),           \
+                                  _m, _v, fl);
+
+#define UPDATE_ENTRY1(_t ,_p ,_o ,_n ,_m ,_v , fl, out) do {        \
+    intpte_t old_ = update_intpte(&_t ## e_get_intpte(*(_p)),       \
+                                  _t ## e_get_intpte(_o),           \
+                                  _t ## e_get_intpte(_n),           \
+                                  _m, _v, fl);                      \
+    typeof(out) out_ = (out);                                       \
+    if ( out_ )                                                     \
+        *out_ = _t ## e_from_intpte(old_);                          \
+} while ( false )
+
+#define UPDATE_ENTRY__(nr)  UPDATE_ENTRY ## nr
+#define UPDATE_ENTRY_(nr)  UPDATE_ENTRY__(nr)
+
 /*
  * Macro that wraps the appropriate type-changes around update_intpte().
- * Arguments are: type, ptr, old, new, mfn, vcpu
+ * Arguments are: type, ptr, old, new, mfn, vcpu, flags, [out]
  */
-#define UPDATE_ENTRY(_t ,_p ,_o ,_n ,_m ,_v , fl)                   \
-    update_intpte(&_t ## e_get_intpte(*(_p)),                       \
-                  _t ## e_get_intpte(_o), _t ## e_get_intpte(_n),   \
-                  _m, _v, fl)
+#define UPDATE_ENTRY(_t ,_p ,_o ,_n ,_m ,_v, fl, out...) \
+    UPDATE_ENTRY_(count_args(out))(_t ,_p ,_o ,_n ,_m ,_v , fl, ## out)
 
 static always_inline l1_pgentry_t adjust_guest_l1e(l1_pgentry_t l1e,
                                                    const struct domain *d)
Signature · 1 line
2.52.0
#2 lore raw

PATCH v6 2/4 x86: extend do_mmu_update() to support returning the old PTE value

In reply to Kevin Lampis

A new sub-op MMU_PT_UPDATE_SWAP when used will do an atomic swap to set the new value and return the old PTE value through the req.val field.

- The old PTE value is passed back to the guest through the req.val field

If MMU_PT_UPDATE_SWAP is not set then the old behavior is preserved
do_mmu_update -> mod_l*_entry -> UPDATE_ENTRY -> paging_write_guest_entry

The new MMU_PT_UPDATE_SWAP call chain looks like this
do_mmu_update -> mod_l*_entry -> UPDATE_ENTRY -> paging_cmpxchg_guest_entry

New feature flag XENFEAT_mmu_pt_update_swap.

---
Changes in v6:
- Use l1e_get_intpte instead of accessing fields directly
- copy_field_to_guest once at the end of the switch
This removes duplication but keeps type safetly
- I didn't go all the way with passing &req.val through as uint64_t I hope this is an acceptable compromise between type safety and de-duplication.
- Blank lines between declarations and statements

Changes in v5:
- Add braces to the case statements
- Move copy_to_guest to the end so it doesn't interfere with things like flush_linear_pt
- Use copy_field_to_guest instead of copy_to_guest
- Use the raw form of __copy_field_to_guest because bounds have already been checked
- Folded x86: New feature flag XENFEAT_mmu_pt_update_swap patch into this one

Changes in v4:
- Refactor how mod_l*_entry() is used
Checking if (!rc) multiple times in a row is inelegant but reduces the diff size. Let me know if that's not the right tradeoff here.

Changes in v3:
- Do swap for l2, l3, l4
- Allow all values not just 0

Changes in v2:
- Add a sub-op to mmu_update instead of new hypercall
- Rename to "swap" instead of "clear", although only 0 is accepted
- Remove spurious curly brace from case PGT_l1_page_table:
- Restructure case PGT_l1_page_table: to avoid diff churn
- Add missing MMU_PT_UPDATE_SWAP check for the second PGT_writable_page block

---
 xen/arch/x86/mm.c             | 58 +++++++++++++++++++++++++++++------
 xen/common/kernel.c           |  1 +
 xen/include/public/features.h |  3 ++
 xen/include/public/xen.h      |  1 +
 4 files changed, 54 insertions(+), 9 deletions(-)
xen/arch/x86/mm.c +49 -9
index 7ee71e5cc61d..f6af6e531475 100644
@@ -4105,6 +4105,7 @@ long do_mmu_update(
         case MMU_NORMAL_PT_UPDATE:
         case MMU_PT_UPDATE_PRESERVE_AD:
         case MMU_PT_UPDATE_NO_TRANSLATE:
+        case MMU_PT_UPDATE_SWAP:
         {
             p2m_type_t p2mt;
 
@@ -4161,42 +4162,66 @@ long do_mmu_update(
             if ( page_lock(page) )
             {
                 unsigned int update_flags = 0;
+                intpte_t old_pte = 0;
 
                 if ( cmd == MMU_PT_UPDATE_PRESERVE_AD )
                      update_flags = PTE_UPDATE_PRESERVE_AD;
                 else if ( cmd == MMU_PT_UPDATE_NO_TRANSLATE )
                      update_flags = PTE_UPDATE_NO_TRANSLATE;
+                else if ( cmd == MMU_PT_UPDATE_SWAP )
+                     update_flags = PTE_UPDATE_SWAP;
 
                 switch ( page->u.inuse.type_info & PGT_type_mask )
                 {
                 case PGT_l1_page_table:
+                {
+                    l1_pgentry_t ol1e = {};
+
                     rc = mod_l1_entry(va, l1e_from_intpte(req.val), mfn,
-                                      update_flags, v, pg_owner, NULL);
+                                      update_flags, v, pg_owner, &ol1e);
+                    old_pte = l1e_get_intpte(ol1e);
                     break;
-
+                }
                 case PGT_l2_page_table:
+                {
+                    l2_pgentry_t ol2e = {};
+
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
+
                     rc = mod_l2_entry(va, l2e_from_intpte(req.val), mfn,
-                                      update_flags, v, NULL);
+                                      update_flags, v, &ol2e);
+
                     if ( !rc )
                         flush_linear_pt = true;
+                    old_pte = l2e_get_intpte(ol2e);
                     break;
-
+                }
                 case PGT_l3_page_table:
+                {
+                    l3_pgentry_t ol3e = {};
+
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
+
                     rc = mod_l3_entry(va, l3e_from_intpte(req.val), mfn,
-                                      update_flags, v, NULL);
+                                      update_flags, v, &ol3e);
+
                     if ( !rc )
                         flush_linear_pt = true;
+                    old_pte = l3e_get_intpte(ol3e);
                     break;
-
+                }
                 case PGT_l4_page_table:
+                {
+                    l4_pgentry_t ol4e = {};
+
                     if ( unlikely(pg_owner != pt_owner) )
                         break;
+
                     rc = mod_l4_entry(va, l4e_from_intpte(req.val), mfn,
-                                      update_flags, v, NULL);
+                                      update_flags, v, &ol4e);
+
                     if ( !rc )
                         flush_linear_pt = true;
                     if ( !rc && pt_owner->arch.pv.xpti )
@@ -4221,19 +4246,34 @@ long do_mmu_update(
                                      mfn) + local_in_use) )
                             flush_root_pt_others = true;
                     }
+                    old_pte = l4e_get_intpte(ol4e);
                     break;
-
+                }
                 case PGT_writable_page:
+                    if ( unlikely(cmd == MMU_PT_UPDATE_SWAP) )
+                    {
+                        rc = -EINVAL;
+                        break;
+                    }
                     perfc_incr(writable_mmu_updates);
                     paging_write_guest_entry(v, va, req.val, mfn);
                     rc = 0;
                     break;
                 }
                 page_unlock(page);
+
+                if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                {
+                    req.val = old_pte;
+                    if ( unlikely(copy_field_to_guest(ureqs, &req, val)) )
+                        rc = -EFAULT;
+                }
+
                 if ( rc == -EINTR )
                     rc = -ERESTART;
             }
-            else if ( get_page_type(page, PGT_writable_page) )
+            else if ( likely(cmd != MMU_PT_UPDATE_SWAP) &&
+                      get_page_type(page, PGT_writable_page) )
             {
                 perfc_incr(writable_mmu_updates);
                 paging_write_guest_entry(v, va, req.val, mfn);
xen/common/kernel.c +1 -0
index 499023fae6fd..dc3cd9c3321b 100644
@@ -690,6 +690,7 @@ long do_xen_version(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
             if ( is_pv_domain(d) )
             {
                 fi.submap |= (1U << XENFEAT_mmu_pt_update_preserve_ad) |
+                             (1U << XENFEAT_mmu_pt_update_swap) |
                              (1U << XENFEAT_highmem_assist) |
                              (1U << XENFEAT_gnttab_map_avail_bits);
                 if ( IS_ENABLED(CONFIG_PV32) && VM_ASSIST(d, pae_extended_cr3) )
xen/include/public/features.h +3 -0
index 880193094713..227f1e408e41 100644
@@ -128,6 +128,9 @@
  */
 #define XENFEAT_dm_msix_all_writes        20
 
+/* x86: Does this Xen host support the MMU_PT_UPDATE_SWAP hypercall? */
+#define XENFEAT_mmu_pt_update_swap        21
+
 #define XENFEAT_NR_SUBMAPS 1
 
 #endif /* __XEN_PUBLIC_FEATURES_H__ */
xen/include/public/xen.h +1 -0
index 2149b8dd3808..2c403ca47ed6 100644
@@ -349,6 +349,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define MMU_PT_UPDATE_PRESERVE_AD  2 /* atomically: *ptr = val | (*ptr&(A|D)) */
 #define MMU_PT_UPDATE_NO_TRANSLATE 3 /* checked '*ptr = val'. ptr is MA.      */
                                      /* val never translated.                 */
+#define MMU_PT_UPDATE_SWAP         4 /* Update and return the old value. */
 
 /*
  * MMU EXTENDED OPERATIONS
Signature · 1 line
2.52.0
#3 lore raw

PATCH v6 3/4 xen: Add new Xen pv-op pte_get_and_clear

In reply to Kevin Lampis

If Xen supports XENFEAT_mmu_pt_update_swap, use the new MMU_PT_UPDATE_SWAP instead of native_ptep_get_and_clear(). This avoids the pagetable write trapping for emulation.

---
Changes in v6:
- No changes

Changes in v5:
- No changes

Changes in v4:
- Reword the commit message

Changes in v3:
- Add spacing to xen_pte_get_and_clear
- BUG_ON if hypercall fails
- Remove extra blank line
- Add documentation for MMU_PT_UPDATE_SWAP

Changes in v2:
- New patch

---
 arch/x86/include/asm/paravirt.h       |  5 +++++
 arch/x86/include/asm/paravirt_types.h |  1 +
 arch/x86/include/asm/pgtable.h        |  3 ++-
 arch/x86/kernel/paravirt.c            |  1 +
 arch/x86/xen/mmu_pv.c                 | 17 +++++++++++++++++
 include/xen/interface/features.h      |  2 ++
 include/xen/interface/xen.h           |  1 +
 7 files changed, 29 insertions(+), 1 deletion(-)
arch/x86/include/asm/paravirt.h +5 -0
index 0591aa38fd85..0a3a286c46f0 100644
@@ -373,6 +373,11 @@ static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
 	PVOP_VCALL2(pv_ops, mmu.set_pmd, pmdp, native_pmd_val(pmd));
 }
 
+static inline pte_t pte_get_and_clear(pte_t *ptep)
+{
+	return (pte_t){PVOP_CALL1(pte_t, mmu.pte_get_and_clear, ptep)};
+}
+
 static inline pmd_t __pmd(pmdval_t val)
 {
 	return (pmd_t) { PVOP_ALT_CALLEE1(pmdval_t, pv_ops, mmu.make_pmd, val,
arch/x86/include/asm/paravirt_types.h +1 -0
index b4c4a23e77a1..1bd4c19450b5 100644
@@ -135,6 +135,7 @@ struct pv_mmu_ops {
 	/* Pagetable manipulation functions */
 	void (*set_pte)(pte_t *ptep, pte_t pteval);
 	void (*set_pmd)(pmd_t *pmdp, pmd_t pmdval);
+	pte_t (*pte_get_and_clear)(pte_t *ptep);
 
 	pte_t (*ptep_modify_prot_start)(struct vm_area_struct *vma, unsigned long addr,
 					pte_t *ptep);
arch/x86/include/asm/pgtable.h +2 -1
index ac295ca6c92f..c2a7035b4848 100644
@@ -58,6 +58,7 @@ extern pmdval_t early_pmd_flags;
 #include <asm/paravirt.h>
 #else  /* !CONFIG_PARAVIRT_XXL */
 #define set_pte(ptep, pte)		native_set_pte(ptep, pte)
+#define pte_get_and_clear(ptep)	native_ptep_get_and_clear(ptep)
 
 #define set_pte_atomic(ptep, pte)					\
 	native_set_pte_atomic(ptep, pte)
@@ -1243,7 +1244,7 @@ bool ptep_clear_flush_young(struct vm_area_struct *vma,
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
 				       pte_t *ptep)
 {
-	pte_t pte = native_ptep_get_and_clear(ptep);
+	pte_t pte = pte_get_and_clear(ptep);
 	page_table_check_pte_clear(mm, addr, pte);
 	return pte;
 }
arch/x86/kernel/paravirt.c +1 -0
index 00b59d774389..58ecd12b10ee 100644
@@ -178,6 +178,7 @@ struct paravirt_patch_template pv_ops = {
 
 	.mmu.set_pte		= native_set_pte,
 	.mmu.set_pmd		= native_set_pmd,
+	.mmu.pte_get_and_clear	= native_ptep_get_and_clear,
 
 	.mmu.ptep_modify_prot_start	= __ptep_modify_prot_start,
 	.mmu.ptep_modify_prot_commit	= __ptep_modify_prot_commit,
arch/x86/xen/mmu_pv.c +17 -0
index 820af6f0aa57..a1e79672a408 100644
@@ -359,6 +359,18 @@ static void xen_set_pte(pte_t *ptep, pte_t pteval)
 	__xen_set_pte(ptep, pteval);
 }
 
+static pte_t xen_pte_get_and_clear(pte_t *ptep)
+{
+	struct mmu_update u = {
+		.ptr = virt_to_machine(ptep).maddr | MMU_PT_UPDATE_SWAP,
+		.val = pte_val_ma(native_make_pte(0))
+	};
+
+	BUG_ON(HYPERVISOR_mmu_update(&u, 1, NULL, DOMID_SELF) != 0);
+
+	return native_make_pte(u.val);
+}
+
 static pte_t xen_ptep_modify_prot_start(struct vm_area_struct *vma,
 					unsigned long addr, pte_t *ptep)
 {
@@ -2165,6 +2177,11 @@ static void __init xen_post_allocator_init(void)
 	pv_ops.mmu.set_pud = xen_set_pud;
 	pv_ops.mmu.set_p4d = xen_set_p4d;
 
+	if (xen_feature(XENFEAT_mmu_pt_update_swap))
+		pv_ops.mmu.pte_get_and_clear = xen_pte_get_and_clear;
+	else
+		pv_ops.mmu.pte_get_and_clear = native_ptep_get_and_clear;
+
 	/* This will work as long as patching hasn't happened yet
 	   (which it hasn't) */
 	pv_ops.mmu.alloc_pte = xen_alloc_pte;
include/xen/interface/features.h +2 -0
index 53f760378e39..b346d58c0a41 100644
@@ -97,6 +97,8 @@
 #define XENFEAT_not_direct_mapped         16
 #define XENFEAT_direct_mapped             17
 
+#define XENFEAT_mmu_pt_update_swap        21
+
 #define XENFEAT_NR_SUBMAPS 1
 
 #endif /* __XEN_PUBLIC_FEATURES_H__ */
include/xen/interface/xen.h +1 -0
index 40c9793e9880..64c120d8a100 100644
@@ -252,6 +252,7 @@
 #define MMU_MACHPHYS_UPDATE        1 /* ptr = MA of frame to modify entry for */
 #define MMU_PT_UPDATE_PRESERVE_AD  2 /* atomically: *ptr = val | (*ptr&(A|D)) */
 #define MMU_PT_UPDATE_NO_TRANSLATE 3 /* checked '*ptr = val'. ptr is MA.      */
+#define MMU_PT_UPDATE_SWAP         4 /* Update and return the old value. */
 
 /*
  * MMU EXTENDED OPERATIONS
Signature · 1 line
2.52.0
#4 lore raw

PATCH v6 4/4 Add test mmu-pt-update-swap

In reply to Kevin Lampis

Test for new mmu_update subop MMU_PT_UPDATE_SWAP

---
Changes in v6:
- No changes

Changes in v5:
- New patch

---
 include/xen/version.h             |   8 +++
 include/xen/xen.h                 |   1 +
 tests/mmu-pt-update-swap/Makefile |   9 +++
 tests/mmu-pt-update-swap/main.c   | 114 ++++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 tests/mmu-pt-update-swap/Makefile
 create mode 100644 tests/mmu-pt-update-swap/main.c
include/xen/version.h +8 -0
index 45ce490b4b8f..73719a358c9a 100644
@@ -24,6 +24,14 @@ typedef struct xen_compile_info xen_compile_info_t;
 #define XENVER_changeset 4
 typedef char xen_changeset_info_t[64];
 
+#define XENVER_get_features 6
+struct xen_feature_info {
+    uint32_t     submap_idx;    /* IN: which 32-bit submap to return */
+    uint32_t     submap;        /* OUT: 32-bit submap */
+};
+typedef struct xen_feature_info xen_feature_info_t;
+#define XENFEAT_mmu_pt_update_swap        21
+
 #endif /* __XEN_PUBLIC_VERSION_H__ */
 
 /*
include/xen/xen.h +1 -0
index 11af167702cd..8a239ea2b046 100644
@@ -246,6 +246,7 @@ struct mmu_update {
 #define MMU_NORMAL_PT_UPDATE      0 /* checked '*ptr = val'. ptr is MA.      */
 #define MMU_MACHPHYS_UPDATE       1 /* ptr = MA of frame to modify entry for */
 #define MMU_PT_UPDATE_PRESERVE_AD 2 /* atomically: *ptr = val | (*ptr&(A|D)) */
+#define MMU_PT_UPDATE_SWAP        4 /* Update and return the old value. */
     uint64_t ptr;       /* Machine address of PTE. */
     uint64_t val;       /* New contents of PTE.    */
 };
tests/mmu-pt-update-swap/Makefile new file +9 -0
new file mode 100644
index 000000000000..9e01c6bace63
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := mmu-pt-update-swap
+CATEGORY  := functional
+TEST-ENVS := $(PV_ENVIRONMENTS)
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/mmu-pt-update-swap/main.c new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
2.52.0