Skip to contentSkip to conversation

xen: avoid race in p2m handling

PATCH Superseded Juergen Gross 2 participants 4 messages Oct 16, 2014 – Oct 17, 2014

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

PATCH 1/1 xen: avoid race in p2m handling

When a new p2m leaf is allocated this leaf is linked into the p2m tree via cmpxchg. Unfortunately the compare value for checking the success of the update is read after checking for the need of a new leaf. It is possible that a new leaf has been linked into the tree concurrently in between. This could lead to a leaked memory page and to the loss of some p2m entries.

Avoid the race by using the read compare value for checking the need of a new p2m leaf.

---
 arch/x86/xen/p2m.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/xen/p2m.c +3 -3
index d1b3da2..8c1a278 100644
@@ -532,6 +532,7 @@ static bool alloc_p2m(unsigned long pfn)
 	unsigned topidx, mididx;
 	unsigned long ***top_p, **mid;
 	unsigned long *top_mfn_p, *mid_mfn;
+	unsigned long *p2m_orig;
 
 	topidx = p2m_top_index(pfn);
 	mididx = p2m_mid_index(pfn);
@@ -579,11 +580,10 @@ static bool alloc_p2m(unsigned long pfn)
 		}
 	}
 
-	if (p2m_top[topidx][mididx] == p2m_identity ||
-	    p2m_top[topidx][mididx] == p2m_missing) {
+	p2m_orig = p2m_top[topidx][mididx];
+	if (p2m_orig == p2m_identity || p2m_orig == p2m_missing) {
 		/* p2m leaf page is missing */
 		unsigned long *p2m;
-		unsigned long *p2m_orig = p2m_top[topidx][mididx];
 
 		p2m = alloc_p2m_page();
 		if (!p2m)
Signature · 1 line
1.8.4.5
#1 lore raw
Quoted 9 lines · Juergen Gross

When a new p2m leaf is allocated this leaf is linked into the p2m tree via cmpxchg. Unfortunately the compare value for checking the success of the update is read after checking for the need of a new leaf. It is possible that a new leaf has been linked into the tree concurrently in between. This could lead to a leaked memory page and to the loss of some p2m entries.

Avoid the race by using the read compare value for checking the need of a new p2m leaf.

[...]

arch/x86/xen/p2m.c static bool alloc_p2m(unsigned long pfn) · line 583

Quoted 7 lines
arch/x86/xen/p2m.c +1 -2
@@ -579,11 +580,10 @@ static bool alloc_p2m(unsigned long pfn)
 		}
 	}
 
-	if (p2m_top[topidx][mididx] == p2m_identity ||
-	    p2m_top[topidx][mididx] == p2m_missing) {
+	p2m_orig = p2m_top[topidx][mididx];

Do you need to use ACCESS_ONCE() here?

David

author maintainer
#2 lore raw
Quoted 15 lines · David Vrabel
Quoted 9 lines · Juergen Gross

When a new p2m leaf is allocated this leaf is linked into the p2m tree via cmpxchg. Unfortunately the compare value for checking the success of the update is read after checking for the need of a new leaf. It is possible that a new leaf has been linked into the tree concurrently in between. This could lead to a leaked memory page and to the loss of some p2m entries.

Avoid the race by using the read compare value for checking the need of a new p2m leaf.

[...]

Quoted 4 lines
(quoted diff) +0 -0
@@ -579,11 +580,10 @@ static bool alloc_p2m(unsigned long pfn)
  		}
  	}
 
Quoted 5 lines

arch/x86/xen/p2m.c static bool alloc_p2m(unsigned long pfn) · line 583

Quoted 3 lines
arch/x86/xen/p2m.c +1 -2
-	if (p2m_top[topidx][mididx] == p2m_identity ||
-	    p2m_top[topidx][mididx] == p2m_missing) {
+	p2m_orig = p2m_top[topidx][mididx];

Do you need to use ACCESS_ONCE() here?

Yes, you are probably right. Should I send a new patch or do you want to modify it?

Juergen

#3 lore raw
Quoted 16 lines · Juergen Gross
Quoted 15 lines · David Vrabel
Quoted 9 lines · Juergen Gross

When a new p2m leaf is allocated this leaf is linked into the p2m tree via cmpxchg. Unfortunately the compare value for checking the success of the update is read after checking for the need of a new leaf. It is possible that a new leaf has been linked into the tree concurrently in between. This could lead to a leaked memory page and to the loss of some p2m entries.

Avoid the race by using the read compare value for checking the need of a new p2m leaf.

[...]

Quoted 4 lines
(quoted diff) +0 -0
@@ -579,11 +580,10 @@ static bool alloc_p2m(unsigned long pfn)
          }
      }
 
Quoted 8 lines
Quoted 5 lines

arch/x86/xen/p2m.c static bool alloc_p2m(unsigned long pfn) · line 583

Quoted 3 lines
arch/x86/xen/p2m.c +1 -2
-    if (p2m_top[topidx][mididx] == p2m_identity ||
-        p2m_top[topidx][mididx] == p2m_missing) {
+    p2m_orig = p2m_top[topidx][mididx];

Do you need to use ACCESS_ONCE() here?

Yes, you are probably right. Should I send a new patch or do you want to modify it?

Can you go through and see if there are any other places in the p2m code that also need ACCESS_ONCE()? And then repost, thanks!

David