virt/kvm/kvm_main.c
Source file repositories/reference/linux-study-clean/virt/kvm/kvm_main.c
File Facts
- System
- Linux kernel
- Corpus path
virt/kvm/kvm_main.c- Extension
.c- Size
- 171203 bytes
- Lines
- 6597
- Domain
- Kernel Services
- Bucket
- virt
- Inferred role
- Kernel Services: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kvm/iodev.hlinux/kvm_host.hlinux/kvm.hlinux/module.hlinux/errno.hlinux/percpu.hlinux/mm.hlinux/miscdevice.hlinux/vmalloc.hlinux/reboot.hlinux/debugfs.hlinux/highmem.hlinux/file.hlinux/syscore_ops.hlinux/cpu.hlinux/sched/signal.hlinux/sched/mm.hlinux/sched/stat.hlinux/cpumask.hlinux/smp.hlinux/anon_inodes.hlinux/profile.hlinux/kvm_para.hlinux/pagemap.hlinux/mman.hlinux/swap.hlinux/bitops.hlinux/spinlock.hlinux/compat.hlinux/srcu.hlinux/slab.hlinux/sort.h
Detected Declarations
struct kvm_mmu_notifier_rangestruct compat_kvm_dirty_logstruct compat_kvm_clear_dirty_logfunction kvm_no_compat_ioctlfunction kvm_no_compat_openfunction kvm_arch_guest_memory_reclaimedfunction vcpu_putfunction kvm_request_needs_ipifunction ack_kickfunction kvm_make_vcpu_requestfunction kvm_request_needs_ipifunction kvm_make_vcpus_request_maskfunction for_each_set_bitfunction kvm_make_all_cpus_requestfunction kvm_flush_remote_tlbsfunction kvm_flush_remote_tlbs_rangefunction kvm_flush_remote_tlbs_memslotfunction kvm_flush_shadow_allfunction __kvm_mmu_topup_memory_cachefunction kvm_mmu_topup_memory_cachefunction kvm_mmu_memory_cache_nr_free_objectsfunction kvm_mmu_free_memory_cachefunction kvm_vcpu_initfunction kvm_vcpu_destroyfunction kvm_destroy_vcpusfunction kvm_for_each_vcpufunction timefunction kvm_handle_hva_rangefunction kvm_for_each_memslot_in_hva_rangefunction kvm_age_hva_rangefunction kvm_age_hva_range_no_flushfunction kvm_mmu_invalidate_startfunction kvm_mmu_invalidate_range_addfunction kvm_mmu_unmap_gfn_rangefunction kvm_mmu_notifier_invalidate_range_startfunction kvm_mmu_invalidate_endfunction kvm_mmu_notifier_invalidate_range_endfunction kvm_mmu_notifier_clear_flush_youngfunction kvm_mmu_notifier_clear_youngfunction kvm_mmu_notifier_test_youngfunction kvm_mmu_notifier_releasefunction kvm_init_mmu_notifierfunction kvm_pm_notifier_callfunction kvm_init_pm_notifierfunction kvm_destroy_pm_notifierfunction kvm_init_pm_notifierfunction kvm_free_memslotfunction kvm_free_memslots
Annotated Snippet
static const struct file_operations stat_fops_per_vm;
static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
unsigned long arg);
#ifdef CONFIG_KVM_COMPAT
static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
unsigned long arg);
#define KVM_COMPAT(c) .compat_ioctl = (c)
#else
/*
* For architectures that don't implement a compat infrastructure,
* adopt a double line of defense:
* - Prevent a compat task from opening /dev/kvm
* - If the open has been done by a 64bit task, and the KVM fd
* passed to a compat task, let the ioctls fail.
*/
static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl,
unsigned long arg) { return -EINVAL; }
static int kvm_no_compat_open(struct inode *inode, struct file *file)
{
return is_compat_task() ? -ENODEV : 0;
}
#define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \
.open = kvm_no_compat_open
#endif
static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
#define KVM_EVENT_CREATE_VM 0
#define KVM_EVENT_DESTROY_VM 1
static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm);
static unsigned long long kvm_createvm_count;
static unsigned long long kvm_active_vms;
static DEFINE_PER_CPU(cpumask_var_t, cpu_kick_mask);
__weak void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)
{
}
/*
* Switches to specified vcpu, until a matching vcpu_put()
*/
void vcpu_load(struct kvm_vcpu *vcpu)
{
int cpu = get_cpu();
__this_cpu_write(kvm_running_vcpu, vcpu);
preempt_notifier_register(&vcpu->preempt_notifier);
kvm_arch_vcpu_load(vcpu, cpu);
put_cpu();
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(vcpu_load);
void vcpu_put(struct kvm_vcpu *vcpu)
{
preempt_disable();
kvm_arch_vcpu_put(vcpu);
preempt_notifier_unregister(&vcpu->preempt_notifier);
__this_cpu_write(kvm_running_vcpu, NULL);
preempt_enable();
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(vcpu_put);
/* TODO: merge with kvm_arch_vcpu_should_kick */
static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)
{
int mode = kvm_vcpu_exiting_guest_mode(vcpu);
/*
* We need to wait for the VCPU to reenable interrupts and get out of
* READING_SHADOW_PAGE_TABLES mode.
*/
if (req & KVM_REQUEST_WAIT)
return mode != OUTSIDE_GUEST_MODE;
/*
* Need to kick a running VCPU, but otherwise there is nothing to do.
*/
return mode == IN_GUEST_MODE;
}
static void ack_kick(void *_completed)
{
}
static inline bool kvm_kick_many_cpus(struct cpumask *cpus, bool wait)
{
if (cpumask_empty(cpus))
Annotation
- Immediate include surface: `kvm/iodev.h`, `linux/kvm_host.h`, `linux/kvm.h`, `linux/module.h`, `linux/errno.h`, `linux/percpu.h`, `linux/mm.h`, `linux/miscdevice.h`.
- Detected declarations: `struct kvm_mmu_notifier_range`, `struct compat_kvm_dirty_log`, `struct compat_kvm_clear_dirty_log`, `function kvm_no_compat_ioctl`, `function kvm_no_compat_open`, `function kvm_arch_guest_memory_reclaimed`, `function vcpu_put`, `function kvm_request_needs_ipi`, `function ack_kick`, `function kvm_make_vcpu_request`.
- Atlas domain: Kernel Services / virt.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.