Skip to contentSkip to conversation

vhost/vsock: handle vhost_vq_init_access() error

PATCH In review Stefan Hajnoczi 3 participants 6 messages Jan 19, 2017 – Jan 23, 2017

Subsystems (3)
lore
Share & tools
lore

Stefan Hajnoczi

author maintainer
#0 lore raw

PATCH 1/1 vhost/vsock: handle vhost_vq_init_access() error

Propagate the error when vhost_vq_init_access() fails and set vq->private_data to NULL.

---
 drivers/vhost/vsock.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +9 -4
index bbbf5885..ce5e63d 100644
@@ -373,6 +373,7 @@ static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
 
 static int vhost_vsock_start(struct vhost_vsock *vsock)
 {
+	struct vhost_virtqueue *vq;
 	size_t i;
 	int ret;
 
@@ -383,19 +384,20 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
 		goto err;
 
 	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
 
 		mutex_lock(&vq->mutex);
 
 		if (!vhost_vq_access_ok(vq)) {
 			ret = -EFAULT;
-			mutex_unlock(&vq->mutex);
 			goto err_vq;
 		}
 
 		if (!vq->private_data) {
 			vq->private_data = vsock;
-			vhost_vq_init_access(vq);
+			ret = vhost_vq_init_access(vq);
+			if (ret)
+				goto err_vq;
 		}
 
 		mutex_unlock(&vq->mutex);
@@ -405,8 +407,11 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
 	return 0;
 
 err_vq:
+	vq->private_data = NULL;
+	mutex_unlock(&vq->mutex);
+
 	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
 
 		mutex_lock(&vq->mutex);
 		vq->private_data = NULL;
Signature · 1 line
2.9.3
#1 lore raw
Quoted 45 lines · Stefan Hajnoczi

Propagate the error when vhost_vq_init_access() fails and set vq->private_data to NULL.

---
  drivers/vhost/vsock.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +5 -3
index bbbf5885..ce5e63d 100644
@@ -373,6 +373,7 @@ static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
 
  static int vhost_vsock_start(struct vhost_vsock *vsock)
  {
+	struct vhost_virtqueue *vq;
  	size_t i;
  	int ret;
 
@@ -383,19 +384,20 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
  		goto err;
 
  	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
 
  		mutex_lock(&vq->mutex);
 
  		if (!vhost_vq_access_ok(vq)) {
  			ret = -EFAULT;
-			mutex_unlock(&vq->mutex);
  			goto err_vq;
  		}
 
  		if (!vq->private_data) {
  			vq->private_data = vsock;
-			vhost_vq_init_access(vq);
+			ret = vhost_vq_init_access(vq);
+			if (ret)
+				goto err_vq;
  		}
 
  		mutex_unlock(&vq->mutex);

drivers/vhost/vsock.c static int vhost_vsock_start(struct vhost_vsock *vsock) · line 410

Quoted 4 lines
drivers/vhost/vsock.c +1 -0
@@ -405,8 +407,11 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
  	return 0;
 
  err_vq:
+	vq->private_data = NULL;

Looks like this will be done twice?

Thanks

Quoted 8 lines
drivers/vhost/vsock.c +3 -1
+	mutex_unlock(&vq->mutex);
+
  	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
 
  		mutex_lock(&vq->mutex);
  		vq->private_data = NULL;
author maintainer
#2 lore raw
Quoted 38 lines · Jason Wang
Quoted 35 lines · Stefan Hajnoczi

Propagate the error when vhost_vq_init_access() fails and set vq->private_data to NULL.

---
  drivers/vhost/vsock.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +5 -3
index bbbf5885..ce5e63d 100644
@@ -373,6 +373,7 @@ static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
  static int vhost_vsock_start(struct vhost_vsock *vsock)
  {
+	struct vhost_virtqueue *vq;
  	size_t i;
  	int ret;
@@ -383,19 +384,20 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
  		goto err;
  	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
  		mutex_lock(&vq->mutex);
  		if (!vhost_vq_access_ok(vq)) {
  			ret = -EFAULT;
-			mutex_unlock(&vq->mutex);
  			goto err_vq;
  		}
  		if (!vq->private_data) {
  			vq->private_data = vsock;
-			vhost_vq_init_access(vq);
+			ret = vhost_vq_init_access(vq);
+			if (ret)
+				goto err_vq;
Quoted 8 lines

drivers/vhost/vsock.c static int vhost_vsock_start(struct vhost_vsock *vsock) · line 409

Quoted 6 lines
drivers/vhost/vsock.c +1 -0
  		}
  		mutex_unlock(&vq->mutex);
@@ -405,8 +407,11 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
  	return 0;
  err_vq:
+	vq->private_data = NULL;

Looks like this will be done twice?

Here we still hold the vq mutex. If we release the mutex then a virtqueue kick could be processed.

Stefan

#3 lore raw
Quoted 45 lines · Stefan Hajnoczi
Quoted 36 lines · Jason Wang
Quoted 34 lines · Stefan Hajnoczi

Propagate the error when vhost_vq_init_access() fails and set vq->private_data to NULL.

---
   drivers/vhost/vsock.c | 13 +++++++++----
   1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +4 -3
index bbbf5885..ce5e63d 100644
@@ -373,6 +373,7 @@ static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
   static int vhost_vsock_start(struct vhost_vsock *vsock)
   {
+	struct vhost_virtqueue *vq;
   	size_t i;
   	int ret;
@@ -383,19 +384,20 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
   		goto err;
   	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
   		mutex_lock(&vq->mutex);
   		if (!vhost_vq_access_ok(vq)) {
   			ret = -EFAULT;
-			mutex_unlock(&vq->mutex);
   			goto err_vq;
   		}
   		if (!vq->private_data) {
   			vq->private_data = vsock;
-			vhost_vq_init_access(vq);
+			ret = vhost_vq_init_access(vq);
+			if (ret)
Quoted 8 lines

drivers/vhost/vsock.c static int vhost_vsock_start(struct vhost_vsock *vsock) · line 409

Quoted 7 lines
drivers/vhost/vsock.c +2 -0
+				goto err_vq;
   		}
   		mutex_unlock(&vq->mutex);
@@ -405,8 +407,11 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
   	return 0;
   err_vq:
+	vq->private_data = NULL;

Looks like this will be done twice?

Quoted 4 lines

Here we still hold the vq mutex. If we release the mutex then a virtqueue kick could be processed.

Stefan

Ok, then it's better to assign vq->private_data after vhost_vq_init_acess() succeed.

Thanks

#4 lore raw
Quoted 48 lines · Jason Wang
Quoted 45 lines · Stefan Hajnoczi
Quoted 44 lines · Jason Wang
Quoted 38 lines · Stefan Hajnoczi

Propagate the error when vhost_vq_init_access() fails and set vq->private_data to NULL.

---
   drivers/vhost/vsock.c | 13 +++++++++----
   1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +5 -3
index bbbf5885..ce5e63d 100644
@@ -373,6 +373,7 @@ static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
   static int vhost_vsock_start(struct vhost_vsock *vsock)
   {
+	struct vhost_virtqueue *vq;
   	size_t i;
   	int ret;
@@ -383,19 +384,20 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
   		goto err;
   	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
   		mutex_lock(&vq->mutex);
   		if (!vhost_vq_access_ok(vq)) {
   			ret = -EFAULT;
-			mutex_unlock(&vq->mutex);
   			goto err_vq;
   		}
   		if (!vq->private_data) {
   			vq->private_data = vsock;
-			vhost_vq_init_access(vq);
+			ret = vhost_vq_init_access(vq);
+			if (ret)
+				goto err_vq;
   		}
   		mutex_unlock(&vq->mutex);

drivers/vhost/vsock.c static int vhost_vsock_start(struct vhost_vsock *vsock) · line 409

Quoted 3 lines
drivers/vhost/vsock.c +1 -0
@@ -405,8 +407,11 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
   	return 0;
   err_vq:
+	vq->private_data = NULL;

Looks like this will be done twice?

Quoted 7 lines
Quoted 4 lines

Here we still hold the vq mutex. If we release the mutex then a virtqueue kick could be processed.

Stefan

Ok, then it's better to assign vq->private_data after vhost_vq_init_acess() succeed.

No, that won't work because vhost_vq_init_acess() requires that vq->private_data is non-NULL.

Stefan

Acked-by
#5 lore raw
Quoted 50 lines · Stefan Hajnoczi
Quoted 49 lines · Jason Wang
Quoted 47 lines · Stefan Hajnoczi
Quoted 35 lines · Jason Wang
Quoted 34 lines · Stefan Hajnoczi

Propagate the error when vhost_vq_init_access() fails and set vq->private_data to NULL.

---
    drivers/vhost/vsock.c | 13 +++++++++----
    1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +4 -3
index bbbf5885..ce5e63d 100644
@@ -373,6 +373,7 @@ static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
    static int vhost_vsock_start(struct vhost_vsock *vsock)
    {
+	struct vhost_virtqueue *vq;
    	size_t i;
    	int ret;
@@ -383,19 +384,20 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
    		goto err;
    	for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
-		struct vhost_virtqueue *vq = &vsock->vqs[i];
+		vq = &vsock->vqs[i];
    		mutex_lock(&vq->mutex);
    		if (!vhost_vq_access_ok(vq)) {
    			ret = -EFAULT;
-			mutex_unlock(&vq->mutex);
    			goto err_vq;
    		}
    		if (!vq->private_data) {
    			vq->private_data = vsock;
-			vhost_vq_init_access(vq);
+			ret = vhost_vq_init_access(vq);
+			if (ret)
Quoted 8 lines

drivers/vhost/vsock.c static int vhost_vsock_start(struct vhost_vsock *vsock) · line 409

Quoted 7 lines
drivers/vhost/vsock.c +2 -0
+				goto err_vq;
    		}
    		mutex_unlock(&vq->mutex);
@@ -405,8 +407,11 @@ static int vhost_vsock_start(struct vhost_vsock *vsock)
    	return 0;
    err_vq:
+	vq->private_data = NULL;

Looks like this will be done twice?

Here we still hold the vq mutex. If we release the mutex then a virtqueue kick could be processed.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Stefan

Ok, then it's better to assign vq->private_data after vhost_vq_init_acess() succeed.

No, that won't work because vhost_vq_init_acess() requires that vq->private_data is non-NULL.

Stefan

Oh right. Then