system/ramblock: Sanitize header
cover system/ramblock: Sanitize header
Usual API cleanups, here focusing on RAMBlock API:
move few prototypes out of "exec/cpu-common.h" and "system/ram_addr.h" to "system/ramblock.h".
Philippe Mathieu-Daudé (6):
system/ramblock: Remove obsolete comment
system/ramblock: Move ram_block_is_pmem() declaration system/ramblock: Move ram_block_discard_*_range() declarations system/ramblock: Use ram_addr_t in ram_block_discard_guest_memfd_range system/ramblock: Use ram_addr_t in ram_block_discard_range() system/ramblock: Move RAMBlock helpers out of "system/ram_addr.h"
include/exec/cpu-common.h | 3 -- include/system/ram_addr.h | 13 ------ include/system/ramblock.h | 27 +++++++++--- accel/kvm/kvm-all.c | 1 + hw/hyperv/hv-balloon-our_range_memslots.c | 1 + hw/virtio/virtio-balloon.c | 1 + hw/virtio/virtio-mem.c | 1 + migration/ram.c | 3 +- system/physmem.c | 50 ++++++++++++----------- 9 files changed, 55 insertions(+), 45 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/6 system/ramblock: Remove obsolete comment
This comment was added almost 5 years ago in commit 41aa4e9fd84 ("ram_addr: Split RAMBlock definition"). Clearly it got ignored:
$ git grep -l system/ramblock.h hw/display/virtio-gpu-udmabuf.c hw/hyperv/hv-balloon.c hw/virtio/vhost-user.c migration/dirtyrate.c migration/file.c migration/multifd-nocomp.c migration/multifd-qatzip.c migration/multifd-qpl.c migration/multifd-uadk.c migration/multifd-zero-page.c migration/multifd-zlib.c migration/multifd-zstd.c migration/multifd.c migration/postcopy-ram.c system/ram-block-attributes.c target/i386/kvm/tdx.c tests/qtest/fuzz/generic_fuzz.c
At this point it seems saner to just remove it.
--- include/system/ramblock.h | 5 ----- 1 file changed, 5 deletions(-)
include/system/ramblock.h +0 -5
@@ -11,11 +11,6 @@* */ -/* -* This header is for use by exec.c and memory.c ONLY. Do not include it. -* The functions declared here will be removed soon. -*/ -#ifndef SYSTEM_RAMBLOCK_H #define SYSTEM_RAMBLOCK_H
Signature · 1 line
2.51.0
Re: [PATCH 1/6] system/ramblock: Remove obsolete comment
Quoted 22 lines · Philippe Mathieu-Daudé
This comment was added almost 5 years ago in commit
41aa4e9fd84("ram_addr: Split RAMBlock definition"). Clearly it got ignored:$ git grep -l system/ramblock.h hw/display/virtio-gpu-udmabuf.c hw/hyperv/hv-balloon.c hw/virtio/vhost-user.c migration/dirtyrate.c migration/file.c migration/multifd-nocomp.c migration/multifd-qatzip.c migration/multifd-qpl.c migration/multifd-uadk.c migration/multifd-zero-page.c migration/multifd-zlib.c migration/multifd-zstd.c migration/multifd.c migration/postcopy-ram.c system/ram-block-attributes.c target/i386/kvm/tdx.c tests/qtest/fuzz/generic_fuzz.c
Quoted 4 lines
At this point it seems saner to just remove it.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Signature · 3 lines
Cheers David / dhildenb
PATCH 2/6 system/ramblock: Move ram_block_is_pmem() declaration
In reply to Philippe Mathieu-DaudéMove ramblock_is_pmem() along with the RAM Block API exposed by the "system/ramblock.h" header. Rename as ram_block_is_pmem() to keep API prefix consistency.
--- include/system/ram_addr.h | 2 -- include/system/ramblock.h | 5 +++++ migration/ram.c | 3 ++- system/physmem.c | 5 +++-- 4 files changed, 10 insertions(+), 5 deletions(-)
include/system/ram_addr.h +0 -2
@@ -99,8 +99,6 @@ static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,return host_addr_offset >>TARGET_PAGE_BITS ; } -bool ramblock_is_pmem (RAMBlock *rb); -/** * qemu_ram_alloc_from_file, * qemu_ram_alloc_from_fd: Allocate a ram block from the specified backing
include/system/ramblock.h +5 -0
@@ -108,4 +108,9 @@ void ram_block_attributes_destroy(RamBlockAttributes *attr);int ram_block_attributes_state_change (RamBlockAttributes *attr,uint64_t offset,uint64_t size,bool to_discard); +/** +* ramblock_is_pmem: Whether the RAM block is of persistent memory +*/ +bool ram_block_is_pmem (RAMBlock *rb); +#endif
migration/ram.c +2 -1
@@ -53,6 +53,7 @@#include "qemu/rcu_queue.h" #include "migration/colo.h" #include "system/cpu-throttle.h" +#include "system/ramblock.h" #include "savevm.h" #include "qemu/iov.h" #include "multifd.h"
@@ -4367,7 +4368,7 @@ static bool ram_has_postcopy(void *opaque) {RAMBlock *rb;RAMBLOCK_FOREACH_NOT_IGNORED (rb) { -if (ramblock_is_pmem (rb)) { +if (ram_block_is_pmem (rb)) {info_report ("Block: %s, host: %p is a nvdimm memory, postcopy" "is not supported now!" , rb->idstr, rb->host);return false;
system/physmem.c +3 -2
@@ -43,6 +43,7 @@#include "system/kvm.h" #include "system/tcg.h" #include "system/qtest.h" +#include "system/ramblock.h" #include "qemu/timer.h" #include "qemu/config-file.h" #include "qemu/error-report.h"
@@ -1804,7 +1805,7 @@ void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length)#ifdef CONFIG_LIBPMEM /* The lack of support for pmem should not block the sync */ -if (ramblock_is_pmem (block)) { +if (ram_block_is_pmem (block)) {void *addr =ramblock_ptr (block, start);pmem_persist (addr, length);return ;
@@ -3943,7 +3944,7 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,return ret; } -bool ramblock_is_pmem (RAMBlock *rb) +bool ram_block_is_pmem (RAMBlock *rb) {return rb->flags &RAM_PMEM ; }
Signature · 1 line
2.51.0
Re: [PATCH 2/6] system/ramblock: Move ram_block_is_pmem() declaration
Quoted 30 lines · Philippe Mathieu-Daudé
Move
ramblock_is_pmem()along with the RAM Block API exposed by the "system/ramblock.h" header. Rename asram_block_is_pmem()to keep API prefix consistency.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/system/ram_addr.h | 2 -- include/system/ramblock.h | 5 +++++ migration/ram.c | 3 ++- system/physmem.c | 5 +++-- 4 files changed, 10 insertions(+), 5 deletions(-)include/system/ram_addr.h +0
-2@@ -99,8 +99,6 @@ static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,return host_addr_offset >>TARGET_PAGE_BITS ; } -bool ramblock_is_pmem (RAMBlock *rb); -/** * qemu_ram_alloc_from_file, * qemu_ram_alloc_from_fd: Allocate a ram block from the specified backing include/system/ramblock.h +0
-0
include/system/ramblock.h void ram_block_attributes_destroy(RamBlockAttributes *attr); · line 112
Quoted 5 lines
include/system/ramblock.h +2
-0@@ -108,4 +108,9 @@ void ram_block_attributes_destroy(RamBlockAttributes *attr);int ram_block_attributes_state_change (RamBlockAttributes *attr,uint64_t offset,uint64_t size,bool to_discard); +/** +* ramblock_is_pmem: Whether the RAM block is of persistent memory
missed a rename
Otherwise:
Quoted 55 lines
include/system/ramblock.h +3
-0+*/ +bool ram_block_is_pmem (RAMBlock *rb); +#endif migration/ram.c +2
-1@@ -53,6 +53,7 @@#include "qemu/rcu_queue.h" #include "migration/colo.h" #include "system/cpu-throttle.h" +#include "system/ramblock.h" #include "savevm.h" #include "qemu/iov.h" #include "multifd.h" @@ -4367,7 +4368,7 @@ static bool ram_has_postcopy(void *opaque) {RAMBlock *rb;RAMBLOCK_FOREACH_NOT_IGNORED (rb) { -if (ramblock_is_pmem (rb)) { +if (ram_block_is_pmem (rb)) {info_report ("Block: %s, host: %p is a nvdimm memory, postcopy" "is not supported now!" , rb->idstr, rb->host);return false;system/physmem.c +3
-2@@ -43,6 +43,7 @@#include "system/kvm.h" #include "system/tcg.h" #include "system/qtest.h" +#include "system/ramblock.h" #include "qemu/timer.h" #include "qemu/config-file.h" #include "qemu/error-report.h" @@ -1804,7 +1805,7 @@ void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length)#ifdef CONFIG_LIBPMEM /* The lack of support for pmem should not block the sync */ -if (ramblock_is_pmem (block)) { +if (ram_block_is_pmem (block)) {void *addr =ramblock_ptr (block, start);pmem_persist (addr, length);return ;@@ -3943,7 +3944,7 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,return ret; } -bool ramblock_is_pmem (RAMBlock *rb) +bool ram_block_is_pmem (RAMBlock *rb) {return rb->flags &RAM_PMEM ; }
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 3/6 system/ramblock: Move ram_block_discard_*_range() declarations
In reply to Philippe Mathieu-DaudéKeep RAM blocks API in the same header: "system/ramblock.h".
--- include/exec/cpu-common.h | 3 --- include/system/ramblock.h | 4 ++++ accel/kvm/kvm-all.c | 1 + hw/hyperv/hv-balloon-our_range_memslots.c | 1 + hw/virtio/virtio-balloon.c | 1 + hw/virtio/virtio-mem.c | 1 + 6 files changed, 8 insertions(+), 3 deletions(-)
include/exec/cpu-common.h +0 -3
@@ -163,9 +163,6 @@ void cpu_flush_icache_range(hwaddr start, hwaddr len);typedef int (RAMBlockIterFunc )(RAMBlock *rb,void *opaque);int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque); -int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length); -int ram_block_discard_guest_memfd_range (RAMBlock *rb,uint64_t start, -size_t length);/* Returns: 0 on success, -1 on error */ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
include/system/ramblock.h +4 -0
@@ -103,6 +103,10 @@ struct RamBlockAttributes { QLIST_HEAD(, RamDiscardListener) rdl_list; }; +int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length); +int ram_block_discard_guest_memfd_range (RAMBlock *rb,uint64_t start, +size_t length); +RamBlockAttributes *ram_block_attributes_create (RAMBlock *ram_block);void ram_block_attributes_destroy (RamBlockAttributes *attr); int ram_block_attributes_state_change(RamBlockAttributes *attr, uint64_t offset,
accel/kvm/kvm-all.c +1 -0
@@ -32,6 +32,7 @@#include "system/runstate.h" #include "system/cpus.h" #include "system/accel-blocker.h" +#include "system/ramblock.h" #include "accel/accel-ops.h" #include "qemu/bswap.h" #include "exec/tswap.h"
hw/hyperv/hv-balloon-our_range_memslots.c +1 -0
@@ -8,6 +8,7 @@*/ #include "qemu/osdep.h" +#include "system/ramblock.h" #include "hv-balloon-internal.h" #include "hv-balloon-our_range_memslots.h" #include "trace.h"
hw/virtio/virtio-balloon.c +1 -0
@@ -23,6 +23,7 @@#include "hw/qdev-properties.h" #include "hw/boards.h" #include "system/balloon.h" +#include "system/ramblock.h" #include "hw/virtio/virtio-balloon.h" #include "system/address-spaces.h" #include "qapi/error.h"
hw/virtio/virtio-mem.c +1 -0
@@ -17,6 +17,7 @@#include "qemu/units.h" #include "system/numa.h" #include "system/system.h" +#include "system/ramblock.h" #include "system/reset.h" #include "system/runstate.h" #include "hw/virtio/virtio.h"
Signature · 1 line
2.51.0
PATCH 4/6 system/ramblock: Use ram_addr_t in ram_block_discard_guest_memfd_range
In reply to Philippe Mathieu-DaudéRename @start as @offset. Since it express an offset within a RAMBlock, use the ram_addr_t type to make emphasis on the QEMU intermediate address space represented.
--- include/system/ramblock.h | 3 ++- system/physmem.c | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-)
include/system/ramblock.h +2 -1
@@ -104,7 +104,8 @@ struct RamBlockAttributes { };int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length); -int ram_block_discard_guest_memfd_range (RAMBlock *rb,uint64_t start, +/* @offset: the offset within the RAMBlock */ +int ram_block_discard_guest_memfd_range (RAMBlock *rb,ram_addr_t offset,size_t length);RamBlockAttributes *ram_block_attributes_create (RAMBlock *ram_block);
system/physmem.c +6 -6
@@ -3920,7 +3920,7 @@ err:return ret; } -int ram_block_discard_guest_memfd_range (RAMBlock *rb,uint64_t start, +int ram_block_discard_guest_memfd_range (RAMBlock *rb,ram_addr_t offset,size_t length) {int ret =-1 ;
@@ -3928,17 +3928,17 @@ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,#ifdef CONFIG_FALLOCATE_PUNCH_HOLE /* ignore fd_offset with guest_memfd */ ret =fallocate (rb->guest_memfd,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , - start, length); + offset, length);if (ret) { ret = -errno; -error_report ("%s: Failed to fallocate %s:%" PRIx64" +%zx (%d)" , - __func__, rb->idstr, start, length, ret); +error_report ("%s: Failed to fallocate %s:" RAM_ADDR_FMT " +%zx (%d)" , + __func__, rb->idstr, offset, length, ret); }#else ret = -ENOSYS ; -error_report ("%s: fallocate not available %s:%" PRIx64" +%zx (%d)" , - __func__, rb->idstr, start, length, ret); +error_report ("%s: fallocate not available %s:" RAM_ADDR_FMT " +%zx (%d)" , + __func__, rb->idstr, offset, length, ret);#endif return ret;
Signature · 1 line
2.51.0
Re: [PATCH 4/6] system/ramblock: Use ram_addr_t in ram_block_discard_guest_memfd_range
Quoted 15 lines · Philippe Mathieu-Daudé
Rename @start as @offset. Since it express an offset within a RAMBlock, use the
ram_addr_ttype to make emphasis on the QEMU intermediate address space represented.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/system/ramblock.h | 3 ++- system/physmem.c | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-)include/system/ramblock.h +0
-0
include/system/ramblock.h struct RamBlockAttributes { · line 109
Quoted 7 lines
include/system/ramblock.h +2
-1@@ -104,7 +104,8 @@ struct RamBlockAttributes { };int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length); -int ram_block_discard_guest_memfd_range (RAMBlock *rb,uint64_t start, +/* @offset: the offset within the RAMBlock */ +int ram_block_discard_guest_memfd_range (RAMBlock *rb,ram_addr_t offset,size_t length);
This isn't a ram_addr_t, it's an offset.
You can't pass the value to one of the lookup functions, for instance.
Though I suppose 80% of the ram_addr.h interface uses ram_addr_t for lots of things that aren't.
r~
PATCH 5/6 system/ramblock: Use ram_addr_t in ram_block_discard_range()
In reply to Philippe Mathieu-DaudéRename @start as @offset. Since it express an offset within a RAMBlock, use the ram_addr_t type to make emphasis on the QEMU intermediate address space represented.
--- include/system/ramblock.h | 3 ++- system/physmem.c | 33 ++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-)
include/system/ramblock.h +2 -1
@@ -103,7 +103,8 @@ struct RamBlockAttributes { QLIST_HEAD(, RamDiscardListener) rdl_list; }; -int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length); +/* @offset: the offset within the RAMBlock */ +int ram_block_discard_range (RAMBlock *rb,ram_addr_t offset,size_t length);/* @offset: the offset within the RAMBlock */ int ram_block_discard_guest_memfd_range (RAMBlock *rb,ram_addr_t offset,size_t length);
system/physmem.c +18 -15
@@ -3797,18 +3797,18 @@ int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque) }/* -* Unmap pages of memory from start to start+length such that +* Unmap pages of memory from offset to offset+length such that * they a) read as 0, b) Trigger whatever fault mechanism * the OS provides for postcopy. * The pages must be unmapped by the end of the function. * Returns: 0 on success, none-0 on failure * */ -int ram_block_discard_range (RAMBlock *rb,uint64_t start,size_t length) +int ram_block_discard_range (RAMBlock *rb,ram_addr_t offset,size_t length) {int ret =-1 ; -uint8_t *host_startaddr = rb->host + start; +uint8_t *host_startaddr = rb->host + offset;if (!QEMU_PTR_IS_ALIGNED (host_startaddr, rb->page_size)) { error_report("%s: Unaligned start address: %p",
@@ -3816,7 +3816,7 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length) goto err; } -if ((start + length) <= rb->max_length) { +if ((offset + length) <= rb->max_length) {bool need_madvise, need_fallocate;if (!QEMU_IS_ALIGNED (length, rb->page_size)) {error_report ("%s: Unaligned length: %zx" , __func__, length);
@@ -3867,19 +3867,20 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length) } ret =fallocate (rb->fd,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , - start + rb->fd_offset, length); + offset + rb->fd_offset, length);if (ret) { ret = -errno; -error_report ("%s: Failed to fallocate %s:%" PRIx64"+%" PRIx64 -" +%zx (%d)" , __func__, rb->idstr, start, +error_report ("%s: Failed to fallocate %s:" +RAM_ADDR_FMT "+%" PRIx64" +%zx (%d)" , + __func__, rb->idstr, offset, rb->fd_offset, length, ret); goto err; }#else ret = -ENOSYS ;error_report ("%s: fallocate not available/file" -"%s:%" PRIx64"+%" PRIx64" +%zx (%d)" , __func__, - rb->idstr, start, rb->fd_offset, length, ret); +"%s:" RAM_ADDR_FMT "+%" PRIx64" +%zx (%d)" , __func__, + rb->idstr, offset, rb->fd_offset, length, ret); goto err;#endif }
@@ -3898,22 +3899,24 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)if (ret) { ret = -errno;error_report ("%s: Failed to discard range " -"%s:%" PRIx64" +%zx (%d)" , - __func__, rb->idstr, start, length, ret); +"%s:" RAM_ADDR_FMT " +%zx (%d)" , + __func__, rb->idstr, offset, length, ret); goto err; }#else ret = -ENOSYS ; -error_report ("%s: MADVISE not available %s:%" PRIx64" +%zx (%d)" , - __func__, rb->idstr, start, length, ret); +error_report ("%s: MADVISE not available %s:" +RAM_ADDR_FMT " +%zx (%d)" , + __func__, rb->idstr, offset, length, ret); goto err;#endif }trace_ram_block_discard_range (rb->idstr, host_startaddr, length, need_madvise, need_fallocate, ret); }else { -error_report ("%s: Overrun block '%s' (%" PRIu64"/%zx/" RAM_ADDR_FMT ")" , - __func__, rb->idstr, start, length, rb->max_length); +error_report ("%s: Overrun block '%s' " +"(" RAM_ADDR_FMT "/%zx/" RAM_ADDR_FMT ")" , + __func__, rb->idstr, offset, length, rb->max_length); } err:
Signature · 1 line
2.51.0
PATCH 6/6 system/ramblock: Move RAMBlock helpers out of "system/ram_addr.h"
In reply to Philippe Mathieu-Daudé--- include/system/ram_addr.h | 11 ----------- include/system/ramblock.h | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-)
include/system/ram_addr.h +0 -11
@@ -80,17 +80,6 @@ static inline bool clear_bmap_test_and_clear(RAMBlock *rb, uint64_t page)return bitmap_test_and_clear (rb->clear_bmap, page >> shift,1 ); } -static inline bool offset_in_ramblock (RAMBlock *b,ram_addr_t offset) -{ -return (b && b->host && offset < b->used_length) ? true : false; -} - -static inline void *ramblock_ptr (RAMBlock *block,ram_addr_t offset) -{ -assert (offset_in_ramblock (block, offset)); -return (char *)block->host + offset; -} -static inline unsigned long int ramblock_recv_bitmap_offset (void *host_addr,RAMBlock *rb) {
include/system/ramblock.h +11 -0
@@ -119,4 +119,15 @@ int ram_block_attributes_state_change(RamBlockAttributes *attr, uint64_t offset,*/ bool ram_block_is_pmem (RAMBlock *rb); +static inline bool offset_in_ramblock (RAMBlock *b,ram_addr_t offset) +{ +return (b && b->host && offset < b->used_length) ? true : false; +} + +static inline void *ramblock_ptr (RAMBlock *block,ram_addr_t offset) +{ +assert (offset_in_ramblock (block, offset)); +return (char *)block->host + offset; +} +#endif
Signature · 1 line
2.51.0
Re: [PATCH 6/6] system/ramblock: Move RAMBlock helpers out of "system/ram_addr.h"
Quoted 5 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/system/ram_addr.h | 11 ----------- include/system/ramblock.h | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-)
include/system/ramblock.h int ram_block_attributes_state_change(RamBlockAttributes *attr, uint64_t offset, · line 124
Quoted 3 lines
include/system/ramblock.h +3
-0+static inline bool offset_in_ramblock (RAMBlock *b,ram_addr_t offset) +{ +return (b && b->host && offset < b->used_length) ? true : false;
... though we could stand to clean up "? true : false" silliness.
I assume this comes from programmers who first learned something other than C, but I can't work out what language that might be. I see it in patches every once in a while.
r~