From: Andrew Murray <andrew.murray@arm•com>
To: maz@kernel•org
Cc: Mark Rutland <mark.rutland@arm•com>,
kvm@vger•kernel.org, Suzuki K Poulose <suzuki.poulose@arm•com>,
James Morse <james.morse@arm•com>,
linux-arm-kernel@lists•infradead.org,
kvmarm@lists•cs.columbia.edu,
Julien Thierry <julien.thierry.kdev@gmail•com>
Subject: Re: [PATCH 3/3] KVM: arm64: pmu: Reset sample period on overflow handling
Date: Mon, 7 Oct 2019 10:43:27 +0100 [thread overview]
Message-ID: <20191007094325.GX42880@e119886-lin.cambridge.arm.com> (raw)
In-Reply-To: <20191006104636.11194-4-maz@kernel.org>
On Sun, Oct 06, 2019 at 11:46:36AM +0100, maz@kernel•org wrote:
> From: Marc Zyngier <maz@kernel•org>
>
> The PMU emulation code uses the perf event sample period to trigger
> the overflow detection. This works fine for the *first* overflow
> handling
Although, even though the first overflow is timed correctly, the value
the guest reads may be wrong...
Assuming a Linux guest with the arm_pmu.c driver, if I recall correctly
this writes the -remainingperiod to the counter upon stopping/starting.
In the case of a perf_event that is pinned to a task, this will happen
upon every context switch of that task. If the counter was getting close
to overflow before the context switch, then the value written to the
guest counter will be very high and thus the sample_period written in KVM
will be very low...
The best scenario is when the host handles the overflow, the guest
handles its overflow and rewrites the guest counter (resulting in a new
host perf_event) - all before the first host perf_event fires again. This
is clearly the assumption the code makes.
Or - the host handles its overflow and kicks the guest, but the guest
doesn't respond in time, so we end up endlessly and pointlessly kicking it
for each host overflow - thus resulting in the large difference between number
of interrupts between host and guest. This isn't ideal, because when the
guest does read its counter, the value isn't correct (because it overflowed
a zillion times at a value less than the arrchitected max).
Worse still is when the sample_period is so small, the host doesn't
even keep up.
> , but results in a huge number of interrupts on the host,
> unrelated to the number of interrupts handled in the guest (a x20
> factor is pretty common for the cycle counter). On a slow system
> (such as a SW model), this can result in the guest only making
> forward progress at a glacial pace.
>
> It turns out that the clue is in the name. The sample period is
> exactly that: a period. And once the an overflow has occured,
> the following period should be the full width of the associated
> counter, instead of whatever the guest had initially programed.
>
> Reset the sample period to the architected value in the overflow
> handler, which now results in a number of host interrupts that is
> much closer to the number of interrupts in the guest.
This seems a reasonable pragmatic approach - though of course you will end
up counting slightly slower due to the host interrupt latency. But that's
better than the status quo.
It may be possible with perf to have a single-fire counter (this mitigates
against my third scenario but you still end up with a loss of precision) -
See PERF_EVENT_IOC_REFRESH.
Ideally the PERF_EVENT_IOC_REFRESH type of functionality could be updated
to reload to a different value after the first hit.
This problem also exists on arch/x86/kvm/pmu.c (though I'm not sure what
their PMU drivers do with respect to the value they write).
>
> Fixes: b02386eb7dac ("arm64: KVM: Add PMU overflow interrupt routing")
> Signed-off-by: Marc Zyngier <maz@kernel•org>
> ---
> virt/kvm/arm/pmu.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/virt/kvm/arm/pmu.c b/virt/kvm/arm/pmu.c
> index c30c3a74fc7f..3ca4761fc0f5 100644
> --- a/virt/kvm/arm/pmu.c
> +++ b/virt/kvm/arm/pmu.c
> @@ -444,6 +444,18 @@ static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
> struct kvm_pmc *pmc = perf_event->overflow_handler_context;
> struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
> int idx = pmc->idx;
> + u64 val, period;
> +
> + /* Start by resetting the sample period to the architectural limit */
> + val = kvm_pmu_get_pair_counter_value(vcpu, pmc);
> +
> + if (kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
This is correct, because in this case we *do* care about _PMCR_LC.
> + period = (-val) & GENMASK(63, 0);
> + else
> + period = (-val) & GENMASK(31, 0);
> +
> + pmc->perf_event->attr.sample_period = period;
> + pmc->perf_event->hw.sample_period = period;
I'm not sure about the above line - does direct manipulation of sample_period
work on a running perf event? As far as I can tell this is already done in the
kernel with __perf_event_period - however this also does other stuff (such as
disable and re-enable the event).
>
Thanks,
Andrew Murray
> __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx);
>
> --
> 2.20.1
>
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists•infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2019-10-07 9:43 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-06 10:46 [PATCH 0/3] KVM: arm64: Assorted PMU emulation fixes maz
2019-10-06 10:46 ` [PATCH 1/3] KVM: arm64: pmu: Fix cycle counter truncation maz
2019-10-07 8:48 ` Andrew Murray
2019-10-06 10:46 ` [PATCH 2/3] arm64: KVM: Handle PMCR_EL0.LC as RES1 on pure AArch64 systems maz
2019-10-06 10:46 ` [PATCH 3/3] KVM: arm64: pmu: Reset sample period on overflow handling maz
2019-10-07 9:43 ` Andrew Murray [this message]
2019-10-07 10:48 ` Marc Zyngier
2019-10-07 13:04 ` Andrew Murray
2019-10-07 17:17 ` Marc Zyngier
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=20191007094325.GX42880@e119886-lin.cambridge.arm.com \
--to=andrew.murray@arm$(echo .)com \
--cc=james.morse@arm$(echo .)com \
--cc=julien.thierry.kdev@gmail$(echo .)com \
--cc=kvm@vger$(echo .)kernel.org \
--cc=kvmarm@lists$(echo .)cs.columbia.edu \
--cc=linux-arm-kernel@lists$(echo .)infradead.org \
--cc=mark.rutland@arm$(echo .)com \
--cc=maz@kernel$(echo .)org \
--cc=suzuki.poulose@arm$(echo .)com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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