Skip to content

Commit 4f988f1

Browse files
committed
seqlock: add 'raw_seqcount_begin()' function
The normal read_seqcount_begin() function will wait for any current writers to exit their critical region by looping until the sequence count is even. That "wait for sequence count to stabilize" is the right thing to do if the read-locker will just retry the whole operation on contention: no point in doing a potentially expensive reader sequence if we know at the beginning that we'll just end up re-doing it all. HOWEVER. Some users don't actually retry the operation, but instead will abort and do the operation with proper locking. So the sequence count case may be the optimistic quick case, but in the presense of writers you may want to do full locking in order to guarantee forward progress. The prime example of this would be the RCU name lookup. And in that case, you may well be better off without the "retry early", and are in a rush to instead get to the failure handling. Thus this "raw" interface that just returns the sequence number without testing it - it just forces the low bit to zero so that read_seqcount_retry() will always fail such a "active concurrent writer" scenario. Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2f62427 commit 4f988f1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

include/linux/seqlock.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ static inline unsigned read_seqcount_begin(const seqcount_t *s)
165165
return ret;
166166
}
167167

168+
/**
169+
* raw_seqcount_begin - begin a seq-read critical section
170+
* @s: pointer to seqcount_t
171+
* Returns: count to be passed to read_seqcount_retry
172+
*
173+
* raw_seqcount_begin opens a read critical section of the given seqcount.
174+
* Validity of the critical section is tested by checking read_seqcount_retry
175+
* function.
176+
*
177+
* Unlike read_seqcount_begin(), this function will not wait for the count
178+
* to stabilize. If a writer is active when we begin, we will fail the
179+
* read_seqcount_retry() instead of stabilizing at the beginning of the
180+
* critical section.
181+
*/
182+
static inline unsigned raw_seqcount_begin(const seqcount_t *s)
183+
{
184+
unsigned ret = ACCESS_ONCE(s->sequence);
185+
smp_rmb();
186+
return ret & ~1;
187+
}
188+
168189
/**
169190
* __read_seqcount_retry - end a seq-read critical section (without barrier)
170191
* @s: pointer to seqcount_t

0 commit comments

Comments
 (0)