Thanks to visit codestin.com
Credit goes to github.com

Skip to content

vmm: Prefault snapshot pages in background - #8150

Merged
rbradford merged 1 commit into
cloud-hypervisor:mainfrom
sboeuf:uffd_prefault
May 19, 2026
Merged

rbradford merged 1 commit into
cloud-hypervisor:mainfrom
sboeuf:uffd_prefault

Conversation

@sboeuf

@sboeuf sboeuf commented May 6, 2026

Copy link
Copy Markdown
Member

Userfaultfd is a great mechanism for providing fast restore to Cloud
Hypervisor VMs. But that means the price to pay for bringing pages in
happens at runtime, which might slow down the guest when it's touching
pages which haven't been brought in yet.

By prefaulting the pages in the background, we're trying to get the best
of both worlds. That means we still get a very fast restore with the
uffd handler, but within a few seconds (depending on VM's RAM size), we
also get the pages fully faulted and we can stop the uffd handler thread
at that point.

@sboeuf
sboeuf requested a review from a team as a code owner May 6, 2026 15:25
@sboeuf

sboeuf commented May 6, 2026

Copy link
Copy Markdown
Member Author

/cc @shayonj

@rbradford rbradford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the user triggers another snapshot or live migration whilst this thread is still alive (this was quite possibly a problem before the thread was added.)

@sboeuf

sboeuf commented May 7, 2026

Copy link
Copy Markdown
Member Author

What happens if the user triggers another snapshot or live migration whilst this thread is still alive (this was quite possibly a problem before the thread was added.)

@rbradford

This is a good question and actually this is now less of a problem with the new thread. Given we're actively bringing the pages in with the prefaulting thread, after a few seconds (minutes if the VM RAM size is huge), the uffd thread is going to complete. This was almost never the case before, unless the guest was actively touching every single page.

About the underlying problem that you have mentioned, if not all the pages have been faulted yet when a new snapshot occurs, I'd expect the snapshot to be slowed down but still should succeed. The slowdown would come from the fact the VMM would be accessing the pages to copy them into the new snapshot file, but some pages might not be there yet, and this would trigger the uffd handler.

Or am I mistaken and would an access to the guest RAM from the VMM not trigger the uffd handler?

Comment thread vmm/src/memory_manager.rs Outdated
let page_addr = range.host_addr + i * range.page_size;
let len = range.page_size as usize;

if let Err(e) = snapshot_file.read_at(&mut page_buf[..len], file_pos) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you also use read_exact_at here?

Comment thread vmm/src/memory_manager.rs Outdated
let mut pages_existed: u64 = 0;

for range in ranges {
let num_pages = range.length / range.page_size;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use div_ceil for page count calculations (than floor division.) That would match the other code in this file.

Comment thread vmm/src/memory_manager.rs Outdated
"UFFD prefault: done in {elapsed:.3?} — installed={pages_installed} \
existed={pages_existed} total={total_pages}"
);
stop_event.write(1).ok();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if this were called "done" or "completed" stop sounds like a verb - that would be used to stop the execution rather than to say it's completed.

Comment thread vmm/src/memory_manager.rs Outdated
Comment on lines +1116 to +1117
let mut pages_installed: u64 = 0;
let mut pages_existed: u64 = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also find the naming of these slightly confusing?

@sboeuf

sboeuf commented May 15, 2026

Copy link
Copy Markdown
Member Author

@rbradford I've updated the PR. It doesn't introduce a dedicated thread anymore, as it reuses the exiting one, faulting one page per iteration. And also introduced a bitmap to track the pages which have been previously faulted so that we don't copy them again from the prefault handler.

@sboeuf
sboeuf force-pushed the uffd_prefault branch 2 times, most recently from 72264a3 to 20d5a6f Compare May 15, 2026 18:29

@rbradford rbradford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like we don't have an extra thread.

Comment thread vmm/src/memory_manager.rs Outdated
Comment thread vmm/src/memory_manager.rs
Comment thread vmm/src/memory_manager.rs
@sboeuf
sboeuf requested a review from rbradford May 19, 2026 08:29
Userfaultfd is a great mechanism for providing fast restore to Cloud
Hypervisor VMs. But that means the price to pay for bringing pages in
happens at runtime, which might slow down the guest when it's touching
pages which haven't been brought in yet.

By prefaulting the pages in the background, we're trying to get the best
of both worlds. That means we still get a very fast restore with the
uffd handler, but within a few seconds (depending on VM's RAM size), we
also get the pages fully faulted and we can stop the uffd handler thread
at that point.

Signed-off-by: Sebastien Boeuf <[email protected]>
Assisted-by: Claude:claude-opus-4-7

@rbradford rbradford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@rbradford
rbradford added this pull request to the merge queue May 19, 2026
@shayonj

shayonj commented May 19, 2026

Copy link
Copy Markdown
Contributor

Apologies for being late to the party. I have been busy at work. I love the approach and skimmed it and looks good to me. I have nothing useful to add, if anything I can always follow up. Thank you for the contribution and giving this a good design shape 🙏🏾

Merged via the queue into cloud-hypervisor:main with commit 6ef9b58 May 19, 2026
41 checks passed
@rbradford rbradford added the new-feature New feature to include in release notes label Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New feature to include in release notes

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

4 participants