mm/util.c
Source file repositories/reference/linux-study-clean/mm/util.c
File Facts
- System
- Linux kernel
- Corpus path
mm/util.c- Extension
.c- Size
- 43913 bytes
- Lines
- 1609
- 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/slab.hlinux/string.hlinux/compiler.hlinux/export.hlinux/err.hlinux/sched.hlinux/sched/mm.hlinux/sched/signal.hlinux/sched/task_stack.hlinux/security.hlinux/swap.hlinux/swapops.hlinux/sysctl.hlinux/mman.hlinux/hugetlb.hlinux/vmalloc.hlinux/userfaultfd_k.hlinux/elf.hlinux/elf-randomize.hlinux/personality.hlinux/random.hlinux/processor.hlinux/sizes.hlinux/compat.hlinux/fsnotify.hlinux/page_idle.hlinux/uaccess.hkunit/visibility.hinternal.hswap.h
Detected Declarations
function kfree_constfunction kreallocfunction init_user_bucketsfunction vma_is_stack_for_currentfunction vma_set_filefunction randomize_stack_topfunction randomize_pagefunction arch_randomize_brkfunction arch_mmap_rndfunction mmap_is_legacyfunction mmap_basefunction arch_pick_mmap_layoutfunction arch_pick_mmap_layoutfunction __account_locked_vmfunction account_locked_vmfunction vm_mmap_pgofffunction do_mmapfunction vm_mmap_shadow_stackfunction infunction folio_copyfunction folio_mc_copyfunction overcommit_ratio_handlerfunction sync_overcommit_asfunction overcommit_policy_handlerfunction overcommit_kbytes_handlerfunction init_vm_util_sysctlsfunction vm_commit_limitfunction vm_memory_committedfunction __vm_enough_memoryfunction get_cmdlinefunction setproctitlefunction memcmp_pagesfunction pr_contfunction page_offline_freezefunction page_offline_thawfunction page_offline_beginfunction page_offline_endfunction flush_dcache_foliofunction compat_set_desc_from_vmafunction __compat_vma_mmapfunction compat_vma_mmapfunction set_ps_flagsfunction snapshot_pagefunction call_vma_mappedfunction mmap_action_finishfunction check_mmap_actionfunction mmap_action_preparefunction mmap_action_prepare
Annotated Snippet
ret = do_mmap(file, addr, len, prot, flag, 0, pgoff, &populate,
&uf);
mmap_write_unlock(mm);
userfaultfd_unmap_complete(mm, &uf);
if (populate)
mm_populate(ret, populate);
}
return ret;
}
/*
* Perform a userland memory mapping into the current process address space. See
* the comment for do_mmap() for more details on this operation in general.
*
* This differs from do_mmap() in that:
*
* a. An offset parameter is provided rather than pgoff, which is both checked
* for overflow and page alignment.
* b. mmap locking is performed on the caller's behalf.
* c. Userfaultfd unmap events and memory population are handled.
*
* This means that this function performs essentially the same work as if
* userland were invoking mmap (2).
*
* Returns either an error, or the address at which the requested mapping has
* been performed.
*/
unsigned long vm_mmap(struct file *file, unsigned long addr,
unsigned long len, unsigned long prot,
unsigned long flag, unsigned long offset)
{
if (unlikely(offset + PAGE_ALIGN(len) < offset))
return -EINVAL;
if (unlikely(offset_in_page(offset)))
return -EINVAL;
return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
}
EXPORT_SYMBOL(vm_mmap);
#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK
/*
* Perform a userland memory mapping for a shadow stack into the current
* process address space. This is intended to be used by architectures that
* support user shadow stacks.
*/
unsigned long vm_mmap_shadow_stack(unsigned long addr, unsigned long len,
unsigned long flags)
{
struct mm_struct *mm = current->mm;
unsigned long ret, unused;
vm_flags_t vm_flags = VM_SHADOW_STACK;
flags |= MAP_ANONYMOUS | MAP_PRIVATE;
if (addr)
flags |= MAP_FIXED_NOREPLACE;
if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
vm_flags |= VM_NOHUGEPAGE;
mmap_write_lock(mm);
ret = do_mmap(NULL, addr, len, PROT_READ | PROT_WRITE, flags,
vm_flags, 0, &unused, NULL);
mmap_write_unlock(mm);
return ret;
}
#endif /* CONFIG_ARCH_HAS_USER_SHADOW_STACK */
/**
* __vmalloc_array - allocate memory for a virtually contiguous array.
* @n: number of elements.
* @size: element size.
* @flags: the type of memory to allocate (see kmalloc).
*/
void *__vmalloc_array_noprof(size_t n, size_t size, gfp_t flags)
{
size_t bytes;
if (unlikely(check_mul_overflow(n, size, &bytes)))
return NULL;
return __vmalloc_noprof(bytes, flags);
}
EXPORT_SYMBOL(__vmalloc_array_noprof);
/**
* vmalloc_array - allocate memory for a virtually contiguous array.
* @n: number of elements.
* @size: element size.
*/
Annotation
- Immediate include surface: `linux/mm.h`, `linux/slab.h`, `linux/string.h`, `linux/compiler.h`, `linux/export.h`, `linux/err.h`, `linux/sched.h`, `linux/sched/mm.h`.
- Detected declarations: `function kfree_const`, `function krealloc`, `function init_user_buckets`, `function vma_is_stack_for_current`, `function vma_set_file`, `function randomize_stack_top`, `function randomize_page`, `function arch_randomize_brk`, `function arch_mmap_rnd`, `function mmap_is_legacy`.
- 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.