Skip to content

Commit 719168f

Browse files
Steve SistareFabiano Rosas
authored andcommitted
physmem: fix qemu_ram_alloc_from_fd size calculation
qemu_ram_alloc_from_fd allocates space if file_size == 0. If non-zero, it uses the existing space and verifies it is large enough, but the verification was broken when the offset parameter was introduced. As a result, a file smaller than offset passes the verification and causes errors later. Fix that, and update the error message to include offset. Peter provides this concise reproducer: $ touch ramfile $ truncate -s 64M ramfile $ ./qemu-system-x86_64 -object memory-backend-file,mem-path=./ramfile,offset=128M,size=128M,id=mem1,prealloc=on qemu-system-x86_64: qemu_prealloc_mem: preallocating memory failed: Bad address With the fix, the error message is: qemu-system-x86_64: mem1 backing store size 0x4000000 is too small for 'size' option 0x8000000 plus 'offset' option 0x8000000 Cc: [email protected] Fixes: 4b870dc ("hostmem-file: add offset option") Signed-off-by: Steve Sistare <[email protected]> Reviewed-by: Peter Xu <[email protected]> Acked-by: David Hildenbrand <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Fabiano Rosas <[email protected]>
1 parent 57ad6ab commit 719168f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

system/physmem.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,10 +1970,12 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
19701970
size = REAL_HOST_PAGE_ALIGN(size);
19711971

19721972
file_size = get_file_size(fd);
1973-
if (file_size > offset && file_size < (offset + size)) {
1974-
error_setg(errp, "backing store size 0x%" PRIx64
1975-
" does not match 'size' option 0x" RAM_ADDR_FMT,
1976-
file_size, size);
1973+
if (file_size && file_size < offset + size) {
1974+
error_setg(errp, "%s backing store size 0x%" PRIx64
1975+
" is too small for 'size' option 0x" RAM_ADDR_FMT
1976+
" plus 'offset' option 0x%" PRIx64,
1977+
memory_region_name(mr), file_size, size,
1978+
(uint64_t)offset);
19771979
return NULL;
19781980
}
19791981

0 commit comments

Comments
 (0)