Skip to content

Commit 886ee55

Browse files
author
Ingo Molnar
committed
locking/seqlock: Propagate 'const' pointers within read-only methods, remove forced type casts
Currently __seqprop_ptr() is an inline function that must chose to either use 'const' or non-const seqcount related pointers - but this results in the undesirable loss of 'const' propagation, via a forced type cast. The easiest solution would be to turn the pointer wrappers into macros that pass through whatever type is passed to them - but the clever maze of seqlock API instantiation macros relies on the GCC CPP '##' macro extension, which isn't recursive, so inline functions must be used here. So create two wrapper variants instead: 'ptr' and 'const_ptr', and pick the right one for the codepaths that are const: read_seqcount_begin() and read_seqcount_retry(). This cleans up type handling and allows the removal of all type forcing. No change in functionality. Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Oleg Nesterov <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Waiman Long <[email protected]> Cc: Will Deacon <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Paul E. McKenney <[email protected]>
1 parent ac8b60b commit 886ee55

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

include/linux/seqlock.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,15 @@ typedef struct seqcount_##lockname { \
200200
} seqcount_##lockname##_t; \
201201
\
202202
static __always_inline seqcount_t * \
203-
__seqprop_##lockname##_ptr(const seqcount_##lockname##_t *s) \
203+
__seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
204204
{ \
205-
return (void *)&s->seqcount; /* drop const */ \
205+
return &s->seqcount; \
206+
} \
207+
\
208+
static __always_inline const seqcount_t * \
209+
__seqprop_##lockname##_const_ptr(const seqcount_##lockname##_t *s) \
210+
{ \
211+
return &s->seqcount; \
206212
} \
207213
\
208214
static __always_inline unsigned \
@@ -247,9 +253,14 @@ __seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
247253
* __seqprop() for seqcount_t
248254
*/
249255

250-
static inline seqcount_t *__seqprop_ptr(const seqcount_t *s)
256+
static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
257+
{
258+
return s;
259+
}
260+
261+
static inline const seqcount_t *__seqprop_const_ptr(const seqcount_t *s)
251262
{
252-
return (void *)s; /* drop const */
263+
return s;
253264
}
254265

255266
static inline unsigned __seqprop_sequence(const seqcount_t *s)
@@ -302,6 +313,7 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
302313
__seqprop_case((s), mutex, prop))
303314

304315
#define seqprop_ptr(s) __seqprop(s, ptr)(s)
316+
#define seqprop_const_ptr(s) __seqprop(s, const_ptr)(s)
305317
#define seqprop_sequence(s) __seqprop(s, sequence)(s)
306318
#define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
307319
#define seqprop_assert(s) __seqprop(s, assert)(s)
@@ -353,7 +365,7 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
353365
*/
354366
#define read_seqcount_begin(s) \
355367
({ \
356-
seqcount_lockdep_reader_access(seqprop_ptr(s)); \
368+
seqcount_lockdep_reader_access(seqprop_const_ptr(s)); \
357369
raw_read_seqcount_begin(s); \
358370
})
359371

@@ -419,7 +431,7 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
419431
* Return: true if a read section retry is required, else false
420432
*/
421433
#define __read_seqcount_retry(s, start) \
422-
do___read_seqcount_retry(seqprop_ptr(s), start)
434+
do___read_seqcount_retry(seqprop_const_ptr(s), start)
423435

424436
static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
425437
{
@@ -439,7 +451,7 @@ static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
439451
* Return: true if a read section retry is required, else false
440452
*/
441453
#define read_seqcount_retry(s, start) \
442-
do_read_seqcount_retry(seqprop_ptr(s), start)
454+
do_read_seqcount_retry(seqprop_const_ptr(s), start)
443455

444456
static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
445457
{

0 commit comments

Comments
 (0)