svn commit: r327726 - head/sys/amd64/vmm/amd

Andriy Gapon avg at FreeBSD.org
Tue Jan 9 14:22:20 UTC 2018


Author: avg
Date: Tue Jan  9 14:22:18 2018
New Revision: 327726
URL: https://svnweb.freebsd.org/changeset/base/327726

Log:
  vmm/svm: contigmalloc of the whole svm_softc is excessive
  
  This is a followup to r307903.
  
  struct svm_softc takes more than 200 kilobytes while what we really need
  is 3 contiguous pages for I/O permission map and 2 contiguous pages for
  MSR permission map.  Other physically mapped structures have a size of
  a single page, so a proper alignment is sufficient for their correct
  mapping.
  
  Thus, only the permission maps are allocated with contigmalloc now,
  the softc is allocated with a regular malloc.
  
  Additionally, this commit adds a check that malloc returns memory with the
  expected page alignment and that contigmalloc does not fail.
  Unfortunately, at present svm_vminit() is expected to always succeed and
  there is no way to report an error.
  So, a contigmalloc failure leads to a panic.
  We should probably fix this.
  
  MFC after:	2 weeks

Modified:
  head/sys/amd64/vmm/amd/svm.c
  head/sys/amd64/vmm/amd/svm_softc.h

Modified: head/sys/amd64/vmm/amd/svm.c
==============================================================================
--- head/sys/amd64/vmm/amd/svm.c	Tue Jan  9 13:43:04 2018	(r327725)
+++ head/sys/amd64/vmm/amd/svm.c	Tue Jan  9 14:22:18 2018	(r327726)
@@ -517,15 +517,26 @@ svm_vminit(struct vm *vm, pmap_t pmap)
 	vm_paddr_t msrpm_pa, iopm_pa, pml4_pa;
 	int i;
 
-	svm_sc = contigmalloc(sizeof (*svm_sc), M_SVM, M_WAITOK | M_ZERO,
-	    0, ~(vm_paddr_t)0, PAGE_SIZE, 0);
+	svm_sc = malloc(sizeof (*svm_sc), M_SVM, M_WAITOK | M_ZERO);
+	if (((uintptr_t)svm_sc & PAGE_MASK) != 0)
+		panic("malloc of svm_softc not aligned on page boundary");
+
+	svm_sc->msr_bitmap = contigmalloc(SVM_MSR_BITMAP_SIZE, M_SVM,
+	    M_WAITOK, 0, ~(vm_paddr_t)0, PAGE_SIZE, 0);
+	if (svm_sc->msr_bitmap == NULL)
+		panic("contigmalloc of SVM MSR bitmap failed");
+	svm_sc->iopm_bitmap = contigmalloc(SVM_IO_BITMAP_SIZE, M_SVM,
+	    M_WAITOK, 0, ~(vm_paddr_t)0, PAGE_SIZE, 0);
+	if (svm_sc->iopm_bitmap == NULL)
+		panic("contigmalloc of SVM IO bitmap failed");
+
 	svm_sc->vm = vm;
 	svm_sc->nptp = (vm_offset_t)vtophys(pmap->pm_pml4);
 
 	/*
 	 * Intercept read and write accesses to all MSRs.
 	 */
-	memset(svm_sc->msr_bitmap, 0xFF, sizeof(svm_sc->msr_bitmap));
+	memset(svm_sc->msr_bitmap, 0xFF, SVM_MSR_BITMAP_SIZE);
 
 	/*
 	 * Access to the following MSRs is redirected to the VMCB when the
@@ -553,7 +564,7 @@ svm_vminit(struct vm *vm, pmap_t pmap)
 	svm_msr_rd_ok(svm_sc->msr_bitmap, MSR_EFER);
 
 	/* Intercept access to all I/O ports. */
-	memset(svm_sc->iopm_bitmap, 0xFF, sizeof(svm_sc->iopm_bitmap));
+	memset(svm_sc->iopm_bitmap, 0xFF, SVM_IO_BITMAP_SIZE);
 
 	iopm_pa = vtophys(svm_sc->iopm_bitmap);
 	msrpm_pa = vtophys(svm_sc->msr_bitmap);
@@ -2043,7 +2054,9 @@ svm_vmcleanup(void *arg)
 {
 	struct svm_softc *sc = arg;
 
-	contigfree(sc, sizeof (*sc), M_SVM);
+	contigfree(sc->iopm_bitmap, SVM_IO_BITMAP_SIZE, M_SVM);
+	contigfree(sc->msr_bitmap, SVM_MSR_BITMAP_SIZE, M_SVM);
+	free(sc, M_SVM);
 }
 
 static register_t *

Modified: head/sys/amd64/vmm/amd/svm_softc.h
==============================================================================
--- head/sys/amd64/vmm/amd/svm_softc.h	Tue Jan  9 13:43:04 2018	(r327725)
+++ head/sys/amd64/vmm/amd/svm_softc.h	Tue Jan  9 14:22:18 2018	(r327726)
@@ -56,13 +56,13 @@ struct svm_vcpu {
  * SVM softc, one per virtual machine.
  */
 struct svm_softc {
-	uint8_t iopm_bitmap[SVM_IO_BITMAP_SIZE];    /* shared by all vcpus */
-	uint8_t msr_bitmap[SVM_MSR_BITMAP_SIZE];    /* shared by all vcpus */
 	uint8_t apic_page[VM_MAXCPU][PAGE_SIZE];
 	struct svm_vcpu vcpu[VM_MAXCPU];
 	vm_offset_t 	nptp;			    /* nested page table */
+	uint8_t		*iopm_bitmap;    /* shared by all vcpus */
+	uint8_t		*msr_bitmap;    /* shared by all vcpus */
 	struct vm	*vm;
-} __aligned(PAGE_SIZE);
+};
 
 CTASSERT((offsetof(struct svm_softc, nptp) & PAGE_MASK) == 0);
 


More information about the svn-src-head mailing list