Skip to content

Commit 26c7295

Browse files
Liang ChenPeter Zijlstra
authored andcommitted
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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Steven Rostedt (VMware) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent c32b430 commit 26c7295

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

kernel/kthread.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,15 @@ static void __kthread_parkme(struct kthread *self)
199199
if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
200200
break;
201201

202+
/*
203+
* Thread is going to call schedule(), do not preempt it,
204+
* or the caller of kthread_park() may spend more time in
205+
* wait_task_inactive().
206+
*/
207+
preempt_disable();
202208
complete(&self->parked);
203-
schedule();
209+
schedule_preempt_disabled();
210+
preempt_enable();
204211
}
205212
__set_current_state(TASK_RUNNING);
206213
}
@@ -245,8 +252,14 @@ static int kthread(void *_create)
245252
/* OK, tell user we're spawned, wait for stop or wakeup */
246253
__set_current_state(TASK_UNINTERRUPTIBLE);
247254
create->result = current;
255+
/*
256+
* Thread is going to call schedule(), do not preempt it,
257+
* or the creator may spend more time in wait_task_inactive().
258+
*/
259+
preempt_disable();
248260
complete(done);
249-
schedule();
261+
schedule_preempt_disabled();
262+
preempt_enable();
250263

251264
ret = -EINTR;
252265
if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {

0 commit comments

Comments
 (0)