Skip to content

Commit e73c4e3

Browse files
ubizjakIngo Molnar
authored andcommitted
locking/atomic/x86: Introduce arch_atomic64_read_nonatomic() to x86_32
Introduce arch_atomic64_read_nonatomic() for 32-bit targets to load the value from atomic64_t location in a non-atomic way. This function is intended to be used in cases where a subsequent atomic operation will handle the torn value, and can be used to prime the first iteration of unconditional try_cmpxchg() loops. Suggested-by: Mark Rutland <[email protected]> Signed-off-by: Uros Bizjak <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: Linus Torvalds <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 276b893 commit e73c4e3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

arch/x86/include/asm/atomic64_32.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ typedef struct {
1414

1515
#define ATOMIC64_INIT(val) { (val) }
1616

17+
/*
18+
* Read an atomic64_t non-atomically.
19+
*
20+
* This is intended to be used in cases where a subsequent atomic operation
21+
* will handle the torn value, and can be used to prime the first iteration
22+
* of unconditional try_cmpxchg() loops, e.g.:
23+
*
24+
* s64 val = arch_atomic64_read_nonatomic(v);
25+
* do { } while (!arch_atomic64_try_cmpxchg(v, &val, val OP i);
26+
*
27+
* This is NOT safe to use where the value is not always checked by a
28+
* subsequent atomic operation, such as in conditional try_cmpxchg() loops
29+
* that can break before the atomic operation, e.g.:
30+
*
31+
* s64 val = arch_atomic64_read_nonatomic(v);
32+
* do {
33+
* if (condition(val))
34+
* break;
35+
* } while (!arch_atomic64_try_cmpxchg(v, &val, val OP i);
36+
*/
37+
static __always_inline s64 arch_atomic64_read_nonatomic(const atomic64_t *v)
38+
{
39+
/* See comment in arch_atomic_read(). */
40+
return __READ_ONCE(v->counter);
41+
}
42+
1743
#define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
1844
#ifndef ATOMIC64_EXPORT
1945
#define ATOMIC64_DECL_ONE __ATOMIC64_DECL

0 commit comments

Comments
 (0)