diff mbox series

[kvmtool,v3,1/9] arm: Allow use of hugepage with 16K pagesize host

Message ID 20181220152126.18741-2-julien.grall@arm.com (mailing list archive)
State New, archived
Headers show
Series arm: Allow the user to specify where the RAM is placed in the memory | expand

Commit Message

Julien Grall Dec. 20, 2018, 3:21 p.m. UTC
From: Suzuki K Poulose <suzuki.poulose@arm.com>

With 16K pagesize, the hugepage size is 32M. Align the guest
memory to the hugepagesize for 16K.

Cc: Marc Zyngier <marc.zyngier@arm.com>
Cc: Andre Przywara <andre.przywara@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Julien Grall <julien.grall@arm.com>
---
 arm/kvm.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Comments

Suzuki K Poulose Jan. 22, 2019, 9:46 a.m. UTC | #1
Julien,

On 20/12/2018 15:21, Julien Grall wrote:
> From: Suzuki K Poulose <suzuki.poulose@arm.com>
> 
> With 16K pagesize, the hugepage size is 32M. Align the guest
> memory to the hugepagesize for 16K.
> 
> Cc: Marc Zyngier <marc.zyngier@arm.com>
> Cc: Andre Przywara <andre.przywara@arm.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
> Signed-off-by: Julien Grall <julien.grall@arm.com>
> ---
>   arm/kvm.c | 12 ++++++++++--
>   1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/arm/kvm.c b/arm/kvm.c
> index b824f63..5b1f73c 100644
> --- a/arm/kvm.c
> +++ b/arm/kvm.c
> @@ -59,14 +59,22 @@ void kvm__arch_set_cmdline(char *cmdline, bool video)
>   
>   void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
>   {
> +	unsigned long alignment;
> +
>   	/*
>   	 * Allocate guest memory. We must align our buffer to 64K to
>   	 * correlate with the maximum guest page size for virtio-mmio.
>   	 * If using THP, then our minimal alignment becomes 2M.
>   	 * 2M trumps 64K, so let's go with that.
> +	 * If we are running with 16K page size, align the memory to
> +	 * 32M, so that we can make use of the THP.
>   	 */


One issue that I realized with the above adjustment is when we specify a
hugetlbfs path, e.g, 1GB hugepages with 4K (with recently added PUD huge
page support at Stage2), we fail to allocate memory.


I think the whole alignment game should be performed only for the anon_mmap
case. If the hugetlbfs path is specified, we are guaranteed to get aligned
memory banks. Do you think you could fix it with this series (of course as
a separate patch), as it seems more relevant ? You could may be do :

	if (!hugetlbfs_path) {
> +		if (sysconf(_SC_PAGESIZE) == SZ_16K)
> +			alignment = SZ_32M;
> +		else
> +			alignment = SZ_2M;
	} else {
		alignment = 0;
	}


>   	kvm->ram_size = min(ram_size, (u64)ARM_MAX_MEMORY(kvm));
> -	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M;
> +	kvm->arch.ram_alloc_size = kvm->ram_size + alignment;


>   	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path,
>   						kvm->arch.ram_alloc_size);
>   
> @@ -75,7 +83,7 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
>   		    kvm->arch.ram_alloc_size, errno);
>   
>   	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
> -					SZ_2M);
> +					alignment);



Cheers
Suzuki
diff mbox series

Patch

diff --git a/arm/kvm.c b/arm/kvm.c
index b824f63..5b1f73c 100644
--- a/arm/kvm.c
+++ b/arm/kvm.c
@@ -59,14 +59,22 @@  void kvm__arch_set_cmdline(char *cmdline, bool video)
 
 void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
 {
+	unsigned long alignment;
+
 	/*
 	 * Allocate guest memory. We must align our buffer to 64K to
 	 * correlate with the maximum guest page size for virtio-mmio.
 	 * If using THP, then our minimal alignment becomes 2M.
 	 * 2M trumps 64K, so let's go with that.
+	 * If we are running with 16K page size, align the memory to
+	 * 32M, so that we can make use of the THP.
 	 */
+	if (sysconf(_SC_PAGESIZE) == SZ_16K)
+		alignment = SZ_32M;
+	else
+		alignment = SZ_2M;
 	kvm->ram_size = min(ram_size, (u64)ARM_MAX_MEMORY(kvm));
-	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M;
+	kvm->arch.ram_alloc_size = kvm->ram_size + alignment;
 	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path,
 						kvm->arch.ram_alloc_size);
 
@@ -75,7 +83,7 @@  void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
 		    kvm->arch.ram_alloc_size, errno);
 
 	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
-					SZ_2M);
+					alignment);
 
 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
 		MADV_MERGEABLE);