[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL v2 22/50] migrate: Update ram_block_discard_range for
From: |
Michael S. Tsirkin |
Subject: |
[Qemu-devel] [PULL v2 22/50] migrate: Update ram_block_discard_range for shared |
Date: |
Tue, 20 Mar 2018 05:17:34 +0200 |
From: "Dr. David Alan Gilbert" <address@hidden>
The choice of call to discard a block is getting more complicated
for other cases. We use fallocate PUNCH_HOLE in any file cases;
it works for both hugepage and for tmpfs.
We use the DONTNEED for non-hugepage cases either where they're
anonymous or where they're private.
Care should be taken when trying other backing files.
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
Reviewed-by: Peter Xu <address@hidden>
Reviewed-by: Michael S. Tsirkin <address@hidden>
Signed-off-by: Michael S. Tsirkin <address@hidden>
---
exec.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++--------------
trace-events | 3 ++-
2 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/exec.c b/exec.c
index a9181e6..34fdfd9 100644
--- a/exec.c
+++ b/exec.c
@@ -3721,6 +3721,7 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t start,
size_t length)
}
if ((start + length) <= rb->used_length) {
+ bool need_madvise, need_fallocate;
uint8_t *host_endaddr = host_startaddr + length;
if ((uintptr_t)host_endaddr & (rb->page_size - 1)) {
error_report("ram_block_discard_range: Unaligned end address: %p",
@@ -3730,29 +3731,60 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t
start, size_t length)
errno = ENOTSUP; /* If we are missing MADVISE etc */
- if (rb->page_size == qemu_host_page_size) {
-#if defined(CONFIG_MADVISE)
- /* Note: We need the madvise MADV_DONTNEED behaviour of definitely
- * freeing the page.
- */
- ret = madvise(host_startaddr, length, MADV_DONTNEED);
-#endif
- } else {
- /* Huge page case - unfortunately it can't do DONTNEED, but
- * it can do the equivalent by FALLOC_FL_PUNCH_HOLE in the
- * huge page file.
+ /* The logic here is messy;
+ * madvise DONTNEED fails for hugepages
+ * fallocate works on hugepages and shmem
+ */
+ need_madvise = (rb->page_size == qemu_host_page_size);
+ need_fallocate = rb->fd != -1;
+ if (need_fallocate) {
+ /* For a file, this causes the area of the file to be zero'd
+ * if read, and for hugetlbfs also causes it to be unmapped
+ * so a userfault will trigger.
*/
#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
start, length);
+ if (ret) {
+ ret = -errno;
+ error_report("ram_block_discard_range: Failed to fallocate "
+ "%s:%" PRIx64 " +%zx (%d)",
+ rb->idstr, start, length, ret);
+ goto err;
+ }
+#else
+ ret = -ENOSYS;
+ error_report("ram_block_discard_range: fallocate not
available/file"
+ "%s:%" PRIx64 " +%zx (%d)",
+ rb->idstr, start, length, ret);
+ goto err;
#endif
}
- if (ret) {
- ret = -errno;
- error_report("ram_block_discard_range: Failed to discard range "
+ if (need_madvise) {
+ /* For normal RAM this causes it to be unmapped,
+ * for shared memory it causes the local mapping to disappear
+ * and to fall back on the file contents (which we just
+ * fallocate'd away).
+ */
+#if defined(CONFIG_MADVISE)
+ ret = madvise(host_startaddr, length, MADV_DONTNEED);
+ if (ret) {
+ ret = -errno;
+ error_report("ram_block_discard_range: Failed to discard range
"
+ "%s:%" PRIx64 " +%zx (%d)",
+ rb->idstr, start, length, ret);
+ goto err;
+ }
+#else
+ ret = -ENOSYS;
+ error_report("ram_block_discard_range: MADVISE not available"
"%s:%" PRIx64 " +%zx (%d)",
rb->idstr, start, length, ret);
+ goto err;
+#endif
}
+ trace_ram_block_discard_range(rb->idstr, host_startaddr, length,
+ need_madvise, need_fallocate, ret);
} else {
error_report("ram_block_discard_range: Overrun block '%s' (%" PRIu64
"/%zx/" RAM_ADDR_FMT")",
diff --git a/trace-events b/trace-events
index 855b0ab..2c3e3d7 100644
--- a/trace-events
+++ b/trace-events
@@ -55,9 +55,10 @@ dma_complete(void *dbs, int ret, void *cb) "dbs=%p ret=%d
cb=%p"
dma_blk_cb(void *dbs, int ret) "dbs=%p ret=%d"
dma_map_wait(void *dbs) "dbs=%p"
-# # exec.c
+# exec.c
find_ram_offset(uint64_t size, uint64_t offset) "size: 0x%" PRIx64 " @ 0x%"
PRIx64
find_ram_offset_loop(uint64_t size, uint64_t candidate, uint64_t offset,
uint64_t next, uint64_t mingap) "trying size: 0x%" PRIx64 " @ 0x%" PRIx64 ",
offset: 0x%" PRIx64" next: 0x%" PRIx64 " mingap: 0x%" PRIx64
+ram_block_discard_range(const char *rbname, void *hva, size_t length, bool
need_madvise, bool need_fallocate, int ret) "address@hidden + 0x%zx: madvise:
%d fallocate: %d ret: %d"
# memory.c
memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value,
unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
--
MST
- [Qemu-devel] [PULL v2 13/50] tests: acpi: don't read all fields in test_acpi_fadt_table(), (continued)
- [Qemu-devel] [PULL v2 13/50] tests: acpi: don't read all fields in test_acpi_fadt_table(), Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 14/50] standard-headers: update virtio_net.h, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 16/50] pc-dimm: make qmp_pc_dimm_device_list() sort devices by address, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 19/50] tests/bios-tables-test: add test cases for DIMM proximity, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 18/50] hw/acpi-build: build SRAT memory affinity structures for DIMM devices, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 17/50] qmp: distinguish PC-DIMM and NVDIMM in MemoryDeviceInfoList, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 12/50] virt_arm: acpi: reuse common build_fadt(), Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 11/50] acpi: move build_fadt() from i386 specific to generic ACPI source, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 20/50] test/acpi-test-data: add ACPI tables for dimmpxm test, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 21/50] Makefile: add target to print generated files, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 22/50] migrate: Update ram_block_discard_range for shared,
Michael S. Tsirkin <=
- [Qemu-devel] [PULL v2 23/50] qemu_ram_block_host_offset, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 26/50] postcopy: Add vhost-user flag for postcopy and check it, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 24/50] postcopy: use UFFDIO_ZEROPAGE only when available, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 25/50] postcopy: Add notifier chain, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 27/50] vhost-user: Add 'VHOST_USER_POSTCOPY_ADVISE' message, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 28/50] libvhost-user: Support sending fds back to qemu, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 29/50] libvhost-user: Open userfaultfd, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 31/50] vhost+postcopy: Register shared ufd with postcopy, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 32/50] vhost+postcopy: Transmit 'listen' to slave, Michael S. Tsirkin, 2018/03/19
- [Qemu-devel] [PULL v2 33/50] postcopy+vhost-user: Split set_mem_table for postcopy, Michael S. Tsirkin, 2018/03/19