Skip to contentSkip to conversation

ARCv2: Implement atomic64 based on LLOCKD/SCONDD instructions

PATCH Superseded Vineet Gupta 2 participants 3 messages Sep 8, 2016 – Sep 9, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARCv2: Implement atomic64 based on LLOCKD/SCONDD instructions

ARCv2 ISA provides 64-bit exclusive load/stores so use them to implement the 64-bit atomics and elide the spinlock based generic 64-bit atomics

boot tested with atomic64 self-test (and GOD bless the person who wrote them, I realized my inline assmebly is sloppy as hell)

One thing I'm not sure of is the lack of explicit memory clobber in barrier-less ops e.g. atomic64_add() (BTW same is true for 32-bit atomic_add() as well). Per commit 398aa66827 ("ARM: 6212/1: atomic ops:
add memory constraints to inline asm ") Will fixed ARM code by adding appropriate constraint to atomic64_add(). For ARC instead adding memory clobber to atomic64_set() does the trick (otherwise self-test is broked) This is on ARC we can't possibly use "m" in atomic64_add() since that make gcc emit register relative effective addresses which LLOCKD/SCONDD are not allowed by ISA

Cc Peter Zijlstra <peterz at infradead.org>
Cc Will Deacon <will.deacon at arm.com>
Cc linux-snps-arc at lists.infradead.org
Cc linux-kernel at vger.kernel.org
Signed-off-by Vineet Gupta <vgupta at synopsys.com>
---
 arch/arc/Kconfig              |   2 +-
 arch/arc/include/asm/atomic.h | 255 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 254 insertions(+), 3 deletions(-)
arch/arc/Kconfig +1 -1
index 0d3e59f56974..073b3582544b 100644
@@ -13,7 +13,7 @@ config ARC
 	select CLKSRC_OF
 	select CLONE_BACKWARDS
 	select COMMON_CLK
-	select GENERIC_ATOMIC64
+	select GENERIC_ATOMIC64 if !ISA_ARCV2 || !(ARC_HAS_LL64 && ARC_HAS_LLSC)
 	select GENERIC_CLOCKEVENTS
 	select GENERIC_FIND_FIRST_BIT
 	# for now, we don't need GENERIC_IRQ_PROBE, CONFIG_GENERIC_IRQ_CHIP
arch/arc/include/asm/atomic.h +253 -2

269 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 9 lines · Vineet Gupta

One thing I'm not sure of is the lack of explicit memory clobber in barrier-less ops e.g. atomic64_add() (BTW same is true for 32-bit atomic_add() as well). Per commit 398aa66827 ("ARM: 6212/1: atomic ops:
add memory constraints to inline asm ") Will fixed ARM code by adding appropriate constraint to atomic64_add(). For ARC instead adding memory clobber to atomic64_set() does the trick (otherwise self-test is broked) This is on ARC we can't possibly use "m" in atomic64_add() since that make gcc emit register relative effective addresses which LLOCKD/SCONDD are not allowed by ISA

So interestingly my self-test run fine, but I had this oldish version stashed somewhere which did something liek below and that clearly generates wrong code.

int my_test_atomic(void)

{
    long v0 = 0x33333333;
    long onestwos = 0x11112222;

    atomic_t v = ATOMIC_INIT(v0);
    long long r = v0;
    int ret = 0;

    atomic_set(&v, v0); r = v0;
    atomic_add(onestwos, &v);
    r += onestwos;
    if (v.counter != r) {      /* <------ */
                ret = 3;   /* error */
    }

    return ret;
}

key here is the check - if we access the atomic directly, I get error. If I use atomic_read() which forces a reload due to volatile, things are hunky dory. So it seems to me we don't need memory clobber or equivalent in barrier less atomics except the set. Seems too fragile ?

-Vineet

maintainer
#2 lore raw
Quoted 33 lines · Vineet Gupta
Quoted 9 lines · Vineet Gupta

One thing I'm not sure of is the lack of explicit memory clobber in barrier-less ops e.g. atomic64_add() (BTW same is true for 32-bit atomic_add() as well). Per commit 398aa66827 ("ARM: 6212/1: atomic ops:
add memory constraints to inline asm ") Will fixed ARM code by adding appropriate constraint to atomic64_add(). For ARC instead adding memory clobber to atomic64_set() does the trick (otherwise self-test is broked) This is on ARC we can't possibly use "m" in atomic64_add() since that make gcc emit register relative effective addresses which LLOCKD/SCONDD are not allowed by ISA

So interestingly my self-test run fine, but I had this oldish version stashed somewhere which did something liek below and that clearly generates wrong code.

int my_test_atomic(void)

{
    long v0 = 0x33333333;
    long onestwos = 0x11112222;

    atomic_t v = ATOMIC_INIT(v0);
    long long r = v0;
    int ret = 0;

    atomic_set(&v, v0); r = v0;
    atomic_add(onestwos, &v);
    r += onestwos;
    if (v.counter != r) {      /* <------ */
                ret = 3;   /* error */
    }

    return ret;
}
Quoted 4 lines

key here is the check - if we access the atomic directly, I get error. If I use atomic_read() which forces a reload due to volatile, things are hunky dory. So it seems to me we don't need memory clobber or equivalent in barrier less atomics except the set. Seems too fragile ?

Accessing atomic_t::counter without the accessors is undefined behaviour and you pretty much get to keep whatever pieces, although volatile accesses generally work (except when it doesn't, see blackfin SMP for example).

atomic_set() should be at least WRITE_ONCE().
atomic_read() should be at least READ_ONCE().

atomic_$op(), atomic_fetch_$op_relaxed() and atomic_$op_return_relaxed() need not imply any sort of barrier, compiler or otherwise.

atomic_fetch_$op() and atomic_$op_return(), which imply memory ordering, also very much imply a compiler barrier, since all memory barriers do.