mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	kthread: Do not preempt current task if it is going to call schedule()
when we create a kthread with ktrhead_create_on_cpu(),the child thread
entry is ktread.c:ktrhead() which will be preempted by the parent after
call complete(done) while schedule() is not called yet,then the parent
will call wait_task_inactive(child) but the child is still on the runqueue,
so the parent will schedule_hrtimeout() for 1 jiffy,it will waste a lot of
time,especially on startup.
  parent                             child
ktrhead_create_on_cpu()
  wait_fo_completion(&done) -----> ktread.c:ktrhead()
                             |----- complete(done);--wakeup and preempted by parent
 kthread_bind() <------------|  |-> schedule();--dequeue here
  wait_task_inactive(child)     |
   schedule_hrtimeout(1 jiffy) -|
So we hope the child just wakeup parent but not preempted by parent, and the
child is going to call schedule() soon,then the parent will not call
schedule_hrtimeout(1 jiffy) as the child is already dequeue.
The same issue for ktrhead_park()&&kthread_parkme().
This patch can save 120ms on rk312x startup with CONFIG_HZ=300.
Signed-off-by: Liang Chen <cl@rock-chips.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Link: https://lkml.kernel.org/r/20200306070133.18335-2-cl@rock-chips.com
			
			
This commit is contained in:
		
							parent
							
								
									c32b430829
								
							
						
					
					
						commit
						26c7295be0
					
				
					 1 changed files with 15 additions and 2 deletions
				
			
		| 
						 | 
					@ -199,8 +199,15 @@ static void __kthread_parkme(struct kthread *self)
 | 
				
			||||||
		if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
 | 
							if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/*
 | 
				
			||||||
 | 
							 * Thread is going to call schedule(), do not preempt it,
 | 
				
			||||||
 | 
							 * or the caller of kthread_park() may spend more time in
 | 
				
			||||||
 | 
							 * wait_task_inactive().
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
 | 
							preempt_disable();
 | 
				
			||||||
		complete(&self->parked);
 | 
							complete(&self->parked);
 | 
				
			||||||
		schedule();
 | 
							schedule_preempt_disabled();
 | 
				
			||||||
 | 
							preempt_enable();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	__set_current_state(TASK_RUNNING);
 | 
						__set_current_state(TASK_RUNNING);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -245,8 +252,14 @@ static int kthread(void *_create)
 | 
				
			||||||
	/* OK, tell user we're spawned, wait for stop or wakeup */
 | 
						/* OK, tell user we're spawned, wait for stop or wakeup */
 | 
				
			||||||
	__set_current_state(TASK_UNINTERRUPTIBLE);
 | 
						__set_current_state(TASK_UNINTERRUPTIBLE);
 | 
				
			||||||
	create->result = current;
 | 
						create->result = current;
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Thread is going to call schedule(), do not preempt it,
 | 
				
			||||||
 | 
						 * or the creator may spend more time in wait_task_inactive().
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						preempt_disable();
 | 
				
			||||||
	complete(done);
 | 
						complete(done);
 | 
				
			||||||
	schedule();
 | 
						schedule_preempt_disabled();
 | 
				
			||||||
 | 
						preempt_enable();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = -EINTR;
 | 
						ret = -EINTR;
 | 
				
			||||||
	if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
 | 
						if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue