Re: [PATCH v2] sched/core: Preempt current task in favour of bound kthread

Srikar Dronamraju
2019-12-10T10:16:37Z

* Peter Zijlstra <peterz@infradead.org> [2019-12-10 10:26:01]:

> On Tue, Dec 10, 2019 at 11:13:30AM +0530, Srikar Dronamraju wrote:
> > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > index 44123b4d14e8..82126cbf62cd 100644
> > --- a/kernel/sched/core.c
> > +++ b/kernel/sched/core.c
> > @@ -2664,7 +2664,12 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
> > */
> > int wake_up_process(struct task_struct *p)
> > {
> > - return try_to_wake_up(p, TASK_NORMAL, 0);
> > + int wake_flags = 0;
> > +
> > + if (is_per_cpu_kthread(p))
> > + wake_flags = WF_KTHREAD;
> > +
> > + return try_to_wake_up(p, TASK_NORMAL, wake_flags);
> > }
> > EXPORT_SYMBOL(wake_up_process);
>
> Why wake_up_process() and not try_to_wake_up() ? This way
> wake_up_state(.state = TASK_NORMAL() is no longer the same as
> wake_up_process(), and that's weird!
>

Thanks Vincent and Peter for your review comments.

I was trying to be more conservative. But I don't see any reason why we
can't do the same at try_to_wake_up. And I mostly thought the kthreads were
using wake_up_process.

So I shall move the check to try_to_wake_up then.

> > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> > index 69a81a5709ff..36486f71e59f 100644
> > --- a/kernel/sched/fair.c
> > +++ b/kernel/sched/fair.c
> > @@ -6660,6 +6660,27 @@ static void set_skip_buddy(struct sched_entity *se)
> > cfs_rq_of(se)->skip = se;
> > }
> >
> > +static int kthread_wakeup_preempt(struct rq *rq, struct task_struct *p, int wake_flags)
> > +{
> > + struct task_struct *curr = rq->curr;
> > + struct cfs_rq *cfs_rq = task_cfs_rq(curr);
> > +
> > + if (!(wake_flags & WF_KTHREAD))
> > + return 0;
> > +
> > + if (p->nr_cpus_allowed != 1 || curr->nr_cpus_allowed == 1)
> > + return 0;
>
> Per the above, WF_KTHREAD already implies p->nr_cpus_allowed == 1.

Yes, this is redundant.

>
> > + if (cfs_rq->nr_running > 2)
> > + return 0;
> > +
> > + /*
> > + * Don't preempt, if the waking kthread is more CPU intensive than
> > + * the current thread.
> > + */
> > + return p->nvcsw * curr->nivcsw >= p->nivcsw * curr->nvcsw;
>
> Both these conditions seem somewhat arbitrary. The number of context
> switch does not correspond to CPU usage _at_all_.
>
> vtime OTOH does reflect exactly that, if it runs a lot, it will be ahead
> in the tree.
>

Right, my rational was to not allow a runaway kthread to preempt and take
control.

--
Thanks and Regards
Srikar Dronamraju