2014-10-17 11:16:13

by Jürgen Groß

[permalink] [raw]
Subject: [PATCH V2] 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 and use ACCESS_ONCE() to get it.

There are other places which seem to need ACCESS_ONCE() to ensure
proper operation. Change them accordingly.

Signed-off-by: Juergen Gross <[email protected]>
---
arch/x86/xen/p2m.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/x86/xen/p2m.c b/arch/x86/xen/p2m.c
index d1b3da2..b456b04 100644
--- a/arch/x86/xen/p2m.c
+++ b/arch/x86/xen/p2m.c
@@ -532,12 +532,13 @@ 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);

top_p = &p2m_top[topidx];
- mid = *top_p;
+ mid = ACCESS_ONCE(*top_p);

if (mid == p2m_mid_missing) {
/* Mid level is missing, allocate a new one */
@@ -552,7 +553,7 @@ static bool alloc_p2m(unsigned long pfn)
}

top_mfn_p = &p2m_top_mfn[topidx];
- mid_mfn = p2m_top_mfn_p[topidx];
+ mid_mfn = ACCESS_ONCE(p2m_top_mfn_p[topidx]);

BUG_ON(virt_to_mfn(mid_mfn) != *top_mfn_p);

@@ -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 = ACCESS_ONCE(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)
--
1.8.4.5


2014-10-17 12:35:22

by David Vrabel

[permalink] [raw]
Subject: Re: [Xen-devel] [PATCH V2] xen: avoid race in p2m handling

On 17/10/14 12:16, Juergen Gross wrote:
> 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 and use ACCESS_ONCE() to get it.
>
> There are other places which seem to need ACCESS_ONCE() to ensure
> proper operation. Change them accordingly.

Applied, thanks.

David