Skip to contentSkip to conversation

unmap_page_range optimisation

PATCH Superseded Kevin Lampis 3 participants 18 messages Nov 12 – Nov 13

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 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 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 (6):
x86: extend update_intpte() to support atomic get-and-update
x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY
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             | 181 +++++++++++++++++++++++++---------
 xen/arch/x86/pv/mm.h          |  14 ++-
 xen/common/kernel.c           |   1 +
 xen/include/public/features.h |   3 +
 xen/include/public/xen.h      |   1 +
 5 files changed, 152 insertions(+), 48 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v5 1/6 x86: extend update_intpte() to support atomic get-and-update

The update_intpte() now accepts a new swap flag and if set returns the old pte value.

No functional change for existing callers.

---
Changes in v5:
- Update the comment above update_intpte()

Changes in v4:
- Remove UPDATE_ENTRY_SWAP
- Don't move the backslashes in UPDATE_ENTRY

Changes in v3:
- Return 0 in the paging_write_guest_entry() case (update_intpte)
- Add UPDATE_ENTRY_SWAP to convert return value to correct type

Changes in v2:
- Add new PTE_UPDATE_SWAP flag instead of a bool argument
- Return the old pte value instead of turning old into an out pointer

---
 xen/arch/x86/pv/mm.h | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
xen/arch/x86/pv/mm.h +11 -3
index bfee0feb7b21..4016dd1087ba 100644
@@ -64,16 +64,22 @@ static inline intpte_t paging_cmpxchg_guest_entry(
 
 #define PTE_UPDATE_PRESERVE_AD  (1u << 0)
 #define PTE_UPDATE_NO_TRANSLATE (1u << 1)
+#define PTE_UPDATE_SWAP         (1u << 2)
 
 /*
  * How to write an entry to the guest pagetables.
+ * If the SWAP flag is set then does an atomic swap and returns the old PTE
+ * value.
  */
-static inline void update_intpte(intpte_t *p, intpte_t old, intpte_t new,
-                                 mfn_t mfn, struct vcpu *v, unsigned int flags)
+static inline intpte_t update_intpte(intpte_t *p, intpte_t old, intpte_t new,
+                                     mfn_t mfn, struct vcpu *v, unsigned int flags)
 {
 #ifndef PTE_UPDATE_WITH_CMPXCHG
-    if ( !(flags & PTE_UPDATE_PRESERVE_AD) )
+    if ( !(flags & (PTE_UPDATE_PRESERVE_AD|PTE_UPDATE_SWAP)) )
+    {
         paging_write_guest_entry(v, p, new, mfn);
+        return 0;
+    }
     else
 #endif
     {
@@ -95,6 +101,8 @@ static inline void update_intpte(intpte_t *p, intpte_t old, intpte_t new,
             old = t;
         }
     }
+
+    return old;
 }
 
 /*
Signature · 1 line
2.52.0
#3 lore raw

PATCH v5 2/6 x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY

In reply to Kevin Lampis

bloat-o-meter:
Function old new delta

mod_l1_entry                                1186    1113     -73
do_mmu_update                               5546    5392    -154

---
Changes in v5:
- New patch

---
 xen/arch/x86/mm.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
xen/arch/x86/mm.c +19 -26
index e42cd33c0d09..dc0e0d451c02 100644
@@ -2197,12 +2197,12 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
 {
     l1_pgentry_t ol1e = l1e_read(pl1e);
     struct domain *pt_dom = pt_vcpu->domain;
-    int rc = 0;
 
     ASSERT(!paging_mode_refcounts(pt_dom));
 
     if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
     {
+        int rc;
         struct page_info *page = NULL;
         uint32_t disallow = l1_disallow_mask(pt_dom);
 
@@ -2276,16 +2276,14 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
         }
         if ( page )
             put_page(page);
-
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l1e(pt_dom, nl1e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
 
     put_page_from_l1e(ol1e, pt_dom);
-    return rc;
+    return 0;
 }
 
 
@@ -2300,7 +2298,6 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
     struct domain *d = vcpu->domain;
     struct page_info *l2pg = mfn_to_page(mfn);
     unsigned long type = l2pg->u.inuse.type_info;
-    int rc = 0;
 
     if ( unlikely(!is_guest_l2_slot(d, type, pgentry_ptr_to_slot(pl2e))) )
     {
@@ -2313,6 +2310,8 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
     if ( l2e_get_flags(nl2e) & _PAGE_PRESENT )
     {
+        int rc;
+
         if ( unlikely(l2e_get_flags(nl2e) & L2_DISALLOW_MASK) )
         {
             gdprintk(XENLOG_WARNING, "Bad L2 flags %x\n",
@@ -2331,17 +2330,15 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
         if ( unlikely((rc = get_page_from_l2e(nl2e, mfn, d, 0)) < 0) )
             return rc;
-
-        UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l2e(d, nl2e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags);
+
+    UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu, update_flags);
 
     put_page_from_l2e(ol2e, mfn, PTF_defer);
 
-    return rc;
+    return 0;
 }
 
 /* Update the L3 entry at pl3e to new value nl3e. pl3e is within frame mfn. */
@@ -2353,7 +2350,6 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
 {
     l3_pgentry_t ol3e;
     struct domain *d = vcpu->domain;
-    int rc = 0;
 
     /*
      * Disallow updates to final L3 slot. It contains Xen mappings, and it
@@ -2366,6 +2362,8 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
 
     if ( l3e_get_flags(nl3e) & _PAGE_PRESENT )
     {
+        int rc;
+
         if ( unlikely(l3e_get_flags(nl3e) & l3_disallow_mask(d)) )
         {
             gdprintk(XENLOG_WARNING, "Bad L3 flags %x\n",
@@ -2385,17 +2383,14 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
         rc = get_page_from_l3e(nl3e, mfn, d, 0);
         if ( unlikely(rc < 0) )
             return rc;
-        rc = 0;
-
-        UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l3e(d, nl3e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags);
+
+    UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu, update_flags);
 
     put_page_from_l3e(ol3e, mfn, PTF_defer);
-    return rc;
+    return 0;
 }
 
 /* Update the L4 entry at pl4e to new value nl4e. pl4e is within frame mfn. */
@@ -2407,7 +2402,6 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
 {
     struct domain *d = vcpu->domain;
     l4_pgentry_t ol4e;
-    int rc = 0;
 
     if ( unlikely(!is_guest_l4_slot(d, pgentry_ptr_to_slot(pl4e))) )
     {
@@ -2420,6 +2414,8 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
 
     if ( l4e_get_flags(nl4e) & _PAGE_PRESENT )
     {
+        int rc;
+
         if ( unlikely(l4e_get_flags(nl4e) & L4_DISALLOW_MASK) )
         {
             gdprintk(XENLOG_WARNING, "Bad L4 flags %x\n",
@@ -2439,17 +2435,14 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
         rc = get_page_from_l4e(nl4e, mfn, d, 0);
         if ( unlikely(rc < 0) )
             return rc;
-        rc = 0;
-
-        UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l4e(d, nl4e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags);
+
+    UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu, update_flags);
 
     put_page_from_l4e(ol4e, mfn, PTF_defer);
-    return rc;
+    return 0;
 }
 #endif /* CONFIG_PV */
 
Signature · 1 line
2.52.0
Reviewed-by on v5 2/6
#4 lore raw

Re: [PATCH v5 2/6] x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY

Quoted 6 lines · Kevin Lampis

bloat-o-meter:
Function old new delta

mod_l1_entry                                1186    1113     -73
do_mmu_update                               5546    5392    -154

No matter how the next patch is going to end up, this is a good change all on its own:

Likely a Requested-by: or Suggested-by: would want adding here, though.

Jan

#5 lore raw

Re: [PATCH v5 2/6] x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY

In reply to Kevin Lampis
Quoted 4 lines · Kevin Lampis

bloat-o-meter:
Function old new delta

mod_l1_entry                                1186    1113     -73
do_mmu_update                               5546    5392    -154

This was only for the sample I pasted inline.  For this patch it's

add/remove: 0/0 grow/shrink: 1/5 up/down: 10/-641 (-631) Function                                     old     new   delta mod_l1_entry.cold                             56      66     +10 mod_l1_entry                                1891    1885      -6 mod_l4_entry.cold                             39      31      -8 do_mmu_update.cold                           122     112     -10 mod_l4_entry                                 917     760    -157 do_mmu_update                               5745    5285    -460

although it's not particularly relevant to live in the commit message. (I use it as a litmus test that the changes are along the expected lines.)

For the commit message, you probably want "Fold two UPDATE_ENTRY() paths together, in preparation for an upcoming change." or so.

Quoted 66 lines

---
Changes in v5:
- New patch

---
 xen/arch/x86/mm.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
xen/arch/x86/mm.c +6 -7
index e42cd33c0d09..dc0e0d451c02 100644
@@ -2197,12 +2197,12 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
 {
     l1_pgentry_t ol1e = l1e_read(pl1e);
     struct domain *pt_dom = pt_vcpu->domain;
-    int rc = 0;
 
     ASSERT(!paging_mode_refcounts(pt_dom));
 
     if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
     {
+        int rc;
         struct page_info *page = NULL;
         uint32_t disallow = l1_disallow_mask(pt_dom);
 
@@ -2276,16 +2276,14 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
         }
         if ( page )
             put_page(page);
-
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l1e(pt_dom, nl1e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
 
     put_page_from_l1e(ol1e, pt_dom);
-    return rc;
+    return 0;
 }
 
 
@@ -2300,7 +2298,6 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
     struct domain *d = vcpu->domain;
     struct page_info *l2pg = mfn_to_page(mfn);
     unsigned long type = l2pg->u.inuse.type_info;
-    int rc = 0;
 
     if ( unlikely(!is_guest_l2_slot(d, type, pgentry_ptr_to_slot(pl2e))) )
     {
@@ -2313,6 +2310,8 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
     if ( l2e_get_flags(nl2e) & _PAGE_PRESENT )
     {
+        int rc;
+
         if ( unlikely(l2e_get_flags(nl2e) & L2_DISALLOW_MASK) )
         {
             gdprintk(XENLOG_WARNING, "Bad L2 flags %x\n",

xen/arch/x86/mm.c static int mod_l2_entry(l2_pgentry_t *pl2e, · line 2332

Quoted 3 lines
xen/arch/x86/mm.c +0 -0
@@ -2331,17 +2330,15 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
         if ( unlikely((rc = get_page_from_l2e(nl2e, mfn, d, 0)) < 0) )
             return rc;

Looking further through the call tree, I'm now confident that get_page_from_l?e() can't return a positive number.   Continuations are already expressed as -ERESTART.

So, either we want:

    if ( rc )
        return rc;

or:

    if ( rc < 0 )
        return rc;
    ASSERT(rc == 0);

and (whichever we choose), we want to bring l2 in line with l3 and l4.

Given that there are paths under get_page_from_l?e() which do have positive return values which are supposed to be unused in this particular call tree, I'm leaning towards the latter.

~Andrew

#6 lore raw

Re: [PATCH v5 2/6] x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY

Quoted 90 lines · Andrew Cooper
Quoted 4 lines · Kevin Lampis

bloat-o-meter:
Function old new delta

mod_l1_entry                                1186    1113     -73
do_mmu_update                               5546    5392    -154

This was only for the sample I pasted inline.  For this patch it's

add/remove: 0/0 grow/shrink: 1/5 up/down: 10/-641 (-631) Function                                     old     new   delta mod_l1_entry.cold                             56      66     +10 mod_l1_entry                                1891    1885      -6 mod_l4_entry.cold                             39      31      -8 do_mmu_update.cold                           122     112     -10 mod_l4_entry                                 917     760    -157 do_mmu_update                               5745    5285    -460

although it's not particularly relevant to live in the commit message. (I use it as a litmus test that the changes are along the expected lines.)

For the commit message, you probably want "Fold two UPDATE_ENTRY() paths together, in preparation for an upcoming change." or so.

Quoted 67 lines

---
Changes in v5:
- New patch

---
 xen/arch/x86/mm.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
xen/arch/x86/mm.c +6 -7
index e42cd33c0d09..dc0e0d451c02 100644
@@ -2197,12 +2197,12 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
 {
     l1_pgentry_t ol1e = l1e_read(pl1e);
     struct domain *pt_dom = pt_vcpu->domain;
-    int rc = 0;
 
     ASSERT(!paging_mode_refcounts(pt_dom));
 
     if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
     {
+        int rc;
         struct page_info *page = NULL;
         uint32_t disallow = l1_disallow_mask(pt_dom);
 
@@ -2276,16 +2276,14 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
         }
         if ( page )
             put_page(page);
-
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l1e(pt_dom, nl1e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
 
     put_page_from_l1e(ol1e, pt_dom);
-    return rc;
+    return 0;
 }
 
 
@@ -2300,7 +2298,6 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
     struct domain *d = vcpu->domain;
     struct page_info *l2pg = mfn_to_page(mfn);
     unsigned long type = l2pg->u.inuse.type_info;
-    int rc = 0;
 
     if ( unlikely(!is_guest_l2_slot(d, type, pgentry_ptr_to_slot(pl2e))) )
     {
@@ -2313,6 +2310,8 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
     if ( l2e_get_flags(nl2e) & _PAGE_PRESENT )
     {
+        int rc;
+
         if ( unlikely(l2e_get_flags(nl2e) & L2_DISALLOW_MASK) )
         {
             gdprintk(XENLOG_WARNING, "Bad L2 flags %x\n",
@@ -2331,17 +2330,15 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
Quoted 5 lines

xen/arch/x86/mm.c static int mod_l2_entry(l2_pgentry_t *pl2e, · line 2332

Quoted 2 lines
xen/arch/x86/mm.c +0 -0
         if ( unlikely((rc = get_page_from_l2e(nl2e, mfn, d, 0)) < 0) )
             return rc;

Looking further through the call tree, I'm now confident that get_page_from_l?e() can't return a positive number.

They can return L{2,4}E_RECURSIVE (i.e. 1).

Jan

#7 lore raw

Re: [PATCH v5 2/6] x86: rearrange how mod_l*_entry() functions call UPDATE_ENTRY

Quoted 57 lines · Jan Beulich
Quoted 56 lines · Andrew Cooper
Quoted 55 lines · Kevin Lampis
xen/arch/x86/mm.c +6 -7
@@ -2197,12 +2197,12 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
 {
     l1_pgentry_t ol1e = l1e_read(pl1e);
     struct domain *pt_dom = pt_vcpu->domain;
-    int rc = 0;
 
     ASSERT(!paging_mode_refcounts(pt_dom));
 
     if ( l1e_get_flags(nl1e) & _PAGE_PRESENT )
     {
+        int rc;
         struct page_info *page = NULL;
         uint32_t disallow = l1_disallow_mask(pt_dom);
 
@@ -2276,16 +2276,14 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
         }
         if ( page )
             put_page(page);
-
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
     }
     else if ( pv_l1tf_check_l1e(pt_dom, nl1e) )
         return -ERESTART;
-    else
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
 
     put_page_from_l1e(ol1e, pt_dom);
-    return rc;
+    return 0;
 }
 
 
@@ -2300,7 +2298,6 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
     struct domain *d = vcpu->domain;
     struct page_info *l2pg = mfn_to_page(mfn);
     unsigned long type = l2pg->u.inuse.type_info;
-    int rc = 0;
 
     if ( unlikely(!is_guest_l2_slot(d, type, pgentry_ptr_to_slot(pl2e))) )
     {
@@ -2313,6 +2310,8 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
     if ( l2e_get_flags(nl2e) & _PAGE_PRESENT )
     {
+        int rc;
+
         if ( unlikely(l2e_get_flags(nl2e) & L2_DISALLOW_MASK) )
         {
             gdprintk(XENLOG_WARNING, "Bad L2 flags %x\n",
@@ -2331,17 +2330,15 @@ static int mod_l2_entry(l2_pgentry_t *pl2e,
 
Quoted 7 lines
Quoted 5 lines

xen/arch/x86/mm.c static int mod_l2_entry(l2_pgentry_t *pl2e, · line 2332

Quoted 2 lines
xen/arch/x86/mm.c +0 -0
         if ( unlikely((rc = get_page_from_l2e(nl2e, mfn, d, 0)) < 0) )
             return rc;

Looking further through the call tree, I'm now confident that get_page_from_l?e() can't return a positive number.

They can return L{2,4}E_RECURSIVE (i.e. 1).

Oh, sorry, these constants are still only local to my tree.

Jan

#8 lore raw

PATCH v5 3/6 x86: extend mod_l*_entry() to optionally return the old PTE value

In reply to Kevin Lampis

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 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 | 77 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 59 insertions(+), 18 deletions(-)
xen/arch/x86/mm.c +59 -18
index dc0e0d451c02..316a435b9b71 100644
@@ -2193,7 +2193,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;
@@ -2254,7 +2255,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                        update_flags);
+            if ( ol1e_out )
+                *ol1e_out = l1e_from_intpte(old);
+
             if ( page )
                 put_page(page);
             return 0;
@@ -2280,7 +2285,12 @@ 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);
+    {
+        intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                    update_flags);
+        if ( ol1e_out )
+            *ol1e_out = l1e_from_intpte(old);
+    }
 
     put_page_from_l1e(ol1e, pt_dom);
     return 0;
@@ -2292,7 +2302,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;
@@ -2324,7 +2335,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu,
+                                        update_flags);
+            if ( ol2e_out )
+                *ol2e_out = l2e_from_intpte(old);
+
             return 0;
         }
 
@@ -2334,7 +2349,12 @@ 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);
+    {
+        intpte_t old = UPDATE_ENTRY(l2, pl2e, ol2e, nl2e, mfn, vcpu,
+                                    update_flags);
+        if ( ol2e_out )
+            *ol2e_out = l2e_from_intpte(old);
+    }
 
     put_page_from_l2e(ol2e, mfn, PTF_defer);
 
@@ -2346,7 +2366,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;
@@ -2376,7 +2397,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu,
+                                        update_flags);
+            if ( ol3e_out )
+                *ol3e_out = l3e_from_intpte(old);
+
             return 0;
         }
 
@@ -2387,7 +2412,12 @@ 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);
+    {
+        intpte_t old = UPDATE_ENTRY(l3, pl3e, ol3e, nl3e, mfn, vcpu,
+                                    update_flags);
+        if ( ol3e_out )
+            *ol3e_out = l3e_from_intpte(old);
+    }
 
     put_page_from_l3e(ol3e, mfn, PTF_defer);
     return 0;
@@ -2398,7 +2428,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;
@@ -2428,7 +2459,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu,
+                                        update_flags);
+            if ( ol4e_out )
+                *ol4e_out = l4e_from_intpte(old);
+
             return 0;
         }
 
@@ -2439,7 +2474,12 @@ 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);
+    {
+        intpte_t old = UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, mfn, vcpu,
+                                    update_flags);
+        if ( ol4e_out )
+            *ol4e_out = l4e_from_intpte(old);
+    }
 
     put_page_from_l4e(ol4e, mfn, PTF_defer);
     return 0;
@@ -3311,7 +3351,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 )
         {
@@ -4149,14 +4189,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;
@@ -4165,7 +4205,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;
@@ -4174,7 +4214,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 )
@@ -4514,7 +4554,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);
Signature · 1 line
2.52.0
#9 lore raw

Re: [PATCH v5 3/6] x86: extend mod_l*_entry() to optionally return the old PTE value

Quoted 29 lines · Kevin Lampis
xen/arch/x86/mm.c +7 -2
@@ -2193,7 +2193,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;
@@ -2254,7 +2255,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                        update_flags);
+            if ( ol1e_out )
+                *ol1e_out = l1e_from_intpte(old);
+
             if ( page )
                 put_page(page);
             return 0;
@@ -2280,7 +2285,12 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
     else if ( pv_l1tf_check_l1e(pt_dom, nl1e) )
         return -ERESTART;
 

xen/arch/x86/mm.c static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e, · line 2296

Quoted 10 lines
xen/arch/x86/mm.c +6 -1
-    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu, update_flags);
+    {
+        intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                    update_flags);
+        if ( ol1e_out )
+            *ol1e_out = l1e_from_intpte(old);
+    }
 
     put_page_from_l1e(ol1e, pt_dom);
     return 0;

This (recurring) pattern still adds quite a bit of redundancy. Was it ruled out in an earlier version to pass ol1e_out into UPDATE_ENTRY() (and perhaps through to update_intpte()), to have the updating done there? If so, why?

Otherwise, nit: Blank lines please between declaration(s) and statement(s) (which, if this form is to be retained, could likely be adjusted while committing).

Jan

#10 lore raw

Re: [PATCH v5 3/6] x86: extend mod_l*_entry() to optionally return the old PTE value

Quoted 38 lines · Jan Beulich
Quoted 37 lines · Kevin Lampis
xen/arch/x86/mm.c +13 -3
@@ -2193,7 +2193,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;
@@ -2254,7 +2255,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                        update_flags);
+            if ( ol1e_out )
+                *ol1e_out = l1e_from_intpte(old);
+
             if ( page )
                 put_page(page);
             return 0;
@@ -2280,7 +2285,12 @@ 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);
+    {
+        intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                    update_flags);
+        if ( ol1e_out )
+            *ol1e_out = l1e_from_intpte(old);
+    }
 
Quoted 5 lines

xen/arch/x86/mm.c static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e, · line 2296

Quoted 2 lines
xen/arch/x86/mm.c +0 -0
     put_page_from_l1e(ol1e, pt_dom);
     return 0;

This (recurring) pattern still adds quite a bit of redundancy. Was it ruled out in an earlier version to pass ol1e_out into UPDATE_ENTRY() (and perhaps through to update_intpte()), to have the updating done there? If so, why?

I can't spot a nice way of doing this.

The pattern is quite nasty at all levels.  We have pl?e which is the live PTE in guest memory.  We read that (once) to get ol?e, then pass it down to a CMPXCHG which can return it modified by A/D.

The issue is it's modify_l?_entry() which reads pl?e into ol?e, not the caller, so you can't just plumb mod_l1_entry()'s parameter all the way down.

Also, mod_l?_entry() wants to be typesafe, whereas UPDATE_ENTRY() wants to be not.

~Andrew

#11 lore raw

Re: [PATCH v5 3/6] x86: extend mod_l*_entry() to optionally return the old PTE value

Quoted 39 lines · Andrew Cooper
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Kevin Lampis
xen/arch/x86/mm.c +13 -3
@@ -2193,7 +2193,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;
@@ -2254,7 +2255,11 @@ 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);
+            intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                        update_flags);
+            if ( ol1e_out )
+                *ol1e_out = l1e_from_intpte(old);
+
             if ( page )
                 put_page(page);
             return 0;
@@ -2280,7 +2285,12 @@ 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);
+    {
+        intpte_t old = UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, pt_vcpu,
+                                    update_flags);
+        if ( ol1e_out )
+            *ol1e_out = l1e_from_intpte(old);
+    }
 
Quoted 7 lines
Quoted 5 lines

xen/arch/x86/mm.c static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e, · line 2296

Quoted 2 lines
xen/arch/x86/mm.c +0 -0
     put_page_from_l1e(ol1e, pt_dom);
     return 0;

This (recurring) pattern still adds quite a bit of redundancy. Was it ruled out in an earlier version to pass ol1e_out into UPDATE_ENTRY() (and perhaps through to update_intpte()), to have the updating done there? If so, why?

I can't spot a nice way of doing this.

See below. That's still retaining type safety as per your reply to patch 4 and as per ...

Quoted 9 lines

The pattern is quite nasty at all levels.  We have pl?e which is the live PTE in guest memory.  We read that (once) to get ol?e, then pass it down to a CMPXCHG which can return it modified by A/D.

The issue is it's modify_l?_entry() which reads pl?e into ol?e, not the caller, so you can't just plumb mod_l1_entry()'s parameter all the way down.

Also, mod_l?_entry() wants to be typesafe, whereas UPDATE_ENTRY() wants to be not.

... what you say here. Things may get yet a bit easier if we sacrificed that (albeit, as indicated there, more in patch 4 than here). With some count_args() trickery it may also be possible to avoid the need to touch pv/grant_table.c and pv/ro-page-fault.c. Not sure whether that would be worth it.

Jan

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().

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 +27 -18
@@ -2194,7 +2194,8 @@ static void l3t_unlock(struct page_info
 /* 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;
@@ -2255,7 +2256,8 @@ static int mod_l1_entry(l1_pgentry_t *pl
         /* 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;
@@ -2281,7 +2283,7 @@ static int mod_l1_entry(l1_pgentry_t *pl
     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;
@@ -2293,7 +2295,8 @@ static int mod_l2_entry(l2_pgentry_t *pl
                         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;
@@ -2325,7 +2328,8 @@ static int mod_l2_entry(l2_pgentry_t *pl
         /* 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;
         }
 
@@ -2335,7 +2339,7 @@ static int mod_l2_entry(l2_pgentry_t *pl
     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);
 
@@ -2347,7 +2351,8 @@ static int mod_l3_entry(l3_pgentry_t *pl
                         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;
@@ -2377,7 +2382,8 @@ static int mod_l3_entry(l3_pgentry_t *pl
         /* 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;
         }
 
@@ -2388,7 +2394,7 @@ static int mod_l3_entry(l3_pgentry_t *pl
     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;
@@ -2399,7 +2405,8 @@ static int mod_l4_entry(l4_pgentry_t *pl
                         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;
@@ -2429,7 +2436,8 @@ static int mod_l4_entry(l4_pgentry_t *pl
         /* 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;
         }
 
@@ -2440,7 +2448,7 @@ static int mod_l4_entry(l4_pgentry_t *pl
     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;
@@ -3318,7 +3326,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 )
         {
@@ -4156,14 +4164,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;
@@ -4172,7 +4180,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;
@@ -4181,7 +4189,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 )
@@ -4521,7 +4529,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/grant_table.c +3 -3
@@ -98,7 +98,7 @@ int create_grant_pv_mapping(uint64_t add
         goto out_unlock;
 
     ol1e = *pl1e;
-    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, curr, 0);
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, curr, 0, null_pl1e);
     /*
      * We always create mappings in this path.  However, our caller,
      * map_grant_ref(), only passes potentially non-zero cache_flags for
@@ -163,7 +163,7 @@ static bool steal_linear_address(unsigne
         goto out_unlock;
 
     ol1e = *pl1e;
-    UPDATE_ENTRY(l1, pl1e, ol1e, l1e_empty(), gl1mfn, curr, 0);
+    UPDATE_ENTRY(l1, pl1e, ol1e, l1e_empty(), gl1mfn, curr, 0, null_pl1e);
     *out = ol1e;
     okay = true;
 
@@ -290,7 +290,7 @@ int replace_grant_pv_mapping(uint64_t ad
                  "PTE flags %x for %"PRIx64" don't match grant (%x)\n",
                  l1e_get_flags(ol1e), addr, grant_pte_flags);
 
-    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, curr, 0);
+    UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, gl1mfn, curr, 0, null_pl1e);
     /*
      * Generally, replace_grant_pv_mapping() is used to destroy mappings
      * (n1le = l1e_empty()), but it can be a present mapping on the
xen/arch/x86/pv/mm.h +11 -4
@@ -109,10 +109,17 @@ static inline intpte_t update_intpte(int
  * Macro that wraps the appropriate type-changes around update_intpte().
  * Arguments are: type, ptr, old, new, mfn, vcpu
  */
-#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) do {         \
+    intpte_t old_ = update_intpte(&_t ## e_get_intpte(*(_p)),       \
+                                  _t ## e_get_intpte(_o),           \
+                                  _t ## e_get_intpte(_n),           \
+                                  _m, _v, fl);                      \
+    if ( out )                                                      \
+        *(out) = _t ## e_from_intpte(old_);                         \
+} while ( false )
+
+/* Use this instead of NULL for UPDATE_ENTRY()'s last argument. */
+static l1_pgentry_t *const null_pl1e = NULL;
 
 static always_inline l1_pgentry_t adjust_guest_l1e(l1_pgentry_t l1e,
                                                    const struct domain *d)
xen/arch/x86/pv/ro-page-fault.c +1 -1
@@ -200,7 +200,7 @@ static int ptwr_emulated_update(unsigned
     else
     {
         ol1e = *pl1e;
-        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, mfn, v, 0);
+        UPDATE_ENTRY(l1, pl1e, ol1e, nl1e, mfn, v, 0, null_pl1e);
     }
 
     trace_ptwr_emulation(addr, nl1e);
#12 lore raw

PATCH v5 4/6 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 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             | 75 ++++++++++++++++++++++++++++++-----
 xen/common/kernel.c           |  1 +
 xen/include/public/features.h |  3 ++
 xen/include/public/xen.h      |  1 +
 4 files changed, 71 insertions(+), 9 deletions(-)
xen/arch/x86/mm.c +66 -9
index 316a435b9b71..2458fd5e508b 100644
@@ -4123,6 +4123,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;
 
@@ -4184,37 +4185,79 @@ long do_mmu_update(
                      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);
-                    break;
+                                      update_flags, v, pg_owner, &ol1e);
 
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol1e.l1;
+                        if ( unlikely(copy_field_to_guest(ureqs, &req, val)) )
+                            rc = -EFAULT;
+                    }
+                    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;
-                    break;
 
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol2e.l2;
+                        if ( unlikely(copy_field_to_guest(ureqs, &req, val)) )
+                            rc = -EFAULT;
+                    }
+
+                    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;
-                    break;
 
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol3e.l3;
+                        if ( unlikely(copy_field_to_guest(ureqs, &req, val)) )
+                            rc = -EFAULT;
+                    }
+
+                    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 )
@@ -4239,9 +4282,22 @@ long do_mmu_update(
                                      mfn) + local_in_use) )
                             flush_root_pt_others = true;
                     }
-                    break;
 
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol4e.l4;
+                        if ( unlikely(copy_field_to_guest(ureqs, &req, val)) )
+                            rc = -EFAULT;
+                    }
+
+                    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;
@@ -4251,7 +4307,8 @@ long do_mmu_update(
                 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
#13 lore raw

Re: [PATCH v5 4/6] x86: extend do_mmu_update() to support returning the old PTE value

Quoted 20 lines · Kevin Lampis
xen/arch/x86/mm.c +3 -0
@@ -4123,6 +4123,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;
 
@@ -4184,37 +4185,79 @@ long do_mmu_update(
                      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:

xen/arch/x86/mm.c long do_mmu_update( · line 4201

Quoted 10 lines
xen/arch/x86/mm.c +6 -2
+                {
+                    l1_pgentry_t ol1e = {};
                     rc = mod_l1_entry(va, l1e_from_intpte(req.val), mfn,
-                                      update_flags, v, pg_owner, NULL);
-                    break;
+                                      update_flags, v, pg_owner, &ol1e);
 
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol1e.l1;

If you didn't open-code l1e_get_intpte() here, it would become more obvious that the INTPTE -> L<n>E done in mod_l<n>_entry() is simply being reversed here. If ol1e was of type intpte_t, doing the update in update_intpte() (and not UPDATE_ENTRY()) would become feasible. Since intpte_t is a mere alias of uint64_t, passing &req.val into mod_l<n>_entry() directly would then also become possible. Less redundancy would be the result.

xen/arch/x86/mm.c long do_mmu_update( · line 4208

Quoted 7 lines
xen/arch/x86/mm.c +6 -0
+                        if ( unlikely(copy_field_to_guest(ureqs, &req, val)) )
+                            rc = -EFAULT;
+                    }
+                    break;
+                }
                 case PGT_l2_page_table:
+                {

If changes next to these case labels would remain, please also add the so far missing blank lines between non-fall-through case blocks.

Jan

#14 lore raw

Re: [PATCH v5 4/6] x86: extend do_mmu_update() to support returning the old PTE value

Quoted 28 lines · Jan Beulich
Quoted 27 lines · Kevin Lampis
xen/arch/x86/mm.c +6 -2
@@ -4123,6 +4123,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;
 
@@ -4184,37 +4185,79 @@ long do_mmu_update(
                      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);
-                    break;
+                                      update_flags, v, pg_owner, &ol1e);
 
Quoted 9 lines

xen/arch/x86/mm.c long do_mmu_update( · line 4201

Quoted 3 lines
xen/arch/x86/mm.c +3 -0
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol1e.l1;

If you didn't open-code l1e_get_intpte() here, it would become more obvious that the INTPTE -> L<n>E done in mod_l<n>_entry() is simply being reversed here. If ol1e was of type intpte_t, doing the update in update_intpte() (and not UPDATE_ENTRY()) would become feasible. Since intpte_t is a mere alias of uint64_t, passing &req.val into mod_l<n>_entry() directly would then also become possible. Less redundancy would be the result.

Perhaps, but then it would be odd to have mod_l1_entry() require nl1e in a typesafe way while passing back ol1e as type unsafe.

And there's still the problem of reading ol1e.  We could change the indirection of ol?e, but that's invasive both at the source and binary level.  While capable of being inlined, mod_l?_entry() are not due to their size.

~Andrew

#15 lore raw

Re: [PATCH v5 4/6] x86: extend do_mmu_update() to support returning the old PTE value

Quoted 39 lines · Andrew Cooper
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Kevin Lampis
xen/arch/x86/mm.c +6 -2
@@ -4123,6 +4123,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;
 
@@ -4184,37 +4185,79 @@ long do_mmu_update(
                      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);
-                    break;
+                                      update_flags, v, pg_owner, &ol1e);
 
Quoted 9 lines

xen/arch/x86/mm.c long do_mmu_update( · line 4201

Quoted 3 lines
xen/arch/x86/mm.c +3 -0
+                    if ( !rc && cmd == MMU_PT_UPDATE_SWAP )
+                    {
+                        req.val = ol1e.l1;

If you didn't open-code l1e_get_intpte() here, it would become more obvious that the INTPTE -> L<n>E done in mod_l<n>_entry() is simply being reversed here. If ol1e was of type intpte_t, doing the update in update_intpte() (and not UPDATE_ENTRY()) would become feasible. Since intpte_t is a mere alias of uint64_t, passing &req.val into mod_l<n>_entry() directly would then also become possible. Less redundancy would be the result.

Quoted 2 lines

Perhaps, but then it would be odd to have mod_l1_entry() require nl1e in a typesafe way while passing back ol1e as type unsafe.

If the sole purpose of those parameters is to obtain a type-unsafe intpte_t (for use here), I'm not convinced this oddity would be overly problematic.

Quoted 3 lines

And there's still the problem of reading ol1e.  We could change the indirection of ol?e, but that's invasive both at the source and binary level.

I said "passing &req.val" though - no change in indirection.

Quoted 2 lines

  While capable of being inlined, mod_l?_entry() are not due to their size.

Yes for mod_l1_entry(), but not for the other three afaics (debug build with gcc16).

Jan

#16 lore raw

PATCH v5 5/6 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 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
#17 lore raw

PATCH v5 6/6 Add test mmu-pt-update-swap

In reply to Kevin Lampis

Test for new mmu_update subop MMU_PT_UPDATE_SWAP

---
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