mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	sched/wait: Fix a kthread race with wait_woken()
There is a race between kthread_stop() and the new wait_woken() that
can result in a lack of progress.
CPU 0                                    | CPU 1
                                         |
rfcomm_run()                             | kthread_stop()
  ...                                    |
  if (!test_bit(KTHREAD_SHOULD_STOP))    |
                                         |   set_bit(KTHREAD_SHOULD_STOP)
                                         |   wake_up_process()
    wait_woken()                         |   wait_for_completion()
      set_current_state(INTERRUPTIBLE)   |
      if (!WQ_FLAG_WOKEN)                |
        schedule_timeout()               |
                                         |
After which both tasks will wait.. forever.
Fix this by having wait_woken() check for kthread_should_stop() but
only for kthreads (obviously).
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
			
			
This commit is contained in:
		
							parent
							
								
									3427445afd
								
							
						
					
					
						commit
						cb6538e740
					
				
					 1 changed files with 6 additions and 1 deletions
				
			
		| 
						 | 
					@ -9,6 +9,7 @@
 | 
				
			||||||
#include <linux/mm.h>
 | 
					#include <linux/mm.h>
 | 
				
			||||||
#include <linux/wait.h>
 | 
					#include <linux/wait.h>
 | 
				
			||||||
#include <linux/hash.h>
 | 
					#include <linux/hash.h>
 | 
				
			||||||
 | 
					#include <linux/kthread.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *key)
 | 
					void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *key)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -297,6 +298,10 @@ int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL(autoremove_wake_function);
 | 
					EXPORT_SYMBOL(autoremove_wake_function);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline bool is_kthread_should_stop(void)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return (current->flags & PF_KTHREAD) && kthread_should_stop();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * DEFINE_WAIT_FUNC(wait, woken_wake_func);
 | 
					 * DEFINE_WAIT_FUNC(wait, woken_wake_func);
 | 
				
			||||||
| 
						 | 
					@ -326,7 +331,7 @@ long wait_woken(wait_queue_t *wait, unsigned mode, long timeout)
 | 
				
			||||||
	 * woken_wake_function() such that if we observe WQ_FLAG_WOKEN we must
 | 
						 * woken_wake_function() such that if we observe WQ_FLAG_WOKEN we must
 | 
				
			||||||
	 * also observe all state before the wakeup.
 | 
						 * also observe all state before the wakeup.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	if (!(wait->flags & WQ_FLAG_WOKEN))
 | 
						if (!(wait->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
 | 
				
			||||||
		timeout = schedule_timeout(timeout);
 | 
							timeout = schedule_timeout(timeout);
 | 
				
			||||||
	__set_current_state(TASK_RUNNING);
 | 
						__set_current_state(TASK_RUNNING);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue