[v3,2/4] vhost: make the guest_notifications statistic counter atomic

Message ID 168431453456.558450.8798179744539843068.stgit@ebuild.local (mailing list archive)
State Accepted, archived
Delegated to: Maxime Coquelin
Headers
Series vhost: add device op to offload the interrupt kick |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Eelco Chaudron May 17, 2023, 9:08 a.m. UTC
  Making the guest_notifications statistic counter atomic, allows
it to be safely incremented while holding the read access_lock.

Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
---
 lib/vhost/vhost.c |    8 ++++++++
 lib/vhost/vhost.h |    9 ++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)
  

Comments

Maxime Coquelin May 30, 2023, 12:52 p.m. UTC | #1
On 5/17/23 11:08, Eelco Chaudron wrote:
> Making the guest_notifications statistic counter atomic, allows
> it to be safely incremented while holding the read access_lock.
> 
> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
> ---
>   lib/vhost/vhost.c |    8 ++++++++
>   lib/vhost/vhost.h |    9 ++++++---
>   2 files changed, 14 insertions(+), 3 deletions(-)
> 

Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Thanks,
Maxime
  
Chenbo Xia May 31, 2023, 7:03 a.m. UTC | #2
> -----Original Message-----
> From: Eelco Chaudron <echaudro@redhat.com>
> Sent: Wednesday, May 17, 2023 5:09 PM
> To: maxime.coquelin@redhat.com; Xia, Chenbo <chenbo.xia@intel.com>;
> david.marchand@redhat.com
> Cc: dev@dpdk.org
> Subject: [PATCH v3 2/4] vhost: make the guest_notifications statistic
> counter atomic
> 
> Making the guest_notifications statistic counter atomic, allows
> it to be safely incremented while holding the read access_lock.
> 
> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
> ---
>  lib/vhost/vhost.c |    8 ++++++++
>  lib/vhost/vhost.h |    9 ++++++---
>  2 files changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c
> index 74bdbfd810..8ff6434c93 100644
> --- a/lib/vhost/vhost.c
> +++ b/lib/vhost/vhost.c
> @@ -2086,6 +2086,10 @@ rte_vhost_vring_stats_get(int vid, uint16_t
> queue_id,
> 
>  	rte_rwlock_write_lock(&vq->access_lock);
>  	for (i = 0; i < VHOST_NB_VQ_STATS; i++) {
> +		/*
> +		 * No need to the read atomic counters as such, due to the
> +		 * above write access_lock preventing them to be updated.
> +		 */
>  		stats[i].value =
>  			*(uint64_t *)(((char *)vq) +
> vhost_vq_stat_strings[i].offset);
>  		stats[i].id = i;
> @@ -2112,6 +2116,10 @@ int rte_vhost_vring_stats_reset(int vid, uint16_t
> queue_id)
>  	vq = dev->virtqueue[queue_id];
> 
>  	rte_rwlock_write_lock(&vq->access_lock);
> +	/*
> +	 * No need to the reset atomic counters as such, due to the
> +	 * above write access_lock preventing them to be updated.
> +	 */
>  	memset(&vq->stats, 0, sizeof(vq->stats));
>  	rte_rwlock_write_unlock(&vq->access_lock);
> 
> diff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h
> index 5c939ef06f..37609c7c8d 100644
> --- a/lib/vhost/vhost.h
> +++ b/lib/vhost/vhost.h
> @@ -135,11 +135,12 @@ struct virtqueue_stats {
>  	uint64_t broadcast;
>  	/* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */
>  	uint64_t size_bins[8];
> -	uint64_t guest_notifications;
>  	uint64_t iotlb_hits;
>  	uint64_t iotlb_misses;
>  	uint64_t inflight_submitted;
>  	uint64_t inflight_completed;
> +	/* Counters below are atomic, and should be incremented as such. */
> +	uint64_t guest_notifications;
>  };
> 
>  /**
> @@ -907,7 +908,8 @@ vhost_vring_call_split(struct virtio_net *dev, struct
> vhost_virtqueue *vq)
>  				unlikely(!signalled_used_valid)) {
>  			eventfd_write(vq->callfd, (eventfd_t) 1);
>  			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
> -				vq->stats.guest_notifications++;
> +				__atomic_fetch_add(&vq->stats.guest_notifications,
> +					1, __ATOMIC_RELAXED);
>  			if (dev->notify_ops->guest_notified)
>  				dev->notify_ops->guest_notified(dev->vid);
>  		}
> @@ -917,7 +919,8 @@ vhost_vring_call_split(struct virtio_net *dev, struct
> vhost_virtqueue *vq)
>  				&& (vq->callfd >= 0)) {
>  			eventfd_write(vq->callfd, (eventfd_t)1);
>  			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
> -				vq->stats.guest_notifications++;
> +				__atomic_fetch_add(&vq->stats.guest_notifications,
> +					1, __ATOMIC_RELAXED);
>  			if (dev->notify_ops->guest_notified)
>  				dev->notify_ops->guest_notified(dev->vid);
>  		}

Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
  

Patch

diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c
index 74bdbfd810..8ff6434c93 100644
--- a/lib/vhost/vhost.c
+++ b/lib/vhost/vhost.c
@@ -2086,6 +2086,10 @@  rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
 
 	rte_rwlock_write_lock(&vq->access_lock);
 	for (i = 0; i < VHOST_NB_VQ_STATS; i++) {
+		/*
+		 * No need to the read atomic counters as such, due to the
+		 * above write access_lock preventing them to be updated.
+		 */
 		stats[i].value =
 			*(uint64_t *)(((char *)vq) + vhost_vq_stat_strings[i].offset);
 		stats[i].id = i;
@@ -2112,6 +2116,10 @@  int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)
 	vq = dev->virtqueue[queue_id];
 
 	rte_rwlock_write_lock(&vq->access_lock);
+	/*
+	 * No need to the reset atomic counters as such, due to the
+	 * above write access_lock preventing them to be updated.
+	 */
 	memset(&vq->stats, 0, sizeof(vq->stats));
 	rte_rwlock_write_unlock(&vq->access_lock);
 
diff --git a/lib/vhost/vhost.h b/lib/vhost/vhost.h
index 5c939ef06f..37609c7c8d 100644
--- a/lib/vhost/vhost.h
+++ b/lib/vhost/vhost.h
@@ -135,11 +135,12 @@  struct virtqueue_stats {
 	uint64_t broadcast;
 	/* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */
 	uint64_t size_bins[8];
-	uint64_t guest_notifications;
 	uint64_t iotlb_hits;
 	uint64_t iotlb_misses;
 	uint64_t inflight_submitted;
 	uint64_t inflight_completed;
+	/* Counters below are atomic, and should be incremented as such. */
+	uint64_t guest_notifications;
 };
 
 /**
@@ -907,7 +908,8 @@  vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
 				unlikely(!signalled_used_valid)) {
 			eventfd_write(vq->callfd, (eventfd_t) 1);
 			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
-				vq->stats.guest_notifications++;
+				__atomic_fetch_add(&vq->stats.guest_notifications,
+					1, __ATOMIC_RELAXED);
 			if (dev->notify_ops->guest_notified)
 				dev->notify_ops->guest_notified(dev->vid);
 		}
@@ -917,7 +919,8 @@  vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
 				&& (vq->callfd >= 0)) {
 			eventfd_write(vq->callfd, (eventfd_t)1);
 			if (dev->flags & VIRTIO_DEV_STATS_ENABLED)
-				vq->stats.guest_notifications++;
+				__atomic_fetch_add(&vq->stats.guest_notifications,
+					1, __ATOMIC_RELAXED);
 			if (dev->notify_ops->guest_notified)
 				dev->notify_ops->guest_notified(dev->vid);
 		}