Re: numlist_pop(): Re: [RFC PATCH v4 1/9] printk-rb: add a new printk ringbuffer implementation

From: Peter Zijlstra
Date: Wed Sep 04 2019 - 08:20:18 EST


On Tue, Aug 20, 2019 at 10:15:18AM +0200, Petr Mladek wrote:

> do {
> tail_id = atomic_long_read(&nl->tail_id);
>
> /*
> * Read might fail when the tail node has been removed
> * and reused in parallel.
> */
> if (!numlist_read(nl, tail_id, NULL, &next_id))
> continue;
>
> /* Make sure the node is not the only node on the list. */
> if (next_id == tail_id)
> return NULL;
>
> /* cC: Make sure the node is not busy. */
> if (nl->busy(tail_id, nl->busy_arg))
> return NULL;
>
> while (atomic_long_cmpxchg_relaxed(&nl->tail_id, tail_id, next_id) !=
> tail_id);

Both you and John should have a look at atomic*_try_cmpxchg*(); with
that you can write the above as:

tail_id = atomic_long_read(&nl->tai_id);
do {
...
} while (!atomic_long_try_cmpxchg_relaxed(&nl->tail_id, &tail_id, next_id));

And get better code-gen to boot.