md: protect md_unregister_thread from reentrancy
[platform/kernel/linux-rpi.git] / drivers / md / md.c
index b553654..bf1c5c0 100644 (file)
@@ -7942,17 +7942,22 @@ EXPORT_SYMBOL(md_register_thread);
 
 void md_unregister_thread(struct md_thread **threadp)
 {
-       struct md_thread *thread = *threadp;
-       if (!thread)
-               return;
-       pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
-       /* Locking ensures that mddev_unlock does not wake_up a
+       struct md_thread *thread;
+
+       /*
+        * Locking ensures that mddev_unlock does not wake_up a
         * non-existent thread
         */
        spin_lock(&pers_lock);
+       thread = *threadp;
+       if (!thread) {
+               spin_unlock(&pers_lock);
+               return;
+       }
        *threadp = NULL;
        spin_unlock(&pers_lock);
 
+       pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
        kthread_stop(thread->tsk);
        kfree(thread);
 }