Skip to content

Commit 96450ea

Browse files
surenbaghdasaryanPeter Zijlstra
authored andcommitted
seqlock: add raw_seqcount_try_begin
Add raw_seqcount_try_begin() to opens a read critical section of the given seqcount_t if the counter is even. This enables eliding the critical section entirely if the counter is odd, instead of doing the speculation knowing it will fail. Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Suren Baghdasaryan <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: David Hildenbrand <[email protected]> Reviewed-by: Liam R. Howlett <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent b4943b8 commit 96450ea

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/linux/seqlock.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
318318
__seq; \
319319
})
320320

321+
/**
322+
* raw_seqcount_try_begin() - begin a seqcount_t read critical section
323+
* w/o lockdep and w/o counter stabilization
324+
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
325+
*
326+
* Similar to raw_seqcount_begin(), except it enables eliding the critical
327+
* section entirely if odd, instead of doing the speculation knowing it will
328+
* fail.
329+
*
330+
* Useful when counter stabilization is more or less equivalent to taking
331+
* the lock and there is a slowpath that does that.
332+
*
333+
* If true, start will be set to the (even) sequence count read.
334+
*
335+
* Return: true when a read critical section is started.
336+
*/
337+
#define raw_seqcount_try_begin(s, start) \
338+
({ \
339+
start = raw_read_seqcount(s); \
340+
!(start & 1); \
341+
})
342+
321343
/**
322344
* raw_seqcount_begin() - begin a seqcount_t read critical section w/o
323345
* lockdep and w/o counter stabilization

0 commit comments

Comments
 (0)