Re: [PATCH] rxrpc_find_service_conn_rcu: use read_seqbegin() rather than read_seqbegin_or_lock()

From: David Howells
Date: Wed Nov 01 2023 - 11:46:21 EST


Oleg Nesterov <oleg@xxxxxxxxxx> wrote:

> read_seqbegin_or_lock() makes no sense unless you make "seq" odd
> after the lockless access failed.

I think you're wrong.

write_seqlock() turns it odd. For instance, if the read lock is taken first:

sequence seq CPU 1 CPU 2
======= ======= =============================== ===============
0
0 0 seq = 0 // MUST BE EVEN ACCORDING TO DOC
0 0 read_seqbegin_or_lock() [lockless]
...
1 0 write_seqlock()
1 0 need_seqretry() [seq=even; sequence!=seq: retry]
1 1 read_seqbegin_or_lock() [exclusive]
-->spin_lock(lock);
2 1 write_sequnlock()
<--locked
...
2 1 need_seqretry()

However, if the write lock is taken first:

sequence seq CPU 1 CPU 2
======= ======= =============================== ===============
0
1 write_seqlock()
1 0 seq = 0 // MUST BE EVEN ACCORDING TO DOC
1 0 read_seqbegin_or_lock() [lockless]
1 0 __read_seqcount_begin()
while (lock.sequence is odd)
cpu_relax();
2 0 write_sequnlock()
2 2 [loop end]
...
2 2 need_seqretry() [seq=even; sequence==seq; done]

Note that it spins in __read_seqcount_begin() until we get an even seq,
indicating that no write is currently in progress - at which point we can
perform a lockless pass.

> See thread_group_cputime() as an example, note that it does nextseq = 1 for
> the 2nd round.

That's not especially convincing.

David