mm/rmap.c
Source file repositories/reference/linux-study-clean/mm/rmap.c
File Facts
- System
- Linux kernel
- Corpus path
mm/rmap.c- Extension
.c- Size
- 94438 bytes
- Lines
- 3156
- 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.
- 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/mm.hlinux/sched/mm.hlinux/sched/task.hlinux/pagemap.hlinux/swap.hlinux/leafops.hlinux/slab.hlinux/init.hlinux/ksm.hlinux/rmap.hlinux/rcupdate.hlinux/export.hlinux/memcontrol.hlinux/mmu_notifier.hlinux/migrate.hlinux/hugetlb.hlinux/huge_mm.hlinux/backing-dev.hlinux/page_idle.hlinux/memremap.hlinux/userfaultfd_k.hlinux/mm_inline.hlinux/oom.hasm/tlb.htrace/events/migrate.hinternal.hswap.h
Detected Declarations
struct folio_referenced_argstruct wrprotect_file_statefunction anon_vma_freefunction anon_vma_chain_freefunction anon_vma_chain_assignfunction anon_vma_preparefunction check_anon_vma_clonefunction maybe_reuse_anon_vmafunction anon_vma_clonefunction anon_vma_forkfunction anon_vma_clonefunction list_for_each_entry_safefunction unlink_anon_vmasfunction anon_vma_ctorfunction anon_vma_initfunction freedfunction reusedfunction folio_get_anon_vmafunction try_to_unmap_flushfunction try_to_unmap_flush_dirtyfunction set_tlb_ubc_flush_pendingfunction should_defer_flushfunction flush_tlb_batched_pendingfunction set_tlb_ubc_flush_pendingfunction lockfunction folio_referenced_onefunction invalid_folio_referenced_vmafunction folio_referencedfunction page_vma_mkclean_onefunction page_mkclean_onefunction invalid_mkclean_vmafunction folio_mkcleanfunction mapping_wrprotect_range_onefunction mapping_wrprotect_rangefunction PTEsfunction __folio_mod_statfunction __folio_add_rmapfunction folio_move_anon_rmapfunction __folio_set_anonfunction __page_check_anon_rmapfunction __folio_add_anon_rmapfunction folio_add_anon_rmap_ptesfunction folio_add_anon_rmap_pmdfunction folio_add_new_anon_rmapfunction __folio_add_file_rmapfunction folio_add_file_rmap_ptesfunction folio_add_file_rmap_pmdfunction folio_add_file_rmap_pud
Annotated Snippet
struct folio_referenced_arg {
int mapcount;
int referenced;
vm_flags_t vm_flags;
struct mem_cgroup *memcg;
};
/*
* arg: folio_referenced_arg will be passed
*/
static bool folio_referenced_one(struct folio *folio,
struct vm_area_struct *vma, unsigned long address, void *arg)
{
struct folio_referenced_arg *pra = arg;
DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
int ptes = 0, referenced = 0;
unsigned int nr;
while (page_vma_mapped_walk(&pvmw)) {
address = pvmw.address;
nr = 1;
if (vma->vm_flags & VM_LOCKED) {
ptes++;
pra->mapcount--;
/* Only mlock fully mapped pages */
if (pvmw.pte && ptes != pvmw.nr_pages)
continue;
/*
* All PTEs must be protected by page table lock in
* order to mlock the page.
*
* If page table boundary has been cross, current ptl
* only protect part of ptes.
*/
if (pvmw.flags & PVMW_PGTABLE_CROSSED)
continue;
/* Restore the mlock which got missed */
mlock_vma_folio(folio, vma);
page_vma_mapped_walk_done(&pvmw);
pra->vm_flags |= VM_LOCKED;
return false; /* To break the loop */
}
/*
* Skip the non-shared swapbacked folio mapped solely by
* the exiting or OOM-reaped process. This avoids redundant
* swap-out followed by an immediate unmap.
*/
if ((!atomic_read(&vma->vm_mm->mm_users) ||
check_stable_address_space(vma->vm_mm)) &&
folio_test_anon(folio) && folio_test_swapbacked(folio) &&
!folio_maybe_mapped_shared(folio)) {
pra->referenced = -1;
page_vma_mapped_walk_done(&pvmw);
return false;
}
if (pvmw.pte && folio_test_large(folio)) {
const unsigned long end_addr = pmd_addr_end(address, vma->vm_end);
const unsigned int max_nr = (end_addr - address) >> PAGE_SHIFT;
pte_t pteval = ptep_get(pvmw.pte);
nr = folio_pte_batch(folio, pvmw.pte, pteval, max_nr);
}
/*
* When LRU is switching, we don’t know where the surrounding folios
* are. —they could be on active/inactive lists or on MGLRU. So the
* simplest approach is to disable this look-around optimization.
*/
if (lru_gen_enabled() && !lru_gen_switching() && pvmw.pte) {
if (lru_gen_look_around(&pvmw, nr))
referenced++;
} else if (pvmw.pte) {
if (clear_flush_young_ptes_notify(vma, address, pvmw.pte, nr))
referenced++;
} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
if (pmdp_clear_flush_young_notify(vma, address,
pvmw.pmd))
referenced++;
} else {
/* unexpected pmd-mapped folio? */
WARN_ON_ONCE(1);
}
ptes += nr;
Annotation
- Immediate include surface: `linux/mm.h`, `linux/sched/mm.h`, `linux/sched/task.h`, `linux/pagemap.h`, `linux/swap.h`, `linux/leafops.h`, `linux/slab.h`, `linux/init.h`.
- Detected declarations: `struct folio_referenced_arg`, `struct wrprotect_file_state`, `function anon_vma_free`, `function anon_vma_chain_free`, `function anon_vma_chain_assign`, `function anon_vma_prepare`, `function check_anon_vma_clone`, `function maybe_reuse_anon_vma`, `function anon_vma_clone`, `function anon_vma_fork`.
- 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.
- 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.