Skip to content

Commit

Permalink
14731 clean malloc/free shims from bhyve
Browse files Browse the repository at this point in the history
Reviewed by: Andy Fiddaman <andy@omnios.org>
Reviewed by: Dan Cross <cross@oxidecomputer.com>
Approved by: Gordon Ross <gordon.w.ross@gmail.com>
  • Loading branch information
pfmooney committed Jun 10, 2022
1 parent d4f59ae commit 8130f8e
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 225 deletions.
49 changes: 0 additions & 49 deletions usr/src/compat/bhyve/sys/malloc.h

This file was deleted.

27 changes: 10 additions & 17 deletions usr/src/uts/intel/io/vmm/amd/svm.c
Expand Up @@ -46,7 +46,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
Expand Down Expand Up @@ -112,9 +112,6 @@ static uint32_t vmcb_clean = VMCB_CACHE_DEFAULT;
SYSCTL_INT(_hw_vmm_svm, OID_AUTO, vmcb_clean, CTLFLAG_RDTUN, &vmcb_clean,
0, NULL);

static MALLOC_DEFINE(M_SVM, "svm", "svm");
static MALLOC_DEFINE(M_SVM_VLAPIC, "svm-vlapic", "svm-vlapic");

/* SVM features advertised by CPUID.8000000AH:EDX */
static uint32_t svm_feature = ~0U; /* AMD SVM features. */

Expand Down Expand Up @@ -427,16 +424,13 @@ svm_vminit(struct vm *vm)
int i;
uint16_t maxcpus;

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 = kmem_zalloc(sizeof (*svm_sc), KM_SLEEP);
VERIFY3U(((uintptr_t)svm_sc & PAGE_MASK), ==, 0);

svm_sc->msr_bitmap = contigmalloc(SVM_MSR_BITMAP_SIZE, M_SVM,
M_WAITOK, 0, ~(vm_paddr_t)0, PAGE_SIZE, 0);
svm_sc->msr_bitmap = vmm_contig_alloc(SVM_MSR_BITMAP_SIZE);
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);
svm_sc->iopm_bitmap = vmm_contig_alloc(SVM_IO_BITMAP_SIZE);
if (svm_sc->iopm_bitmap == NULL)
panic("contigmalloc of SVM IO bitmap failed");

Expand Down Expand Up @@ -2035,9 +2029,9 @@ svm_vmcleanup(void *arg)
{
struct svm_softc *sc = arg;

contigfree(sc->iopm_bitmap, SVM_IO_BITMAP_SIZE, M_SVM);
contigfree(sc->msr_bitmap, SVM_MSR_BITMAP_SIZE, M_SVM);
free(sc, M_SVM);
vmm_contig_free(sc->iopm_bitmap, SVM_IO_BITMAP_SIZE);
vmm_contig_free(sc->msr_bitmap, SVM_MSR_BITMAP_SIZE);
kmem_free(sc, sizeof (*sc));
}

static uint64_t *
Expand Down Expand Up @@ -2400,8 +2394,7 @@ svm_vlapic_init(void *arg, int vcpuid)
struct vlapic *vlapic;

svm_sc = arg;
vlapic = malloc(sizeof (struct vlapic), M_SVM_VLAPIC,
M_WAITOK | M_ZERO);
vlapic = kmem_zalloc(sizeof (struct vlapic), KM_SLEEP);
vlapic->vm = svm_sc->vm;
vlapic->vcpuid = vcpuid;
vlapic->apic_page = (struct LAPIC *)&svm_sc->apic_page[vcpuid];
Expand All @@ -2415,7 +2408,7 @@ static void
svm_vlapic_cleanup(void *arg, struct vlapic *vlapic)
{
vlapic_cleanup(vlapic);
free(vlapic, M_SVM_VLAPIC);
kmem_free(vlapic, sizeof (struct vlapic));
}

static void
Expand Down
34 changes: 12 additions & 22 deletions usr/src/uts/intel/io/vmm/intel/vmx.c
Expand Up @@ -49,7 +49,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
Expand Down Expand Up @@ -162,9 +162,6 @@ __FBSDID("$FreeBSD$");
#define HANDLED 1
#define UNHANDLED 0

static MALLOC_DEFINE(M_VMX, "vmx", "vmx");
static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic");

SYSCTL_DECL(_hw_vmm);
SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
NULL);
Expand Down Expand Up @@ -694,13 +691,10 @@ vmx_vminit(struct vm *vm)
uint32_t proc_ctls, proc2_ctls, pin_ctls;
uint64_t apic_access_pa = UINT64_MAX;

vmx = malloc(sizeof (struct vmx), M_VMX, M_WAITOK | M_ZERO);
if ((uintptr_t)vmx & PAGE_MASK) {
panic("malloc of struct vmx not aligned on %d byte boundary",
PAGE_SIZE);
}
vmx->vm = vm;
vmx = kmem_zalloc(sizeof (struct vmx), KM_SLEEP);
VERIFY3U((uintptr_t)vmx & PAGE_MASK, ==, 0);

vmx->vm = vm;
vmx->eptp = vmspace_table_root(vm_get_vmspace(vm));

/*
Expand Down Expand Up @@ -2937,7 +2931,7 @@ vmx_vmcleanup(void *arg)
for (i = 0; i < maxcpus; i++)
vpid_free(vmx->state[i].vpid);

free(vmx, M_VMX);
kmem_free(vmx, sizeof (*vmx));
}

static uint64_t *
Expand Down Expand Up @@ -3628,22 +3622,19 @@ vmx_tpr_shadow_exit(struct vlapic *vlapic)
static struct vlapic *
vmx_vlapic_init(void *arg, int vcpuid)
{
struct vmx *vmx;
struct vlapic *vlapic;
struct vmx *vmx = arg;
struct vlapic_vtx *vlapic_vtx;
struct vlapic *vlapic;

vmx = arg;
vlapic_vtx = kmem_zalloc(sizeof (struct vlapic_vtx), KM_SLEEP);
vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
vlapic_vtx->vmx = vmx;

vlapic = malloc(sizeof (struct vlapic_vtx), M_VLAPIC,
M_WAITOK | M_ZERO);
vlapic = &vlapic_vtx->vlapic;
vlapic->vm = vmx->vm;
vlapic->vcpuid = vcpuid;
vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid];

vlapic_vtx = (struct vlapic_vtx *)vlapic;
vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid];
vlapic_vtx->vmx = vmx;

if (vmx_cap_en(vmx, VMX_CAP_TPR_SHADOW)) {
vlapic->ops.enable_x2apic_mode = vmx_enable_x2apic_mode_ts;
}
Expand All @@ -3666,9 +3657,8 @@ vmx_vlapic_init(void *arg, int vcpuid)
static void
vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic)
{

vlapic_cleanup(vlapic);
free(vlapic, M_VLAPIC);
kmem_free(vlapic, sizeof (struct vlapic_vtx));
}

static void
Expand Down
15 changes: 6 additions & 9 deletions usr/src/uts/intel/io/vmm/intel/vtd.c
Expand Up @@ -34,7 +34,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kmem.h>

#include <dev/pci/pcireg.h>

Expand Down Expand Up @@ -131,8 +131,6 @@ static dev_info_t *vtddips[DRHD_MAX_UNITS];
static uint64_t root_table[PAGE_SIZE / sizeof (uint64_t)] __aligned(4096);
static uint64_t ctx_tables[256][PAGE_SIZE / sizeof (uint64_t)] __aligned(4096);

static MALLOC_DEFINE(M_VTD, "vtd", "vtd");

static int
vtd_max_domains(struct vtdmap *vtdmap)
{
Expand Down Expand Up @@ -666,7 +664,7 @@ vtd_update_mapping(void *arg, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len,
* to it from the current page table.
*/
if (ptp[ptpindex] == 0) {
void *nlp = malloc(PAGE_SIZE, M_VTD, M_WAITOK | M_ZERO);
void *nlp = vmm_ptp_alloc();
ptp[ptpindex] = vtophys(nlp)| VTD_PTE_RD | VTD_PTE_WR;
}

Expand Down Expand Up @@ -779,12 +777,12 @@ vtd_create_domain(vm_paddr_t maxaddr)
tmp, agaw);
}

dom = malloc(sizeof (struct domain), M_VTD, M_ZERO | M_WAITOK);
dom = kmem_zalloc(sizeof (struct domain), KM_SLEEP);
dom->pt_levels = pt_levels;
dom->addrwidth = addrwidth;
dom->id = domain_id();
dom->maxaddr = maxaddr;
dom->ptp = malloc(PAGE_SIZE, M_VTD, M_ZERO | M_WAITOK);
dom->ptp = vmm_ptp_alloc();
if ((uintptr_t)dom->ptp & PAGE_MASK)
panic("vtd_create_domain: ptp (%p) not page aligned", dom->ptp);

Expand Down Expand Up @@ -846,8 +844,7 @@ vtd_free_ptp(uint64_t *ptp, int level)
}
}

bzero(ptp, PAGE_SIZE);
free(ptp, M_VTD);
vmm_ptp_free(ptp);
}

static void
Expand All @@ -859,7 +856,7 @@ vtd_destroy_domain(void *arg)

SLIST_REMOVE(&domhead, dom, domain, next);
vtd_free_ptp(dom->ptp, dom->pt_levels);
free(dom, M_VTD);
kmem_free(dom, sizeof (*dom));
}

const struct iommu_ops iommu_ops_intel = {
Expand Down
5 changes: 5 additions & 0 deletions usr/src/uts/intel/io/vmm/io/iommu.h
Expand Up @@ -73,4 +73,9 @@ void iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len);
void iommu_add_device(void *dom, uint16_t rid);
void iommu_remove_device(void *dom, uint16_t rid);
void iommu_invalidate_tlb(void *domain);

/* Glue functions used by iommu provider(s) */
void *vmm_ptp_alloc(void);
void vmm_ptp_free(void *);

#endif
2 changes: 1 addition & 1 deletion usr/src/uts/intel/io/vmm/io/ppt.c
Expand Up @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/pciio.h>
Expand Down
8 changes: 3 additions & 5 deletions usr/src/uts/intel/io/vmm/io/vatpic.c
Expand Up @@ -45,7 +45,7 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/systm.h>

Expand All @@ -58,8 +58,6 @@ __FBSDID("$FreeBSD$");
#include "vioapic.h"
#include "vatpic.h"

static MALLOC_DEFINE(M_VATPIC, "atpic", "bhyve virtual atpic (8259)");

#define VATPIC_LOCK(vatpic) mutex_enter(&((vatpic)->lock))
#define VATPIC_UNLOCK(vatpic) mutex_exit(&((vatpic)->lock))
#define VATPIC_LOCKED(vatpic) MUTEX_HELD(&((vatpic)->lock))
Expand Down Expand Up @@ -781,7 +779,7 @@ vatpic_init(struct vm *vm)
{
struct vatpic *vatpic;

vatpic = malloc(sizeof (struct vatpic), M_VATPIC, M_WAITOK | M_ZERO);
vatpic = kmem_zalloc(sizeof (struct vatpic), KM_SLEEP);
vatpic->vm = vm;

mutex_init(&vatpic->lock, NULL, MUTEX_ADAPTIVE, NULL);
Expand All @@ -793,5 +791,5 @@ void
vatpic_cleanup(struct vatpic *vatpic)
{
mutex_destroy(&vatpic->lock);
free(vatpic, M_VATPIC);
kmem_free(vatpic, sizeof (*vatpic));
}
8 changes: 3 additions & 5 deletions usr/src/uts/intel/io/vmm/io/vatpit.c
Expand Up @@ -34,7 +34,7 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/systm.h>

Expand All @@ -44,8 +44,6 @@ __FBSDID("$FreeBSD$");
#include "vioapic.h"
#include "vatpit.h"

static MALLOC_DEFINE(M_VATPIT, "atpit", "bhyve virtual atpit (8254)");

#define VATPIT_LOCK(vatpit) mutex_enter(&((vatpit)->lock))
#define VATPIT_UNLOCK(vatpit) mutex_exit(&((vatpit)->lock))

Expand Down Expand Up @@ -447,7 +445,7 @@ vatpit_init(struct vm *vm)
struct vatpit_callout_arg *arg;
int i;

vatpit = malloc(sizeof (struct vatpit), M_VATPIT, M_WAITOK | M_ZERO);
vatpit = kmem_zalloc(sizeof (struct vatpit), KM_SLEEP);
vatpit->vm = vm;

mutex_init(&vatpit->lock, NULL, MUTEX_ADAPTIVE, NULL);
Expand All @@ -471,7 +469,7 @@ vatpit_cleanup(struct vatpit *vatpit)
callout_drain(&vatpit->channel[i].callout);

mutex_destroy(&vatpit->lock);
free(vatpit, M_VATPIT);
kmem_free(vatpit, sizeof (*vatpit));
}

void
Expand Down
7 changes: 3 additions & 4 deletions usr/src/uts/intel/io/vmm/io/vhpet.c
Expand Up @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/mutex.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/systm.h>

#include <dev/acpica/acpi_hpet.h>
Expand All @@ -52,7 +52,6 @@ __FBSDID("$FreeBSD$");
#include "vioapic.h"
#include "vhpet.h"

static MALLOC_DEFINE(M_VHPET, "vhpet", "bhyve virtual hpet");

#define HPET_FREQ 16777216 /* 16.7 (2^24) Mhz */
#define FS_PER_S 1000000000000000ul
Expand Down Expand Up @@ -682,7 +681,7 @@ vhpet_init(struct vm *vm)
uint64_t allowed_irqs;
struct vhpet_callout_arg *arg;

vhpet = malloc(sizeof (struct vhpet), M_VHPET, M_WAITOK | M_ZERO);
vhpet = kmem_zalloc(sizeof (struct vhpet), KM_SLEEP);
vhpet->vm = vm;
mutex_init(&vhpet->lock, NULL, MUTEX_ADAPTIVE, NULL);

Expand Down Expand Up @@ -722,7 +721,7 @@ vhpet_cleanup(struct vhpet *vhpet)
callout_drain(&vhpet->timer[i].callout);

mutex_destroy(&vhpet->lock);
free(vhpet, M_VHPET);
kmem_free(vhpet, sizeof (*vhpet));
}

int
Expand Down

0 comments on commit 8130f8e

Please sign in to comment.