srcu: Check for in-flight callbacks in _cleanup_srcu_struct()
authorPaul E. McKenney <paulmck@linux.ibm.com>
Tue, 12 Feb 2019 18:44:33 +0000 (10:44 -0800)
committerPaul E. McKenney <paulmck@linux.ibm.com>
Tue, 26 Mar 2019 21:39:24 +0000 (14:39 -0700)
If someone fails to drain the corresponding SRCU callbacks (for
example, by failing to invoke srcu_barrier()) before invoking either
cleanup_srcu_struct() or cleanup_srcu_struct_quiesced(), the resulting
diagnostic is an ambiguous use-after-free diagnostic, and even then
only if you are running something like KASAN.  This commit therefore
improves SRCU diagnostics by adding checks for in-flight callbacks at
_cleanup_srcu_struct() time.

Note that these diagnostics can still be defeated, for example, by
invoking call_srcu() concurrently with cleanup_srcu_struct().  Which is
a really bad idea, but sometimes all too easy to do.  But even then,
these diagnostics have at least some probability of catching the problem.

Reported-by: Sagi Grimberg <sagi@grimberg.me>
Reported-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
Tested-by: Bart Van Assche <bvanassche@acm.org>
kernel/rcu/srcutree.c

index a60b8ba9e1aca68d27731606f25f02b14ddcdbb9..4f30f3ecabc1fc3aba89e0119f02783608358838 100644 (file)
@@ -387,6 +387,8 @@ void _cleanup_srcu_struct(struct srcu_struct *ssp, bool quiesced)
                        del_timer_sync(&sdp->delay_work);
                        flush_work(&sdp->work);
                }
+               if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist)))
+                       return; /* Forgot srcu_barrier(), so just leak it! */
        }
        if (WARN_ON(rcu_seq_state(READ_ONCE(ssp->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
            WARN_ON(srcu_readers_active(ssp))) {