]> Gentwo Git Trees - linux/.git/commit
Avoid memory barrier in read_seqcount() through load acquire b4/seq_optimize
authorChristoph Lameter (Ampere) <cl@gentwo.org>
Tue, 13 Aug 2024 18:09:01 +0000 (11:09 -0700)
committerChristoph Lameter <cl@gentwo.org>
Tue, 24 Sep 2024 19:04:30 +0000 (12:04 -0700)
commit42fb2afeabf2d7bf87f4c50a8bfcc4bc2ff8dbf7
tree1ad210923a7ed7de7b1c356914af320e018c650c
parent5d9c725d489306453751216a41a77997008bfc0a
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.

Replace the first read barrier with a load acquire of
the seqcount which saves 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).

Further optimization is possible through the use of 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>
drivers/gpu/drm/i915/gt/intel_tlb.h
include/linux/seqlock.h