From patchwork Sun Nov 27 02:42:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pingfan Liu X-Patchwork-Id: 127828 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5F283B7105 for ; Sun, 27 Nov 2011 13:42:42 +1100 (EST) Received: from localhost ([::1]:45021 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RUUhR-0007T8-5j for incoming@patchwork.ozlabs.org; Sat, 26 Nov 2011 21:42:37 -0500 Received: from eggs.gnu.org ([140.186.70.92]:37108) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RUUhM-0007T3-FT for qemu-devel@nongnu.org; Sat, 26 Nov 2011 21:42:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RUUhL-00082Q-A6 for qemu-devel@nongnu.org; Sat, 26 Nov 2011 21:42:32 -0500 Received: from mail-iy0-f173.google.com ([209.85.210.173]:41294) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RUUhL-00082M-72 for qemu-devel@nongnu.org; Sat, 26 Nov 2011 21:42:31 -0500 Received: by iakk32 with SMTP id k32so7708418iak.4 for ; Sat, 26 Nov 2011 18:42:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=jt/i3NlGQldVaLYP4mQtJ6r7jXLyQRDTCNgjv1ft7dI=; b=T+VANVCEXakOO7klwGA+I6TniKI46QcnWrZhXZBnQp80YuvXFlSss+1jL3DM2x/ulp TnXLXrPMJwwjKmVmb8COJ+p2zTnymW1RXfJNUQHxBI74sCBk+aIkwhTbI4KlLWIqbL9X wTZuXw7mYBjGVCGkzNeL0GdXz0HOlgHq9mf6k= Received: by 10.50.40.135 with SMTP id x7mr6565312igk.48.1322361749418; Sat, 26 Nov 2011 18:42:29 -0800 (PST) Received: from localhost ([111.192.255.64]) by mx.google.com with ESMTPS id jm11sm24712436ibb.1.2011.11.26.18.42.22 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 26 Nov 2011 18:42:27 -0800 (PST) From: Liu Ping Fan To: kvm@vger.kernel.org, qemu-devel@nongnu.org Date: Sun, 27 Nov 2011 10:42:15 +0800 Message-Id: <1322361735-21428-1-git-send-email-kernelfans@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1322188529-11609-1-git-send-email-kernelfans@gmail.com> References: <1322188529-11609-1-git-send-email-kernelfans@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.210.173 Cc: aliguori@us.ibm.com, ryanh@us.ibm.com, jan.kiszka@web.de, linux-kernel@vger.kernel.org, avi@redhat.com Subject: [Qemu-devel] [PATCH 2/2] kvm: exit to userspace with reason KVM_EXIT_VCPU_DEAD X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Liu Ping Fan The vcpu can be safely released when --1.guest tells us that the vcpu is not needed any longer. --2.vcpu hits the last instruction _halt_ If both of the conditions are satisfied, kvm exits to userspace with the reason vcpu dead. So the user thread can exit safely. Signed-off-by: Liu Ping Fan --- arch/x86/kvm/x86.c | 16 ++++++++++++++++ include/linux/kvm.h | 11 +++++++++++ include/linux/kvm_host.h | 1 + 3 files changed, 28 insertions(+), 0 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ea2315a..7948eaf 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5825,11 +5825,27 @@ static int __vcpu_run(struct kvm_vcpu *vcpu) !vcpu->arch.apf.halted) r = vcpu_enter_guest(vcpu); else { +retry: + if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) { + /*1st check whether guest notify CPU_DEAD*/ + if (vcpu->state == KVM_VCPU_STATE_DYING) { + vcpu->state = KVM_VCPU_STATE_DEAD; + vcpu->run->exit_reason = KVM_EXIT_VCPU_DEAD; + break; + } + } srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); kvm_vcpu_block(vcpu); vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) { + switch (vcpu->state) { + case KVM_VCPU_STATE_DYING: + r = 1; + goto retry; + default: + break; + } switch(vcpu->arch.mp_state) { case KVM_MP_STATE_HALTED: vcpu->arch.mp_state = diff --git a/include/linux/kvm.h b/include/linux/kvm.h index c3892fc..d5ff3f7 100644 --- a/include/linux/kvm.h +++ b/include/linux/kvm.h @@ -162,6 +162,7 @@ struct kvm_pit_config { #define KVM_EXIT_INTERNAL_ERROR 17 #define KVM_EXIT_OSI 18 #define KVM_EXIT_PAPR_HCALL 19 +#define KVM_EXIT_VCPU_DEAD 20 /* For KVM_EXIT_INTERNAL_ERROR */ #define KVM_INTERNAL_ERROR_EMULATION 1 @@ -334,6 +335,12 @@ struct kvm_signal_mask { __u8 sigset[0]; }; +/*for KVM_VCPU_SET_STATE */ +struct kvm_vcpu_state { + int vcpu_id; + int state; +}; + /* for KVM_TPR_ACCESS_REPORTING */ struct kvm_tpr_access_ctl { __u32 enabled; @@ -354,6 +361,9 @@ struct kvm_vapic_addr { #define KVM_MP_STATE_HALTED 3 #define KVM_MP_STATE_SIPI_RECEIVED 4 +#define KVM_VCPU_STATE_DYING 1 +#define KVM_VCPU_STATE_DEAD 2 + struct kvm_mp_state { __u32 mp_state; }; @@ -762,6 +772,7 @@ struct kvm_clock_data { #define KVM_CREATE_SPAPR_TCE _IOW(KVMIO, 0xa8, struct kvm_create_spapr_tce) /* Available with KVM_CAP_RMA */ #define KVM_ALLOCATE_RMA _IOR(KVMIO, 0xa9, struct kvm_allocate_rma) +#define KVM_SETSTATE_VCPU _IOW(KVMIO, 0xaa, struct kvm_vcpu_state) #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fe35078..6fdf927 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -114,6 +114,7 @@ enum { struct kvm_vcpu { struct kvm *kvm; struct kref refcount; + int state; #ifdef CONFIG_PREEMPT_NOTIFIERS struct preempt_notifier preempt_notifier; #endif