Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177847689
D61072.1795422316.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D61072.1795422316.diff
View Options
diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c
--- a/usr.sbin/bhyve/snapshot.c
+++ b/usr.sbin/bhyve/snapshot.c
@@ -10,6 +10,11 @@
* All rights reserved.
* The bhyve-snapshot feature was developed under sponsorships
* from Matthew Grooms.
+ * Copyright (c) 2026 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by
+ * Alchemilla Ventures Private Limited <hayzam@alchemilla.io> under
+ * sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -33,7 +38,7 @@
* SUCH DAMAGE.
*/
-#include <sys/types.h>
+#include <sys/param.h>
#ifndef WITHOUT_CAPSICUM
#include <sys/capsicum.h>
#endif
@@ -68,6 +73,7 @@
#include <machine/vmm_dev.h>
#endif
#include <machine/vmm_snapshot.h>
+#include <dev/vmm/vmm_dev.h>
#include <vmmapi.h>
#include "bhyverun.h"
@@ -653,22 +659,141 @@
return NULL;
}
+/*
+ * Reading or writing guest memory faults in pages that the guest never
+ * touched or that were discarded, for example by a balloon device. To avoid
+ * that, saving writes zeroes for pages that the kernel reports as
+ * unpopulated without reading them, and restoring leaves unpopulated pages
+ * alone if their saved contents are zero. Saving must not modify guest
+ * memory, since devices without a pause method, such as e1000, can write to
+ * it during a checkpoint.
+ */
+_Static_assert(SNAPSHOT_CHUNK / PAGE_SIZE <= VM_POPULATED_PAGES_MAX_PAGES,
+ "a chunk must fit in one population query");
+
+static bool
+vm_snapshot_page_is_zero(const char *page)
+{
+ const uint64_t *p;
+ size_t i;
+
+ p = (const uint64_t *)(const void *)page;
+ for (i = 0; i < PAGE_SIZE / sizeof(*p); i++) {
+ if (p[i] != 0)
+ return (false);
+ }
+ return (true);
+}
+
static int
-vm_snapshot_mem_part(const int snapfd, const size_t foff, void *src,
- const size_t len, const size_t totalmem, const bool op_wr)
+vm_snapshot_mem_io(int snapfd, void *buf, size_t len, bool op_wr)
{
- int rc;
- size_t part_done, todo, rem;
ssize_t done;
+
+ while (len > 0) {
+ if (op_wr)
+ done = write(snapfd, buf, len);
+ else
+ done = read(snapfd, buf, len);
+ if (done <= 0) {
+ if (done == 0)
+ errno = EIO;
+ perror(op_wr ? "Failed to write guest memory" :
+ "Failed to read guest memory");
+ return (-1);
+ }
+ buf = (char *)buf + done;
+ len -= done;
+ }
+ return (0);
+}
+
+/*
+ * Find the populated pages of a chunk of guest memory. If the kernel cannot
+ * tell, for example because the chunk spans two memory segments, report
+ * every page as populated.
+ */
+static void
+vm_snapshot_populated(struct vmctx *ctx, vm_paddr_t gpa, size_t len,
+ uint64_t *map)
+{
+
+ if (vm_get_populated_pages(ctx, gpa, len, map) != 0)
+ memset(map, 0xff, howmany(len / PAGE_SIZE, 64) * sizeof(*map));
+}
+
+static bool
+vm_snapshot_page_populated(const uint64_t *map, size_t i)
+{
+
+ return ((map[i / 64] & ((uint64_t)1 << (i % 64))) != 0);
+}
+
+static int
+vm_snapshot_mem_save(struct vmctx *ctx, int snapfd, char *src,
+ vm_paddr_t gpa, size_t len, char *zero)
+{
+ uint64_t map[howmany(SNAPSHOT_CHUNK / PAGE_SIZE, 64)];
+ size_t i, j, npages;
+ bool populated;
+
+ vm_snapshot_populated(ctx, gpa, len, map);
+ npages = len / PAGE_SIZE;
+ for (i = 0; i < npages; i = j) {
+ populated = vm_snapshot_page_populated(map, i);
+ for (j = i + 1; j < npages &&
+ vm_snapshot_page_populated(map, j) == populated; j++)
+ ;
+ if (vm_snapshot_mem_io(snapfd, populated ?
+ src + i * PAGE_SIZE : zero, (j - i) * PAGE_SIZE, true) != 0)
+ return (-1);
+ }
+ return (0);
+}
+
+static int
+vm_snapshot_mem_restore(struct vmctx *ctx, int snapfd, char *dst,
+ vm_paddr_t gpa, size_t len, char *buf)
+{
+ uint64_t map[howmany(SNAPSHOT_CHUNK / PAGE_SIZE, 64)];
+ size_t i;
+
+ if (vm_snapshot_mem_io(snapfd, buf, len, false) != 0)
+ return (-1);
+ vm_snapshot_populated(ctx, gpa, len, map);
+ for (i = 0; i < len / PAGE_SIZE; i++) {
+ if (vm_snapshot_page_populated(map, i) ||
+ !vm_snapshot_page_is_zero(buf + i * PAGE_SIZE))
+ memcpy(dst + i * PAGE_SIZE, buf + i * PAGE_SIZE,
+ PAGE_SIZE);
+ }
+ return (0);
+}
+
+static int
+vm_snapshot_mem_part(struct vmctx *ctx, const int snapfd, const size_t foff,
+ char *addr, vm_paddr_t gpa, const size_t len, const size_t totalmem,
+ const bool op_wr)
+{
+ int rc, ret;
+ size_t part_done, todo, rem;
bool show_progress;
pthread_t spinner_th;
struct spinner_info *si;
+ char *buf;
if (lseek(snapfd, foff, SEEK_SET) < 0) {
perror("Failed to change file offset");
return (-1);
}
+ /* Saving needs zeroes, restoring needs a bounce buffer. */
+ buf = op_wr ? calloc(1, SNAPSHOT_CHUNK) : malloc(SNAPSHOT_CHUNK);
+ if (buf == NULL) {
+ perror("Failed to allocate guest memory buffer");
+ return (-1);
+ }
+
show_progress = false;
if (isatty(STDIN_FILENO) && (winsize.ws_col != 0))
show_progress = true;
@@ -690,24 +815,25 @@
}
}
+ ret = 0;
while (rem > 0) {
- if (show_progress)
- todo = min(SNAPSHOT_CHUNK, rem);
- else
- todo = rem;
-
+ todo = min(SNAPSHOT_CHUNK, rem);
if (op_wr)
- done = write(snapfd, src, todo);
+ ret = vm_snapshot_mem_save(ctx, snapfd, addr, gpa,
+ todo, buf);
else
- done = read(snapfd, src, todo);
- if (done < 0) {
- perror("Failed to write in file");
- return (-1);
+ ret = vm_snapshot_mem_restore(ctx, snapfd, addr, gpa,
+ todo, buf);
+ if (ret != 0) {
+ /* Let the spinner thread finish. */
+ part_done = foff + len;
+ break;
}
- src = (uint8_t *)src + done;
- part_done += done;
- rem -= done;
+ addr += todo;
+ gpa += todo;
+ part_done += todo;
+ rem -= todo;
}
if (show_progress) {
@@ -716,7 +842,8 @@
perror("Unable to end spinner thread");
}
- return (0);
+ free(buf);
+ return (ret);
}
static size_t
@@ -746,7 +873,7 @@
#endif /* TIOCGWINSZ */
old_winch_handler = signal(SIGWINCH, winch_handler);
- ret = vm_snapshot_mem_part(snapfd, 0, baseaddr, lowmem,
+ ret = vm_snapshot_mem_part(ctx, snapfd, 0, baseaddr, 0, lowmem,
totalmem, op_wr);
if (ret) {
fprintf(stderr, "%s: Could not %s lowmem\r\n",
@@ -758,8 +885,9 @@
if (highmem == 0)
goto done;
- ret = vm_snapshot_mem_part(snapfd, lowmem,
- baseaddr + vm_get_highmem_base(ctx), highmem, totalmem, op_wr);
+ ret = vm_snapshot_mem_part(ctx, snapfd, lowmem,
+ baseaddr + vm_get_highmem_base(ctx), vm_get_highmem_base(ctx),
+ highmem, totalmem, op_wr);
if (ret) {
fprintf(stderr, "%s: Could not %s highmem\r\n",
__func__, op_wr ? "write" : "read");
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 23, 8:25 AM (31 m, 19 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41272033
Default Alt Text
D61072.1795422316.diff (6 KB)
Attached To
Mode
D61072: bhyve: Avoid populating guest memory in checkpoint and restore
Attached
Detach File
Event Timeline