
Recall first from EPT, NPT and stage-2 hardware paging
- Hardware applies the second translation stage to guest page-table accesses as well as to the final guest physical address.
- EPTP and nCR3 identify x86 second-stage roots, while Arm uses VTTBR_EL2 and RISC-V uses hgatp.
- An EPT leaf supplies the host physical base and its position in the hierarchy determines the offset width.
Why page-table edits stop causing exits
3 min readThe last lesson built the EPT and stage-2 tables. This lesson answers why they win over shadow paging, and what they cost in TLB walks and dirty/accessed-bit tracking.
Consider a Linux guest writing to a newly allocated anonymous page. The first store can raise a guest page fault. The guest kernel allocates a page and writes a page-table entry so the retried store can succeed. Now repeat that operation over a large allocation. The Linux KVM MMU documentation describes the practical problem for shadow paging: KVM has to keep the hardware-visible shadow translations consistent as the guest changes its own tables. Depending on the state of a shadowed page, KVM may write-protect it and intercept edits, or defer synchronization until a guest operation that requires translations to be coherent. The extra work can include decoding a write, invalidating stale mappings, and rebuilding translations.
With Intel EPT, AMD NPT, or Arm stage-2, the processor uses the guest entry itself when it needs a new translation. A normal store to a guest page-table page goes through the same stage-2 permissions as other guest RAM. If stage 2 permits it, the store completes without a VM exit for shadow synchronization. Guest page-table churn therefore matters even if the application never invokes a hypervisor interface. The guest kernel still follows the architecture's rules for invalidating stale translations after changing a mapping. Removing shadow synchronization does not remove those rules.
Distinguish the two faults by asking which translation failed. A genuine guest page fault means the guest virtual mapping is absent or violates guest permissions. With EPT enabled and ordinary KVM settings, the processor can deliver the resulting #PF directly to the guest kernel. It causes a VM exit if the hypervisor has configured interception for that exception. A stage-2 fault means the processor could not use the host mapping for a guest-physical access. On Intel, an EPT violation exits to KVM, which may install a RAM mapping, handle a tracked write, or identify an access that requires device emulation. The faulting access can be a read of a guest page-table entry during a walk, as well as the load or store the guest instruction requested. Of these ordinary paging operations, stage-2 faults and explicitly intercepted guest faults are the ones that require hypervisor handling. Other VM exit reasons still exist.
That distinction also applies to measurements. A guest minor fault is a guest OS accounting event. A KVM page-fault trace event records entry to a KVM MMU fault path. An EPT violation is a hardware exit reason. One store may cause work at both levels. For example, the guest may take a minor fault, install a mapping and retry, while KVM needs to establish backing for a guest-physical page used along the way. The three observations answer different questions and cannot be equated.
What a TLB miss costs
4 min readA a processor cache of recent address translations and their permissions, or TLB, can hold a combined guest-virtual to host-physical translation. Modern processors can tag cached translations with context identifiers so that the same virtual address in different guest contexts can resolve correctly. On Intel, a virtual processor identifier (VPID), when enabled, helps retain translations across VM transitions. The EPT pointer distinguishes EPT translation contexts. This is the sense in which caching is two-dimensional: hardware can cache results of the combined translation and reuse intermediate information from the walks. Two independent full TLB lookups are not required for every guest memory access. A combined hit already supplies the usable translation and the cached permissions.
To calculate a cold walk, assume four levels of guest paging, four levels of EPT, and base pages at both levels. Assume that no relevant translation or paging-structure cache entries can be reused. Each of the four guest page-table entries is stored at a guest-physical address. For each entry, the CPU must first read four EPT entries to translate that address, then read the guest entry itself. That gives 4 × (4 + 1) = 20 reads. The last guest entry supplies the guest-physical address of the requested data. Translating that address requires another four EPT reads. The total is 24 page-table reads, followed by the requested data access. This count is for translation reads. Hardware updates to accessed or dirty state can create additional traffic.
A TLB miss is handled in hardware when both sets of entries allow the access. It need not produce a page fault or cause a VM exit. The 24 reads describe an uncached upper-bound walk for the stated layout. In a running guest, paging-structure caches can save steps, and page-table data can already be in ordinary CPU caches. A large mapping can end a walk at a higher level. It also covers a larger range with one cached translation, so a program can reuse a translation over more bytes. The guest and host decide mapping sizes independently. A guest large page alone does not ensure a large combined translation if the corresponding host mappings are split into smaller pages.
To see the difference, measure first writes separately from repeated writes. In an Ubuntu guest, Python's mmap can create a writable private anonymous mapping with mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS and mmap.PROT_READ | mmap.PROT_WRITE. Use mmap.PAGESIZE as the stride and write a byte at each offset. Call madvise(mmap.MADV_NOHUGEPAGE) on the mapping before touching it if you want to limit guest transparent huge pages in this test. Take timestamps with time.perf_counter() and sample resource.getrusage(resource.RUSAGE_SELF).ru_minflt before and after each measured phase. Use the difference between the two samples. The first pass measures the cost of touching fresh pages, including guest fault handling. Repeated passes over the same mapping exercise already established translations. Compare a small resident range with a larger resident range and normalize timing by the number of page touches. Python loop overhead, CPU caches and scheduling also affect that timing. Treat the result as a workload comparison rather than a direct measurement of TLB miss latency.
What KVM still tracks and how to measure it
5 min readStage-2 accessed and dirty bits describe use of a guest-physical mapping. Where hardware supports and enables these bits, the processor can record that a mapping was used and that a writable leaf was written. Intel EPT accessed and dirty support depends on CPU capability and configuration. Accessed information can help the host estimate which guest-backed pages were used recently and which are candidates for reclaim. If the host removes backing or defers establishing a stage-2 mapping, a later access can fault so the host can supply the page and reinstall the mapping. The accessed bit supplies usage information. The missing mapping supplies the demand fault.
Dirty information answers a different question. During live migration, a page copied while the guest is running can change before switchover. Dirty logging lets the migration program identify guest frame numbers that need to be copied again. KVM can use hardware dirty tracking where supported, and it can also track writes by removing write permission and recording the resulting fault on the first write in a tracking interval. That method deliberately adds exits even with nested paging enabled. The migration program reads KVM's dirty log, rather than reading EPT bits itself. A hardware dirty bit describes observed writes through that mapping. KVM and the migration program also have to account for writes to guest RAM made through paths outside that mapping.
Guest and host demand paging can occur independently. The guest kernel can evict an application page to guest swap and later fault it back in through its virtual storage device. Meanwhile, the host can reclaim or swap eligible userspace pages that back guest RAM. A guest mapping may still be present while the host page that backs it needs to be recovered. Conversely, a guest fault can occur even when the host already has the relevant RAM resident. Under pressure at both levels, the guest can spend time paging while the host also spends time reclaiming memory or restoring backing.
Memory overcommit allows the RAM configured across VMs to exceed the host RAM available to back them at once. It depends on how much memory the workloads use at the same time. Ballooning adds cooperation: a guest driver allocates pages inside the guest and tells the host which guest frames it has taken out of normal use, allowing their backing to be reclaimed. As the balloon inflates, the guest has less memory available for other uses and may drop caches or swap if pressured. Deflating the balloon returns pages to guest use, and the host can supply backing again as needed. Neither the configured guest RAM size nor a guest's minor fault count tells you how much host memory the VM currently needs.
Use an Ubuntu 24.04 host on Intel hardware with EPT. An Ubuntu VM can serve as the KVM host if the outer hypervisor exposes nested VMX and EPT and KVM acceleration works. WSL2 is suitable only if the required Intel KVM modules, acceleration and tracing facilities are available. An Arm board does not expose the Intel EPT controls or VMX exit reasons used here.
On a dedicated lab host with kvm_intel loaded as a module, shut down all KVM guests before changing the setting. Run sudo modprobe -r kvm_intel, then sudo modprobe kvm_intel ept=1. Read /sys/module/kvm_intel/parameters/ept to verify the setting and boot the guest with KVM acceleration. For the shadow run, shut the guest down, unload the module again, and load it with ept=0. Keep the guest image, RAM size, vCPU count and benchmark sizes fixed. Capture after boot so that the guest's initialization does not dominate the counters. Use the same host background load and repeat each run.
For a short host trace, sudo perf record -a -e kvm:kvm_page_fault -e kvm:kvm_exit -- sleep 20 records while you run the workload in the guest from another terminal. Read it with sudo perf script. On Intel, look for the EPT_VIOLATION reason in the kvm_exit events and inspect the kvm_page_fault events separately. The exit reason identifies the EPT violation. The MMU tracepoint can occur in both modes and is not a proxy for guest minor faults. With EPT disabled, memory-related exits associated with shadow paging can still occur even though the EPT violation reason disappears. Compare distributions and timings, since there is no required speedup or event count.
For summarized counts, keep EPT enabled and run sudo perf kvm stat record -a -- sleep 20 on the host while the guest executes one workload. Then run sudo perf kvm stat report --event=vmexit before starting the next recording. Compare the EPT_VIOLATION sample count for repeated fresh allocations and first writes against the count for writes to a mapping prepared before recording. Use equal recording windows, keep other guests stopped, and log how many page touches each workload completed. Fresh guest mappings can reuse guest-physical pages whose EPT entries already exist. First touches may therefore raise many guest minor faults with little change in EPT violations. If the report has no EPT violation row, record that no such exits were observed in that capture. The exit count measures how often hardware handed control to KVM for an EPT violation. TLB refill costs can accumulate even when that count is zero.
The next lesson, QEMU's architecture and its acceleration backends, explains how QEMU connects the virtual machine it presents to KVM's hardware acceleration, where the memory behavior measured here takes effect.
Key takeaways
- Nested paging removes the need to synchronize shadow mappings with guest page-table writes.
- A combined TLB hit can serve a guest memory access without either page-table walk.
- A cold four-by-four x86 walk can require 24 page-table reads before the requested data access.
- Guest paging and host backing are independent, and host page tracking uses guest-physical mappings.
Reflect
- Explain to a colleague, in two sentences: explain why ordinary guest page-table writes can complete without a VM exit under nested paging.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationThe x86 KVM shadow MMU
- Kernel documentationThe KVM API: guest memory and dirty logging
- Man pagegetrusage(2): get resource usage
- Man pagemadvise(2): give advice about use of memory
Official documentation first. Links checked when the lesson was published.