ipc/sem.c
Source file repositories/reference/linux-study-clean/ipc/sem.c
File Facts
- System
- Linux kernel
- Corpus path
ipc/sem.c- Extension
.c- Size
- 64625 bytes
- Lines
- 2485
- Domain
- Core OS
- Bucket
- IPC
- 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/compat.hlinux/slab.hlinux/spinlock.hlinux/init.hlinux/proc_fs.hlinux/time.hlinux/security.hlinux/syscalls.hlinux/audit.hlinux/capability.hlinux/seq_file.hlinux/rwsem.hlinux/nsproxy.hlinux/ipc_namespace.hlinux/sched/wake_q.hlinux/nospec.hlinux/rhashtable.hlinux/uaccess.hutil.h
Detected Declarations
syscall semgetsyscall semctlsyscall old_semctlsyscall semtimedopsyscall semtimedop_time32syscall semopstruct semstruct sem_arraystruct sem_queuestruct sem_undostruct sem_undo_liststruct compat_semid_dsfunction sem_lockfunction sem_exit_nsfunction sem_initfunction unmerge_queuesfunction merge_queuesfunction sem_rcu_freefunction complexmode_enterfunction complexmode_tryleavefunction sem_lockfunction sem_unlockfunction sem_lock_function sem_lock_and_putreffunction sem_rmidfunction newaryfunction sem_more_checksfunction ksys_semgetfunction operationfunction perform_atomic_semopfunction wake_up_sem_queue_preparefunction unlink_queuefunction Ofunction wake_const_opsfunction list_for_each_entry_safefunction do_smart_wakeup_zerofunction update_queuefunction set_semotimefunction do_smart_updatefunction check_qopfunction count_semcntfunction list_for_each_entryfunction list_for_each_entryfunction freearyfunction list_for_each_entry_safefunction list_for_each_entry_safefunction list_for_each_entry_safefunction copy_semid_to_user
Annotated Snippet
SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
{
return ksys_semget(key, nsems, semflg);
}
/**
* perform_atomic_semop[_slow] - Attempt to perform semaphore
* operations on a given array.
* @sma: semaphore array
* @q: struct sem_queue that describes the operation
*
* Caller blocking are as follows, based the value
* indicated by the semaphore operation (sem_op):
*
* (1) >0 never blocks.
* (2) 0 (wait-for-zero operation): semval is non-zero.
* (3) <0 attempting to decrement semval to a value smaller than zero.
*
* Returns 0 if the operation was possible.
* Returns 1 if the operation is impossible, the caller must sleep.
* Returns <0 for error codes.
*/
static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
{
int result, sem_op, nsops;
struct pid *pid;
struct sembuf *sop;
struct sem *curr;
struct sembuf *sops;
struct sem_undo *un;
sops = q->sops;
nsops = q->nsops;
un = q->undo;
for (sop = sops; sop < sops + nsops; sop++) {
int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
curr = &sma->sems[idx];
sem_op = sop->sem_op;
result = curr->semval;
if (!sem_op && result)
goto would_block;
result += sem_op;
if (result < 0)
goto would_block;
if (result > SEMVMX)
goto out_of_range;
if (sop->sem_flg & SEM_UNDO) {
int undo = un->semadj[sop->sem_num] - sem_op;
/* Exceeding the undo range is an error. */
if (undo < (-SEMAEM - 1) || undo > SEMAEM)
goto out_of_range;
un->semadj[sop->sem_num] = undo;
}
curr->semval = result;
}
sop--;
pid = q->pid;
while (sop >= sops) {
ipc_update_pid(&sma->sems[sop->sem_num].sempid, pid);
sop--;
}
return 0;
out_of_range:
result = -ERANGE;
goto undo;
would_block:
q->blocking = sop;
if (sop->sem_flg & IPC_NOWAIT)
result = -EAGAIN;
else
result = 1;
undo:
sop--;
while (sop >= sops) {
sem_op = sop->sem_op;
sma->sems[sop->sem_num].semval -= sem_op;
if (sop->sem_flg & SEM_UNDO)
un->semadj[sop->sem_num] += sem_op;
sop--;
Annotation
- Immediate include surface: `linux/compat.h`, `linux/slab.h`, `linux/spinlock.h`, `linux/init.h`, `linux/proc_fs.h`, `linux/time.h`, `linux/security.h`, `linux/syscalls.h`.
- Detected declarations: `syscall semget`, `syscall semctl`, `syscall old_semctl`, `syscall semtimedop`, `syscall semtimedop_time32`, `syscall semop`, `struct sem`, `struct sem_array`, `struct sem_queue`, `struct sem_undo`.
- Atlas domain: Core OS / IPC.
- 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.