]> Gentwo Git Trees - linux/.git/commit
Avoid memory barrier in read_seqcount() through load acquire sent/20240813-seq_optimize-68c48696c798-v3
authorChristoph Lameter (Ampere) <cl@gentwo.org>
Tue, 13 Aug 2024 18:09:01 +0000 (11:09 -0700)
committerChristoph Lameter <cl@gentwo.org>
Thu, 12 Sep 2024 22:44:22 +0000 (15:44 -0700)
commit08dce3cb0767702b65e8b4be85eadda00bd82417
treed19ad408992e767db13105b9aeae23a008af66f9
parent77f587896757708780a7e8792efe62939f25a5ab
Avoid memory barrier in read_seqcount() through load acquire

Some architectures support load acquire which can save us a memory
barrier and save some cycles.

A typical sequence

do {
seq = read_seqcount_begin(&s);
<something>
} while (read_seqcount_retry(&s, seq);

requires 13 cycles on ARM64 for an empty loop. Two read memory
barriers are needed. One for each of the seqcount_* functions.

We can replace the first read barrier with a load acquire of
the seqcount which saves us one barrier.

On ARM64 doing so reduces the cycle count from 13 to 8.

This is a general improvement for the ARM64 architecture and not
specific to a certain processor. The cycle count here was
obtained on a Neoverse N1 (Ampere Altra).

We can further optimize handling by using the cond_load_acquire logic
which will give an ARM CPU a chance to enter some power saving mode
while waiting for changes to a cacheline thereby avoiding busy loops
and therefore saving power.

The ARM documentation states that load acquire is more effective
than a load plus barrier. In general that tends to be true on all
compute platforms that support both.

See (as quoted by Linus Torvalds):
   https://developer.arm.com/documentation/102336/0100/Load-Acquire-and-Store-Release-instructions

 "Weaker ordering requirements that are imposed by Load-Acquire and
  Store-Release instructions allow for micro-architectural
  optimizations, which could reduce some of the performance impacts that
  are otherwise imposed by an explicit memory barrier.

  If the ordering requirement is satisfied using either a Load-Acquire
  or Store-Release, then it would be preferable to use these
  instructions instead of a DMB"

The patch benefited significantly from the knowledge of the innards
of the seqlock code by Thomas Gleixner.

Signed-off-by: Christoph Lameter (Ampere) <cl@gentwo.org>
arch/Kconfig
arch/arm64/Kconfig
include/linux/seqlock.h