Re: [PATCH v1 1/1] sched/fair: do not preempt current task if it is going to call schedule()

From: Peter Zijlstra
Date: Thu Mar 05 2020 - 04:59:47 EST


On Thu, Mar 05, 2020 at 04:16:11PM +0800, cl@xxxxxxxxxxxxxx wrote:
> From: Liang Chen <cl@xxxxxxxxxxxxxx>
>
> 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.

> diff --git a/kernel/kthread.c b/kernel/kthread.c
> index b262f47046ca..8a4e4c9cdc22 100644
> --- a/kernel/kthread.c
> +++ b/kernel/kthread.c
> @@ -199,8 +199,10 @@ static void __kthread_parkme(struct kthread *self)
> if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
> break;
>
> + set_tsk_going_to_sched(current);
> complete(&self->parked);
> schedule();
> + clear_tsk_going_to_sched(current);
> }
> __set_current_state(TASK_RUNNING);
> }
> @@ -245,8 +247,10 @@ static int kthread(void *_create)
> /* OK, tell user we're spawned, wait for stop or wakeup */
> __set_current_state(TASK_UNINTERRUPTIBLE);
> create->result = current;
> + set_tsk_going_to_sched(current);
> complete(done);
> schedule();
> + clear_tsk_going_to_sched(current);
>
> ret = -EINTR;
> if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {

Were you looking for this? I think it does the same without having
fallen from the ugly tree...

diff --git a/kernel/kthread.c b/kernel/kthread.c
index b262f47046ca..62699ff414f4 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -199,8 +199,10 @@ static void __kthread_parkme(struct kthread *self)
if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags))
break;

+ preempt_disable()
complete(&self->parked);
- schedule();
+ schedule_preempt_disabled();
+ preempt_enable();
}
__set_current_state(TASK_RUNNING);
}
@@ -245,8 +247,10 @@ static int kthread(void *_create)
/* OK, tell user we're spawned, wait for stop or wakeup */
__set_current_state(TASK_UNINTERRUPTIBLE);
create->result = current;
+ preempt_disable()
complete(done);
- schedule();
+ schedule_preempt_disabled();
+ preempt_enable();

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