Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH kvmtool] arm64: THPs for systems with pages bigger than 4K
@ 2026-10-19 14:59 Vincent Donnefort
  2026-10-20 14:43 ` Suzuki K Poulose
  0 siblings, 1 reply; 2+ messages in thread
From: Vincent Donnefort @ 2026-10-19 14:59 UTC (permalink / raw)
  To: will, julien.thierry.kdev
  Cc: kvm, kvmarm, linux-arm-kernel, maz, oliver.upton, suzuki.poulose,
	alexandru.elisei, andre.przywara, Vincent Donnefort

The THP support imposes an alignment of the memslot userspace address to
the size of a THP (PMD_SIZE). To do so, kvmtool overallocates the RAM
size and aligns it. However it assumes PMD_SIZE is 2MiB, which is only
true on 4KiB page systems.

Use a huge-page size, based on the system page size. This value can be
massive for a 64KiB page system, so skip the alignment if we couldn't
even install one THP.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

diff --git a/arm64/kvm.c b/arm64/kvm.c
index 36b3284e4a92..2c01dc32fa9a 100644
--- a/arm64/kvm.c
+++ b/arm64/kvm.c
@@ -18,6 +18,8 @@
 
 #include <asm/image.h>
 
+#define HUGEPAGE_SIZE (PAGE_SIZE * (PAGE_SIZE / 8UL))
+
 static struct arm64_image_header *kernel_header;
 
 struct kvm_ext kvm_req_ext[] = {
@@ -35,20 +37,23 @@ bool kvm__arch_cpu_supports_vm(void)
 
 void kvm__init_ram(struct kvm *kvm)
 {
-	u64 phys_start, phys_size;
+	u64 phys_start, phys_size, ram_alloc_align = SZ_64K;
 	void *host_mem;
 	int err;
 
 	/*
-	 * 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.
+	 * 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 trumps 64K, so let's go with that.
 	 */
 	kvm->ram_size = kvm->cfg.ram_size;
 	kvm->arch.ram_alloc_size = kvm->ram_size;
-	if (!kvm->cfg.hugetlbfs_path)
-		kvm->arch.ram_alloc_size += SZ_2M;
+	if (!kvm->cfg.hugetlbfs_path) {
+		if (kvm->ram_size >= HUGEPAGE_SIZE)
+			ram_alloc_align = HUGEPAGE_SIZE;
+
+		kvm->arch.ram_alloc_size += ram_alloc_align;
+	}
 
 	if (kvm->cfg.arch.guest_memfd)
 		kvm->arch.ram_alloc_start = mmap_guest_memfd(kvm,
@@ -63,7 +68,7 @@ void kvm__init_ram(struct kvm *kvm)
 		    kvm->arch.ram_alloc_size, errno);
 
 	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
-					SZ_2M);
+					ram_alloc_align);
 
 	if (!kvm->cfg.arch.guest_memfd) {
 		madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,

base-commit: f67bc0bdae9433a9cfd05e65ea2c1bb6102566d9
-- 
2.56.0.176.g3575e65682-goog



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH kvmtool] arm64: THPs for systems with pages bigger than 4K
  2026-10-19 14:59 [PATCH kvmtool] arm64: THPs for systems with pages bigger than 4K Vincent Donnefort
@ 2026-10-20 14:43 ` Suzuki K Poulose
  0 siblings, 0 replies; 2+ messages in thread
From: Suzuki K Poulose @ 2026-10-20 14:43 UTC (permalink / raw)
  To: Vincent Donnefort, will, julien.thierry.kdev
  Cc: kvm, kvmarm, linux-arm-kernel, maz, oliver.upton,
	alexandru.elisei, andre.przywara

On 19/10/2026 15:59, Vincent Donnefort wrote:
> The THP support imposes an alignment of the memslot userspace address to
> the size of a THP (PMD_SIZE). To do so, kvmtool overallocates the RAM
> size and aligns it. However it assumes PMD_SIZE is 2MiB, which is only
> true on 4KiB page systems.
> 
> Use a huge-page size, based on the system page size. This value can be
> massive for a 64KiB page system, so skip the alignment if we couldn't
> even install one THP.
> 
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> 
> diff --git a/arm64/kvm.c b/arm64/kvm.c
> index 36b3284e4a92..2c01dc32fa9a 100644
> --- a/arm64/kvm.c
> +++ b/arm64/kvm.c
> @@ -18,6 +18,8 @@
>   
>   #include <asm/image.h>
>   
> +#define HUGEPAGE_SIZE (PAGE_SIZE * (PAGE_SIZE / 8UL))
> +
>   static struct arm64_image_header *kernel_header;
>   
>   struct kvm_ext kvm_req_ext[] = {
> @@ -35,20 +37,23 @@ bool kvm__arch_cpu_supports_vm(void)
>   
>   void kvm__init_ram(struct kvm *kvm)
>   {
> -	u64 phys_start, phys_size;
> +	u64 phys_start, phys_size, ram_alloc_align = SZ_64K;
>   	void *host_mem;
>   	int err;
>   
>   	/*
> -	 * 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.
> +	 * 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 trumps 64K, so let's go with that.
>   	 */
>   	kvm->ram_size = kvm->cfg.ram_size;
>   	kvm->arch.ram_alloc_size = kvm->ram_size;
> -	if (!kvm->cfg.hugetlbfs_path)
> -		kvm->arch.ram_alloc_size += SZ_2M;
> +	if (!kvm->cfg.hugetlbfs_path) {
> +		if (kvm->ram_size >= HUGEPAGE_SIZE)
> +			ram_alloc_align = HUGEPAGE_SIZE;

There is logic in builtin-run.c to detect the host_ram_size(), could we
use that instead to decide if we should apply this alignment ? Or retry
the RAM allocation without the alignment on failure ?

It doesn't look like a good idea to hard code this based just on the
requested RAM size.

Cheers
Suzuki



> +
> +		kvm->arch.ram_alloc_size += ram_alloc_align;
> +	}
>   
>   	if (kvm->cfg.arch.guest_memfd)
>   		kvm->arch.ram_alloc_start = mmap_guest_memfd(kvm,
> @@ -63,7 +68,7 @@ void kvm__init_ram(struct kvm *kvm)
>   		    kvm->arch.ram_alloc_size, errno);
>   
>   	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
> -					SZ_2M);
> +					ram_alloc_align);
>   
>   	if (!kvm->cfg.arch.guest_memfd) {
>   		madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
> 
> base-commit: f67bc0bdae9433a9cfd05e65ea2c1bb6102566d9



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-20 14:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-19 14:59 [PATCH kvmtool] arm64: THPs for systems with pages bigger than 4K Vincent Donnefort
2026-10-20 14:43 ` Suzuki K Poulose

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox