mm/migrate.c
Source file repositories/reference/linux-study-clean/mm/migrate.c
File Facts
- System
- Linux kernel
- Corpus path
mm/migrate.c- Extension
.c- Size
- 77434 bytes
- Lines
- 2772
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core 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.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- 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/migrate.hlinux/export.hlinux/swap.hlinux/leafops.hlinux/pagemap.hlinux/buffer_head.hlinux/mm_inline.hlinux/ksm.hlinux/rmap.hlinux/topology.hlinux/cpu.hlinux/cpuset.hlinux/writeback.hlinux/mempolicy.hlinux/vmalloc.hlinux/security.hlinux/backing-dev.hlinux/compaction.hlinux/syscalls.hlinux/compat.hlinux/hugetlb.hlinux/gfp.hlinux/page_idle.hlinux/page_owner.hlinux/sched/mm.hlinux/ptrace.hlinux/memory.hlinux/sched/sysctl.hlinux/memory-tiers.hlinux/pagewalk.hasm/tlbflush.htrace/events/migrate.h
Detected Declarations
syscall move_pagesstruct rmap_walk_argstruct migrate_pages_statsfunction set_movable_opsfunction isolate_movable_ops_pagefunction putback_movable_ops_pagefunction migrate_movable_ops_pagefunction folio_isolate_hugetlbfunction list_for_each_entry_safefunction isolate_folio_to_listfunction try_to_map_unused_to_zeropagefunction remove_migration_ptefunction remove_migration_ptesfunction migration_entry_waitfunction migration_entry_wait_hugefunction pmd_migration_entry_waitfunction __folio_migrate_mappingfunction folio_migrate_mappingfunction folio_migrate_mappingfunction folio_migrate_flagsfunction __migrate_foliofunction migrate_foliofunction buffer_migrate_lock_buffersfunction __buffer_migrate_foliofunction buffer_migrate_foliofunction buffer_migrate_folio_norefsfunction filemap_migrate_foliofunction fallback_migrate_foliofunction move_to_new_foliofunction __migrate_folio_recordfunction __migrate_folio_extractfunction migrate_folio_undo_srcfunction migrate_folio_undo_dstfunction migrate_folio_donefunction migrate_folio_unmapfunction try_to_unmapfunction migrate_folio_movefunction unmap_and_move_pagefunction folio_mappingfunction try_split_foliofunction migrate_hugetlbsfunction list_for_each_entry_safefunction migrate_folios_movefunction migrate_folios_undofunction migrate_pages_batchfunction list_for_each_entry_safefunction migrate_pagesfunction migrate_pages_sync
Annotated Snippet
SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
const void __user * __user *, pages,
const int __user *, nodes,
int __user *, status, int, flags)
{
return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
}
#endif /* CONFIG_NUMA_MIGRATION */
#ifdef CONFIG_NUMA_BALANCING
/*
* Returns true if this is a safe migration target node for misplaced NUMA
* pages. Currently it only checks the watermarks which is crude.
*/
static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
unsigned long nr_migrate_pages)
{
int z;
for (z = pgdat->nr_zones - 1; z >= 0; z--) {
struct zone *zone = pgdat->node_zones + z;
if (!managed_zone(zone))
continue;
/* Avoid waking kswapd by allocating pages_to_migrate pages. */
if (!zone_watermark_ok(zone, 0,
high_wmark_pages(zone) +
nr_migrate_pages,
ZONE_MOVABLE, ALLOC_CMA))
continue;
return true;
}
return false;
}
static struct folio *alloc_misplaced_dst_folio(struct folio *src,
unsigned long data)
{
int nid = (int) data;
int order = folio_order(src);
gfp_t gfp = __GFP_THISNODE;
if (order > 0)
gfp |= GFP_TRANSHUGE_LIGHT;
else {
gfp |= GFP_HIGHUSER_MOVABLE | __GFP_NOMEMALLOC | __GFP_NORETRY |
__GFP_NOWARN;
gfp &= ~__GFP_RECLAIM;
}
return __folio_alloc_node(gfp, order, nid);
}
/*
* Prepare for calling migrate_misplaced_folio() by isolating the folio if
* permitted. Must be called with the PTL still held.
*/
int migrate_misplaced_folio_prepare(struct folio *folio,
struct vm_area_struct *vma, int node)
{
int nr_pages = folio_nr_pages(folio);
pg_data_t *pgdat = NODE_DATA(node);
if (folio_is_file_lru(folio)) {
/*
* Do not migrate file folios that are mapped in multiple
* processes with execute permissions as they are probably
* shared libraries.
*
* See folio_maybe_mapped_shared() on possible imprecision
* when we cannot easily detect if a folio is shared.
*/
if ((vma->vm_flags & VM_EXEC) && folio_maybe_mapped_shared(folio))
return -EACCES;
/*
* Do not migrate dirty folios as not all filesystems can move
* dirty folios in MIGRATE_ASYNC mode which is a waste of
* cycles.
*/
if (folio_test_dirty(folio))
return -EAGAIN;
}
/* Avoid migrating to a node that is nearly full */
if (!migrate_balanced_pgdat(pgdat, nr_pages)) {
int z;
if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING))
return -EAGAIN;
Annotation
- Immediate include surface: `linux/migrate.h`, `linux/export.h`, `linux/swap.h`, `linux/leafops.h`, `linux/pagemap.h`, `linux/buffer_head.h`, `linux/mm_inline.h`, `linux/ksm.h`.
- Detected declarations: `syscall move_pages`, `struct rmap_walk_arg`, `struct migrate_pages_stats`, `function set_movable_ops`, `function isolate_movable_ops_page`, `function putback_movable_ops_page`, `function migrate_movable_ops_page`, `function folio_isolate_hugetlb`, `function list_for_each_entry_safe`, `function isolate_folio_to_list`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: core 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.