# HG changeset patch
# User Ian Campbell <ian.campbell@citrix.com>
# Date 1343123936 -3600
# Node ID 48ce1f45392708a70723e99fa80947958ae69732
# Parent  c6eb61ed6f04b4079525c3944b5a55268e1db4f1
xen: only check for shared pages while any exist on teardown

Avoids worst case behavour when guest has a large p2m.

This is XSA-11 / CVE-2012-nnn

Signed-off-by: Tim Deegan <tim@xen.org>
Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Tested-by: Olaf Hering <olaf@aepfle.de>

diff -r c6eb61ed6f04 -r 48ce1f453927 xen/arch/x86/mm/p2m.c
--- a/xen/arch/x86/mm/p2m.c	Mon May 14 17:02:16 2012 +0100
+++ b/xen/arch/x86/mm/p2m.c	Tue Jul 24 10:58:56 2012 +0100
@@ -1725,6 +1725,8 @@ void p2m_teardown(struct domain *d)
 #ifdef __x86_64__
     for ( gfn=0; gfn < p2m->max_mapped_pfn; gfn++ )
     {
+        if ( atomic_read(&d->shr_pages) == 0 )
+            break;
         mfn = p2m->get_entry(d, gfn, &t, p2m_query);
         if ( mfn_valid(mfn) && (t == p2m_ram_shared) )
             BUG_ON(mem_sharing_unshare_page(d, gfn, MEM_SHARING_DESTROY_GFN));
