From: Sean Christopherson <seanjc@google.com>
To: David Matlack <dmatlack@google.com>
Cc: Marc Zyngier <maz@kernel.org>, Albert Ou <aou@eecs.berkeley.edu>,
"open list:KERNEL VIRTUAL MACHINE FOR MIPS \(KVM/mips\)"
<kvm@vger.kernel.org>, Huacai Chen <chenhuacai@kernel.org>,
"open list:KERNEL VIRTUAL MACHINE FOR MIPS \(KVM/mips\)"
<linux-mips@vger.kernel.org>,
Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
"open list:KERNEL VIRTUAL MACHINE FOR RISC-V \(KVM/riscv\)"
<kvm-riscv@lists.infradead.org>,
Paul Walmsley <paul.walmsley@sifive.com>,
Ben Gardon <bgardon@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
maciej.szmigiero@oracle.com,
"moderated list:KERNEL VIRTUAL MACHINE FOR ARM64 \(KVM/arm64\)"
<kvmarm@lists.cs.columbia.edu>, Peter Feiner <pfeiner@google.com>
Subject: Re: [PATCH v3 16/23] KVM: x86/mmu: Cache the access bits of shadowed translations
Date: Sat, 9 Apr 2022 00:02:27 +0000 [thread overview]
Message-ID: <YlDNE55k9DNf/v2+@google.com> (raw)
In-Reply-To: <20220401175554.1931568-17-dmatlack@google.com>
On Fri, Apr 01, 2022, David Matlack wrote:
> @@ -733,7 +733,7 @@ static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
> static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
> {
> if (!sp->role.direct)
> - return sp->gfns[index];
> + return sp->shadowed_translation[index].gfn;
>
> return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
> }
> @@ -741,7 +741,7 @@ static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
> static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
This should be replaced with a single helper to set the gfn+access. Under no
circumstance should _just_ the gfn change, and that will allow us to optimize
writing the entry. More below.
> {
> if (!sp->role.direct) {
> - sp->gfns[index] = gfn;
> + sp->shadowed_translation[index].gfn = gfn;
> return;
> }
>
> @@ -752,6 +752,47 @@ static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
> kvm_mmu_page_get_gfn(sp, index), gfn);
> }
>
> +static void kvm_mmu_page_set_access(struct kvm_mmu_page *sp, int index, u32 access)
> +{
> + if (!sp->role.direct) {
> + sp->shadowed_translation[index].access = access;
> + return;
> + }
> +
> + if (WARN_ON(access != sp->role.access))
> + pr_err_ratelimited("access mismatch under direct page %llx "
LOL, I realize this is not your code, but ratelimiting under a WARN ain't gonna
help much :-)
This also generates a warning and fails to compile with KVM_WERROR=y, though I
believe the test bots already reported that.
arch/x86/kvm/mmu/mmu.c: In function ‘kvm_mmu_page_set_access’:
include/linux/kern_levels.h:5:25: error: format ‘%llx’ expects argument of type ‘long long unsigned int’, but argument 3 has type ‘int’ [-Werror=format=]
5 | #define KERN_SOH "\001" /* ASCII Start Of Header */
| ^~~~~~
include/linux/printk.h:418:25: note: in definition of macro ‘printk_index_wrap’
418 | _p_func(_fmt, ##__VA_ARGS__); \
| ^~~~
include/linux/printk.h:640:17: note: in expansion of macro ‘printk’
640 | printk(fmt, ##__VA_ARGS__); \
| ^~~~~~
include/linux/printk.h:654:9: note: in expansion of macro ‘printk_ratelimited’
654 | printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
| ^~~~~~~~~~~~~~~~~~
include/linux/kern_levels.h:11:25: note: in expansion of macro ‘KERN_SOH’
11 | #define KERN_ERR KERN_SOH "3" /* error conditions */
| ^~~~~~~~
include/linux/printk.h:654:28: note: in expansion of macro ‘KERN_ERR’
654 | printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
| ^~~~~~~~
arch/x86/kvm/mmu/mmu.c:763:17: note: in expansion of macro ‘pr_err_ratelimited’
763 | pr_err_ratelimited("access mismatch under direct page %llx "
| ^~~~~~~~~~~~~~~~~~
> + "(expected %llx, got %llx)\n",
> + kvm_mmu_page_get_gfn(sp, index),
> + sp->role.access, access);
> +}
> +
> +/*
> + * For leaf SPTEs, fetch the *guest* access permissions being shadowed. Note
> + * that the SPTE itself may have a more constrained access permissions that
> + * what the guest enforces. For example, a guest may create an executable
> + * huge PTE but KVM may disallow execution to mitigate iTLB multihit.
> + */
> +static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index)
> +{
> + if (!sp->role.direct)
> + return sp->shadowed_translation[index].access;
> +
> + /*
> + * For direct MMUs (e.g. TDP or non-paging guests) there are no *guest*
> + * access permissions being shadowed. So we can just return ACC_ALL
> + * here.
> + *
> + * For indirect MMUs (shadow paging), direct shadow pages exist when KVM
> + * is shadowing a guest huge page with smaller pages, since the guest
> + * huge page is being directly mapped. In this case the guest access
> + * permissions being shadowed are the access permissions of the huge
> + * page.
> + *
> + * In both cases, sp->role.access contains exactly what we want.
> + */
> + return sp->role.access;
> +}
...
> diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h
> index b6e22ba9c654..3f76f4c1ae59 100644
> --- a/arch/x86/kvm/mmu/mmu_internal.h
> +++ b/arch/x86/kvm/mmu/mmu_internal.h
> @@ -32,6 +32,18 @@ extern bool dbg;
>
> typedef u64 __rcu *tdp_ptep_t;
>
> +/*
> + * Stores the result of the guest translation being shadowed by an SPTE. KVM
> + * shadows two types of guest translations: nGPA -> GPA (shadow EPT/NPT) and
> + * GVA -> GPA (traditional shadow paging). In both cases the result of the
> + * translation is a GPA and a set of access constraints.
> + */
> +struct shadowed_translation_entry {
> + /* Note, GFNs can have at most 64 - PAGE_SHIFT = 52 bits. */
> + u64 gfn:52;
> + u64 access:3;
A bitfield is completely unnecessary and generates bad code. As is, it generates
_really_ bad code because extracting and setting requires non-standard 64-bit value
masks, multiple operations, and accesses to unaligned data. The generated code can
be made slightly less awful by using a fully byte for access and 64 bits for GFN,
but it still sucks compared to what we can hand generate.
The other aspect of this is that retrieving the GFN is a frequent operation,
whereas the access is almost never read. I.e. we should bias for reading the GFN
above all else.
The simple and obvious thing is to not reinvent the wheel. GFN = (GPA >> PAGE_SHIFT),
and ignoring NX, access lives in the lower 12 bits of a PTE. Then reading the GFN is
a simple SHR, and reading access info is a simple AND.
We might also be able to optimize FNAME(sync_page), but I don't care much about
that, it's rarely used for nested TDP.
So, keep translation_entry a gfn_t *, then do:
static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
{
if (!sp->role.direct)
return sp->shadowed_translation[index] >> PAGE_SHIFT;
return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
}
static void kvm_mmu_page_set_translation(struct kvm_mmu_page *sp, int index,
gfn_t gfn, unsigned int access)
{
if (!sp->role.direct) {
sp->shadowed_translation[index] = (gfn << PAGE_SHIFT) | access;
return;
}
if (WARN_ON(gfn != kvm_mmu_page_get_gfn(sp, index)))
pr_err_ratelimited("gfn mismatch under direct page %llx "
"(expected %llx, got %llx)\n",
sp->gfn,
kvm_mmu_page_get_gfn(sp, index), gfn);
}
static void kvm_mmu_page_set_access(struct kvm_mmu_page *sp, int index,
unsigned int access)
{
if (sp->role.direct)
return;
sp->shadowed_translation[index] &= PAGE_MASK;
sp->shadowed_translation[index] |= access;
}
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
next prev parent reply other threads:[~2022-04-09 0:02 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-01 17:55 [PATCH v3 00/23] KVM: Extend Eager Page Splitting to the shadow MMU David Matlack
2022-04-01 17:55 ` [PATCH v3 01/23] KVM: x86/mmu: Optimize MMU page cache lookup for all direct SPs David Matlack
2022-04-01 17:55 ` [PATCH v3 02/23] KVM: x86/mmu: Use a bool for direct David Matlack
2022-04-08 22:24 ` Sean Christopherson
2022-04-01 17:55 ` [PATCH v3 03/23] KVM: x86/mmu: Derive shadow MMU page role from parent David Matlack
2022-04-01 17:55 ` [PATCH v3 04/23] KVM: x86/mmu: Decompose kvm_mmu_get_page() into separate functions David Matlack
2022-04-01 17:55 ` [PATCH v3 05/23] KVM: x86/mmu: Rename shadow MMU functions that deal with shadow pages David Matlack
2022-04-01 17:55 ` [PATCH v3 06/23] KVM: x86/mmu: Pass memslot to kvm_mmu_new_shadow_page() David Matlack
2022-04-01 17:55 ` [PATCH v3 07/23] KVM: x86/mmu: Separate shadow MMU sp allocation from initialization David Matlack
2022-04-01 17:55 ` [PATCH v3 08/23] KVM: x86/mmu: Link spt to sp during allocation David Matlack
2022-04-01 17:55 ` [PATCH v3 09/23] KVM: x86/mmu: Move huge page split sp allocation code to mmu.c David Matlack
2022-04-01 17:55 ` [PATCH v3 10/23] KVM: x86/mmu: Use common code to free kvm_mmu_page structs David Matlack
2022-04-01 17:55 ` [PATCH v3 11/23] KVM: x86/mmu: Use common code to allocate shadow pages from vCPU caches David Matlack
2022-04-01 17:55 ` [PATCH v3 12/23] KVM: x86/mmu: Pass const memslot to rmap_add() David Matlack
2022-04-01 17:55 ` [PATCH v3 13/23] KVM: x86/mmu: Pass const memslot to init_shadow_page() and descendants David Matlack
2022-04-01 17:55 ` [PATCH v3 14/23] KVM: x86/mmu: Decouple rmap_add() and link_shadow_page() from kvm_vcpu David Matlack
2022-04-01 17:55 ` [PATCH v3 15/23] KVM: x86/mmu: Update page stats in __rmap_add() David Matlack
2022-04-01 17:55 ` [PATCH v3 16/23] KVM: x86/mmu: Cache the access bits of shadowed translations David Matlack
2022-04-02 6:19 ` kernel test robot
2022-04-02 7:01 ` kernel test robot
2022-04-09 0:02 ` Sean Christopherson [this message]
2022-04-14 16:47 ` David Matlack
2022-04-01 17:55 ` [PATCH v3 17/23] KVM: x86/mmu: Extend make_huge_page_split_spte() for the shadow MMU David Matlack
2022-04-01 17:55 ` [PATCH v3 18/23] KVM: x86/mmu: Zap collapsible SPTEs at all levels in " David Matlack
2022-04-01 17:55 ` [PATCH v3 19/23] KVM: x86/mmu: Refactor drop_large_spte() David Matlack
2022-04-01 17:55 ` [PATCH v3 20/23] KVM: Allow for different capacities in kvm_mmu_memory_cache structs David Matlack
2022-04-20 10:55 ` Anup Patel
2022-04-21 16:19 ` Ben Gardon
2022-04-21 16:33 ` David Matlack
2022-04-01 17:55 ` [PATCH v3 21/23] KVM: Allow GFP flags to be passed when topping up MMU caches David Matlack
2022-04-01 17:55 ` [PATCH v3 22/23] KVM: x86/mmu: Support Eager Page Splitting in the shadow MMU David Matlack
2022-04-09 0:39 ` Sean Christopherson
2022-04-14 16:50 ` David Matlack
2022-04-01 17:55 ` [PATCH v3 23/23] KVM: selftests: Map x86_64 guest virtual memory with huge pages David Matlack
2022-04-11 17:12 ` [PATCH v3 00/23] KVM: Extend Eager Page Splitting to the shadow MMU Sean Christopherson
2022-04-11 17:54 ` David Matlack
2022-04-11 20:12 ` Sean Christopherson
2022-04-11 23:41 ` David Matlack
2022-04-12 0:39 ` Sean Christopherson
2022-04-12 16:49 ` David Matlack
2022-04-13 1:02 ` Sean Christopherson
2022-04-13 17:57 ` David Matlack
2022-04-13 18:28 ` Sean Christopherson
2022-04-13 21:22 ` David Matlack
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=YlDNE55k9DNf/v2+@google.com \
--to=seanjc@google.com \
--cc=aleksandar.qemu.devel@gmail.com \
--cc=aou@eecs.berkeley.edu \
--cc=bgardon@google.com \
--cc=chenhuacai@kernel.org \
--cc=dmatlack@google.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=linux-mips@vger.kernel.org \
--cc=maciej.szmigiero@oracle.com \
--cc=maz@kernel.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=pbonzini@redhat.com \
--cc=pfeiner@google.com \
/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).