mm/mmap_lock.c
Source file repositories/reference/linux-study-clean/mm/mmap_lock.c
File Facts
- System
- Linux kernel
- Corpus path
mm/mmap_lock.c- Extension
.c- Size
- 16041 bytes
- Lines
- 571
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
trace/events/mmap_lock.hlinux/mm.hlinux/cgroup.hlinux/memcontrol.hlinux/mmap_lock.hlinux/mutex.hlinux/percpu.hlinux/rcupdate.hlinux/smp.hlinux/trace_events.hlinux/local_lock.hlinux/extable.h
Detected Declarations
struct vma_exclude_readers_statefunction __mmap_lock_do_trace_start_lockingfunction __mmap_lock_do_trace_acquire_returnedfunction __mmap_lock_do_trace_releasedfunction __vma_end_exclude_readersfunction get_target_refcntfunction vma_refcount_putfunction vma_mark_attachedfunction __vma_start_writefunction __vma_exclude_readers_for_detachfunction READ_ONCEfunction __refcount_inc_not_zero_limited_acquirefunction vma_end_write_allfunction get_mmap_lock_carefullyfunction mmap_upgrade_trylockfunction upgrade_mmap_lock_carefullyfunction lockingexport __mmap_lock_do_trace_start_lockingexport __mmap_lock_do_trace_acquire_returnedexport __mmap_lock_do_trace_releasedexport __vma_start_write
Annotated Snippet
struct vma_exclude_readers_state {
/* Input parameters. */
struct vm_area_struct *vma;
int state; /* TASK_KILLABLE or TASK_UNINTERRUPTIBLE. */
bool detaching;
/* Output parameters. */
bool detached;
bool exclusive; /* Are we exclusively locked? */
};
/*
* Now that all readers have been evicted, mark the VMA as being out of the
* 'exclude readers' state.
*/
static void __vma_end_exclude_readers(struct vma_exclude_readers_state *ves)
{
struct vm_area_struct *vma = ves->vma;
VM_WARN_ON_ONCE(ves->detached);
ves->detached = refcount_sub_and_test(VM_REFCNT_EXCLUDE_READERS_FLAG,
&vma->vm_refcnt);
__vma_lockdep_release_exclusive(vma);
}
static unsigned int get_target_refcnt(struct vma_exclude_readers_state *ves)
{
const unsigned int tgt = ves->detaching ? 0 : 1;
return tgt | VM_REFCNT_EXCLUDE_READERS_FLAG;
}
/*
* Mark the VMA as being in a state of excluding readers, check to see if any
* VMA read locks are indeed held, and if so wait for them to be released.
*
* Note that this function pairs with vma_refcount_put() which will wake up this
* thread when it detects that the last reader has released its lock.
*
* The ves->state parameter ought to be set to TASK_UNINTERRUPTIBLE in cases
* where we wish the thread to sleep uninterruptibly or TASK_KILLABLE if a fatal
* signal is permitted to kill it.
*
* The function sets the ves->exclusive parameter to true if readers were
* excluded, or false if the VMA was detached or an error arose on wait.
*
* If the function indicates an exclusive lock was acquired via ves->exclusive
* the caller is required to invoke __vma_end_exclude_readers() once the
* exclusive state is no longer required.
*
* If ves->state is set to something other than TASK_UNINTERRUPTIBLE, the
* function may also return -EINTR to indicate a fatal signal was received while
* waiting. Otherwise, the function returns 0.
*/
static int __vma_start_exclude_readers(struct vma_exclude_readers_state *ves)
{
struct vm_area_struct *vma = ves->vma;
unsigned int tgt_refcnt = get_target_refcnt(ves);
int err = 0;
mmap_assert_write_locked(vma->vm_mm);
/*
* If vma is detached then only vma_mark_attached() can raise the
* vm_refcnt. mmap_write_lock prevents racing with vma_mark_attached().
*
* See the comment describing the vm_area_struct->vm_refcnt field for
* details of possible refcnt values.
*/
if (!refcount_add_not_zero(VM_REFCNT_EXCLUDE_READERS_FLAG, &vma->vm_refcnt)) {
ves->detached = true;
return 0;
}
__vma_lockdep_acquire_exclusive(vma);
err = rcuwait_wait_event(&vma->vm_mm->vma_writer_wait,
refcount_read(&vma->vm_refcnt) == tgt_refcnt,
ves->state);
if (err) {
__vma_end_exclude_readers(ves);
return err;
}
__vma_lockdep_stat_mark_acquired(vma);
ves->exclusive = true;
return 0;
}
int __vma_start_write(struct vm_area_struct *vma, int state)
Annotation
- Immediate include surface: `trace/events/mmap_lock.h`, `linux/mm.h`, `linux/cgroup.h`, `linux/memcontrol.h`, `linux/mmap_lock.h`, `linux/mutex.h`, `linux/percpu.h`, `linux/rcupdate.h`.
- Detected declarations: `struct vma_exclude_readers_state`, `function __mmap_lock_do_trace_start_locking`, `function __mmap_lock_do_trace_acquire_returned`, `function __mmap_lock_do_trace_released`, `function __vma_end_exclude_readers`, `function get_target_refcnt`, `function vma_refcount_put`, `function vma_mark_attached`, `function __vma_start_write`, `function __vma_exclude_readers_for_detach`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration implementation candidate.
- 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.