Skip to content
systemcoding.dev
Virtualization

What /dev/kvm Is and What Happens When QEMU Opens It

Trace what QEMU does with /dev/kvm, from open and KVM_CREATE_VM to the anonymous VM and vCPU file descriptors, using strace and /proc to see each step.

8 min read
Summarize in AI
Flat illustration of guest machines running on a hypervisor over hardware, with the QEMU, Linux and RISC-V logos.

/dev/kvm looks like any device file in ls, and almost nothing about how it behaves is ordinary. Opening it does not give you a stream of bytes. It gives you a handle to the KVM subsystem, and a few ioctl calls on it turn into file descriptors for whole virtual machines and processors that do not exist anywhere in the file system. This post shows what the file is, what the kernel does on each call QEMU makes, and how to watch it happen with strace and /proc. It uses QEMU 9.2 and Linux 6.12 on x86-64, needs no source reading and works as a first look before the complete guide to the Linux KVM API.

What the file is

/dev/kvm is a character device, major number 10 (the miscellaneous device class) and minor number 232:

Shell
ls -l /dev/kvm
Text
crw-rw---- 1 root kvm 10, 232 Nov 23 09:14 /dev/kvm

The leading c marks a character device. Major 10 is where the kernel's misc subsystem registers small singleton drivers, and minor 232 is KVM_MINOR, defined in the kernel headers. The device node exists when the kvm module is loaded, which creates it through misc_register() in virt/kvm/kvm_main.c during initialization. The vendor modules, kvm_intel or kvm_amd, register themselves with the generic one at their own load time. If the node is missing, either the modules are not loaded or the CPU feature is off.

The device does not implement read or write. Its file operations are open, release, and unlocked_ioctl, so the whole interface is ioctl. The llseek and compat_ioctl entries are there for completeness. That explains why cat /dev/kvm fails immediately: there is nothing to read.

Three descriptors from one device

The key idea is that the three kinds of KVM object are all file descriptors, and only the first comes from a path.

  1. open("/dev/kvm") returns the system descriptor. It represents the KVM subsystem as a whole.

  2. ioctl(system_fd, KVM_CREATE_VM) returns a VM descriptor. The kernel allocates a struct kvm, with its memory slots, interrupt state and architecture data, and makes an anonymous inode for it.

  3. ioctl(vm_fd, KVM_CREATE_VCPU, n) returns a vCPU descriptor, with another anonymous inode, wrapping a struct kvm_vcpu.

An anonymous inode is a file descriptor with no name in any file system. The kernel creates one with anon_inode_getfd() and gives it a label and a set of file operations. For KVM the labels are kvm-vm and kvm-vcpu:N. This is why the VM and vCPU do not appear under /dev, and why closing the VM descriptor, the last reference to it, destroys the virtual machine.

Watch it with strace

strace decodes KVM ioctl names, so you can see the sequence QEMU performs. Run a tiny guest under it, with -f to follow the threads QEMU creates:

Shell
strace -f -e trace=openat,ioctl,mmap -o qemu.trace \
  qemu-system-x86_64 -accel kvm -m 64M -display none -S -monitor none &
sleep 2; kill %1
grep -E 'kvm|KVM' qemu.trace | head -n 12
Text
1203  openat(AT_FDCWD, "/dev/kvm", O_RDWR|O_CLOEXEC) = 8
1203  ioctl(8, KVM_GET_API_VERSION, 0) = 12
1203  ioctl(8, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS) = 1
1203  ioctl(8, KVM_CHECK_EXTENSION, KVM_CAP_MAX_VCPUS) = 1024
1203  ioctl(8, KVM_CREATE_VM, 0) = 9
1203  ioctl(9, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY) = 1
1203  ioctl(9, KVM_SET_TSS_ADDR, 0xfeffd000) = 0
1203  ioctl(9, KVM_SET_USER_MEMORY_REGION, {slot=0, flags=0, guest_phys_addr=0, memory_size=65536, userspace_addr=0x7f2a1c000000}) = 0
1203  ioctl(9, KVM_CREATE_IRQCHIP, 0) = 0
1203  ioctl(9, KVM_CREATE_VCPU, 0) = 10
1203  ioctl(8, KVM_GET_VCPU_MMAP_SIZE, 0) = 12288
1203  mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_SHARED, 10, 0) = 0x7f2a1c800000

File descriptor numbers, thread IDs, addresses and the exact order of the calls differ between runs and QEMU versions, and a real trace contains many more lines. The pattern is what matters. The first four lines are QEMU asking the system descriptor (8) about itself: is the API version right, how many vCPUs are allowed. KVM_CREATE_VM returns 9, and from then on the ioctls go to descriptor 9: KVM_SET_USER_MEMORY_REGION registers the guest's RAM, here 64 KiB at guest physical address 0 (the full command line would register more regions), and KVM_CREATE_VCPU returns 10. The last two lines map the kvm_run structure from the vCPU descriptor, sized by what the kernel reported.

Each line is one of the steps in the guide. QEMU does the same sequence a hand-written VMM does, plus a great deal of device setup.

Look at the descriptors in /proc

While QEMU runs, /proc shows the anonymous files. List the descriptors of the running process:

Shell
ls -l /proc/$(pgrep -n qemu-system-x86)/fd | grep -i kvm
Text
lrwx------ 1 dev dev 64 Nov 23 10:02 8 -> /dev/kvm
lrwx------ 1 dev dev 64 Nov 23 10:02 9 -> anon_inode:kvm-vm
lrwx------ 1 dev dev 64 Nov 23 10:02 10 -> anon_inode:kvm-vcpu:0

There are the three objects: the device, the VM and the first vCPU. With -smp 4 you would see kvm-vcpu:0 through kvm-vcpu:3. The numbers and times will differ on your machine. Another place to see the structure is the host thread list:

Shell
ps -L -p $(pgrep -n qemu-system-x86) -o tid,comm
Text
    TID COMMAND
   1203 qemu-system-x86
   1207 qemu-system-x86
   1208 CPU 0/KVM

Each virtual CPU is a thread in the QEMU process, named CPU n/KVM, and it spends nearly all its life blocked inside the KVM_RUN ioctl. The kernel schedules that thread like any other, which is a large part of why KVM is simple: no separate scheduler exists for guests.

What the kernel does on each call

The behavior behind the ioctls is worth knowing in outline, because it explains the costs and the limits.

**open.** The kernel increments a reference on the KVM module so it cannot be unloaded while open, and creates a file with no per-open state. Opening is cheap and can be done many times; each open is an independent handle to the subsystem.

**KVM_GET_API_VERSION.** Returns the constant 12, and nothing changes.

**KVM_CREATE_VM.** Calls kvm_create_vm(). The kernel allocates struct kvm, initializes its locks, memory slot arrays and the architecture-specific part (kvm_arch_init_vm), which on x86 sets up the MMU state and the hypervisor hooks. The VM is registered in a global list used by debugfs and statistics, and a descriptor is returned. The new VM is attached to the calling process's address space: guest memory slots refer to addresses in that mm, and the kernel tracks it through MMU notifiers so the host memory manager can tell KVM when it moves or reclaims a page.

**KVM_SET_USER_MEMORY_REGION.** Records a memory slot, a mapping from a range of guest physical addresses to a range of host virtual addresses. It does not touch the pages. They stay unallocated and unmapped in the hardware's second-level page tables until the guest first touches them, at which point an EPT violation exit makes KVM look up the host page, fault it in if needed and install the mapping. This lazy behavior is why a guest started with 16 GiB of RAM does not use 16 GiB of host memory until the guest does.

**KVM_CREATE_VCPU.** Allocates struct kvm_vcpu and, through the vendor module, the hardware control structure, the VMCS on Intel or the VMCB on AMD. It also allocates the kvm_run page that the process later maps. On x86 it initializes the vCPU in the architectural reset state.

**KVM_RUN.** The heart: the kernel loads the vCPU's state into the processor, executes the VM entry instruction and the guest runs. When an event the kernel cannot handle itself occurs, it stores the reason in kvm_run and returns to user space. The thread has been in the kernel the whole time, so it shows as S or R in ps, never in user code.

**close.** When the VM descriptor and every descriptor derived from it close, kvm_destroy_vm() frees the memory slots and architecture state, releases the vCPUs and drops the reference to the process's address space.

Permission and security model

Opening /dev/kvm is the only permission check, controlled by the device's mode and group. Once a process holds a system descriptor, it can create as many VMs as the kernel limits allow, constrained by RLIMIT settings and memory cgroups. There is no per-VM permission. The guest's memory is the process's memory and is accounted to its cgroup, so a memory.max limit on the QEMU process also caps the guest. For that reason the standard way to confine a VM is to confine the process: a cgroup for resources, namespaces and seccomp for isolation, and a dedicated user for file access. Libvirt does exactly that for each guest.

KVM does not have a CAP_ capability of its own. Access to the device is the capability.

What changes with other architectures

The device and its three-level structure are the same on Arm, RISC-V, s390 and PowerPC, because virt/kvm/ is shared code. The architecture directory supplies kvm_arch_* functions. On Arm64, KVM is only available if the kernel booted in EL2, and /dev/kvm appears with the module present. The KVM_CREATE_VM argument can carry the intermediate physical address size. The strace output has the same shape, with architecture-specific calls such as KVM_ARM_PREFERRED_TARGET and KVM_ARM_VCPU_INIT in place of the x86 TSS and irqchip setup.

Why this matters in practice

Knowing these facts makes several common situations obvious. A VM that "leaks" after a crashed VMM does not exist, because the kernel closes the descriptors when the process dies and destroys the VM. A guest's memory shows up as the VMM's resident memory, so top and free report it plainly, and ulimit -v limits it. Debugging tools that attach to the VMM, like strace, gdb and perf, work as for any process, and can show every ioctl. And the quickest check for "is KVM really in use" is not a flag in a log but the presence of anon_inode:kvm-vm in the process's descriptors.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.