mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	workqueue: UNBOUND -> REBIND morphing in rebind_workers() should be atomic
The compiler may compile the following code into TWO write/modify
instructions.
	worker->flags &= ~WORKER_UNBOUND;
	worker->flags |= WORKER_REBIND;
so the other CPU may temporarily see worker->flags which doesn't have
either WORKER_UNBOUND or WORKER_REBIND set and perform local wakeup
prematurely.
Fix it by using single explicit assignment via ACCESS_ONCE().
Because idle workers have another WORKER_NOT_RUNNING flag, this bug
doesn't exist for them; however, update it to use the same pattern for
consistency.
tj: Applied the change to idle workers too and updated comments and
    patch description a bit.
Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: stable@vger.kernel.org
			
			
This commit is contained in:
		
							parent
							
								
									0d7614f09c
								
							
						
					
					
						commit
						96e65306b8
					
				
					 1 changed files with 11 additions and 6 deletions
				
			
		| 
						 | 
					@ -1396,12 +1396,15 @@ static void rebind_workers(struct global_cwq *gcwq)
 | 
				
			||||||
	/* set REBIND and kick idle ones, we'll wait for these later */
 | 
						/* set REBIND and kick idle ones, we'll wait for these later */
 | 
				
			||||||
	for_each_worker_pool(pool, gcwq) {
 | 
						for_each_worker_pool(pool, gcwq) {
 | 
				
			||||||
		list_for_each_entry(worker, &pool->idle_list, entry) {
 | 
							list_for_each_entry(worker, &pool->idle_list, entry) {
 | 
				
			||||||
 | 
								unsigned long worker_flags = worker->flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (worker->flags & WORKER_REBIND)
 | 
								if (worker->flags & WORKER_REBIND)
 | 
				
			||||||
				continue;
 | 
									continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			/* morph UNBOUND to REBIND */
 | 
								/* morph UNBOUND to REBIND atomically */
 | 
				
			||||||
			worker->flags &= ~WORKER_UNBOUND;
 | 
								worker_flags &= ~WORKER_UNBOUND;
 | 
				
			||||||
			worker->flags |= WORKER_REBIND;
 | 
								worker_flags |= WORKER_REBIND;
 | 
				
			||||||
 | 
								ACCESS_ONCE(worker->flags) = worker_flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			idle_rebind.cnt++;
 | 
								idle_rebind.cnt++;
 | 
				
			||||||
			worker->idle_rebind = &idle_rebind;
 | 
								worker->idle_rebind = &idle_rebind;
 | 
				
			||||||
| 
						 | 
					@ -1434,10 +1437,12 @@ static void rebind_workers(struct global_cwq *gcwq)
 | 
				
			||||||
	/* rebind busy workers */
 | 
						/* rebind busy workers */
 | 
				
			||||||
	for_each_busy_worker(worker, i, pos, gcwq) {
 | 
						for_each_busy_worker(worker, i, pos, gcwq) {
 | 
				
			||||||
		struct work_struct *rebind_work = &worker->rebind_work;
 | 
							struct work_struct *rebind_work = &worker->rebind_work;
 | 
				
			||||||
 | 
							unsigned long worker_flags = worker->flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		/* morph UNBOUND to REBIND */
 | 
							/* morph UNBOUND to REBIND atomically */
 | 
				
			||||||
		worker->flags &= ~WORKER_UNBOUND;
 | 
							worker_flags &= ~WORKER_UNBOUND;
 | 
				
			||||||
		worker->flags |= WORKER_REBIND;
 | 
							worker_flags |= WORKER_REBIND;
 | 
				
			||||||
 | 
							ACCESS_ONCE(worker->flags) = worker_flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
 | 
							if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
 | 
				
			||||||
				     work_data_bits(rebind_work)))
 | 
									     work_data_bits(rebind_work)))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue