Commit 1d78814d authored by Mark Rutland's avatar Mark Rutland Committed by Peter Zijlstra
Browse files

locking/atomic: scripts: simplify raw_atomic*() definitions



Currently each ordering variant has several potential definitions,
with a mixture of preprocessor and C definitions, including several
copies of its C prototype, e.g.

| #if defined(arch_atomic_fetch_andnot_acquire)
| #define raw_atomic_fetch_andnot_acquire arch_atomic_fetch_andnot_acquire
| #elif defined(arch_atomic_fetch_andnot_relaxed)
| static __always_inline int
| raw_atomic_fetch_andnot_acquire(int i, atomic_t *v)
| {
|       int ret = arch_atomic_fetch_andnot_relaxed(i, v);
|       __atomic_acquire_fence();
|       return ret;
| }
| #elif defined(arch_atomic_fetch_andnot)
| #define raw_atomic_fetch_andnot_acquire arch_atomic_fetch_andnot
| #else
| static __always_inline int
| raw_atomic_fetch_andnot_acquire(int i, atomic_t *v)
| {
|       return raw_atomic_fetch_and_acquire(~i, v);
| }
| #endif

Make this a bit simpler by defining the C prototype once, and writing
the various potential definitions as plain C code guarded by ifdeffery.
For example, the above becomes:

| static __always_inline int
| raw_atomic_fetch_andnot_acquire(int i, atomic_t *v)
| {
| #if defined(arch_atomic_fetch_andnot_acquire)
|         return arch_atomic_fetch_andnot_acquire(i, v);
| #elif defined(arch_atomic_fetch_andnot_relaxed)
|         int ret = arch_atomic_fetch_andnot_relaxed(i, v);
|         __atomic_acquire_fence();
|         return ret;
| #elif defined(arch_atomic_fetch_andnot)
|         return arch_atomic_fetch_andnot(i, v);
| #else
|         return raw_atomic_fetch_and_acquire(~i, v);
| #endif
| }

Which is far easier to read. As we now always have a single copy of the
C prototype wrapping all the potential definitions, we now have an
obvious single location for kerneldoc comments.

At the same time, the fallbacks for raw_atomic*_xhcg() are made to use
'new' rather than 'i' as the name of the new value. This is what the
existing fallback template used, and is more consistent with the
raw_atomic{_try,}cmpxchg() fallbacks.

There should be no functional change as a result of this patch.

Signed-off-by: default avatarMark Rutland <mark.rutland@arm.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230605070124.3741859-24-mark.rutland@arm.com
parent 63039946
Loading
Loading
Loading
Loading
+843 −947

File changed.

Preview size limit exceeded, changes collapsed.

+25 −25
Original line number Diff line number Diff line
@@ -462,33 +462,33 @@ atomic_fetch_xor_relaxed(int i, atomic_t *v)
}

static __always_inline int
atomic_xchg(atomic_t *v, int i)
atomic_xchg(atomic_t *v, int new)
{
	kcsan_mb();
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_xchg(v, i);
	return raw_atomic_xchg(v, new);
}

static __always_inline int
atomic_xchg_acquire(atomic_t *v, int i)
atomic_xchg_acquire(atomic_t *v, int new)
{
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_xchg_acquire(v, i);
	return raw_atomic_xchg_acquire(v, new);
}

static __always_inline int
atomic_xchg_release(atomic_t *v, int i)
atomic_xchg_release(atomic_t *v, int new)
{
	kcsan_release();
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_xchg_release(v, i);
	return raw_atomic_xchg_release(v, new);
}

static __always_inline int
atomic_xchg_relaxed(atomic_t *v, int i)
atomic_xchg_relaxed(atomic_t *v, int new)
{
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_xchg_relaxed(v, i);
	return raw_atomic_xchg_relaxed(v, new);
}

static __always_inline int
@@ -1103,33 +1103,33 @@ atomic64_fetch_xor_relaxed(s64 i, atomic64_t *v)
}

static __always_inline s64
atomic64_xchg(atomic64_t *v, s64 i)
atomic64_xchg(atomic64_t *v, s64 new)
{
	kcsan_mb();
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic64_xchg(v, i);
	return raw_atomic64_xchg(v, new);
}

static __always_inline s64
atomic64_xchg_acquire(atomic64_t *v, s64 i)
atomic64_xchg_acquire(atomic64_t *v, s64 new)
{
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic64_xchg_acquire(v, i);
	return raw_atomic64_xchg_acquire(v, new);
}

static __always_inline s64
atomic64_xchg_release(atomic64_t *v, s64 i)
atomic64_xchg_release(atomic64_t *v, s64 new)
{
	kcsan_release();
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic64_xchg_release(v, i);
	return raw_atomic64_xchg_release(v, new);
}

static __always_inline s64
atomic64_xchg_relaxed(atomic64_t *v, s64 i)
atomic64_xchg_relaxed(atomic64_t *v, s64 new)
{
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic64_xchg_relaxed(v, i);
	return raw_atomic64_xchg_relaxed(v, new);
}

static __always_inline s64
@@ -1744,33 +1744,33 @@ atomic_long_fetch_xor_relaxed(long i, atomic_long_t *v)
}

static __always_inline long
atomic_long_xchg(atomic_long_t *v, long i)
atomic_long_xchg(atomic_long_t *v, long new)
{
	kcsan_mb();
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_long_xchg(v, i);
	return raw_atomic_long_xchg(v, new);
}

static __always_inline long
atomic_long_xchg_acquire(atomic_long_t *v, long i)
atomic_long_xchg_acquire(atomic_long_t *v, long new)
{
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_long_xchg_acquire(v, i);
	return raw_atomic_long_xchg_acquire(v, new);
}

static __always_inline long
atomic_long_xchg_release(atomic_long_t *v, long i)
atomic_long_xchg_release(atomic_long_t *v, long new)
{
	kcsan_release();
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_long_xchg_release(v, i);
	return raw_atomic_long_xchg_release(v, new);
}

static __always_inline long
atomic_long_xchg_relaxed(atomic_long_t *v, long i)
atomic_long_xchg_relaxed(atomic_long_t *v, long new)
{
	instrument_atomic_read_write(v, sizeof(*v));
	return raw_atomic_long_xchg_relaxed(v, i);
	return raw_atomic_long_xchg_relaxed(v, new);
}

static __always_inline long
@@ -2231,4 +2231,4 @@ atomic_long_dec_if_positive(atomic_long_t *v)


#endif /* _LINUX_ATOMIC_INSTRUMENTED_H */
// f6502977180430e61c1a7c4e5e665f04f501fb8d
// a4c3d2b229f907654cc53cb5d40e80f7fed1ec9c
+13 −13
Original line number Diff line number Diff line
@@ -622,42 +622,42 @@ raw_atomic_long_fetch_xor_relaxed(long i, atomic_long_t *v)
}

static __always_inline long
raw_atomic_long_xchg(atomic_long_t *v, long i)
raw_atomic_long_xchg(atomic_long_t *v, long new)
{
#ifdef CONFIG_64BIT
	return raw_atomic64_xchg(v, i);
	return raw_atomic64_xchg(v, new);
#else
	return raw_atomic_xchg(v, i);
	return raw_atomic_xchg(v, new);
#endif
}

static __always_inline long
raw_atomic_long_xchg_acquire(atomic_long_t *v, long i)
raw_atomic_long_xchg_acquire(atomic_long_t *v, long new)
{
#ifdef CONFIG_64BIT
	return raw_atomic64_xchg_acquire(v, i);
	return raw_atomic64_xchg_acquire(v, new);
#else
	return raw_atomic_xchg_acquire(v, i);
	return raw_atomic_xchg_acquire(v, new);
#endif
}

static __always_inline long
raw_atomic_long_xchg_release(atomic_long_t *v, long i)
raw_atomic_long_xchg_release(atomic_long_t *v, long new)
{
#ifdef CONFIG_64BIT
	return raw_atomic64_xchg_release(v, i);
	return raw_atomic64_xchg_release(v, new);
#else
	return raw_atomic_xchg_release(v, i);
	return raw_atomic_xchg_release(v, new);
#endif
}

static __always_inline long
raw_atomic_long_xchg_relaxed(atomic_long_t *v, long i)
raw_atomic_long_xchg_relaxed(atomic_long_t *v, long new)
{
#ifdef CONFIG_64BIT
	return raw_atomic64_xchg_relaxed(v, i);
	return raw_atomic64_xchg_relaxed(v, new);
#else
	return raw_atomic_xchg_relaxed(v, i);
	return raw_atomic_xchg_relaxed(v, new);
#endif
}

@@ -872,4 +872,4 @@ raw_atomic_long_dec_if_positive(atomic_long_t *v)
}

#endif /* _LINUX_ATOMIC_LONG_H */
// ad09f849db0db5b30c82e497eeb9056a394c5f22
// e785d25cc3f220b7d473d36aac9da85dd7eb13a8
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ and vF i v
andnot			vF	i	v
or			vF	i	v
xor			vF	i	v
xchg			I	v	i
xchg			I	v	i:new
cmpxchg			I	v	i:old	i:new
try_cmpxchg		B	v	p:old	i:new
sub_and_test		b	i	v
+0 −4
Original line number Diff line number Diff line
cat <<EOF
static __always_inline ${ret}
raw_${atomic}_${pfx}${name}${sfx}_acquire(${params})
{
	${ret} ret = arch_${atomic}_${pfx}${name}${sfx}_relaxed(${args});
	__atomic_acquire_fence();
	return ret;
}
EOF
Loading