mm/mempolicy.c
Source file repositories/reference/linux-study-clean/mm/mempolicy.c
File Facts
- System
- Linux kernel
- Corpus path
mm/mempolicy.c- Extension
.c- Size
- 102785 bytes
- Lines
- 3949
- 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/mempolicy.hlinux/pagewalk.hlinux/highmem.hlinux/hugetlb.hlinux/kernel.hlinux/sched.hlinux/sched/mm.hlinux/sched/numa_balancing.hlinux/sched/sysctl.hlinux/sched/task.hlinux/nodemask.hlinux/cpuset.hlinux/slab.hlinux/string.hlinux/export.hlinux/nsproxy.hlinux/interrupt.hlinux/init.hlinux/compat.hlinux/ptrace.hlinux/swap.hlinux/seq_file.hlinux/proc_fs.hlinux/memory-tiers.hlinux/migrate.hlinux/ksm.hlinux/rmap.hlinux/security.hlinux/syscalls.hlinux/ctype.hlinux/mm_inline.hlinux/mmu_notifier.h
Detected Declarations
syscall set_mempolicy_home_nodesyscall mbindsyscall set_mempolicysyscall migrate_pagessyscall get_mempolicystruct weighted_interleave_statestruct migration_mpolstruct queue_pagesstruct iw_node_attrstruct sysfs_wi_groupfunction get_il_weightfunction reduce_interleave_weightsfunction mempolicy_set_node_perffunction numa_nearest_nodefunction IDfunction for_each_node_maskfunction mpol_store_user_nodemaskfunction mpol_relative_nodemaskfunction mpol_new_nodemaskfunction mpol_new_preferredfunction mpol_newfunction emptyfunction __mpol_putfunction mpol_rebind_defaultfunction mpol_rebind_preferredfunction mpol_rebind_policyfunction mpol_rebind_policyfunction mpol_rebind_mmfunction strictly_unmovablefunction queue_folio_requiredfunction queue_folios_pmdfunction queue_folios_hugetlbfunction folio_can_map_prot_numafunction change_prot_numafunction queue_pages_test_walkfunction queue_pages_rangefunction vma_replace_policyfunction mbind_rangefunction do_set_mempolicyfunction get_mempolicyfunction lookup_nodefunction do_get_mempolicyfunction migrate_folio_addfunction migrate_to_nodefunction do_migrate_pagesfunction for_each_node_maskfunction migrate_folio_addfunction do_migrate_pages
Annotated Snippet
SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, len,
unsigned long, home_node, unsigned long, flags)
{
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma, *prev;
struct mempolicy *new, *old;
unsigned long end;
int err = -ENOENT;
VMA_ITERATOR(vmi, mm, start);
start = untagged_addr(start);
if (start & ~PAGE_MASK)
return -EINVAL;
/*
* flags is used for future extension if any.
*/
if (flags != 0)
return -EINVAL;
/*
* Check home_node is online to avoid accessing uninitialized
* NODE_DATA.
*/
if (home_node >= MAX_NUMNODES || !node_online(home_node))
return -EINVAL;
len = PAGE_ALIGN(len);
end = start + len;
if (end < start)
return -EINVAL;
if (end == start)
return 0;
mmap_write_lock(mm);
prev = vma_prev(&vmi);
for_each_vma_range(vmi, vma, end) {
/*
* If any vma in the range got policy other than MPOL_BIND
* or MPOL_PREFERRED_MANY we return error. We don't reset
* the home node for vmas we already updated before.
*/
old = vma_policy(vma);
if (!old) {
prev = vma;
continue;
}
if (old->mode != MPOL_BIND && old->mode != MPOL_PREFERRED_MANY) {
err = -EOPNOTSUPP;
break;
}
new = mpol_dup(old);
if (IS_ERR(new)) {
err = PTR_ERR(new);
break;
}
vma_start_write(vma);
new->home_node = home_node;
err = mbind_range(&vmi, vma, &prev, start, end, new);
mpol_put(new);
if (err)
break;
}
mmap_write_unlock(mm);
return err;
}
SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
unsigned long, mode, const unsigned long __user *, nmask,
unsigned long, maxnode, unsigned int, flags)
{
return kernel_mbind(start, len, mode, nmask, maxnode, flags);
}
/* Set the process memory policy */
static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask,
unsigned long maxnode)
{
unsigned short mode_flags;
nodemask_t nodes;
int lmode = mode;
int err;
err = sanitize_mpol_flags(&lmode, &mode_flags);
if (err)
return err;
err = get_nodes(&nodes, nmask, maxnode);
if (err)
return err;
Annotation
- Immediate include surface: `linux/mempolicy.h`, `linux/pagewalk.h`, `linux/highmem.h`, `linux/hugetlb.h`, `linux/kernel.h`, `linux/sched.h`, `linux/sched/mm.h`, `linux/sched/numa_balancing.h`.
- Detected declarations: `syscall set_mempolicy_home_node`, `syscall mbind`, `syscall set_mempolicy`, `syscall migrate_pages`, `syscall get_mempolicy`, `struct weighted_interleave_state`, `struct migration_mpol`, `struct queue_pages`, `struct iw_node_attr`, `struct sysfs_wi_group`.
- 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.