How KVM Integrates With the Linux Kernel
See how KVM reuses the Linux scheduler, memory manager, MMU notifiers, eventfds and cgroups instead of building its own, and where to read each in the source.

KVM is small compared to other hypervisors, and the reason is a design decision made at the start: do not build an operating system inside the hypervisor, borrow the one that is already running. The Linux scheduler schedules virtual CPUs. The Linux memory manager supplies guest RAM and reclaims it. Linux's file descriptors, eventfds, cgroups, signals and tracing all apply to virtual machines with no special code. This post walks through those seams, one subsystem at a time, naming the kernel mechanism KVM hooks into, what it gains and where to look in the Linux 6.12 source. It is aimed at readers who know the KVM API from the complete guide to the Linux KVM API and want to understand why the pieces behave as they do, and it assumes comfort reading C and the kernel tree layout.
The code layout
KVM lives in virt/kvm/ for the architecture-independent core and in arch/<arch>/kvm/ for each architecture. On x86 the directory has three layers:
| Path | Role |
|---|---|
virt/kvm/kvm_main.c | The /dev/kvm device, VM and vCPU creation, ioctl dispatch, memory slots |
arch/x86/kvm/x86.c | Architecture-generic x86 logic: CPUID, MSRs, exit handling, run loop |
arch/x86/kvm/vmx/ | Intel VMX backend: VMCS, VM entry and exit |
arch/x86/kvm/svm/ | AMD SVM backend: VMCB, VMRUN |
arch/x86/kvm/mmu/ | Second-level page table (EPT, NPT) management |
arch/x86/kvm/lapic.c, i8254.c, ioapic.c | In-kernel interrupt controllers and timer |
At build time kvm.ko holds the core and the x86 common code, and kvm-intel.ko or kvm-amd.ko holds the vendor backend. The vendor module registers a table of function pointers, struct kvm_x86_ops, with the common code, which then calls through it for operations such as "enter the guest" and "read the exit reason". That table is how one common run loop serves both vendors.
The scheduler: vCPUs are threads
A vCPU runs because a host thread called KVM_RUN. The kernel's scheduler decides when that thread gets a CPU, exactly as for any thread. Nothing in KVM implements priorities, time slices or load balancing. The consequences are the ones you would expect: nice, chrt, taskset, cgroup CPU limits and cpusets apply to vCPU threads, and a host under load makes guests slow in the same way it slows any process.
KVM does need to know when its thread is scheduled in or out, because guest state loaded into the processor must be saved when another thread takes over. It uses preempt notifiers, a scheduler hook defined in include/linux/preempt.h. KVM registers a preempt_ops structure for each vCPU with sched_in and sched_out callbacks, and the scheduler calls them on context switches involving that thread. On sched_out, KVM can put the vCPU's hardware state aside, and on sched_in, kvm_arch_vcpu_load loads it again, possibly on a different physical CPU. That is why migrating a vCPU between host cores is routine.
ps -L -p "$(pgrep -n qemu-system-x86)" -o tid,psr,pcpu,comm TID PSR %CPU COMMAND
2201 3 0.4 qemu-system-x86
2205 1 0.0 qemu-system-x86
2206 5 7.8 CPU 0/KVM
2207 2 7.5 CPU 1/KVMThe PSR column is the processor each thread last ran on, so the vCPU threads spread across cores without any hypervisor decision. The numbers vary. A guest that halts, executing HLT, blocks its vCPU thread, and the scheduler is free to run something else on that core. Before blocking, KVM can poll: the module parameter halt_poll_ns lets a halted vCPU spin briefly, because many guests wake within microseconds, and a poll saves a full sleep and wake-up. Read the current setting:
cat /sys/module/kvm/parameters/halt_poll_ns200000The default is 200000 nanoseconds on current kernels. The polling window adjusts itself between a minimum and this maximum, and a guest that rarely wakes quickly stops polling. This tuning knob belongs to the trade-off between wake-up latency and wasted CPU.
Memory: guest RAM is process memory
The central trick, introduced in the KVM API guide, is that guest RAM is anonymous memory in the VMM's address space. The second-level page tables (EPT or NPT) point at the same physical pages the process's own page tables do. This brings the whole Linux memory manager along:
Lazy allocation. A page does not exist until touched. A guest access to an unmapped page causes an EPT violation, and KVM's fault handler calls
hva_to_pfn()which usesget_user_pages-style machinery to fault the host page in, then maps it into the EPT.Swap. The kernel can swap out guest pages as it would any process pages. The next access faults them back in.
Transparent huge pages. If the host backs the region with a 2 MiB page, KVM can map it in the EPT as a huge page, shortening the nested page walk.
Kernel samepage merging (KSM). If the VMM marks its memory with
madvise(MADV_MERGEABLE), identical pages across guests share one copy.NUMA policy and cgroup limits.
mbind,numactland memory cgroups apply, and memory is charged to the VMM's cgroup.
MMU notifiers: keeping the EPT honest
There is a synchronization problem. When the host kernel swaps out a page, migrates it, merges it or frees it because the process unmapped the region, any mapping the EPT holds becomes stale. A guest that kept using the old physical page would read or corrupt memory that belongs to someone else. KVM solves this with MMU notifiers, a generic kernel mechanism (include/linux/mmu_notifier.h) that lets a subsystem register to be told about changes to a process's mappings. KVM registers one per VM. When the host changes the mapping of an address range backing guest RAM, the notifier calls into KVM (kvm_mmu_notifier_invalidate_range_start and its relatives in virt/kvm/kvm_main.c), which removes the affected entries from the EPT and flushes TLBs on the vCPUs that might have cached them. Only then may the host proceed. The same mechanism supports page migration for NUMA balancing and memory compaction.
You can see the effect by pressure-testing: allocate memory in the guest, force the host to swap and watch guest performance degrade while remaining correct. Correctness is the property the notifiers provide.
File descriptors, eventfds and the VFS
KVM objects are files, so ordinary descriptor semantics apply. fork duplicates the descriptors but not the guest, so a forked child holds references and the VM lives until both close. close-on-exec matters, which is why programs open /dev/kvm with O_CLOEXEC. Passing a VM descriptor over a Unix socket with SCM_RIGHTS is possible and used by some privilege-separated designs.
Two ioctls turn kernel events into descriptors with no user-space round trip, and both use the kernel's eventfd:
irqfd (
KVM_IRQFD) binds an eventfd to a guest interrupt. Writing to the eventfd, from user space or from another kernel component such asvhost, injects the interrupt. KVM waits on the eventfd's wait queue and its callback raises the interrupt.ioeventfd (
KVM_IOEVENTFD) binds a guest I/O or MMIO address to an eventfd. When the guest writes to the address, the kernel signals the eventfd inside the exit handler and resumes the guest at once, without returning to user space.
Together they let a virtio device backend, running in another thread or in the kernel as vhost, communicate with the guest at the cost of a lightweight kernel exit and an eventfd write. This is why vhost-net and vhost-vsock exist as kernel modules: they consume the guest's virtqueues directly, signalling through these two mechanisms.
Timers and time
A guest needs timers. KVM implements the in-kernel local APIC timer and the PIT with hrtimers, the kernel's high-resolution timers, so a guest's timer interrupts fire at the right host time and wake a halted vCPU. For the guest's clock, KVM provides kvm-clock, a paravirtual structure the guest reads, backed by the host's clocksource, and the KVM_GET_CLOCK and KVM_SET_CLOCK ioctls for save and restore. When a host's TSC is not suitably stable, KVM uses an elaborate scheme to keep the guest's TSC consistent across hosts, and that code in arch/x86/kvm/x86.c is among the most intricate in the module.
RCU and locking
KVM is a heavily concurrent subsystem: vCPU threads, the MMU notifier callbacks and ioctls from user space all run at once. It relies on kernel primitives rather than its own. Memory slots are protected by SRCU (sleepable RCU), so a vCPU in the middle of a fault walks a consistent set of slots while another thread updates them, and the updater waits for readers to finish before freeing the old array. The MMU uses a spinlock or, on recent kernels with the TDP MMU, a read-write lock plus atomic page table updates so many vCPUs can handle faults in parallel. The lock ordering rules are documented in Documentation/virt/kvm/locking.rst, and breaking them is a classic source of deadlocks in KVM patches.
Signals, cgroups and namespaces
Because a VM belongs to a process, kill signals interrupt KVM_RUN like they interrupt any blocking ioctl, which is the mechanism a VMM uses to stop a vCPU. Cgroups apply to the whole process, including guest memory charged to its memory cgroup and vCPU time charged to its CPU cgroup, so resource limits for a guest are cgroup limits for its VMM. Namespaces and seccomp do not affect the guest at all, only what the VMM can reach, which is why a VMM can be tightly sandboxed while the guest still runs at full speed.
Observability for free
Since KVM uses kernel facilities, kernel tools see it. The tracepoints under events/kvm/ work with ftrace, trace-cmd and perf. perf kvm stat reports exits. The kvm module's statistics appear in debugfs and through the stats file descriptor. Standard tools such as top -H, pidstat and pmap show vCPU threads and guest memory. The next post in this series, on tracepoints and perf kvm, uses these directly.
What KVM adds that Linux does not already have
The list is short, and the shortness is the point. KVM adds the hardware virtualization layer: the code in vmx/ and svm/ that manages VMCS and VMCB state and performs VM entry and exit. It adds the second-level MMU, which builds and maintains EPT and NPT tables from the host's mappings. It adds instruction emulation for the few cases hardware cannot complete. And it adds in-kernel interrupt controllers and timers for performance. Everything else, scheduling, memory management, I/O infrastructure, security and accounting, is Linux.
Reading the source
A plan for reading KVM for the first time: begin with virt/kvm/kvm_main.c and trace kvm_dev_ioctl() to kvm_vm_ioctl() to kvm_vcpu_ioctl() and the KVM_RUN case. Follow kvm_arch_vcpu_ioctl_run() in arch/x86/kvm/x86.c into vcpu_enter_guest() and then into the vendor vmx_vcpu_run(). Read handle_exit in vmx/vmx.c to see the exit dispatch table. Only then open mmu/mmu.c, which is large, starting with kvm_mmu_page_fault(). The documentation in Documentation/virt/kvm/ explains the design of each part and is more reliable than comments.


