From: Tim Deegan Subject: Re: [PATCH] x86 shadow: fix race when domain is dying Date: Thu, 26 Nov 2009 11:07:46 +0000 Message-ID: <20091126110746.GF14284@whitby.uk.xensource.com> References: <7kvdgxya1h.fsf@pingu.sky.yk.fujitsu.co.jp> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="HG+GLK89HZ1zG0kk" Return-path: Content-Disposition: inline In-Reply-To: <7kvdgxya1h.fsf@pingu.sky.yk.fujitsu.co.jp> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Kouya Shimura Cc: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org --HG+GLK89HZ1zG0kk Content-Type: text/plain; charset="iso-8859-1" Content-Disposition: inline Hi, At 08:17 +0000 on 26 Nov (1259223466), Kouya Shimura wrote: > There are some cases that shadow_write_p2m_entry() is called after > the domain is killed. It causes Xen to crash. Thanks for catching this! I'm afraid your fix opens a different race window, though: any p2m operation that happens after d->is_dying is set but before p2m_teardown() will corrupt the p2m (because the entry wouldn't actually get written). If it also happens before shadow_teardown() it could break the invariants of the shadow pagetables, possibly causing a crash when shadow_teardown() is reached. The right fix is to test for whether shadow_teardown() has been called, and if so, call safe_write_entry() without trying to fix up the shadows. I've attached a patch. Cheers, Tim. -- Tim Deegan Principal Software Engineer, Citrix Systems (R&D) Ltd. [Company #02300071, SL9 0DZ, UK.] --HG+GLK89HZ1zG0kk Content-Type: text/plain; charset="iso-8859-1" Content-Disposition: attachment; filename="late-p2m-entry-write" x86 shadow: don't try to unsshadow for p2m changes after the shadows have been torn down. Signed-off-by: Tim Deegan diff -r d62bede476cb xen/arch/x86/mm/shadow/common.c --- a/xen/arch/x86/mm/shadow/common.c Mon Nov 23 15:19:38 2009 +0000 +++ b/xen/arch/x86/mm/shadow/common.c Thu Nov 26 10:49:35 2009 +0000 @@ -3440,14 +3440,12 @@ * with new content. It is responsible for update the entry, as well as other * shadow processing jobs. */ -void -shadow_write_p2m_entry(struct vcpu *v, unsigned long gfn, - l1_pgentry_t *p, mfn_t table_mfn, - l1_pgentry_t new, unsigned int level) + +static void sh_unshadow_for_p2m_change(struct vcpu *v, unsigned long gfn, + l1_pgentry_t *p, mfn_t table_mfn, + l1_pgentry_t new, unsigned int level) { struct domain *d = v->domain; - - shadow_lock(d); /* If we're removing an MFN from the p2m, remove it from the shadows too */ if ( level == 1 ) @@ -3503,6 +3501,21 @@ unmap_domain_page(npte); } } +} + +void +shadow_write_p2m_entry(struct vcpu *v, unsigned long gfn, + l1_pgentry_t *p, mfn_t table_mfn, + l1_pgentry_t new, unsigned int level) +{ + struct domain *d = v->domain; + + shadow_lock(d); + + /* If there are any shadows, update them. But if shadow_teardown() + * has already been called then it's not safe to try. */ + if ( likely(d->arch.paging.shadow.total_pages != 0) ) + sh_unshadow_for_p2m_change(v, gfn, p, table_mfn, new, level); /* Update the entry with new content */ safe_write_pte(p, new); --HG+GLK89HZ1zG0kk Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --HG+GLK89HZ1zG0kk--