Skip to contentSkip to conversation

tcm_vhost endpoint

PATCH Superseded Asias He 4 participants 33 messages Mar 28, 2013 – Apr 12, 2013

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Asias He

author
#0 lore raw

v2 cover tcm_vhost endpoint

Reordered the patch, so patch 1 can go to v3.9.

Asias He (2):
tcm_vhost: Initialize vq->last_used_idx when set endpoint
tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

 drivers/vhost/tcm_vhost.c | 47 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 6 deletions(-)
Signature · 1 line
1.8.1.4
author
#1 lore raw

PATCH v2 1/2 tcm_vhost: Initialize vq->last_used_idx when set endpoint

This patch fixes guest hang when booting seabios and guest.

  [    0.576238] scsi0 : Virtio SCSI HBA
  [    0.616754] virtio_scsi virtio1: request:id 0 is not a head!

vq->last_used_idx is initialized only when /dev/vhost-scsi is opened or closed.

vhost_scsi_open -> vhost_dev_init() -> vhost_vq_reset() vhost_scsi_release() -> vhost_dev_cleanup -> vhost_vq_reset()

So, when guest talks to tcm_vhost after seabios does, vq->last_used_idx still contains the old valule for seabios. This confuses guest.

Fix this by calling vhost_init_used() to init vq->last_used_idx when we set endpoint.

---
 drivers/vhost/tcm_vhost.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/tcm_vhost.c +8 -1
index 43fb11e..5e3d4487 100644
@@ -781,8 +781,9 @@ static int vhost_scsi_set_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
 	bool match = false;
-	int index, ret;
+	int index, ret, i;
 
 	mutex_lock(&vs->dev.mutex);
 	/* Verify that ring has been setup correctly. */
@@ -826,6 +827,12 @@ static int vhost_scsi_set_endpoint(
 	if (match) {
 		memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
 		       sizeof(vs->vs_vhost_wwpn));
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			mutex_lock(&vq->mutex);
+			vhost_init_used(vq);
+			mutex_unlock(&vq->mutex);
+		}
 		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
Signature · 1 line
1.8.1.4
author
#2 lore raw

PATCH v2 1/2 tcm_vhost: Initialize vq->last_used_idx when set endpoint

In reply to Asias He

This patch fixes guest hang when booting seabios and guest.

  [    0.576238] scsi0 : Virtio SCSI HBA
  [    0.616754] virtio_scsi virtio1: request:id 0 is not a head!

vq->last_used_idx is initialized only when /dev/vhost-scsi is opened or closed.

vhost_scsi_open -> vhost_dev_init() -> vhost_vq_reset() vhost_scsi_release() -> vhost_dev_cleanup -> vhost_vq_reset()

So, when guest talks to tcm_vhost after seabios does, vq->last_used_idx still contains the old valule for seabios. This confuses guest.

Fix this by calling vhost_init_used() to init vq->last_used_idx when we set endpoint.

---
 drivers/vhost/tcm_vhost.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/tcm_vhost.c +8 -1
index 43fb11e..5e3d4487 100644
@@ -781,8 +781,9 @@ static int vhost_scsi_set_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
 	bool match = false;
-	int index, ret;
+	int index, ret, i;
 
 	mutex_lock(&vs->dev.mutex);
 	/* Verify that ring has been setup correctly. */
@@ -826,6 +827,12 @@ static int vhost_scsi_set_endpoint(
 	if (match) {
 		memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
 		       sizeof(vs->vs_vhost_wwpn));
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			mutex_lock(&vq->mutex);
+			vhost_init_used(vq);
+			mutex_unlock(&vq->mutex);
+		}
 		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
Signature · 1 line
1.8.1.4
#3 lore raw

Re: [PATCH V2 1/2] tcm_vhost: Initialize vq->last_used_idx when set endpoint

Hi Asias,

Quoted 38 lines · Asias He

This patch fixes guest hang when booting seabios and guest.

  [    0.576238] scsi0 : Virtio SCSI HBA
  [    0.616754] virtio_scsi virtio1: request:id 0 is not a head!

vq->last_used_idx is initialized only when /dev/vhost-scsi is opened or closed.

   vhost_scsi_open -> vhost_dev_init() -> vhost_vq_reset()
   vhost_scsi_release() -> vhost_dev_cleanup -> vhost_vq_reset()

So, when guest talks to tcm_vhost after seabios does, vq->last_used_idx still contains the old valule for seabios. This confuses guest.

Fix this by calling vhost_init_used() to init vq->last_used_idx when we set endpoint.

---
 drivers/vhost/tcm_vhost.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/tcm_vhost.c +2 -1
index 43fb11e..5e3d4487 100644
@@ -781,8 +781,9 @@ static int vhost_scsi_set_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
 	bool match = false;
-	int index, ret;
+	int index, ret, i;
 
 	mutex_lock(&vs->dev.mutex);
 	/* Verify that ring has been setup correctly. */

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 835

Quoted 9 lines
drivers/vhost/tcm_vhost.c +6 -0
@@ -826,6 +827,12 @@ static int vhost_scsi_set_endpoint(
 	if (match) {
 		memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
 		       sizeof(vs->vs_vhost_wwpn));
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			mutex_lock(&vq->mutex);
+			vhost_init_used(vq);
+			mutex_unlock(&vq->mutex);
+		}

Already tried a similar patch earlier today, but as vhost_init_used() depends upon a vq->private_data being set it does not actually re-initialize ->last_used_idx..

author
#4 lore raw

Re: [PATCH V2 1/2] tcm_vhost: Initialize vq->last_used_idx when set endpoint

Quoted 38 lines · Nicholas A. Bellinger

Hi Asias,

Quoted 35 lines · Asias He

This patch fixes guest hang when booting seabios and guest.

  [    0.576238] scsi0 : Virtio SCSI HBA
  [    0.616754] virtio_scsi virtio1: request:id 0 is not a head!

vq->last_used_idx is initialized only when /dev/vhost-scsi is opened or closed.

   vhost_scsi_open -> vhost_dev_init() -> vhost_vq_reset()
   vhost_scsi_release() -> vhost_dev_cleanup -> vhost_vq_reset()

So, when guest talks to tcm_vhost after seabios does, vq->last_used_idx still contains the old valule for seabios. This confuses guest.

Fix this by calling vhost_init_used() to init vq->last_used_idx when we set endpoint.

---
 drivers/vhost/tcm_vhost.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/tcm_vhost.c +2 -1
index 43fb11e..5e3d4487 100644
@@ -781,8 +781,9 @@ static int vhost_scsi_set_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
 	bool match = false;
-	int index, ret;
+	int index, ret, i;
 
Quoted 17 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 835

Quoted 12 lines
drivers/vhost/tcm_vhost.c +6 -0
 	mutex_lock(&vs->dev.mutex);
 	/* Verify that ring has been setup correctly. */
@@ -826,6 +827,12 @@ static int vhost_scsi_set_endpoint(
 	if (match) {
 		memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
 		       sizeof(vs->vs_vhost_wwpn));
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			mutex_lock(&vq->mutex);
+			vhost_init_used(vq);
+			mutex_unlock(&vq->mutex);
+		}

Already tried a similar patch earlier today, but as vhost_init_used() depends upon a vq->private_data being set it does not actually re-initialize ->last_used_idx..

Sigh... Ah, We have this in vhost_init_used

        if (!vq->private_data)
                return 0;

Michael, how bad if we let the original patch 1/2 and 2/2 go to 3.9.

Signature · 1 line
Asias
author
#5 lore raw

Re: [PATCH V2 1/2] tcm_vhost: Initialize vq->last_used_idx when set endpoint

In reply to Nicholas A. Bellinger
Quoted 38 lines · Nicholas A. Bellinger

Hi Asias,

Quoted 35 lines · Asias He

This patch fixes guest hang when booting seabios and guest.

  [    0.576238] scsi0 : Virtio SCSI HBA
  [    0.616754] virtio_scsi virtio1: request:id 0 is not a head!

vq->last_used_idx is initialized only when /dev/vhost-scsi is opened or closed.

   vhost_scsi_open -> vhost_dev_init() -> vhost_vq_reset()
   vhost_scsi_release() -> vhost_dev_cleanup -> vhost_vq_reset()

So, when guest talks to tcm_vhost after seabios does, vq->last_used_idx still contains the old valule for seabios. This confuses guest.

Fix this by calling vhost_init_used() to init vq->last_used_idx when we set endpoint.

---
 drivers/vhost/tcm_vhost.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/tcm_vhost.c +2 -1
index 43fb11e..5e3d4487 100644
@@ -781,8 +781,9 @@ static int vhost_scsi_set_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
 	bool match = false;
-	int index, ret;
+	int index, ret, i;
 
Quoted 17 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 835

Quoted 12 lines
drivers/vhost/tcm_vhost.c +6 -0
 	mutex_lock(&vs->dev.mutex);
 	/* Verify that ring has been setup correctly. */
@@ -826,6 +827,12 @@ static int vhost_scsi_set_endpoint(
 	if (match) {
 		memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
 		       sizeof(vs->vs_vhost_wwpn));
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			mutex_lock(&vq->mutex);
+			vhost_init_used(vq);
+			mutex_unlock(&vq->mutex);
+		}

Already tried a similar patch earlier today, but as vhost_init_used() depends upon a vq->private_data being set it does not actually re-initialize ->last_used_idx..

Sigh... Ah, We have this in vhost_init_used

        if (!vq->private_data)
                return 0;

Michael, how bad if we let the original patch 1/2 and 2/2 go to 3.9.

Signature · 1 line
Asias
#6 lore raw

Re: [PATCH V2 1/2] tcm_vhost: Initialize vq->last_used_idx when set endpoint

In reply to Asias He

Hi Asias,

Quoted 38 lines · Asias He

This patch fixes guest hang when booting seabios and guest.

  [    0.576238] scsi0 : Virtio SCSI HBA
  [    0.616754] virtio_scsi virtio1: request:id 0 is not a head!

vq->last_used_idx is initialized only when /dev/vhost-scsi is opened or closed.

   vhost_scsi_open -> vhost_dev_init() -> vhost_vq_reset()
   vhost_scsi_release() -> vhost_dev_cleanup -> vhost_vq_reset()

So, when guest talks to tcm_vhost after seabios does, vq->last_used_idx still contains the old valule for seabios. This confuses guest.

Fix this by calling vhost_init_used() to init vq->last_used_idx when we set endpoint.

---
 drivers/vhost/tcm_vhost.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/tcm_vhost.c +2 -1
index 43fb11e..5e3d4487 100644
@@ -781,8 +781,9 @@ static int vhost_scsi_set_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
 	bool match = false;
-	int index, ret;
+	int index, ret, i;
 
 	mutex_lock(&vs->dev.mutex);
 	/* Verify that ring has been setup correctly. */

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 835

Quoted 9 lines
drivers/vhost/tcm_vhost.c +6 -0
@@ -826,6 +827,12 @@ static int vhost_scsi_set_endpoint(
 	if (match) {
 		memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
 		       sizeof(vs->vs_vhost_wwpn));
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			mutex_lock(&vq->mutex);
+			vhost_init_used(vq);
+			mutex_unlock(&vq->mutex);
+		}

Already tried a similar patch earlier today, but as vhost_init_used() depends upon a vq->private_data being set it does not actually re-initialize ->last_used_idx..

author
#7 lore raw

PATCH v2 2/2 tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +33 -5
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
+			mutex_unlock(&vq->mutex);
+		}
+	}
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
#8 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 56 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 8 lines
drivers/vhost/tcm_vhost.c +1 -2
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 16 lines
drivers/vhost/tcm_vhost.c +3 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 10 lines
drivers/vhost/tcm_vhost.c +9 -0
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
author
#9 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

handle_tx:

           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
#10 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

Quoted 66 lines
Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
author
#11 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

I am wondering:

1. Why there is a lot of spurios wakeups

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

Quoted 67 lines
Quoted 67 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
#12 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 114 lines · Asias He
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

Quoted 8 lines

I am wondering:

1. Why there is a lot of spurios wakeups

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

At some point when this was written, yes. We can revisit this, but let's focus on fixing vhost-scsi.

Quoted 73 lines
Quoted 70 lines
Quoted 70 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
author
#13 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 119 lines · Michael S. Tsirkin
Quoted 118 lines · Asias He
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

I am wondering:

1. Why there is a lot of spurios wakeups

Quoted 7 lines
Quoted 4 lines

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

At some point when this was written, yes. We can revisit this, but let's focus on fixing vhost-scsi.

If no perf difference is measurable, we can simplify the -net. It would be one small step towards removing the vhost rcu thing.

Quoted 73 lines
Quoted 73 lines
Quoted 70 lines
Quoted 70 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
#14 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 125 lines · Asias He
Quoted 124 lines · Michael S. Tsirkin
Quoted 122 lines · Asias He
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

I am wondering:

1. Why there is a lot of spurios wakeups

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

Quoted 5 lines
Quoted 2 lines

At some point when this was written, yes. We can revisit this, but let's focus on fixing vhost-scsi.

If no perf difference is measurable, we can simplify the -net. It would be one small step towards removing the vhost rcu thing.

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

Quoted 76 lines
Quoted 73 lines
Quoted 73 lines
Quoted 70 lines
Quoted 70 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
author
#15 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 129 lines · Michael S. Tsirkin
Quoted 128 lines · Asias He
Quoted 126 lines · Michael S. Tsirkin
Quoted 114 lines · Asias He
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

Quoted 8 lines

I am wondering:

1. Why there is a lot of spurios wakeups

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

At some point when this was written, yes. We can revisit this, but let's focus on fixing vhost-scsi.

Quoted 7 lines
Quoted 2 lines

If no perf difference is measurable, we can simplify the -net. It would be one small step towards removing the vhost rcu thing.

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

There are set endpoint, hotplug and flush series in flight already. So I will not start framing assumptions fix until we merge the in flight ones. However, IMO, we can probably fix it in 3.10.

Quoted 76 lines
Quoted 76 lines
Quoted 73 lines
Quoted 73 lines
Quoted 70 lines
Quoted 70 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
#16 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Michael S. Tsirkin
Quoted 4 lines · Michael S. Tsirkin

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

I am waiting for your removal of the dma-compelete ordering stuff in vhost-net.

Cheers,
Rusty.

#17 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 11 lines · Rusty Russell
Quoted 4 lines · Michael S. Tsirkin

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

I am waiting for your removal of the dma-compelete ordering stuff in vhost-net.

Cheers,
Rusty.

Sure.

#18 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Rusty Russell
Quoted 11 lines · Rusty Russell
Quoted 4 lines · Michael S. Tsirkin

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

I am waiting for your removal of the dma-compelete ordering stuff in vhost-net.

Cheers,
Rusty.

Now, it looks like it's actually a smart datastructure. It allows signalling consumptions from multiple without any locks, with multiple consumers, and just a single kref counter.
Nothing simpler than a producer/consumer does this. Yes it can in theory delay some tx completions a bit but normally no one is waiting for them.

We can refactor it to save some memory, and cleanup the code, playing with this now.

#19 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Michael S. Tsirkin
Quoted 4 lines · Michael S. Tsirkin

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

I am waiting for your removal of the dma-compelete ordering stuff in vhost-net.

Cheers,
Rusty.

#20 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Asias He
Quoted 125 lines · Asias He
Quoted 124 lines · Michael S. Tsirkin
Quoted 122 lines · Asias He
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

I am wondering:

1. Why there is a lot of spurios wakeups

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

Quoted 5 lines
Quoted 2 lines

At some point when this was written, yes. We can revisit this, but let's focus on fixing vhost-scsi.

If no perf difference is measurable, we can simplify the -net. It would be one small step towards removing the vhost rcu thing.

Rusty's currently doing some reorgs of -net let's delay cleanups there to avoid stepping on each other's toys. Let's focus on scsi here.
E.g. any chance framing assumptions can be fixed in 3.10?

Quoted 76 lines
Quoted 73 lines
Quoted 73 lines
Quoted 70 lines
Quoted 70 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
author
#21 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Michael S. Tsirkin
Quoted 119 lines · Michael S. Tsirkin
Quoted 118 lines · Asias He
Quoted 104 lines · Michael S. Tsirkin
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 8 lines
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

I am wondering:

1. Why there is a lot of spurios wakeups

Quoted 7 lines
Quoted 4 lines

2. What performance impact it would give if we take the lock to check vq->private_data. Sinc, at any time, either handle_tx or handle_rx can be running. So we can almost always take the vq->mutex mutex. Did you managed to measure real perf difference?

At some point when this was written, yes. We can revisit this, but let's focus on fixing vhost-scsi.

If no perf difference is measurable, we can simplify the -net. It would be one small step towards removing the vhost rcu thing.

Quoted 73 lines
Quoted 73 lines
Quoted 70 lines
Quoted 70 lines
Quoted 45 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
#22 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Asias He
Quoted 103 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);
Quoted 4 lines

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

The point is to make spurios wakeups as lightweight as possible. The seemed to happen a lot with -net.
Should not happen with -scsi at all.

Quoted 66 lines
Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
#23 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Asias He
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
author
#24 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 162 lines · Michael S. Tsirkin
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 52 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 16 lines
drivers/vhost/tcm_vhost.c +3 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 10 lines
drivers/vhost/tcm_vhost.c +9 -0
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 6 lines
Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done. At that time, the vs->vs_tpg[] is already ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
#25 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 167 lines · Asias He
Quoted 166 lines · Michael S. Tsirkin
Quoted 164 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

Quoted 7 lines
Quoted 2 lines

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done.

But nothing prevents multiple SET_ENDPOINT calls while the previous one is in progress.

Quoted 4 lines

At that time, the vs->vs_tpg[] is already
ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

We check it without a common lock so it can become NULL after we test it.

Quoted 7 lines
Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

No, I want to put it at the array of targets.

Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
author
#26 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 171 lines · Michael S. Tsirkin
Quoted 170 lines · Asias He
Quoted 168 lines · Michael S. Tsirkin
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Quoted 7 lines
Quoted 4 lines

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done.

But nothing prevents multiple SET_ENDPOINT calls while the previous one is in progress.

vhost_scsi_set_endpoint() and vhost_scsi_clear_endpoint() are protected by vs->dev.mutex, no?

And in vhost_scsi_set_endpoint():

	if (tv_tpg->tv_tpg_vhost_count != 0) {
		mutex_unlock(&tv_tpg->tv_tpg_mutex);
		continue;
	}

This prevents calling of vhost_scsi_set_endpoint before we call vhost_scsi_clear_endpoint to decrease tv_tpg->tv_tpg_vhost_count.

Quoted 7 lines
Quoted 4 lines

At that time, the vs->vs_tpg[] is already
ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

We check it without a common lock so it can become NULL after we test it.

vhost_scsi_handle_vq:

     tv_tpg = vs->vs_tpg[target];	
     if (!tv_tpg)
         we fail the cmd
     ...

     INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
     queue_work(tcm_vhost_workqueue, &tv_cmd->work);

So, after we test tv_tpg, event if vs->vs_tpg[target] become NULL, it
does not matter if the tpg is not deleted by calling tcm_vhost_drop_tpg(). tcm_vhost_drop_tpg() will not succeed if we do not call vhost_scsi_clear_endpoint() Becasue, tcm_vhost_drop_tpg -> tcm_vhost_drop_nexus -> check if (tpg->tv_tpg_vhost_count != 0)

Further, the tcm core should fail the cmd if the tpg is gonna when we submit the cmd in tcm_vhost_submission_work. (nab, is this true?)

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

No, I want to put it at the array of targets.

tcm_vhost_tpg is allocated in tcm_vhost_make_tpg. There is no array of the targets. The targets exist when user create them in host side using targetcli tools or /sys/kernel/config interface.

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
#27 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 185 lines · Asias He
Quoted 171 lines · Michael S. Tsirkin
Quoted 170 lines · Asias He
Quoted 168 lines · Michael S. Tsirkin
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Quoted 7 lines
Quoted 4 lines

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done.

But nothing prevents multiple SET_ENDPOINT calls while the previous one is in progress.

vhost_scsi_set_endpoint() and vhost_scsi_clear_endpoint() are protected by vs->dev.mutex, no?

And in vhost_scsi_set_endpoint():

Quoted 7 lines
	if (tv_tpg->tv_tpg_vhost_count != 0) {
		mutex_unlock(&tv_tpg->tv_tpg_mutex);
		continue;
	}

This prevents calling of vhost_scsi_set_endpoint before we call vhost_scsi_clear_endpoint to decrease tv_tpg->tv_tpg_vhost_count.

All this seems to do is prevent reusing the same target in multiple vhosts.

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines

At that time, the vs->vs_tpg[] is already
ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

We check it without a common lock so it can become NULL after we test it.

vhost_scsi_handle_vq:

     tv_tpg = vs->vs_tpg[target];	
     if (!tv_tpg)
         we fail the cmd
     ...

     INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
     queue_work(tcm_vhost_workqueue, &tv_cmd->work);
Quoted 4 lines

So, after we test tv_tpg, event if vs->vs_tpg[target] become NULL, it
does not matter if the tpg is not deleted by calling tcm_vhost_drop_tpg(). tcm_vhost_drop_tpg() will not succeed if we do not call vhost_scsi_clear_endpoint() Becasue, tcm_vhost_drop_tpg -> tcm_vhost_drop_nexus -> check if (tpg->tv_tpg_vhost_count != 0)

My point is this:

		tv_tpg = vs->vs_tpg[target];
		if (!tv_tpg) {
			....
			return
		}

                tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,

above line can legally reread vs->vs_tpg[target] from array. You need ACCESS_ONCE if you don't want that.

Quoted 8 lines

Further, the tcm core should fail the cmd if the tpg is gonna when we submit the cmd in tcm_vhost_submission_work. (nab, is this true?)

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

No, I want to put it at the array of targets.

tcm_vhost_tpg is allocated in tcm_vhost_make_tpg. There is no array of the targets. The targets exist when user create them in host side using targetcli tools or /sys/kernel/config interface.

I really simply mean this field:

	        struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];

allocate it dynamically when endpoint is set, and
set private data for each vq.

Quoted 16 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
author
#28 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 230 lines · Michael S. Tsirkin
Quoted 185 lines · Asias He
Quoted 171 lines · Michael S. Tsirkin
Quoted 170 lines · Asias He
Quoted 168 lines · Michael S. Tsirkin
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Quoted 7 lines
Quoted 4 lines

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done.

But nothing prevents multiple SET_ENDPOINT calls while the previous one is in progress.

vhost_scsi_set_endpoint() and vhost_scsi_clear_endpoint() are protected by vs->dev.mutex, no?

And in vhost_scsi_set_endpoint():

Quoted 7 lines
	if (tv_tpg->tv_tpg_vhost_count != 0) {
		mutex_unlock(&tv_tpg->tv_tpg_mutex);
		continue;
	}

This prevents calling of vhost_scsi_set_endpoint before we call vhost_scsi_clear_endpoint to decrease tv_tpg->tv_tpg_vhost_count.

All this seems to do is prevent reusing the same target in multiple vhosts.

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines

At that time, the vs->vs_tpg[] is already
ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

We check it without a common lock so it can become NULL after we test it.

vhost_scsi_handle_vq:

     tv_tpg = vs->vs_tpg[target];	
     if (!tv_tpg)
         we fail the cmd
     ...

     INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
     queue_work(tcm_vhost_workqueue, &tv_cmd->work);
Quoted 4 lines

So, after we test tv_tpg, event if vs->vs_tpg[target] become NULL, it
does not matter if the tpg is not deleted by calling tcm_vhost_drop_tpg(). tcm_vhost_drop_tpg() will not succeed if we do not call vhost_scsi_clear_endpoint() Becasue, tcm_vhost_drop_tpg -> tcm_vhost_drop_nexus -> check if (tpg->tv_tpg_vhost_count != 0)

My point is this:

		tv_tpg = vs->vs_tpg[target];
		if (!tv_tpg) {
			....
			return
		}

                tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
Quoted 2 lines

above line can legally reread vs->vs_tpg[target] from array. You need ACCESS_ONCE if you don't want that.

Well, this is another problem we have. Will include it in next version.

Quoted 18 lines
Quoted 9 lines

Further, the tcm core should fail the cmd if the tpg is gonna when we submit the cmd in tcm_vhost_submission_work. (nab, is this true?)

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

No, I want to put it at the array of targets.

tcm_vhost_tpg is allocated in tcm_vhost_make_tpg. There is no array of the targets. The targets exist when user create them in host side using targetcli tools or /sys/kernel/config interface.

Quoted 5 lines

I really simply mean this field:

	        struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];

allocate it dynamically when endpoint is set, and
set private data for each vq.

What's the benefit of allocating it dynamically? Why bother it if the current static and simpler one works ok.

So do you have further concerns other than the ACCESS_ONCE one.

Quoted 16 lines
Quoted 16 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
#29 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 254 lines · Asias He
Quoted 230 lines · Michael S. Tsirkin
Quoted 185 lines · Asias He
Quoted 171 lines · Michael S. Tsirkin
Quoted 170 lines · Asias He
Quoted 168 lines · Michael S. Tsirkin
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Quoted 7 lines
Quoted 4 lines

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done.

But nothing prevents multiple SET_ENDPOINT calls while the previous one is in progress.

vhost_scsi_set_endpoint() and vhost_scsi_clear_endpoint() are protected by vs->dev.mutex, no?

And in vhost_scsi_set_endpoint():

Quoted 7 lines
	if (tv_tpg->tv_tpg_vhost_count != 0) {
		mutex_unlock(&tv_tpg->tv_tpg_mutex);
		continue;
	}

This prevents calling of vhost_scsi_set_endpoint before we call vhost_scsi_clear_endpoint to decrease tv_tpg->tv_tpg_vhost_count.

All this seems to do is prevent reusing the same target in multiple vhosts.

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines

At that time, the vs->vs_tpg[] is already
ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

We check it without a common lock so it can become NULL after we test it.

vhost_scsi_handle_vq:

     tv_tpg = vs->vs_tpg[target];	
     if (!tv_tpg)
         we fail the cmd
     ...

     INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
     queue_work(tcm_vhost_workqueue, &tv_cmd->work);
Quoted 4 lines

So, after we test tv_tpg, event if vs->vs_tpg[target] become NULL, it
does not matter if the tpg is not deleted by calling tcm_vhost_drop_tpg(). tcm_vhost_drop_tpg() will not succeed if we do not call vhost_scsi_clear_endpoint() Becasue, tcm_vhost_drop_tpg -> tcm_vhost_drop_nexus -> check if (tpg->tv_tpg_vhost_count != 0)

My point is this:

		tv_tpg = vs->vs_tpg[target];
		if (!tv_tpg) {
			....
			return
		}

                tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
Quoted 2 lines

above line can legally reread vs->vs_tpg[target] from array. You need ACCESS_ONCE if you don't want that.

Well, this is another problem we have. Will include it in next version.

Quoted 18 lines
Quoted 17 lines

Further, the tcm core should fail the cmd if the tpg is gonna when we submit the cmd in tcm_vhost_submission_work. (nab, is this true?)

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

No, I want to put it at the array of targets.

tcm_vhost_tpg is allocated in tcm_vhost_make_tpg. There is no array of the targets. The targets exist when user create them in host side using targetcli tools or /sys/kernel/config interface.

Quoted 8 lines
Quoted 5 lines

I really simply mean this field:

	        struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];

allocate it dynamically when endpoint is set, and
set private data for each vq.

What's the benefit of allocating it dynamically? Why bother it if the current static and simpler one works ok.

Because this makes it the lifetime rules clear:
instead of changing values in the array, you
replace the pointer to the array.

Quoted 2 lines

So do you have further concerns other than the ACCESS_ONCE one.

It's just ugly to use a pointer as a flag. vhost uses private_data to point to the constant backend structure, and NULL if there's no backend, so vhost-scsi should just do this too then it won't have problems.

Quoted 19 lines
Quoted 16 lines
Quoted 16 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
author
#30 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

Quoted 269 lines · Michael S. Tsirkin
Quoted 254 lines · Asias He
Quoted 230 lines · Michael S. Tsirkin
Quoted 185 lines · Asias He
Quoted 171 lines · Michael S. Tsirkin
Quoted 170 lines · Asias He
Quoted 168 lines · Michael S. Tsirkin
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Quoted 7 lines
Quoted 4 lines

I do not see a problem here.

Access of vs->vs_tpg[target] in vhost_scsi_handle_vq() happens only when the SET_ENDPOINT is done.

But nothing prevents multiple SET_ENDPOINT calls while the previous one is in progress.

vhost_scsi_set_endpoint() and vhost_scsi_clear_endpoint() are protected by vs->dev.mutex, no?

And in vhost_scsi_set_endpoint():

Quoted 7 lines
	if (tv_tpg->tv_tpg_vhost_count != 0) {
		mutex_unlock(&tv_tpg->tv_tpg_mutex);
		continue;
	}

This prevents calling of vhost_scsi_set_endpoint before we call vhost_scsi_clear_endpoint to decrease tv_tpg->tv_tpg_vhost_count.

All this seems to do is prevent reusing the same target in multiple vhosts.

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines

At that time, the vs->vs_tpg[] is already
ready. Even if the vs->vs_tpg[target] is changed to NULL in CLEAR_ENDPOINT, it is safe since we fail the request if vs->vs_tpg[target] is NULL.

We check it without a common lock so it can become NULL after we test it.

vhost_scsi_handle_vq:

     tv_tpg = vs->vs_tpg[target];	
     if (!tv_tpg)
         we fail the cmd
     ...

     INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
     queue_work(tcm_vhost_workqueue, &tv_cmd->work);
Quoted 4 lines

So, after we test tv_tpg, event if vs->vs_tpg[target] become NULL, it
does not matter if the tpg is not deleted by calling tcm_vhost_drop_tpg(). tcm_vhost_drop_tpg() will not succeed if we do not call vhost_scsi_clear_endpoint() Becasue, tcm_vhost_drop_tpg -> tcm_vhost_drop_nexus -> check if (tpg->tv_tpg_vhost_count != 0)

My point is this:

		tv_tpg = vs->vs_tpg[target];
		if (!tv_tpg) {
			....
			return
		}

                tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
Quoted 2 lines

above line can legally reread vs->vs_tpg[target] from array. You need ACCESS_ONCE if you don't want that.

Well, this is another problem we have. Will include it in next version.

Quoted 18 lines
Quoted 17 lines

Further, the tcm core should fail the cmd if the tpg is gonna when we submit the cmd in tcm_vhost_submission_work. (nab, is this true?)

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

The struct tcm_vhost_tpg is per target. I assume you want to point private_data to the 'struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET]'

No, I want to put it at the array of targets.

tcm_vhost_tpg is allocated in tcm_vhost_make_tpg. There is no array of the targets. The targets exist when user create them in host side using targetcli tools or /sys/kernel/config interface.

Quoted 8 lines
Quoted 5 lines

I really simply mean this field:

	        struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];

allocate it dynamically when endpoint is set, and
set private data for each vq.

What's the benefit of allocating it dynamically? Why bother it if the current static and simpler one works ok.

Because this makes it the lifetime rules clear:
instead of changing values in the array, you
replace the pointer to the array.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

So do you have further concerns other than the ACCESS_ONCE one.

It's just ugly to use a pointer as a flag. vhost uses private_data to point to the constant backend structure, and NULL if there's no backend, so vhost-scsi should just do this too then it won't have problems.

I'm fine to point vq->private_data to vs_tpg, but I'm not 100% like the idea to allocate it dynamically.

Anyway, I will send out v3 trying to make you happy ;-)

Quoted 19 lines
Quoted 19 lines
Quoted 16 lines
Quoted 16 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
Signature · 1 line
Asias
#31 lore raw

Re: [PATCH V2 2/2] tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Asias He
Quoted 161 lines · Asias He
Quoted 60 lines · Michael S. Tsirkin
Quoted 59 lines · Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +18 -1
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
Quoted 10 lines

drivers/vhost/tcm_vhost.c static void vhost_scsi_handle_vq(struct vhost_scsi *vs, · line 603

Quoted 5 lines
drivers/vhost/tcm_vhost.c +1 -2
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 

I would just move the check to under vq mutex, and avoid rcu completely. In vhost-net we are using private data outside lock so we can't do this, no such issue here.

Are you talking about:

   handle_tx:
           /* TODO: check that we are running from vhost_worker? */
           sock = rcu_dereference_check(vq->private_data, 1);
           if (!sock)
                   return;
   
           wmem = atomic_read(&sock->sk->sk_wmem_alloc);
           if (wmem >= sock->sk->sk_sndbuf) {
                   mutex_lock(&vq->mutex);
                   tx_poll_start(net, sock);
                   mutex_unlock(&vq->mutex);
                   return;
           }
           mutex_lock(&vq->mutex);

Why not do the atomic_read and tx_poll_start under the vq->mutex, and thus do the check under the lock as well.

   handle_rx:
           mutex_lock(&vq->mutex);
           /* TODO: check that we are running from vhost_worker? */
           struct socket *sock = rcu_dereference_check(vq->private_data, 1);
   
           if (!sock)
                   return;
   
           mutex_lock(&vq->mutex);

Can't we can do the check under the vq->mutex here?

The rcu is still there but it makes the code easier to read. IMO, If we want to use rcu, use it explicitly and avoid the vhost rcu completely.

Quoted 44 lines
Quoted 5 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];

drivers/vhost/tcm_vhost.c static int vhost_scsi_set_endpoint( · line 856

Quoted 10 lines
drivers/vhost/tcm_vhost.c +2 -1
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;

There's also some weird smp_mb__after_atomic_inc() with no atomic in sight just above ... Nicholas what was the point there?

Quoted 23 lines
drivers/vhost/tcm_vhost.c +9 -1
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
Quoted 8 lines

drivers/vhost/tcm_vhost.c static int vhost_scsi_clear_endpoint( · line 917

Quoted 3 lines
drivers/vhost/tcm_vhost.c +3 -0
+			mutex_unlock(&vq->mutex);
+		}
+	}

I'm trying to understand what's going on here. Does vhost_scsi only have a single target? Because the moment you clear one target you
also set private_data to NULL ...

Quoted 3 lines

vhost_scsi supports multi target. Currently, We can not disable specific target under the wwpn. When we clear or set the endpoint, we disable or enable all the targets under the wwpn.

okay, but changing vs->vs_tpg[target] under dev mutex, then using it under vq mutex looks wrong.

Since we want to use private_data anyway, how about making private_data point at struct tcm_vhost_tpg * ?

Allocate it dynamically in SET_ENDPOINT (and free old value if any).

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
drivers/vhost/tcm_vhost.c +0 -0
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4
Signature · 1 line
Asias
author
#32 lore raw

PATCH v2 2/2 tcm_vhost: Use vq->private_data to indicate if the endpoint is setup

In reply to Asias He

Currently, vs->vs_endpoint is used indicate if the endpoint is setup or not. It is set or cleared in vhost_scsi_set_endpoint() or vhost_scsi_clear_endpoint() under the vs->dev.mutex lock. However, when we check it in vhost_scsi_handle_vq(), we ignored the lock.

Instead of using the vs->vs_endpoint and the vs->dev.mutex lock to indicate the status of the endpoint, we use per virtqueue vq->private_data to indicate it. In this way, we can only take the vq->mutex lock which is per queue and make the concurrent multiqueue process having less lock contention. Further, in the read side of vq->private_data, we can even do not take only lock if it is accessed in the vhost worker thread, because it is protected by "vhost rcu".

---
 drivers/vhost/tcm_vhost.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
drivers/vhost/tcm_vhost.c +33 -5
index 5e3d4487..0524267 100644
@@ -67,7 +67,6 @@ struct vhost_scsi {
 	/* Protected by vhost_scsi->dev.mutex */
 	struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
 	char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
-	bool vs_endpoint;
 
 	struct vhost_dev dev;
 	struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
@@ -91,6 +90,24 @@ static int iov_num_pages(struct iovec *iov)
 	       ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
 }
 
+static bool tcm_vhost_check_endpoint(struct vhost_virtqueue *vq)
+{
+	bool ret = false;
+
+	/*
+	 * We can handle the vq only after the endpoint is setup by calling the
+	 * VHOST_SCSI_SET_ENDPOINT ioctl.
+	 *
+	 * TODO: Check that we are running from vhost_worker which acts
+	 * as read-side critical section for vhost kind of RCU.
+	 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
+	 */
+	if (rcu_dereference_check(vq->private_data, 1))
+		ret = true;
+
+	return ret;
+}
+
 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
 {
 	return 1;
@@ -581,8 +598,7 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
 	int head, ret;
 	u8 target;
 
-	/* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
-	if (unlikely(!vs->vs_endpoint))
+	if (!tcm_vhost_check_endpoint(vq))
 		return;
 
 	mutex_lock(&vq->mutex);
@@ -829,11 +845,12 @@ static int vhost_scsi_set_endpoint(
 		       sizeof(vs->vs_vhost_wwpn));
 		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
 			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
 			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, vs);
 			vhost_init_used(vq);
 			mutex_unlock(&vq->mutex);
 		}
-		vs->vs_endpoint = true;
 		ret = 0;
 	} else {
 		ret = -EEXIST;
@@ -849,6 +866,8 @@ static int vhost_scsi_clear_endpoint(
 {
 	struct tcm_vhost_tport *tv_tport;
 	struct tcm_vhost_tpg *tv_tpg;
+	struct vhost_virtqueue *vq;
+	bool match = false;
 	int index, ret, i;
 	u8 target;
 
@@ -884,9 +903,18 @@ static int vhost_scsi_clear_endpoint(
 		}
 		tv_tpg->tv_tpg_vhost_count--;
 		vs->vs_tpg[target] = NULL;
-		vs->vs_endpoint = false;
+		match = true;
 		mutex_unlock(&tv_tpg->tv_tpg_mutex);
 	}
+	if (match) {
+		for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
+			vq = &vs->vqs[i];
+			/* Flushing the vhost_work acts as synchronize_rcu */
+			mutex_lock(&vq->mutex);
+			rcu_assign_pointer(vq->private_data, NULL);
+			mutex_unlock(&vq->mutex);
+		}
+	}
 	mutex_unlock(&vs->dev.mutex);
 	return 0;
 
Signature · 1 line
1.8.1.4