[v2,3/3] vhost: optimize vhost virtqueue struct

Message ID 20210316124153.503928-4-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series vhost: make virtqueue cache-friendly |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/travis-robot success travis build: passed
ci/github-robot success github build: passed
ci/iol-abi-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Functional success Functional Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

Maxime Coquelin March 16, 2021, 12:41 p.m. UTC
  This patch moves vhost_virtuqueue struct fields in order
to both optimize packing and move hot fields on the first
cachelines.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/librte_vhost/vhost.h | 52 +++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 25 deletions(-)
  

Comments

David Marchand March 16, 2021, 1:38 p.m. UTC | #1
On Tue, Mar 16, 2021 at 1:42 PM Maxime Coquelin
<maxime.coquelin@redhat.com> wrote:
>
> This patch moves vhost_virtuqueue struct fields in order

virtqueue
You might want to add something in your local dictionary :-p.


> to both optimize packing and move hot fields on the first
> cachelines.
>
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  lib/librte_vhost/vhost.h | 52 +++++++++++++++++++++-------------------
>  1 file changed, 27 insertions(+), 25 deletions(-)
>
> diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
> index 3a71dfeed9..ae45b05dd1 100644
> --- a/lib/librte_vhost/vhost.h
> +++ b/lib/librte_vhost/vhost.h
> @@ -133,7 +133,7 @@ struct vhost_virtqueue {
>                 struct vring_used       *used;
>                 struct vring_packed_desc_event *device_event;
>         };
> -       uint32_t                size;
> +       uint16_t                size;

There is one site in the code that could be problematic:

vhost_user_set_vring_num()
...
vq->size = msg->payload.state.num;
...
if (vq->size > 32768) {

payload.state.num is an unsigned int, so we'd better check its value
as an unsigned int, before storing to vq->size.


>
>         uint16_t                last_avail_idx;
>         uint16_t                last_used_idx;
> @@ -143,29 +143,12 @@ struct vhost_virtqueue {
>  #define VIRTIO_INVALID_EVENTFD         (-1)
>  #define VIRTIO_UNINITIALIZED_EVENTFD   (-2)
>
> -       int                     enabled;
> -       int                     access_ok;
> -       int                     ready;
> -       int                     notif_enable;
> -#define VIRTIO_UNINITIALIZED_NOTIF     (-1)
> +       bool                    enabled;
> +       bool                    access_ok;
> +       bool                    ready;

Changing those types is fine, but it is a bit odd to still see
boolean_var = 0 or boolean_var = 1 in the rest of the code.


>
>         rte_spinlock_t          access_lock;
>
> -       /* Used to notify the guest (trigger interrupt) */
> -       int                     callfd;
> -       /* Currently unused as polling mode is enabled */
> -       int                     kickfd;
> -
> -       /* Physical address of used ring, for logging */
> -       uint64_t                log_guest_addr;
> -
> -       /* inflight share memory info */
> -       union {
> -               struct rte_vhost_inflight_info_split *inflight_split;
> -               struct rte_vhost_inflight_info_packed *inflight_packed;
> -       };
> -       struct rte_vhost_resubmit_info *resubmit_inflight;
> -       uint64_t                global_counter;
>
>         union {
>                 struct vring_used_elem  *shadow_used_split;
> @@ -176,22 +159,36 @@ struct vhost_virtqueue {
>         uint16_t                shadow_aligned_idx;
>         /* Record packed ring first dequeue desc index */
>         uint16_t                shadow_last_used_idx;
> -       struct vhost_vring_addr ring_addrs;
>
> -       struct batch_copy_elem  *batch_copy_elems;
>         uint16_t                batch_copy_nb_elems;
> +       struct batch_copy_elem  *batch_copy_elems;
>         bool                    used_wrap_counter;
>         bool                    avail_wrap_counter;
>
> -       struct log_cache_entry *log_cache;
> +       /* Physical address of used ring, for logging */
>         uint16_t log_cache_nb_elem;

Indent is broken / not consistent, probably because you cut/pasted lines.


> +       uint64_t                log_guest_addr;
> +       struct log_cache_entry *log_cache;
>
>         rte_rwlock_t    iotlb_lock;
>         rte_rwlock_t    iotlb_pending_lock;
>         struct rte_mempool *iotlb_pool;
>         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
> -       int                             iotlb_cache_nr;
>         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
> +       int                             iotlb_cache_nr;
> +
> +       /* Used to notify the guest (trigger interrupt) */
> +       int                     callfd;
> +       /* Currently unused as polling mode is enabled */
> +       int                     kickfd;
> +
> +       /* inflight share memory info */
> +       union {
> +               struct rte_vhost_inflight_info_split *inflight_split;
> +               struct rte_vhost_inflight_info_packed *inflight_packed;
> +       };
> +       struct rte_vhost_resubmit_info *resubmit_inflight;
> +       uint64_t                global_counter;
>
>         /* operation callbacks for async dma */
>         struct rte_vhost_async_channel_ops      async_ops;
> @@ -212,6 +209,11 @@ struct vhost_virtqueue {
>         bool            async_inorder;
>         bool            async_registered;
>         uint16_t        async_threshold;
> +
> +       int                     notif_enable;
> +#define VIRTIO_UNINITIALIZED_NOTIF     (-1)
> +
> +       struct vhost_vring_addr ring_addrs;
>  } __rte_cache_aligned;
>
>  /* Virtio device status as per Virtio specification */
> --
> 2.29.2
>
  
Maxime Coquelin March 17, 2021, 10:26 a.m. UTC | #2
On 3/16/21 2:38 PM, David Marchand wrote:
> On Tue, Mar 16, 2021 at 1:42 PM Maxime Coquelin
> <maxime.coquelin@redhat.com> wrote:
>>
>> This patch moves vhost_virtuqueue struct fields in order
> 
> virtqueue
> You might want to add something in your local dictionary :-p.
> 
> 
>> to both optimize packing and move hot fields on the first
>> cachelines.
>>
>> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
>> ---
>>  lib/librte_vhost/vhost.h | 52 +++++++++++++++++++++-------------------
>>  1 file changed, 27 insertions(+), 25 deletions(-)
>>
>> diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
>> index 3a71dfeed9..ae45b05dd1 100644
>> --- a/lib/librte_vhost/vhost.h
>> +++ b/lib/librte_vhost/vhost.h
>> @@ -133,7 +133,7 @@ struct vhost_virtqueue {
>>                 struct vring_used       *used;
>>                 struct vring_packed_desc_event *device_event;
>>         };
>> -       uint32_t                size;
>> +       uint16_t                size;
> 
> There is one site in the code that could be problematic:
> 
> vhost_user_set_vring_num()
> ...
> vq->size = msg->payload.state.num;
> ...
> if (vq->size > 32768) {
> 
> payload.state.num is an unsigned int, so we'd better check its value
> as an unsigned int, before storing to vq->size.

Indeed, I will rework the function, so that it checks that
msg->payload.state.num is smaller than 32K to comply with the spec,
and so remove the later check on vq->size.

> 
>>
>>         uint16_t                last_avail_idx;
>>         uint16_t                last_used_idx;
>> @@ -143,29 +143,12 @@ struct vhost_virtqueue {
>>  #define VIRTIO_INVALID_EVENTFD         (-1)
>>  #define VIRTIO_UNINITIALIZED_EVENTFD   (-2)
>>
>> -       int                     enabled;
>> -       int                     access_ok;
>> -       int                     ready;
>> -       int                     notif_enable;
>> -#define VIRTIO_UNINITIALIZED_NOTIF     (-1)
>> +       bool                    enabled;
>> +       bool                    access_ok;
>> +       bool                    ready;
> 
> Changing those types is fine, but it is a bit odd to still see
> boolean_var = 0 or boolean_var = 1 in the rest of the code.

Yes, I fix all the places with assigning boolean values.

> 
>>
>>         rte_spinlock_t          access_lock;
>>
>> -       /* Used to notify the guest (trigger interrupt) */
>> -       int                     callfd;
>> -       /* Currently unused as polling mode is enabled */
>> -       int                     kickfd;
>> -
>> -       /* Physical address of used ring, for logging */
>> -       uint64_t                log_guest_addr;
>> -
>> -       /* inflight share memory info */
>> -       union {
>> -               struct rte_vhost_inflight_info_split *inflight_split;
>> -               struct rte_vhost_inflight_info_packed *inflight_packed;
>> -       };
>> -       struct rte_vhost_resubmit_info *resubmit_inflight;
>> -       uint64_t                global_counter;
>>
>>         union {
>>                 struct vring_used_elem  *shadow_used_split;
>> @@ -176,22 +159,36 @@ struct vhost_virtqueue {
>>         uint16_t                shadow_aligned_idx;
>>         /* Record packed ring first dequeue desc index */
>>         uint16_t                shadow_last_used_idx;
>> -       struct vhost_vring_addr ring_addrs;
>>
>> -       struct batch_copy_elem  *batch_copy_elems;
>>         uint16_t                batch_copy_nb_elems;
>> +       struct batch_copy_elem  *batch_copy_elems;
>>         bool                    used_wrap_counter;
>>         bool                    avail_wrap_counter;
>>
>> -       struct log_cache_entry *log_cache;
>> +       /* Physical address of used ring, for logging */
>>         uint16_t log_cache_nb_elem;
> 
> Indent is broken / not consistent, probably because you cut/pasted lines.

Will fix it.

Thanks,
Maxime
  

Patch

diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h
index 3a71dfeed9..ae45b05dd1 100644
--- a/lib/librte_vhost/vhost.h
+++ b/lib/librte_vhost/vhost.h
@@ -133,7 +133,7 @@  struct vhost_virtqueue {
 		struct vring_used	*used;
 		struct vring_packed_desc_event *device_event;
 	};
-	uint32_t		size;
+	uint16_t		size;
 
 	uint16_t		last_avail_idx;
 	uint16_t		last_used_idx;
@@ -143,29 +143,12 @@  struct vhost_virtqueue {
 #define VIRTIO_INVALID_EVENTFD		(-1)
 #define VIRTIO_UNINITIALIZED_EVENTFD	(-2)
 
-	int			enabled;
-	int			access_ok;
-	int			ready;
-	int			notif_enable;
-#define VIRTIO_UNINITIALIZED_NOTIF	(-1)
+	bool			enabled;
+	bool			access_ok;
+	bool			ready;
 
 	rte_spinlock_t		access_lock;
 
-	/* Used to notify the guest (trigger interrupt) */
-	int			callfd;
-	/* Currently unused as polling mode is enabled */
-	int			kickfd;
-
-	/* Physical address of used ring, for logging */
-	uint64_t		log_guest_addr;
-
-	/* inflight share memory info */
-	union {
-		struct rte_vhost_inflight_info_split *inflight_split;
-		struct rte_vhost_inflight_info_packed *inflight_packed;
-	};
-	struct rte_vhost_resubmit_info *resubmit_inflight;
-	uint64_t		global_counter;
 
 	union {
 		struct vring_used_elem  *shadow_used_split;
@@ -176,22 +159,36 @@  struct vhost_virtqueue {
 	uint16_t		shadow_aligned_idx;
 	/* Record packed ring first dequeue desc index */
 	uint16_t		shadow_last_used_idx;
-	struct vhost_vring_addr ring_addrs;
 
-	struct batch_copy_elem	*batch_copy_elems;
 	uint16_t		batch_copy_nb_elems;
+	struct batch_copy_elem	*batch_copy_elems;
 	bool			used_wrap_counter;
 	bool			avail_wrap_counter;
 
-	struct log_cache_entry *log_cache;
+	/* Physical address of used ring, for logging */
 	uint16_t log_cache_nb_elem;
+	uint64_t		log_guest_addr;
+	struct log_cache_entry *log_cache;
 
 	rte_rwlock_t	iotlb_lock;
 	rte_rwlock_t	iotlb_pending_lock;
 	struct rte_mempool *iotlb_pool;
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
-	int				iotlb_cache_nr;
 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
+	int				iotlb_cache_nr;
+
+	/* Used to notify the guest (trigger interrupt) */
+	int			callfd;
+	/* Currently unused as polling mode is enabled */
+	int			kickfd;
+
+	/* inflight share memory info */
+	union {
+		struct rte_vhost_inflight_info_split *inflight_split;
+		struct rte_vhost_inflight_info_packed *inflight_packed;
+	};
+	struct rte_vhost_resubmit_info *resubmit_inflight;
+	uint64_t		global_counter;
 
 	/* operation callbacks for async dma */
 	struct rte_vhost_async_channel_ops	async_ops;
@@ -212,6 +209,11 @@  struct vhost_virtqueue {
 	bool		async_inorder;
 	bool		async_registered;
 	uint16_t	async_threshold;
+
+	int			notif_enable;
+#define VIRTIO_UNINITIALIZED_NOTIF	(-1)
+
+	struct vhost_vring_addr ring_addrs;
 } __rte_cache_aligned;
 
 /* Virtio device status as per Virtio specification */