From nobody Thu Jan 26 22:12:04 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P2w103Plsz3c8Mx; Thu, 26 Jan 2023 22:12:04 +0000 (UTC) (envelope-from git@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P2w102999z3wQl; Thu, 26 Jan 2023 22:12:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674771124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0QA60UOhmRVX0qo3F9c6TYrtyCWtDR8olS4SPQRPTHc=; b=Sllhz8WYrotw+zjs7jTSOy+HeeIqiRoGpgVEauS1hfloDj9kZr/+M3VotU0j4s97P9+uyl sdWE9a6ThaAExRClPsazcWuPc7FL+aBRXnfL4gWHmJJ7jFXdOUwtpi16+HKu1ShccTmVjC DcnKssPmkgCwbThuo4JZb+j+DDfgPs07KLAt6c1TKwRoaewBzalxJr9HKzdoqjERWKQU3m 0ZH2QaFBQyffGZ9YtZ59ILHcWmQCBo/Hz+ctU6Nv1w5UoaX6QxtGxf54HSqp+VPVUeXok4 DRrB6WLgPv+gpPTKLPt6v1/fLFbbVnpMAhJd0OPDdyqF0OQZLJw8UASTFzH4UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674771124; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0QA60UOhmRVX0qo3F9c6TYrtyCWtDR8olS4SPQRPTHc=; b=xnVvhV/l/x8tQfso3WlZIqUtulZu2Uv/AIIIWMPevbxZ03KEYAZkwaUbnHjuvZtiiOTmcc DU21C9mcsiqce1XAI6Owy1NXwZPsork3aTg5qmN6lSwYvTFWrSgYbdwkVN4v6UHor1b3EL nehULpNnjf6zgHKALueV6j3jZ7WzvhYCiBUZl3BcLX1H9pqL1ls14H9gbdwmdaUxodGsOB MeRoOOIe2J66eP3Jrki5/i5NpI+W6rZbXOqryb695hjDEmME/c7PXghBsAw+Vl6OUjeHHB etKJqR4CrG5ftvQKCTvek6EdU/n2yAkKhdFof5WI33Z00K6y1arjtZFn8ti/GA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674771124; a=rsa-sha256; cv=none; b=ndjp49cob7QGCoD11Zs4MtuMisKjaeNDD9opSgcCW2SAodIlmtFNOQ6RbGjszPFtXJEqHD 8S/5Kf9+d56ULE/jt5W67F3+aV746yH/APHYcncjd3Hp5mFM2/AkUH381hEsmnxqY8gi5P +vKkny0eid6yf7PPzKvnAT54+qleFHt7iqx5Z0TX9rNpg6C2PP0GIM1OwtXWswvubsYToc 0Tjj4S4TSPbIJGt/pmOGp1KIa3cRUxewCP75mHvPwxI8AoW4FhONkROeSmEIBdJY3PYNv8 IBps71+c367mJojBYgyL0Iq78rYZFAwt9N5vT9S9nlBB3mh+nL0rWjr29ppTEw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P2w10176VzlmF; Thu, 26 Jan 2023 22:12:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30QMC4Vl022187; Thu, 26 Jan 2023 22:12:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30QMC4xl022186; Thu, 26 Jan 2023 22:12:04 GMT (envelope-from git) Date: Thu, 26 Jan 2023 22:12:04 GMT Message-Id: <202301262212.30QMC4xl022186@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 20406b29b3ec - stable/13 - vmm vmx: Allocate vpids on demand as each vCPU is initialized. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 20406b29b3ecd14e650ee85291d3977f3bd28432 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=20406b29b3ecd14e650ee85291d3977f3bd28432 commit 20406b29b3ecd14e650ee85291d3977f3bd28432 Author: John Baldwin AuthorDate: 2022-11-18 18:04:11 +0000 Commit: John Baldwin CommitDate: 2023-01-26 22:04:30 +0000 vmm vmx: Allocate vpids on demand as each vCPU is initialized. Compared to the previous version this does mean that if the system as a whole runs out of dedicated vPIDs you might end up with some vCPUs within a single VM using dedicated vPIDs and others using shared vPIDs, but this should not break anything. Reviewed by: corvink, markj Differential Revision: https://reviews.freebsd.org/D37169 (cherry picked from commit 58eefc67a1cf16623c23354efd089f65401c0455) --- sys/amd64/vmm/intel/vmx.c | 47 +++++++++++++++++------------------------------ sys/amd64/vmm/intel/vmx.h | 1 - 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c index 9db638fd858e..52573416ded7 100644 --- a/sys/amd64/vmm/intel/vmx.c +++ b/sys/amd64/vmm/intel/vmx.c @@ -527,36 +527,25 @@ vpid_free(int vpid) free_unr(vpid_unr, vpid); } -static void -vpid_alloc(uint16_t *vpid, int num) +static uint16_t +vpid_alloc(int vcpuid) { - int i, x; - - if (num <= 0 || num > VM_MAXCPU) - panic("invalid number of vpids requested: %d", num); + int x; /* * If the "enable vpid" execution control is not enabled then the * VPID is required to be 0 for all vcpus. */ - if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) { - for (i = 0; i < num; i++) - vpid[i] = 0; - return; - } + if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) + return (0); /* - * Allocate a unique VPID for each vcpu from the unit number allocator. + * Try to allocate a unique VPID for each from the unit number + * allocator. */ - for (i = 0; i < num; i++) { - x = alloc_unr(vpid_unr); - if (x == -1) - break; - else - vpid[i] = x; - } + x = alloc_unr(vpid_unr); - if (i < num) { + if (x == -1) { atomic_add_int(&vpid_alloc_failed, 1); /* @@ -570,12 +559,10 @@ vpid_alloc(uint16_t *vpid, int num) * It is still sub-optimal because the invvpid will invalidate * combined mappings for a particular VPID across all EP4TAs. */ - while (i-- > 0) - vpid_free(vpid[i]); - - for (i = 0; i < num; i++) - vpid[i] = i + 1; + return (vcpuid + 1); } + + return (x); } static void @@ -1035,7 +1022,6 @@ vmx_init(struct vm *vm, pmap_t pmap) { int error; struct vmx *vmx; - uint16_t maxcpus = vm_get_maxcpus(vm); vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO); vmx->vm = vm; @@ -1096,8 +1082,6 @@ vmx_init(struct vm *vm, pmap_t pmap) ((cap_rdpid || cap_rdtscp) && guest_msr_ro(vmx, MSR_TSC_AUX))) panic("vmx_init: error setting guest msr access"); - vpid_alloc(vmx->vpids, maxcpus); - if (virtual_interrupt_delivery) { error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE, APIC_ACCESS_ADDRESS); @@ -1116,8 +1100,11 @@ vmx_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid) struct vmcs *vmcs; struct vmx_vcpu *vcpu; uint32_t exc_bitmap; + uint16_t vpid; int error; + vpid = vpid_alloc(vcpuid); + vcpu = malloc(sizeof(*vcpu), M_VMX, M_WAITOK | M_ZERO); vcpu->vmx = vmx; vcpu->vcpu = vcpu1; @@ -1156,7 +1143,7 @@ vmx_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid) error += vmwrite(VMCS_EXIT_CTLS, exit_ctls); error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls); error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap)); - error += vmwrite(VMCS_VPID, vmx->vpids[vcpuid]); + error += vmwrite(VMCS_VPID, vpid); if (guest_l1d_flush && !guest_l1d_flush_sw) { vmcs_write(VMCS_ENTRY_MSR_LOAD, pmap_kextract( @@ -1204,7 +1191,7 @@ vmx_vcpu_init(void *vmi, struct vcpu *vcpu1, int vcpuid) vcpu->state.nextrip = ~0; vcpu->state.lastcpu = NOCPU; - vcpu->state.vpid = vmx->vpids[vcpuid]; + vcpu->state.vpid = vpid; /* * Set up the CR0/4 shadows, and init the read shadow diff --git a/sys/amd64/vmm/intel/vmx.h b/sys/amd64/vmm/intel/vmx.h index e91675b62800..ad172073b03f 100644 --- a/sys/amd64/vmm/intel/vmx.h +++ b/sys/amd64/vmm/intel/vmx.h @@ -147,7 +147,6 @@ struct vmx { uint64_t eptp; long eptgen[MAXCPU]; /* cached pmap->pm_eptgen */ pmap_t pmap; - uint16_t vpids[VM_MAXCPU]; }; extern bool vmx_have_msr_tsc_aux;