Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b3fbab05 authored by Paul E. McKenney's avatar Paul E. McKenney
Browse files

rcu: Restore checks for blocking in RCU read-side critical sections



Long ago, using TREE_RCU with PREEMPT would result in "scheduling
while atomic" diagnostics if you blocked in an RCU read-side critical
section.  However, PREEMPT now implies TREE_PREEMPT_RCU, which defeats
this diagnostic.  This commit therefore adds a replacement diagnostic
based on PROVE_RCU.

Because rcu_lockdep_assert() and lockdep_rcu_dereference() are now being
used for things that have nothing to do with rcu_dereference(), rename
lockdep_rcu_dereference() to lockdep_rcu_suspicious() and add a third
argument that is a string indicating what is suspicious.  This third
argument is passed in from a new third argument to rcu_lockdep_assert().
Update all calls to rcu_lockdep_assert() to add an informative third
argument.

Also, add a pair of rcu_lockdep_assert() calls from within
rcu_note_context_switch(), one complaining if a context switch occurs
in an RCU-bh read-side critical section and another complaining if a
context switch occurs in an RCU-sched read-side critical section.
These are present only if the PROVE_RCU kernel parameter is enabled.

Finally, fix some checkpatch whitespace complaints in lockdep.c.

Again, you must enable PROVE_RCU to see these new diagnostics.  But you
are enabling PROVE_RCU to check out new RCU uses in any case, aren't you?

Signed-off-by: default avatarPaul E. McKenney <paulmck@linux.vnet.ibm.com>
parent d5988af5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -548,7 +548,7 @@ do { \
#endif

#ifdef CONFIG_PROVE_RCU
extern void lockdep_rcu_dereference(const char *file, const int line);
void lockdep_rcu_suspicious(const char *file, const int line, const char *s);
#endif

#endif /* __LINUX_LOCKDEP_H */
+22 −6
Original line number Diff line number Diff line
@@ -297,19 +297,31 @@ extern int rcu_my_thread_group_empty(void);
/**
 * rcu_lockdep_assert - emit lockdep splat if specified condition not met
 * @c: condition to check
 * @s: informative message
 */
#define rcu_lockdep_assert(c)						\
#define rcu_lockdep_assert(c, s)					\
	do {								\
		static bool __warned;					\
		if (debug_lockdep_rcu_enabled() && !__warned && !(c)) {	\
			__warned = true;				\
			lockdep_rcu_dereference(__FILE__, __LINE__);	\
			lockdep_rcu_suspicious(__FILE__, __LINE__, s);	\
		}							\
	} while (0)

#define rcu_sleep_check()						\
	do {								\
		rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map),	\
				   "Illegal context switch in RCU-bh"	\
				   " read-side critical section");	\
		rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map),	\
				   "Illegal context switch in RCU-sched"\
				   " read-side critical section");	\
	} while (0)

#else /* #ifdef CONFIG_PROVE_RCU */

#define rcu_lockdep_assert(c) do { } while (0)
#define rcu_lockdep_assert(c, s) do { } while (0)
#define rcu_sleep_check() do { } while (0)

#endif /* #else #ifdef CONFIG_PROVE_RCU */

@@ -338,14 +350,16 @@ extern int rcu_my_thread_group_empty(void);
#define __rcu_dereference_check(p, c, space) \
	({ \
		typeof(*p) *_________p1 = (typeof(*p)*__force )ACCESS_ONCE(p); \
		rcu_lockdep_assert(c); \
		rcu_lockdep_assert(c, "suspicious rcu_dereference_check()" \
				      " usage"); \
		rcu_dereference_sparse(p, space); \
		smp_read_barrier_depends(); \
		((typeof(*p) __force __kernel *)(_________p1)); \
	})
#define __rcu_dereference_protected(p, c, space) \
	({ \
		rcu_lockdep_assert(c); \
		rcu_lockdep_assert(c, "suspicious rcu_dereference_protected()" \
				      " usage"); \
		rcu_dereference_sparse(p, space); \
		((typeof(*p) __force __kernel *)(p)); \
	})
@@ -359,7 +373,9 @@ extern int rcu_my_thread_group_empty(void);
#define __rcu_dereference_index_check(p, c) \
	({ \
		typeof(p) _________p1 = ACCESS_ONCE(p); \
		rcu_lockdep_assert(c); \
		rcu_lockdep_assert(c, \
				   "suspicious rcu_dereference_index_check()" \
				   " usage"); \
		smp_read_barrier_depends(); \
		(_________p1); \
	})
+47 −37
Original line number Diff line number Diff line
@@ -1129,7 +1129,8 @@ print_circular_bug_header(struct lock_list *entry, unsigned int depth,
	if (debug_locks_silent)
		return 0;

	printk("\n=======================================================\n");
	printk("\n");
	printk("======================================================\n");
	printk("[ INFO: possible circular locking dependency detected ]\n");
	print_kernel_version();
	printk("-------------------------------------------------------\n");
@@ -1463,7 +1464,8 @@ print_bad_irq_dependency(struct task_struct *curr,
	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
		return 0;

	printk("\n======================================================\n");
	printk("\n");
	printk("======================================================\n");
	printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
		irqclass, irqclass);
	print_kernel_version();
@@ -1692,7 +1694,8 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
		return 0;

	printk("\n=============================================\n");
	printk("\n");
	printk("=============================================\n");
	printk("[ INFO: possible recursive locking detected ]\n");
	print_kernel_version();
	printk("---------------------------------------------\n");
@@ -2177,7 +2180,8 @@ print_usage_bug(struct task_struct *curr, struct held_lock *this,
	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
		return 0;

	printk("\n=================================\n");
	printk("\n");
	printk("=================================\n");
	printk("[ INFO: inconsistent lock state ]\n");
	print_kernel_version();
	printk("---------------------------------\n");
@@ -2241,7 +2245,8 @@ print_irq_inversion_bug(struct task_struct *curr,
	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
		return 0;

	printk("\n=========================================================\n");
	printk("\n");
	printk("=========================================================\n");
	printk("[ INFO: possible irq lock inversion dependency detected ]\n");
	print_kernel_version();
	printk("---------------------------------------------------------\n");
@@ -3065,7 +3070,8 @@ print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
	if (debug_locks_silent)
		return 0;

	printk("\n=====================================\n");
	printk("\n");
	printk("=====================================\n");
	printk("[ BUG: bad unlock balance detected! ]\n");
	printk("-------------------------------------\n");
	printk("%s/%d is trying to release lock (",
@@ -3478,7 +3484,8 @@ print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
	if (debug_locks_silent)
		return 0;

	printk("\n=================================\n");
	printk("\n");
	printk("=================================\n");
	printk("[ BUG: bad contention detected! ]\n");
	printk("---------------------------------\n");
	printk("%s/%d is trying to contend lock (",
@@ -3839,7 +3846,8 @@ print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
	if (debug_locks_silent)
		return;

	printk("\n=========================\n");
	printk("\n");
	printk("=========================\n");
	printk("[ BUG: held lock freed! ]\n");
	printk("-------------------------\n");
	printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
@@ -3895,7 +3903,8 @@ static void print_held_locks_bug(struct task_struct *curr)
	if (debug_locks_silent)
		return;

	printk("\n=====================================\n");
	printk("\n");
	printk("=====================================\n");
	printk("[ BUG: lock held at task exit time! ]\n");
	printk("-------------------------------------\n");
	printk("%s/%d is exiting with locks still held!\n",
@@ -3991,7 +4000,8 @@ void lockdep_sys_exit(void)
	if (unlikely(curr->lockdep_depth)) {
		if (!debug_locks_off())
			return;
		printk("\n================================================\n");
		printk("\n");
		printk("================================================\n");
		printk("[ BUG: lock held when returning to user space! ]\n");
		printk("------------------------------------------------\n");
		printk("%s/%d is leaving the kernel with locks still held!\n",
@@ -4000,7 +4010,7 @@ void lockdep_sys_exit(void)
	}
}

void lockdep_rcu_dereference(const char *file, const int line)
void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
{
	struct task_struct *curr = current;

@@ -4009,15 +4019,15 @@ void lockdep_rcu_dereference(const char *file, const int line)
		return;
#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
	/* Note: the following can be executed concurrently, so be careful. */
	printk("\n===================================================\n");
	printk(  "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
	printk(  "---------------------------------------------------\n");
	printk("%s:%d invoked rcu_dereference_check() without protection!\n",
			file, line);
	printk("\n");
	printk("===============================\n");
	printk("[ INFO: suspicious RCU usage. ]\n");
	printk("-------------------------------\n");
	printk("%s:%d %s!\n", file, line, s);
	printk("\nother info that might help us debug this:\n\n");
	printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
	lockdep_print_held_locks(curr);
	printk("\nstack backtrace:\n");
	dump_stack();
}
EXPORT_SYMBOL_GPL(lockdep_rcu_dereference);
EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
+3 −1
Original line number Diff line number Diff line
@@ -418,7 +418,9 @@ EXPORT_SYMBOL(pid_task);
 */
struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
{
	rcu_lockdep_assert(rcu_read_lock_held());
	rcu_lockdep_assert(rcu_read_lock_held(),
			   "find_task_by_pid_ns() needs rcu_read_lock()"
			   " protection");
	return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
}

+2 −0
Original line number Diff line number Diff line
@@ -4237,6 +4237,7 @@ static inline void schedule_debug(struct task_struct *prev)
	 */
	if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
		__schedule_bug(prev);
	rcu_sleep_check();

	profile_hit(SCHED_PROFILING, __builtin_return_address(0));

@@ -8230,6 +8231,7 @@ void __might_sleep(const char *file, int line, int preempt_offset)
{
	static unsigned long prev_jiffy;	/* ratelimiting */

	rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
	if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
	    system_state != SYSTEM_RUNNING || oops_in_progress)
		return;