diff mbox series

[kvmtool,v3,3/9] virtio/scsi: Allow to use multiple banks

Message ID 20181220152126.18741-4-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
At the moment, virtio scsi only register a bank starting at 0. On some
architectures this may not be true and the guest may have multiple
memory region.

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>

---

This code was not tested as I don't have any setup with scsi.

    Changes in v3:
        - Don't forget to increment i in the loop.
---
 virtio/scsi.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

Comments

Will Deacon Jan. 22, 2019, 5:44 a.m. UTC | #1
On Thu, Dec 20, 2018 at 03:21:20PM +0000, Julien Grall wrote:
> At the moment, virtio scsi only register a bank starting at 0. On some
> architectures this may not be true and the guest may have multiple
> memory region.
> 
> 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>
> 
> ---
> 
> This code was not tested as I don't have any setup with scsi.
> 
>     Changes in v3:
>         - Don't forget to increment i in the loop.
> ---
>  virtio/scsi.c | 21 +++++++++++++--------
>  1 file changed, 13 insertions(+), 8 deletions(-)
> 
> diff --git a/virtio/scsi.c b/virtio/scsi.c
> index a429ac8..e541ab5 100644
> --- a/virtio/scsi.c
> +++ b/virtio/scsi.c
> @@ -178,24 +178,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[0] = (struct vhost_memory_region) {
> +			.guest_phys_addr	= bank->guest_phys_addr,
> +			.memory_size		= bank->size,
> +			.userspace_addr		= (unsigned long)bank->host_addr,
> +		};

Should this be mem->regions[i] ?

Maybe we should just remove this altogether if nobody is using it.

Will
Suzuki K Poulose Jan. 22, 2019, 9:22 a.m. UTC | #2
On 22/01/2019 05:44, Will Deacon wrote:
> On Thu, Dec 20, 2018 at 03:21:20PM +0000, Julien Grall wrote:
>> At the moment, virtio scsi only register a bank starting at 0. On some
>> architectures this may not be true and the guest may have multiple
>> memory region.
>>
>> 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>
>>

>> diff --git a/virtio/scsi.c b/virtio/scsi.c
>> index a429ac8..e541ab5 100644
>> --- a/virtio/scsi.c
>> +++ b/virtio/scsi.c
>> @@ -178,24 +178,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[0] = (struct vhost_memory_region) {
>> +			.guest_phys_addr	= bank->guest_phys_addr,
>> +			.memory_size		= bank->size,
>> +			.userspace_addr		= (unsigned long)bank->host_addr,
>> +		};
> 
> Should this be mem->regions[i] ?
> 
> Maybe we should just remove this altogether if nobody is using it.

And also incrementing i in the loop. I assume we are adding the support for this
by specifying the memory placement option later in the series. I had raised this
in one of the previous iterations.

Cheers
Suzuki
Julien Grall Feb. 19, 2019, 5:56 p.m. UTC | #3
Hi,

On 1/22/19 9:22 AM, Suzuki K Poulose wrote:
> 
> 
> On 22/01/2019 05:44, Will Deacon wrote:
>> On Thu, Dec 20, 2018 at 03:21:20PM +0000, Julien Grall wrote:
>>> At the moment, virtio scsi only register a bank starting at 0. On some
>>> architectures this may not be true and the guest may have multiple
>>> memory region.
>>>
>>> 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>
>>>
> 
>>> diff --git a/virtio/scsi.c b/virtio/scsi.c
>>> index a429ac8..e541ab5 100644
>>> --- a/virtio/scsi.c
>>> +++ b/virtio/scsi.c
>>> @@ -178,24 +178,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[0] = (struct vhost_memory_region) {
>>> +            .guest_phys_addr    = bank->guest_phys_addr,
>>> +            .memory_size        = bank->size,
>>> +            .userspace_addr        = (unsigned long)bank->host_addr,
>>> +        };
>>
>> Should this be mem->regions[i] ?
>>
>> Maybe we should just remove this altogether if nobody is using it.
> 
> And also incrementing i in the loop. I assume we are adding the support 
> for this
> by specifying the memory placement option later in the series. I had 
> raised this
> in one of the previous iterations.

I have incremented 'i' in this version but forgot to update 
mem->regions[i] :/. Sorry for that.

Cheers,
diff mbox series

Patch

diff --git a/virtio/scsi.c b/virtio/scsi.c
index a429ac8..e541ab5 100644
--- a/virtio/scsi.c
+++ b/virtio/scsi.c
@@ -178,24 +178,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[0] = (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)