lib/cmpxchg-emu.c
Source file repositories/reference/linux-study-clean/lib/cmpxchg-emu.c
File Facts
- System
- Linux kernel
- Corpus path
lib/cmpxchg-emu.c- Extension
.c- Size
- 1086 bytes
- Lines
- 46
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- 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.
Dependency Surface
linux/types.hlinux/export.hlinux/instrumented.hlinux/atomic.hlinux/panic.hlinux/bug.hasm-generic/rwonce.hlinux/cmpxchg-emu.h
Detected Declarations
function cmpxchg_emu_u8export cmpxchg_emu_u8
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0+
/*
* Emulated 1-byte cmpxchg operation for architectures lacking direct
* support for this size. This is implemented in terms of 4-byte cmpxchg
* operations.
*
* Copyright (C) 2024 Paul E. McKenney.
*/
#include <linux/types.h>
#include <linux/export.h>
#include <linux/instrumented.h>
#include <linux/atomic.h>
#include <linux/panic.h>
#include <linux/bug.h>
#include <asm-generic/rwonce.h>
#include <linux/cmpxchg-emu.h>
union u8_32 {
u8 b[4];
u32 w;
};
/* Emulate one-byte cmpxchg() in terms of 4-byte cmpxchg. */
uintptr_t cmpxchg_emu_u8(volatile u8 *p, uintptr_t old, uintptr_t new)
{
u32 *p32 = (u32 *)(((uintptr_t)p) & ~0x3);
int i = ((uintptr_t)p) & 0x3;
union u8_32 old32;
union u8_32 new32;
u32 ret;
ret = READ_ONCE(*p32);
do {
old32.w = ret;
if (old32.b[i] != old)
return old32.b[i];
new32.w = old32.w;
new32.b[i] = new;
instrument_atomic_read_write(p, 1);
ret = data_race(cmpxchg(p32, old32.w, new32.w)); // Overridden above.
} while (ret != old32.w);
return old;
}
EXPORT_SYMBOL_GPL(cmpxchg_emu_u8);
Annotation
- Immediate include surface: `linux/types.h`, `linux/export.h`, `linux/instrumented.h`, `linux/atomic.h`, `linux/panic.h`, `linux/bug.h`, `asm-generic/rwonce.h`, `linux/cmpxchg-emu.h`.
- Detected declarations: `function cmpxchg_emu_u8`, `export cmpxchg_emu_u8`.
- Atlas domain: Kernel Services / lib.
- 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.