From: Will Deacon <will@kernel.org>
To: Gabriel Krisman Bertazi <krisman@suse.de>
Cc: catalin.marinas@arm.com, linux-arm-kernel@lists.infradead.org,
broonie@kernel.org
Subject: Re: [RFC PATCH] arm64: Cache HW update of Access Flag support
Date: Fri, 6 Jan 2023 17:09:50 +0000 [thread overview]
Message-ID: <20230106170949.GA5019@willie-the-truck> (raw)
In-Reply-To: <20230106163825.4812-1-krisman@suse.de>
On Fri, Jan 06, 2023 at 01:38:25PM -0300, Gabriel Krisman Bertazi wrote:
> Accessing AA64MMFR1_EL1 is expensive in KVM guests, since it is emulated
> in the hypervisor. In fact, ARM documentation mentions some feature
> registers are not supposed to be accessed frequently by the OS, and
> therefore should be emulated for guests [1].
>
> Commit 0388f9c74330 ("arm64: mm: Implement
> arch_wants_old_prefaulted_pte()") introduced a read of this register in
> the page fault path. But, even when the feature of setting faultaround
> pages with the old flag is disabled for a given cpu, we are still paying
> the cost of checking the register on every pagefault. This results in an
> explosion of vmexit events in KVM guests, which directly impacts the
> performance of virtualized workloads. For instance, running kernbench
> yields a 15% increase in system time solely due to the increased vmexit
> cycles.
>
> This patch avoids the extra cost by caching the register value after the
> first read. It should be safe to do so, since this register mustn't
> change for a specific processor.
>
> As a side note, we can't rely on the usual cpucap for this bit, because
> for some reason that I'm missing, it is always enabled for aarch64, even if
> the functionality is not supported by the processor.
>
> [1] https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Armv8-A%20virtualization.pdf?revision=a765a7df-1a00-434d-b241-357bfda2dd31
>
> Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
> ---
> arch/arm64/include/asm/cpufeature.h | 16 +++++++++++++---
> 1 file changed, 13 insertions(+), 3 deletions(-)
>
> diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h
> index 03d1c9d7af82..599c7a22155b 100644
> --- a/arch/arm64/include/asm/cpufeature.h
> +++ b/arch/arm64/include/asm/cpufeature.h
> @@ -859,14 +859,24 @@ static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange)
> /* Check whether hardware update of the Access flag is supported */
> static inline bool cpu_has_hw_af(void)
> {
> - u64 mmfr1;
> + static unsigned int has_af = -1U;
>
> if (!IS_ENABLED(CONFIG_ARM64_HW_AFDBM))
> return false;
>
> - mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
> - return cpuid_feature_extract_unsigned_field(mmfr1,
> + /*
> + * Cache the register to avoid repeatedly reading it,
> + * which is an emulated operation on KVM guests.
> + *
> + * Also do not rely on cpucap, since this bit is always set
> + * there, regardless of actual status. See has_hw_dbm() for
> + * details.
> + */
> + if (unlikely(has_af == -1U))
> + has_af = cpuid_feature_extract_unsigned_field(
> + read_cpuid(ID_AA64MMFR1_EL1),
> ID_AA64MMFR1_EL1_HAFDBS_SHIFT);
> + return has_af;
The intention here was to read the value for the _current_ CPU, since it
might not be the same across the system when big.MISTAKE gets involved.
However, since this is really just a performance optimisation and I
think that the access flag tends to be uniformly supported in practice
anyway, the best bet is probably just to read the sanitised version of
the field using read_sanitised_ftr_reg().
Can you give that a shot, please?
Will
_______________________________________________
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:[~2023-01-06 17:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-06 16:38 [RFC PATCH] arm64: Cache HW update of Access Flag support Gabriel Krisman Bertazi
2023-01-06 17:09 ` Will Deacon [this message]
2023-01-09 15:16 ` Gabriel Krisman Bertazi
2023-01-10 16:16 ` Will Deacon
2023-01-10 19:38 ` Gabriel Krisman Bertazi
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=20230106170949.GA5019@willie-the-truck \
--to=will@kernel.org \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=krisman@suse.de \
--cc=linux-arm-kernel@lists.infradead.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