kernel/locking/spinlock.c
Source file repositories/reference/linux-study-clean/kernel/locking/spinlock.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/locking/spinlock.c- Extension
.c- Size
- 10853 bytes
- Lines
- 428
- 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/linkage.hlinux/preempt.hlinux/spinlock.hlinux/interrupt.hlinux/debug_locks.hlinux/export.h
Detected Declarations
function _raw_spin_trylockfunction _raw_spin_trylock_bhfunction _raw_spin_lockfunction _raw_spin_lock_irqsavefunction _raw_spin_lock_irqfunction _raw_spin_lock_bhfunction _raw_spin_unlockfunction _raw_spin_unlock_irqrestorefunction _raw_spin_unlock_irqfunction _raw_spin_unlock_bhfunction _raw_read_trylockfunction _raw_read_lockfunction _raw_read_lock_irqsavefunction _raw_read_lock_irqfunction _raw_read_lock_bhfunction _raw_read_unlockfunction _raw_read_unlock_irqrestorefunction _raw_read_unlock_irqfunction _raw_read_unlock_bhfunction _raw_write_trylockfunction _raw_write_lockfunction _raw_write_lock_nestedfunction _raw_write_lock_irqsavefunction _raw_write_lock_irqfunction _raw_write_lock_bhfunction _raw_write_unlockfunction _raw_write_unlock_irqrestorefunction _raw_write_unlock_irqfunction _raw_write_unlock_bhfunction _raw_spin_lock_nestedfunction _raw_spin_lock_irqsave_nestedfunction _raw_spin_lock_nest_lockfunction in_lock_functionsfunction lockdep_assert_in_softirq_funcexport _raw_spin_trylockexport _raw_spin_trylock_bhexport _raw_spin_lockexport _raw_spin_lock_irqsaveexport _raw_spin_lock_irqexport _raw_spin_lock_bhexport _raw_spin_unlockexport _raw_spin_unlock_irqrestoreexport _raw_spin_unlock_irqexport _raw_spin_unlock_bhexport _raw_read_trylockexport _raw_read_lockexport _raw_read_lock_irqsaveexport _raw_read_lock_irq
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (2004) Linus Torvalds
*
* Author: Zwane Mwaikambo <zwane@fsmlabs.com>
*
* Copyright (2004, 2005) Ingo Molnar
*
* This file contains the spinlock/rwlock implementations for the
* SMP and the DEBUG_SPINLOCK cases. (UP-nondebug inlines them)
*
* Note that some architectures have special knowledge about the
* stack frames of these functions in their profile_pc. If you
* change anything significant here that could change the stack
* frame contact the architecture maintainers.
*/
#include <linux/linkage.h>
#include <linux/preempt.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/debug_locks.h>
#include <linux/export.h>
#ifdef CONFIG_MMIOWB
#ifndef arch_mmiowb_state
DEFINE_PER_CPU(struct mmiowb_state, __mmiowb_state);
EXPORT_PER_CPU_SYMBOL(__mmiowb_state);
#endif
#endif
/*
* If lockdep is enabled then we use the non-preemption spin-ops
* even on CONFIG_PREEMPT, because lockdep assumes that interrupts are
* not re-enabled during lock-acquire (which the preempt-spin-ops do):
*/
#if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC)
/*
* The __lock_function inlines are taken from
* spinlock : include/linux/spinlock_api_smp.h
* rwlock : include/linux/rwlock_api_smp.h
*/
#else
/*
* Some architectures can relax in favour of the CPU owning the lock.
*/
#ifndef arch_read_relax
# define arch_read_relax(l) cpu_relax()
#endif
#ifndef arch_write_relax
# define arch_write_relax(l) cpu_relax()
#endif
#ifndef arch_spin_relax
# define arch_spin_relax(l) cpu_relax()
#endif
/*
* We build the __lock_function inlines here. They are too large for
* inlining all over the place, but here is only one user per function
* which embeds them into the calling _lock_function below.
*
* This could be a long-held lock. We both prepare to spin for a long
* time (making _this_ CPU preemptible if possible), and we also signal
* towards that other CPU that it should break the lock ASAP.
*/
#define BUILD_LOCK_OPS(op, locktype, lock_ctx_op) \
static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \
lock_ctx_op(lock) \
{ \
for (;;) { \
preempt_disable(); \
if (likely(do_raw_##op##_trylock(lock))) \
break; \
preempt_enable(); \
\
arch_##op##_relax(&lock->raw_lock); \
} \
} \
\
static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \
lock_ctx_op(lock) \
{ \
unsigned long flags; \
\
for (;;) { \
preempt_disable(); \
local_irq_save(flags); \
if (likely(do_raw_##op##_trylock(lock))) \
break; \
Annotation
- Immediate include surface: `linux/linkage.h`, `linux/preempt.h`, `linux/spinlock.h`, `linux/interrupt.h`, `linux/debug_locks.h`, `linux/export.h`.
- Detected declarations: `function _raw_spin_trylock`, `function _raw_spin_trylock_bh`, `function _raw_spin_lock`, `function _raw_spin_lock_irqsave`, `function _raw_spin_lock_irq`, `function _raw_spin_lock_bh`, `function _raw_spin_unlock`, `function _raw_spin_unlock_irqrestore`, `function _raw_spin_unlock_irq`, `function _raw_spin_unlock_bh`.
- 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.