diff mbox series

[kvmtool,03/16] virtio/scsi: Allow the use of multiple banks

Message ID 1569245722-23375-4-git-send-email-alexandru.elisei@arm.com (mailing list archive)
State New, archived
Headers show
Series arm: Allow the user to define the memory layout | expand

Commit Message

Alexandru Elisei Sept. 23, 2019, 1:35 p.m. UTC
From: Julien Grall <julien.grall@arm.com>

At the moment, virtio scsi registers only one bank starting at 0. On some
architectures (like on x86, for example), this may not be true and the
guest may have multiple memory regions.

Register all the memory regions to vhost by browsing kvm->mem_banks. The
code is based on the virtio_net__vhost_init implementation.

Signed-off-by: Julien Grall <julien.grall@arm.com>
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
---
 virtio/scsi.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

Comments

Andre Przywara Nov. 6, 2019, 4:48 p.m. UTC | #1
On Mon, 23 Sep 2019 14:35:09 +0100
Alexandru Elisei <alexandru.elisei@arm.com> wrote:

Hi,

> From: Julien Grall <julien.grall@arm.com>
> 
> At the moment, virtio scsi registers only one bank starting at 0. On some
> architectures (like on x86, for example), this may not be true and the
> guest may have multiple memory regions.
> 
> Register all the memory regions to vhost by browsing kvm->mem_banks. The
> code is based on the virtio_net__vhost_init implementation.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>
> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>

Reviewed-by: Andre Przywara <andre.przywara@arm.com>

Cheers,
Andre

> ---
>  virtio/scsi.c | 21 +++++++++++++--------
>  1 file changed, 13 insertions(+), 8 deletions(-)
> 
> diff --git a/virtio/scsi.c b/virtio/scsi.c
> index a72bb2a9a206..63fc4f4635a2 100644
> --- a/virtio/scsi.c
> +++ b/virtio/scsi.c
> @@ -190,24 +190,29 @@ static struct virtio_ops scsi_dev_virtio_ops = {
>  
>  static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev)
>  {
> +	struct kvm_mem_bank *bank;
>  	struct vhost_memory *mem;
>  	u64 features;
> -	int r;
> +	int r, i;
>  
>  	sdev->vhost_fd = open("/dev/vhost-scsi", O_RDWR);
>  	if (sdev->vhost_fd < 0)
>  		die_perror("Failed openning vhost-scsi device");
>  
> -	mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region));
> +	mem = calloc(1, sizeof(*mem) + kvm->mem_slots * sizeof(struct vhost_memory_region));
>  	if (mem == NULL)
>  		die("Failed allocating memory for vhost memory map");
>  
> -	mem->nregions = 1;
> -	mem->regions[0] = (struct vhost_memory_region) {
> -		.guest_phys_addr	= 0,
> -		.memory_size		= kvm->ram_size,
> -		.userspace_addr		= (unsigned long)kvm->ram_start,
> -	};
> +	i = 0;
> +	list_for_each_entry(bank, &kvm->mem_banks, list) {
> +		mem->regions[i] = (struct vhost_memory_region) {
> +			.guest_phys_addr	= bank->guest_phys_addr,
> +			.memory_size		= bank->size,
> +			.userspace_addr		= (unsigned long)bank->host_addr,
> +		};
> +		i++;
> +	}
> +	mem->nregions = i;
>  
>  	r = ioctl(sdev->vhost_fd, VHOST_SET_OWNER);
>  	if (r != 0)
Suzuki K Poulose Feb. 5, 2020, 6:07 p.m. UTC | #2
On 23/09/2019 14:35, Alexandru Elisei wrote:
> From: Julien Grall <julien.grall@arm.com>
> 
> At the moment, virtio scsi registers only one bank starting at 0. On some
> architectures (like on x86, for example), this may not be true and the
> guest may have multiple memory regions.
> 
> Register all the memory regions to vhost by browsing kvm->mem_banks. The
> code is based on the virtio_net__vhost_init implementation.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>
> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
> ---
>   virtio/scsi.c | 21 +++++++++++++--------
>   1 file changed, 13 insertions(+), 8 deletions(-)
> 
> diff --git a/virtio/scsi.c b/virtio/scsi.c
> index a72bb2a9a206..63fc4f4635a2 100644
> --- a/virtio/scsi.c
> +++ b/virtio/scsi.c
> @@ -190,24 +190,29 @@ static struct virtio_ops scsi_dev_virtio_ops = {
>   
>   static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev)
>   {
> +	struct kvm_mem_bank *bank;
>   	struct vhost_memory *mem;
>   	u64 features;
> -	int r;
> +	int r, i;
>   
>   	sdev->vhost_fd = open("/dev/vhost-scsi", O_RDWR);
>   	if (sdev->vhost_fd < 0)
>   		die_perror("Failed openning vhost-scsi device");
>   
> -	mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region));
> +	mem = calloc(1, sizeof(*mem) + kvm->mem_slots * sizeof(struct vhost_memory_region));
>   	if (mem == NULL)
>   		die("Failed allocating memory for vhost memory map");
>   
> -	mem->nregions = 1;
> -	mem->regions[0] = (struct vhost_memory_region) {
> -		.guest_phys_addr	= 0,
> -		.memory_size		= kvm->ram_size,
> -		.userspace_addr		= (unsigned long)kvm->ram_start,
> -	};
> +	i = 0;
> +	list_for_each_entry(bank, &kvm->mem_banks, list) {
> +		mem->regions[i] = (struct vhost_memory_region) {
> +			.guest_phys_addr	= bank->guest_phys_addr,
> +			.memory_size		= bank->size,
> +			.userspace_addr		= (unsigned long)bank->host_addr,
> +		};
> +		i++;
> +	}
> +	mem->nregions = i;
>   

Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
diff mbox series

Patch

diff --git a/virtio/scsi.c b/virtio/scsi.c
index a72bb2a9a206..63fc4f4635a2 100644
--- a/virtio/scsi.c
+++ b/virtio/scsi.c
@@ -190,24 +190,29 @@  static struct virtio_ops scsi_dev_virtio_ops = {
 
 static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev)
 {
+	struct kvm_mem_bank *bank;
 	struct vhost_memory *mem;
 	u64 features;
-	int r;
+	int r, i;
 
 	sdev->vhost_fd = open("/dev/vhost-scsi", O_RDWR);
 	if (sdev->vhost_fd < 0)
 		die_perror("Failed openning vhost-scsi device");
 
-	mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region));
+	mem = calloc(1, sizeof(*mem) + kvm->mem_slots * sizeof(struct vhost_memory_region));
 	if (mem == NULL)
 		die("Failed allocating memory for vhost memory map");
 
-	mem->nregions = 1;
-	mem->regions[0] = (struct vhost_memory_region) {
-		.guest_phys_addr	= 0,
-		.memory_size		= kvm->ram_size,
-		.userspace_addr		= (unsigned long)kvm->ram_start,
-	};
+	i = 0;
+	list_for_each_entry(bank, &kvm->mem_banks, list) {
+		mem->regions[i] = (struct vhost_memory_region) {
+			.guest_phys_addr	= bank->guest_phys_addr,
+			.memory_size		= bank->size,
+			.userspace_addr		= (unsigned long)bank->host_addr,
+		};
+		i++;
+	}
+	mem->nregions = i;
 
 	r = ioctl(sdev->vhost_fd, VHOST_SET_OWNER);
 	if (r != 0)