Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do spinlocks really need DMB?

I'm working with a dual Cortex-A9 system and I've been trying to understand exactly why spinlock functions need to use DMB. It seems that as long as the merging store buffer is flushed the lock value should end up in the L1 on the unlocking core and the SCU should either invalidate or update the value in the L1 of the other core. This is enough to maintain coherency and safe locking right? And doesn't STREX skip the merging store buffer anyway, meaning we don't even need the flush?

DMB appears to be something of a blunt hammer, especially since it defaults to the system domain, which likely means a write all the way to main memory, which can be expensive.

Are the DMBs in the locks there as a workaround for drivers that don't use smp_mb properly?

I'm currently seeing, based on the performance counters, about 5% of my system cycles disappearing in stalls caused by DMB.

like image 971
Pete Fordham Avatar asked Oct 30 '12 22:10

Pete Fordham


People also ask

Why are spin locks normally avoided?

However, spinlocks become wasteful if held for longer durations, as they may prevent other threads from running and require rescheduling. The longer a thread holds a lock, the greater the risk that the thread will be interrupted by the OS scheduler while holding the lock.

Can a SpinLock sleep?

Therefore, the core rule that applies to spinlocks is that any code must, while holding a spinlock, be atomic. It cannot sleep; in fact, it cannot relinquish the processor for any reason except to service interrupts (and sometimes not even then).

Why do we use spin locks?

Spin locks are a low-level synchronization mechanism suitable primarily for use on shared memory multiprocessors. When the calling thread requests a spin lock that is already held by another thread, the second thread spins in a loop to test if the lock has become available.

Does Linux use spinlocks?

Any other processes that subsequently try to acquire the lock get stopped; they are said to "spin in place" waiting on the lock to be released by the first process, thus the name spin lock. The Linux kernel uses spin locks for many things, such as when sending data to a particular peripheral.


2 Answers

I found these articles may answer your question:

  • Locks, SWPs and two Smoking Barriers
  • Locks, SWPs and two Smoking Barriers (Part 2)

In particular:

You will note the Data Memory Barrier (DMB) instruction that is issued once the lock has been acquired. The DMB guarantees that all memory accesses before the memory barrier will be observed by all of the other CPUs in the system before all memory accesses made after the memory barrier. This makes more sense if you consider that once a lock has been acquired, a program will then access the data structure(s) locked by the lock. The DMB in the lock function above ensures that accesses to the locked data structure are observed after accesses to the lock.

like image 130
Mike Avatar answered Nov 15 '22 09:11

Mike


The DMB is needed in the SMP case because the other processor may see the memory accesses happening in a different order without it, i.e. accesses from inside the critical section may happen before the lock is taken from the point-of-view of the second core.

So the second core could see itself holding the lock and also see updates from inside the cricital section running on the other core, breaking consistency.

like image 27
Pete Fordham Avatar answered Nov 15 '22 07:11

Pete Fordham