lib/test_hmm.c
Source file repositories/reference/linux-study-clean/lib/test_hmm.c
File Facts
- System
- Linux kernel
- Corpus path
lib/test_hmm.c- Extension
.c- Size
- 46546 bytes
- Lines
- 1874
- Domain
- Kernel Services
- Bucket
- lib
- 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/init.hlinux/fs.hlinux/mm.hlinux/module.hlinux/kernel.hlinux/cdev.hlinux/device.hlinux/memremap.hlinux/mutex.hlinux/rwsem.hlinux/sched.hlinux/slab.hlinux/highmem.hlinux/delay.hlinux/pagemap.hlinux/hmm.hlinux/vmalloc.hlinux/swap.hlinux/swapops.hlinux/sched/mm.hlinux/platform_device.hlinux/rmap.hlinux/mmu_notifier.hlinux/migrate.htest_hmm_uapi.h
Detected Declarations
struct dmirror_devicestruct dmirror_bouncestruct dmirror_intervalstruct dmirrorstruct dmirror_chunkstruct dmirror_devicefunction dmirror_bounce_initfunction dmirror_is_private_zonefunction dmirror_select_devicefunction dmirror_bounce_finifunction dmirror_fops_openfunction dmirror_device_evict_chunkfunction dmirror_fops_releasefunction dmirror_do_faultfunction dmirror_do_updatefunction dmirror_interval_invalidatefunction dmirror_range_faultfunction dmirror_faultfunction dmirror_do_readfunction dmirror_readfunction dmirror_do_writefunction dmirror_writefunction dmirror_allocate_chunkfunction dmirror_migrate_alloc_and_copyfunction dmirror_check_atomicfunction dmirror_atomic_mapfunction dmirror_migrate_finalize_and_mapfunction dmirror_exclusivefunction dmirror_devmem_fault_alloc_and_copyfunction dmirror_successful_migrated_pagesfunction dmirror_migrate_to_systemfunction dmirror_migrate_to_devicefunction dmirror_mkentryfunction dmirror_snapshot_invalidatefunction dmirror_range_snapshotfunction dmirror_snapshotfunction dmirror_remove_free_pagesfunction dmirror_device_remove_chunksfunction dmirror_fops_unlocked_ioctlfunction dmirror_fops_mmapfunction dmirror_devmem_freefunction dmirror_devmem_faultfunction migrate_vma_setupfunction dmirror_devmem_folio_splitfunction dmirror_device_releasefunction dmirror_device_initfunction dmirror_device_removefunction hmm_dmirror_init
Annotated Snippet
static const struct file_operations dmirror_fops = {
.open = dmirror_fops_open,
.release = dmirror_fops_release,
.mmap = dmirror_fops_mmap,
.unlocked_ioctl = dmirror_fops_unlocked_ioctl,
.llseek = default_llseek,
.owner = THIS_MODULE,
};
static void dmirror_devmem_free(struct folio *folio)
{
struct page *page = &folio->page;
struct page *rpage = BACKING_PAGE(page);
struct dmirror_device *mdevice;
struct folio *rfolio = page_folio(rpage);
unsigned int order = folio_order(rfolio);
if (rpage != page) {
if (order)
__free_pages(rpage, order);
else
__free_page(rpage);
rpage = NULL;
}
mdevice = dmirror_page_to_device(page);
spin_lock(&mdevice->lock);
/* Return page to our allocator if not freeing the chunk */
if (!dmirror_page_to_chunk(page)->remove) {
mdevice->cfree += 1 << order;
if (order) {
page->zone_device_data = mdevice->free_folios;
mdevice->free_folios = page_folio(page);
} else {
page->zone_device_data = mdevice->free_pages;
mdevice->free_pages = page;
}
}
spin_unlock(&mdevice->lock);
}
static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)
{
struct migrate_vma args = { 0 };
struct page *rpage;
struct dmirror *dmirror;
vm_fault_t ret = 0;
unsigned int order, nr;
/*
* Normally, a device would use the page->zone_device_data to point to
* the mirror but here we use it to hold the page for the simulated
* device memory and that page holds the pointer to the mirror.
*/
rpage = folio_zone_device_data(page_folio(vmf->page));
dmirror = rpage->zone_device_data;
/* FIXME demonstrate how we can adjust migrate range */
order = folio_order(page_folio(vmf->page));
nr = 1 << order;
/*
* When folios are partially mapped, we can't rely on the folio
* order of vmf->page as the folio might not be fully split yet
*/
if (vmf->pte) {
order = 0;
nr = 1;
}
/*
* Consider a per-cpu cache of src and dst pfns, but with
* large number of cpus that might not scale well.
*/
args.start = ALIGN_DOWN(vmf->address, (PAGE_SIZE << order));
args.vma = vmf->vma;
args.end = args.start + (PAGE_SIZE << order);
nr = (args.end - args.start) >> PAGE_SHIFT;
args.src = kcalloc(nr, sizeof(unsigned long), GFP_KERNEL);
args.dst = kcalloc(nr, sizeof(unsigned long), GFP_KERNEL);
args.pgmap_owner = dmirror->mdevice;
args.flags = dmirror_select_device(dmirror);
args.fault_page = vmf->page;
if (!args.src || !args.dst) {
ret = VM_FAULT_OOM;
goto err;
}
Annotation
- Immediate include surface: `linux/init.h`, `linux/fs.h`, `linux/mm.h`, `linux/module.h`, `linux/kernel.h`, `linux/cdev.h`, `linux/device.h`, `linux/memremap.h`.
- Detected declarations: `struct dmirror_device`, `struct dmirror_bounce`, `struct dmirror_interval`, `struct dmirror`, `struct dmirror_chunk`, `struct dmirror_device`, `function dmirror_bounce_init`, `function dmirror_is_private_zone`, `function dmirror_select_device`, `function dmirror_bounce_fini`.
- Atlas domain: Kernel Services / lib.
- 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.