From owner-svn-src-stable-12@freebsd.org Thu Jan 9 08:29:32 2020 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id A26C722D724; Thu, 9 Jan 2020 08:29:32 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47tfRc33ztz3Hsy; Thu, 9 Jan 2020 08:29:32 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 6468025EE0; Thu, 9 Jan 2020 08:29:32 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0098TWP3014250; Thu, 9 Jan 2020 08:29:32 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0098TV4a014246; Thu, 9 Jan 2020 08:29:31 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202001090829.0098TV4a014246@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Thu, 9 Jan 2020 08:29:31 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r356543 - in stable/12/sys/amd64: include vmm vmm/io X-SVN-Group: stable-12 X-SVN-Commit-Author: kib X-SVN-Commit-Paths: in stable/12/sys/amd64: include vmm vmm/io X-SVN-Commit-Revision: 356543 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Jan 2020 08:29:32 -0000 Author: kib Date: Thu Jan 9 08:29:31 2020 New Revision: 356543 URL: https://svnweb.freebsd.org/changeset/base/356543 Log: MFC r356296: bhyve: terminate waiting loops if thread suspension is requested. PR: 242724 Modified: stable/12/sys/amd64/include/vmm.h stable/12/sys/amd64/vmm/io/vioapic.c stable/12/sys/amd64/vmm/vmm.c stable/12/sys/amd64/vmm/vmm_dev.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/amd64/include/vmm.h ============================================================================== --- stable/12/sys/amd64/include/vmm.h Thu Jan 9 08:24:09 2020 (r356542) +++ stable/12/sys/amd64/include/vmm.h Thu Jan 9 08:29:31 2020 (r356543) @@ -267,7 +267,7 @@ void vm_exit_reqidle(struct vm *vm, int vcpuid, uint64 * forward progress when the rendezvous is in progress. */ typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); -void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, +int vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, vm_rendezvous_func_t func, void *arg); cpuset_t vm_active_cpus(struct vm *vm); cpuset_t vm_debug_cpus(struct vm *vm); Modified: stable/12/sys/amd64/vmm/io/vioapic.c ============================================================================== --- stable/12/sys/amd64/vmm/io/vioapic.c Thu Jan 9 08:24:09 2020 (r356542) +++ stable/12/sys/amd64/vmm/io/vioapic.c Thu Jan 9 08:29:31 2020 (r356543) @@ -353,7 +353,7 @@ vioapic_write(struct vioapic *vioapic, int vcpuid, uin "vlapic trigger-mode register", pin); VIOAPIC_UNLOCK(vioapic); allvcpus = vm_active_cpus(vioapic->vm); - vm_smp_rendezvous(vioapic->vm, vcpuid, allvcpus, + (void)vm_smp_rendezvous(vioapic->vm, vcpuid, allvcpus, vioapic_update_tmr, NULL); VIOAPIC_LOCK(vioapic); } Modified: stable/12/sys/amd64/vmm/vmm.c ============================================================================== --- stable/12/sys/amd64/vmm/vmm.c Thu Jan 9 08:24:09 2020 (r356542) +++ stable/12/sys/amd64/vmm/vmm.c Thu Jan 9 08:29:31 2020 (r356543) @@ -1244,13 +1244,17 @@ vcpu_require_state_locked(struct vm *vm, int vcpuid, e VM_CTR0(vm, fmt); \ } while (0) -static void +static int vm_handle_rendezvous(struct vm *vm, int vcpuid) { + struct thread *td; + int error; KASSERT(vcpuid == -1 || (vcpuid >= 0 && vcpuid < vm->maxcpus), ("vm_handle_rendezvous: invalid vcpuid %d", vcpuid)); + error = 0; + td = curthread; mtx_lock(&vm->rendezvous_mtx); while (vm->rendezvous_func != NULL) { /* 'rendezvous_req_cpus' must be a subset of 'active_cpus' */ @@ -1272,9 +1276,17 @@ vm_handle_rendezvous(struct vm *vm, int vcpuid) } RENDEZVOUS_CTR0(vm, vcpuid, "Wait for rendezvous completion"); mtx_sleep(&vm->rendezvous_func, &vm->rendezvous_mtx, 0, - "vmrndv", 0); + "vmrndv", hz); + if ((td->td_flags & TDF_NEEDSUSPCHK) != 0) { + mtx_unlock(&vm->rendezvous_mtx); + error = thread_check_susp(td, true); + if (error != 0) + return (error); + mtx_lock(&vm->rendezvous_mtx); + } } mtx_unlock(&vm->rendezvous_mtx); + return (0); } /* @@ -1285,13 +1297,16 @@ vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_dis { struct vcpu *vcpu; const char *wmesg; - int t, vcpu_halted, vm_halted; + struct thread *td; + int error, t, vcpu_halted, vm_halted; KASSERT(!CPU_ISSET(vcpuid, &vm->halted_cpus), ("vcpu already halted")); vcpu = &vm->vcpu[vcpuid]; vcpu_halted = 0; vm_halted = 0; + error = 0; + td = curthread; vcpu_lock(vcpu); while (1) { @@ -1352,6 +1367,13 @@ vm_handle_hlt(struct vm *vm, int vcpuid, bool intr_dis msleep_spin(vcpu, &vcpu->mtx, wmesg, hz); vcpu_require_state_locked(vm, vcpuid, VCPU_FROZEN); vmm_stat_incr(vm, vcpuid, VCPU_IDLE_TICKS, ticks - t); + if ((td->td_flags & TDF_NEEDSUSPCHK) != 0) { + vcpu_unlock(vcpu); + error = thread_check_susp(td, false); + if (error != 0) + return (error); + vcpu_lock(vcpu); + } } if (vcpu_halted) @@ -1488,11 +1510,13 @@ vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *r static int vm_handle_suspend(struct vm *vm, int vcpuid, bool *retu) { - int i, done; + int error, i; struct vcpu *vcpu; + struct thread *td; - done = 0; + error = 0; vcpu = &vm->vcpu[vcpuid]; + td = curthread; CPU_SET_ATOMIC(vcpuid, &vm->suspended_cpus); @@ -1504,7 +1528,7 @@ vm_handle_suspend(struct vm *vm, int vcpuid, bool *ret * handler while we are waiting to prevent a deadlock. */ vcpu_lock(vcpu); - while (1) { + while (error == 0) { if (CPU_CMP(&vm->suspended_cpus, &vm->active_cpus) == 0) { VCPU_CTR0(vm, vcpuid, "All vcpus suspended"); break; @@ -1515,10 +1539,15 @@ vm_handle_suspend(struct vm *vm, int vcpuid, bool *ret vcpu_require_state_locked(vm, vcpuid, VCPU_SLEEPING); msleep_spin(vcpu, &vcpu->mtx, "vmsusp", hz); vcpu_require_state_locked(vm, vcpuid, VCPU_FROZEN); + if ((td->td_flags & TDF_NEEDSUSPCHK) != 0) { + vcpu_unlock(vcpu); + error = thread_check_susp(td, false); + vcpu_lock(vcpu); + } } else { VCPU_CTR0(vm, vcpuid, "Rendezvous during suspend"); vcpu_unlock(vcpu); - vm_handle_rendezvous(vm, vcpuid); + error = vm_handle_rendezvous(vm, vcpuid); vcpu_lock(vcpu); } } @@ -1534,7 +1563,7 @@ vm_handle_suspend(struct vm *vm, int vcpuid, bool *ret } *retu = true; - return (0); + return (error); } static int @@ -1708,8 +1737,7 @@ restart: vme->u.ioapic_eoi.vector); break; case VM_EXITCODE_RENDEZVOUS: - vm_handle_rendezvous(vm, vcpuid); - error = 0; + error = vm_handle_rendezvous(vm, vcpuid); break; case VM_EXITCODE_HLT: intr_disabled = ((vme->u.hlt.rflags & PSL_I) == 0); @@ -2487,11 +2515,11 @@ vm_apicid2vcpuid(struct vm *vm, int apicid) return (apicid); } -void +int vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, vm_rendezvous_func_t func, void *arg) { - int i; + int error, i; /* * Enforce that this function is called without any locks @@ -2510,7 +2538,9 @@ restart: */ RENDEZVOUS_CTR0(vm, vcpuid, "Rendezvous already in progress"); mtx_unlock(&vm->rendezvous_mtx); - vm_handle_rendezvous(vm, vcpuid); + error = vm_handle_rendezvous(vm, vcpuid); + if (error != 0) + return (error); goto restart; } KASSERT(vm->rendezvous_func == NULL, ("vm_smp_rendezvous: previous " @@ -2532,7 +2562,7 @@ restart: vcpu_notify_event(vm, i, false); } - vm_handle_rendezvous(vm, vcpuid); + return (vm_handle_rendezvous(vm, vcpuid)); } struct vatpic * Modified: stable/12/sys/amd64/vmm/vmm_dev.c ============================================================================== --- stable/12/sys/amd64/vmm/vmm_dev.c Thu Jan 9 08:24:09 2020 (r356542) +++ stable/12/sys/amd64/vmm/vmm_dev.c Thu Jan 9 08:29:31 2020 (r356543) @@ -790,8 +790,12 @@ vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t da vcpu_unlock_all(sc); done: - /* Make sure that no handler returns a bogus value like ERESTART */ - KASSERT(error >= 0, ("vmmdev_ioctl: invalid error return %d", error)); + /* + * Make sure that no handler returns a kernel-internal + * error value to userspace. + */ + KASSERT(error == ERESTART || error >= 0, + ("vmmdev_ioctl: invalid error return %d", error)); return (error); }