linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Turquette <mturquette@baylibre.com>
To: Steve Muckle <steve.muckle@linaro.org>,
	"Rafael J. Wysocki" <rjw@rjwysocki.net>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Morten Rasmussen" <morten.rasmussen@arm.com>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Juri Lelli" <Juri.Lelli@arm.com>,
	"Patrick Bellasi" <patrick.bellasi@arm.com>,
	"Ricky Liang" <jcliang@chromium.org>
Subject: Re: [RFCv7 PATCH 03/10] sched: scheduler-driven cpu frequency selection
Date: Tue, 01 Mar 2016 23:49:10 -0800	[thread overview]
Message-ID: <20160302074910.10178.35029@quark.deferred.io> (raw)
In-Reply-To: <56CF9D8F.7010607@linaro.org>

Hi,

I'm still catching up on the plurality of scheduler/cpufreq threads but
I thought I would chime in with some historical reasons for why
cpufreq_sched.c looks the way it does today.

Quoting Steve Muckle (2016-02-25 16:34:23)
> On 02/24/2016 07:55 PM, Rafael J. Wysocki wrote:
> > On Monday, February 22, 2016 05:22:43 PM Steve Muckle wrote:
> > Besides, governors traditionally go to drivers/cpufreq.  Why is this different?
> 
> This predates my involvement but I'm guessing this was done because the
> long term vision was for cpufreq to eventually be removed from the
> picture. But it may be quite some time before that happens - I think
> communicating directly with drivers may be difficult due to the need to
> synchronize with thermal or userspace min/max requests etc, plus there's
> the fact that we've got a longstanding API exposed to userspace.
> 
> I'm happy to move this to drivers/cpufreq.

Originally it was put in kernel/sched/ because cpufreq_sched.c needed
access to kernel/sched/sched.h. Rafael's implementation flips the
push-pull relationship between the governor and scheduler so that this
is not necessary. If that requirement is removed from Steve's series
then there is no need to put the file outside of drivers/cpufreq.

...
> > One thing I personally like in the RCU-based approach is its universality.  The
> > callbacks may be installed by different entities in a uniform way: intel_pstate
> > can do that, the old governors can do that, my experimental schedutil code can
> > do that and your code could have done that too in principle.  And this is very
> > nice, because it is a common underlying mechanism that can be used by everybody
> > regardless of their particular implementations on the other side.
> > 
> > Why would I want to use something different, then?
> 
> I've got nothing against a callback registration mechanism. As you
> mentioned in another mail it could itself use static keys, enabling the
> static key when a callback is registered and disabling it otherwise to
> avoid calling into cpufreq_update_util().

I'm very happy to see the return of capacity/util ops. I had these in my
initial prototype back in 2014 but Peter shot it down, partially due to
the performance hit of indirect function call overhead in the fast path:

http://permalink.gmane.org/gmane.linux.kernel/1803410

...
> >> +
> >> +/*
> >> + * Capacity margin added to CFS and RT capacity requests to provide
> >> + * some head room if task utilization further increases.
> >> + */
> > 
> > OK, where does this number come from?
> 
> Someone's posterior :) .

Indeed, this margin is based on a posterior-derived value, in particular
the 25% imbalance_pct in struct sched_domain:

include/linux/sched.h:
struct sched_domain {
        /* These fields must be setup */
        ...
        unsigned int imbalance_pct;     /* No balance until over watermark */
...

and,

kernel/sched/core.c:
static struct sched_domain *
sd_init(struct sched_domain_topology_level *tl, int cpu)
{
        struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
        ...
        sd = (struct sched_domain){
                .min_interval           = sd_weight,
                .max_interval           = 2*sd_weight,
                .busy_factor            = 32,
                .imbalance_pct          = 125,
...

> 
> This really should be a tunable IMO, but there's a fairly strong
> anti-tunable sentiment, so it's been left hard-coded in an attempt to
> provide something that "just works."

It should definitely be tunable.

> 
> At the least I can add a comment saying that the 20% idle headroom
> requirement was an off the cuff estimate and that at this time, we don't
> have significant data to suggest it's the best number.

Yeah, it was just for prototyping.

...
> > Now, what really is the advantage of having those extra threads vs using
> > workqueues?

More historical anecdotes: The RT/DL stuff below is absolutely worth
talking about, but if your question is, "why did the design choose
kthreads over wq's", the answer is legacy.

Originally back in 2014 Morten played with queuing work up via wq within
schedule() context, but hit a problem where that caused re-entering
schedule() from schedule() and entering reentrancy hell and entering
reentrancy hell and entering reentrancy hell and entering reentrancy
hell ... well you get the idea.

I came up with an ugly workaround to wake up a dormant kthread with
wake_up_process() which was called from an arch-specific callback to
change cpu frequency and later Juri suggested to use irq_work to kick
the kthread, which is still the method used in Steve's patch series.

Seems that it is fine to toss work onto a workqueue from irq_work
context however, which is nice, except that the RT thread approach
yields better latency. I'll let you guys sort out the issues around
RT/DL starvation, which seems like a real issue.

> > 
> > I guess the underlying concern is that RT tasks may stall workqueues indefinitely
> > in theory and then the frequency won't be updated, but there's much more kernel
> > stuff run from workqueues and if that is starved, you won't get very far anyway.
> > 
> > If you take special measures to prevent frequency change requests from being
> > stalled by RT tasks, question is why are they so special?  Aren't there any
> > other kernel activities that also should be protected from that and may be
> > more important than CPU frequency changes?
> 
> I think updating the CPU frequency during periods of heavy RT/DL load is
> one of the most (if not the most) important things. I can't speak for
> other system activities that may get blocked, but there's an opportunity
> to protect CPU frequency changes here, and it seems worth taking to me.
> 
> > 
> > Plus if this really is the problem here, then it also affects the other cpufreq
> > governors, so maybe it should be solved for everybody in some common way?
> 
> Agreed, I'd think a freq change thread that serves frequency change
> requests would be a useful common component. The locking and throttling
> (slowpath_lock, finish_last_request()) are somewhat specific to this
> implementation, but could probably be done generically and maybe even
> used in other governors. If you're okay with it though I'd like to view
> that as a slightly longer term effort, as I think it would get unwieldy
> trying to do that as part of this initial change.

I do not have any data to back up a case for stalls caused by RT/DL
starvation, but conceptually I would say that latency is fundamentally
more important in a scheduler-driven cpu frequency selection scenario,
versus the legacy timer-based governors.

In the latter case we get woken up by a timer (prior to Rafael's recent
"cpufreq: governor: Replace timers with utilization update callbacks"
patch), we sample idleness/busyness, and change frequency, all in one go
and all from process context.

In the case of the scheduler selecting frequency in the hot path, with
hardware that takes a long time to transition frequency (and thus must
be done in a slow path), we want to minimize the time delta between the
scheduler picking a frequency and the thread that executes that change
actually being run.

In my over-simplified view of the scheduler, it would be great if we
could have a backdoor mechanism to place the frequency transition
kthread onto a runqueue from within the schedule() context and dispense
with the irq_work stuff in Steve's series altogether.

Regards,
Mike

  parent reply	other threads:[~2016-03-02  7:49 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-23  1:22 [RFCv7 PATCH 00/10] sched: scheduler-driven CPU frequency selection Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 01/10] sched: Compute cpu capacity available at current frequency Steve Muckle
2016-02-23  1:41   ` Rafael J. Wysocki
2016-02-23  9:19     ` Peter Zijlstra
2016-02-26  1:37       ` Rafael J. Wysocki
2016-02-26  9:14         ` Peter Zijlstra
2016-02-23  1:22 ` [RFCv7 PATCH 02/10] cpufreq: introduce cpufreq_driver_is_slow Steve Muckle
2016-02-23  1:31   ` Rafael J. Wysocki
2016-02-26  0:50     ` Michael Turquette
2016-02-26  1:07       ` Steve Muckle
2016-02-26  1:16       ` Rafael J. Wysocki
     [not found]         ` <20160226185503.2278.20479@quark.deferred.io>
2016-02-26 21:00           ` Rafael J. Wysocki
2016-02-23  1:22 ` [RFCv7 PATCH 03/10] sched: scheduler-driven cpu frequency selection Steve Muckle
2016-02-25  3:55   ` Rafael J. Wysocki
2016-02-25  9:21     ` Peter Zijlstra
2016-02-25 21:04       ` Rafael J. Wysocki
2016-02-25  9:28     ` Peter Zijlstra
2016-02-25 21:08       ` Rafael J. Wysocki
2016-02-26  9:18         ` Peter Zijlstra
2016-02-27  0:08           ` Rafael J. Wysocki
2016-03-01 12:57             ` Peter Zijlstra
2016-03-01 19:44               ` Rafael J. Wysocki
2016-02-25 11:04     ` Rafael J. Wysocki
2016-02-26  0:34     ` Steve Muckle
2016-02-27  2:39       ` Rafael J. Wysocki
2016-02-27  4:17         ` Steve Muckle
2016-02-28  2:26           ` Rafael J. Wysocki
2016-03-01 14:31             ` Peter Zijlstra
2016-03-01 20:32               ` Rafael J. Wysocki
2016-03-01 13:26           ` Peter Zijlstra
2016-03-01 13:17       ` Peter Zijlstra
2016-03-02  7:49       ` Michael Turquette [this message]
2016-03-03  2:49         ` Rafael J. Wysocki
2016-03-03  3:50           ` Steve Muckle
2016-03-03  9:34             ` Juri Lelli
2016-03-03 13:03         ` Peter Zijlstra
2016-03-03 14:21   ` Ingo Molnar
2016-02-23  1:22 ` [RFCv7 PATCH 04/10] sched/fair: add triggers for OPP change requests Steve Muckle
2016-03-01  6:51   ` Ricky Liang
2016-03-03  3:55     ` Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 05/10] sched/{core,fair}: trigger OPP change request on fork() Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 06/10] sched/fair: cpufreq_sched triggers for load balancing Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 07/10] sched/fair: jump to max OPP when crossing UP threshold Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 08/10] sched: remove call of sched_avg_update from sched_rt_avg_update Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 09/10] sched/deadline: split rt_avg in 2 distincts metrics Steve Muckle
2016-02-23  1:22 ` [RFCv7 PATCH 10/10] sched: rt scheduler sets capacity requirement Steve Muckle
2016-02-23  1:33 ` [RFCv7 PATCH 00/10] sched: scheduler-driven CPU frequency selection Steve Muckle
2016-03-30  0:45 ` Yuyang Du
2016-03-31  1:35   ` Steve Muckle
2016-03-30 20:22     ` Yuyang Du

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160302074910.10178.35029@quark.deferred.io \
    --to=mturquette@baylibre.com \
    --cc=Juri.Lelli@arm.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jcliang@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=morten.rasmussen@arm.com \
    --cc=patrick.bellasi@arm.com \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=rjw@rjwysocki.net \
    --cc=steve.muckle@linaro.org \
    --cc=vincent.guittot@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).