mm/hmm.c
Source file repositories/reference/linux-study-clean/mm/hmm.c
File Facts
- System
- Linux kernel
- Corpus path
mm/hmm.c- Extension
.c- Size
- 26140 bytes
- Lines
- 896
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/pagewalk.hlinux/hmm.hlinux/hmm-dma.hlinux/init.hlinux/rmap.hlinux/swap.hlinux/slab.hlinux/sched.hlinux/mmzone.hlinux/pagemap.hlinux/leafops.hlinux/hugetlb.hlinux/memremap.hlinux/sched/mm.hlinux/jump_label.hlinux/dma-mapping.hlinux/pci-p2pdma.hlinux/mmu_notifier.hlinux/memory_hotplug.hinternal.h
Detected Declarations
struct hmm_vma_walkfunction hmm_pfns_fillfunction hmm_vma_faultfunction hmm_pte_need_faultfunction hmm_range_need_faultfunction hmm_vma_walk_holefunction hmm_pfn_flags_orderfunction pmd_to_hmm_pfn_flagsfunction hmm_vma_handle_pmdfunction pte_to_hmm_pfn_flagsfunction hmm_vma_handle_ptefunction hmm_vma_handle_absent_pmdfunction hmm_vma_handle_absent_pmdfunction hmm_vma_walk_pmdfunction pud_to_hmm_pfn_flagsfunction hmm_vma_walk_pudfunction hmm_vma_walk_hugetlb_entryfunction hmm_vma_walk_testfunction get_user_pagesfunction hmm_dma_map_allocfunction hmm_dma_map_freefunction dma_alloc_iovafunction hmm_dma_unmap_pfnexport hmm_range_faultexport hmm_dma_map_allocexport hmm_dma_map_freeexport hmm_dma_map_pfnexport hmm_dma_unmap_pfn
Annotated Snippet
if (handle_mm_fault(vma, addr, fault_flags, NULL) &
VM_FAULT_ERROR)
return -EFAULT;
return -EBUSY;
}
static unsigned int hmm_pte_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
unsigned long pfn_req_flags,
unsigned long cpu_flags)
{
struct hmm_range *range = hmm_vma_walk->range;
/*
* So we not only consider the individual per page request we also
* consider the default flags requested for the range. The API can
* be used 2 ways. The first one where the HMM user coalesces
* multiple page faults into one request and sets flags per pfn for
* those faults. The second one where the HMM user wants to pre-
* fault a range with specific flags. For the latter one it is a
* waste to have the user pre-fill the pfn arrays with a default
* flags value.
*/
pfn_req_flags &= range->pfn_flags_mask;
pfn_req_flags |= range->default_flags;
/* We aren't ask to do anything ... */
if (!(pfn_req_flags & HMM_PFN_REQ_FAULT))
return 0;
/* Need to write fault ? */
if ((pfn_req_flags & HMM_PFN_REQ_WRITE) &&
!(cpu_flags & HMM_PFN_WRITE))
return HMM_NEED_FAULT | HMM_NEED_WRITE_FAULT;
/* If CPU page table is not valid then we need to fault */
if (!(cpu_flags & HMM_PFN_VALID))
return HMM_NEED_FAULT;
return 0;
}
static unsigned int
hmm_range_need_fault(const struct hmm_vma_walk *hmm_vma_walk,
const unsigned long hmm_pfns[], unsigned long npages,
unsigned long cpu_flags)
{
struct hmm_range *range = hmm_vma_walk->range;
unsigned int required_fault = 0;
unsigned long i;
/*
* If the default flags do not request to fault pages, and the mask does
* not allow for individual pages to be faulted, then
* hmm_pte_need_fault() will always return 0.
*/
if (!((range->default_flags | range->pfn_flags_mask) &
HMM_PFN_REQ_FAULT))
return 0;
for (i = 0; i < npages; ++i) {
required_fault |= hmm_pte_need_fault(hmm_vma_walk, hmm_pfns[i],
cpu_flags);
if (required_fault == HMM_NEED_ALL_BITS)
return required_fault;
}
return required_fault;
}
static int hmm_vma_walk_hole(unsigned long addr, unsigned long end,
__always_unused int depth, struct mm_walk *walk)
{
struct hmm_vma_walk *hmm_vma_walk = walk->private;
struct hmm_range *range = hmm_vma_walk->range;
unsigned int required_fault;
unsigned long i, npages;
unsigned long *hmm_pfns;
i = (addr - range->start) >> PAGE_SHIFT;
npages = (end - addr) >> PAGE_SHIFT;
hmm_pfns = &range->hmm_pfns[i];
required_fault =
hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0);
if (!walk->vma) {
if (required_fault)
return -EFAULT;
return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR);
}
if (required_fault)
return hmm_vma_fault(addr, end, required_fault, walk);
return hmm_pfns_fill(addr, end, range, 0);
}
Annotation
- Immediate include surface: `linux/pagewalk.h`, `linux/hmm.h`, `linux/hmm-dma.h`, `linux/init.h`, `linux/rmap.h`, `linux/swap.h`, `linux/slab.h`, `linux/sched.h`.
- Detected declarations: `struct hmm_vma_walk`, `function hmm_pfns_fill`, `function hmm_vma_fault`, `function hmm_pte_need_fault`, `function hmm_range_need_fault`, `function hmm_vma_walk_hole`, `function hmm_pfn_flags_order`, `function pmd_to_hmm_pfn_flags`, `function hmm_vma_handle_pmd`, `function pte_to_hmm_pfn_flags`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.