From owner-svn-src-all@FreeBSD.ORG Tue Mar 18 02:49:28 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id C3A5E779; Tue, 18 Mar 2014 02:49:28 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id B1385C6B; Tue, 18 Mar 2014 02:49:28 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s2I2nSGT094703; Tue, 18 Mar 2014 02:49:28 GMT (envelope-from neel@svn.freebsd.org) Received: (from neel@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s2I2nS9G094702; Tue, 18 Mar 2014 02:49:28 GMT (envelope-from neel@svn.freebsd.org) Message-Id: <201403180249.s2I2nS9G094702@svn.freebsd.org> From: Neel Natu Date: Tue, 18 Mar 2014 02:49:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r263290 - head/sys/amd64/vmm X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Mar 2014 02:49:28 -0000 Author: neel Date: Tue Mar 18 02:49:28 2014 New Revision: 263290 URL: http://svnweb.freebsd.org/changeset/base/263290 Log: When a vcpu is deactivated it must also unblock any rendezvous that may be blocked on it. This is done by issuing a wakeup after clearing the 'vcpuid' from 'active_cpus'. Also, use CPU_CLR_ATOMIC() to guarantee visibility of the updated 'active_cpus' across all host cpus. Modified: head/sys/amd64/vmm/vmm.c Modified: head/sys/amd64/vmm/vmm.c ============================================================================== --- head/sys/amd64/vmm/vmm.c Tue Mar 18 01:40:25 2014 (r263289) +++ head/sys/amd64/vmm/vmm.c Tue Mar 18 02:49:28 2014 (r263290) @@ -130,7 +130,7 @@ struct vm { * An active vcpu is one that has been started implicitly (BSP) or * explicitly (AP) by sending it a startup ipi. */ - cpuset_t active_cpus; + volatile cpuset_t active_cpus; struct mtx rendezvous_mtx; cpuset_t rendezvous_req_cpus; @@ -969,8 +969,12 @@ vm_handle_rendezvous(struct vm *vm, int mtx_lock(&vm->rendezvous_mtx); while (vm->rendezvous_func != NULL) { + /* 'rendezvous_req_cpus' must be a subset of 'active_cpus' */ + CPU_AND(&vm->rendezvous_req_cpus, &vm->active_cpus); + if (vcpuid != -1 && - CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus)) { + CPU_ISSET(vcpuid, &vm->rendezvous_req_cpus) && + !CPU_ISSET(vcpuid, &vm->rendezvous_done_cpus)) { VCPU_CTR0(vm, vcpuid, "Calling rendezvous func"); (*vm->rendezvous_func)(vm, vcpuid, vm->rendezvous_arg); CPU_SET(vcpuid, &vm->rendezvous_done_cpus); @@ -997,9 +1001,10 @@ vm_handle_hlt(struct vm *vm, int vcpuid, { struct vm_exit *vmexit; struct vcpu *vcpu; - int t, timo; + int t, timo, spindown; vcpu = &vm->vcpu[vcpuid]; + spindown = 0; vcpu_lock(vcpu); @@ -1026,17 +1031,25 @@ vm_handle_hlt(struct vm *vm, int vcpuid, * Spindown the vcpu if the apic is disabled and it * had entered the halted state. */ - *retu = true; - vmexit = vm_exitinfo(vm, vcpuid); - vmexit->exitcode = VM_EXITCODE_SPINDOWN_CPU; - vm_deactivate_cpu(vm, vcpuid); - VCPU_CTR0(vm, vcpuid, "spinning down cpu"); + spindown = 1; } vcpu_require_state_locked(vcpu, VCPU_FROZEN); vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); } vcpu_unlock(vcpu); + /* + * Since 'vm_deactivate_cpu()' grabs a sleep mutex we must call it + * outside the confines of the vcpu spinlock. + */ + if (spindown) { + *retu = true; + vmexit = vm_exitinfo(vm, vcpuid); + vmexit->exitcode = VM_EXITCODE_SPINDOWN_CPU; + vm_deactivate_cpu(vm, vcpuid); + VCPU_CTR0(vm, vcpuid, "spinning down cpu"); + } + return (0); } @@ -1541,16 +1554,37 @@ void vm_activate_cpu(struct vm *vm, int vcpuid) { - if (vcpuid >= 0 && vcpuid < VM_MAXCPU) - CPU_SET(vcpuid, &vm->active_cpus); + KASSERT(vcpuid >= 0 && vcpuid < VM_MAXCPU, + ("vm_activate_cpu: invalid vcpuid %d", vcpuid)); + KASSERT(!CPU_ISSET(vcpuid, &vm->active_cpus), + ("vm_activate_cpu: vcpuid %d is already active", vcpuid)); + + VCPU_CTR0(vm, vcpuid, "activated"); + CPU_SET_ATOMIC(vcpuid, &vm->active_cpus); } static void vm_deactivate_cpu(struct vm *vm, int vcpuid) { - if (vcpuid >= 0 && vcpuid < VM_MAXCPU) - CPU_CLR(vcpuid, &vm->active_cpus); + KASSERT(vcpuid >= 0 && vcpuid < VM_MAXCPU, + ("vm_deactivate_cpu: invalid vcpuid %d", vcpuid)); + KASSERT(CPU_ISSET(vcpuid, &vm->active_cpus), + ("vm_deactivate_cpu: vcpuid %d is not active", vcpuid)); + + VCPU_CTR0(vm, vcpuid, "deactivated"); + CPU_CLR_ATOMIC(vcpuid, &vm->active_cpus); + + /* + * If a vcpu rendezvous is in progress then it could be blocked + * on 'vcpuid' - unblock it before disappearing forever. + */ + mtx_lock(&vm->rendezvous_mtx); + if (vm->rendezvous_func != NULL) { + VCPU_CTR0(vm, vcpuid, "unblock rendezvous after deactivation"); + wakeup(&vm->rendezvous_func); + } + mtx_unlock(&vm->rendezvous_mtx); } cpuset_t