virt/kvm/guest_memfd.c
Source file repositories/reference/linux-study-clean/virt/kvm/guest_memfd.c
File Facts
- System
- Linux kernel
- Corpus path
virt/kvm/guest_memfd.c- Extension
.c- Size
- 25886 bytes
- Lines
- 1033
- 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.
- 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
linux/anon_inodes.hlinux/backing-dev.hlinux/falloc.hlinux/fs.hlinux/kvm_host.hlinux/mempolicy.hlinux/pseudo_fs.hlinux/pagemap.hkvm_mm.h
Detected Declarations
struct gmem_filestruct gmem_inodefunction folio_file_pfnfunction kvm_gmem_get_indexfunction __kvm_gmem_prepare_foliofunction kvm_gmem_prepare_foliofunction kvm_gmem_get_invalidate_filterfunction __kvm_gmem_invalidate_startfunction xa_for_each_rangefunction kvm_gmem_invalidate_startfunction __kvm_gmem_invalidate_endfunction kvm_gmem_invalidate_endfunction kvm_gmem_punch_holefunction kvm_gmem_allocatefunction kvm_gmem_fallocatefunction kvm_gmem_releasefunction kvm_gmem_supports_mmapfunction kvm_gmem_fault_user_mappingfunction kvm_gmem_set_policyfunction kvm_gmem_mmapfunction kvm_gmem_migrate_foliofunction kvm_gmem_error_foliofunction kvm_gmem_free_foliofunction kvm_gmem_setattrfunction kvm_arch_supports_gmem_init_sharedfunction __kvm_gmem_createfunction kvm_gmem_createfunction kvm_gmem_bindfunction __kvm_gmem_unbindfunction kvm_gmem_unbindfunction kvm_gmem_releasefunction kvm_gmem_get_pfnfunction __kvm_gmem_populatefunction kvm_gmem_populatefunction kvm_gmem_init_inode_oncefunction kvm_gmem_destroy_inodefunction kvm_gmem_free_inodefunction kvm_gmem_init_fs_contextfunction kvm_gmem_init_mountfunction kvm_gmem_initfunction kvm_gmem_exit
Annotated Snippet
static struct file_operations kvm_gmem_fops = {
.mmap = kvm_gmem_mmap,
.open = generic_file_open,
.release = kvm_gmem_release,
.fallocate = kvm_gmem_fallocate,
};
static int kvm_gmem_migrate_folio(struct address_space *mapping,
struct folio *dst, struct folio *src,
enum migrate_mode mode)
{
WARN_ON_ONCE(1);
return -EINVAL;
}
static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folio)
{
pgoff_t start, end;
filemap_invalidate_lock_shared(mapping);
start = folio->index;
end = start + folio_nr_pages(folio);
kvm_gmem_invalidate_start(mapping->host, start, end);
/*
* Do not truncate the range, what action is taken in response to the
* error is userspace's decision (assuming the architecture supports
* gracefully handling memory errors). If/when the guest attempts to
* access a poisoned page, kvm_gmem_get_pfn() will return -EHWPOISON,
* at which point KVM can either terminate the VM or propagate the
* error to userspace.
*/
kvm_gmem_invalidate_end(mapping->host, start, end);
filemap_invalidate_unlock_shared(mapping);
return MF_DELAYED;
}
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
static void kvm_gmem_free_folio(struct folio *folio)
{
struct page *page = folio_page(folio, 0);
kvm_pfn_t pfn = page_to_pfn(page);
int order = folio_order(folio);
kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order));
}
#endif
static const struct address_space_operations kvm_gmem_aops = {
.dirty_folio = noop_dirty_folio,
.migrate_folio = kvm_gmem_migrate_folio,
.error_remove_folio = kvm_gmem_error_folio,
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
.free_folio = kvm_gmem_free_folio,
#endif
};
static int kvm_gmem_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
struct iattr *attr)
{
return -EINVAL;
}
static const struct inode_operations kvm_gmem_iops = {
.setattr = kvm_gmem_setattr,
};
bool __weak kvm_arch_supports_gmem_init_shared(struct kvm *kvm)
{
return true;
}
static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
{
static const char *name = "[kvm-gmem]";
struct gmem_file *f;
struct inode *inode;
struct file *file;
int fd, err;
fd = get_unused_fd_flags(0);
if (fd < 0)
return fd;
f = kzalloc_obj(*f);
if (!f) {
Annotation
- Immediate include surface: `linux/anon_inodes.h`, `linux/backing-dev.h`, `linux/falloc.h`, `linux/fs.h`, `linux/kvm_host.h`, `linux/mempolicy.h`, `linux/pseudo_fs.h`, `linux/pagemap.h`.
- Detected declarations: `struct gmem_file`, `struct gmem_inode`, `function folio_file_pfn`, `function kvm_gmem_get_index`, `function __kvm_gmem_prepare_folio`, `function kvm_gmem_prepare_folio`, `function kvm_gmem_get_invalidate_filter`, `function __kvm_gmem_invalidate_start`, `function xa_for_each_range`, `function kvm_gmem_invalidate_start`.
- 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.
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.