Re: [PATCH v2 2/3] printk: add lockless buffer

From: Linus Torvalds
Date: Mon May 18 2020 - 13:22:53 EST


On Mon, May 18, 2020 at 6:04 AM John Ogness <john.ogness@xxxxxxxxxxxxx> wrote:
>
> The cmpxchg() needs to be moved out of the while condition so that a
> continue can be used as intended. Patch below.

This seems pointless and wrong (patch edited to remove the '-' lines
so that you see the end result):

> smp_mb(); /* LMM(data_push_tail:C) */
>
> + if (atomic_long_try_cmpxchg_relaxed(&data_ring->tail_lpos,
> + &tail_lpos,
> + next_lpos)) { /* LMM(data_push_tail:D) */
> + break;
> + }

Doing an "smp_mb()" followed by a "cmpxchg_relaxed" seems all kinds of
odd and pointless, and is very much non-optimal on x86 for example.,

Just remove the smp_mb(), and use the non-relaxed form of cmpxchg.
It's defined to be fully ordered if it succeeds (and if the cmpxchg
doesn't succeed, it's a no-op and the memory barrier shouldn't make
any difference).

Otherwise you'll do two memory ordering operations on x86 (and
probably some other architectures), since the cmpxchg is always
ordered on x86 and there exists no "relaxed" form of it.

Linus