Fix hot-unplug of devices with ROM or RAM
cover Fix hot-unplug of devices with ROM or RAM
Reported by Amos.
Paolo Bonzini (2):
osdep: rename qemu_vmalloc to qemu_anon_ram_alloc
osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
HACKING | 7 +++---- exec.c | 14 +++++--------- include/qemu/osdep.h | 3 ++- include/sysemu/kvm.h | 4 ++-- kvm-all.c | 6 +++--- target-s390x/kvm.c | 2 +- trace-events | 3 ++- util/oslib-posix.c | 12 ++++++++++-- util/oslib-win32.c | 12 ++++++++++-- 9 files changed, 38 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.2.1
PATCH 1/2 osdep, kvm: rename low-level RAM allocation functions
This is preparatory to the introduction of a separate freeing API.
--- HACKING | 7 +++---- exec.c | 6 +++--- include/qemu/osdep.h | 2 +- include/sysemu/kvm.h | 4 ++-- kvm-all.c | 6 +++--- target-s390x/kvm.c | 2 +- trace-events | 2 +- util/oslib-posix.c | 4 ++-- util/oslib-win32.c | 4 ++-- 9 files changed, 18 insertions(+), 19 deletions(-)
HACKING +3 -4
@@ -78,16 +78,15 @@ avoided. Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign APIs is not allowed in the QEMU codebase. Instead of these routines, use the GLib memory allocation routines g_malloc/g_malloc0/g_new/ -g_new0/g_realloc/g_free or QEMU's qemu_vmalloc/qemu_memalign/qemu_vfree +g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree APIs. Please note that g_malloc will exit on allocation failure, so there is no need to test for failure (as you would have to with malloc). Calling g_malloc with a zero size is valid and will return NULL. -Memory allocated by qemu_vmalloc or qemu_memalign must be freed with -qemu_vfree, since breaking this will cause problems on Win32 and user -emulators. +Memory allocated by qemu_memalign or qemu_blockalign must be freed with +qemu_vfree, since breaking this will cause problems on Win32. 4. String manipulation
exec.c +3 -3
@@ -1062,7 +1062,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,#if defined (__linux__) && !defined(TARGET_S390X ) new_block->host =file_ram_alloc (new_block, size, mem_path);if (!new_block->host) { - new_block->host =qemu_vmalloc (size); + new_block->host =qemu_anon_ram_alloc (size);memory_try_enable_merging (new_block->host, size); }#else
@@ -1074,9 +1074,9 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,xen_ram_alloc (new_block->offset, size, mr); }else if (kvm_enabled ()) {/* some s390/kvm configurations have special constraints */ - new_block->host =kvm_vmalloc (size); + new_block->host =kvm_ram_alloc (size); }else { - new_block->host =qemu_vmalloc (size); + new_block->host =qemu_anon_ram_alloc (size); }memory_try_enable_merging (new_block->host, size); }
include/qemu/osdep.h +1 -1
@@ -96,7 +96,7 @@ typedef signed int int_fast16_t;int qemu_daemon (int nochdir,int noclose);void *qemu_memalign (size_t alignment,size_t size); -void *qemu_vmalloc (size_t size); +void *qemu_anon_ram_alloc (size_t size);void qemu_vfree (void *ptr);#define QEMU_MADV_INVALID -1
include/sysemu/kvm.h +2 -2
@@ -142,8 +142,8 @@ int kvm_init_vcpu(CPUState *cpu);int kvm_cpu_exec (CPUArchState *env);#if !defined(CONFIG_USER_ONLY ) -void *kvm_vmalloc (ram_addr_t size); -void *kvm_arch_vmalloc (ram_addr_t size); +void *kvm_ram_alloc (ram_addr_t size); +void *kvm_arch_ram_alloc (ram_addr_t size);#endif void kvm_setup_guest_memory (void *start,size_t size);
kvm-all.c +3 -3
@@ -1790,17 +1790,17 @@ int kvm_has_intx_set_mask(void)return kvm_state->intx_set_mask; } -void *kvm_vmalloc (ram_addr_t size) +void *kvm_ram_alloc (ram_addr_t size) {#ifdef TARGET_S390X void *mem; - mem =kvm_arch_vmalloc (size); + mem =kvm_arch_ram_alloc (size);if (mem) {return mem; }#endif -return qemu_vmalloc (size); +return qemu_anon_ram_alloc (size); }void kvm_setup_guest_memory (void *start,size_t size)
target-s390x/kvm.c +1 -1
@@ -332,7 +332,7 @@ static void *legacy_s390_alloc(ram_addr_t size)return mem; } -void *kvm_arch_vmalloc (ram_addr_t size) +void *kvm_arch_ram_alloc (ram_addr_t size) {/* Can we use the standard allocation ? */ if (kvm_check_extension(kvm_state, KVM_CAP_S390_GMAP) &&
trace-events +1 -1
@@ -32,7 +32,7 @@ g_free(void *ptr) "ptr %p" # osdep.c qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p" -qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p" +qemu_anon_ram_alloc(size_t size, void *ptr) "size %zu ptr %p" qemu_vfree(void *ptr) "ptr %p" # hw/virtio.c
util/oslib-posix.c +2 -2
@@ -101,7 +101,7 @@ void *qemu_memalign(size_t alignment, size_t size) }/* alloc shared memory pages */ -void *qemu_vmalloc (size_t size) +void *qemu_anon_ram_alloc (size_t size) {size_t align =QEMU_VMALLOC_ALIGN ;size_t total = size + align -getpagesize ();
@@ -125,7 +125,7 @@ void *qemu_vmalloc(size_t size)munmap (ptr + size, total - size); } -trace_qemu_vmalloc (size, ptr); +trace_qemu_anon_ram_alloc (size, ptr);return ptr; }
util/oslib-win32.c +2 -2
@@ -53,7 +53,7 @@ void *qemu_memalign(size_t alignment, size_t size)return ptr; } -void *qemu_vmalloc (size_t size) +void *qemu_anon_ram_alloc (size_t size) {void *ptr;
@@ -64,7 +64,7 @@ void *qemu_vmalloc(size_t size)abort (); } ptr =qemu_oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE )); -trace_qemu_vmalloc (size, ptr); +trace_qemu_anon_ram_alloc (size, ptr);return ptr; }
Signature · 1 line
1.8.2.1
PATCH 2/2 osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
In reply to Paolo BonziniWe switched from qemu_memalign to mmap() but then we don't modify qemu_vfree() to do a munmap() over free(). Which we cannot do because qemu_vfree() frees memory allocated by qemu_{mem,block}align.
Introduce a new function that does the munmap(), luckily the size is available in the RAMBlock.
--- exec.c | 8 ++------ include/qemu/osdep.h | 1 + trace-events | 1 + util/oslib-posix.c | 8 ++++++++ util/oslib-win32.c | 8 ++++++++ 5 files changed, 20 insertions(+), 6 deletions(-)
exec.c +2 -6
@@ -1156,21 +1156,17 @@ void qemu_ram_free(ram_addr_t addr)munmap (block->host, block->length); close(block->fd); }else { -qemu_vfree (block->host); +qemu_anon_ram_free (block->host, block->length); }#else abort ();#endif }else { -#if defined(TARGET_S390X ) && defined(CONFIG_KVM ) -munmap (block->host, block->length); -#else if (xen_enabled ()) {xen_invalidate_map_cache_entry (block->host); }else { -qemu_vfree (block->host); +qemu_anon_ram_free (block->host, block->length); } -#endif }g_free (block);break ;
include/qemu/osdep.h +1 -0
@@ -98,6 +98,7 @@ int qemu_daemon(int nochdir, int noclose);void *qemu_memalign (size_t alignment,size_t size);void *qemu_anon_ram_alloc (size_t size);void qemu_vfree (void *ptr); +void qemu_anon_ram_free (void *ptr,size_t size);#define QEMU_MADV_INVALID -1
trace-events +1 -0
@@ -34,6 +34,7 @@ g_free(void *ptr) "ptr %p" qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p" qemu_anon_ram_alloc(size_t size, void *ptr) "size %zu ptr %p" qemu_vfree(void *ptr) "ptr %p" +qemu_anon_ram_free(void *ptr, size_t size) "size %zu ptr %p" # hw/virtio.c virtqueue_fill(void *vq, const void *elem, unsigned int len, unsigned int idx) "vq %p elem %p len %u idx %u"
util/oslib-posix.c +8 -0
@@ -135,6 +135,14 @@ void qemu_vfree(void *ptr)free (ptr); } +void qemu_anon_ram_free (void *ptr,size_t size) +{ +trace_qemu_anon_ram_free (ptr, size); +if (ptr) { +munmap (ptr, size); + } +} +void qemu_set_block (int fd) {int f;
util/oslib-win32.c +8 -0
@@ -76,6 +76,14 @@ void qemu_vfree(void *ptr) } } +void qemu_anon_ram_free (void *ptr,size_t size) +{ +trace_qemu_anon_ram_free (ptr, size); +if (ptr) { +VirtualFree (ptr,0 ,MEM_RELEASE ); + } +} +/* FIXME: add proper locking */ struct tm *gmtime_r (const time_t *timep,struct tm *result) {
Signature · 1 line
1.8.2.1
Re: [Qemu-devel] [PATCH for-1.5 2/2] osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
include/qemu/osdep.h int qemu_daemon(int nochdir, int noclose); · line 101
Quoted 7 lines · Paolo Bonzini
include/qemu/osdep.h +1
-0@@ -98,6 +98,7 @@ int qemu_daemon(int nochdir, int noclose);void *qemu_memalign (size_t alignment,size_t size);void *qemu_anon_ram_alloc (size_t size);void qemu_vfree (void *ptr); +void qemu_anon_ram_free (void *ptr,size_t size);
No documentation comment?
thanks
-- PMM
Re: [Qemu-devel] [PATCH for-1.5 2/2] osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
Quoted 10 lines · Paolo Bonzini
include/qemu/osdep.h int qemu_daemon(int nochdir, int noclose); · line 101
Quoted 7 lines
include/qemu/osdep.h +1
-0@@ -98,6 +98,7 @@ int qemu_daemon(int nochdir, int noclose);void *qemu_memalign (size_t alignment,size_t size);void *qemu_anon_ram_alloc (size_t size);void qemu_vfree (void *ptr); +void qemu_anon_ram_free (void *ptr,size_t size);No documentation comment?
None in the proximity, either, and it's (a) pretty obvious that it matches qemu_anon_ram_alloc (b) not really commonly used, so it won't help a lot of readers.
Paolo
Re: [Qemu-devel] [PATCH for-1.5 2/2] osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
In reply to Paolo BonziniThis patch would break the build for me, if I didn't configure --disable-werror. Using --enable-trace-backend=stderr in case it makes a difference. Troublemaker flagged inline.
Quoted 6 lines · Paolo Bonzini
We switched from
qemu_memaligntommap()but then we don't modifyqemu_vfree()to do amunmap()overfree(). Which we cannot do becauseqemu_vfree()frees memory allocated by qemu_{mem,block}align.Introduce a new function that does the
munmap(), luckily the size is available in the RAMBlock.
[...]
trace-events g_free(void *ptr) "ptr %p" · line 37
Quoted 9 lines
trace-events +1
-0@@ -34,6 +34,7 @@ g_free(void *ptr) "ptr %p" qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p" qemu_anon_ram_alloc(size_t size, void *ptr) "size %zu ptr %p" qemu_vfree(void *ptr) "ptr %p" +qemu_anon_ram_free(void *ptr, size_t size) "size %zu ptr %p"
In file included from /work/armbru/qemu/include/trace.h:4:0,
from /work/armbru/qemu/util/osdep.c:49:
./trace/generated-tracers.h: In function 'trace_qemu_anon_ram_free':./trace/generated-tracers.h:64:9: warning: format '%zu' expects argument of type 'size_t', but argument 3 has type 'void *' [-Wformat] ./trace/generated-tracers.h:64:9: warning: format '%p' expects argument of type 'void *', but argument 4 has type 'size_t' [-Wformat]
Quoted 3 lines
trace-events +0
-0# hw/virtio.c virtqueue_fill(void *vq, const void *elem, unsigned int len, unsigned int idx) "vq %p elem %p len %u idx %u"
[...]
Re: [Qemu-devel] [PATCH for-1.5 2/2] osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
Il 15/05/2013 16:36, Markus Armbruster ha scritto:
Quoted 3 lines
This patch would break the build for me, if I didn't configure --disable-werror. Using --enable-trace-backend=stderr in case it makes a difference. Troublemaker flagged inline.
Yes, it does---and it makes it a bit less urgent. Will fix tomorrow.
Paolo
Re: [Qemu-devel] [PATCH for-1.5 2/2] osdep: introduce qemu_anon_ram_free to free qemu_anon_ram_alloc-ed memory
Am 15.05.2013 16:44, schrieb Paolo Bonzini:
Quoted 7 lines
Il 15/05/2013 16:36, Markus Armbruster ha scritto:
Quoted 3 lines
This patch would break the build for me, if I didn't configure --disable-werror. Using --enable-trace-backend=stderr in case it makes a difference. Troublemaker flagged inline.
Yes, it does---and it makes it a bit less urgent. Will fix tomorrow.
Paolo
I just got this error with the latest QEMU release candidate and have sent a patch to qemu-devel now.
Cheers, Stefan
Quoted 5 lines · Paolo Bonzini
Reported by Amos.
Paolo Bonzini (2):
osdep: renameqemu_vmalloctoqemu_anon_ram_alloc
osdep: introduceqemu_anon_ram_freeto free qemu_anon_ram_alloc-ed memory
Looks good to me, hotplug works now.
Quoted 14 lines
HACKING | 7 +++---- exec.c | 14 +++++--------- include/qemu/osdep.h | 3 ++- include/sysemu/kvm.h | 4 ++-- kvm-all.c | 6 +++--- target-s390x/kvm.c | 2 +- trace-events | 3 ++- util/oslib-posix.c | 12 ++++++++++-- util/oslib-win32.c | 12 ++++++++++-- 9 files changed, 38 insertions(+), 25 deletions(-)Signature · 1 line
1.8.2.1
Signature · 1 line
Amos.
Anthony Liguori
AppliedApplied. Thanks.
Regards,
Anthony Liguori