Skip to content

Commit 423a86a

Browse files
joelagnelPaul E. McKenney
authored andcommitted
rcu: Add sparse check to rcu_assign_pointer()
The rcu_assign_pointer() function currently doesn't do any sparse checking on the assigned-to pointer. So its possible that a pointer that is not __rcu annotated is assigned with rcu_assign_pointer without sparse complaints. Because rcu_dereference() already does such checking, this commit makes rcu_assign_pointer() to do the same. The extra error could be helpful in cases where an RCU pointer is assigned with rcu_assign_pointer() but not annotated with __rcu. This doesn't generate any code in the normal case because __CHECKER__ is defined only in the context of sparse. This commit also renames rcu_dereference_sparse() to rcu_check_parse() since the checking now happens not only during derereferencing but also during assignment. Test: Introduced an rcu_assign_pointer in code and checked the output of sparse with and without this change. The change correctly causes sparse to throw an error. Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org> Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
1 parent c2d8089 commit 423a86a

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

include/linux/rcupdate.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,30 +309,30 @@ static inline void rcu_preempt_sleep_check(void) { }
309309
*/
310310

311311
#ifdef __CHECKER__
312-
#define rcu_dereference_sparse(p, space) \
312+
#define rcu_check_sparse(p, space) \
313313
((void)(((typeof(*p) space *)p) == p))
314314
#else /* #ifdef __CHECKER__ */
315-
#define rcu_dereference_sparse(p, space)
315+
#define rcu_check_sparse(p, space)
316316
#endif /* #else #ifdef __CHECKER__ */
317317

318318
#define __rcu_access_pointer(p, space) \
319319
({ \
320320
typeof(*p) *_________p1 = (typeof(*p) *__force)READ_ONCE(p); \
321-
rcu_dereference_sparse(p, space); \
321+
rcu_check_sparse(p, space); \
322322
((typeof(*p) __force __kernel *)(_________p1)); \
323323
})
324324
#define __rcu_dereference_check(p, c, space) \
325325
({ \
326326
/* Dependency order vs. p above. */ \
327327
typeof(*p) *________p1 = (typeof(*p) *__force)READ_ONCE(p); \
328328
RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
329-
rcu_dereference_sparse(p, space); \
329+
rcu_check_sparse(p, space); \
330330
((typeof(*p) __force __kernel *)(________p1)); \
331331
})
332332
#define __rcu_dereference_protected(p, c, space) \
333333
({ \
334334
RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
335-
rcu_dereference_sparse(p, space); \
335+
rcu_check_sparse(p, space); \
336336
((typeof(*p) __force __kernel *)(p)); \
337337
})
338338
#define rcu_dereference_raw(p) \
@@ -382,6 +382,7 @@ static inline void rcu_preempt_sleep_check(void) { }
382382
#define rcu_assign_pointer(p, v) \
383383
({ \
384384
uintptr_t _r_a_p__v = (uintptr_t)(v); \
385+
rcu_check_sparse(p, __rcu); \
385386
\
386387
if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
387388
WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
@@ -785,7 +786,7 @@ static inline notrace void rcu_read_unlock_sched_notrace(void)
785786
*/
786787
#define RCU_INIT_POINTER(p, v) \
787788
do { \
788-
rcu_dereference_sparse(p, __rcu); \
789+
rcu_check_sparse(p, __rcu); \
789790
WRITE_ONCE(p, RCU_INITIALIZER(v)); \
790791
} while (0)
791792

0 commit comments

Comments
 (0)