--- qemu/xen-mapcache.c 2018/04/24 18:55:39 1.1.1.1 +++ qemu/xen-mapcache.c 2018/04/24 19:16:42 1.1.1.2 @@ -40,6 +40,13 @@ #endif #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT) +/* This is the size of the virtual address space reserve to QEMU that will not + * be use by MapCache. + * From empirical tests I observed that qemu use 75MB more than the + * max_mcache_size. + */ +#define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024) + #define mapcache_lock() ((void)0) #define mapcache_unlock() ((void)0) @@ -87,20 +94,32 @@ void xen_map_cache_init(void) unsigned long size; struct rlimit rlimit_as; - mapcache = qemu_mallocz(sizeof (MapCache)); + mapcache = g_malloc0(sizeof (MapCache)); QTAILQ_INIT(&mapcache->locked_entries); mapcache->last_address_index = -1; - getrlimit(RLIMIT_AS, &rlimit_as); - if (rlimit_as.rlim_max < MCACHE_MAX_SIZE) { - rlimit_as.rlim_cur = rlimit_as.rlim_max; + if (geteuid() == 0) { + rlimit_as.rlim_cur = RLIM_INFINITY; + rlimit_as.rlim_max = RLIM_INFINITY; + mapcache->max_mcache_size = MCACHE_MAX_SIZE; } else { - rlimit_as.rlim_cur = MCACHE_MAX_SIZE; + getrlimit(RLIMIT_AS, &rlimit_as); + rlimit_as.rlim_cur = rlimit_as.rlim_max; + + if (rlimit_as.rlim_max != RLIM_INFINITY) { + fprintf(stderr, "Warning: QEMU's maximum size of virtual" + " memory is not infinity.\n"); + } + if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) { + mapcache->max_mcache_size = rlimit_as.rlim_max - + NON_MCACHE_MEMORY_SIZE; + } else { + mapcache->max_mcache_size = MCACHE_MAX_SIZE; + } } setrlimit(RLIMIT_AS, &rlimit_as); - mapcache->max_mcache_size = rlimit_as.rlim_cur; mapcache->nr_buckets = (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) + @@ -111,7 +130,7 @@ void xen_map_cache_init(void) size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1); DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__, mapcache->nr_buckets, size); - mapcache->entry = qemu_mallocz(size); + mapcache->entry = g_malloc0(size); } static void xen_remap_bucket(MapCacheEntry *entry, @@ -126,8 +145,8 @@ static void xen_remap_bucket(MapCacheEnt trace_xen_remap_bucket(address_index); - pfns = qemu_mallocz(nb_pfn * sizeof (xen_pfn_t)); - err = qemu_mallocz(nb_pfn * sizeof (int)); + pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t)); + err = g_malloc0(nb_pfn * sizeof (int)); if (entry->vaddr_base != NULL) { if (munmap(entry->vaddr_base, entry->size) != 0) { @@ -136,7 +155,7 @@ static void xen_remap_bucket(MapCacheEnt } } if (entry->valid_mapping != NULL) { - qemu_free(entry->valid_mapping); + g_free(entry->valid_mapping); entry->valid_mapping = NULL; } @@ -154,7 +173,7 @@ static void xen_remap_bucket(MapCacheEnt entry->vaddr_base = vaddr_base; entry->paddr_index = address_index; entry->size = size; - entry->valid_mapping = (unsigned long *) qemu_mallocz(sizeof(unsigned long) * + entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) * BITS_TO_LONGS(size >> XC_PAGE_SHIFT)); bitmap_zero(entry->valid_mapping, nb_pfn); @@ -164,8 +183,8 @@ static void xen_remap_bucket(MapCacheEnt } } - qemu_free(pfns); - qemu_free(err); + g_free(pfns); + g_free(err); } uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size, @@ -201,7 +220,7 @@ uint8_t *xen_map_cache(target_phys_addr_ entry = entry->next; } if (!entry) { - entry = qemu_mallocz(sizeof (MapCacheEntry)); + entry = g_malloc0(sizeof (MapCacheEntry)); pentry->next = entry; xen_remap_bucket(entry, __size, address_index); } else if (!entry->lock) { @@ -223,7 +242,7 @@ uint8_t *xen_map_cache(target_phys_addr_ mapcache->last_address_index = address_index; mapcache->last_address_vaddr = entry->vaddr_base; if (lock) { - MapCacheRev *reventry = qemu_mallocz(sizeof(MapCacheRev)); + MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev)); entry->lock++; reventry->vaddr_req = mapcache->last_address_vaddr + address_offset; reventry->paddr_index = mapcache->last_address_index; @@ -237,7 +256,7 @@ uint8_t *xen_map_cache(target_phys_addr_ ram_addr_t xen_ram_addr_from_mapcache(void *ptr) { - MapCacheEntry *entry = NULL, *pentry = NULL; + MapCacheEntry *entry = NULL; MapCacheRev *reventry; target_phys_addr_t paddr_index; target_phys_addr_t size; @@ -263,7 +282,6 @@ ram_addr_t xen_ram_addr_from_mapcache(vo entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { - pentry = entry; entry = entry->next; } if (!entry) { @@ -302,7 +320,7 @@ void xen_invalidate_map_cache_entry(uint return; } QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next); - qemu_free(reventry); + g_free(reventry); entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { @@ -323,8 +341,8 @@ void xen_invalidate_map_cache_entry(uint perror("unmap fails"); exit(-1); } - qemu_free(entry->valid_mapping); - qemu_free(entry); + g_free(entry->valid_mapping); + g_free(entry); } void xen_invalidate_map_cache(void) @@ -358,7 +376,7 @@ void xen_invalidate_map_cache(void) entry->paddr_index = 0; entry->vaddr_base = NULL; entry->size = 0; - qemu_free(entry->valid_mapping); + g_free(entry->valid_mapping); entry->valid_mapping = NULL; }