KVM: arm64: Non-protected guest stage-2 support for pKVM
v2 cover KVM: arm64: Non-protected guest stage-2 support for pKVM
Hi all,
This is the v2 of the series adding support for non-protected guests to pKVM. Please refer to v1 for all the context:
https://lore.kernel.org/kvmarm/[email protected]/
The series is organized as follows:
- Patches 01 to 04 move the host ownership state tracking from the host's stage-2 page-table to the hypervisor's vmemmap. This avoids fragmenting the host stage-2 for shared pages, which is only needed to store an annotation in the SW bits of the corresponding PTE. All pages mapped into non-protected guests are shared from pKVM's PoV, so the cost of stage-2 fragmentation will increase massively as we start tracking that at EL2. Note that these patches also help with the existing sharing for e.g. FF-A, so they could possibly be merged separately from the rest of the series.
- Patches 05 to 07 implement a minor refactoring of the pgtable code to ease the integration of the pKVM MMU later on.
- Patches 08 to 16 introduce all the infrastructure needed on the pKVM side for handling guest stage-2 page-tables at EL2.
- Patches 17 and 18 plumb the newly introduced pKVM support into KVM/arm64.
Patches based on 6.13-rc1, tested on Pixel 6 and Qemu.
Changes in v2:
- Rebased on 6.13-rc1 (small conflicts with 2362506f7cff ("KVM: arm64:
Don't mark "struct page" accessed when making SPTE young") in particular)
- Fixed kerneldoc breakage for __unmap_stage2_range()
- Fixed pkvm_pgtable_test_clear_young() to use correct HVC
- Folded guest_get_valid_pte() into __check_host_unshare_guest() for clarity
Thanks,
Quentin
Marc Zyngier (1):
KVM: arm64: Introduce __pkvm_vcpu_{load,put}()
Quentin Perret (17):
KVM: arm64: Change the layout of enum pkvm_page_state
KVM: arm64: Move enum pkvm_page_state to memory.h
KVM: arm64: Make hyp_page::order a u8
KVM: arm64: Move host page ownership tracking to the hyp vmemmap
KVM: arm64: Pass walk flags to kvm_pgtable_stage2_mkyoung
KVM: arm64: Pass walk flags to kvm_pgtable_stage2_relax_perms
KVM: arm64: Make kvm_pgtable_stage2_init() a static inline function
KVM: arm64: Add {get,put}_pkvm_hyp_vm() helpers
KVM: arm64: Introduce __pkvm_host_share_guest()
KVM: arm64: Introduce __pkvm_host_unshare_guest()
KVM: arm64: Introduce __pkvm_host_relax_guest_perms()
KVM: arm64: Introduce __pkvm_host_wrprotect_guest()
KVM: arm64: Introduce __pkvm_host_test_clear_young_guest()
KVM: arm64: Introduce __pkvm_host_mkyoung_guest()
KVM: arm64: Introduce __pkvm_tlb_flush_vmid()
KVM: arm64: Introduce the EL1 pKVM MMU
KVM: arm64: Plumb the pKVM MMU in KVM
arch/arm64/include/asm/kvm_asm.h | 9 + arch/arm64/include/asm/kvm_host.h | 4 + arch/arm64/include/asm/kvm_pgtable.h | 42 ++- arch/arm64/include/asm/kvm_pkvm.h | 28 ++ arch/arm64/kvm/arm.c | 23 +- arch/arm64/kvm/hyp/include/nvhe/gfp.h | 6 +- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 38 +-- arch/arm64/kvm/hyp/include/nvhe/memory.h | 43 ++- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 15 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 210 +++++++++++- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 312 ++++++++++++++++-- arch/arm64/kvm/hyp/nvhe/page_alloc.c | 14 +- arch/arm64/kvm/hyp/nvhe/pkvm.c | 56 ++++ arch/arm64/kvm/hyp/nvhe/setup.c | 7 +- arch/arm64/kvm/hyp/pgtable.c | 13 +- arch/arm64/kvm/mmu.c | 109 ++++-- arch/arm64/kvm/pkvm.c | 195 +++++++++++ arch/arm64/kvm/vgic/vgic-v3.c | 6 +- 18 files changed, 987 insertions(+), 143 deletions(-)
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 1/18 KVM: arm64: Change the layout of enum pkvm_page_state
The 'concrete' (a.k.a non-meta) page states are currently encoded using software bits in PTEs. For performance reasons, the abstract pkvm_page_state enum uses the same bits to encode these states as that makes conversions from and to PTEs easy.
In order to prepare the ground for moving the 'concrete' state storage to the hyp vmemmap, re-arrange the enum to use bits 0 and 1 for this purpose.
No functional changes intended.
--- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +10 -7
@@ -24,25 +24,28 @@*/ enum pkvm_page_state {PKVM_PAGE_OWNED =0ULL , -PKVM_PAGE_SHARED_OWNED =KVM_PGTABLE_PROT_SW0 , -PKVM_PAGE_SHARED_BORROWED =KVM_PGTABLE_PROT_SW1 , - __PKVM_PAGE_RESERVED =KVM_PGTABLE_PROT_SW0 | -KVM_PGTABLE_PROT_SW1 , +PKVM_PAGE_SHARED_OWNED =BIT (0 ), +PKVM_PAGE_SHARED_BORROWED =BIT (1 ), + __PKVM_PAGE_RESERVED =BIT (0 ) |BIT (1 ),/* Meta-states which aren't encoded directly in the PTE's SW bits */ -PKVM_NOPAGE , +PKVM_NOPAGE =BIT (2 ), }; +#define PKVM_PAGE_META_STATES_MASK (~(BIT(0) | BIT(1)))#define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1)static inline enum kvm_pgtable_prot pkvm_mkstate (enum kvm_pgtable_prot prot,enum pkvm_page_state state) { -return (prot & ~PKVM_PAGE_STATE_PROT_MASK ) | state; +BUG_ON (state &PKVM_PAGE_META_STATES_MASK ); + prot &= ~PKVM_PAGE_STATE_PROT_MASK ; + prot |=FIELD_PREP (PKVM_PAGE_STATE_PROT_MASK , state); +return prot; }static inline enum pkvm_page_state pkvm_getstate (enum kvm_pgtable_prot prot) { -return prot &PKVM_PAGE_STATE_PROT_MASK ; +return FIELD_GET (PKVM_PAGE_STATE_PROT_MASK , prot); }struct host_mmu {
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 01/18] KVM: arm64: Change the layout of enum pkvm_page_state
Hi Quentin,
Quoted 35 lines · Quentin Perret
The 'concrete' (a.k.a non-meta) page states are currently encoded using software bits in PTEs. For performance reasons, the abstract
pkvm_page_stateenum uses the same bits to encode these states as that makes conversions from and to PTEs easy.In order to prepare the ground for moving the 'concrete' state storage to the hyp vmemmap, re-arrange the enum to use bits 0 and 1 for this purpose.
No functional changes intended.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +3
-5@@ -24,25 +24,28 @@*/ enum pkvm_page_state {PKVM_PAGE_OWNED =0ULL , -PKVM_PAGE_SHARED_OWNED =KVM_PGTABLE_PROT_SW0 , -PKVM_PAGE_SHARED_BORROWED =KVM_PGTABLE_PROT_SW1 , - __PKVM_PAGE_RESERVED =KVM_PGTABLE_PROT_SW0 | -KVM_PGTABLE_PROT_SW1 , +PKVM_PAGE_SHARED_OWNED =BIT (0 ), +PKVM_PAGE_SHARED_BORROWED =BIT (1 ), + __PKVM_PAGE_RESERVED =BIT (0 ) |BIT (1 ),/* Meta-states which aren't encoded directly in the PTE's SW bits */ -PKVM_NOPAGE ,
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h line 40
Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +3
-1+ PKVM_NOPAGE = BIT(2), }; +#define PKVM_PAGE_META_STATES_MASK (~(BIT(0) | BIT(1)))#define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1)static inline enum kvm_pgtable_prot pkvm_mkstate (enum kvm_pgtable_prot prot,enum pkvm_page_state state) { -return (prot & ~PKVM_PAGE_STATE_PROT_MASK ) | state; +BUG_ON (state &PKVM_PAGE_META_STATES_MASK );
This is a slight change in functionality, having a BUG_ON instead of just masking out illegal states. Is it necessary?
Cheers,
/fuad
Quoted 15 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +4
-2+ prot &= ~PKVM_PAGE_STATE_PROT_MASK ; + prot |=FIELD_PREP (PKVM_PAGE_STATE_PROT_MASK , state); +return prot; }static inline enum pkvm_page_state pkvm_getstate (enum kvm_pgtable_prot prot) { -return prot &PKVM_PAGE_STATE_PROT_MASK ; +return FIELD_GET (PKVM_PAGE_STATE_PROT_MASK , prot); }struct host_mmu { --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 01/18] KVM: arm64: Change the layout of enum pkvm_page_state
Hey Fuad,
Quoted 42 lines · Fuad Tabba
Hi Quentin,
Quoted 39 lines · Quentin Perret
The 'concrete' (a.k.a non-meta) page states are currently encoded using software bits in PTEs. For performance reasons, the abstract
pkvm_page_stateenum uses the same bits to encode these states as that makes conversions from and to PTEs easy.In order to prepare the ground for moving the 'concrete' state storage to the hyp vmemmap, re-arrange the enum to use bits 0 and 1 for this purpose.
No functional changes intended.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +5
-5@@ -24,25 +24,28 @@*/ enum pkvm_page_state {PKVM_PAGE_OWNED =0ULL , -PKVM_PAGE_SHARED_OWNED =KVM_PGTABLE_PROT_SW0 , -PKVM_PAGE_SHARED_BORROWED =KVM_PGTABLE_PROT_SW1 , - __PKVM_PAGE_RESERVED =KVM_PGTABLE_PROT_SW0 | -KVM_PGTABLE_PROT_SW1 , +PKVM_PAGE_SHARED_OWNED =BIT (0 ), +PKVM_PAGE_SHARED_BORROWED =BIT (1 ), + __PKVM_PAGE_RESERVED =BIT (0 ) |BIT (1 ),/* Meta-states which aren't encoded directly in the PTE's SW bits */ -PKVM_NOPAGE , +PKVM_NOPAGE =BIT (2 ), }; +#define PKVM_PAGE_META_STATES_MASK (~(BIT(0) | BIT(1)))
Quoted 9 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h line 40
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-1#define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1)static inline enum kvm_pgtable_prot pkvm_mkstate (enum kvm_pgtable_prot prot,enum pkvm_page_state state) { -return (prot & ~PKVM_PAGE_STATE_PROT_MASK ) | state; +BUG_ON (state &PKVM_PAGE_META_STATES_MASK );This is a slight change in functionality, having a
BUG_ONinstead of just masking out illegal states. Is it necessary?
Yep, this is arguably a bit zealous. Passing e.g. PKVM_NOPAGE to pkvm_mkstate() would be properly bogus, so having a WARN_ON() or BUG_ON() in there is still a good thing, but it should be done in a separate patch.
I'll rework in v3.
Quoted 15 lines
Quoted 15 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +4
-2+ prot &= ~PKVM_PAGE_STATE_PROT_MASK ; + prot |=FIELD_PREP (PKVM_PAGE_STATE_PROT_MASK , state); +return prot; }static inline enum pkvm_page_state pkvm_getstate (enum kvm_pgtable_prot prot) { -return prot &PKVM_PAGE_STATE_PROT_MASK ; +return FIELD_GET (PKVM_PAGE_STATE_PROT_MASK , prot); }struct host_mmu { --2.47.0.338.g60cca15819-goog
PATCH v2 2/18 KVM: arm64: Move enum pkvm_page_state to memory.h
In reply to Quentin PerretIn order to prepare the way for storing page-tracking information in pKVM's vmemmap, move the enum pkvm_page_state definition to nvhe/memory.h.
No functional changes intended.
--- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 35 +------------------ arch/arm64/kvm/hyp/include/nvhe/memory.h | 34 ++++++++++++++++++ 2 files changed, 35 insertions(+), 34 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -34
@@ -11,43 +11,10 @@#include <asm/kvm_mmu.h> #include <asm/kvm_pgtable.h> #include <asm/virt.h> +#include <nvhe/memory.h> #include <nvhe/pkvm.h> #include <nvhe/spinlock.h> -/* -* SW bits 0-1 are reserved to track the memory ownership state of each page: -* 00: The page is owned exclusively by the page-table owner. -* 01: The page is owned by the page-table owner, but is shared -* with another entity. -* 10: The page is shared with, but not owned by the page-table owner. -* 11: Reserved for future use (lending). -*/ -enum pkvm_page_state { -PKVM_PAGE_OWNED =0ULL , -PKVM_PAGE_SHARED_OWNED =BIT (0 ), -PKVM_PAGE_SHARED_BORROWED =BIT (1 ), - __PKVM_PAGE_RESERVED =BIT (0 ) |BIT (1 ), - -/* Meta-states which aren't encoded directly in the PTE's SW bits */ -PKVM_NOPAGE =BIT (2 ), -}; -#define PKVM_PAGE_META_STATES_MASK (~(BIT(0) | BIT(1))) - -#define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1) -static inline enum kvm_pgtable_prot pkvm_mkstate (enum kvm_pgtable_prot prot, -enum pkvm_page_state state) -{ -BUG_ON (state &PKVM_PAGE_META_STATES_MASK ); - prot &= ~PKVM_PAGE_STATE_PROT_MASK ; - prot |=FIELD_PREP (PKVM_PAGE_STATE_PROT_MASK , state); -return prot; -} - -static inline enum pkvm_page_state pkvm_getstate (enum kvm_pgtable_prot prot) -{ -return FIELD_GET (PKVM_PAGE_STATE_PROT_MASK , prot); -} -struct host_mmu {struct kvm_arch arch;struct kvm_pgtable pgt;
arch/arm64/kvm/hyp/include/nvhe/memory.h +34 -0
@@ -7,6 +7,40 @@#include <linux/types.h> +/* +* SW bits 0-1 are reserved to track the memory ownership state of each page: +* 00: The page is owned exclusively by the page-table owner. +* 01: The page is owned by the page-table owner, but is shared +* with another entity. +* 10: The page is shared with, but not owned by the page-table owner. +* 11: Reserved for future use (lending). +*/ +enum pkvm_page_state { +PKVM_PAGE_OWNED =0ULL , +PKVM_PAGE_SHARED_OWNED =BIT (0 ), +PKVM_PAGE_SHARED_BORROWED =BIT (1 ), + __PKVM_PAGE_RESERVED =BIT (0 ) |BIT (1 ), + +/* Meta-states which aren't encoded directly in the PTE's SW bits */ +PKVM_NOPAGE =BIT (2 ), +}; +#define PKVM_PAGE_META_STATES_MASK (~(BIT(0) | BIT(1))) + +#define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1) +static inline enum kvm_pgtable_prot pkvm_mkstate (enum kvm_pgtable_prot prot, +enum pkvm_page_state state) +{ +BUG_ON (state &PKVM_PAGE_META_STATES_MASK ); + prot &= ~PKVM_PAGE_STATE_PROT_MASK ; + prot |=FIELD_PREP (PKVM_PAGE_STATE_PROT_MASK , state); +return prot; +} + +static inline enum pkvm_page_state pkvm_getstate (enum kvm_pgtable_prot prot) +{ +return FIELD_GET (PKVM_PAGE_STATE_PROT_MASK , prot); +} +struct hyp_page {unsigned short refcount;unsigned short order;
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 3/18 KVM: arm64: Make hyp_page::order a u8
In reply to Quentin PerretWe don't need 16 bits to store the hyp page order, and we'll need some bits to store page ownership data soon, so let's reduce the order member.
--- arch/arm64/kvm/hyp/include/nvhe/gfp.h | 6 +++--- arch/arm64/kvm/hyp/include/nvhe/memory.h | 5 +++-- arch/arm64/kvm/hyp/nvhe/page_alloc.c | 14 +++++++------- 3 files changed, 13 insertions(+), 12 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/gfp.h +3 -3
@@ -7,7 +7,7 @@#include <nvhe/memory.h> #include <nvhe/spinlock.h> -#define HYP_NO_ORDER USHRT_MAX +#define HYP_NO_ORDER 0xffstruct hyp_pool {/*
@@ -19,11 +19,11 @@ struct hyp_pool {struct list_head free_area[NR_PAGE_ORDERS ];phys_addr_t range_start;phys_addr_t range_end; -unsigned short max_order; +u8 max_order; };/* Allocation */ -void *hyp_alloc_pages (struct hyp_pool *pool,unsigned short order); +void *hyp_alloc_pages (struct hyp_pool *pool,u8 order);void hyp_split_page (struct hyp_page *page);void hyp_get_page (struct hyp_pool *pool,void *addr);void hyp_put_page (struct hyp_pool *pool,void *addr);
arch/arm64/kvm/hyp/include/nvhe/memory.h +3 -2
@@ -42,8 +42,9 @@ static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot) }struct hyp_page { -unsigned short refcount; -unsigned short order; +u16 refcount; +u8 order; +u8 reserved; };extern u64 __hyp_vmemmap;
arch/arm64/kvm/hyp/nvhe/page_alloc.c +7 -7
@@ -32,7 +32,7 @@ u64 __hyp_vmemmap;*/ static struct hyp_page *__find_buddy_nocheck (struct hyp_pool *pool,struct hyp_page *p, -unsigned short order) +u8 order) {phys_addr_t addr =hyp_page_to_phys (p);
@@ -51,7 +51,7 @@ static struct hyp_page *__find_buddy_nocheck(struct hyp_pool *pool,/* Find a buddy page currently available for allocation */ static struct hyp_page *__find_buddy_avail (struct hyp_pool *pool,struct hyp_page *p, -unsigned short order) +u8 order) {struct hyp_page *buddy =__find_buddy_nocheck (pool, p, order);
@@ -94,7 +94,7 @@ static void __hyp_attach_page(struct hyp_pool *pool, struct hyp_page *p) {phys_addr_t phys =hyp_page_to_phys (p); -unsigned short order = p->order; +u8 order = p->order;struct hyp_page *buddy;memset (hyp_page_to_virt (p),0 ,PAGE_SIZE << p->order);
@@ -129,7 +129,7 @@ static void __hyp_attach_page(struct hyp_pool *pool,static struct hyp_page *__hyp_extract_page (struct hyp_pool *pool,struct hyp_page *p, -unsigned short order) +u8 order) {struct hyp_page *buddy;
@@ -183,7 +183,7 @@ void hyp_get_page(struct hyp_pool *pool, void *addr)void hyp_split_page (struct hyp_page *p) { -unsigned short order = p->order; +u8 order = p->order;unsigned int i; p->order =0 ;
@@ -195,10 +195,10 @@ void hyp_split_page(struct hyp_page *p) } } -void *hyp_alloc_pages (struct hyp_pool *pool,unsigned short order) +void *hyp_alloc_pages (struct hyp_pool *pool,u8 order) { -unsigned short i = order;struct hyp_page *p; +u8 i = order;hyp_spin_lock (&pool->lock);
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 4/18 KVM: arm64: Move host page ownership tracking to the hyp vmemmap
In reply to Quentin PerretWe currently store part of the page-tracking state in PTE software bits for the host, guests and the hypervisor. This is sub-optimal when e.g. sharing pages as this forces to break block mappings purely to support this software tracking. This causes an unnecessarily fragmented stage-2 page-table for the host in particular when it shares pages with Secure, which can lead to measurable regressions. Moreover, having this state stored in the page-table forces us to do multiple costly walks on the page transition path, hence causing overhead.
In order to work around these problems, move the host-side page-tracking logic from SW bits in its stage-2 PTEs to the hypervisor's vmemmap.
--- arch/arm64/kvm/hyp/include/nvhe/memory.h | 6 +- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 94 ++++++++++++++++-------- arch/arm64/kvm/hyp/nvhe/setup.c | 7 +- 3 files changed, 71 insertions(+), 36 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/memory.h +4 -2
@@ -8,7 +8,7 @@#include <linux/types.h> /* -* SW bits 0-1 are reserved to track the memory ownership state of each page: +* Bits 0-1 are reserved to track the memory ownership state of each page: * 00: The page is owned exclusively by the page-table owner. * 01: The page is owned by the page-table owner, but is shared * with another entity.
@@ -44,7 +44,9 @@ static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)struct hyp_page {u16 refcount;u8 order; -u8 reserved; + +/* Host (non-meta) state. Guarded by the host stage-2 lock. */ +enum pkvm_page_state host_state :8 ; };extern u64 __hyp_vmemmap;
arch/arm64/kvm/hyp/nvhe/mem_protect.c +64 -30
157 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/setup.c +3 -4
@@ -180,7 +180,6 @@ static void hpool_put_page(void *addr)static int fix_host_ownership_walker (const struct kvm_pgtable_visit_ctx *ctx,enum kvm_pgtable_walk_flags visit) { -enum kvm_pgtable_prot prot;enum pkvm_page_state state;phys_addr_t phys;
@@ -203,16 +202,16 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,case PKVM_PAGE_OWNED :return host_stage2_set_owner_locked (phys,PAGE_SIZE ,PKVM_ID_HYP );case PKVM_PAGE_SHARED_OWNED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_BORROWED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_BORROWED ;break ;case PKVM_PAGE_SHARED_BORROWED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_OWNED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_OWNED ;break ;default :return -EINVAL ; } -return host_stage2_idmap_locked (phys,PAGE_SIZE , prot); +return 0 ; } static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx,
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 04/18] KVM: arm64: Move host page ownership tracking to the hyp vmemmap
Hi Quentin,
Quoted 25 lines · Quentin Perret
We currently store part of the page-tracking state in PTE software bits for the host, guests and the hypervisor. This is sub-optimal when e.g. sharing pages as this forces to break block mappings purely to support this software tracking. This causes an unnecessarily fragmented stage-2 page-table for the host in particular when it shares pages with Secure, which can lead to measurable regressions. Moreover, having this state stored in the page-table forces us to do multiple costly walks on the page transition path, hence causing overhead.
In order to work around these problems, move the host-side page-tracking logic from SW bits in its stage-2 PTEs to the hypervisor's vmemmap.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/memory.h | 6 +- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 94 ++++++++++++++++-------- arch/arm64/kvm/hyp/nvhe/setup.c | 7 +- 3 files changed, 71 insertions(+), 36 deletions(-)arch/arm64/kvm/hyp/include/nvhe/memory.h +0
-0
arch/arm64/kvm/hyp/include/nvhe/memory.h line 14
Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/memory.h +1
-1@@ -8,7 +8,7 @@#include <linux/types.h> /* -* SW bits 0-1 are reserved to track the memory ownership state of each page: +* Bits 0-1 are reserved to track the memory ownership state of each page: * 00: The page is owned exclusively by the page-table owner. * 01: The page is owned by the page-table owner, but is shared * with another entity.
Not shown in this patch, but a couple of lines below, you might want to update the comment on PKVM_NOPAGE to fix the reference to "PTE's SW bits":
Quoted 48 lines
/* Meta-states which aren't encoded directly in the PTE's SW bits */
PKVM_NOPAGE= BIT(2),arch/arm64/kvm/hyp/include/nvhe/memory.h +3
-1@@ -44,7 +44,9 @@ static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)struct hyp_page {u16 refcount;u8 order; -u8 reserved; + +/* Host (non-meta) state. Guarded by the host stage-2 lock. */ +enum pkvm_page_state host_state :8 ; };extern u64 __hyp_vmemmap;arch/arm64/kvm/hyp/nvhe/mem_protect.c +5
-2@@ -201,8 +201,8 @@ static void *guest_s2_zalloc_page(void *mc)memset (addr,0 ,PAGE_SIZE ); p =hyp_virt_to_page (addr); -memset (p,0 ,sizeof (*p)); p->refcount =1 ; + p->order =0 ;return addr; }@@ -268,6 +268,7 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)void reclaim_guest_pages (struct pkvm_hyp_vm *vm,struct kvm_hyp_memcache *mc) { +struct hyp_page *page;void *addr;/* Dump all pgtable pages in the hyp_pool */ @@ -279,7 +280,9 @@ void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)/* Drain the hyp_pool into the memcache */ addr =hyp_alloc_pages (&vm->pool,0 );while (addr) { -memset (hyp_virt_to_page (addr),0 ,sizeof (struct hyp_page )); + page =hyp_virt_to_page (addr); + page->refcount =0 ; + page->order =0 ;push_hyp_memcache (mc, addr, hyp_virt_to_phys);WARN_ON (__pkvm_hyp_donate_host (hyp_virt_to_pfn (addr),1 )); addr =hyp_alloc_pages (&vm->pool,0 );
arch/arm64/kvm/hyp/nvhe/mem_protect.c bool addr_is_memory(phys_addr_t phys) · line 393
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-1@@ -382,19 +385,25 @@ bool addr_is_memory(phys_addr_t phys)return !!find_mem_range (phys, &range); } -static bool addr_is_allowed_memory (phys_addr_t phys) +static bool is_in_mem_range (u64 addr,struct kvm_mem_range *range) +{ +return range->start <= addr && addr < range->end; +} + +static int range_is_allowed_memory (u64 start,u64 end)
The name of this function "range_*is*_..." implies that it returns a boolean, which other functions in this file (and patch) with similar names do, but it returns an error instead. Maybe check_range_allowed_memory)?
arch/arm64/kvm/hyp/nvhe/mem_protect.c bool addr_is_memory(phys_addr_t phys) · line 398
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1
-1{struct memblock_region *reg;struct kvm_mem_range range; - reg =find_mem_range (phys, &range); +/* Can't check the state of both MMIO and memory regions at once */
I don't understand this comment in relation to the code. Could you explain it to me please?
Quoted 17 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-5+ reg =find_mem_range (start, &range); +if (!is_in_mem_range (end -1 , &range)) +return -EINVAL ; -return reg && !(reg->flags &MEMBLOCK_NOMAP ); -} +if (!reg || reg->flags &MEMBLOCK_NOMAP ) +return -EPERM ; -static bool is_in_mem_range (u64 addr,struct kvm_mem_range *range) -{ -return range->start <= addr && addr < range->end; +return 0 ; }static bool range_is_memory (u64 start,u64 end)
arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range) · line 468
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-1@@ -454,8 +463,11 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)if (kvm_pte_valid (pte))return -EAGAIN ; -if (pte) +if (pte) { +WARN_ON (addr_is_memory (addr) && + !(hyp_phys_to_page (addr)->host_state &PKVM_NOPAGE ));
nit: since the host state is now an enum, should this just be an equality check rather than an &? This makes it consistent with other checks of pkvm_page_state in this patch too.
arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 494
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-0return -EPERM ; + } do { u64 granule = kvm_granule_size(level);@@ -477,10 +489,29 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,return host_stage2_try (__host_stage2_idmap, addr, addr + size, prot); } +static void __host_update_page_state (phys_addr_t addr,u64 size,enum pkvm_page_state state) +{ +phys_addr_t end = addr + size;
nit: newline
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-0+for (; addr < end; addr +=PAGE_SIZE ) +hyp_phys_to_page (addr)->host_state = state; +} +
arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 506
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-2int host_stage2_set_owner_locked (phys_addr_t addr,u64 size,u8 owner_id) { -return host_stage2_try (kvm_pgtable_stage2_set_owner, &host_mmu.pgt, - addr, size, &host_s2_pool, owner_id); +int ret; + + ret =host_stage2_try (kvm_pgtable_stage2_set_owner, &host_mmu.pgt, + addr, size, &host_s2_pool, owner_id); +if (ret || !addr_is_memory (addr)) +return ret;
Can hyp set an owner for an address that isn't memory? Trying to understand why we need to update the host stage2 pagetable but not the hypervisor's vmemmap in that case.
Quoted 45 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +17
-16+ +/* Don't forget to update the vmemmap tracking for the host */ +if (owner_id ==PKVM_ID_HOST ) +__host_update_page_state (addr, size,PKVM_PAGE_OWNED ); +else +__host_update_page_state (addr, size,PKVM_NOPAGE ); + +return 0 ; }static bool host_stage2_force_pte_cb (u64 addr,u64 end,enum kvm_pgtable_prot prot)@@ -604,35 +635,38 @@ static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,return kvm_pgtable_walk (pgt, addr, size, &walker); } -static enum pkvm_page_state host_get_page_state (kvm_pte_t pte,u64 addr) -{ -if (!addr_is_allowed_memory (addr)) -return PKVM_NOPAGE ; - -if (!kvm_pte_valid (pte) && pte) -return PKVM_NOPAGE ; - -return pkvm_getstate (kvm_pgtable_stage2_pte_prot (pte)); -} -static int __host_check_page_state_range (u64 addr,u64 size,enum pkvm_page_state state) { -struct check_walk_data d = { - .desired = state, - .get_page_state = host_get_page_state, - }; +u64 end = addr + size; +int ret; + + ret =range_is_allowed_memory (addr, end); +if (ret) +return ret;hyp_assert_lock_held (&host_mmu.lock); -return check_page_state_range (&host_mmu.pgt, addr, size, &d); +for (; addr < end; addr +=PAGE_SIZE ) { +if (hyp_phys_to_page (addr)->host_state != state) +return -EPERM ;
arch/arm64/kvm/hyp/nvhe/mem_protect.c static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size, · line 660
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-1+ } + +return 0 ; }static int __host_set_page_state_range (u64 addr,u64 size,enum pkvm_page_state state) { -enum kvm_pgtable_prot prot =pkvm_mkstate (PKVM_HOST_MEM_PROT , state); +if (hyp_phys_to_page (addr)->host_state &PKVM_NOPAGE ) {
Same nit as above regarding checking for PKVM_NOPAGE
Cheers,
/fuad
Quoted 48 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8
-1+int ret =host_stage2_idmap_locked (addr, size,PKVM_HOST_MEM_PROT ); -return host_stage2_idmap_locked (addr, size, prot); +if (ret) +return ret; + } + +__host_update_page_state (addr, size, state); + +return 0 ; } static int host_request_owned_transition(u64 *completer_addr,arch/arm64/kvm/hyp/nvhe/setup.c +3
-5@@ -180,7 +180,6 @@ static void hpool_put_page(void *addr)static int fix_host_ownership_walker (const struct kvm_pgtable_visit_ctx *ctx,enum kvm_pgtable_walk_flags visit) { -enum kvm_pgtable_prot prot;enum pkvm_page_state state;phys_addr_t phys;@@ -203,16 +202,16 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,case PKVM_PAGE_OWNED :return host_stage2_set_owner_locked (phys,PAGE_SIZE ,PKVM_ID_HYP );case PKVM_PAGE_SHARED_OWNED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_BORROWED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_BORROWED ;break ;case PKVM_PAGE_SHARED_BORROWED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_OWNED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_OWNED ;break ;default :return -EINVAL ; } -return host_stage2_idmap_locked (phys,PAGE_SIZE , prot); +return 0 ; } static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx, --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 04/18] KVM: arm64: Move host page ownership tracking to the hyp vmemmap
Hey Fuad,
Quoted 30 lines · Fuad Tabba
Hi Quentin,
Quoted 27 lines · Quentin Perret
We currently store part of the page-tracking state in PTE software bits for the host, guests and the hypervisor. This is sub-optimal when e.g. sharing pages as this forces to break block mappings purely to support this software tracking. This causes an unnecessarily fragmented stage-2 page-table for the host in particular when it shares pages with Secure, which can lead to measurable regressions. Moreover, having this state stored in the page-table forces us to do multiple costly walks on the page transition path, hence causing overhead.
In order to work around these problems, move the host-side page-tracking logic from SW bits in its stage-2 PTEs to the hypervisor's vmemmap.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/memory.h | 6 +- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 94 ++++++++++++++++-------- arch/arm64/kvm/hyp/nvhe/setup.c | 7 +- 3 files changed, 71 insertions(+), 36 deletions(-)arch/arm64/kvm/hyp/include/nvhe/memory.h +0
-0@@ -8,7 +8,7 @@#include <linux/types.h>
Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/memory.h line 14
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/memory.h +1
-1/* -* SW bits 0-1 are reserved to track the memory ownership state of each page: +* Bits 0-1 are reserved to track the memory ownership state of each page: * 00: The page is owned exclusively by the page-table owner. * 01: The page is owned by the page-table owner, but is shared * with another entity. Not shown in this patch, but a couple of lines below, you might want to update the comment on
PKVM_NOPAGEto fix the reference to "PTE's SW bits":
I actually think the comment is still correct -- PKVM_NOPAGE never goes in the software bits, with or without this patch, so I figured we could leave it as-is. But happy to reword if you have a good idea :)
Quoted 51 lines
Quoted 51 lines
/* Meta-states which aren't encoded directly in the PTE's SW bits */
PKVM_NOPAGE= BIT(2),arch/arm64/kvm/hyp/include/nvhe/memory.h +3
-1@@ -44,7 +44,9 @@ static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)struct hyp_page {u16 refcount;u8 order; -u8 reserved; + +/* Host (non-meta) state. Guarded by the host stage-2 lock. */ +enum pkvm_page_state host_state :8 ; };extern u64 __hyp_vmemmap;arch/arm64/kvm/hyp/nvhe/mem_protect.c +5
-2@@ -201,8 +201,8 @@ static void *guest_s2_zalloc_page(void *mc)memset (addr,0 ,PAGE_SIZE ); p =hyp_virt_to_page (addr); -memset (p,0 ,sizeof (*p)); p->refcount =1 ; + p->order =0 ;return addr; }@@ -268,6 +268,7 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)void reclaim_guest_pages (struct pkvm_hyp_vm *vm,struct kvm_hyp_memcache *mc) { +struct hyp_page *page;void *addr;/* Dump all pgtable pages in the hyp_pool */ @@ -279,7 +280,9 @@ void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)/* Drain the hyp_pool into the memcache */ addr =hyp_alloc_pages (&vm->pool,0 );while (addr) { -memset (hyp_virt_to_page (addr),0 ,sizeof (struct hyp_page )); + page =hyp_virt_to_page (addr); + page->refcount =0 ; + page->order =0 ;push_hyp_memcache (mc, addr, hyp_virt_to_phys);WARN_ON (__pkvm_hyp_donate_host (hyp_virt_to_pfn (addr),1 )); addr =hyp_alloc_pages (&vm->pool,0 );@@ -382,19 +385,25 @@ bool addr_is_memory(phys_addr_t phys)return !!find_mem_range (phys, &range); }
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c bool addr_is_memory(phys_addr_t phys) · line 393
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-1-static bool addr_is_allowed_memory (phys_addr_t phys) +static bool is_in_mem_range (u64 addr,struct kvm_mem_range *range) +{ +return range->start <= addr && addr < range->end; +} + +static int range_is_allowed_memory (u64 start,u64 end)The name of this function "range_*is*_..." implies that it returns a boolean, which other functions in this file (and patch) with similar names do, but it returns an error instead. Maybe
check_range_allowed_memory)?
Ack, I'll rename in v3.
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c bool addr_is_memory(phys_addr_t phys) · line 398
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1
-1{struct memblock_region *reg;struct kvm_mem_range range; - reg =find_mem_range (phys, &range); +/* Can't check the state of both MMIO and memory regions at once */ I don't understand this comment in relation to the code. Could you explain it to me please?
find_mem_range() will iterate the list of memblocks to find the 'range' in which @start falls. That might either be in a memblock (so @addr is memory, and @reg != NULL) or outside of one (so @addr is mmio, and @reg == NULL). The check right after ensures that @end is in the same PA range as @start. IOW, this checks that [start, end[ doesn't overlap memory and MMIO, because the following logic wouldn't work for a mixed case like that.
Quoted 20 lines
Quoted 20 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-5+ reg =find_mem_range (start, &range); +if (!is_in_mem_range (end -1 , &range)) +return -EINVAL ; -return reg && !(reg->flags &MEMBLOCK_NOMAP ); -} +if (!reg || reg->flags &MEMBLOCK_NOMAP ) +return -EPERM ; -static bool is_in_mem_range (u64 addr,struct kvm_mem_range *range) -{ -return range->start <= addr && addr < range->end; +return 0 ; }static bool range_is_memory (u64 start,u64 end)@@ -454,8 +463,11 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)if (kvm_pte_valid (pte))return -EAGAIN ;
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range) · line 468
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-1-if (pte) +if (pte) { +WARN_ON (addr_is_memory (addr) && + !(hyp_phys_to_page (addr)->host_state &PKVM_NOPAGE ));nit: since the host state is now an enum, should this just be an equality check rather than an &? This makes it consistent with other checks of
pkvm_page_statein this patch too.
We don't currently have a state that is additive to PKVM_NOPAGE, so no objection from me.
Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 494
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-0return -EPERM ; + } do { u64 granule = kvm_granule_size(level);@@ -477,10 +489,29 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,return host_stage2_try (__host_stage2_idmap, addr, addr + size, prot); } +static void __host_update_page_state (phys_addr_t addr,u64 size,enum pkvm_page_state state) +{ +phys_addr_t end = addr + size;nit: newline
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-2+for (; addr < end; addr +=PAGE_SIZE ) +hyp_phys_to_page (addr)->host_state = state; +} +int host_stage2_set_owner_locked (phys_addr_t addr,u64 size,u8 owner_id) { -return host_stage2_try (kvm_pgtable_stage2_set_owner, &host_mmu.pgt, - addr, size, &host_s2_pool, owner_id); +int ret; +
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 506
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-0+ ret =host_stage2_try (kvm_pgtable_stage2_set_owner, &host_mmu.pgt, + addr, size, &host_s2_pool, owner_id); +if (ret || !addr_is_memory (addr)) +return ret;Can hyp set an owner for an address that isn't memory? Trying to understand why we need to update the host stage2 pagetable but not the hypervisor's vmemmap in that case.
I think the answer is not currently, but we will when we'll have to e.g. donate IOMMU registers to EL2 and things of that nature. Note that this does require an extension to __host_check_page_state_range() to go query the page-table 'the old way' for MMIO addresses, though that isn't done in this series. If you think strongly that this is confusing, I'm happy to drop that check and we'll add it back with the IOMMU series or something like that.
Quoted 110 lines
Quoted 45 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +17
-16+ +/* Don't forget to update the vmemmap tracking for the host */ +if (owner_id ==PKVM_ID_HOST ) +__host_update_page_state (addr, size,PKVM_PAGE_OWNED ); +else +__host_update_page_state (addr, size,PKVM_NOPAGE ); + +return 0 ; }static bool host_stage2_force_pte_cb (u64 addr,u64 end,enum kvm_pgtable_prot prot)@@ -604,35 +635,38 @@ static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,return kvm_pgtable_walk (pgt, addr, size, &walker); } -static enum pkvm_page_state host_get_page_state (kvm_pte_t pte,u64 addr) -{ -if (!addr_is_allowed_memory (addr)) -return PKVM_NOPAGE ; - -if (!kvm_pte_valid (pte) && pte) -return PKVM_NOPAGE ; - -return pkvm_getstate (kvm_pgtable_stage2_pte_prot (pte)); -} -static int __host_check_page_state_range (u64 addr,u64 size,enum pkvm_page_state state) { -struct check_walk_data d = { - .desired = state, - .get_page_state = host_get_page_state, - }; +u64 end = addr + size; +int ret; + + ret =range_is_allowed_memory (addr, end); +if (ret) +return ret;hyp_assert_lock_held (&host_mmu.lock); -return check_page_state_range (&host_mmu.pgt, addr, size, &d); +for (; addr < end; addr +=PAGE_SIZE ) { +if (hyp_phys_to_page (addr)->host_state != state) +return -EPERM ;arch/arm64/kvm/hyp/nvhe/mem_protect.c static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size, · line 660
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-1+ } + +return 0 ; }static int __host_set_page_state_range (u64 addr,u64 size,enum pkvm_page_state state) { -enum kvm_pgtable_prot prot =pkvm_mkstate (PKVM_HOST_MEM_PROT , state); +if (hyp_phys_to_page (addr)->host_state &PKVM_NOPAGE ) {Same nit as above regarding checking for
PKVM_NOPAGECheers,
/fuadQuoted 48 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8
-1+int ret =host_stage2_idmap_locked (addr, size,PKVM_HOST_MEM_PROT ); -return host_stage2_idmap_locked (addr, size, prot); +if (ret) +return ret; + } + +__host_update_page_state (addr, size, state); + +return 0 ; } static int host_request_owned_transition(u64 *completer_addr,arch/arm64/kvm/hyp/nvhe/setup.c +3
-5@@ -180,7 +180,6 @@ static void hpool_put_page(void *addr)static int fix_host_ownership_walker (const struct kvm_pgtable_visit_ctx *ctx,enum kvm_pgtable_walk_flags visit) { -enum kvm_pgtable_prot prot;enum pkvm_page_state state;phys_addr_t phys;@@ -203,16 +202,16 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,case PKVM_PAGE_OWNED :return host_stage2_set_owner_locked (phys,PAGE_SIZE ,PKVM_ID_HYP );case PKVM_PAGE_SHARED_OWNED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_BORROWED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_BORROWED ;break ;case PKVM_PAGE_SHARED_BORROWED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_OWNED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_OWNED ;break ;default :return -EINVAL ; } -return host_stage2_idmap_locked (phys,PAGE_SIZE , prot); +return 0 ; } static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx, --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 04/18] KVM: arm64: Move host page ownership tracking to the hyp vmemmap
Hi Quentin,
Quoted 45 lines · Quentin Perret
Hey Fuad,
Quoted 30 lines · Fuad Tabba
Hi Quentin,
Quoted 27 lines · Quentin Perret
We currently store part of the page-tracking state in PTE software bits for the host, guests and the hypervisor. This is sub-optimal when e.g. sharing pages as this forces to break block mappings purely to support this software tracking. This causes an unnecessarily fragmented stage-2 page-table for the host in particular when it shares pages with Secure, which can lead to measurable regressions. Moreover, having this state stored in the page-table forces us to do multiple costly walks on the page transition path, hence causing overhead.
In order to work around these problems, move the host-side page-tracking logic from SW bits in its stage-2 PTEs to the hypervisor's vmemmap.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/memory.h | 6 +- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 94 ++++++++++++++++-------- arch/arm64/kvm/hyp/nvhe/setup.c | 7 +- 3 files changed, 71 insertions(+), 36 deletions(-)arch/arm64/kvm/hyp/include/nvhe/memory.h +0
-0@@ -8,7 +8,7 @@#include <linux/types.h> Quoted 10 lines
arch/arm64/kvm/hyp/include/nvhe/memory.h line 14
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/memory.h +1
-1/* -* SW bits 0-1 are reserved to track the memory ownership state of each page: +* Bits 0-1 are reserved to track the memory ownership state of each page: * 00: The page is owned exclusively by the page-table owner. * 01: The page is owned by the page-table owner, but is shared * with another entity. Not shown in this patch, but a couple of lines below, you might want to update the comment on
PKVM_NOPAGEto fix the reference to "PTE's SW bits":
Quoted 3 lines
I actually think the comment is still correct --
PKVM_NOPAGEnever goes in the software bits, with or without this patch, so I figured we could leave it as-is. But happy to reword if you have a good idea :)
I see, no, that's fine.
Quoted 76 lines
Quoted 51 lines
Quoted 51 lines
/* Meta-states which aren't encoded directly in the PTE's SW bits */
PKVM_NOPAGE= BIT(2),arch/arm64/kvm/hyp/include/nvhe/memory.h +3
-1@@ -44,7 +44,9 @@ static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)struct hyp_page {u16 refcount;u8 order; -u8 reserved; + +/* Host (non-meta) state. Guarded by the host stage-2 lock. */ +enum pkvm_page_state host_state :8 ; };extern u64 __hyp_vmemmap;arch/arm64/kvm/hyp/nvhe/mem_protect.c +5
-2@@ -201,8 +201,8 @@ static void *guest_s2_zalloc_page(void *mc)memset (addr,0 ,PAGE_SIZE ); p =hyp_virt_to_page (addr); -memset (p,0 ,sizeof (*p)); p->refcount =1 ; + p->order =0 ;return addr; }@@ -268,6 +268,7 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)void reclaim_guest_pages (struct pkvm_hyp_vm *vm,struct kvm_hyp_memcache *mc) { +struct hyp_page *page;void *addr;/* Dump all pgtable pages in the hyp_pool */ @@ -279,7 +280,9 @@ void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)/* Drain the hyp_pool into the memcache */ addr =hyp_alloc_pages (&vm->pool,0 );while (addr) { -memset (hyp_virt_to_page (addr),0 ,sizeof (struct hyp_page )); + page =hyp_virt_to_page (addr); + page->refcount =0 ; + page->order =0 ;push_hyp_memcache (mc, addr, hyp_virt_to_phys);WARN_ON (__pkvm_hyp_donate_host (hyp_virt_to_pfn (addr),1 )); addr =hyp_alloc_pages (&vm->pool,0 );@@ -382,19 +385,25 @@ bool addr_is_memory(phys_addr_t phys)return !!find_mem_range (phys, &range); }Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c bool addr_is_memory(phys_addr_t phys) · line 393
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-1-static bool addr_is_allowed_memory (phys_addr_t phys) +static bool is_in_mem_range (u64 addr,struct kvm_mem_range *range) +{ +return range->start <= addr && addr < range->end; +} + +static int range_is_allowed_memory (u64 start,u64 end)The name of this function "range_*is*_..." implies that it returns a boolean, which other functions in this file (and patch) with similar names do, but it returns an error instead. Maybe
check_range_allowed_memory)?Ack, I'll rename in v3.
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c bool addr_is_memory(phys_addr_t phys) · line 398
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1
-1{struct memblock_region *reg;struct kvm_mem_range range; - reg =find_mem_range (phys, &range); +/* Can't check the state of both MMIO and memory regions at once */ I don't understand this comment in relation to the code. Could you explain it to me please?
Quoted 7 lines
find_mem_range()will iterate the list of memblocks to find the 'range' in which @start falls. That might either be in a memblock (so @addr is memory, and @reg != NULL) or outside of one (so @addr is mmio, and @reg == NULL). The check right after ensures that @end is in the same PA range as @start. IOW, this checks that [start, end[ doesn't overlap memory and MMIO, because the following logic wouldn't work for a mixed case like that.
I understand now. I think it might be worth elaborating a bit on the comment to clarify that. What is confusing to me was that the comment refers to checking state, but it's in a function that does not care about page state, i.e., it's not immediately obvious that its primary callers/users are __host_check_page_state_range(), and other functions that check the state of a range.
Quoted 66 lines
Quoted 20 lines
Quoted 20 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-5+ reg =find_mem_range (start, &range); +if (!is_in_mem_range (end -1 , &range)) +return -EINVAL ; -return reg && !(reg->flags &MEMBLOCK_NOMAP ); -} +if (!reg || reg->flags &MEMBLOCK_NOMAP ) +return -EPERM ; -static bool is_in_mem_range (u64 addr,struct kvm_mem_range *range) -{ -return range->start <= addr && addr < range->end; +return 0 ; }static bool range_is_memory (u64 start,u64 end)@@ -454,8 +463,11 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)if (kvm_pte_valid (pte))return -EAGAIN ;Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range) · line 468
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-1-if (pte) +if (pte) { +WARN_ON (addr_is_memory (addr) && + !(hyp_phys_to_page (addr)->host_state &PKVM_NOPAGE ));nit: since the host state is now an enum, should this just be an equality check rather than an &? This makes it consistent with other checks of
pkvm_page_statein this patch too.We don't currently have a state that is additive to
PKVM_NOPAGE, so no objection from me.Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 494
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-0return -EPERM ; + } do { u64 granule = kvm_granule_size(level);@@ -477,10 +489,29 @@ int host_stage2_idmap_locked(phys_addr_t addr, u64 size,return host_stage2_try (__host_stage2_idmap, addr, addr + size, prot); } +static void __host_update_page_state (phys_addr_t addr,u64 size,enum pkvm_page_state state) +{ +phys_addr_t end = addr + size;nit: newline
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6
-2+for (; addr < end; addr +=PAGE_SIZE ) +hyp_phys_to_page (addr)->host_state = state; +} +int host_stage2_set_owner_locked (phys_addr_t addr,u64 size,u8 owner_id) { -return host_stage2_try (kvm_pgtable_stage2_set_owner, &host_mmu.pgt, - addr, size, &host_s2_pool, owner_id); +int ret; +Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_idmap_locked(phys_addr_t addr, u64 size, · line 506
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-0+ ret =host_stage2_try (kvm_pgtable_stage2_set_owner, &host_mmu.pgt, + addr, size, &host_s2_pool, owner_id); +if (ret || !addr_is_memory (addr)) +return ret;Can hyp set an owner for an address that isn't memory? Trying to understand why we need to update the host stage2 pagetable but not the hypervisor's vmemmap in that case.
Quoted 7 lines
I think the answer is not currently, but we will when we'll have to e.g. donate IOMMU registers to EL2 and things of that nature. Note that this does require an extension to
__host_check_page_state_range()to go query the page-table 'the old way' for MMIO addresses, though that isn't done in this series. If you think strongly that this is confusing, I'm happy to drop that check and we'll add it back with the IOMMU series or something like that.
I think it's worth a comment if you're not dropping the check..
Quoted 111 lines
Quoted 111 lines
Quoted 46 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +17
-16+ +/* Don't forget to update the vmemmap tracking for the host */ +if (owner_id ==PKVM_ID_HOST ) +__host_update_page_state (addr, size,PKVM_PAGE_OWNED ); +else +__host_update_page_state (addr, size,PKVM_NOPAGE ); + +return 0 ; }static bool host_stage2_force_pte_cb (u64 addr,u64 end,enum kvm_pgtable_prot prot)@@ -604,35 +635,38 @@ static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,return kvm_pgtable_walk (pgt, addr, size, &walker); } -static enum pkvm_page_state host_get_page_state (kvm_pte_t pte,u64 addr) -{ -if (!addr_is_allowed_memory (addr)) -return PKVM_NOPAGE ; - -if (!kvm_pte_valid (pte) && pte) -return PKVM_NOPAGE ; - -return pkvm_getstate (kvm_pgtable_stage2_pte_prot (pte)); -} -static int __host_check_page_state_range (u64 addr,u64 size,enum pkvm_page_state state) { -struct check_walk_data d = { - .desired = state, - .get_page_state = host_get_page_state, - }; +u64 end = addr + size; +int ret; + + ret =range_is_allowed_memory (addr, end); +if (ret) +return ret;hyp_assert_lock_held (&host_mmu.lock); -return check_page_state_range (&host_mmu.pgt, addr, size, &d); +for (; addr < end; addr +=PAGE_SIZE ) { +if (hyp_phys_to_page (addr)->host_state != state) +return -EPERM ;arch/arm64/kvm/hyp/nvhe/mem_protect.c static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size, · line 660
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4
-1+ } + +return 0 ; }static int __host_set_page_state_range (u64 addr,u64 size,enum pkvm_page_state state) { -enum kvm_pgtable_prot prot =pkvm_mkstate (PKVM_HOST_MEM_PROT , state); +if (hyp_phys_to_page (addr)->host_state &PKVM_NOPAGE ) {Same nit as above regarding checking for
PKVM_NOPAGECheers,
/fuadQuoted 48 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8
-1+int ret =host_stage2_idmap_locked (addr, size,PKVM_HOST_MEM_PROT ); -return host_stage2_idmap_locked (addr, size, prot); +if (ret) +return ret; + } + +__host_update_page_state (addr, size, state); + +return 0 ; } static int host_request_owned_transition(u64 *completer_addr,arch/arm64/kvm/hyp/nvhe/setup.c +3
-5@@ -180,7 +180,6 @@ static void hpool_put_page(void *addr)static int fix_host_ownership_walker (const struct kvm_pgtable_visit_ctx *ctx,enum kvm_pgtable_walk_flags visit) { -enum kvm_pgtable_prot prot;enum pkvm_page_state state;phys_addr_t phys;@@ -203,16 +202,16 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,case PKVM_PAGE_OWNED :return host_stage2_set_owner_locked (phys,PAGE_SIZE ,PKVM_ID_HYP );case PKVM_PAGE_SHARED_OWNED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_BORROWED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_BORROWED ;break ;case PKVM_PAGE_SHARED_BORROWED : - prot =pkvm_mkstate (PKVM_HOST_MEM_PROT ,PKVM_PAGE_SHARED_OWNED ); +hyp_phys_to_page (phys)->host_state =PKVM_PAGE_SHARED_OWNED ;break ;default :return -EINVAL ; } -return host_stage2_idmap_locked (phys,PAGE_SIZE , prot); +return 0 ; } static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx, --2.47.0.338.g60cca15819-goog
PATCH v2 5/18 KVM: arm64: Pass walk flags to kvm_pgtable_stage2_mkyoung
In reply to Quentin Perretkvm_pgtable_stage2_mkyoung currently assumes that it is being called from a 'shared' walker, which will not be true once called from pKVM. To allow for the re-use of that function, make the walk flags one of its parameters.
--- arch/arm64/include/asm/kvm_pgtable.h | 4 +++- arch/arm64/kvm/hyp/pgtable.c | 7 +++---- arch/arm64/kvm/mmu.c | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +3 -1
@@ -669,13 +669,15 @@ int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);* kvm_pgtable_stage2_mkyoung() - Set the access flag in a page-table entry. * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*(). * @addr: Intermediate physical address to identify the page-table entry. +* @flags: Flags to control the page-table walk (ex. a shared walk) * * The offset of @addr within a page is ignored. * * If there is a valid, leaf page-table entry used to translate @addr, then * set the access flag in that entry. */ -void kvm_pgtable_stage2_mkyoung (struct kvm_pgtable *pgt,u64 addr); +void kvm_pgtable_stage2_mkyoung (struct kvm_pgtable *pgt,u64 addr, +enum kvm_pgtable_walk_flags flags);/** * kvm_pgtable_stage2_test_clear_young() - Test and optionally clear the access
arch/arm64/kvm/hyp/pgtable.c +3 -4
@@ -1245,14 +1245,13 @@ int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) NULL, NULL, 0); } -void kvm_pgtable_stage2_mkyoung (struct kvm_pgtable *pgt,u64 addr) +void kvm_pgtable_stage2_mkyoung (struct kvm_pgtable *pgt,u64 addr, +enum kvm_pgtable_walk_flags flags) {int ret; ret =stage2_update_leaf_attrs (pgt, addr,1 ,KVM_PTE_LEAF_ATTR_LO_S2_AF ,0 , -NULL ,NULL , -KVM_PGTABLE_WALK_HANDLE_FAULT | -KVM_PGTABLE_WALK_SHARED ); +NULL ,NULL , flags);if (!ret)dsb (ishst); }
arch/arm64/kvm/mmu.c +2 -1
@@ -1718,13 +1718,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,/* Resolve the access fault by making the page young again. */ static void handle_access_fault (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa) { +enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_HANDLE_FAULT |KVM_PGTABLE_WALK_SHARED ;struct kvm_s2_mmu *mmu;trace_kvm_access_fault (fault_ipa);read_lock (&vcpu->kvm->mmu_lock); mmu = vcpu->arch.hw_mmu; -kvm_pgtable_stage2_mkyoung (mmu->pgt, fault_ipa); +kvm_pgtable_stage2_mkyoung (mmu->pgt, fault_ipa, flags);read_unlock (&vcpu->kvm->mmu_lock); }
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 6/18 KVM: arm64: Pass walk flags to kvm_pgtable_stage2_relax_perms
In reply to Quentin Perretkvm_pgtable_stage2_relax_perms currently assumes that it is being called from a 'shared' walker, which will not be true once called from pKVM. To allow for the re-use of that function, make the walk flags one of its parameters.
--- arch/arm64/include/asm/kvm_pgtable.h | 4 +++- arch/arm64/kvm/hyp/pgtable.c | 6 ++---- arch/arm64/kvm/mmu.c | 7 +++---- 3 files changed, 8 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +3 -1
@@ -707,6 +707,7 @@ bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr,* @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*(). * @addr: Intermediate physical address to identify the page-table entry. * @prot: Additional permissions to grant for the mapping. +* @flags: Flags to control the page-table walk (ex. a shared walk) * * The offset of @addr within a page is ignored. *
@@ -719,7 +720,8 @@ bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr,* Return: 0 on success, negative error code on failure. */ int kvm_pgtable_stage2_relax_perms (struct kvm_pgtable *pgt,u64 addr, -enum kvm_pgtable_prot prot); +enum kvm_pgtable_prot prot, +enum kvm_pgtable_walk_flags flags);/** * kvm_pgtable_stage2_flush_range() - Clean and invalidate data cache to Point
arch/arm64/kvm/hyp/pgtable.c +2 -4
@@ -1307,7 +1307,7 @@ bool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, }int kvm_pgtable_stage2_relax_perms (struct kvm_pgtable *pgt,u64 addr, -enum kvm_pgtable_prot prot) +enum kvm_pgtable_prot prot,enum kvm_pgtable_walk_flags flags) {int ret;s8 level;
@@ -1325,9 +1325,7 @@ int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,if (prot &KVM_PGTABLE_PROT_X ) clr |=KVM_PTE_LEAF_ATTR_HI_S2_XN ; - ret =stage2_update_leaf_attrs (pgt, addr,1 , set, clr,NULL , &level, -KVM_PGTABLE_WALK_HANDLE_FAULT | -KVM_PGTABLE_WALK_SHARED ); + ret =stage2_update_leaf_attrs (pgt, addr,1 , set, clr,NULL , &level, flags);if (!ret || ret == -EAGAIN )kvm_call_hyp (__kvm_tlb_flush_vmid_ipa_nsh, pgt->mmu, addr, level);return ret;
arch/arm64/kvm/mmu.c +3 -4
@@ -1452,6 +1452,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ;struct kvm_pgtable *pgt;struct page *page; +enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_HANDLE_FAULT |KVM_PGTABLE_WALK_SHARED ;if (fault_is_perm) fault_granule =kvm_vcpu_trap_get_perm_fault_granule (vcpu);
@@ -1695,13 +1696,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,* PTE, which will be preserved. */ prot &= ~KVM_NV_GUEST_MAP_SZ ; - ret =kvm_pgtable_stage2_relax_perms (pgt, fault_ipa, prot); + ret =kvm_pgtable_stage2_relax_perms (pgt, fault_ipa, prot, flags); }else { ret =kvm_pgtable_stage2_map (pgt, fault_ipa, vma_pagesize,__pfn_to_phys (pfn), prot, - memcache, -KVM_PGTABLE_WALK_HANDLE_FAULT | -KVM_PGTABLE_WALK_SHARED ); + memcache, flags); } out_unlock:
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 7/18 KVM: arm64: Make kvm_pgtable_stage2_init() a static inline function
In reply to Quentin PerretTurn kvm_pgtable_stage2_init() into a static inline function instead of a macro. This will allow the usage of typeof() on it later on.
--- arch/arm64/include/asm/kvm_pgtable.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +5 -2
@@ -526,8 +526,11 @@ int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,enum kvm_pgtable_stage2_flags flags, kvm_pgtable_force_pte_cb_t force_pte_cb); -#define kvm_pgtable_stage2_init (pgt, mmu, mm_ops) \ - __kvm_pgtable_stage2_init(pgt, mmu, mm_ops, 0, NULL) +static inline int kvm_pgtable_stage2_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu, +struct kvm_pgtable_mm_ops *mm_ops) +{ +return __kvm_pgtable_stage2_init (pgt, mmu, mm_ops,0 ,NULL ); +}/** * kvm_pgtable_stage2_destroy() - Destroy an unused guest stage-2 page-table.
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 8/18 KVM: arm64: Add {get,put}_pkvm_hyp_vm() helpers
In reply to Quentin PerretIn preparation for accessing pkvm_hyp_vm structures at EL2 in a context where we can't always expect a vCPU to be loaded (e.g. MMU notifiers), introduce get/put helpers to get temporary references to hyp VMs from any context.
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 3 +++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+)
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +3 -0
@@ -70,4 +70,7 @@ struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle, unsigned int vcpu_idx);void pkvm_put_hyp_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu); +struct pkvm_hyp_vm *get_pkvm_hyp_vm (pkvm_handle_t handle); +void put_pkvm_hyp_vm (struct pkvm_hyp_vm *hyp_vm); +#endif /* __ARM64_KVM_NVHE_PKVM_H__ */
arch/arm64/kvm/hyp/nvhe/pkvm.c +20 -0
@@ -327,6 +327,26 @@ void pkvm_put_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)hyp_spin_unlock (&vm_table_lock); } +struct pkvm_hyp_vm *get_pkvm_hyp_vm (pkvm_handle_t handle) +{ +struct pkvm_hyp_vm *hyp_vm; + +hyp_spin_lock (&vm_table_lock); + hyp_vm =get_vm_by_handle (handle); +if (hyp_vm) +hyp_page_ref_inc (hyp_virt_to_page (hyp_vm)); +hyp_spin_unlock (&vm_table_lock); + +return hyp_vm; +} + +void put_pkvm_hyp_vm (struct pkvm_hyp_vm *hyp_vm) +{ +hyp_spin_lock (&vm_table_lock); +hyp_page_ref_dec (hyp_virt_to_page (hyp_vm)); +hyp_spin_unlock (&vm_table_lock); +} +static void pkvm_init_features_from_host (struct pkvm_hyp_vm *hyp_vm,const struct kvm *host_kvm) {struct kvm *kvm = &hyp_vm->kvm;
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 9/18 KVM: arm64: Introduce __pkvm_vcpu_{load,put}()
In reply to Quentin PerretFrom: Marc Zyngier <[email protected]>
Rather than look-up the hyp vCPU on every run hypercall at EL2, introduce a per-CPU 'loaded_hyp_vcpu' tracking variable which is updated by a pair of load/put hypercalls called directly from kvm_arch_vcpu_{load,put}() when pKVM is enabled.
--- arch/arm64/include/asm/kvm_asm.h | 2 ++ arch/arm64/kvm/arm.c | 14 ++++++++ arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 7 ++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 47 ++++++++++++++++++++------ arch/arm64/kvm/hyp/nvhe/pkvm.c | 29 ++++++++++++++++ arch/arm64/kvm/vgic/vgic-v3.c | 6 ++-- 6 files changed, 93 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_asm.h +2 -0
@@ -79,6 +79,8 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_init_vm, __KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu, __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, + __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, + __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, };#define DECLARE_KVM_VHE_SYM (sym) extern char sym[]
arch/arm64/kvm/arm.c +14 -0
@@ -619,12 +619,26 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_arch_vcpu_load_debug_state_flags (vcpu); +if (is_protected_kvm_enabled ()) { +kvm_call_hyp_nvhe (__pkvm_vcpu_load, + vcpu->kvm->arch.pkvm.handle, + vcpu->vcpu_idx, vcpu->arch.hcr_el2); +kvm_call_hyp (__vgic_v3_restore_vmcr_aprs, + &vcpu->arch.vgic_cpu.vgic_v3); + } +if (!cpumask_test_cpu (cpu, vcpu->kvm->arch.supported_cpus))vcpu_set_on_unsupported_cpu (vcpu); }void kvm_arch_vcpu_put (struct kvm_vcpu *vcpu) { +if (is_protected_kvm_enabled ()) { +kvm_call_hyp (__vgic_v3_save_vmcr_aprs, + &vcpu->arch.vgic_cpu.vgic_v3); +kvm_call_hyp_nvhe (__pkvm_vcpu_put); + } +kvm_arch_vcpu_put_debug_state_flags (vcpu);kvm_arch_vcpu_put_fp (vcpu);if (has_vhe ())
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +7 -0
@@ -20,6 +20,12 @@ struct pkvm_hyp_vcpu {/* Backpointer to the host's (untrusted) vCPU instance. */ struct kvm_vcpu *host_vcpu; + +/* +* If this hyp vCPU is loaded, then this is a backpointer to the +* per-cpu pointer tracking us. Otherwise, NULL if not loaded. +*/ +struct pkvm_hyp_vcpu **loaded_hyp_vcpu; };/*
@@ -69,6 +75,7 @@ int __pkvm_teardown_vm(pkvm_handle_t handle);struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu (pkvm_handle_t handle,unsigned int vcpu_idx);void pkvm_put_hyp_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu); +struct pkvm_hyp_vcpu *pkvm_get_loaded_hyp_vcpu (void );struct pkvm_hyp_vm *get_pkvm_hyp_vm (pkvm_handle_t handle);void put_pkvm_hyp_vm (struct pkvm_hyp_vm *hyp_vm);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +37 -10
80 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/pkvm.c +29 -0
@@ -23,6 +23,12 @@ unsigned int kvm_arm_vmid_bits;unsigned int kvm_host_sve_max_vl; +/* +* The currently loaded hyp vCPU for each physical CPU. Used only when +* protected KVM is enabled, but for both protected and non-protected VMs. +*/ +static DEFINE_PER_CPU(struct pkvm_hyp_vcpu *, loaded_hyp_vcpu); +/* * Set trap register values based on features in ID_AA64PFR0. */
@@ -306,15 +312,30 @@ struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle,struct pkvm_hyp_vcpu *hyp_vcpu =NULL ;struct pkvm_hyp_vm *hyp_vm; +/* Cannot load a new vcpu without putting the old one first. */ +if (__this_cpu_read (loaded_hyp_vcpu)) +return NULL ; +hyp_spin_lock (&vm_table_lock); hyp_vm =get_vm_by_handle (handle);if (!hyp_vm || hyp_vm->nr_vcpus <= vcpu_idx) goto unlock; hyp_vcpu = hyp_vm->vcpus[vcpu_idx]; + +/* Ensure vcpu isn't loaded on more than one cpu simultaneously. */ +if (unlikely (hyp_vcpu->loaded_hyp_vcpu)) { + hyp_vcpu =NULL ; + goto unlock; + } + + hyp_vcpu->loaded_hyp_vcpu =this_cpu_ptr (&loaded_hyp_vcpu);hyp_page_ref_inc (hyp_virt_to_page (hyp_vm)); unlock:hyp_spin_unlock (&vm_table_lock); + +if (hyp_vcpu) +__this_cpu_write (loaded_hyp_vcpu, hyp_vcpu);return hyp_vcpu; }
@@ -323,10 +344,18 @@ void pkvm_put_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)struct pkvm_hyp_vm *hyp_vm =pkvm_hyp_vcpu_to_hyp_vm (hyp_vcpu);hyp_spin_lock (&vm_table_lock); + hyp_vcpu->loaded_hyp_vcpu =NULL ; +__this_cpu_write (loaded_hyp_vcpu,NULL );hyp_page_ref_dec (hyp_virt_to_page (hyp_vm));hyp_spin_unlock (&vm_table_lock); } +struct pkvm_hyp_vcpu *pkvm_get_loaded_hyp_vcpu (void ) +{ +return __this_cpu_read (loaded_hyp_vcpu); + +} +struct pkvm_hyp_vm *get_pkvm_hyp_vm (pkvm_handle_t handle) {struct pkvm_hyp_vm *hyp_vm;
arch/arm64/kvm/vgic/vgic-v3.c +4 -2
@@ -734,7 +734,8 @@ void vgic_v3_load(struct kvm_vcpu *vcpu) {struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; -kvm_call_hyp (__vgic_v3_restore_vmcr_aprs, cpu_if); +if (likely (!is_protected_kvm_enabled ())) +kvm_call_hyp (__vgic_v3_restore_vmcr_aprs, cpu_if);if (has_vhe ())__vgic_v3_activate_traps (cpu_if);
@@ -746,7 +747,8 @@ void vgic_v3_put(struct kvm_vcpu *vcpu) {struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; -kvm_call_hyp (__vgic_v3_save_vmcr_aprs, cpu_if); +if (likely (!is_protected_kvm_enabled ())) +kvm_call_hyp (__vgic_v3_save_vmcr_aprs, cpu_if);WARN_ON (vgic_v4_put (vcpu));if (has_vhe ())
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH v2 10/18 KVM: arm64: Introduce __pkvm_host_share_guest()
In reply to Quentin PerretIn preparation for handling guest stage-2 mappings at EL2, introduce a new pKVM hypercall allowing to share pages with non-protected guests.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/memory.h | 2 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 34 +++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 70 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 7 ++ 7 files changed, 118 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -65,6 +65,7 @@ enum __kvm_host_smccc_func {/* Hypercalls available after pKVM finalisation */ __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, + __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -771,6 +771,9 @@ struct kvm_vcpu_arch {/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache; +/* Pages to be donated to pkvm/EL2 if it runs out */ +struct kvm_hyp_memcache pkvm_memcache; +/* Virtual SError ESR to restore when HCR_EL2.VSE is set */ u64 vsesr_el2;
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -39,6 +39,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages); +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);
arch/arm64/kvm/hyp/include/nvhe/memory.h +2 -0
@@ -47,6 +47,8 @@ struct hyp_page {/* Host (non-meta) state. Guarded by the host stage-2 lock. */ enum pkvm_page_state host_state : 8; + +u32 host_share_guest_count; };extern u64 __hyp_vmemmap;
arch/arm64/kvm/hyp/nvhe/hyp-main.c +34 -0
@@ -211,6 +211,39 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static int pkvm_refill_memcache (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; + +return refill_memcache (&hyp_vcpu->vcpu.arch.pkvm_memcache, + host_vcpu->arch.pkvm_memcache.nr_pages, + &host_vcpu->arch.pkvm_memcache); +} + +static void handle___pkvm_host_share_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, pfn, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =pkvm_refill_memcache (hyp_vcpu); +if (ret) + goto out; + + ret =__pkvm_host_share_guest (pfn, gfn, hyp_vcpu, prot); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
@@ -420,6 +453,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp), +HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),
arch/arm64/kvm/hyp/nvhe/mem_protect.c +70 -0
79 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/pkvm.c +7 -0
@@ -795,6 +795,13 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)/* Push the metadata pages to the teardown memcache */ for (idx =0 ; idx < hyp_vm->nr_vcpus; ++idx) {struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx]; +struct kvm_hyp_memcache *vcpu_mc = &hyp_vcpu->vcpu.arch.pkvm_memcache; + +while (vcpu_mc->nr_pages) { +void *addr =pop_hyp_memcache (vcpu_mc, hyp_phys_to_virt); +push_hyp_memcache (mc, addr, hyp_virt_to_phys); +unmap_donated_memory_noclear (addr,PAGE_SIZE ); + }teardown_donated_memory (mc, hyp_vcpu,sizeof (*hyp_vcpu)); }
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Hi Quentin,
Quoted 32 lines · Quentin Perret
In preparation for handling guest stage-2 mappings at EL2, introduce a new pKVM hypercall allowing to share pages with non-protected guests.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/memory.h | 2 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 34 +++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 70 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 7 ++ 7 files changed, 118 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -65,6 +65,7 @@ enum __kvm_host_smccc_func {/* Hypercalls available after pKVM finalisation */ __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, + __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/include/asm/kvm_host.h +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 774
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +1
-0@@ -771,6 +771,9 @@ struct kvm_vcpu_arch {/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache; +/* Pages to be donated to pkvm/EL2 if it runs out */
Runs out of what? :) I'm being facetious, it's just that the comment is a bit unclear.
Quoted 139 lines
arch/arm64/include/asm/kvm_host.h +2
-0+struct kvm_hyp_memcache pkvm_memcache; +/* Virtual SError ESR to restore when HCR_EL2.VSE is set */ u64 vsesr_el2;arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -39,6 +39,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages); +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/include/nvhe/memory.h +2
-0@@ -47,6 +47,8 @@ struct hyp_page {/* Host (non-meta) state. Guarded by the host stage-2 lock. */ enum pkvm_page_state host_state : 8; + +u32 host_share_guest_count; };extern u64 __hyp_vmemmap;arch/arm64/kvm/hyp/nvhe/hyp-main.c +34
-0@@ -211,6 +211,39 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static int pkvm_refill_memcache (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; + +return refill_memcache (&hyp_vcpu->vcpu.arch.pkvm_memcache, + host_vcpu->arch.pkvm_memcache.nr_pages, + &host_vcpu->arch.pkvm_memcache); +} + +static void handle___pkvm_host_share_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, pfn, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =pkvm_refill_memcache (hyp_vcpu); +if (ret) + goto out; + + ret =__pkvm_host_share_guest (pfn, gfn, hyp_vcpu, prot); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -420,6 +453,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp), +HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +42
-0@@ -861,6 +861,27 @@ static int hyp_complete_donation(u64 addr,return pkvm_create_mappings_locked (start, end, prot); } +static enum pkvm_page_state guest_get_page_state (kvm_pte_t pte,u64 addr) +{ +if (!kvm_pte_valid (pte)) +return PKVM_NOPAGE ; + +return pkvm_getstate (kvm_pgtable_stage2_pte_prot (pte)); +} + +static int __guest_check_page_state_range (struct pkvm_hyp_vcpu *vcpu,u64 addr, +u64 size,enum pkvm_page_state state) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +struct check_walk_data d = { + .desired = state, + .get_page_state = guest_get_page_state, + }; + +hyp_assert_lock_held (&vm->lock); +return check_page_state_range (&vm->pgt, addr, size, &d); +} +static int check_share (struct pkvm_mem_share *share) {const struct pkvm_mem_transition *tx = &share->tx;@@ -1343,3 +1364,52 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)return ret; } + +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu, +enum kvm_pgtable_prot prot) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 phys =hyp_pfn_to_phys (pfn); +u64 ipa =hyp_pfn_to_phys (gfn); +struct hyp_page *page; +int ret; + +if (prot & ~KVM_PGTABLE_PROT_RWX ) +return -EINVAL ; + + ret =range_is_allowed_memory (phys, phys +PAGE_SIZE ); +if (ret) +return ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__guest_check_page_state_range (vcpu, ipa,PAGE_SIZE ,PKVM_NOPAGE );
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1397
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10
-0+if (ret) + goto unlock; + + page =hyp_phys_to_page (phys); +switch (page->host_state) { +case PKVM_PAGE_OWNED : +WARN_ON (__host_set_page_state_range (phys,PAGE_SIZE ,PKVM_PAGE_SHARED_OWNED )); +break ; +case PKVM_PAGE_SHARED_OWNED : +/* Only host to np-guest multi-sharing is tolerated */
Initially I thought the comment was related to the warning below, which confused me. Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the PKVM_PAGE_SHARED_OWNED is consistent with the share count.
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */
+ if (pkvm_hyp_vcpu_is_protected(vcpu))
+ return -EPERM;
That would even make the comment unnecessary.
Quoted 23 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +18
-0+WARN_ON (!page->host_share_guest_count); +break ; +default : + ret = -EPERM ; + goto unlock; + } + +WARN_ON (kvm_pgtable_stage2_map (&vm->pgt, ipa,PAGE_SIZE , phys, +pkvm_mkstate (prot,PKVM_PAGE_SHARED_BORROWED ), + &vcpu->vcpu.arch.pkvm_memcache,0 )); + page->host_share_guest_count++; + +unlock: +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0
arch/arm64/kvm/hyp/nvhe/pkvm.c int __pkvm_teardown_vm(pkvm_handle_t handle) · line 801
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4
-0@@ -795,6 +795,13 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)/* Push the metadata pages to the teardown memcache */ for (idx =0 ; idx < hyp_vm->nr_vcpus; ++idx) {struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx]; +struct kvm_hyp_memcache *vcpu_mc = &hyp_vcpu->vcpu.arch.pkvm_memcache; + +while (vcpu_mc->nr_pages) { +void *addr =pop_hyp_memcache (vcpu_mc, hyp_phys_to_virt);
nit: newline
Cheers,
/fuad
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3
-1+push_hyp_memcache (mc, addr, hyp_virt_to_phys); +unmap_donated_memory_noclear (addr,PAGE_SIZE ); + }teardown_donated_memory (mc, hyp_vcpu,sizeof (*hyp_vcpu)); } --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 38 lines · Fuad Tabba
Hi Quentin,
Quoted 35 lines · Quentin Perret
In preparation for handling guest stage-2 mappings at EL2, introduce a new pKVM hypercall allowing to share pages with non-protected guests.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/memory.h | 2 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 34 +++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 70 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 7 ++ 7 files changed, 118 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -65,6 +65,7 @@ enum __kvm_host_smccc_func {/* Hypercalls available after pKVM finalisation */ __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, + __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/include/asm/kvm_host.h +0
-0@@ -771,6 +771,9 @@ struct kvm_vcpu_arch {/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache;
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 774
Quoted 1 line
arch/arm64/include/asm/kvm_host.h +1
-0+/* Pages to be donated to pkvm/EL2 if it runs out */ Runs out of what? :) I'm being facetious, it's just that the comment is a bit unclear.
/* Pages to top-up the pKVM/EL2 guest pool */
Is that any better?
Quoted 144 lines
Quoted 144 lines
arch/arm64/include/asm/kvm_host.h +2
-0+struct kvm_hyp_memcache pkvm_memcache; +/* Virtual SError ESR to restore when HCR_EL2.VSE is set */ u64 vsesr_el2;arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -39,6 +39,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages); +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/include/nvhe/memory.h +2
-0@@ -47,6 +47,8 @@ struct hyp_page {/* Host (non-meta) state. Guarded by the host stage-2 lock. */ enum pkvm_page_state host_state : 8; + +u32 host_share_guest_count; };extern u64 __hyp_vmemmap;arch/arm64/kvm/hyp/nvhe/hyp-main.c +34
-0@@ -211,6 +211,39 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static int pkvm_refill_memcache (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; + +return refill_memcache (&hyp_vcpu->vcpu.arch.pkvm_memcache, + host_vcpu->arch.pkvm_memcache.nr_pages, + &host_vcpu->arch.pkvm_memcache); +} + +static void handle___pkvm_host_share_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, pfn, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =pkvm_refill_memcache (hyp_vcpu); +if (ret) + goto out; + + ret =__pkvm_host_share_guest (pfn, gfn, hyp_vcpu, prot); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -420,6 +453,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp), +HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +47
-0@@ -861,6 +861,27 @@ static int hyp_complete_donation(u64 addr,return pkvm_create_mappings_locked (start, end, prot); } +static enum pkvm_page_state guest_get_page_state (kvm_pte_t pte,u64 addr) +{ +if (!kvm_pte_valid (pte)) +return PKVM_NOPAGE ; + +return pkvm_getstate (kvm_pgtable_stage2_pte_prot (pte)); +} + +static int __guest_check_page_state_range (struct pkvm_hyp_vcpu *vcpu,u64 addr, +u64 size,enum pkvm_page_state state) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +struct check_walk_data d = { + .desired = state, + .get_page_state = guest_get_page_state, + }; + +hyp_assert_lock_held (&vm->lock); +return check_page_state_range (&vm->pgt, addr, size, &d); +} +static int check_share (struct pkvm_mem_share *share) {const struct pkvm_mem_transition *tx = &share->tx;@@ -1343,3 +1364,52 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)return ret; } + +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu, +enum kvm_pgtable_prot prot) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 phys =hyp_pfn_to_phys (pfn); +u64 ipa =hyp_pfn_to_phys (gfn); +struct hyp_page *page; +int ret; + +if (prot & ~KVM_PGTABLE_PROT_RWX ) +return -EINVAL ; + + ret =range_is_allowed_memory (phys, phys +PAGE_SIZE ); +if (ret) +return ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__guest_check_page_state_range (vcpu, ipa,PAGE_SIZE ,PKVM_NOPAGE ); +if (ret) + goto unlock; + + page =hyp_phys_to_page (phys); +switch (page->host_state) {
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1397
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5
-0+case PKVM_PAGE_OWNED : +WARN_ON (__host_set_page_state_range (phys,PAGE_SIZE ,PKVM_PAGE_SHARED_OWNED )); +break ; +case PKVM_PAGE_SHARED_OWNED : +/* Only host to np-guest multi-sharing is tolerated */ Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.
So, the only case where the host should ever attempt do use __pkvm_host_share_guest() on a page that is already shared is for a page already shared *with an np-guest*. The page->host_share_guest_count being elevated is the easiest way to check that the page is indeed in that state, hence the warning.
If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 8 lines
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
I would prefer not adding this here, handle___pkvm_host_share_guest() in hyp-main.c already does that for us.
Quoted 48 lines
Quoted 24 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +18
-0+WARN_ON (!page->host_share_guest_count); +break ; +default : + ret = -EPERM ; + goto unlock; + } + +WARN_ON (kvm_pgtable_stage2_map (&vm->pgt, ipa,PAGE_SIZE , phys, +pkvm_mkstate (prot,PKVM_PAGE_SHARED_BORROWED ), + &vcpu->vcpu.arch.pkvm_memcache,0 )); + page->host_share_guest_count++; + +unlock: +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0arch/arm64/kvm/hyp/nvhe/pkvm.c int __pkvm_teardown_vm(pkvm_handle_t handle) · line 801
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4
-0@@ -795,6 +795,13 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)/* Push the metadata pages to the teardown memcache */ for (idx =0 ; idx < hyp_vm->nr_vcpus; ++idx) {struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx]; +struct kvm_hyp_memcache *vcpu_mc = &hyp_vcpu->vcpu.arch.pkvm_memcache; + +while (vcpu_mc->nr_pages) { +void *addr =pop_hyp_memcache (vcpu_mc, hyp_phys_to_virt);nit: newline
Cheers,
/fuadQuoted 9 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3
-1+push_hyp_memcache (mc, addr, hyp_virt_to_phys); +unmap_donated_memory_noclear (addr,PAGE_SIZE ); + }teardown_donated_memory (mc, hyp_vcpu,sizeof (*hyp_vcpu)); } --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 224 lines · Quentin Perret
Quoted 38 lines · Fuad Tabba
Hi Quentin,
Quoted 35 lines · Quentin Perret
In preparation for handling guest stage-2 mappings at EL2, introduce a new pKVM hypercall allowing to share pages with non-protected guests.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/memory.h | 2 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 34 +++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 70 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 7 ++ 7 files changed, 118 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -65,6 +65,7 @@ enum __kvm_host_smccc_func {/* Hypercalls available after pKVM finalisation */ __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, + __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/include/asm/kvm_host.h +0
-0@@ -771,6 +771,9 @@ struct kvm_vcpu_arch {/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache;Quoted 4 lines
arch/arm64/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 774
Quoted 1 line
arch/arm64/include/asm/kvm_host.h +1
-0+/* Pages to be donated to pkvm/EL2 if it runs out */ Runs out of what? :) I'm being facetious, it's just that the comment is a bit unclear.
/* Pages to top-up the pKVM/EL2 guest pool */Is that any better?
Quoted 144 lines
Quoted 144 lines
arch/arm64/include/asm/kvm_host.h +2
-0+struct kvm_hyp_memcache pkvm_memcache; +/* Virtual SError ESR to restore when HCR_EL2.VSE is set */ u64 vsesr_el2;arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -39,6 +39,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);int __pkvm_hyp_donate_host (u64 pfn,u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages); +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/include/nvhe/memory.h +2
-0@@ -47,6 +47,8 @@ struct hyp_page {/* Host (non-meta) state. Guarded by the host stage-2 lock. */ enum pkvm_page_state host_state : 8; + +u32 host_share_guest_count; };extern u64 __hyp_vmemmap;arch/arm64/kvm/hyp/nvhe/hyp-main.c +34
-0@@ -211,6 +211,39 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static int pkvm_refill_memcache (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; + +return refill_memcache (&hyp_vcpu->vcpu.arch.pkvm_memcache, + host_vcpu->arch.pkvm_memcache.nr_pages, + &host_vcpu->arch.pkvm_memcache); +} + +static void handle___pkvm_host_share_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, pfn, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =pkvm_refill_memcache (hyp_vcpu); +if (ret) + goto out; + + ret =__pkvm_host_share_guest (pfn, gfn, hyp_vcpu, prot); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -420,6 +453,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp), +HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +47
-0@@ -861,6 +861,27 @@ static int hyp_complete_donation(u64 addr,return pkvm_create_mappings_locked (start, end, prot); } +static enum pkvm_page_state guest_get_page_state (kvm_pte_t pte,u64 addr) +{ +if (!kvm_pte_valid (pte)) +return PKVM_NOPAGE ; + +return pkvm_getstate (kvm_pgtable_stage2_pte_prot (pte)); +} + +static int __guest_check_page_state_range (struct pkvm_hyp_vcpu *vcpu,u64 addr, +u64 size,enum pkvm_page_state state) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +struct check_walk_data d = { + .desired = state, + .get_page_state = guest_get_page_state, + }; + +hyp_assert_lock_held (&vm->lock); +return check_page_state_range (&vm->pgt, addr, size, &d); +} +static int check_share (struct pkvm_mem_share *share) {const struct pkvm_mem_transition *tx = &share->tx;@@ -1343,3 +1364,52 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)return ret; } + +int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu, +enum kvm_pgtable_prot prot) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 phys =hyp_pfn_to_phys (pfn); +u64 ipa =hyp_pfn_to_phys (gfn); +struct hyp_page *page; +int ret; + +if (prot & ~KVM_PGTABLE_PROT_RWX ) +return -EINVAL ; + + ret =range_is_allowed_memory (phys, phys +PAGE_SIZE ); +if (ret) +return ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__guest_check_page_state_range (vcpu, ipa,PAGE_SIZE ,PKVM_NOPAGE ); +if (ret) + goto unlock; + + page =hyp_phys_to_page (phys); +switch (page->host_state) {Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1397
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5
-0+case PKVM_PAGE_OWNED : +WARN_ON (__host_set_page_state_range (phys,PAGE_SIZE ,PKVM_PAGE_SHARED_OWNED )); +break ; +case PKVM_PAGE_SHARED_OWNED : +/* Only host to np-guest multi-sharing is tolerated */ Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.So, the only case where the host should ever attempt do use
__pkvm_host_share_guest()on a page that is already shared is for a page already shared *with an np-guest*. Thepage->host_share_guest_countbeing elevated is the easiest way to check that the page is indeed in that state, hence the warning.If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 3 lines
I think what you should have here, which would work better with the comment, is something like:
Quoted 8 lines
Quoted 5 lines
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
I would prefer not adding this here,
handle___pkvm_host_share_guest()inhyp-main.calready does that for us.
I understand now, and I agree that an additional check isn't necessary. Could you clarify the comment though? It's the word "only" that threw me off, since to me it implied that the check was enforcing the word "only". Maybe:
Quoted 1 line
/* Tolerate host to np-guest multi-sharing. */
Thanks,
/fuad
Quoted 48 lines
Quoted 48 lines
Quoted 24 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +18
-0+WARN_ON (!page->host_share_guest_count); +break ; +default : + ret = -EPERM ; + goto unlock; + } + +WARN_ON (kvm_pgtable_stage2_map (&vm->pgt, ipa,PAGE_SIZE , phys, +pkvm_mkstate (prot,PKVM_PAGE_SHARED_BORROWED ), + &vcpu->vcpu.arch.pkvm_memcache,0 )); + page->host_share_guest_count++; + +unlock: +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0arch/arm64/kvm/hyp/nvhe/pkvm.c int __pkvm_teardown_vm(pkvm_handle_t handle) · line 801
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4
-0@@ -795,6 +795,13 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)/* Push the metadata pages to the teardown memcache */ for (idx =0 ; idx < hyp_vm->nr_vcpus; ++idx) {struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx]; +struct kvm_hyp_memcache *vcpu_mc = &hyp_vcpu->vcpu.arch.pkvm_memcache; + +while (vcpu_mc->nr_pages) { +void *addr =pop_hyp_memcache (vcpu_mc, hyp_phys_to_virt);nit: newline
Cheers,
/fuadQuoted 9 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3
-1+push_hyp_memcache (mc, addr, hyp_virt_to_phys); +unmap_donated_memory_noclear (addr,PAGE_SIZE ); + }teardown_donated_memory (mc, hyp_vcpu,sizeof (*hyp_vcpu)); } --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 32 lines · Fuad Tabba
Quoted 31 lines · Quentin Perret
Quoted 2 lines
Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.So, the only case where the host should ever attempt do use
__pkvm_host_share_guest()on a page that is already shared is for a page already shared *with an np-guest*. Thepage->host_share_guest_countbeing elevated is the easiest way to check that the page is indeed in that state, hence the warning.If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 8 lines
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
Quoted 9 lines
Quoted 2 lines
I would prefer not adding this here,
handle___pkvm_host_share_guest()inhyp-main.calready does that for us.I understand now, and I agree that an additional check isn't necessary. Could you clarify the comment though? It's the word "only" that threw me off, since to me it implied that the check was enforcing the word "only". Maybe:
Quoted 1 line
/* Tolerate host to np-guest multi-sharing. */
I guess 'only' is somewhat important, it is the _only_ type of multi-sharing that we allow and the check enforces precisely that. The WARN_ON() will be triggered for any other type of multi-sharing, so we are really checking that _only_ np-guest multi-sharing goes through.
Perhaps the confusing part is that the code as-is relies on WARN_ON() being fatal for the enforcement. Would it help if I changed the 'break' statement right after to 'fallthrough' so we proceed to return -EPERM? In practice we won't return anything as the hypervisor will panic, but I presume it is better from a logic perspective.
Cheers,
Quentin
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 49 lines · Quentin Perret
Quoted 32 lines · Fuad Tabba
Quoted 31 lines · Quentin Perret
Quoted 2 lines
Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.So, the only case where the host should ever attempt do use
__pkvm_host_share_guest()on a page that is already shared is for a page already shared *with an np-guest*. Thepage->host_share_guest_countbeing elevated is the easiest way to check that the page is indeed in that state, hence the warning.If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 8 lines
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
Quoted 9 lines
Quoted 2 lines
I would prefer not adding this here,
handle___pkvm_host_share_guest()inhyp-main.calready does that for us.I understand now, and I agree that an additional check isn't necessary. Could you clarify the comment though? It's the word "only" that threw me off, since to me it implied that the check was enforcing the word "only". Maybe:
Quoted 1 line
/* Tolerate host to np-guest multi-sharing. */I guess 'only' is somewhat important, it is the _only_ type of multi-sharing that we allow and the check enforces precisely that. The
WARN_ON()will be triggered for any other type of multi-sharing, so we are really checking that _only_ np-guest multi-sharing goes through.
Quoted 5 lines
Perhaps the confusing part is that the code as-is relies on
WARN_ON()being fatal for the enforcement. Would it help if I changed the 'break' statement right after to 'fallthrough' so we proceed to return-EPERM? In practice we won't return anything as the hypervisor will panic, but I presume it is better from a logic perspective.
It would, but then we wouldn't be tolerating np-guest multisharing, but like you said, it's not like we're tolerating it now anyway.
I wonder if it would be better simply not to allow multisharing at all for now.
Cheers,
/fuad
Quoted 2 lines
Cheers,
Quentin
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 56 lines · Fuad Tabba
Quoted 49 lines · Quentin Perret
Quoted 32 lines · Fuad Tabba
Quoted 31 lines · Quentin Perret
Quoted 2 lines
Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.So, the only case where the host should ever attempt do use
__pkvm_host_share_guest()on a page that is already shared is for a page already shared *with an np-guest*. Thepage->host_share_guest_countbeing elevated is the easiest way to check that the page is indeed in that state, hence the warning.If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 8 lines
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
Quoted 9 lines
Quoted 2 lines
I would prefer not adding this here,
handle___pkvm_host_share_guest()inhyp-main.calready does that for us.I understand now, and I agree that an additional check isn't necessary. Could you clarify the comment though? It's the word "only" that threw me off, since to me it implied that the check was enforcing the word "only". Maybe:
Quoted 1 line
/* Tolerate host to np-guest multi-sharing. */I guess 'only' is somewhat important, it is the _only_ type of multi-sharing that we allow and the check enforces precisely that. The
WARN_ON()will be triggered for any other type of multi-sharing, so we are really checking that _only_ np-guest multi-sharing goes through.Quoted 5 lines
Perhaps the confusing part is that the code as-is relies on
WARN_ON()being fatal for the enforcement. Would it help if I changed the 'break' statement right after to 'fallthrough' so we proceed to return-EPERM? In practice we won't return anything as the hypervisor will panic, but I presume it is better from a logic perspective.
Quoted 4 lines
It would, but then we wouldn't be tolerating np-guest multisharing, but like you said, it's not like we're tolerating it now anyway.
I wonder if it would be better simply not to allow multisharing at all for now.
That would mean turning off MMU notifiers in the host and taking long-term GUP pins on np-guest pages I think. Multi-sharing can be caused by many things, KSM, the zero page ... so we we'd need to turn all of that off (IOW, no MMU notifiers).
That's more or less the status quo in Android, but I vote for not going down that path upstream. pKVM should ideally be transparent for np-guest support if at all possible.
Thanks,
Quentin
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 62 lines · Quentin Perret
Quoted 56 lines · Fuad Tabba
Quoted 49 lines · Quentin Perret
Quoted 32 lines · Fuad Tabba
Quoted 31 lines · Quentin Perret
Quoted 2 lines
Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.So, the only case where the host should ever attempt do use
__pkvm_host_share_guest()on a page that is already shared is for a page already shared *with an np-guest*. Thepage->host_share_guest_countbeing elevated is the easiest way to check that the page is indeed in that state, hence the warning.If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 8 lines
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
Quoted 9 lines
Quoted 2 lines
I would prefer not adding this here,
handle___pkvm_host_share_guest()inhyp-main.calready does that for us.I understand now, and I agree that an additional check isn't necessary. Could you clarify the comment though? It's the word "only" that threw me off, since to me it implied that the check was enforcing the word "only". Maybe:
Quoted 1 line
/* Tolerate host to np-guest multi-sharing. */I guess 'only' is somewhat important, it is the _only_ type of multi-sharing that we allow and the check enforces precisely that. The
WARN_ON()will be triggered for any other type of multi-sharing, so we are really checking that _only_ np-guest multi-sharing goes through.Quoted 5 lines
Perhaps the confusing part is that the code as-is relies on
WARN_ON()being fatal for the enforcement. Would it help if I changed the 'break' statement right after to 'fallthrough' so we proceed to return-EPERM? In practice we won't return anything as the hypervisor will panic, but I presume it is better from a logic perspective.Quoted 4 lines
It would, but then we wouldn't be tolerating np-guest multisharing, but like you said, it's not like we're tolerating it now anyway.
I wonder if it would be better simply not to allow multisharing at all for now.
Quoted 8 lines
That would mean turning off MMU notifiers in the host and taking long-term GUP pins on np-guest pages I think. Multi-sharing can be caused by many things, KSM, the zero page ... so we we'd need to turn all of that off (IOW, no MMU notifiers).
That's more or less the status quo in Android, but I vote for not going down that path upstream. pKVM should ideally be transparent for np-guest support if at all possible.
And to clarify my suggestion above, we should fallthrough IFF host_share_guest_count is 0, but break otherwise to retain multi-sharing support. So it's not a simple s/break/fallthrough change, that needs a tiny bit of added logic.
Re: [PATCH v2 10/18] KVM: arm64: Introduce __pkvm_host_share_guest()
Quoted 69 lines · Quentin Perret
Quoted 67 lines · Quentin Perret
Quoted 56 lines · Fuad Tabba
Quoted 49 lines · Quentin Perret
Quoted 32 lines · Fuad Tabba
Quoted 31 lines · Quentin Perret
Quoted 2 lines
Initially I thought the comment was related to the warning below, which confused me.
It actually is about the warning below :-)
Quoted 4 lines
Now I think what you're trying to say is that we'll allow the share, and the (unrelated to the comment) warning is to ensure that the
PKVM_PAGE_SHARED_OWNEDis consistent with the share count.So, the only case where the host should ever attempt do use
__pkvm_host_share_guest()on a page that is already shared is for a page already shared *with an np-guest*. Thepage->host_share_guest_countbeing elevated is the easiest way to check that the page is indeed in that state, hence the warning.If for example the host was trying to share with an np-guest a page that is currently shared with the hypervisor, that check would fail. We can discuss whether or not we would want to allow it, but for now there is strictly no need for it so I went with the restrictive option. We can relax that constraint later if need be.
Quoted 8 lines
I think what you should have here, which would work better with the comment, is something like:
/* Only host to np-guest multi-sharing is tolerated */(quoted diff) +2
-0+ if (pkvm_hyp_vcpu_is_protected(vcpu)) + return -EPERM;That would even make the comment unnecessary.
Quoted 9 lines
Quoted 2 lines
I would prefer not adding this here,
handle___pkvm_host_share_guest()inhyp-main.calready does that for us.I understand now, and I agree that an additional check isn't necessary. Could you clarify the comment though? It's the word "only" that threw me off, since to me it implied that the check was enforcing the word "only". Maybe:
Quoted 1 line
/* Tolerate host to np-guest multi-sharing. */I guess 'only' is somewhat important, it is the _only_ type of multi-sharing that we allow and the check enforces precisely that. The
WARN_ON()will be triggered for any other type of multi-sharing, so we are really checking that _only_ np-guest multi-sharing goes through.Quoted 5 lines
Perhaps the confusing part is that the code as-is relies on
WARN_ON()being fatal for the enforcement. Would it help if I changed the 'break' statement right after to 'fallthrough' so we proceed to return-EPERM? In practice we won't return anything as the hypervisor will panic, but I presume it is better from a logic perspective.Quoted 4 lines
It would, but then we wouldn't be tolerating np-guest multisharing, but like you said, it's not like we're tolerating it now anyway.
I wonder if it would be better simply not to allow multisharing at all for now.
That would mean turning off MMU notifiers in the host and taking long-term GUP pins on np-guest pages I think. Multi-sharing can be caused by many things, KSM, the zero page ... so we we'd need to turn all of that off (IOW, no MMU notifiers).
Quoted 8 lines
Quoted 3 lines
That's more or less the status quo in Android, but I vote for not going down that path upstream. pKVM should ideally be transparent for np-guest support if at all possible.
And to clarify my suggestion above, we should fallthrough IFF
host_share_guest_countis 0, but break otherwise to retain multi-sharing support. So it's not a simple s/break/fallthrough change, that needs a tiny bit of added logic.
I think this would make things clearer. Thanks.
/fuad
PATCH v2 11/18 KVM: arm64: Introduce __pkvm_host_unshare_guest()
In reply to Quentin PerretIn preparation for letting the host unmap pages from non-protected guests, introduce a new hypercall implementing the host-unshare-guest transition.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 5 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 67 +++++++++++++++++++ 5 files changed, 98 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -66,6 +66,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -40,6 +40,7 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot); +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +5 -0
@@ -64,6 +64,11 @@ static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)return vcpu_is_protected (&hyp_vcpu->vcpu); } +static inline bool pkvm_hyp_vm_is_protected (struct pkvm_hyp_vm *hyp_vm) +{ +return kvm_vm_is_protected (&hyp_vm->kvm); +} +void pkvm_hyp_vm_table_init (void *tbl); int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
arch/arm64/kvm/hyp/nvhe/hyp-main.c +24 -0
@@ -244,6 +244,29 @@ static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_unshare_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm; + + ret =__pkvm_host_unshare_guest (gfn, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
@@ -454,6 +477,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest), +HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),
arch/arm64/kvm/hyp/nvhe/mem_protect.c +67 -0
70 lines not on this page · raw message
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 11/18] KVM: arm64: Introduce __pkvm_host_unshare_guest()
Hi Quentin,
Quoted 31 lines · Quentin Perret
In preparation for letting the host unmap pages from non-protected guests, introduce a new hypercall implementing the host-unshare-guest transition.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 5 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 67 +++++++++++++++++++ 5 files changed, 98 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -66,6 +66,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages); · line 43
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -40,6 +40,7 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot); +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);
The parameters of share_guest and unshare_guest are quite different. I think that the unshare makes more sense, that it uses the hyp_vm as opposed to the hyp_vcpu. Still, I think that one of the two should change.
Quoted 31 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/include/nvhe/pkvm.h +5
-0@@ -64,6 +64,11 @@ static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)return vcpu_is_protected (&hyp_vcpu->vcpu); } +static inline bool pkvm_hyp_vm_is_protected (struct pkvm_hyp_vm *hyp_vm) +{ +return kvm_vm_is_protected (&hyp_vm->kvm); +} +void pkvm_hyp_vm_table_init (void *tbl); int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0@@ -244,6 +244,29 @@ static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_unshare_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm;
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt) · line 261
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +10
-0+int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm;
bikeshedding: is -EINVAL the best return value, or might -EPERM be better if the VM is protected?
Quoted 24 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9
-0+ + ret =__pkvm_host_unshare_guest (gfn, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -454,6 +477,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest), +HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, · line 1417
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +2
-0@@ -1413,3 +1413,70 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,return ret; } + +static int __check_host_unshare_guest (struct pkvm_hyp_vm *vm,u64 *__phys,u64 ipa)
nit: sometimes (in this and other patches) you use vm to refer to pkvm_hyp_vm, and other times you use hyp_vm. Makes grepping/searching a bit more tricky.
Cheers,
/fuad
Quoted 68 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +65
-1+{ +enum pkvm_page_state state; +struct hyp_page *page; +kvm_pte_t pte; +u64 phys; +s8 level; +int ret; + + ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level); +if (ret) +return ret; +if (level !=KVM_PGTABLE_LAST_LEVEL ) +return -E2BIG ; +if (!kvm_pte_valid (pte)) +return -ENOENT ; + + state =guest_get_page_state (pte, ipa); +if (state !=PKVM_PAGE_SHARED_BORROWED ) +return -EPERM ; + + phys =kvm_pte_to_phys (pte); + ret =range_is_allowed_memory (phys, phys +PAGE_SIZE ); +if (WARN_ON (ret)) +return ret; + + page =hyp_phys_to_page (phys); +if (page->host_state !=PKVM_PAGE_SHARED_OWNED ) +return -EPERM ; +if (WARN_ON (!page->host_share_guest_count)) +return -EINVAL ; + + *__phys = phys; + +return 0 ; +} + +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +struct hyp_page *page; +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (hyp_vm); + + ret =__check_host_unshare_guest (hyp_vm, &phys, ipa); +if (ret) + goto unlock; + + ret =kvm_pgtable_stage2_unmap (&hyp_vm->pgt, ipa,PAGE_SIZE ); +if (ret) + goto unlock; + + page =hyp_phys_to_page (phys); + page->host_share_guest_count--; +if (!page->host_share_guest_count) +WARN_ON (__host_set_page_state_range (phys,PAGE_SIZE ,PKVM_PAGE_OWNED )); + +unlock: +guest_unlock_component (hyp_vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 11/18] KVM: arm64: Introduce __pkvm_host_unshare_guest()
Quoted 35 lines · Fuad Tabba
Hi Quentin,
Quoted 32 lines · Quentin Perret
In preparation for letting the host unmap pages from non-protected guests, introduce a new hypercall implementing the host-unshare-guest transition.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 5 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 67 +++++++++++++++++++ 5 files changed, 98 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -66,6 +66,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0@@ -40,6 +40,7 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);
Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages); · line 43
Quoted 3 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot); +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);The parameters of
share_guestandunshare_guestare quite different. I think that the unshare makes more sense, that it uses thehyp_vmas opposed to thehyp_vcpu. Still, I think that one of the two should change.
Hmm, so that is actually a bit difficult. __pkvm_host_share_guest() is guaranteed to always be called when a vCPU is loaded, and it needs to use the per-vCPU memcache so we can't just give it the pkvm_hyp_vm as is.
And on the other hand, __pkvm_host_unshare_guest() can end up being called from MMU notifier where no vCPU is loaded, so it's not clear which vCPU it should be using. We also just don't need to access per-vCPU data-structures on that path (the unmap call can only free page-table pages, which are always put back into the per-guest pool directly, not in a memcache).
Quoted 36 lines
Quoted 36 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/include/nvhe/pkvm.h +5
-0@@ -64,6 +64,11 @@ static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)return vcpu_is_protected (&hyp_vcpu->vcpu); } +static inline bool pkvm_hyp_vm_is_protected (struct pkvm_hyp_vm *hyp_vm) +{ +return kvm_vm_is_protected (&hyp_vm->kvm); +} +void pkvm_hyp_vm_table_init (void *tbl); int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,arch/arm64/kvm/hyp/nvhe/hyp-main.c +10
-0@@ -244,6 +244,29 @@ static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_unshare_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; +
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt) · line 261
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0+ hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm;bikeshedding: is
-EINVALthe best return value, or might-EPERMbe better if the VM is protected?
-EINVAL makes the code marginally simpler, especially given that we have this pattern all across hyp-main.c, so I have a minor personal preference for keeping it as-is, but no strong opinion really. This really shouldn't ever hit at run-time, modulo major bugs or a malicious host, so probably not a huge deal if EINVAL isn't particularly accurate.
Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9
-0+ + ret =__pkvm_host_unshare_guest (gfn, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -454,6 +477,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest), +HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0@@ -1413,3 +1413,70 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, · line 1417
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +2
-0return ret; } + +static int __check_host_unshare_guest (struct pkvm_hyp_vm *vm,u64 *__phys,u64 ipa)nit: sometimes (in this and other patches) you use vm to refer to
pkvm_hyp_vm, and other times you usehyp_vm. Makes grepping/searching a bit more tricky.
Ack, I'll do a pass on the series to improve the consistency.
Quoted 68 lines
Quoted 68 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +65
-1+{ +enum pkvm_page_state state; +struct hyp_page *page; +kvm_pte_t pte; +u64 phys; +s8 level; +int ret; + + ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level); +if (ret) +return ret; +if (level !=KVM_PGTABLE_LAST_LEVEL ) +return -E2BIG ; +if (!kvm_pte_valid (pte)) +return -ENOENT ; + + state =guest_get_page_state (pte, ipa); +if (state !=PKVM_PAGE_SHARED_BORROWED ) +return -EPERM ; + + phys =kvm_pte_to_phys (pte); + ret =range_is_allowed_memory (phys, phys +PAGE_SIZE ); +if (WARN_ON (ret)) +return ret; + + page =hyp_phys_to_page (phys); +if (page->host_state !=PKVM_PAGE_SHARED_OWNED ) +return -EPERM ; +if (WARN_ON (!page->host_share_guest_count)) +return -EINVAL ; + + *__phys = phys; + +return 0 ; +} + +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +struct hyp_page *page; +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (hyp_vm); + + ret =__check_host_unshare_guest (hyp_vm, &phys, ipa); +if (ret) + goto unlock; + + ret =kvm_pgtable_stage2_unmap (&hyp_vm->pgt, ipa,PAGE_SIZE ); +if (ret) + goto unlock; + + page =hyp_phys_to_page (phys); + page->host_share_guest_count--; +if (!page->host_share_guest_count) +WARN_ON (__host_set_page_state_range (phys,PAGE_SIZE ,PKVM_PAGE_OWNED )); + +unlock: +guest_unlock_component (hyp_vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 11/18] KVM: arm64: Introduce __pkvm_host_unshare_guest()
Hi Quentin,
Quoted 51 lines · Quentin Perret
Quoted 35 lines · Fuad Tabba
Hi Quentin,
Quoted 32 lines · Quentin Perret
In preparation for letting the host unmap pages from non-protected guests, introduce a new hypercall implementing the host-unshare-guest transition.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 5 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 67 +++++++++++++++++++ 5 files changed, 98 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -66,6 +66,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0@@ -40,6 +40,7 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);int __pkvm_host_share_ffa (u64 pfn,u64 nr_pages);Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages); · line 43
Quoted 3 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot); +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);The parameters of
share_guestandunshare_guestare quite different. I think that the unshare makes more sense, that it uses thehyp_vmas opposed to thehyp_vcpu. Still, I think that one of the two should change.Hmm, so that is actually a bit difficult.
__pkvm_host_share_guest()is guaranteed to always be called when a vCPU is loaded, and it needs to use the per-vCPU memcache so we can't just give it thepkvm_hyp_vmas is.
Quoted 6 lines
And on the other hand,
__pkvm_host_unshare_guest()can end up being called from MMU notifier where no vCPU is loaded, so it's not clear which vCPU it should be using. We also just don't need to access per-vCPU data-structures on that path (the unmap call can only free page-table pages, which are always put back into the per-guest pool directly, not in a memcache).
I understand. That makes sense.
Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/include/nvhe/pkvm.h +5
-0@@ -64,6 +64,11 @@ static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)return vcpu_is_protected (&hyp_vcpu->vcpu); } +static inline bool pkvm_hyp_vm_is_protected (struct pkvm_hyp_vm *hyp_vm) +{ +return kvm_vm_is_protected (&hyp_vm->kvm); +} +void pkvm_hyp_vm_table_init (void *tbl); int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,arch/arm64/kvm/hyp/nvhe/hyp-main.c +10
-0@@ -244,6 +244,29 @@ static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_unshare_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; +Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_host_share_guest(struct kvm_cpu_context *host_ctxt) · line 261
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0+ hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm;bikeshedding: is
-EINVALthe best return value, or might-EPERMbe better if the VM is protected?
Quoted 5 lines
-EINVALmakes the code marginally simpler, especially given that we have this pattern all acrosshyp-main.c, so I have a minor personal preference for keeping it as-is, but no strong opinion really. This really shouldn't ever hit at run-time, modulo major bugs or a malicious host, so probably not a huge deal if EINVAL isn't particularly accurate.
That's fine.
Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9
-0+ + ret =__pkvm_host_unshare_guest (gfn, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -454,6 +477,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest), +HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0@@ -1413,3 +1413,70 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, · line 1417
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +2
-0return ret; } + +static int __check_host_unshare_guest (struct pkvm_hyp_vm *vm,u64 *__phys,u64 ipa)nit: sometimes (in this and other patches) you use vm to refer to
pkvm_hyp_vm, and other times you usehyp_vm. Makes grepping/searching a bit more tricky.
Quoted 1 line
Ack, I'll do a pass on the series to improve the consistency.
Thanks!
/fuad
Quoted 68 lines
Quoted 68 lines
Quoted 68 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +65
-1+{ +enum pkvm_page_state state; +struct hyp_page *page; +kvm_pte_t pte; +u64 phys; +s8 level; +int ret; + + ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level); +if (ret) +return ret; +if (level !=KVM_PGTABLE_LAST_LEVEL ) +return -E2BIG ; +if (!kvm_pte_valid (pte)) +return -ENOENT ; + + state =guest_get_page_state (pte, ipa); +if (state !=PKVM_PAGE_SHARED_BORROWED ) +return -EPERM ; + + phys =kvm_pte_to_phys (pte); + ret =range_is_allowed_memory (phys, phys +PAGE_SIZE ); +if (WARN_ON (ret)) +return ret; + + page =hyp_phys_to_page (phys); +if (page->host_state !=PKVM_PAGE_SHARED_OWNED ) +return -EPERM ; +if (WARN_ON (!page->host_share_guest_count)) +return -EINVAL ; + + *__phys = phys; + +return 0 ; +} + +int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +struct hyp_page *page; +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (hyp_vm); + + ret =__check_host_unshare_guest (hyp_vm, &phys, ipa); +if (ret) + goto unlock; + + ret =kvm_pgtable_stage2_unmap (&hyp_vm->pgt, ipa,PAGE_SIZE ); +if (ret) + goto unlock; + + page =hyp_phys_to_page (phys); + page->host_share_guest_count--; +if (!page->host_share_guest_count) +WARN_ON (__host_set_page_state_range (phys,PAGE_SIZE ,PKVM_PAGE_OWNED )); + +unlock: +guest_unlock_component (hyp_vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
PATCH v2 12/18 KVM: arm64: Introduce __pkvm_host_relax_guest_perms()
In reply to Quentin PerretIntroduce a new hypercall allowing the host to relax the stage-2 permissions of mappings in a non-protected guest page-table. It will be used later once we start allowing RO memslots and dirty logging.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 20 ++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 23 +++++++++++++++++++ 4 files changed, 45 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -67,6 +67,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -41,6 +41,7 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm); +int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +20 -0
@@ -267,6 +267,25 @@ static void handle___pkvm_host_unshare_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_relax_guest_perms (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, gfn, host_ctxt,1 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 2); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =__pkvm_host_relax_guest_perms (gfn, prot, hyp_vcpu); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
@@ -478,6 +497,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__pkvm_host_unshare_guest), +HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),
arch/arm64/kvm/hyp/nvhe/mem_protect.c +23 -0
@@ -1480,3 +1480,26 @@ int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm)return ret; } + +int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +if ((prot &KVM_PGTABLE_PROT_RWX ) != prot) +return -EPERM ; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_relax_perms (&vm->pgt, ipa, prot,0 ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 12/18] KVM: arm64: Introduce __pkvm_host_relax_guest_perms()
Hi Quentin,
Quoted 30 lines · Quentin Perret
Introduce a new hypercall allowing the host to relax the stage-2 permissions of mappings in a non-protected guest page-table. It will be used later once we start allowing RO memslots and dirty logging.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 20 ++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 23 +++++++++++++++++++ 4 files changed, 45 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -67,6 +67,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages); · line 44
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -41,6 +41,7 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm); +int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);
The parameters are the same as __pkvm_host_share_guest, but in a different order. I looked ahead at later patches in the series, and similar issues regarding parameter type and ordering, so I won't mention it for the later patches.
Quoted 48 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/nvhe/hyp-main.c +20
-0@@ -267,6 +267,25 @@ static void handle___pkvm_host_unshare_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_relax_guest_perms (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, gfn, host_ctxt,1 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 2); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =__pkvm_host_relax_guest_perms (gfn, prot, hyp_vcpu); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -478,6 +497,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__pkvm_host_unshare_guest), +HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0@@ -1480,3 +1480,26 @@ int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm)return ret; }
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm) · line 1492
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10
-0+ +int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +if ((prot &KVM_PGTABLE_PROT_RWX ) != prot) +return -EPERM ;
Why not
+ if (prot & ~KVM_PGTABLE_PROT_RWX)
Simpler and consistent with similar checks in the file (e.g., __pkvm_host_share_guest)
Cheers,
/fuad
Quoted 16 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +13
-1+ +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_relax_perms (&vm->pgt, ipa, prot,0 ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 12/18] KVM: arm64: Introduce __pkvm_host_relax_guest_perms()
Quoted 34 lines · Fuad Tabba
Hi Quentin,
Quoted 31 lines · Quentin Perret
Introduce a new hypercall allowing the host to relax the stage-2 permissions of mappings in a non-protected guest page-table. It will be used later once we start allowing RO memslots and dirty logging.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 20 ++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 23 +++++++++++++++++++ 4 files changed, 45 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -67,6 +67,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0@@ -41,6 +41,7 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);int __pkvm_host_unshare_ffa (u64 pfn,u64 nr_pages);
Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages); · line 44
Quoted 3 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm); +int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);The parameters are the same as
__pkvm_host_share_guest, but in a different order. I looked ahead at later patches in the series, and similar issues regarding parameter type and ordering, so I won't mention it for the later patches.
Ack to this and the other comment below, thanks for the review!
Quoted 87 lines
Quoted 49 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/nvhe/hyp-main.c +20
-0@@ -267,6 +267,25 @@ static void handle___pkvm_host_unshare_guest(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_relax_guest_perms (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, gfn, host_ctxt,1 ); + DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 2); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =__pkvm_host_relax_guest_perms (gfn, prot, hyp_vcpu); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -478,6 +497,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_unshare_hyp),HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__pkvm_host_unshare_guest), +HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0@@ -1480,3 +1480,26 @@ int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm)return ret; }arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm) · line 1492
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10
-0+ +int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +if ((prot &KVM_PGTABLE_PROT_RWX ) != prot) +return -EPERM ;Why not
+ if (prot & ~KVM_PGTABLE_PROT_RWX)
Simpler and consistent with similar checks in the file (e.g.,
__pkvm_host_share_guest)Cheers,
/fuadQuoted 16 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +13
-1+ +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_relax_perms (&vm->pgt, ipa, prot,0 ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
PATCH v2 13/18 KVM: arm64: Introduce __pkvm_host_wrprotect_guest()
In reply to Quentin PerretIntroduce a new hypercall to remove the write permission from a non-protected guest stage-2 mapping. This will be used for e.g. enabling dirty logging.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 19 +++++++++++++++ 4 files changed, 45 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -68,6 +68,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, + __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -42,6 +42,7 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu); +int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +24 -0
@@ -286,6 +286,29 @@ static void handle___pkvm_host_relax_guest_perms(struct kvm_cpu_context *host_ctcpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_wrprotect_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm; + + ret =__pkvm_host_wrprotect_guest (gfn, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
@@ -498,6 +521,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__pkvm_host_relax_guest_perms), +HANDLE_FUNC (__pkvm_host_wrprotect_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),
arch/arm64/kvm/hyp/nvhe/mem_protect.c +19 -0
@@ -1503,3 +1503,22 @@ int __pkvm_host_relax_guest_perms(u64 gfn, enum kvm_pgtable_prot prot, struct pkreturn ret; } + +int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_wrprotect (&vm->pgt, ipa,PAGE_SIZE ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 13/18] KVM: arm64: Introduce __pkvm_host_wrprotect_guest()
Hi Quentin,
Quoted 50 lines · Quentin Perret
Introduce a new hypercall to remove the write permission from a non-protected guest stage-2 mapping. This will be used for e.g. enabling dirty logging.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 19 +++++++++++++++ 4 files changed, 45 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -68,6 +68,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, + __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -42,6 +42,7 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);int __pkvm_host_share_guest (u64 pfn,u64 gfn,struct pkvm_hyp_vcpu *vcpu,enum kvm_pgtable_prot prot);int __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu); +int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0@@ -286,6 +286,29 @@ static void handle___pkvm_host_relax_guest_perms(struct kvm_cpu_context *host_ctcpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_wrprotect_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm;
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_host_relax_guest_perms(struct kvm_cpu_context *host_ct · line 303
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +10
-0+int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm;
These checks are (unsurprisingly) the same for all these functions. Does it make sense to have a helper do these checks?
Cheers,
/fuad
Quoted 48 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +8
-0+ ret =__pkvm_host_wrprotect_guest (gfn, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -498,6 +521,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_guest),HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__pkvm_host_relax_guest_perms), +HANDLE_FUNC (__pkvm_host_wrprotect_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +19
-1@@ -1503,3 +1503,22 @@ int __pkvm_host_relax_guest_perms(u64 gfn, enum kvm_pgtable_prot prot, struct pkreturn ret; } + +int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_wrprotect (&vm->pgt, ipa,PAGE_SIZE ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 13/18] KVM: arm64: Introduce __pkvm_host_wrprotect_guest()
Quoted 10 lines · Fuad Tabba
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +10
-0+static void handle___pkvm_host_wrprotect_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; +
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_host_relax_guest_perms(struct kvm_cpu_context *host_ct · line 303
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0+ hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm;These checks are (unsurprisingly) the same for all these functions. Does it make sense to have a helper do these checks?
Yup, that makes sense and should simplify the error handling on all the call sites. I'll probably call that get_np_pkvm_hyp_vm() or something along those lines and shove in pkvm.c in v3.
PATCH v2 14/18 KVM: arm64: Introduce __pkvm_host_test_clear_young_guest()
In reply to Quentin PerretPlumb the kvm_stage2_test_clear_young() callback into pKVM for non-protected guest. It will be later be called from MMU notifiers.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 25 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 19 ++++++++++++++ 4 files changed, 46 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -69,6 +69,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_test_clear_young_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -43,6 +43,7 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kint __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm); +int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +25 -0
@@ -309,6 +309,30 @@ static void handle___pkvm_host_wrprotect_guest(struct kvm_cpu_context *host_ctxtcpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_test_clear_young_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +DECLARE_REG (bool, mkold, host_ctxt,3 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm; + + ret =__pkvm_host_test_clear_young_guest (gfn, mkold, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
@@ -522,6 +546,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__pkvm_host_wrprotect_guest), +HANDLE_FUNC (__pkvm_host_test_clear_young_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),
arch/arm64/kvm/hyp/nvhe/mem_protect.c +19 -0
@@ -1522,3 +1522,22 @@ int __pkvm_host_wrprotect_guest(u64 gfn, struct pkvm_hyp_vm *vm)return ret; } + +int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_test_clear_young (&vm->pgt, ipa,PAGE_SIZE , mkold); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 14/18] KVM: arm64: Introduce __pkvm_host_test_clear_young_guest()
Hi Quentin,
Quoted 29 lines · Quentin Perret
Plumb the
kvm_stage2_test_clear_young()callback into pKVM for non-protected guest. It will be later be called from MMU notifiers.Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 25 +++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 19 ++++++++++++++ 4 files changed, 46 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -69,6 +69,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_test_clear_young_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum k · line 46
Quoted 4 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -43,6 +43,7 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kint __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm); +int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm);
While I'm piling on the function names/parameters, some functions have _guest as a postfix at the end (e.g., this one), others have it in the middle (__pkvm_host_relax_guest_perms). I guess __pkvm_host_relax_guest_perms is the odd one out. Could you rename it?
Cheers,
/fuad
Quoted 75 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/nvhe/hyp-main.c +25
-0@@ -309,6 +309,30 @@ static void handle___pkvm_host_wrprotect_guest(struct kvm_cpu_context *host_ctxtcpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_test_clear_young_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +DECLARE_REG (u64, gfn, host_ctxt,2 ); +DECLARE_REG (bool, mkold, host_ctxt,3 ); +struct pkvm_hyp_vm *hyp_vm; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) + goto out; +if (pkvm_hyp_vm_is_protected (hyp_vm)) + goto put_hyp_vm; + + ret =__pkvm_host_test_clear_young_guest (gfn, mkold, hyp_vm); +put_hyp_vm: +put_pkvm_hyp_vm (hyp_vm); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -522,6 +546,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_unshare_guest),HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__pkvm_host_wrprotect_guest), +HANDLE_FUNC (__pkvm_host_test_clear_young_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +19
-1@@ -1522,3 +1522,22 @@ int __pkvm_host_wrprotect_guest(u64 gfn, struct pkvm_hyp_vm *vm)return ret; } + +int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm) +{ +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) + ret =kvm_pgtable_stage2_test_clear_young (&vm->pgt, ipa,PAGE_SIZE , mkold); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 14/18] KVM: arm64: Introduce __pkvm_host_test_clear_young_guest()
Quoted 6 lines · Fuad Tabba
Quoted 6 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +0
-0@@ -43,6 +43,7 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kint __pkvm_host_unshare_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);
Quoted 8 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum k · line 46
Quoted 3 lines
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm); +int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm);While I'm piling on the function names/parameters, some functions have
_guestas a postfix at the end (e.g., this one), others have it in the middle (__pkvm_host_relax_guest_perms). I guess__pkvm_host_relax_guest_permsis the odd one out. Could you rename it?
Right, 'relax_guest_perms' felt more natural to me, but consistency should take precedence, so I'll rename :-)
PATCH v2 15/18 KVM: arm64: Introduce __pkvm_host_mkyoung_guest()
In reply to Quentin PerretPlumb the kvm_pgtable_stage2_mkyoung() callback into pKVM for non-protected guests. It will be called later from the fault handling path.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 19 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 20 +++++++++++++++++++ 4 files changed, 41 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -70,6 +70,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_test_clear_young_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_mkyoung_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1 -0
@@ -44,6 +44,7 @@ int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm); +int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +19 -0
@@ -333,6 +333,24 @@ static void handle___pkvm_host_test_clear_young_guest(struct kvm_cpu_context *hocpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_mkyoung_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, gfn, host_ctxt,1 ); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =__pkvm_host_mkyoung_guest (gfn, hyp_vcpu); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
@@ -547,6 +565,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__pkvm_host_wrprotect_guest),HANDLE_FUNC (__pkvm_host_test_clear_young_guest), +HANDLE_FUNC (__pkvm_host_mkyoung_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),
arch/arm64/kvm/hyp/nvhe/mem_protect.c +20 -0
@@ -1541,3 +1541,23 @@ int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm *return ret; } + +int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa); +if (!ret) +kvm_pgtable_stage2_mkyoung (&vm->pgt, ipa,0 ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +}
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 15/18] KVM: arm64: Introduce __pkvm_host_mkyoung_guest()
Hi Quentin,
Quoted 84 lines · Quentin Perret
Plumb the
kvm_pgtable_stage2_mkyoung()callback into pKVM for non-protected guests. It will be called later from the fault handling path.Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 19 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 20 +++++++++++++++++++ 4 files changed, 41 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -70,6 +70,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_guest_perms, __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, __KVM_HOST_SMCCC_FUNC___pkvm_host_test_clear_young_guest, + __KVM_HOST_SMCCC_FUNC___pkvm_host_mkyoung_guest, __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context,arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +1
-0@@ -44,6 +44,7 @@ int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_relax_guest_perms (u64 gfn,enum kvm_pgtable_prot prot,struct pkvm_hyp_vcpu *vcpu);int __pkvm_host_wrprotect_guest (u64 gfn,struct pkvm_hyp_vm *hyp_vm);int __pkvm_host_test_clear_young_guest (u64 gfn,bool mkold,struct pkvm_hyp_vm *vm); +int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu);bool addr_is_memory (phys_addr_t phys);int host_stage2_idmap_locked (phys_addr_t addr,u64 size,enum kvm_pgtable_prot prot);arch/arm64/kvm/hyp/nvhe/hyp-main.c +19
-0@@ -333,6 +333,24 @@ static void handle___pkvm_host_test_clear_young_guest(struct kvm_cpu_context *hocpu_reg (host_ctxt,1 ) = ret; } +static void handle___pkvm_host_mkyoung_guest (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u64, gfn, host_ctxt,1 ); +struct pkvm_hyp_vcpu *hyp_vcpu; +int ret = -EINVAL ; + +if (!is_protected_kvm_enabled ()) + goto out; + + hyp_vcpu =pkvm_get_loaded_hyp_vcpu (); +if (!hyp_vcpu ||pkvm_hyp_vcpu_is_protected (hyp_vcpu)) + goto out; + + ret =__pkvm_host_mkyoung_guest (gfn, hyp_vcpu); +out: +cpu_reg (host_ctxt,1 ) = ret; +} +static void handle___kvm_adjust_pc (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);@@ -547,6 +565,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_relax_guest_perms),HANDLE_FUNC (__pkvm_host_wrprotect_guest),HANDLE_FUNC (__pkvm_host_test_clear_young_guest), +HANDLE_FUNC (__pkvm_host_mkyoung_guest),HANDLE_FUNC (__kvm_adjust_pc),HANDLE_FUNC (__kvm_vcpu_run), HANDLE_FUNC(__kvm_flush_vm_context),arch/arm64/kvm/hyp/nvhe/mem_protect.c +2
-0@@ -1541,3 +1541,23 @@ int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm *return ret; } + +int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu)
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm * · line 1555
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10
-0+{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa);
While I'm bikeshedding some more, does the name __check_host_unshare_guest() make sense? Should it be something like __check_host_changeperm_guest(), or something along those lines? (feel free to ignore this :) )
Thanks,
/fuad
Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8
-1+if (!ret) +kvm_pgtable_stage2_mkyoung (&vm->pgt, ipa,0 ); + +guest_unlock_component (vm); +host_unlock_component (); + +return ret; +} --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 15/18] KVM: arm64: Introduce __pkvm_host_mkyoung_guest()
Quoted 8 lines · Fuad Tabba
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8
-0+int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component ();
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm * · line 1555
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-0+guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa);While I'm bikeshedding some more, does the name
__check_host_unshare_guest()make sense? Should it be something like__check_host_changeperm_guest(), or something along those lines? (feel free to ignore this :) )
I understand the comment, but not a huge fan of 'changeperm' as that sounds like we're only allowing permission changes while we use this all over the place. Maybe __check_host_is_shared_guest()? Naming is hard, so happy to take suggestions :-)
Re: [PATCH v2 15/18] KVM: arm64: Introduce __pkvm_host_mkyoung_guest()
Hi Quentin,
Quoted 19 lines · Quentin Perret
Quoted 8 lines · Fuad Tabba
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +8
-0+int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component ();Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm * · line 1555
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-0+guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa);While I'm bikeshedding some more, does the name
__check_host_unshare_guest()make sense? Should it be something like__check_host_changeperm_guest(), or something along those lines? (feel free to ignore this :) )
Quoted 4 lines
I understand the comment, but not a huge fan of 'changeperm' as that sounds like we're only allowing permission changes while we use this all over the place. Maybe
__check_host_is_shared_guest()? Naming is hard, so happy to take suggestions :-)
I've gone and done it now :) I almost like that, it's the *is* part I don't like since it implied a boolean return. Maybe just __check_host_shared_guest(), no is?
Cheers,
/fuad
Re: [PATCH v2 15/18] KVM: arm64: Introduce __pkvm_host_mkyoung_guest()
Quoted 22 lines · Fuad Tabba
Hi Quentin,
Quoted 19 lines · Quentin Perret
Quoted 16 lines · Fuad Tabba
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hyp_vm * · line 1555
Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +11
-0+int __pkvm_host_mkyoung_guest (u64 gfn,struct pkvm_hyp_vcpu *vcpu) +{ +struct pkvm_hyp_vm *vm =pkvm_hyp_vcpu_to_hyp_vm (vcpu); +u64 ipa =hyp_pfn_to_phys (gfn); +u64 phys; +int ret; + +host_lock_component (); +guest_lock_component (vm); + + ret =__check_host_unshare_guest (vm, &phys, ipa);While I'm bikeshedding some more, does the name
__check_host_unshare_guest()make sense? Should it be something like__check_host_changeperm_guest(), or something along those lines? (feel free to ignore this :) )
Quoted 8 lines
Quoted 4 lines
I understand the comment, but not a huge fan of 'changeperm' as that sounds like we're only allowing permission changes while we use this all over the place. Maybe
__check_host_is_shared_guest()? Naming is hard, so happy to take suggestions :-)I've gone and done it now :) I almost like that, it's the *is* part I don't like since it implied a boolean return. Maybe just
__check_host_shared_guest(), no is?
Deal!
Cheers,
Quentin
PATCH v2 16/18 KVM: arm64: Introduce __pkvm_tlb_flush_vmid()
In reply to Quentin PerretIntroduce a new hypercall to flush the TLBs of non-protected guests. The host kernel will be responsible for issuing this hypercall after changing stage-2 permissions using the __pkvm_host_relax_guest_perms() or __pkvm_host_wrprotect_guest() paths. This is left under the host's responsibility for performance reasons.
Note however that the TLB maintenance for all *unmap* operations still remains entirely under the hypervisor's responsibility for security reasons -- an unmapped page may be donated to another entity, so a stale TLB entry could be used to leak private data.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -87,6 +87,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, + __KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid, };#define DECLARE_KVM_VHE_SYM (sym) extern char sym[]
arch/arm64/kvm/hyp/nvhe/hyp-main.c +17 -0
@@ -398,6 +398,22 @@ static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)__kvm_tlb_flush_vmid (kern_hyp_va (mmu)); } +static void handle___pkvm_tlb_flush_vmid (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +struct pkvm_hyp_vm *hyp_vm; + +if (!is_protected_kvm_enabled ()) +return ; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) +return ; + +__kvm_tlb_flush_vmid (&hyp_vm->kvm.arch.mmu); +put_pkvm_hyp_vm (hyp_vm); +} +static void handle___kvm_flush_cpu_context (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
@@ -582,6 +598,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_teardown_vm),HANDLE_FUNC (__pkvm_vcpu_load),HANDLE_FUNC (__pkvm_vcpu_put), + HANDLE_FUNC(__pkvm_tlb_flush_vmid), };static void handle_host_hcall (struct kvm_cpu_context *host_ctxt)
Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH v2 16/18] KVM: arm64: Introduce __pkvm_tlb_flush_vmid()
Hi Quentin,
Quoted 43 lines · Quentin Perret
Introduce a new hypercall to flush the TLBs of non-protected guests. The host kernel will be responsible for issuing this hypercall after changing stage-2 permissions using the
__pkvm_host_relax_guest_perms()or__pkvm_host_wrprotect_guest()paths. This is left under the host's responsibility for performance reasons.Note however that the TLB maintenance for all *unmap* operations still remains entirely under the hypervisor's responsibility for security reasons -- an unmapped page may be donated to another entity, so a stale TLB entry could be used to leak private data.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -87,6 +87,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, + __KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid, };#define DECLARE_KVM_VHE_SYM (sym) extern char sym[]arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0@@ -398,6 +398,22 @@ static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)__kvm_tlb_flush_vmid (kern_hyp_va (mmu)); } +static void handle___pkvm_tlb_flush_vmid (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +struct pkvm_hyp_vm *hyp_vm; +
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt) · line 415
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +10
-0+if (!is_protected_kvm_enabled ()) +return ; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) +return ; + +__kvm_tlb_flush_vmid (&hyp_vm->kvm.arch.mmu); +put_pkvm_hyp_vm (hyp_vm); +}
Since this is practically the same as kvm_tlb_flush_vmid(), does it make sense to modify that instead (handle___kvm_tlb_flush_vmid()) to do the right thing depending on whether pkvm is enabled? Thinking as well for the future in case we want to support the rest of the kvm_tlb_flush_vmid_*().
Cheers,
/fuad
Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2
-1+static void handle___kvm_flush_cpu_context (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);@@ -582,6 +598,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_teardown_vm),HANDLE_FUNC (__pkvm_vcpu_load),HANDLE_FUNC (__pkvm_vcpu_put), + HANDLE_FUNC(__pkvm_tlb_flush_vmid), };static void handle_host_hcall (struct kvm_cpu_context *host_ctxt) --2.47.0.338.g60cca15819-goog
Re: [PATCH v2 16/18] KVM: arm64: Introduce __pkvm_tlb_flush_vmid()
Quoted 54 lines · Fuad Tabba
Hi Quentin,
Quoted 51 lines · Quentin Perret
Introduce a new hypercall to flush the TLBs of non-protected guests. The host kernel will be responsible for issuing this hypercall after changing stage-2 permissions using the
__pkvm_host_relax_guest_perms()or__pkvm_host_wrprotect_guest()paths. This is left under the host's responsibility for performance reasons.Note however that the TLB maintenance for all *unmap* operations still remains entirely under the hypervisor's responsibility for security reasons -- an unmapped page may be donated to another entity, so a stale TLB entry could be used to leak private data.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -87,6 +87,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, + __KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid, };#define DECLARE_KVM_VHE_SYM (sym) extern char sym[]arch/arm64/kvm/hyp/nvhe/hyp-main.c +13
-0@@ -398,6 +398,22 @@ static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)__kvm_tlb_flush_vmid (kern_hyp_va (mmu)); } +static void handle___pkvm_tlb_flush_vmid (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +struct pkvm_hyp_vm *hyp_vm; + +if (!is_protected_kvm_enabled ()) +return ; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) +return ; + +__kvm_tlb_flush_vmid (&hyp_vm->kvm.arch.mmu);
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt) · line 415
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2
-0+put_pkvm_hyp_vm (hyp_vm); +}Since this is practically the same as
kvm_tlb_flush_vmid(), does it make sense to modify that instead (handle___kvm_tlb_flush_vmid()) to do the right thing depending on whether pkvm is enabled? Thinking as well for the future in case we want to support the rest of the kvm_tlb_flush_vmid_*().
I considered it, but the two implementations want different arguments -- pkvm wants the handle while standard KVM uses the kvm struct address directly. I had an implementation at some point that multiplexed the implementations on a single HVC (we'd interpret the arguments differently depending on pKVM being enabled or not) but that felt more error prone than simply having two HVCs.
Happy to reconsider if we can find a good way to make it work though.
Re: [PATCH v2 16/18] KVM: arm64: Introduce __pkvm_tlb_flush_vmid()
Hi Quentin,
Quoted 65 lines · Quentin Perret
Quoted 54 lines · Fuad Tabba
Hi Quentin,
Quoted 51 lines · Quentin Perret
Introduce a new hypercall to flush the TLBs of non-protected guests. The host kernel will be responsible for issuing this hypercall after changing stage-2 permissions using the
__pkvm_host_relax_guest_perms()or__pkvm_host_wrprotect_guest()paths. This is left under the host's responsibility for performance reasons.Note however that the TLB maintenance for all *unmap* operations still remains entirely under the hypervisor's responsibility for security reasons -- an unmapped page may be donated to another entity, so a stale TLB entry could be used to leak private data.
Signed-off-by Quentin Perret <[email protected]>--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+)arch/arm64/include/asm/kvm_asm.h +1
-0@@ -87,6 +87,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___pkvm_teardown_vm, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, + __KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid, };#define DECLARE_KVM_VHE_SYM (sym) extern char sym[]arch/arm64/kvm/hyp/nvhe/hyp-main.c +13
-0@@ -398,6 +398,22 @@ static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)__kvm_tlb_flush_vmid (kern_hyp_va (mmu)); } +static void handle___pkvm_tlb_flush_vmid (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (pkvm_handle_t, handle, host_ctxt,1 ); +struct pkvm_hyp_vm *hyp_vm; + +if (!is_protected_kvm_enabled ()) +return ; + + hyp_vm =get_pkvm_hyp_vm (handle); +if (!hyp_vm) +return ; + +__kvm_tlb_flush_vmid (&hyp_vm->kvm.arch.mmu);Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt) · line 415
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2
-0+put_pkvm_hyp_vm (hyp_vm); +}Since this is practically the same as
kvm_tlb_flush_vmid(), does it make sense to modify that instead (handle___kvm_tlb_flush_vmid()) to do the right thing depending on whether pkvm is enabled? Thinking as well for the future in case we want to support the rest of the kvm_tlb_flush_vmid_*().
Quoted 8 lines
I considered it, but the two implementations want different arguments -- pkvm wants the handle while standard KVM uses the kvm struct address directly. I had an implementation at some point that multiplexed the implementations on a single HVC (we'd interpret the arguments differently depending on pKVM being enabled or not) but that felt more error prone than simply having two HVCs.
Happy to reconsider if we can find a good way to make it work though.
I don't have a strong opinion about this. I think that for now, since it's only this function, it's probably fine. That said, the multiplexing is (as of patch 18, which I haven't reviewed yet) is just lifted higher up to the host kernel, albeit with fewer parameters to wiggle around.
To summarize, I think we can worry about it if/once we need the other tlb_flush_* variants.
Cheers,
/fuad
PATCH v2 17/18 KVM: arm64: Introduce the EL1 pKVM MMU
In reply to Quentin PerretIntroduce a set of helper functions allowing to manipulate the pKVM guest stage-2 page-tables from EL1 using pKVM's HVC interface.
Each helper has an exact one-to-one correspondance with the traditional kvm_pgtable_stage2_*() functions from pgtable.c, with a strictly matching prototype. This will ease plumbing later on in mmu.c.
These callbacks track the gfn->pfn mappings in a simple rb_tree indexed by IPA in lieu of a page-table. This rb-tree is kept in sync with pKVM's state and is protected by a new rwlock -- the existing mmu_lock protection does not suffice in the map() path where the tree must be modified while user_mem_abort() only acquires a read_lock.
---
The embedded union inside struct kvm_pgtable is arguably a bit horrible currently... I considered making the pgt argument to all kvm_pgtable_*() functions an opaque void * ptr, and moving the definition of struct kvm_pgtable to pgtable.c and the pkvm version into pkvm.c. Given that the allocation of that data-structure is done by the caller, that means we'd need to expose kvm_pgtable_get_pgd_size() or something that each MMU (pgtable.c and pkvm.c) would have to implement and things like that. But that felt like a bigger surgery, so I went with the simpler option. Thoughts welcome :-)
Similarly, happy to drop the mappings_lock if we want to teach user_mem_abort() about taking a write lock on the mmu_lock in the pKVM case, but again this implementation is the least invasive into normal KVM so that felt like a reasonable starting point.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_pgtable.h | 27 ++-- arch/arm64/include/asm/kvm_pkvm.h | 28 ++++ arch/arm64/kvm/pkvm.c | 195 +++++++++++++++++++++++++++ 4 files changed, 242 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -85,6 +85,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);struct kvm_hyp_memcache {phys_addr_t head;unsigned long nr_pages; +struct pkvm_mapping *mapping;/* only used from EL1 */ }; static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,
arch/arm64/include/asm/kvm_pgtable.h +18 -9
@@ -412,15 +412,24 @@ static inline bool kvm_pgtable_walk_lock_held(void)* be used instead of block mappings. */ struct kvm_pgtable { -u32 ia_bits; -s8 start_level; -kvm_pteref_t pgd; -struct kvm_pgtable_mm_ops *mm_ops; - -/* Stage-2 only */ -struct kvm_s2_mmu *mmu; -enum kvm_pgtable_stage2_flags flags; -kvm_pgtable_force_pte_cb_t force_pte_cb; +union { +struct { +u32 ia_bits; +s8 start_level; +kvm_pteref_t pgd; +struct kvm_pgtable_mm_ops *mm_ops; + +/* Stage-2 only */ +struct kvm_s2_mmu *mmu; +enum kvm_pgtable_stage2_flags flags; +kvm_pgtable_force_pte_cb_t force_pte_cb; + }; +struct { +struct kvm *kvm; +struct rb_root mappings; +rwlock_t mappings_lock; + } pkvm; + }; };/**
arch/arm64/include/asm/kvm_pkvm.h +28 -0
@@ -11,6 +11,12 @@#include <linux/scatterlist.h> #include <asm/kvm_pgtable.h> +struct pkvm_mapping { +u64 gfn; +u64 pfn; +struct rb_node node; +}; +/* Maximum number of VMs that can co-exist under pKVM. */ #define KVM_MAX_PVMS 255
@@ -137,4 +143,26 @@ static inline size_t pkvm_host_sve_state_size(void) SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl))); } +static inline pkvm_handle_t pkvm_pgt_to_handle (struct kvm_pgtable *pgt) +{ +return pgt->pkvm.kvm->arch.pkvm.handle; +} + +int pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops); +void pkvm_pgtable_destroy (struct kvm_pgtable *pgt); +int pkvm_pgtable_map (struct kvm_pgtable *pgt,u64 addr,u64 size, +u64 phys,enum kvm_pgtable_prot prot, +void *mc,enum kvm_pgtable_walk_flags flags); +int pkvm_pgtable_unmap (struct kvm_pgtable *pgt,u64 addr,u64 size); +int pkvm_pgtable_wrprotect (struct kvm_pgtable *pgt,u64 addr,u64 size); +int pkvm_pgtable_flush (struct kvm_pgtable *pgt,u64 addr,u64 size); +bool pkvm_pgtable_test_clear_young (struct kvm_pgtable *pgt,u64 addr,u64 size,bool mkold); +int pkvm_pgtable_relax_perms (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_prot prot, +enum kvm_pgtable_walk_flags flags); +void pkvm_pgtable_mkyoung (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_walk_flags flags); +int pkvm_pgtable_split (struct kvm_pgtable *pgt,u64 addr,u64 size,struct kvm_mmu_memory_cache *mc); +void pkvm_pgtable_free_unlinked (struct kvm_pgtable_mm_ops *mm_ops,void *pgtable,s8 level); +kvm_pte_t *pkvm_pgtable_create_unlinked (struct kvm_pgtable *pgt,u64 phys,s8 level, +enum kvm_pgtable_prot prot,void *mc,bool force_pte); +#endif /* __ARM64_KVM_PKVM_H__ */
arch/arm64/kvm/pkvm.c +195 -0
204 lines not on this page · raw message
Signature · 1 line
2.47.0.338.g60cca15819-goog
Marc Zyngier
maintainerRe: [PATCH v2 17/18] KVM: arm64: Introduce the EL1 pKVM MMU
Quoted 17 lines · Quentin Perret
Introduce a set of helper functions allowing to manipulate the pKVM guest stage-2 page-tables from EL1 using pKVM's HVC interface.
Each helper has an exact one-to-one correspondance with the traditional kvm_pgtable_stage2_*() functions from
pgtable.c, with a strictly matching prototype. This will ease plumbing later on inmmu.c.These callbacks track the
gfn->pfnmappings in a simplerb_treeindexed by IPA in lieu of a page-table. This rb-tree is kept in sync with pKVM's state and is protected by a new rwlock -- the existingmmu_lockprotection does not suffice in themap()path where the tree must be modified whileuser_mem_abort()only acquires aread_lock.Signed-off-by Quentin Perret <[email protected]>---
Quoted 9 lines
The embedded union inside struct
kvm_pgtableis arguably a bit horrible currently... I considered making the pgt argument to all kvm_pgtable_*() functions an opaque void * ptr, and moving the definition of structkvm_pgtabletopgtable.cand the pkvm version intopkvm.c. Given that the allocation of that data-structure is done by the caller, that means we'd need to exposekvm_pgtable_get_pgd_size()or something that each MMU (pgtable.candpkvm.c) would have to implement and things like that. But that felt like a bigger surgery, so I went with the simpler option. Thoughts welcome :-)
I really don't think it is too bad, and I rather keep some typing rather than going the void * route. Some comments below.
Quoted 45 lines
Similarly, happy to drop the
mappings_lockif we want to teachuser_mem_abort()about taking a write lock on themmu_lockin the pKVM case, but again this implementation is the least invasive into normal KVM so that felt like a reasonable starting point.--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_pgtable.h | 27 ++-- arch/arm64/include/asm/kvm_pkvm.h | 28 ++++ arch/arm64/kvm/pkvm.c | 195 +++++++++++++++++++++++++++ 4 files changed, 242 insertions(+), 9 deletions(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -85,6 +85,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);struct kvm_hyp_memcache {phys_addr_t head;unsigned long nr_pages; +struct pkvm_mapping *mapping;/* only used from EL1 */ }; static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,arch/arm64/include/asm/kvm_pgtable.h +4
-9@@ -412,15 +412,24 @@ static inline bool kvm_pgtable_walk_lock_held(void)* be used instead of block mappings. */ struct kvm_pgtable { -u32 ia_bits; -s8 start_level; -kvm_pteref_t pgd; -struct kvm_pgtable_mm_ops *mm_ops; - -/* Stage-2 only */ -struct kvm_s2_mmu *mmu; -enum kvm_pgtable_stage2_flags flags; -kvm_pgtable_force_pte_cb_t force_pte_cb; + union { + struct { + u32 ia_bits; + s8 start_level;
arch/arm64/include/asm/kvm_pgtable.h static inline bool kvm_pgtable_walk_lock_held(void) · line 428
Quoted 10 lines
arch/arm64/include/asm/kvm_pgtable.h +10
-0+kvm_pteref_t pgd; +struct kvm_pgtable_mm_ops *mm_ops; + +/* Stage-2 only */ +struct kvm_s2_mmu *mmu; +enum kvm_pgtable_stage2_flags flags; +kvm_pgtable_force_pte_cb_t force_pte_cb; + }; +struct { +struct kvm *kvm;
Given that the kvm_s2_mmu already has a back-pointer to kvm_arch, maybe you could keep that one common and use it?
There is also some baked assumption that non-NV is always using the s2_mmu that's embedded in kvm_arch.
Quoted 12 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0+struct rb_root mappings; +rwlock_t mappings_lock; + } pkvm; + }; };/** arch/arm64/include/asm/kvm_pkvm.h +0
-0
arch/arm64/include/asm/kvm_pkvm.h line 17
Quoted 7 lines
arch/arm64/include/asm/kvm_pkvm.h +4
-0@@ -11,6 +11,12 @@#include <linux/scatterlist.h> #include <asm/kvm_pgtable.h> +struct pkvm_mapping { +u64 gfn; +u64 pfn; +struct rb_node node;
nit: make the node the first field.
Quoted 75 lines
arch/arm64/include/asm/kvm_pkvm.h +24
-0+}; +/* Maximum number of VMs that can co-exist under pKVM. */ #define KVM_MAX_PVMS 255@@ -137,4 +143,26 @@ static inline size_t pkvm_host_sve_state_size(void) SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl))); } +static inline pkvm_handle_t pkvm_pgt_to_handle (struct kvm_pgtable *pgt) +{ +return pgt->pkvm.kvm->arch.pkvm.handle; +} + +int pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops); +void pkvm_pgtable_destroy (struct kvm_pgtable *pgt); +int pkvm_pgtable_map (struct kvm_pgtable *pgt,u64 addr,u64 size, +u64 phys,enum kvm_pgtable_prot prot, +void *mc,enum kvm_pgtable_walk_flags flags); +int pkvm_pgtable_unmap (struct kvm_pgtable *pgt,u64 addr,u64 size); +int pkvm_pgtable_wrprotect (struct kvm_pgtable *pgt,u64 addr,u64 size); +int pkvm_pgtable_flush (struct kvm_pgtable *pgt,u64 addr,u64 size); +bool pkvm_pgtable_test_clear_young (struct kvm_pgtable *pgt,u64 addr,u64 size,bool mkold); +int pkvm_pgtable_relax_perms (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_prot prot, +enum kvm_pgtable_walk_flags flags); +void pkvm_pgtable_mkyoung (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_walk_flags flags); +int pkvm_pgtable_split (struct kvm_pgtable *pgt,u64 addr,u64 size,struct kvm_mmu_memory_cache *mc); +void pkvm_pgtable_free_unlinked (struct kvm_pgtable_mm_ops *mm_ops,void *pgtable,s8 level); +kvm_pte_t *pkvm_pgtable_create_unlinked (struct kvm_pgtable *pgt,u64 phys,s8 level, +enum kvm_pgtable_prot prot,void *mc,bool force_pte); +#endif /* __ARM64_KVM_PKVM_H__ */ arch/arm64/kvm/pkvm.c +28
-0@@ -7,6 +7,7 @@#include <linux/init.h> #include <linux/kmemleak.h> #include <linux/kvm_host.h> +#include <asm/kvm_mmu.h> #include <linux/memblock.h> #include <linux/mutex.h> #include <linux/sort.h> @@ -268,3 +269,197 @@ static int __init finalize_pkvm(void)return ret; }device_initcall_sync (finalize_pkvm); + +static int cmp_mappings (struct rb_node *node,const struct rb_node *parent) +{ + struct pkvm_mapping *a = rb_entry(node, struct pkvm_mapping, node); + struct pkvm_mapping *b = rb_entry(parent, struct pkvm_mapping, node); + +if (a->gfn < b->gfn) +return -1 ; +if (a->gfn > b->gfn) +return 1 ; +return 0 ; +} + +static struct rb_node *find_first_mapping_node (struct rb_root *root,u64 gfn) +{ +struct rb_node *node = root->rb_node, *prev =NULL ; +struct pkvm_mapping *mapping; + +while (node) { + mapping = rb_entry(node, struct pkvm_mapping, node); +if (mapping->gfn == gfn) +return node; + prev = node; + node = (gfn < mapping->gfn) ? node->rb_left : node->rb_right; + } + +return prev;
arch/arm64/kvm/pkvm.c static int __init finalize_pkvm(void) · line 308
Quoted 10 lines
arch/arm64/kvm/pkvm.c +10
-0+} + +#define for_each_mapping_in_range (pgt, start_ipa, end_ipa, mapping, tmp) \ + for (tmp = find_first_mapping_node(&pgt->pkvm.mappings, ((start_ipa) >> PAGE_SHIFT)); \ + tmp && ({ mapping = rb_entry(tmp, struct pkvm_mapping, node); tmp = rb_next(tmp); 1; });) \ + if (mapping->gfn < ((start_ipa) >> PAGE_SHIFT)) \ + continue; \ + else if (mapping->gfn >= ((end_ipa) >> PAGE_SHIFT)) \ + break; \ + else
Oh gawd... This makes my head spin, and it can't be said that I'm adverse to the most bizarre macro constructs. I came up with this:
arch/arm64/kvm/pkvm.c +17 -15
@@ -298,13 +298,19 @@ static struct rb_node *find_first_mapping_node(struct rb_root *root, u64 gfn)return prev; } -#define for_each_mapping_in_range (pgt, start_ipa, end_ipa, mapping, tmp) \ - for (tmp = find_first_mapping_node(&pgt->pkvm.mappings, ((start_ipa) >> PAGE_SHIFT)); \ - tmp && ({ mapping = rb_entry(tmp, struct pkvm_mapping, node); tmp = rb_next(tmp); 1; });) \ - if (mapping->gfn < ((start_ipa) >> PAGE_SHIFT)) \ - continue; \ - else if (mapping->gfn >= ((end_ipa) >> PAGE_SHIFT)) \ - break; \ +#define for_each_mapping_in_range (__pgt, __start, __end, __map) \ + for (struct rb_node *__tmp = find_first_mapping_node(&__pgt->pkvm.mappings, \ + ((__start) >> PAGE_SHIFT)); \ + __tmp && ({ \ + __map = rb_entry(__tmp, struct pkvm_mapping, node); \ + __tmp = rb_next(__tmp); \ + true; \ + }); \ + ) \ + if (__map->gfn < ((__start) >> PAGE_SHIFT)) \ + continue; \ + else if (__map->gfn >= ((__end) >> PAGE_SHIFT)) \ + break; \ elseint pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops)
@@ -371,11 +377,10 @@ int pkvm_pgtable_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size) {pkvm_handle_t handle =pkvm_pgt_to_handle (pgt);struct pkvm_mapping *mapping; -struct rb_node *tmp;int ret =0 ;write_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) { +for_each_mapping_in_range (pgt, addr, addr + size, mapping) { ret =kvm_call_hyp_nvhe (__pkvm_host_unshare_guest, handle, mapping->gfn);if (WARN_ON (ret))break ;
@@ -392,11 +397,10 @@ int pkvm_pgtable_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) {pkvm_handle_t handle =pkvm_pgt_to_handle (pgt);struct pkvm_mapping *mapping; -struct rb_node *tmp;int ret =0 ;read_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) { +for_each_mapping_in_range (pgt, addr, addr + size, mapping) { ret =kvm_call_hyp_nvhe (__pkvm_host_wrprotect_guest, handle, mapping->gfn);if (WARN_ON (ret))break ;
@@ -409,10 +413,9 @@ int pkvm_pgtable_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)int pkvm_pgtable_flush (struct kvm_pgtable *pgt,u64 addr,u64 size) {struct pkvm_mapping *mapping; -struct rb_node *tmp;read_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) +for_each_mapping_in_range (pgt, addr, addr + size, mapping)__clean_dcache_guest_page (pfn_to_kaddr (mapping->pfn),PAGE_SIZE );read_unlock (&pgt->pkvm.mappings_lock);
@@ -423,11 +426,10 @@ bool pkvm_pgtable_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 size, {pkvm_handle_t handle =pkvm_pgt_to_handle (pgt);struct pkvm_mapping *mapping; -struct rb_node *tmp;bool young = false;read_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) +for_each_mapping_in_range (pgt, addr, addr + size, mapping) young |=kvm_call_hyp_nvhe (__pkvm_host_test_clear_young_guest, handle, mapping->gfn, mkold);read_unlock (&pgt->pkvm.mappings_lock);
Should be semantically equivalent, but I find it way more readable.
Also, maybe add a comment indicating why __tmp needs to be updated *before* the body of the loop gets executed (case of freeing the mapping from within the body).
arch/arm64/kvm/pkvm.c static int __init finalize_pkvm(void) · line 314
Quoted 6 lines
arch/arm64/kvm/pkvm.c +6
-0+ +int pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops) +{ + pgt->pkvm.kvm =kvm_s2_mmu_to_kvm (mmu); + pgt->pkvm.mappings =RB_ROOT ; +rwlock_init (&pgt->pkvm.mappings_lock);
We talked about this f2f: Given that this lock is semantically equivalent to the MMU lock, maybe just use that by upgrading it to be taken as for write when pKVM is enabled.
It should be easy enough to wrap that in helpers that DTRT, and all this code could become devoid of any extra locking.
[...]
Quoted 18 lines
arch/arm64/kvm/pkvm.c +18
-0+int pkvm_pgtable_relax_perms (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_prot prot, +enum kvm_pgtable_walk_flags flags) +{ +return kvm_call_hyp_nvhe (__pkvm_host_relax_guest_perms, addr >>PAGE_SHIFT , prot); +} + +void pkvm_pgtable_mkyoung (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_walk_flags flags) +{ +WARN_ON (kvm_call_hyp_nvhe (__pkvm_host_mkyoung_guest, addr >>PAGE_SHIFT )); +} + +void pkvm_pgtable_free_unlinked (struct kvm_pgtable_mm_ops *mm_ops,void *pgtable,s8 level) +{ +WARN_ON (1 ); +} + +kvm_pte_t *pkvm_pgtable_create_unlinked (struct kvm_pgtable *pgt,u64 phys,s8 level, +enum kvm_pgtable_prot prot,void *mc,bool force_pte)
arch/arm64/kvm/pkvm.c static int __init finalize_pkvm(void) · line 342
Quoted 10 lines
arch/arm64/kvm/pkvm.c +10
-0+{ +WARN_ON (1 ); +return NULL ; +} + +int pkvm_pgtable_split (struct kvm_pgtable *pgt,u64 addr,u64 size,struct kvm_mmu_memory_cache *mc) +{ +WARN_ON (1 ); +return -EINVAL ; +}
Maybe turn these warnings into their _ONCE version. If we end-up here, seeing it once should be enough to realise we're toast.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v2 17/18] KVM: arm64: Introduce the EL1 pKVM MMU
Quoted 29 lines · Marc Zyngier
Quoted 17 lines · Quentin Perret
Introduce a set of helper functions allowing to manipulate the pKVM guest stage-2 page-tables from EL1 using pKVM's HVC interface.
Each helper has an exact one-to-one correspondance with the traditional kvm_pgtable_stage2_*() functions from
pgtable.c, with a strictly matching prototype. This will ease plumbing later on inmmu.c.These callbacks track the
gfn->pfnmappings in a simplerb_treeindexed by IPA in lieu of a page-table. This rb-tree is kept in sync with pKVM's state and is protected by a new rwlock -- the existingmmu_lockprotection does not suffice in themap()path where the tree must be modified whileuser_mem_abort()only acquires aread_lock.Signed-off-by Quentin Perret <[email protected]>---
Quoted 9 lines
The embedded union inside struct
kvm_pgtableis arguably a bit horrible currently... I considered making the pgt argument to all kvm_pgtable_*() functions an opaque void * ptr, and moving the definition of structkvm_pgtabletopgtable.cand the pkvm version intopkvm.c. Given that the allocation of that data-structure is done by the caller, that means we'd need to exposekvm_pgtable_get_pgd_size()or something that each MMU (pgtable.candpkvm.c) would have to implement and things like that. But that felt like a bigger surgery, so I went with the simpler option. Thoughts welcome :-)
Quoted 2 lines
I really don't think it is too bad, and I rather keep some typing rather than going the void * route. Some comments below.
Sounds good.
Quoted 59 lines
Quoted 45 lines
Similarly, happy to drop the
mappings_lockif we want to teachuser_mem_abort()about taking a write lock on themmu_lockin the pKVM case, but again this implementation is the least invasive into normal KVM so that felt like a reasonable starting point.--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_pgtable.h | 27 ++-- arch/arm64/include/asm/kvm_pkvm.h | 28 ++++ arch/arm64/kvm/pkvm.c | 195 +++++++++++++++++++++++++++ 4 files changed, 242 insertions(+), 9 deletions(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -85,6 +85,7 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);struct kvm_hyp_memcache {phys_addr_t head;unsigned long nr_pages; +struct pkvm_mapping *mapping;/* only used from EL1 */ }; static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,arch/arm64/include/asm/kvm_pgtable.h +4
-9@@ -412,15 +412,24 @@ static inline bool kvm_pgtable_walk_lock_held(void)* be used instead of block mappings. */ struct kvm_pgtable { -u32 ia_bits; -s8 start_level; -kvm_pteref_t pgd; -struct kvm_pgtable_mm_ops *mm_ops; - -/* Stage-2 only */ -struct kvm_s2_mmu *mmu; -enum kvm_pgtable_stage2_flags flags; -kvm_pgtable_force_pte_cb_t force_pte_cb; + union { + struct { + u32 ia_bits; + s8 start_level;arch/arm64/include/asm/kvm_pgtable.h static inline bool kvm_pgtable_walk_lock_held(void) · line 428
Quoted 10 lines
arch/arm64/include/asm/kvm_pgtable.h +10
-0+kvm_pteref_t pgd; +struct kvm_pgtable_mm_ops *mm_ops; + +/* Stage-2 only */ +struct kvm_s2_mmu *mmu; +enum kvm_pgtable_stage2_flags flags; +kvm_pgtable_force_pte_cb_t force_pte_cb; + }; +struct { +struct kvm *kvm;Given that the
kvm_s2_mmualready has a back-pointer tokvm_arch, maybe you could keep that one common and use it?
Quoted 2 lines
There is also some baked assumption that non-NV is always using the
s2_mmuthat's embedded inkvm_arch.
Right, what I need is one kvm_s2_mmu_to_kvm() away, so that should work nicely. And as discussed below, I'll try ditching mappings_lock, so we should be left the rb_root on its own.
Quoted 15 lines
Quoted 15 lines
arch/arm64/include/asm/kvm_pgtable.h +4
-0+struct rb_root mappings; +rwlock_t mappings_lock; + } pkvm; + }; };/** arch/arm64/include/asm/kvm_pkvm.h +0
-0@@ -11,6 +11,12 @@#include <linux/scatterlist.h> #include <asm/kvm_pgtable.h>
Quoted 6 lines
arch/arm64/include/asm/kvm_pkvm.h line 17
Quoted 4 lines
arch/arm64/include/asm/kvm_pkvm.h +4
-0+struct pkvm_mapping { +u64 gfn; +u64 pfn; +struct rb_node node;nit: make the node the first field.
Ack.
Quoted 172 lines
Quoted 75 lines
arch/arm64/include/asm/kvm_pkvm.h +24
-0+}; +/* Maximum number of VMs that can co-exist under pKVM. */ #define KVM_MAX_PVMS 255@@ -137,4 +143,26 @@ static inline size_t pkvm_host_sve_state_size(void) SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl))); } +static inline pkvm_handle_t pkvm_pgt_to_handle (struct kvm_pgtable *pgt) +{ +return pgt->pkvm.kvm->arch.pkvm.handle; +} + +int pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops); +void pkvm_pgtable_destroy (struct kvm_pgtable *pgt); +int pkvm_pgtable_map (struct kvm_pgtable *pgt,u64 addr,u64 size, +u64 phys,enum kvm_pgtable_prot prot, +void *mc,enum kvm_pgtable_walk_flags flags); +int pkvm_pgtable_unmap (struct kvm_pgtable *pgt,u64 addr,u64 size); +int pkvm_pgtable_wrprotect (struct kvm_pgtable *pgt,u64 addr,u64 size); +int pkvm_pgtable_flush (struct kvm_pgtable *pgt,u64 addr,u64 size); +bool pkvm_pgtable_test_clear_young (struct kvm_pgtable *pgt,u64 addr,u64 size,bool mkold); +int pkvm_pgtable_relax_perms (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_prot prot, +enum kvm_pgtable_walk_flags flags); +void pkvm_pgtable_mkyoung (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_walk_flags flags); +int pkvm_pgtable_split (struct kvm_pgtable *pgt,u64 addr,u64 size,struct kvm_mmu_memory_cache *mc); +void pkvm_pgtable_free_unlinked (struct kvm_pgtable_mm_ops *mm_ops,void *pgtable,s8 level); +kvm_pte_t *pkvm_pgtable_create_unlinked (struct kvm_pgtable *pgt,u64 phys,s8 level, +enum kvm_pgtable_prot prot,void *mc,bool force_pte); +#endif /* __ARM64_KVM_PKVM_H__ */ arch/arm64/kvm/pkvm.c +28
-0@@ -7,6 +7,7 @@#include <linux/init.h> #include <linux/kmemleak.h> #include <linux/kvm_host.h> +#include <asm/kvm_mmu.h> #include <linux/memblock.h> #include <linux/mutex.h> #include <linux/sort.h> @@ -268,3 +269,197 @@ static int __init finalize_pkvm(void)return ret; }device_initcall_sync (finalize_pkvm); + +static int cmp_mappings (struct rb_node *node,const struct rb_node *parent) +{ + struct pkvm_mapping *a = rb_entry(node, struct pkvm_mapping, node); + struct pkvm_mapping *b = rb_entry(parent, struct pkvm_mapping, node); + +if (a->gfn < b->gfn) +return -1 ; +if (a->gfn > b->gfn) +return 1 ; +return 0 ; +} + +static struct rb_node *find_first_mapping_node (struct rb_root *root,u64 gfn) +{ +struct rb_node *node = root->rb_node, *prev =NULL ; +struct pkvm_mapping *mapping; + +while (node) { + mapping = rb_entry(node, struct pkvm_mapping, node); +if (mapping->gfn == gfn) +return node; + prev = node; + node = (gfn < mapping->gfn) ? node->rb_left : node->rb_right; + } + +return prev;arch/arm64/kvm/pkvm.c static int __init finalize_pkvm(void) · line 308
Quoted 10 lines
arch/arm64/kvm/pkvm.c +10
-0+} + +#define for_each_mapping_in_range (pgt, start_ipa, end_ipa, mapping, tmp) \ + for (tmp = find_first_mapping_node(&pgt->pkvm.mappings, ((start_ipa) >> PAGE_SHIFT)); \ + tmp && ({ mapping = rb_entry(tmp, struct pkvm_mapping, node); tmp = rb_next(tmp); 1; });) \ + if (mapping->gfn < ((start_ipa) >> PAGE_SHIFT)) \ + continue; \ + else if (mapping->gfn >= ((end_ipa) >> PAGE_SHIFT)) \ + break; \ + elseOh gawd... This makes my head spin, and it can't be said that I'm adverse to the most bizarre macro constructs. I came up with this:
arch/arm64/kvm/pkvm.c +17
-15@@ -298,13 +298,19 @@ static struct rb_node *find_first_mapping_node(struct rb_root *root, u64 gfn)return prev; } -#define for_each_mapping_in_range (pgt, start_ipa, end_ipa, mapping, tmp) \ - for (tmp = find_first_mapping_node(&pgt->pkvm.mappings, ((start_ipa) >> PAGE_SHIFT)); \ - tmp && ({ mapping = rb_entry(tmp, struct pkvm_mapping, node); tmp = rb_next(tmp); 1; });) \ - if (mapping->gfn < ((start_ipa) >> PAGE_SHIFT)) \ - continue; \ - else if (mapping->gfn >= ((end_ipa) >> PAGE_SHIFT)) \ - break; \ +#define for_each_mapping_in_range (__pgt, __start, __end, __map) \ + for (struct rb_node *__tmp = find_first_mapping_node(&__pgt->pkvm.mappings, \ + ((__start) >> PAGE_SHIFT)); \ + __tmp && ({ \ + __map = rb_entry(__tmp, struct pkvm_mapping, node); \ + __tmp = rb_next(__tmp); \ + true; \ + }); \ + ) \ + if (__map->gfn < ((__start) >> PAGE_SHIFT)) \ + continue; \ + else if (__map->gfn >= ((__end) >> PAGE_SHIFT)) \ + break; \ elseint pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops)@@ -371,11 +377,10 @@ int pkvm_pgtable_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size) {pkvm_handle_t handle =pkvm_pgt_to_handle (pgt);struct pkvm_mapping *mapping; -struct rb_node *tmp;int ret =0 ;write_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) { +for_each_mapping_in_range (pgt, addr, addr + size, mapping) { ret =kvm_call_hyp_nvhe (__pkvm_host_unshare_guest, handle, mapping->gfn);if (WARN_ON (ret))break ;@@ -392,11 +397,10 @@ int pkvm_pgtable_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) {pkvm_handle_t handle =pkvm_pgt_to_handle (pgt);struct pkvm_mapping *mapping; -struct rb_node *tmp;int ret =0 ;read_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) { +for_each_mapping_in_range (pgt, addr, addr + size, mapping) { ret =kvm_call_hyp_nvhe (__pkvm_host_wrprotect_guest, handle, mapping->gfn);if (WARN_ON (ret))break ;@@ -409,10 +413,9 @@ int pkvm_pgtable_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)int pkvm_pgtable_flush (struct kvm_pgtable *pgt,u64 addr,u64 size) {struct pkvm_mapping *mapping; -struct rb_node *tmp;read_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) +for_each_mapping_in_range (pgt, addr, addr + size, mapping)__clean_dcache_guest_page (pfn_to_kaddr (mapping->pfn),PAGE_SIZE );read_unlock (&pgt->pkvm.mappings_lock);@@ -423,11 +426,10 @@ bool pkvm_pgtable_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 size, {pkvm_handle_t handle =pkvm_pgt_to_handle (pgt);struct pkvm_mapping *mapping; -struct rb_node *tmp;bool young = false;read_lock (&pgt->pkvm.mappings_lock); -for_each_mapping_in_range (pgt, addr, addr + size, mapping, tmp) +for_each_mapping_in_range (pgt, addr, addr + size, mapping) young |=kvm_call_hyp_nvhe (__pkvm_host_test_clear_young_guest, handle, mapping->gfn, mkold);read_unlock (&pgt->pkvm.mappings_lock);
Quoted 1 line
Should be semantically equivalent, but I find it way more readable.
Yep, declaring __tmp within the loop itself is much nicer, it certainly doesn't need to outlive it. I'll fold that in, thanks!
Quoted 3 lines
Also, maybe add a comment indicating why
__tmpneeds to be updated *before* the body of the loop gets executed (case of freeing the mapping from within the body).
Ack, and I might rename the macro to for_each_mapping_in_range_safe() as well to make it clear we have that property.
Quoted 11 lines
arch/arm64/kvm/pkvm.c static int __init finalize_pkvm(void) · line 314
Quoted 6 lines
arch/arm64/kvm/pkvm.c +6
-0+ +int pkvm_pgtable_init (struct kvm_pgtable *pgt,struct kvm_s2_mmu *mmu,struct kvm_pgtable_mm_ops *mm_ops) +{ + pgt->pkvm.kvm =kvm_s2_mmu_to_kvm (mmu); + pgt->pkvm.mappings =RB_ROOT ; +rwlock_init (&pgt->pkvm.mappings_lock);We talked about this f2f: Given that this lock is semantically equivalent to the MMU lock, maybe just use that by upgrading it to be taken as for write when pKVM is enabled.
Quoted 2 lines
It should be easy enough to wrap that in helpers that DTRT, and all this code could become devoid of any extra locking.
OK, I'll also stick lockdep assertions to document and enforce the locking requirements from here.
Quoted 23 lines
Quoted 23 lines
arch/arm64/kvm/pkvm.c +23
-0+int pkvm_pgtable_relax_perms (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_prot prot, +enum kvm_pgtable_walk_flags flags) +{ +return kvm_call_hyp_nvhe (__pkvm_host_relax_guest_perms, addr >>PAGE_SHIFT , prot); +} + +void pkvm_pgtable_mkyoung (struct kvm_pgtable *pgt,u64 addr,enum kvm_pgtable_walk_flags flags) +{ +WARN_ON (kvm_call_hyp_nvhe (__pkvm_host_mkyoung_guest, addr >>PAGE_SHIFT )); +} + +void pkvm_pgtable_free_unlinked (struct kvm_pgtable_mm_ops *mm_ops,void *pgtable,s8 level) +{ +WARN_ON (1 ); +} + +kvm_pte_t *pkvm_pgtable_create_unlinked (struct kvm_pgtable *pgt,u64 phys,s8 level, +enum kvm_pgtable_prot prot,void *mc,bool force_pte) +{ +WARN_ON (1 ); +return NULL ; +} +
Quoted 8 lines
arch/arm64/kvm/pkvm.c static int __init finalize_pkvm(void) · line 342
Quoted 5 lines
arch/arm64/kvm/pkvm.c +5
-0+int pkvm_pgtable_split (struct kvm_pgtable *pgt,u64 addr,u64 size,struct kvm_mmu_memory_cache *mc) +{ +WARN_ON (1 ); +return -EINVAL ; +}Maybe turn these warnings into their
_ONCEversion. If we end-up here, seeing it once should be enough to realise we're toast.
Will do.
Cheers,
Quentin
PATCH v2 18/18 KVM: arm64: Plumb the pKVM MMU in KVM
In reply to Quentin PerretIntroduce the KVM_PGT_S2() helper macro to allow switching from the traditional pgtable code to the pKVM version easily in mmu.c. The cost of this 'indirection' is expected to be very minimal due to is_protected_kvm_enabled() being backed by a static key.
With this, everything is in place to allow the delegation of non-protected guest stage-2 page-tables to pKVM, so let's stop using the host's kvm_s2_mmu from EL2 and enjoy the ride.
--- arch/arm64/kvm/arm.c | 9 ++- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 2 - arch/arm64/kvm/mmu.c | 103 +++++++++++++++++++++-------- 3 files changed, 83 insertions(+), 31 deletions(-)
arch/arm64/kvm/arm.c +8 -1
@@ -502,7 +502,10 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)void kvm_arch_vcpu_destroy (struct kvm_vcpu *vcpu) { -kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_cache); +if (!is_protected_kvm_enabled ()) +kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_cache); +else +free_hyp_memcache (&vcpu->arch.pkvm_memcache);kvm_timer_vcpu_terminate (vcpu);kvm_pmu_vcpu_destroy (vcpu);kvm_vgic_vcpu_destroy (vcpu);
@@ -574,6 +577,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)struct kvm_s2_mmu *mmu;int *last_ran; +if (is_protected_kvm_enabled ()) + goto nommu; +if (vcpu_has_nv (vcpu))kvm_vcpu_load_hw_mmu (vcpu);
@@ -594,6 +600,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) *last_ran = vcpu->vcpu_idx; } +nommu: vcpu->cpu = cpu;kvm_vgic_load (vcpu);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -2
@@ -103,8 +103,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* Limit guest vector length to the maximum supported by the host. */ hyp_vcpu->vcpu.arch.sve_max_vl =min (host_vcpu->arch.sve_max_vl, kvm_host_sve_max_vl); - hyp_vcpu->vcpu.arch.hw_mmu = host_vcpu->arch.hw_mmu; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); hyp_vcpu->vcpu.arch.hcr_el2 |= READ_ONCE(host_vcpu->arch.hcr_el2) &
arch/arm64/kvm/mmu.c +75 -28
267 lines not on this page · raw message
Signature · 1 line
2.47.0.338.g60cca15819-goog