Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Fuad Tabba <fuad.tabba@linux.dev>
Cc: kvm@vger.kernel.org, kvmarm@lists.linux.dev,
	Will Deacon <will@kernel.org>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Andre Przywara <andre.przywara@arm.com>,
	Oliver Upton <oliver.upton@linux.dev>,
	Marc Zyngier <maz@kernel.org>, Fuad Tabba <tabba@google.com>
Subject: Re: [PATCH kvmtool v2 3/4] Add support for registering guest_memfd-backed memory regions
Date: Thu, 9 Jul 2026 16:07:43 +0100	[thread overview]
Message-ID: <ak-5P-bZkbO4UCce@raptor> (raw)
In-Reply-To: <20260708182510.2181857-4-fuad.tabba@linux.dev>

Hi Fuad,

On Wed, Jul 08, 2026 at 07:25:09PM +0100, Fuad Tabba wrote:
> Extend kvm__register_mem() with a guest_memfd file descriptor and
> offset. A bank with a guest_memfd is registered with
> KVM_SET_USER_MEMORY_REGION2 and KVM_MEM_GUEST_MEMFD, binding the
> memslot to the file. This prepares for backing guest RAM with
> guest_memfd on arm64.
> 
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
> ---
>  hw/cfi_flash.c    |  2 +-
>  include/kvm/kvm.h | 15 +++++++++++++--
>  kvm.c             | 36 ++++++++++++++++++++++++++----------
>  3 files changed, 40 insertions(+), 13 deletions(-)
> 
> diff --git a/hw/cfi_flash.c b/hw/cfi_flash.c
> index b68d9e0..4413955 100644
> --- a/hw/cfi_flash.c
> +++ b/hw/cfi_flash.c
> @@ -451,7 +451,7 @@ static int map_flash_memory(struct kvm *kvm, struct cfi_flash_device *sfdev)
>  	int ret;
>  
>  	ret = kvm__register_mem(kvm, sfdev->base_addr, sfdev->size,
> -				sfdev->flash_memory,
> +				sfdev->flash_memory, -1, 0,
>  				KVM_MEM_TYPE_RAM | KVM_MEM_TYPE_READONLY);
>  	if (!ret)
>  		sfdev->is_mapped = true;
> diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h
> index eb23e2f..5e7b5eb 100644
> --- a/include/kvm/kvm.h
> +++ b/include/kvm/kvm.h
> @@ -128,11 +128,22 @@ bool kvm__emulate_io(struct kvm_cpu *vcpu, u16 port, void *data, int direction,
>  bool kvm__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr, u8 *data, u32 len, u8 is_write);
>  int kvm__destroy_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr);
>  int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr,
> +		      int guest_memfd, u64 guest_memfd_offset,
>  		      enum kvm_mem_type type);
>  static inline int kvm__register_ram(struct kvm *kvm, u64 guest_phys, u64 size,
>  				    void *userspace_addr)
>  {
>  	return kvm__register_mem(kvm, guest_phys, size, userspace_addr,
> +				 -1, 0, KVM_MEM_TYPE_RAM);
> +}
> +
> +static inline int kvm__register_ram_guest_memfd(struct kvm *kvm, u64 guest_phys,
> +						u64 size, void *userspace_addr,
> +						int guest_memfd,
> +						u64 guest_memfd_offset)
> +{
> +	return kvm__register_mem(kvm, guest_phys, size, userspace_addr,
> +				 guest_memfd, guest_memfd_offset,
>  				 KVM_MEM_TYPE_RAM);

One small nitpick here, KVM_CREATE_GUEST_MEMFD returns -1 on error. If
callers forget to check the result of KVM_CREATE_GUEST_MEMFD and pass the
result to kvm__register_ram_guest_memfd(), the error won't be detected and
kvmtool will end up creating a non-guest_memfd backed memslot. Definitely
unexpected. It might be useful to someone, someday, to add a check here for
guest_memfd >= 0, but it's not terribly important.

Regardless:

Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>

Thanks,
Alex

>  }
>  
> @@ -140,13 +151,13 @@ static inline int kvm__register_dev_mem(struct kvm *kvm, u64 guest_phys,
>  					u64 size, void *userspace_addr)
>  {
>  	return kvm__register_mem(kvm, guest_phys, size, userspace_addr,
> -				 KVM_MEM_TYPE_DEVICE);
> +				 -1, 0, KVM_MEM_TYPE_DEVICE);
>  }
>  
>  static inline int kvm__reserve_mem(struct kvm *kvm, u64 guest_phys, u64 size)
>  {
>  	return kvm__register_mem(kvm, guest_phys, size, NULL,
> -				 KVM_MEM_TYPE_RESERVED);
> +				 -1, 0, KVM_MEM_TYPE_RESERVED);
>  }
>  
>  int __must_check kvm__register_iotrap(struct kvm *kvm, u64 phys_addr, u64 len,
> diff --git a/kvm.c b/kvm.c
> index 95ccfcb..947721b 100644
> --- a/kvm.c
> +++ b/kvm.c
> @@ -237,9 +237,9 @@ out:
>  }
>  
>  int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
> -		      void *userspace_addr, enum kvm_mem_type type)
> +		      void *userspace_addr, int guest_memfd,
> +		      u64 guest_memfd_offset, enum kvm_mem_type type)
>  {
> -	struct kvm_userspace_memory_region mem;
>  	struct kvm_mem_bank *merged = NULL;
>  	struct kvm_mem_bank *bank;
>  	struct list_head *prev_entry;
> @@ -320,15 +320,31 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size,
>  		flags |= KVM_MEM_READONLY;
>  
>  	if (type != KVM_MEM_TYPE_RESERVED) {
> -		mem = (struct kvm_userspace_memory_region) {
> -			.slot			= slot,
> -			.flags			= flags,
> -			.guest_phys_addr	= guest_phys,
> -			.memory_size		= size,
> -			.userspace_addr		= (unsigned long)userspace_addr,
> -		};
> +		if (guest_memfd >= 0) {
> +			struct kvm_userspace_memory_region2 mem2 = {
> +				.slot			= slot,
> +				.flags			= flags | KVM_MEM_GUEST_MEMFD,
> +				.guest_phys_addr	= guest_phys,
> +				.memory_size		= size,
> +				.userspace_addr		= (unsigned long)userspace_addr,
> +				.guest_memfd		= guest_memfd,
> +				.guest_memfd_offset	= guest_memfd_offset,
> +			};
>  
> -		ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &mem);
> +			ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION2,
> +				    &mem2);
> +		} else {
> +			struct kvm_userspace_memory_region mem = {
> +				.slot			= slot,
> +				.flags			= flags,
> +				.guest_phys_addr	= guest_phys,
> +				.memory_size		= size,
> +				.userspace_addr		= (unsigned long)userspace_addr,
> +			};
> +
> +			ret = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION,
> +				    &mem);
> +		}
>  		if (ret < 0) {
>  			ret = -errno;
>  			goto out;
> -- 
> 2.39.5
> 

  reply	other threads:[~2026-07-09 15:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08 18:25 [PATCH kvmtool v2 0/4] Add guest_memfd support for arm64 Fuad Tabba
2026-07-08 18:25 ` [PATCH kvmtool v2 1/4] Initialize the return value in kvm__for_each_mem_bank() Fuad Tabba
2026-07-08 18:25 ` [PATCH kvmtool v2 2/4] Remove newline from end of die() aborts Fuad Tabba
2026-07-08 18:25 ` [PATCH kvmtool v2 3/4] Add support for registering guest_memfd-backed memory regions Fuad Tabba
2026-07-09 15:07   ` Alexandru Elisei [this message]
2026-07-09 15:32     ` Fuad Tabba
2026-07-08 18:25 ` [PATCH kvmtool v2 4/4] arm64: Add --guest-memfd option to back guest RAM with guest_memfd Fuad Tabba
2026-07-09 15:08   ` Alexandru Elisei
2026-07-09 15:33     ` Fuad Tabba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ak-5P-bZkbO4UCce@raptor \
    --to=alexandru.elisei@arm.com \
    --cc=andre.przywara@arm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oliver.upton@linux.dev \
    --cc=suzuki.poulose@arm.com \
    --cc=tabba@google.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox