Skip to contentSkip to conversation

x86/mem_sharing: Rectify test for "empty" physmap entry in sharing_add_to_physmap

PATCH Applied Andres Lagar-Cavilla 3 participants 9 messages May 16, 2012 – May 23, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mem_sharing: Rectify test for "empty" physmap entry in sharing_add_to_physmap

 xen/arch/x86/mm/mem_sharing.c |  7 ++++---
 xen/include/asm-x86/p2m.h     |  8 ++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
xen/arch/x86/mm/mem_sharing.c +4 -3
diff -r f83397dfb6c0 -r 3169fba29613 xen/arch/x86/mm/mem_sharing.c
@@ -1073,9 +1073,10 @@ int mem_sharing_add_to_physmap(struct do
     if ( spage->sharing->handle != sh )
         goto err_unlock;
 
-    /* Make sure the target page is a hole in the physmap */
-    if ( mfn_valid(cmfn) ||
-         (!(p2m_is_ram(cmfn_type))) )
+    /* Make sure the target page is a hole in the physmap. These are typically
+     * p2m_mmio_dm, but also accept p2m_invalid and paged out pages. See the
+     * definition of p2m_is_hole in p2m.h. */
+    if ( !p2m_is_hole(cmfn_type) || mfn_valid(cmfn) )
     {
         ret = XENMEM_SHARING_OP_C_HANDLE_INVALID;
         goto err_unlock;
xen/include/asm-x86/p2m.h +8 -0
diff -r f83397dfb6c0 -r 3169fba29613 xen/include/asm-x86/p2m.h
@@ -133,6 +133,13 @@ typedef unsigned int p2m_query_t;
                        | p2m_to_mask(p2m_ram_paging_in)       \
                        | p2m_to_mask(p2m_ram_shared))
 
+/* Types that represent a physmap hole that is ok to replace with a shared
+ * entry */
+#define P2M_HOLE_TYPES (p2m_to_mask(p2m_mmio_dm)        \
+                       | p2m_to_mask(p2m_invalid)       \
+                       | p2m_to_mask(p2m_ram_paging_in) \
+                       | p2m_to_mask(p2m_ram_paged))
+
 /* Grant mapping types, which map to a real machine frame in another
  * VM */
 #define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw)  \
@@ -173,6 +180,7 @@ typedef unsigned int p2m_query_t;
 
 /* Useful predicates */
 #define p2m_is_ram(_t) (p2m_to_mask(_t) & P2M_RAM_TYPES)
+#define p2m_is_hole(_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)
 #define p2m_is_mmio(_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)
 #define p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES)
 #define p2m_is_magic(_t) (p2m_to_mask(_t) & P2M_MAGIC_TYPES)
#1 lore raw
Quoted 5 lines

diff -r f83397dfb6c0 -r 3169fba29613 xen/arch/x86/mm/mem_sharing.c

xen/arch/x86/mm/mem_sharing.c int mem_sharing_add_to_physmap(struct do · line 1079

Quoted 10 lines
xen/arch/x86/mm/mem_sharing.c +4 -3
@@ -1073,9 +1073,10 @@ int mem_sharing_add_to_physmap(struct do
     if ( spage->sharing->handle != sh )
         goto err_unlock;
 
-    /* Make sure the target page is a hole in the physmap */
-    if ( mfn_valid(cmfn) ||
-         (!(p2m_is_ram(cmfn_type))) )
+    /* Make sure the target page is a hole in the physmap. These are typically
+     * p2m_mmio_dm, but also accept p2m_invalid and paged out pages. See the
+     * definition of p2m_is_hole in p2m.h. */
+    if ( !p2m_is_hole(cmfn_type) || mfn_valid(cmfn) )

As I said last time, the mfn_valid test is wrong for everything except p2m_paging_in.

I've checked in a reduced version of this that just drops p2m_paging_in from P2M_HOLE_TYPES (so the pager wins this race instead of the sharer) which I think will do for 4.2:
http://xenbits.xen.org/hg/staging/xen-unstable.hg/rev/25bdef4493ae
can you check that it makes sense, please?

Cheers,

Tim.

#2 lore raw
Quoted 8 lines
Quoted 8 lines

diff -r f83397dfb6c0 -r 3169fba29613 xen/arch/x86/mm/mem_sharing.c

xen/arch/x86/mm/mem_sharing.c +0 -0
@@ -1073,9 +1073,10 @@ int mem_sharing_add_to_physmap(struct do
     if ( spage->sharing->handle != sh )
         goto err_unlock;
 
Quoted 12 lines

xen/arch/x86/mm/mem_sharing.c

Quoted 9 lines
xen/arch/x86/mm/mem_sharing.c +1 -3
-    /* Make sure the target page is a hole in the physmap */
-    if ( mfn_valid(cmfn) ||
-         (!(p2m_is_ram(cmfn_type))) )
+    /* Make sure the target page is a hole in the physmap. These are

typically
+ * p2m_mmio_dm, but also accept p2m_invalid and paged out pages.
See the

xen/arch/x86/mm/mem_sharing.c +2 -0
+     * definition of p2m_is_hole in p2m.h. */
+    if ( !p2m_is_hole(cmfn_type) || mfn_valid(cmfn) )

As I said last time, the mfn_valid test is wrong for everything except p2m_paging_in.

I am sorry. I have been testing all along -- and therefore sent -- the wrong patch. Can we please revert this? My sloppiness.

Quoted 6 lines

I've checked in a reduced version of this that just drops p2m_paging_in from P2M_HOLE_TYPES (so the pager wins this race instead of the sharer) which I think will do for 4.2:
http://xenbits.xen.org/hg/staging/xen-unstable.hg/rev/25bdef4493ae
can you check that it makes sense, please?

Unfortunately not. The way it's been checked in, it nullifies the ability to plug in a paging fault with a shared page -- which was healthily working before.

I believe the fix we'll converge to is keep paging_in in the "hole" set of types, and remove the check for valid mfn. Something along the lines of what I've pasted below. But no more sloppinees, I'll do some testing and hopefully submit the right thing!

Thanks
Andres

# HG changeset patch
# Parent 9fc0252536f0a4ddf48b7ec9cd7a7243271545f8 x86/mem_sharing: Rectify test for "empty" physmap entry in sharing_add_to_physmap.

xen/arch/x86/mm/mem_sharing.c +1 -3
diff -r 9fc0252536f0 xen/arch/x86/mm/mem_sharing.c
@@ -1073,9 +1073,10 @@ int mem_sharing_add_to_physmap(struct do
     if ( spage->sharing->handle != sh )
         goto err_unlock;
 
-    /* Make sure the target page is a hole in the physmap */
-    if ( mfn_valid(cmfn) ||
-         (!(p2m_is_ram(cmfn_type))) )
+    /* Make sure the target page is a hole in the physmap. These are

typically
+ * p2m_mmio_dm, but also accept p2m_invalid and paged out pages. See the
+ * definition of p2m_is_hole in p2m.h. */
+ if ( !p2m_is_hole(cmfn_type) )

     {
         ret = XENMEM_SHARING_OP_C_HANDLE_INVALID;
         goto err_unlock;
xen/include/asm-x86/p2m.h +8 -0
diff -r 9fc0252536f0 xen/include/asm-x86/p2m.h
@@ -133,6 +133,13 @@ typedef unsigned int p2m_query_t;
                        | p2m_to_mask(p2m_ram_paging_in)       \
                        | p2m_to_mask(p2m_ram_shared))
 
+/* Types that represent a physmap hole that is ok to replace with a shared
+ * entry */
+#define P2M_HOLE_TYPES (p2m_to_mask(p2m_mmio_dm)        \
+                       | p2m_to_mask(p2m_invalid)       \
+                       | p2m_to_mask(p2m_ram_paging_in) \
+                       | p2m_to_mask(p2m_ram_paged))
+
 /* Grant mapping types, which map to a real machine frame in another
  * VM */
 #define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw)  \
@@ -173,6 +180,7 @@ typedef unsigned int p2m_query_t;
 
 /* Useful predicates */
 #define p2m_is_ram(_t) (p2m_to_mask(_t) & P2M_RAM_TYPES)
+#define p2m_is_hole(_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)
 #define p2m_is_mmio(_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)
 #define p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES)
 #define p2m_is_magic(_t) (p2m_to_mask(_t) & P2M_MAGIC_TYPES)
Quoted 5 lines

Cheers,

Tim.

#3 lore raw
Quoted 3 lines · Andres Lagar-Cavilla

I believe the fix we'll converge to is keep paging_in in the "hole" set of types, and remove the check for valid mfn. Something along the lines of what I've pasted below.

OK. Please send it as a diff against what's already applied - I think what we have now is more correct (if less useful) than what we had before.

Do you have to worry about freeing the page as well? Will it otherwise be leaked into a state where it's allocated but not in the p2m? I see that guest_physmap_add_entry() doesn't free paging_in pages but maybe that's wrong too?

Cheers,

Tim.

Quoted 58 lines

# HG changeset patch
# Parent 9fc0252536f0a4ddf48b7ec9cd7a7243271545f8 x86/mem_sharing: Rectify test for "empty" physmap entry in sharing_add_to_physmap.

diff -r 9fc0252536f0 xen/arch/x86/mm/mem_sharing.c

xen/arch/x86/mm/mem_sharing.c +1 -3
@@ -1073,9 +1073,10 @@ int mem_sharing_add_to_physmap(struct do
     if ( spage->sharing->handle != sh )
         goto err_unlock;
 
-    /* Make sure the target page is a hole in the physmap */
-    if ( mfn_valid(cmfn) ||
-         (!(p2m_is_ram(cmfn_type))) )
+    /* Make sure the target page is a hole in the physmap. These are

typically

xen/arch/x86/mm/mem_sharing.c +3 -0
+     * p2m_mmio_dm, but also accept p2m_invalid and paged out pages. See the
+     * definition of p2m_is_hole in p2m.h. */
+    if ( !p2m_is_hole(cmfn_type) )
     {
         ret = XENMEM_SHARING_OP_C_HANDLE_INVALID;
         goto err_unlock;

diff -r 9fc0252536f0 xen/include/asm-x86/p2m.h

xen/include/asm-x86/p2m.h +8 -0
@@ -133,6 +133,13 @@ typedef unsigned int p2m_query_t;
                        | p2m_to_mask(p2m_ram_paging_in)       \
                        | p2m_to_mask(p2m_ram_shared))
 
+/* Types that represent a physmap hole that is ok to replace with a shared
+ * entry */
+#define P2M_HOLE_TYPES (p2m_to_mask(p2m_mmio_dm)        \
+                       | p2m_to_mask(p2m_invalid)       \
+                       | p2m_to_mask(p2m_ram_paging_in) \
+                       | p2m_to_mask(p2m_ram_paged))
+
 /* Grant mapping types, which map to a real machine frame in another
  * VM */
 #define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw)  \
@@ -173,6 +180,7 @@ typedef unsigned int p2m_query_t;
 
 /* Useful predicates */
 #define p2m_is_ram(_t) (p2m_to_mask(_t) & P2M_RAM_TYPES)
+#define p2m_is_hole(_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)
 #define p2m_is_mmio(_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)
 #define p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES)
 #define p2m_is_magic(_t) (p2m_to_mask(_t) & P2M_MAGIC_TYPES)
Quoted 6 lines

Cheers,

Tim.

#4 lore raw
Quoted 9 lines
Quoted 4 lines · Andres Lagar-Cavilla

I believe the fix we'll converge to is keep paging_in in the "hole" set of
types, and remove the check for valid mfn. Something along the lines of what I've pasted below.

OK. Please send it as a diff against what's already applied - I think what we have now is more correct (if less useful) than what we had before.

Done. Tested and pasted below. Hopefully you can apply it before end-of-day. Works for all known users of sharing+paging ;)

Quoted 5 lines

Do you have to worry about freeing the page as well? Will it otherwise be leaked into a state where it's allocated but not in the p2m? I see that guest_physmap_add_entry() doesn't free paging_in pages but maybe that's wrong too?

Exactly. Leaked out of the p2m. Will still be cleaned up properly on domain destruction.

The patch below takes care of the leak for sharing_add_to_physmap. However I am not touching guest_physmap_add_entry -- it's been that way for pretty much forever. My observation is that guest_physmap_add_entry is mostly called from hypercalls (grant, XENMEM), so the domain is leaking its own memory and risking running against the max_pages limit sooner, if being sloppy. The only exception is populate physmap, which maybe should be looked into.

Thanks,
Andres

# HG changeset patch
# Parent 485cd11f131da88b286b3b64e8f095508b67ab0b x86/mem_sharing: Re-rectify sharing add to physmap hole test.

xen/arch/x86/mm/mem_sharing.c +0 -0
diff -r 485cd11f131d xen/arch/x86/mm/mem_sharing.c
@@ -1103,7 +1103,17 @@ int mem_sharing_add_to_physmap(struct do
         ret = 0;
         /* There is a chance we're plugging a hole where a paged out page

was */

         if ( p2m_is_paging(cmfn_type) && (cmfn_type != p2m_ram_paging_out) )
+        {
             atomic_dec(&cd->paged_pages);

+ /* Further, there is a chance this was a valid page. Don't
leak it. */
+ if ( mfn_valid(cmfn) )
+ {

+                struct page_info *cpage = mfn_to_page(cmfn);
+                ASSERT(cpage != NULL);
+                if(test_and_clear_bit(_PGC_allocated, &cpage->count_info))
+                    put_page(cpage);
+            }
+        }
     }

     atomic_inc(&nr_saved_mfns);
xen/include/asm-x86/p2m.h +1 -0
diff -r 485cd11f131d xen/include/asm-x86/p2m.h
@@ -137,6 +137,7 @@ typedef unsigned int p2m_query_t;
  * entry */
 #define P2M_HOLE_TYPES (p2m_to_mask(p2m_mmio_dm)        \
                        | p2m_to_mask(p2m_invalid)       \
+                       | p2m_to_mask(p2m_ram_paging_in) \
                        | p2m_to_mask(p2m_ram_paged))
 
 /* Grant mapping types, which map to a real machine frame in another

guest_p

Quoted 66 lines

Cheers,

Tim.

Quoted 60 lines

# HG changeset patch
# Parent 9fc0252536f0a4ddf48b7ec9cd7a7243271545f8 x86/mem_sharing: Rectify test for "empty" physmap entry in sharing_add_to_physmap.

diff -r 9fc0252536f0 xen/arch/x86/mm/mem_sharing.c

xen/arch/x86/mm/mem_sharing.c +1 -3
@@ -1073,9 +1073,10 @@ int mem_sharing_add_to_physmap(struct do
     if ( spage->sharing->handle != sh )
         goto err_unlock;
 
-    /* Make sure the target page is a hole in the physmap */
-    if ( mfn_valid(cmfn) ||
-         (!(p2m_is_ram(cmfn_type))) )
+    /* Make sure the target page is a hole in the physmap. These are

typically
+ * p2m_mmio_dm, but also accept p2m_invalid and paged out pages.
See the

xen/arch/x86/mm/mem_sharing.c +2 -0
+     * definition of p2m_is_hole in p2m.h. */
+    if ( !p2m_is_hole(cmfn_type) )
     {
         ret = XENMEM_SHARING_OP_C_HANDLE_INVALID;
         goto err_unlock;

diff -r 9fc0252536f0 xen/include/asm-x86/p2m.h

xen/include/asm-x86/p2m.h +1 -0
@@ -133,6 +133,13 @@ typedef unsigned int p2m_query_t;
                        | p2m_to_mask(p2m_ram_paging_in)       \
                        | p2m_to_mask(p2m_ram_shared))
 
+/* Types that represent a physmap hole that is ok to replace with a

shared
+ * entry */

xen/include/asm-x86/p2m.h +6 -0
+#define P2M_HOLE_TYPES (p2m_to_mask(p2m_mmio_dm)        \
+                       | p2m_to_mask(p2m_invalid)       \
+                       | p2m_to_mask(p2m_ram_paging_in) \
+                       | p2m_to_mask(p2m_ram_paged))
+
 /* Grant mapping types, which map to a real machine frame in another
  * VM */
 #define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw)  \
@@ -173,6 +180,7 @@ typedef unsigned int p2m_query_t;
 
 /* Useful predicates */
 #define p2m_is_ram(_t) (p2m_to_mask(_t) & P2M_RAM_TYPES)
+#define p2m_is_hole(_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)
 #define p2m_is_mmio(_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)
 #define p2m_is_readonly(_t) (p2m_to_mask(_t) & P2M_RO_TYPES)
 #define p2m_is_magic(_t) (p2m_to_mask(_t) & P2M_MAGIC_TYPES)
Quoted 6 lines

Cheers,

Tim.

Applied
#5 lore raw
Quoted 5 lines · Andres Lagar-Cavilla

# HG changeset patch
# Parent 485cd11f131da88b286b3b64e8f095508b67ab0b x86/mem_sharing: Re-rectify sharing add to physmap hole test.

Applied, thanks. I gave it a slightly fuller description and fixed some whitespace around parentheses on the way past.

I think that empties the queue, at least of things that are wanted for 4.2.

Cheers,

Tim.

#6 lore raw

Thanks. Yeah, queue empty. Waiting for ghouls awakened by rc testing. That almost never happens of course :) Andres

Quoted 15 lines · Tim Deegan
Quoted 5 lines · Andres Lagar-Cavilla

# HG changeset patch
# Parent 485cd11f131da88b286b3b64e8f095508b67ab0b x86/mem_sharing: Re-rectify sharing add to physmap hole test.

Applied, thanks. I gave it a slightly fuller description and fixed some whitespace around parentheses on the way past.

I think that empties the queue, at least of things that are wanted for 4.2.

Cheers,

Tim.

#7 lore raw
In reply to Tim Deegan
Quoted 5 lines
Quoted 4 lines · Andres Lagar-Cavilla

# HG changeset patch
# Parent 485cd11f131da88b286b3b64e8f095508b67ab0b x86/mem_sharing: Re-rectify sharing add to physmap hole test.

Quoted 7 lines
Quoted 1 line

Applied, thanks. I gave it a slightly fuller description and fixed some whitespace around parentheses on the way past.

I think that empties the queue, at least of things that are wanted for 4.2.

Actually, patch 6 of this series posted a while ago
http://lists.xen.org/archives/html/xen-devel/2012-04/msg01364.html
http://lists.xen.org/archives/html/xen-devel/2012-04/msg00962.html

turns on locking p2m queries for shadow mode and seemingly you've acked it. This is good to go afaict and would be a shame to be left off of 4.2.

Andres

Quoted 5 lines

Cheers,

Tim.

#8 lore raw
Quoted 13 lines · Andres Lagar-Cavilla
Quoted 5 lines
Quoted 4 lines · Andres Lagar-Cavilla

# HG changeset patch
# Parent 485cd11f131da88b286b3b64e8f095508b67ab0b x86/mem_sharing: Re-rectify sharing add to physmap hole test.

Quoted 7 lines
Quoted 1 line

Applied, thanks. I gave it a slightly fuller description and fixed some whitespace around parentheses on the way past.

I think that empties the queue, at least of things that are wanted for 4.2.

Quoted 6 lines

Actually, patch 6 of this series posted a while ago
http://lists.xen.org/archives/html/xen-devel/2012-04/msg01364.html
http://lists.xen.org/archives/html/xen-devel/2012-04/msg00962.html

turns on locking p2m queries for shadow mode and seemingly you've acked it. This is good to go afaict and would be a shame to be left off of 4.2.

I think I'll leave it out for now. Shadow doesn't coexist with paging/sharing/memattr and already has its own interlocks against everything else so it's not a bugfix.

Cheers,

Tim.