[21.02,1/3] vhost: refactor postcopy region registration

Message ID 20201116113620.587073-2-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series vhost: vhost_user_set_mem_table refactoring |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Maxime Coquelin Nov. 16, 2020, 11:36 a.m. UTC
  This patch moves the registration of memory regions to
userfaultfd to a dedicated function, with the goal of
simplifying VHOST_USER_SET_MEM_TABLE request handling
function.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost_user.c | 77 +++++++++++++++++++++--------------
 1 file changed, 46 insertions(+), 31 deletions(-)
  

Comments

Chenbo Xia Dec. 9, 2020, 2:16 p.m. UTC | #1
Hi Maxime,

> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Monday, November 16, 2020 7:36 PM
> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; Ding, Xuan
> <xuan.ding@intel.com>
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [PATCH 21.02 1/3] vhost: refactor postcopy region registration
> 
> This patch moves the registration of memory regions to
> userfaultfd to a dedicated function, with the goal of
> simplifying VHOST_USER_SET_MEM_TABLE request handling
> function.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  lib/librte_vhost/vhost_user.c | 77 +++++++++++++++++++++--------------
>  1 file changed, 46 insertions(+), 31 deletions(-)
> 
> diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
> index 45c8ac09da..b8a9e41a2d 100644
> --- a/lib/librte_vhost/vhost_user.c
> +++ b/lib/librte_vhost/vhost_user.c
> @@ -998,6 +998,49 @@ vhost_memory_changed(struct VhostUserMemory *new,
>  	return false;
>  }
> 
> +#ifdef RTE_LIBRTE_VHOST_POSTCOPY
> +static int
> +vhost_user_postcopy_region_register(struct virtio_net *dev,
> +		struct rte_vhost_mem_region *reg)
> +{
> +	struct uffdio_register reg_struct;
> +
> +	/*
> +	 * Let's register all the mmap'ed area to ensure
> +	 * alignment on page boundary.
> +	 */
> +	reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
> +	reg_struct.range.len = reg->mmap_size;
> +	reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
> +
> +	if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
> +				&reg_struct)) {
> +		VHOST_LOG_CONFIG(ERR, "Failed to register ufd for region "
> +				"%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
> +				(uint64_t)reg_struct.range.start,
> +				(uint64_t)reg_struct.range.start +
> +				(uint64_t)reg_struct.range.len - 1,
> +				dev->postcopy_ufd,
> +				strerror(errno));
> +		return -1;
> +	}
> +
> +	VHOST_LOG_CONFIG(INFO, "\t userfaultfd registered for range : %"
> PRIx64 " - %" PRIx64 "\n",
> +			(uint64_t)reg_struct.range.start,
> +			(uint64_t)reg_struct.range.start +
> +			(uint64_t)reg_struct.range.len - 1);
> +
> +	return 0;
> +}
> +#else
> +static int
> +vhost_user_postcopy_region_register(struct virtio_net *dev,
> +		struct rte_vhost_mem_region *reg)
> +{
> +	return -1;
> +}

Better add __rte_unused here to avoid warnings when postcopy not configured 😊

Thanks,
Chenbo

> +#endif
> +
>  static int
>  vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg
> *msg,
>  			int main_fd)
> @@ -1209,38 +1252,10 @@ vhost_user_set_mem_table(struct virtio_net **pdev,
> struct VhostUserMsg *msg,
>  		}
> 
>  		/* Now userfault register and we can use the memory */
> -		for (i = 0; i < memory->nregions; i++) {
> -#ifdef RTE_LIBRTE_VHOST_POSTCOPY
> -			reg = &dev->mem->regions[i];
> -			struct uffdio_register reg_struct;
> -
> -			/*
> -			 * Let's register all the mmap'ed area to ensure
> -			 * alignment on page boundary.
> -			 */
> -			reg_struct.range.start =
> -				(uint64_t)(uintptr_t)reg->mmap_addr;
> -			reg_struct.range.len = reg->mmap_size;
> -			reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
> -
> -			if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
> -						&reg_struct)) {
> -				VHOST_LOG_CONFIG(ERR,
> -					"Failed to register ufd for region %d: (ufd
> = %d) %s\n",
> -					i, dev->postcopy_ufd,
> -					strerror(errno));
> +		for (i = 0; i < memory->nregions; i++)
> +			if (vhost_user_postcopy_region_register(dev,
> +						&dev->mem->regions[i]) < 0)
>  				goto free_mem_table;
> -			}
> -			VHOST_LOG_CONFIG(INFO,
> -				"\t userfaultfd registered for range : "
> -				"%" PRIx64 " - %" PRIx64 "\n",
> -				(uint64_t)reg_struct.range.start,
> -				(uint64_t)reg_struct.range.start +
> -				(uint64_t)reg_struct.range.len - 1);
> -#else
> -			goto free_mem_table;
> -#endif
> -		}
>  	}
> 
>  	for (i = 0; i < dev->nr_vring; i++) {
> --
> 2.26.2
  
Maxime Coquelin Dec. 22, 2020, 10:51 a.m. UTC | #2
Hi Chenbo,

On 12/9/20 3:16 PM, Xia, Chenbo wrote:
> Hi Maxime,
> 
>> -----Original Message-----
>> From: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Sent: Monday, November 16, 2020 7:36 PM
>> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; Ding, Xuan
>> <xuan.ding@intel.com>
>> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
>> Subject: [PATCH 21.02 1/3] vhost: refactor postcopy region registration
>>
>> This patch moves the registration of memory regions to
>> userfaultfd to a dedicated function, with the goal of
>> simplifying VHOST_USER_SET_MEM_TABLE request handling
>> function.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> ---
>>  lib/librte_vhost/vhost_user.c | 77 +++++++++++++++++++++--------------
>>  1 file changed, 46 insertions(+), 31 deletions(-)
>>
>> diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
>> index 45c8ac09da..b8a9e41a2d 100644
>> --- a/lib/librte_vhost/vhost_user.c
>> +++ b/lib/librte_vhost/vhost_user.c
>> @@ -998,6 +998,49 @@ vhost_memory_changed(struct VhostUserMemory *new,
>>  	return false;
>>  }
>>
>> +#ifdef RTE_LIBRTE_VHOST_POSTCOPY
>> +static int
>> +vhost_user_postcopy_region_register(struct virtio_net *dev,
>> +		struct rte_vhost_mem_region *reg)
>> +{
>> +	struct uffdio_register reg_struct;
>> +
>> +	/*
>> +	 * Let's register all the mmap'ed area to ensure
>> +	 * alignment on page boundary.
>> +	 */
>> +	reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
>> +	reg_struct.range.len = reg->mmap_size;
>> +	reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
>> +
>> +	if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
>> +				&reg_struct)) {
>> +		VHOST_LOG_CONFIG(ERR, "Failed to register ufd for region "
>> +				"%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
>> +				(uint64_t)reg_struct.range.start,
>> +				(uint64_t)reg_struct.range.start +
>> +				(uint64_t)reg_struct.range.len - 1,
>> +				dev->postcopy_ufd,
>> +				strerror(errno));
>> +		return -1;
>> +	}
>> +
>> +	VHOST_LOG_CONFIG(INFO, "\t userfaultfd registered for range : %"
>> PRIx64 " - %" PRIx64 "\n",
>> +			(uint64_t)reg_struct.range.start,
>> +			(uint64_t)reg_struct.range.start +
>> +			(uint64_t)reg_struct.range.len - 1);
>> +
>> +	return 0;
>> +}
>> +#else
>> +static int
>> +vhost_user_postcopy_region_register(struct virtio_net *dev,
>> +		struct rte_vhost_mem_region *reg)
>> +{
>> +	return -1;
>> +}
> 
> Better add __rte_unused here to avoid warnings when postcopy not configured 😊

Good catch!
I will post a v2 with adding these.

Thanks,
Maxime

> Thanks,
> Chenbo
> 
>> +#endif
>> +
>>  static int
>>  vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg
>> *msg,
>>  			int main_fd)
>> @@ -1209,38 +1252,10 @@ vhost_user_set_mem_table(struct virtio_net **pdev,
>> struct VhostUserMsg *msg,
>>  		}
>>
>>  		/* Now userfault register and we can use the memory */
>> -		for (i = 0; i < memory->nregions; i++) {
>> -#ifdef RTE_LIBRTE_VHOST_POSTCOPY
>> -			reg = &dev->mem->regions[i];
>> -			struct uffdio_register reg_struct;
>> -
>> -			/*
>> -			 * Let's register all the mmap'ed area to ensure
>> -			 * alignment on page boundary.
>> -			 */
>> -			reg_struct.range.start =
>> -				(uint64_t)(uintptr_t)reg->mmap_addr;
>> -			reg_struct.range.len = reg->mmap_size;
>> -			reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
>> -
>> -			if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
>> -						&reg_struct)) {
>> -				VHOST_LOG_CONFIG(ERR,
>> -					"Failed to register ufd for region %d: (ufd
>> = %d) %s\n",
>> -					i, dev->postcopy_ufd,
>> -					strerror(errno));
>> +		for (i = 0; i < memory->nregions; i++)
>> +			if (vhost_user_postcopy_region_register(dev,
>> +						&dev->mem->regions[i]) < 0)
>>  				goto free_mem_table;
>> -			}
>> -			VHOST_LOG_CONFIG(INFO,
>> -				"\t userfaultfd registered for range : "
>> -				"%" PRIx64 " - %" PRIx64 "\n",
>> -				(uint64_t)reg_struct.range.start,
>> -				(uint64_t)reg_struct.range.start +
>> -				(uint64_t)reg_struct.range.len - 1);
>> -#else
>> -			goto free_mem_table;
>> -#endif
>> -		}
>>  	}
>>
>>  	for (i = 0; i < dev->nr_vring; i++) {
>> --
>> 2.26.2
>
  

Patch

diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.c
index 45c8ac09da..b8a9e41a2d 100644
--- a/lib/librte_vhost/vhost_user.c
+++ b/lib/librte_vhost/vhost_user.c
@@ -998,6 +998,49 @@  vhost_memory_changed(struct VhostUserMemory *new,
 	return false;
 }
 
+#ifdef RTE_LIBRTE_VHOST_POSTCOPY
+static int
+vhost_user_postcopy_region_register(struct virtio_net *dev,
+		struct rte_vhost_mem_region *reg)
+{
+	struct uffdio_register reg_struct;
+
+	/*
+	 * Let's register all the mmap'ed area to ensure
+	 * alignment on page boundary.
+	 */
+	reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
+	reg_struct.range.len = reg->mmap_size;
+	reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
+
+	if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
+				&reg_struct)) {
+		VHOST_LOG_CONFIG(ERR, "Failed to register ufd for region "
+				"%" PRIx64 " - %" PRIx64 " (ufd = %d) %s\n",
+				(uint64_t)reg_struct.range.start,
+				(uint64_t)reg_struct.range.start +
+				(uint64_t)reg_struct.range.len - 1,
+				dev->postcopy_ufd,
+				strerror(errno));
+		return -1;
+	}
+
+	VHOST_LOG_CONFIG(INFO, "\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64 "\n",
+			(uint64_t)reg_struct.range.start,
+			(uint64_t)reg_struct.range.start +
+			(uint64_t)reg_struct.range.len - 1);
+
+	return 0;
+}
+#else
+static int
+vhost_user_postcopy_region_register(struct virtio_net *dev,
+		struct rte_vhost_mem_region *reg)
+{
+	return -1;
+}
+#endif
+
 static int
 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 			int main_fd)
@@ -1209,38 +1252,10 @@  vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
 		}
 
 		/* Now userfault register and we can use the memory */
-		for (i = 0; i < memory->nregions; i++) {
-#ifdef RTE_LIBRTE_VHOST_POSTCOPY
-			reg = &dev->mem->regions[i];
-			struct uffdio_register reg_struct;
-
-			/*
-			 * Let's register all the mmap'ed area to ensure
-			 * alignment on page boundary.
-			 */
-			reg_struct.range.start =
-				(uint64_t)(uintptr_t)reg->mmap_addr;
-			reg_struct.range.len = reg->mmap_size;
-			reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
-
-			if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
-						&reg_struct)) {
-				VHOST_LOG_CONFIG(ERR,
-					"Failed to register ufd for region %d: (ufd = %d) %s\n",
-					i, dev->postcopy_ufd,
-					strerror(errno));
+		for (i = 0; i < memory->nregions; i++)
+			if (vhost_user_postcopy_region_register(dev,
+						&dev->mem->regions[i]) < 0)
 				goto free_mem_table;
-			}
-			VHOST_LOG_CONFIG(INFO,
-				"\t userfaultfd registered for range : "
-				"%" PRIx64 " - %" PRIx64 "\n",
-				(uint64_t)reg_struct.range.start,
-				(uint64_t)reg_struct.range.start +
-				(uint64_t)reg_struct.range.len - 1);
-#else
-			goto free_mem_table;
-#endif
-		}
 	}
 
 	for (i = 0; i < dev->nr_vring; i++) {