• Alexandre Chartre's avatar
    KVM: arm64: Disabling disabled PMU counters wastes a lot of time · ca4f202d
    Alexandre Chartre authored
    
    
    In a KVM guest on arm64, performance counters interrupts have an
    unnecessary overhead which slows down execution when using the "perf
    record" command and limits the "perf record" sampling period.
    
    The problem is that when a guest VM disables counters by clearing the
    PMCR_EL0.E bit (bit 0), KVM will disable all counters defined in
    PMCR_EL0 even if they are not enabled in PMCNTENSET_EL0.
    
    KVM disables a counter by calling into the perf framework, in particular
    by calling perf_event_create_kernel_counter() which is a time consuming
    operation. So, for example, with a Neoverse N1 CPU core which has 6 event
    counters and one cycle counter, KVM will always disable all 7 counters
    even if only one is enabled.
    
    This typically happens when using the "perf record" command in a guest
    VM: perf will disable all event counters with PMCNTENTSET_EL0 and only
    uses the cycle counter. And when using the "perf record" -F option with
    a high profiling frequency, the overhead of KVM disabling all counters
    instead of one on every counter interrupt becomes very noticeable.
    
    The problem is fixed by having KVM disable only counters which are
    enabled in PMCNTENSET_EL0. If a counter is not enabled in PMCNTENSET_EL0
    then KVM will not enable it when setting PMCR_EL0.E and it will remain
    disabled as long as it is not enabled in PMCNTENSET_EL0. So there is
    effectively no need to disable a counter when clearing PMCR_EL0.E if it
    is not enabled PMCNTENSET_EL0.
    Acked-by: default avatarRussell King (Oracle) <rmk+kernel@armlinux.org.uk>
    Reviewed-by: default avatarAlexandru Elisei <alexandru.elisei@arm.com>
    Signed-off-by: default avatarAlexandre Chartre <alexandre.chartre@oracle.com>
    [maz: moved 'mask' close to the actual user, simplifying the patch]
    Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
    Link: https://lore.kernel.org/r/20210712170345.660272-1-alexandre.chartre@oracle.com
    Link: https://lore.kernel.org/r/20210719123902.1493805-4-maz@kernel.org
    ca4f202d
pmu-emul.c 26 KB