Replace qemu_real_host_page variables with inlined functions

Replace the global variables with inlined helper functions. getpagesize() is very
likely annotated with a "const" function attribute (at least with glibc), and thus
optimization should apply even better.

This avoids the need for a constructor initialization too.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20220323155743.1585078-12-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Marc-André Lureau 2022-03-23 19:57:22 +04:00 committed by Paolo Bonzini
parent b307e5052d
commit 8e3b0cbb72
53 changed files with 150 additions and 162 deletions

View file

@ -175,7 +175,7 @@ int qemu_fdatasync(int fd)
int qemu_msync(void *addr, size_t length, int fd)
{
#ifdef CONFIG_POSIX
size_t align_mask = ~(qemu_real_host_page_size - 1);
size_t align_mask = ~(qemu_real_host_page_size() - 1);
/**
* There are no strict reqs as per the length of mapping
@ -183,7 +183,7 @@ int qemu_msync(void *addr, size_t length, int fd)
* alignment changes. Additionally - round the size to the multiple
* of PAGE_SIZE
*/
length += ((uintptr_t)addr & (qemu_real_host_page_size - 1));
length += ((uintptr_t)addr & (qemu_real_host_page_size() - 1));
length = (length + ~align_mask) & align_mask;
addr = (void *)((uintptr_t)addr & align_mask);

View file

@ -42,7 +42,6 @@ if have_membarrier
util_ss.add(files('sys_membarrier.c'))
endif
util_ss.add(files('log.c'))
util_ss.add(files('pagesize.c'))
util_ss.add(files('qdist.c'))
util_ss.add(files('qht.c'))
util_ss.add(files('qsp.c'))

View file

@ -50,7 +50,7 @@ size_t qemu_fd_getpagesize(int fd)
#endif
#endif
return qemu_real_host_page_size;
return qemu_real_host_page_size();
}
size_t qemu_mempath_getpagesize(const char *mem_path)
@ -81,7 +81,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path)
#endif
#endif
return qemu_real_host_page_size;
return qemu_real_host_page_size();
}
#define OVERCOMMIT_MEMORY_PATH "/proc/sys/vm/overcommit_memory"
@ -101,7 +101,7 @@ static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags)
* MAP_NORESERVE.
* b) MAP_NORESERVE is not affected by /proc/sys/vm/overcommit_memory.
*/
if (qemu_fd_getpagesize(fd) != qemu_real_host_page_size) {
if (qemu_fd_getpagesize(fd) != qemu_real_host_page_size()) {
return true;
}
@ -166,7 +166,7 @@ static void *mmap_reserve(size_t size, int fd)
* We do this unless we are using the system page size, in which case
* anonymous memory is OK.
*/
if (fd == -1 || qemu_fd_getpagesize(fd) == qemu_real_host_page_size) {
if (fd == -1 || qemu_fd_getpagesize(fd) == qemu_real_host_page_size()) {
fd = -1;
flags |= MAP_ANONYMOUS;
} else {
@ -243,7 +243,7 @@ static inline size_t mmap_guard_pagesize(int fd)
/* Mappings in the same segment must share the same page size */
return qemu_fd_getpagesize(fd);
#else
return qemu_real_host_page_size;
return qemu_real_host_page_size();
#endif
}

View file

@ -69,8 +69,8 @@ int qemu_madvise(void *addr, size_t len, int advice)
static int qemu_mprotect__osdep(void *addr, size_t size, int prot)
{
g_assert(!((uintptr_t)addr & ~qemu_real_host_page_mask));
g_assert(!(size & ~qemu_real_host_page_mask));
g_assert(!((uintptr_t)addr & ~qemu_real_host_page_mask()));
g_assert(!(size & ~qemu_real_host_page_mask()));
#ifdef _WIN32
DWORD old_protect;

View file

@ -767,7 +767,7 @@ void *qemu_alloc_stack(size_t *sz)
#ifdef CONFIG_DEBUG_STACK_USAGE
void *ptr2;
#endif
size_t pagesz = qemu_real_host_page_size;
size_t pagesz = qemu_real_host_page_size();
#ifdef _SC_THREAD_STACK_MIN
/* avoid stacks smaller than _SC_THREAD_STACK_MIN */
long min_stack_sz = sysconf(_SC_THREAD_STACK_MIN);
@ -829,7 +829,7 @@ void qemu_free_stack(void *stack, size_t sz)
unsigned int usage;
void *ptr;
for (ptr = stack + qemu_real_host_page_size; ptr < stack + sz;
for (ptr = stack + qemu_real_host_page_size(); ptr < stack + sz;
ptr += sizeof(uint32_t)) {
if (*(uint32_t *)ptr != 0xdeadbeaf) {
break;
@ -927,10 +927,10 @@ size_t qemu_get_host_physmem(void)
#ifdef _SC_PHYS_PAGES
long pages = sysconf(_SC_PHYS_PAGES);
if (pages > 0) {
if (pages > SIZE_MAX / qemu_real_host_page_size) {
if (pages > SIZE_MAX / qemu_real_host_page_size()) {
return SIZE_MAX;
} else {
return pages * qemu_real_host_page_size;
return pages * qemu_real_host_page_size();
}
}
#endif

View file

@ -319,7 +319,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
Error **errp)
{
int i;
size_t pagesize = qemu_real_host_page_size;
size_t pagesize = qemu_real_host_page_size();
memory = (memory + pagesize - 1) & -pagesize;
for (i = 0; i < memory / pagesize; i++) {

View file

@ -1,18 +0,0 @@
/*
* pagesize.c - query the host about its page size
*
* Copyright (C) 2017, Emilio G. Cota <cota@braap.org>
* License: GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
uintptr_t qemu_real_host_page_size;
intptr_t qemu_real_host_page_mask;
static void __attribute__((constructor)) init_real_host_page_size(void)
{
qemu_real_host_page_size = getpagesize();
qemu_real_host_page_mask = -(intptr_t)qemu_real_host_page_size;
}

View file

@ -163,7 +163,7 @@ void *qemu_vfio_pci_map_bar(QEMUVFIOState *s, int index,
Error **errp)
{
void *p;
assert(QEMU_IS_ALIGNED(offset, qemu_real_host_page_size));
assert(QEMU_IS_ALIGNED(offset, qemu_real_host_page_size()));
assert_bar_index_valid(s, index);
p = mmap(NULL, MIN(size, s->bar_region_info[index].size - offset),
prot, MAP_SHARED,
@ -591,9 +591,9 @@ static IOVAMapping *qemu_vfio_add_mapping(QEMUVFIOState *s,
IOVAMapping m = {.host = host, .size = size, .iova = iova};
IOVAMapping *insert;
assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size));
assert(QEMU_IS_ALIGNED(s->low_water_mark, qemu_real_host_page_size));
assert(QEMU_IS_ALIGNED(s->high_water_mark, qemu_real_host_page_size));
assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size()));
assert(QEMU_IS_ALIGNED(s->low_water_mark, qemu_real_host_page_size()));
assert(QEMU_IS_ALIGNED(s->high_water_mark, qemu_real_host_page_size()));
trace_qemu_vfio_new_mapping(s, host, size, index, iova);
assert(index >= 0);
@ -644,7 +644,7 @@ static void qemu_vfio_undo_mapping(QEMUVFIOState *s, IOVAMapping *mapping,
index = mapping - s->mappings;
assert(mapping->size > 0);
assert(QEMU_IS_ALIGNED(mapping->size, qemu_real_host_page_size));
assert(QEMU_IS_ALIGNED(mapping->size, qemu_real_host_page_size()));
assert(index >= 0 && index < s->nr_mappings);
if (ioctl(s->container, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
error_setg_errno(errp, errno, "VFIO_UNMAP_DMA failed");
@ -752,8 +752,8 @@ int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
IOVAMapping *mapping;
uint64_t iova0;
assert(QEMU_PTR_IS_ALIGNED(host, qemu_real_host_page_size));
assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size));
assert(QEMU_PTR_IS_ALIGNED(host, qemu_real_host_page_size()));
assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size()));
trace_qemu_vfio_dma_map(s, host, size, temporary, iova);
QEMU_LOCK_GUARD(&s->lock);
mapping = qemu_vfio_find_mapping(s, host, &index);