kernel/locking/mutex.c
Source file repositories/reference/linux-study-clean/kernel/locking/mutex.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/locking/mutex.c- Extension
.c- Size
- 34157 bytes
- Lines
- 1298
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- 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
linux/mutex.hlinux/ww_mutex.hlinux/sched/signal.hlinux/sched/rt.hlinux/sched/wake_q.hlinux/sched/debug.hlinux/export.hlinux/spinlock.hlinux/interrupt.hlinux/debug_locks.hlinux/osq_lock.hlinux/hung_task.htrace/events/lock.hmutex.hww_mutex.h
Detected Declarations
function Copyrightfunction mutex_is_lockedfunction __owner_flagsfunction mutex_get_ownerfunction __mutex_trylock_or_handofffunction __mutex_trylockfunction mutex_init_genericfunction __mutex_trylockfunction __mutex_unlock_fastfunction mutex_init_lockdepfunction __mutex_set_flagfunction __mutex_clear_flagfunction __mutex_add_waiterfunction __mutex_remove_waiterfunction __mutex_trylockfunction tofunction ww_mutex_spin_on_ownerfunction mutex_spin_on_ownerfunction mutex_can_spin_on_ownerfunction afunction need_reschedfunction mutex_optimistic_spinfunction tofunction mutexfunction mutex_optimistic_spinfunction mutex_unlockfunction __mutex_lockfunction __ww_mutex_lockfunction ww_mutex_trylockfunction mutex_lock_nestedfunction _mutex_lock_nest_lockfunction _mutex_lock_killablefunction mutex_lock_interruptible_nestedfunction mutex_lock_io_nestedfunction ww_mutex_deadlock_injectionfunction ww_mutex_lockfunction ww_mutex_lock_interruptiblefunction __mutex_unlock_slowpathfunction mutex_lock_interruptiblefunction mutex_lock_killablefunction mutex_lock_iofunction __mutex_lock_slowpathfunction __mutex_lock_killable_slowpathfunction __mutex_lock_interruptible_slowpathfunction __ww_mutex_lock_slowpathfunction __ww_mutex_lock_interruptible_slowpathfunction down_trylockfunction _mutex_trylock_nest_lock
Annotated Snippet
if (task) {
if (flags & MUTEX_FLAG_PICKUP) {
if (task != curr)
break;
flags &= ~MUTEX_FLAG_PICKUP;
} else if (handoff) {
if (flags & MUTEX_FLAG_HANDOFF)
break;
flags |= MUTEX_FLAG_HANDOFF;
} else {
break;
}
} else {
MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
task = curr;
}
if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
if (task == curr)
return NULL;
break;
}
}
return __owner_task(owner);
}
/*
* Trylock or set HANDOFF
*/
static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
{
return !__mutex_trylock_common(lock, handoff);
}
/*
* Actual trylock that will work on any unlocked state.
*/
static inline bool __mutex_trylock(struct mutex *lock)
{
return !__mutex_trylock_common(lock, false);
}
#ifndef CONFIG_DEBUG_LOCK_ALLOC
/*
* Lockdep annotations are contained to the slow paths for simplicity.
* There is nothing that would stop spreading the lockdep annotations outwards
* except more code.
*/
void mutex_init_generic(struct mutex *lock)
{
__mutex_init_generic(lock);
}
EXPORT_SYMBOL(mutex_init_generic);
/*
* Optimistic trylock that only works in the uncontended case. Make sure to
* follow with a __mutex_trylock() before failing.
*/
static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
__cond_acquires(true, lock)
{
unsigned long curr = (unsigned long)current;
unsigned long zero = 0UL;
MUTEX_WARN_ON(lock->magic != lock);
if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
return true;
return false;
}
static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
__cond_releases(true, lock)
{
unsigned long curr = (unsigned long)current;
return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
}
#else /* !CONFIG_DEBUG_LOCK_ALLOC */
void mutex_init_lockdep(struct mutex *lock, const char *name, struct lock_class_key *key)
{
__mutex_init_generic(lock);
/*
* Make sure we are not reinitializing a held lock:
*/
Annotation
- Immediate include surface: `linux/mutex.h`, `linux/ww_mutex.h`, `linux/sched/signal.h`, `linux/sched/rt.h`, `linux/sched/wake_q.h`, `linux/sched/debug.h`, `linux/export.h`, `linux/spinlock.h`.
- Detected declarations: `function Copyright`, `function mutex_is_locked`, `function __owner_flags`, `function mutex_get_owner`, `function __mutex_trylock_or_handoff`, `function __mutex_trylock`, `function mutex_init_generic`, `function __mutex_trylock`, `function __mutex_unlock_fast`, `function mutex_init_lockdep`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- 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.