kernel/locking/semaphore.c
Source file repositories/reference/linux-study-clean/kernel/locking/semaphore.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/locking/semaphore.c- Extension
.c- Size
- 9445 bytes
- Lines
- 355
- 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/compiler.hlinux/kernel.hlinux/export.hlinux/sched.hlinux/sched/debug.hlinux/sched/wake_q.hlinux/semaphore.hlinux/spinlock.hlinux/ftrace.htrace/events/lock.hlinux/hung_task.h
Detected Declarations
struct semaphore_waiterfunction hung_task_sem_set_holderfunction hung_task_sem_clear_if_holderfunction sem_last_holderfunction hung_task_sem_set_holderfunction __sem_acquirefunction down_killablefunction down_interruptiblefunction down_killablefunction down_trylockfunction down_timeoutfunction downfunction sem_del_waiterfunction ___down_commonfunction __down_commonfunction __downfunction __down_interruptiblefunction __down_killablefunction __down_timeoutfunction __upexport downexport down_interruptibleexport down_killableexport down_trylockexport down_timeoutexport up
Annotated Snippet
struct semaphore_waiter {
struct list_head list;
struct task_struct *task;
bool up;
};
static inline
void sem_del_waiter(struct semaphore *sem, struct semaphore_waiter *waiter)
{
if (list_empty(&waiter->list)) {
sem->first_waiter = NULL;
return;
}
if (sem->first_waiter == waiter) {
sem->first_waiter = list_first_entry(&waiter->list,
struct semaphore_waiter, list);
}
list_del(&waiter->list);
}
/*
* Because this function is inlined, the 'state' parameter will be
* constant, and thus optimised away by the compiler. Likewise the
* 'timeout' parameter for the cases without timeouts.
*/
static inline int __sched ___down_common(struct semaphore *sem, long state,
long timeout)
{
struct semaphore_waiter waiter, *first;
first = sem->first_waiter;
if (first) {
list_add_tail(&waiter.list, &first->list);
} else {
INIT_LIST_HEAD(&waiter.list);
sem->first_waiter = &waiter;
}
waiter.task = current;
waiter.up = false;
for (;;) {
if (signal_pending_state(state, current))
goto interrupted;
if (unlikely(timeout <= 0))
goto timed_out;
__set_current_state(state);
raw_spin_unlock_irq(&sem->lock);
timeout = schedule_timeout(timeout);
raw_spin_lock_irq(&sem->lock);
if (waiter.up) {
hung_task_sem_set_holder(sem);
return 0;
}
}
timed_out:
sem_del_waiter(sem, &waiter);
return -ETIME;
interrupted:
sem_del_waiter(sem, &waiter);
return -EINTR;
}
static inline int __sched __down_common(struct semaphore *sem, long state,
long timeout)
{
int ret;
hung_task_set_blocker(sem, BLOCKER_TYPE_SEM);
trace_contention_begin(sem, 0);
ret = ___down_common(sem, state, timeout);
trace_contention_end(sem, ret);
hung_task_clear_blocker();
return ret;
}
static noinline void __sched __down(struct semaphore *sem)
{
__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}
static noinline int __sched __down_interruptible(struct semaphore *sem)
{
return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}
Annotation
- Immediate include surface: `linux/compiler.h`, `linux/kernel.h`, `linux/export.h`, `linux/sched.h`, `linux/sched/debug.h`, `linux/sched/wake_q.h`, `linux/semaphore.h`, `linux/spinlock.h`.
- Detected declarations: `struct semaphore_waiter`, `function hung_task_sem_set_holder`, `function hung_task_sem_clear_if_holder`, `function sem_last_holder`, `function hung_task_sem_set_holder`, `function __sem_acquire`, `function down_killable`, `function down_interruptible`, `function down_killable`, `function down_trylock`.
- 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.