From patchwork Wed Jul 6 15:51:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corneliu ZUZU X-Patchwork-Id: 9216575 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B0EA260467 for ; Wed, 6 Jul 2016 15:54:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A14DF26E8A for ; Wed, 6 Jul 2016 15:54:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 940AB2711E; Wed, 6 Jul 2016 15:54:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B551B26E8A for ; Wed, 6 Jul 2016 15:54:49 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bKp7x-0001a4-0M; Wed, 06 Jul 2016 15:52:41 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bKp7v-0001Zj-Po for xen-devel@lists.xen.org; Wed, 06 Jul 2016 15:52:39 +0000 Received: from [193.109.254.147] by server-14.bemta-14.messagelabs.com id 0D/3F-27600-7492D775; Wed, 06 Jul 2016 15:52:39 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrJIsWRWlGSWpSXmKPExsUSfTxjoa6bZm2 4wcIlihZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8bWU0fYCg57V+z/fIKxgXGpdRcjJ4eQgJvE sg+bWboYuYDsNYwSE29tZ4JwTgI57xezdjFyADnuEqcfikI0rGKU2NMkBGKzCWhLnDt0jwnEF hGQlrj2+TIjSC+zwB9Gif1HOhhBEsICyRILb54Es1kEVCV+XG5jA7F5BVwlOv+3s4DMlxCQk1 hwIR3E5AQ66M7jaohVrhKLFl5kBrElBHIktp56wAhRLSXxv1UJZJOEwFIWia7Ny5ggamQkHk2 8yTaBUWgBI8MqRo3i1KKy1CJdIxO9pKLM9IyS3MTMHF1DQxO93NTi4sT01JzEpGK95PzcTYzA IKxnYGDcwfjzkt8hRkkOJiVRXpZv1eFCfEn5KZUZicUZ8UWlOanFhxhlODiUJHitNWrDhQSLU tNTK9Iyc4DxAJOW4OBREuENAUnzFhck5hZnpkOkTjEqSonzcoEkBEASGaV5cG2wGLzEKCslzM vIwMAgxFOQWpSbWYIq/4pRnINRSZjXDmQKT2ZeCdz0V0CLmYAW/3SpBllckoiQkmpg1JZ7L/+ zehFPenaq27t4UfYrvVd2eAiFyzwS2X9bRftGsIhF0qZLfGse71GaUPTa5alHNB//7fofLZ1X oid+v6wgvDP0vNpDr6rEzkNGb2S0LywvNLNtaC44n8vf0Lul2KNlvpaB6NHu38Jx0eyXGrdGl 6mIm6849UZVrN20kk/GR5/j/GMlluKMREMt5qLiRACA6P+fvAIAAA== X-Env-Sender: czuzu@bitdefender.com X-Msg-Ref: server-12.tower-27.messagelabs.com!1467820357!52248959!1 X-Originating-IP: [91.199.104.161] X-SpamReason: No, hits=0.5 required=7.0 tests=BODY_RANDOM_LONG X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 11241 invoked from network); 6 Jul 2016 15:52:38 -0000 Received: from mx01.bbu.dsd.mx.bitdefender.com (HELO mx01.bbu.dsd.mx.bitdefender.com) (91.199.104.161) by server-12.tower-27.messagelabs.com with DHE-RSA-AES128-GCM-SHA256 encrypted SMTP; 6 Jul 2016 15:52:38 -0000 Received: (qmail 3449 invoked from network); 6 Jul 2016 18:52:37 +0300 Received: from unknown (HELO mx-sr.buh.bitdefender.com) (10.17.80.103) by mx01.bbu.dsd.mx.bitdefender.com with AES256-GCM-SHA384 encrypted SMTP; 6 Jul 2016 18:52:37 +0300 Received: from smtp03.buh.bitdefender.org (unknown [10.17.80.77]) by mx-sr.buh.bitdefender.com (Postfix) with ESMTP id 4AA657FC00 for ; Wed, 6 Jul 2016 18:52:37 +0300 (EEST) Received: (qmail 22236 invoked from network); 6 Jul 2016 18:52:37 +0300 Received: from unknown (HELO czuzu-dev-vm.clj.bitdefender.biz) (czuzu@bitdefender.com@91.199.104.243) by smtp03.buh.bitdefender.org with SMTP; 6 Jul 2016 18:52:32 +0300 From: Corneliu ZUZU To: xen-devel@lists.xen.org Date: Wed, 6 Jul 2016 18:51:39 +0300 Message-Id: <1467820299-13403-1-git-send-email-czuzu@bitdefender.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1467820154-13256-1-git-send-email-czuzu@bitdefender.com> References: <1467820154-13256-1-git-send-email-czuzu@bitdefender.com> X-BitDefender-Scanner: Clean, Agent: BitDefender qmail 3.1.6 on smtp03.buh.bitdefender.org, sigver: 7.66204 X-BitDefender-Spam: No (0) X-BitDefender-SpamStamp: Build: [Engines: 2.15.6.911, Dats: 425554, Stamp: 3], Multi: [Enabled, t: (0.000010, 0.008537)], BW: [Enabled, t: (0.000006,0.000001)], RBL DNSBL: [Disabled], APM: [Enabled, Score: 500, t: (0.006406), Flags: BB9BAF5C; NN_NO_CONTENT_TYPE; NN_LEGIT_SUMM_400_WORDS; NN_NO_LINK_NMD; NN_LEGIT_BITDEFENDER; NN_LEGIT_S_SQARE_BRACKETS; NN_LEGIT_MAILING_LIST_TO], SGN: [Enabled, t: (0.011452)], URL: [Enabled, t: (0.000004)], RTDA: [Enabled, t: (4.430996), Hit: No, Details: v2.3.10; Id: 2m1ghhc.1amgks2k3.q8rq], total: 0(775) X-BitDefender-CF-Stamp: none Cc: Tamas K Lengyel , Razvan Cojocaru , George Dunlap , Andrew Cooper , Paul Durrant , Jan Beulich Subject: [Xen-devel] [PATCH v3 3/8] x86/vm-event/monitor: don't compromise monitor_write_data on domain cleanup X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP The arch_vm_event structure is dynamically allocated and freed @ vm_event_cleanup_domain. This cleanup is triggered e.g. when the toolstack user disables domain monitoring (xc_monitor_disable), which in turn effectively discards any information that was in arch_vm_event.write_data. But this can yield unexpected behavior since if a CR-write was awaiting to be committed on the scheduling tail (hvm_do_resume->arch_monitor_write_data) before xc_monitor_disable is called, then the domain CR write is wrongfully ignored, which of course, in these cases, can easily render a domain crash. To fix the issue, this patch: - makes arch_vm_event.emul_read_data dynamically allocated - in vm_event_cleanup_domain, if there are still uncommitted writes in arch_vm_event.write_data: - only frees emul_read_data - defers xfree of the entire arch_vm_event until vcpu/domain destroyal - otherwise arch_vm_event is freed in vm_event_cleanup_domain, as before For clarity, also introduce inline functions that check initialisation of the vm_event subsystem for a vcpu/domain (vm_event_{vcpu,domain}_initialised), since that is now true only when both arch_vm_event and arch_vm_event.emul_read_data are non-NULL. Signed-off-by: Corneliu ZUZU --- Changed since v2: * introduce vm_event_{vcpu,domain}_initialised inline functions for clarity * xfree arch_vm_event in vm_event_cleanup_domain as before if there are no uncommitted writes in arch_vm_event.write_data --- xen/arch/x86/domain.c | 9 +++++++-- xen/arch/x86/hvm/emulate.c | 6 +++--- xen/arch/x86/hvm/hvm.c | 2 ++ xen/arch/x86/mm/p2m.c | 2 +- xen/arch/x86/vm_event.c | 35 +++++++++++++++++++++++++++++------ xen/common/vm_event.c | 12 ++++++++++++ xen/include/asm-x86/domain.h | 17 +++++++++++------ xen/include/asm-x86/monitor.h | 3 ++- xen/include/asm-x86/vm_event.h | 13 ++++++++++++- 9 files changed, 79 insertions(+), 20 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index bb59247..0313208 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -492,8 +493,12 @@ int vcpu_initialise(struct vcpu *v) void vcpu_destroy(struct vcpu *v) { - xfree(v->arch.vm_event); - v->arch.vm_event = NULL; + if ( unlikely(v->arch.vm_event) ) + { + xfree(v->arch.vm_event->emul_read_data); + xfree(v->arch.vm_event); + v->arch.vm_event = NULL; + } if ( is_pv_32bit_vcpu(v) ) { diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index 855af4d..59e2344 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -73,12 +73,12 @@ static int set_context_data(void *buffer, unsigned int size) { struct vcpu *curr = current; - if ( curr->arch.vm_event ) + if ( vm_event_vcpu_initialised(curr) ) { unsigned int safe_size = - min(size, curr->arch.vm_event->emul_read_data.size); + min(size, curr->arch.vm_event->emul_read_data->size); - memcpy(buffer, curr->arch.vm_event->emul_read_data.data, safe_size); + memcpy(buffer, curr->arch.vm_event->emul_read_data->data, safe_size); memset(buffer + safe_size, 0, size - safe_size); return X86EMUL_OKAY; } diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index e3829d2..ac6d9eb 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -479,6 +479,8 @@ void hvm_do_resume(struct vcpu *v) { enum emul_kind kind = EMUL_KIND_NORMAL; + ASSERT(v->arch.vm_event->emul_read_data); + if ( v->arch.vm_event->emulate_flags & VM_EVENT_FLAG_SET_EMUL_READ_DATA ) kind = EMUL_KIND_SET_CONTEXT; diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c index 16733a4..6616626 100644 --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -1642,7 +1642,7 @@ void p2m_mem_access_emulate_check(struct vcpu *v, v->arch.vm_event->emulate_flags = violation ? rsp->flags : 0; if ( (rsp->flags & VM_EVENT_FLAG_SET_EMUL_READ_DATA) ) - v->arch.vm_event->emul_read_data = rsp->data.emul_read_data; + *v->arch.vm_event->emul_read_data = rsp->data.emul_read_data; } } diff --git a/xen/arch/x86/vm_event.c b/xen/arch/x86/vm_event.c index 80f84d6..ff2ba92 100644 --- a/xen/arch/x86/vm_event.c +++ b/xen/arch/x86/vm_event.c @@ -30,12 +30,18 @@ int vm_event_init_domain(struct domain *d) for_each_vcpu ( d, v ) { - if ( v->arch.vm_event ) + if ( likely(!v->arch.vm_event) ) + { + v->arch.vm_event = xzalloc(struct arch_vm_event); + if ( !v->arch.vm_event ) + return -ENOMEM; + } + else if ( unlikely(v->arch.vm_event->emul_read_data) ) continue; - v->arch.vm_event = xzalloc(struct arch_vm_event); - - if ( !v->arch.vm_event ) + v->arch.vm_event->emul_read_data = + xzalloc(struct vm_event_emul_read_data); + if ( !v->arch.vm_event->emul_read_data ) return -ENOMEM; } @@ -52,8 +58,25 @@ void vm_event_cleanup_domain(struct domain *d) for_each_vcpu ( d, v ) { - xfree(v->arch.vm_event); - v->arch.vm_event = NULL; + if ( likely(!v->arch.vm_event) ) + continue; + + /* + * Only xfree the entire arch_vm_event if write_data was fully handled. + * Otherwise defer entire xfree until domain/vcpu destroyal. + */ + if ( likely(!v->arch.vm_event->write_data.uncommitted_writes) ) + { + xfree(v->arch.vm_event->emul_read_data); + xfree(v->arch.vm_event); + v->arch.vm_event = NULL; + continue; + } + + /* write_data not fully handled, only xfree emul_read_data */ + v->arch.vm_event->emulate_flags = 0; + xfree(v->arch.vm_event->emul_read_data); + v->arch.vm_event->emul_read_data = NULL; } d->arch.mem_access_emulate_each_rep = 0; diff --git a/xen/common/vm_event.c b/xen/common/vm_event.c index 17d2716..47ae96c 100644 --- a/xen/common/vm_event.c +++ b/xen/common/vm_event.c @@ -534,6 +534,8 @@ static void mem_sharing_notification(struct vcpu *v, unsigned int port) /* Clean up on domain destruction */ void vm_event_cleanup(struct domain *d) { + struct vcpu *v; + #ifdef CONFIG_HAS_MEM_PAGING if ( d->vm_event->paging.ring_page ) { @@ -560,6 +562,16 @@ void vm_event_cleanup(struct domain *d) (void)vm_event_disable(d, &d->vm_event->share); } #endif + + for_each_vcpu ( d, v ) + { + if ( unlikely(v->arch.vm_event) ) + { + /* vm_event->emul_read_data freed in vm_event_cleanup_domain */ + xfree(v->arch.vm_event); + v->arch.vm_event = NULL; + } + } } int vm_event_domctl(struct domain *d, xen_domctl_vm_event_op_t *vec, diff --git a/xen/include/asm-x86/domain.h b/xen/include/asm-x86/domain.h index 8f64ae9..0e3e139 100644 --- a/xen/include/asm-x86/domain.h +++ b/xen/include/asm-x86/domain.h @@ -260,12 +260,17 @@ struct pv_domain }; struct monitor_write_data { - struct { - unsigned int msr : 1; - unsigned int cr0 : 1; - unsigned int cr3 : 1; - unsigned int cr4 : 1; - } do_write; + union { + struct { + unsigned int msr : 1; + unsigned int cr0 : 1; + unsigned int cr3 : 1; + unsigned int cr4 : 1; + } do_write; + + /* non-zero when at least one of do_write fields is non-zero */ + unsigned int uncommitted_writes; + }; uint32_t msr; uint64_t value; diff --git a/xen/include/asm-x86/monitor.h b/xen/include/asm-x86/monitor.h index 0611681..9238ec8 100644 --- a/xen/include/asm-x86/monitor.h +++ b/xen/include/asm-x86/monitor.h @@ -26,6 +26,7 @@ #include #include #include +#include #define monitor_ctrlreg_bitmask(ctrlreg_index) (1U << (ctrlreg_index)) @@ -48,7 +49,7 @@ int arch_monitor_domctl_op(struct domain *d, struct xen_domctl_monitor_op *mop) * Enabling mem_access_emulate_each_rep without a vm_event subscriber * is meaningless. */ - if ( d->max_vcpus && d->vcpu[0] && d->vcpu[0]->arch.vm_event ) + if ( vm_event_domain_initialised(d) ) d->arch.mem_access_emulate_each_rep = !!mop->event; else rc = -EINVAL; diff --git a/xen/include/asm-x86/vm_event.h b/xen/include/asm-x86/vm_event.h index 026f42e..9bdeccc 100644 --- a/xen/include/asm-x86/vm_event.h +++ b/xen/include/asm-x86/vm_event.h @@ -28,12 +28,23 @@ */ struct arch_vm_event { uint32_t emulate_flags; - struct vm_event_emul_read_data emul_read_data; + struct vm_event_emul_read_data *emul_read_data; struct monitor_write_data write_data; }; int vm_event_init_domain(struct domain *d); +static inline bool_t vm_event_vcpu_initialised(struct vcpu *v) +{ + return (v->arch.vm_event && v->arch.vm_event->emul_read_data); +} + +static inline bool_t vm_event_domain_initialised(struct domain *d) +{ + return (d->max_vcpus && d->vcpu[0] && + vm_event_vcpu_initialised(d->vcpu[0])); +} + void vm_event_cleanup_domain(struct domain *d); void vm_event_toggle_singlestep(struct domain *d, struct vcpu *v);