KVM: Unify mmu_memory_cache functionality across architectures
Subsystems (12)
v4 cover KVM: Unify mmu_memory_cache functionality across architectures
We currently have duplicated functionality for the mmu_memory_cache used to pre-allocate memory for the page table manipulation code which cannot allocate memory while holding spinlocks. This functionality is duplicated across x86, arm/arm64, and mips.
This was motivated by a debate of modifying the arm code to be more in line with the x86 code and some discussions around changing the page flags used for allocation. This series should make it easier to take a uniform approach across architectures.
While there's not a huge amount of code sharing, we come out with a net gain, and the real win is in the consistency of how we allocate memory for page tables used by secondary MMUs driven by KVM in Linux.
Only tested on arm/arm64, and only compile-tested on x86 and mips. I'm especially curious on getting feedback on the change of GFP flags for x86 (patch 1) and on the use of __GFP_ACCOUNT for mips.
Changes since v3:
- Moved to common GFP_PGTABLE_USER definition for page allocations in the MMU cache for all three architectures. This follows recent work which already did this for arm/arm64.
- Rebased on v5.4-rc4.
Changes since v2:
- Simplified kalloc flag definitions as per Paolo's review comment.
Changes since v1:
- Split out rename from initial x86 patch to have separate patches to move the logic to common code and to rename.
- Introduce KVM_ARCH_WANT_MMU_MEMCACHE to avoid compile breakage on architectures that don't use this functionality.
- Rename KVM_NR_MEM_OBJS to KVM_MMU_NR_MEMCACHE_OBJS
Christoffer Dall (5):
KVM: x86: Move memcache allocation to GFP_PGTABLE_USER
KVM: x86: Move mmu_memory_cache functions to common code
KVM: x86: Rename mmu_memory_cache to kvm_mmu_memcache
KVM: arm/arm64: Move to common kvm_mmu_memcache infrastructure
KVM: mips: Move to common kvm_mmu_memcache infrastructure
arch/arm/include/asm/kvm_host.h | 13 +--- arch/arm/include/asm/kvm_mmu.h | 2 +- arch/arm/include/asm/kvm_types.h | 9 +++ arch/arm64/include/asm/kvm_host.h | 13 +--- arch/arm64/include/asm/kvm_mmu.h | 2 +- arch/arm64/include/asm/kvm_types.h | 9 +++ arch/mips/include/asm/kvm_host.h | 15 +---- arch/mips/include/asm/kvm_types.h | 9 +++ arch/mips/kvm/mips.c | 2 +- arch/mips/kvm/mmu.c | 54 +++------------- arch/powerpc/include/asm/kvm_types.h | 5 ++ arch/s390/include/asm/kvm_types.h | 5 ++ arch/x86/include/asm/kvm_host.h | 17 +---- arch/x86/include/asm/kvm_types.h | 9 +++ arch/x86/kvm/mmu.c | 97 ++++++---------------------- arch/x86/kvm/paging_tmpl.h | 4 +- include/linux/kvm_host.h | 11 ++++ include/linux/kvm_types.h | 13 ++++ virt/kvm/arm/arm.c | 2 +- virt/kvm/arm/mmu.c | 68 +++++-------------- virt/kvm/kvm_main.c | 61 +++++++++++++++++ 21 files changed, 190 insertions(+), 230 deletions(-) create mode 100644 arch/arm/include/asm/kvm_types.h create mode 100644 arch/arm64/include/asm/kvm_types.h create mode 100644 arch/mips/include/asm/kvm_types.h create mode 100644 arch/powerpc/include/asm/kvm_types.h create mode 100644 arch/s390/include/asm/kvm_types.h create mode 100644 arch/x86/include/asm/kvm_types.h
Signature · 1 line
2.18.0
PATCH v4 1/5 KVM: x86: Move memcache allocation to GFP_PGTABLE_USER
Recent commit 50f11a8a4620eee6b6831e69ab5d42456546d7d8 moved page table allocations for both KVM and normal user page table allocations to GFP_PGTABLE_USER in order to get __GFP_ACCOUNT for the page tables.
However, while KVM on other architectures such as arm64 were included in this change, curiously KVM on x86 was not.
Currently, KVM on x86 uses kmem_cache_zalloc(GFP_KERNEL_ACCOUNT) for kmem_cache-based allocations, which expands in the following way:
kmem_cache_zalloc(..., GFP_KERNEL_ACCOUNT) => kmem_cache_alloc(..., GFP_KERNEL_ACCOUNT | __GFP_ZERO) => kmem_cache_alloc(..., GFP_KERNEL | __GFP_ACCOUNT | __GFP_ZERO)
It so happens that GFP_PGTABLE_USER expands as:
GFP_PGTABLE_USER =>
(GFP_PGTABLE_KERNEL | __GFP_ACCOUNT) =>
((GFP_KERNEL | __GFP_ZERO) | __GFP_ACCOUNT) =>
(GFP_KERNEL | __GFP_ACCOUNT | __GFP_ZERO)
Which means that we can replace the current KVM on x86 call as:
- obj = kmem_cache_zalloc(base_cache, GFP_KERNEL_ACCOUNT);
+ obj = kmem_cache_alloc(base_cache, GFP_PGTABLE_USER);
For the single page cache topup allocation, KVM on x86 currently uses __get_free_page(GFP_KERNEL_ACCOUNT). It seems to me that is equivalent to the above, except that the allocated page is not guaranteed to be zero (unless I missed the place where __get_free_page(!__GFP_ZERO) is still guaranteed to be zeroed. It seems natural (and in fact desired) to have both topup functions implement the same expectations towards the caller, and we therefore move to GFP_PGTABLE_USER here as well.
This will make it easier to unify the memchace implementation between architectures.
--- arch/x86/kvm/mmu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu.c +3 -2
@@ -40,6 +40,7 @@#include <asm/page.h> #include <asm/pat.h> +#include <asm/pgalloc.h> #include <asm/cmpxchg.h> #include <asm/e820/api.h> #include <asm/io.h>
@@ -1025,7 +1026,7 @@ static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,if (cache->nobjs >= min)return 0 ;while (cache->nobjs <ARRAY_SIZE (cache->objects)) { - obj =kmem_cache_zalloc (base_cache,GFP_KERNEL_ACCOUNT ); + obj =kmem_cache_alloc (base_cache,GFP_PGTABLE_USER );if (!obj)return cache->nobjs >= min ?0 : -ENOMEM ; cache->objects[cache->nobjs++] = obj;
@@ -1053,7 +1054,7 @@ static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,if (cache->nobjs >= min)return 0 ;while (cache->nobjs <ARRAY_SIZE (cache->objects)) { - page = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); + page = (void *)__get_free_page (GFP_PGTABLE_USER );if (!page)return cache->nobjs >= min ?0 : -ENOMEM ; cache->objects[cache->nobjs++] = page;
Signature · 1 line
2.18.0
Re: [PATCH v4 1/5] KVM: x86: Move memcache allocation to GFP_PGTABLE_USER
Quoted 31 lines · Christoffer Dall
Recent commit
50f11a8a4620eee6b6831e69ab5d42456546d7d8moved page table allocations for both KVM and normal user page table allocations toGFP_PGTABLE_USERin order to get__GFP_ACCOUNTfor the page tables.However, while KVM on other architectures such as arm64 were included in this change, curiously KVM on x86 was not.
Currently, KVM on x86 uses
kmem_cache_zalloc(GFP_KERNEL_ACCOUNT)for kmem_cache-based allocations, which expands in the following way:
kmem_cache_zalloc(..., GFP_KERNEL_ACCOUNT)=>kmem_cache_alloc(..., GFP_KERNEL_ACCOUNT | __GFP_ZERO)=>kmem_cache_alloc(..., GFP_KERNEL | __GFP_ACCOUNT | __GFP_ZERO)It so happens that
GFP_PGTABLE_USERexpands as:
GFP_PGTABLE_USER=>
(GFP_PGTABLE_KERNEL|__GFP_ACCOUNT) =>
((GFP_KERNEL|__GFP_ZERO) |__GFP_ACCOUNT) =>
(GFP_KERNEL|__GFP_ACCOUNT|__GFP_ZERO)Which means that we can replace the current KVM on x86 call as:
arch/x86/kvm/mmu.c +1
-1- obj =kmem_cache_zalloc (base_cache,GFP_KERNEL_ACCOUNT ); + obj =kmem_cache_alloc (base_cache,GFP_PGTABLE_USER );For the single page cache topup allocation, KVM on x86 currently uses
__get_free_page(GFP_KERNEL_ACCOUNT). It seems to me that is equivalent to the above, except that the allocated page is not guaranteed to be zero (unless I missed the place where__get_free_page(!__GFP_ZERO)is still guaranteed to be zeroed. It seems natural (and in fact desired) to have both topup functions implement the same expectations towards the caller, and we therefore move toGFP_PGTABLE_USERhere as well.
Quoted 2 lines
This will make it easier to unify the memchace implementation between architectures.
Functionally, this looks correct (I haven't actually tested). But, it means that x86's shadow pages will be zeroed out twice, which could lead to performance regressions. The cache is also used for the gfns array, and I'm pretty sure the gfns array is never zeroed out in the current code, i.e. zeroing gfns would also introduce overhead.
The redudant zeroing of shadow pages could likely be addressed by removing the call to clear_page() in kvm_mmu_get_page(), but I'd prefer not to go that route because it doesn't address the gfns issue, means KVM pays the cost of zeroing up front (as opposed to when a page is actually used), and I have a future use case where the shadow page needs to be initialized to a non-zero value.
What about having the common mmu_topup_memory_cache{_page}() take a GFP param? That would allow consolidating the bulk of the code while allowing x86 to optimize its specific scenarios.
Quoted 38 lines
Signed-off-by Christoffer Dall <[email protected]>--- arch/x86/kvm/mmu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)arch/x86/kvm/mmu.c +3
-2@@ -40,6 +40,7 @@#include <asm/page.h> #include <asm/pat.h> +#include <asm/pgalloc.h> #include <asm/cmpxchg.h> #include <asm/e820/api.h> #include <asm/io.h> @@ -1025,7 +1026,7 @@ static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,if (cache->nobjs >= min)return 0 ;while (cache->nobjs <ARRAY_SIZE (cache->objects)) { - obj =kmem_cache_zalloc (base_cache,GFP_KERNEL_ACCOUNT ); + obj =kmem_cache_alloc (base_cache,GFP_PGTABLE_USER );if (!obj)return cache->nobjs >= min ?0 : -ENOMEM ; cache->objects[cache->nobjs++] = obj;@@ -1053,7 +1054,7 @@ static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,if (cache->nobjs >= min)return 0 ;while (cache->nobjs <ARRAY_SIZE (cache->objects)) { - page = (void *)__get_free_page (GFP_KERNEL_ACCOUNT ); + page = (void *)__get_free_page (GFP_PGTABLE_USER );if (!page)return cache->nobjs >= min ?0 : -ENOMEM ; cache->objects[cache->nobjs++] = page;Signature · 1 line
2.18.0
PATCH v4 2/5 KVM: x86: Move mmu_memory_cache functions to common code
In reply to Christoffer DallWe are currently duplicating the mmu memory cache functionality quite heavily between the architectures that support KVM. As a first step, move the x86 implementation (which seems to have the most recently maintained version of the mmu memory cache) to common code.
We introduce an arch-specific kvm_types.h which can be used to specify how many objects are required in the memory cache, an aspect which diverges across architectures. Since kvm_host.h defines structures with fields of the memcache object, we define the memcache structure in kvm_types.h, and we include the architecture-specific kvm_types.h to know the size of object in kvm_host.h.
We only define the functions and data types if KVM_ARCH_WANT_MMU_MEMORY_CACHE is defined, because not all architectures require the mmu memory cache.
--- arch/arm/include/asm/kvm_types.h | 5 +++ arch/arm64/include/asm/kvm_types.h | 6 +++ arch/mips/include/asm/kvm_types.h | 5 +++ arch/powerpc/include/asm/kvm_types.h | 5 +++ arch/s390/include/asm/kvm_types.h | 5 +++ arch/x86/include/asm/kvm_host.h | 11 ----- arch/x86/include/asm/kvm_types.h | 9 ++++ arch/x86/kvm/mmu.c | 60 --------------------------- include/linux/kvm_host.h | 11 +++++ include/linux/kvm_types.h | 13 ++++++ virt/kvm/kvm_main.c | 61 ++++++++++++++++++++++++++++ 11 files changed, 120 insertions(+), 71 deletions(-) create mode 100644 arch/arm/include/asm/kvm_types.h create mode 100644 arch/arm64/include/asm/kvm_types.h create mode 100644 arch/mips/include/asm/kvm_types.h create mode 100644 arch/powerpc/include/asm/kvm_types.h create mode 100644 arch/s390/include/asm/kvm_types.h create mode 100644 arch/x86/include/asm/kvm_types.h
arch/arm/include/asm/kvm_types.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ARM_KVM_TYPES_H +#define _ASM_ARM_KVM_TYPES_H + +#endif /* _ASM_ARM_KVM_TYPES_H */
arch/arm64/include/asm/kvm_types.h new file +6 -0
@@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ARM64_KVM_TYPES_H +#define _ASM_ARM64_KVM_TYPES_H + +#endif /* _ASM_ARM64_KVM_TYPES_H */ +
arch/mips/include/asm/kvm_types.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_MIPS_KVM_TYPES_H +#define _ASM_MIPS_KVM_TYPES_H + +#endif /* _ASM_MIPS_KVM_TYPES_H */
arch/powerpc/include/asm/kvm_types.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_POWERPC_KVM_TYPES_H +#define _ASM_POWERPC_KVM_TYPES_H + +#endif /* _ASM_POWERPC_KVM_TYPES_H */
arch/s390/include/asm/kvm_types.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_S390_KVM_TYPES_H +#define _ASM_S390_KVM_TYPES_H + +#endif /* _ASM_S390_KVM_TYPES_H */
arch/x86/include/asm/kvm_host.h +0 -11
@@ -179,8 +179,6 @@ enum {#include <asm/kvm_emulate.h> -#define KVM_NR_MEM_OBJS 40 -#define KVM_NR_DB_REGS 4#define DR6_BD (1 << 13)
@@ -231,15 +229,6 @@ enum {struct kvm_kernel_irq_routing_entry ; -/* -* We don't want allocation failures within the mmu code, so we preallocate -* enough memory for a single page fault in a cache. -*/ -struct kvm_mmu_memory_cache { -int nobjs; -void *objects[KVM_NR_MEM_OBJS ]; -}; -/* * the pages used as guest page table on soft mmu are tracked by * kvm_memory_slot.arch.gfn_track which is 16 bits, so the role bits used
arch/x86/include/asm/kvm_types.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_KVM_TYPES_H +#define _ASM_X86_KVM_TYPES_H + +#define KVM_ARCH_WANT_MMU_MEMORY_CACHE + +#define KVM_NR_MEM_OBJS 40 + +#endif /* _ASM_X86_KVM_TYPES_H */
arch/x86/kvm/mmu.c +0 -60
78 lines not on this page · raw message
include/linux/kvm_host.h +11 -0
@@ -788,6 +788,17 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool usermode_vcpu_not_eligible);void kvm_flush_remote_tlbs (struct kvm *kvm);void kvm_reload_remote_mmus (struct kvm *kvm); +#ifdef KVM_ARCH_WANT_MMU_MEMORY_CACHE +int mmu_topup_memory_cache (struct kvm_mmu_memory_cache *cache, +struct kmem_cache *base_cache,int min); +int mmu_memory_cache_free_objects (struct kvm_mmu_memory_cache *cache); +void mmu_free_memory_cache (struct kvm_mmu_memory_cache *mc, +struct kmem_cache *cache); +int mmu_topup_memory_cache_page (struct kvm_mmu_memory_cache *cache,int min); +void mmu_free_memory_cache_page (struct kvm_mmu_memory_cache *mc); +void *mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc); +#endif +bool kvm_make_vcpus_request_mask (struct kvm *kvm,unsigned int req,unsigned long *vcpu_bitmap,cpumask_var_t tmp);bool kvm_make_all_cpus_request (struct kvm *kvm,unsigned int req);
include/linux/kvm_types.h +13 -0
@@ -18,6 +18,7 @@ struct kvm_memslots;enum kvm_mr_change ; +#include <asm/kvm_types.h> #include <asm/types.h> /*
@@ -49,4 +50,16 @@ struct gfn_to_hva_cache { struct kvm_memory_slot *memslot; }; +#ifdef KVM_ARCH_WANT_MMU_MEMORY_CACHE +/* +* We don't want allocation failures within the mmu code, so we preallocate +* enough memory for a single page fault in a cache. +*/ +struct kvm_mmu_memory_cache { +int nobjs; +void *objects[KVM_NR_MEM_OBJS ]; +}; +#endif + +#endif /* __KVM_TYPES_H__ */
virt/kvm/kvm_main.c +61 -0
73 lines not on this page · raw message
Signature · 1 line
2.18.0
kbuild test robot
build botRe: [PATCH v4 2/5] KVM: x86: Move mmu_memory_cache functions to common code
Thank you for the patch! Yet something to improve:
Hi Christoffer,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v5.4-rc7 next-20191108]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Christoffer-Dall/KVM-Unify-mmu_memory_cache-functionality-across-architectures/20191107-165047 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next config: sh-allmodconfig (attached as .config) compiler: sh4-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sh
If you fix the issue, kindly add following tag
All errors (new ones prefixed by >>):
In file included from <command-line>:0:0:
>> include/linux/kvm_types.h:21:10: fatal error: asm/kvm_types.h: No such file or directory
#include <asm/kvm_types.h>
^~~~~~~~~~~~~~~~~compilation terminated.
vim +21 include/linux/kvm_types.h
20
> 21 #include <asm/kvm_types.h>
22 #include <asm/types.h>
23 ---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation
PATCH v4 3/5 KVM: x86: Rename mmu_memory_cache to kvm_mmu_memcache
In reply to Christoffer DallAs we have moved the mmu memory cache definitions and functions to common code, they are exported as symols to the rest of the kernel.
Let's rename the functions and data types to have a kvm_ prefix to make it clear where these functions belong and take this chance to rename memory_cache to memcache to avoid overly long lines.
This is a bit tedious on the callsites but ends up looking more palatable.
--- arch/x86/include/asm/kvm_host.h | 6 ++--- arch/x86/include/asm/kvm_types.h | 4 ++-- arch/x86/kvm/mmu.c | 38 ++++++++++++++++---------------- arch/x86/kvm/paging_tmpl.h | 4 ++-- include/linux/kvm_host.h | 14 ++++++------ include/linux/kvm_types.h | 6 ++--- virt/kvm/kvm_main.c | 14 ++++++------ 7 files changed, 43 insertions(+), 43 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -3
@@ -586,9 +586,9 @@ struct kvm_vcpu_arch {*/ struct kvm_mmu *walk_mmu; -struct kvm_mmu_memory_cache mmu_pte_list_desc_cache; -struct kvm_mmu_memory_cache mmu_page_cache; -struct kvm_mmu_memory_cache mmu_page_header_cache; +struct kvm_mmu_memcache mmu_pte_list_desc_cache; +struct kvm_mmu_memcache mmu_page_cache; +struct kvm_mmu_memcache mmu_page_header_cache;/* * QEMU userspace and the guest each have their own FPU state.
arch/x86/include/asm/kvm_types.h +2 -2
@@ -2,8 +2,8 @@#ifndef _ASM_X86_KVM_TYPES_H#define _ASM_X86_KVM_TYPES_H -#define KVM_ARCH_WANT_MMU_MEMORY_CACHE +#define KVM_ARCH_WANT_MMU_MEMCACHE -#define KVM_NR_MEM_OBJS 40 +#define KVM_MMU_NR_MEMCACHE_OBJS 40#endif /* _ASM_X86_KVM_TYPES_H */
arch/x86/kvm/mmu.c +19 -19
@@ -1017,35 +1017,35 @@ static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)local_irq_enable (); } -static int mmu_topup_memory_caches (struct kvm_vcpu *vcpu) +static int kvm_mmu_topup_memcaches (struct kvm_vcpu *vcpu) {int r; - r =mmu_topup_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache, + r =kvm_mmu_topup_memcache (&vcpu->arch.mmu_pte_list_desc_cache, pte_list_desc_cache,8 +PTE_PREFETCH_NUM );if (r) goto out; - r =mmu_topup_memory_cache_page (&vcpu->arch.mmu_page_cache,8 ); + r =kvm_mmu_topup_memcache_page (&vcpu->arch.mmu_page_cache,8 );if (r) goto out; - r =mmu_topup_memory_cache (&vcpu->arch.mmu_page_header_cache, + r =kvm_mmu_topup_memcache (&vcpu->arch.mmu_page_header_cache, mmu_page_header_cache,4 ); out:return r; } -static void mmu_free_memory_caches (struct kvm_vcpu *vcpu) +static void kvm_mmu_free_memcaches (struct kvm_vcpu *vcpu) { -mmu_free_memory_cache (&vcpu->arch.mmu_pte_list_desc_cache, +kvm_mmu_free_memcache (&vcpu->arch.mmu_pte_list_desc_cache, pte_list_desc_cache); -mmu_free_memory_cache_page (&vcpu->arch.mmu_page_cache); -mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache, +kvm_mmu_free_memcache_page (&vcpu->arch.mmu_page_cache); +kvm_mmu_free_memcache (&vcpu->arch.mmu_page_header_cache, mmu_page_header_cache); }static struct pte_list_desc *mmu_alloc_pte_list_desc (struct kvm_vcpu *vcpu) { -return mmu_memory_cache_alloc (&vcpu->arch.mmu_pte_list_desc_cache); +return kvm_mmu_memcache_alloc (&vcpu->arch.mmu_pte_list_desc_cache); }static void mmu_free_pte_list_desc (struct pte_list_desc *pte_list_desc)
@@ -1371,10 +1371,10 @@ static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn,static bool rmap_can_add (struct kvm_vcpu *vcpu) { -struct kvm_mmu_memory_cache *cache; +struct kvm_mmu_memcache *cache; cache = &vcpu->arch.mmu_pte_list_desc_cache; -return mmu_memory_cache_free_objects (cache); +return kvm_mmu_memcache_free_objects (cache); }static int rmap_add (struct kvm_vcpu *vcpu,u64 *spte,gfn_t gfn)
@@ -2062,10 +2062,10 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct {struct kvm_mmu_page *sp; - sp =mmu_memory_cache_alloc (&vcpu->arch.mmu_page_header_cache); - sp->spt =mmu_memory_cache_alloc (&vcpu->arch.mmu_page_cache); + sp =kvm_mmu_memcache_alloc (&vcpu->arch.mmu_page_header_cache); + sp->spt =kvm_mmu_memcache_alloc (&vcpu->arch.mmu_page_cache);if (!direct) - sp->gfns =mmu_memory_cache_alloc (&vcpu->arch.mmu_page_cache); + sp->gfns =kvm_mmu_memcache_alloc (&vcpu->arch.mmu_page_cache);set_page_private (virt_to_page (sp->spt), (unsigned long )sp);/*
@@ -4005,7 +4005,7 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,if (page_fault_handle_page_track (vcpu, error_code, gfn))return RET_PF_EMULATE ; - r =mmu_topup_memory_caches (vcpu); + r =kvm_mmu_topup_memcaches (vcpu);if (r)return r;
@@ -4121,7 +4121,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,if (page_fault_handle_page_track (vcpu, error_code, gfn))return RET_PF_EMULATE ; - r =mmu_topup_memory_caches (vcpu); + r =kvm_mmu_topup_memcaches (vcpu);if (r)return r;
@@ -5102,7 +5102,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) {int r; - r =mmu_topup_memory_caches (vcpu); + r =kvm_mmu_topup_memcaches (vcpu);if (r) goto out; r =mmu_alloc_roots (vcpu);
@@ -5280,7 +5280,7 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,* or not since pte prefetch is skiped if it does not have * enough objects in the cache. */ -mmu_topup_memory_caches (vcpu); +kvm_mmu_topup_memcaches (vcpu);spin_lock (&vcpu->kvm->mmu_lock);
@@ -6169,7 +6169,7 @@ void kvm_mmu_destroy(struct kvm_vcpu *vcpu)kvm_mmu_unload (vcpu);free_mmu_pages (&vcpu->arch.root_mmu);free_mmu_pages (&vcpu->arch.guest_mmu); -mmu_free_memory_caches (vcpu); +kvm_mmu_free_memcaches (vcpu); }void kvm_mmu_module_exit (void )
arch/x86/kvm/paging_tmpl.h +2 -2
@@ -765,7 +765,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code); - r =mmu_topup_memory_caches (vcpu); + r =kvm_mmu_topup_memcaches (vcpu);if (r)return r;
@@ -885,7 +885,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa)* No need to check return value here, rmap_can_add() can * help us to skip pte prefetch later. */ -mmu_topup_memory_caches (vcpu); +kvm_mmu_topup_memcaches (vcpu);if (!VALID_PAGE (root_hpa)) {WARN_ON (1 );
include/linux/kvm_host.h +7 -7
@@ -788,15 +788,15 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool usermode_vcpu_not_eligible);void kvm_flush_remote_tlbs (struct kvm *kvm);void kvm_reload_remote_mmus (struct kvm *kvm); -#ifdef KVM_ARCH_WANT_MMU_MEMORY_CACHE -int mmu_topup_memory_cache (struct kvm_mmu_memory_cache *cache, +#ifdef KVM_ARCH_WANT_MMU_MEMCACHE +int kvm_mmu_topup_memcache (struct kvm_mmu_memcache *cache,struct kmem_cache *base_cache,int min); -int mmu_memory_cache_free_objects (struct kvm_mmu_memory_cache *cache); -void mmu_free_memory_cache (struct kvm_mmu_memory_cache *mc, +int kvm_mmu_memcache_free_objects (struct kvm_mmu_memcache *cache); +void kvm_mmu_free_memcache (struct kvm_mmu_memcache *mc,struct kmem_cache *cache); -int mmu_topup_memory_cache_page (struct kvm_mmu_memory_cache *cache,int min); -void mmu_free_memory_cache_page (struct kvm_mmu_memory_cache *mc); -void *mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc); +int kvm_mmu_topup_memcache_page (struct kvm_mmu_memcache *cache,int min); +void kvm_mmu_free_memcache_page (struct kvm_mmu_memcache *mc); +void *kvm_mmu_memcache_alloc (struct kvm_mmu_memcache *mc);#endif bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
include/linux/kvm_types.h +3 -3
@@ -50,14 +50,14 @@ struct gfn_to_hva_cache {struct kvm_memory_slot *memslot; }; -#ifdef KVM_ARCH_WANT_MMU_MEMORY_CACHE +#ifdef KVM_ARCH_WANT_MMU_MEMCACHE /* * We don't want allocation failures within the mmu code, so we preallocate * enough memory for a single page fault in a cache. */ -struct kvm_mmu_memory_cache { +struct kvm_mmu_memcache {int nobjs; -void *objects[KVM_NR_MEM_OBJS ]; +void *objects[KVM_MMU_NR_MEMCACHE_OBJS ]; };#endif
virt/kvm/kvm_main.c +7 -7
@@ -289,8 +289,8 @@ void kvm_reload_remote_mmus(struct kvm *kvm)kvm_make_all_cpus_request (kvm,KVM_REQ_MMU_RELOAD ); } -#ifdef KVM_ARCH_WANT_MMU_MEMORY_CACHE -int mmu_topup_memory_cache (struct kvm_mmu_memory_cache *cache, +#ifdef KVM_ARCH_WANT_MMU_MEMCACHE +int kvm_mmu_topup_memcache (struct kvm_mmu_memcache *cache,struct kmem_cache *base_cache,int min) {void *obj;
@@ -306,19 +306,19 @@ int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,return 0 ; } -int mmu_memory_cache_free_objects (struct kvm_mmu_memory_cache *cache) +int kvm_mmu_memcache_free_objects (struct kvm_mmu_memcache *cache) {return cache->nobjs; } -void mmu_free_memory_cache (struct kvm_mmu_memory_cache *mc, +void kvm_mmu_free_memcache (struct kvm_mmu_memcache *mc,struct kmem_cache *cache) {while (mc->nobjs)kmem_cache_free (cache, mc->objects[--mc->nobjs]); } -int mmu_topup_memory_cache_page (struct kvm_mmu_memory_cache *cache,int min) +int kvm_mmu_topup_memcache_page (struct kvm_mmu_memcache *cache,int min) {void *page;
@@ -333,13 +333,13 @@ int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache, int min)return 0 ; } -void mmu_free_memory_cache_page (struct kvm_mmu_memory_cache *mc) +void kvm_mmu_free_memcache_page (struct kvm_mmu_memcache *mc) {while (mc->nobjs)free_page ((unsigned long )mc->objects[--mc->nobjs]); } -void *mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc) +void *kvm_mmu_memcache_alloc (struct kvm_mmu_memcache *mc) {void *p;
Signature · 1 line
2.18.0
PATCH v4 4/5 KVM: arm/arm64: Move to common kvm_mmu_memcache infrastructure
In reply to Christoffer DallNow when we have a common mmu mmemcache implementation, we can reuse this for arm and arm64.
The common implementation has a slightly different behavior when allocating objects under high memory pressure; whereas the current arm/arm64 implementation will give up and return -ENOMEM if the full size of the cache cannot be allocated during topup, the common implementation is happy with any allocation between min and max. There should be no architecture-specific requirement for doing it one way or the other and it's in fact better to enforce a cross-architecture KVM policy on this behavior.
--- arch/arm/include/asm/kvm_host.h | 13 +----- arch/arm/include/asm/kvm_mmu.h | 2 +- arch/arm/include/asm/kvm_types.h | 4 ++ arch/arm64/include/asm/kvm_host.h | 13 +----- arch/arm64/include/asm/kvm_mmu.h | 2 +- arch/arm64/include/asm/kvm_types.h | 5 ++- virt/kvm/arm/arm.c | 2 +- virt/kvm/arm/mmu.c | 68 ++++++++---------------------- 8 files changed, 30 insertions(+), 79 deletions(-)
arch/arm/include/asm/kvm_host.h +1 -12
@@ -78,17 +78,6 @@ struct kvm_arch {u32 psci_version; }; -#define KVM_NR_MEM_OBJS 40 - -/* -* We don't want allocation failures within the mmu code, so we preallocate -* enough memory for a single page fault in a cache. -*/ -struct kvm_mmu_memory_cache { -int nobjs; -void *objects[KVM_NR_MEM_OBJS ]; -}; -struct kvm_vcpu_fault_info {u32 hsr;/* Hyp Syndrome Register */ u32 hxfar;/* Hyp Data/Inst. Fault Address Register */
@@ -196,7 +185,7 @@ struct kvm_vcpu_arch {struct kvm_decode mmio_decode;/* Cache some mmu pages needed inside spinlock regions */ -struct kvm_mmu_memory_cache mmu_page_cache; +struct kvm_mmu_memcache mmu_page_cache;struct vcpu_reset_state reset_state;
arch/arm/include/asm/kvm_mmu.h +1 -1
@@ -59,7 +59,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run); -void kvm_mmu_free_memory_caches (struct kvm_vcpu *vcpu); +void kvm_mmu_free_memcaches (struct kvm_vcpu *vcpu);phys_addr_t kvm_mmu_get_httbr (void );phys_addr_t kvm_get_idmap_vector (void );
arch/arm/include/asm/kvm_types.h +4 -0
@@ -2,4 +2,8 @@#ifndef _ASM_ARM_KVM_TYPES_H#define _ASM_ARM_KVM_TYPES_H +#define KVM_ARCH_WANT_MMU_MEMCACHE + +#define KVM_MMU_NR_MEMCACHE_OBJS 40 +#endif /* _ASM_ARM_KVM_TYPES_H */
arch/arm64/include/asm/kvm_host.h +1 -12
@@ -85,17 +85,6 @@ struct kvm_arch {u32 psci_version; }; -#define KVM_NR_MEM_OBJS 40 - -/* -* We don't want allocation failures within the mmu code, so we preallocate -* enough memory for a single page fault in a cache. -*/ -struct kvm_mmu_memory_cache { -int nobjs; -void *objects[KVM_NR_MEM_OBJS ]; -}; -struct kvm_vcpu_fault_info {u32 esr_el2;/* Hyp Syndrom Register */ u64 far_el2;/* Hyp Fault Address Register */
@@ -320,7 +309,7 @@ struct kvm_vcpu_arch {struct kvm_decode mmio_decode;/* Cache some mmu pages needed inside spinlock regions */ -struct kvm_mmu_memory_cache mmu_page_cache; +struct kvm_mmu_memcache mmu_page_cache;/* Target CPU and feature flags */ int target;
arch/arm64/include/asm/kvm_mmu.h +1 -1
@@ -160,7 +160,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run); -void kvm_mmu_free_memory_caches (struct kvm_vcpu *vcpu); +void kvm_mmu_free_memcaches (struct kvm_vcpu *vcpu);phys_addr_t kvm_mmu_get_httbr (void );phys_addr_t kvm_get_idmap_vector (void );
arch/arm64/include/asm/kvm_types.h +4 -1
@@ -2,5 +2,8 @@#ifndef _ASM_ARM64_KVM_TYPES_H#define _ASM_ARM64_KVM_TYPES_H -#endif /* _ASM_ARM64_KVM_TYPES_H */ +#define KVM_ARCH_WANT_MMU_MEMCACHE + +#define KVM_MMU_NR_MEMCACHE_OBJS 40 +#endif /* _ASM_ARM64_KVM_TYPES_H */
virt/kvm/arm/arm.c +1 -1
@@ -300,7 +300,7 @@ void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)if (vcpu->arch.has_run_once &&unlikely (!irqchip_in_kernel (vcpu->kvm)))static_branch_dec (&userspace_irqchip_in_use); -kvm_mmu_free_memory_caches (vcpu); +kvm_mmu_free_memcaches (vcpu);kvm_timer_vcpu_terminate (vcpu);kvm_pmu_vcpu_destroy (vcpu);kvm_vcpu_uninit (vcpu);
virt/kvm/arm/mmu.c +17 -51
165 lines not on this page · raw message
Signature · 1 line
2.18.0
PATCH v4 5/5 KVM: mips: Move to common kvm_mmu_memcache infrastructure
In reply to Christoffer DallNow that we have a common infrastructure for doing MMU cache allocations, use this for mips as well.
This will change the GFP flags used for mips from plain GFP_KERNEL to GFP_PGTABLE_USER. This means that mips KVM page table allocations now gain __GFP_ACCOUNT and __GFP_ZERO. There should be no harm in the former, and while the latter might result in slight overhead for zeroing the page, it seems this is what a hypervisor should do on page table allocations.
--- arch/mips/include/asm/kvm_host.h | 15 ++------- arch/mips/include/asm/kvm_types.h | 4 +++ arch/mips/kvm/mips.c | 2 +- arch/mips/kvm/mmu.c | 54 ++++++------------------------- 4 files changed, 17 insertions(+), 58 deletions(-)
arch/mips/include/asm/kvm_host.h +2 -13
@@ -293,17 +293,6 @@ struct kvm_mips_tlb {long tlb_lo[2 ]; }; -#define KVM_NR_MEM_OBJS 4 - -/* -* We don't want allocation failures within the mmu code, so we preallocate -* enough memory for a single page fault in a cache. -*/ -struct kvm_mmu_memory_cache { -int nobjs; -void *objects[KVM_NR_MEM_OBJS ]; -}; -#define KVM_MIPS_AUX_FPU 0x1#define KVM_MIPS_AUX_MSA 0x2
@@ -378,7 +367,7 @@ struct kvm_vcpu_arch {unsigned int last_user_gasid;/* Cache some mmu pages needed inside spinlock regions */ -struct kvm_mmu_memory_cache mmu_page_cache; +struct kvm_mmu_memcache mmu_page_cache;#ifdef CONFIG_KVM_MIPS_VZ /* vcpu's vzguestid is different on each host cpu in an smp system */
@@ -915,7 +904,7 @@ void kvm_mips_flush_gva_pt(pgd_t *pgd, enum kvm_mips_flush flags);bool kvm_mips_flush_gpa_pt (struct kvm *kvm,gfn_t start_gfn,gfn_t end_gfn);int kvm_mips_mkclean_gpa_pt (struct kvm *kvm,gfn_t start_gfn,gfn_t end_gfn);pgd_t *kvm_pgd_alloc (void ); -void kvm_mmu_free_memory_caches (struct kvm_vcpu *vcpu); +void kvm_mmu_free_memcaches (struct kvm_vcpu *vcpu);void kvm_trap_emul_invalidate_gva (struct kvm_vcpu *vcpu,unsigned long addr,bool user);void kvm_trap_emul_gva_lockless_begin (struct kvm_vcpu *vcpu);
arch/mips/include/asm/kvm_types.h +4 -0
@@ -2,4 +2,8 @@#ifndef _ASM_MIPS_KVM_TYPES_H#define _ASM_MIPS_KVM_TYPES_H +#define KVM_ARCH_WANT_MMU_MEMCACHE + +#define KVM_MMU_NR_MEMCACHE_OBJS 4 +#endif /* _ASM_MIPS_KVM_TYPES_H */
arch/mips/kvm/mips.c +1 -1
@@ -415,7 +415,7 @@ void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)kvm_mips_dump_stats (vcpu); -kvm_mmu_free_memory_caches (vcpu); +kvm_mmu_free_memcaches (vcpu);kfree (vcpu->arch.guest_ebase);kfree (vcpu->arch.kseg0_commpage);kfree (vcpu);
arch/mips/kvm/mmu.c +10 -44
107 lines not on this page · raw message
Signature · 1 line
2.18.0