mm/madvise.c
Source file repositories/reference/linux-study-clean/mm/madvise.c
File Facts
- System
- Linux kernel
- Corpus path
mm/madvise.c- Extension
.c- Size
- 59867 bytes
- Lines
- 2239
- 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/mman.hlinux/pagemap.hlinux/syscalls.hlinux/mempolicy.hlinux/page-isolation.hlinux/page_idle.hlinux/userfaultfd_k.hlinux/hugetlb.hlinux/falloc.hlinux/fadvise.hlinux/sched.hlinux/sched/mm.hlinux/mm_inline.hlinux/mmu_context.hlinux/string.hlinux/uio.hlinux/ksm.hlinux/fs.hlinux/file.hlinux/blkdev.hlinux/backing-dev.hlinux/pagewalk.hlinux/swap.hlinux/leafops.hlinux/shmem_fs.hlinux/mmu_notifier.hasm/tlb.hinternal.hswap.h
Detected Declarations
syscall madvisesyscall process_madvisestruct madvise_walk_privatestruct madvise_behavior_rangestruct madvise_behaviorenum madvise_lock_modefunction anon_vma_name_freefunction replace_anon_vma_namefunction replace_anon_vma_namefunction madvise_update_vmafunction swapin_walk_pmd_entryfunction shmem_swapin_rangefunction mark_mmap_lock_droppedfunction madvise_willneedfunction can_do_file_pageoutfunction madvise_folio_pte_batchfunction madvise_cold_or_pageout_pte_rangefunction madvise_cold_page_rangefunction can_madv_lru_vmafunction madvise_coldfunction madvise_pageout_page_rangefunction madvise_pageoutfunction madvise_free_pte_rangefunction softleaf_is_poison_markerfunction get_walk_lockfunction madvise_free_single_vmafunction msyncfunction madvise_dontneed_free_valid_vmafunction madvise_dontneed_freefunction madvise_populatefunction madvise_removefunction is_valid_guard_vmafunction is_guard_pte_markerfunction guard_install_pud_entryfunction guard_install_pmd_entryfunction guard_install_pte_entryfunction guard_install_set_ptefunction madvise_guard_installfunction guard_remove_pud_entryfunction guard_remove_pmd_entryfunction guard_remove_pte_entryfunction madvise_guard_removefunction is_discardfunction madvisefunction can_madvise_modifyfunction madvise_vma_behaviorfunction madvise_inject_errorfunction is_memory_failure
Annotated Snippet
SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
{
return do_madvise(current->mm, start, len_in, behavior);
}
/* Perform an madvise operation over a vector of addresses and lengths. */
static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter,
int behavior)
{
ssize_t ret = 0;
size_t total_len;
struct mmu_gather tlb;
struct madvise_behavior madv_behavior = {
.mm = mm,
.behavior = behavior,
.tlb = &tlb,
};
total_len = iov_iter_count(iter);
ret = madvise_lock(&madv_behavior);
if (ret)
return ret;
madvise_init_tlb(&madv_behavior);
while (iov_iter_count(iter)) {
unsigned long start = (unsigned long)iter_iov_addr(iter);
size_t len_in = iter_iov_len(iter);
int error;
error = check_input_range(start, len_in);
if (error || !len_in)
ret = error;
else
ret = madvise_do_behavior(start, len_in, &madv_behavior);
/*
* An madvise operation is attempting to restart the syscall,
* but we cannot proceed as it would not be correct to repeat
* the operation in aggregate, and would be surprising to the
* user.
*
* We drop and reacquire locks so it is safe to just loop and
* try again. We check for fatal signals in case we need exit
* early anyway.
*/
if (ret == -ERESTARTNOINTR) {
if (fatal_signal_pending(current)) {
ret = -EINTR;
break;
}
/* Drop and reacquire lock to unwind race. */
madvise_finish_tlb(&madv_behavior);
madvise_unlock(&madv_behavior);
ret = madvise_lock(&madv_behavior);
if (ret)
goto out;
madvise_init_tlb(&madv_behavior);
continue;
}
if (ret < 0)
break;
iov_iter_advance(iter, iter_iov_len(iter));
}
madvise_finish_tlb(&madv_behavior);
madvise_unlock(&madv_behavior);
out:
ret = (total_len - iov_iter_count(iter)) ? : ret;
return ret;
}
SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
size_t, vlen, int, behavior, unsigned int, flags)
{
ssize_t ret;
struct iovec iovstack[UIO_FASTIOV];
struct iovec *iov = iovstack;
struct iov_iter iter;
struct task_struct *task;
struct mm_struct *mm;
unsigned int f_flags;
if (flags != 0) {
ret = -EINVAL;
goto out;
}
ret = import_iovec(ITER_DEST, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
Annotation
- Immediate include surface: `linux/mman.h`, `linux/pagemap.h`, `linux/syscalls.h`, `linux/mempolicy.h`, `linux/page-isolation.h`, `linux/page_idle.h`, `linux/userfaultfd_k.h`, `linux/hugetlb.h`.
- Detected declarations: `syscall madvise`, `syscall process_madvise`, `struct madvise_walk_private`, `struct madvise_behavior_range`, `struct madvise_behavior`, `enum madvise_lock_mode`, `function anon_vma_name_free`, `function replace_anon_vma_name`, `function replace_anon_vma_name`, `function madvise_update_vma`.
- 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.