diff mbox

[VHOST] fix race with guest on multi-buffer used buffer updates

Message ID 1274374686.8492.12.camel@w-dls.beaverton.ibm.com (mailing list archive)
State New, archived
Headers show

Commit Message

David Stevens May 20, 2010, 4:58 p.m. UTC
None
diff mbox

Patch

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 7f2568d..74790ab 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -1065,14 +1065,6 @@  static int __vhost_add_used_n(struct vhost_virtqueue *vq,
 		vq_err(vq, "Failed to write used");
 		return -EFAULT;
 	}
-	/* Make sure buffer is written before we update index. */
-	smp_wmb();
-	if (put_user(vq->last_used_idx + count, &vq->used->idx)) {
-		vq_err(vq, "Failed to increment used idx");
-		return -EFAULT;
-	}
-	if (unlikely(vq->log_used))
-		vhost_log_used(vq, used);
 	vq->last_used_idx += count;
 	return 0;
 }
@@ -1093,7 +1085,17 @@  int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
 		heads += n;
 		count -= n;
 	}
-	return __vhost_add_used_n(vq, heads, count);
+	r = __vhost_add_used_n(vq, heads, count);
+
+	/* Make sure buffer is written before we update index. */
+	smp_wmb();
+	if (put_user(vq->last_used_idx, &vq->used->idx)) {
+		vq_err(vq, "Failed to increment used idx");
+		return -EFAULT;
+	}
+	if (unlikely(vq->log_used))
+		vhost_log_used(vq, vq->used->ring + start);
+	return r;
 }
 
 /* This actually signals the guest, using eventfd. */