Skip to contentSkip to conversation

virtiofs: reduce lock contention on fpq->lock

PATCH In review Liu Bo 2 participants 3 messages Jul 30, 2019 – Aug 1, 2019 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Liu Bo

author
#0 lore raw

PATCH 1/1 virtiofs: reduce lock contention on fpq->lock

Right now within virtio_fs_requests_done_work(), virtqueue is protected by both fsvq->lock and fpq->lock for processing both virtqueue and fpq->processing_list, however, it is fine for them to be protected separately.

Also since %req has been removed from fpq->processing_list, no one except request_wait_answer() is looking at this %req and request_wait_answer() waits only on FINISH flag, it's OK to remove fpq->lock after %req is dropped from the list.

---
 fs/fuse/virtio_fs.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
fs/fuse/virtio_fs.c +6 -7
index 9b3f2e9..d88bb6c 100644
@@ -347,21 +347,22 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 	/* Collect completed requests off the virtqueue */
 	spin_lock(&fsvq->lock);
-	/* grab fpq->lock as req is on fpq->processing list */
-	spin_lock(&fpq->lock);
 	do {
 		unsigned len;
 
 		virtqueue_disable_cb(vq);
 
 		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
+			/* grab fpq->lock as req is on fpq->processing list */
+			spin_lock(&fpq->lock);
+
 			/* If !connected, req is freed by fuse_abort_conn. */
-			if (fpq->connected) {
+			if (fpq->connected)
 				list_move_tail(&req->list, &reqs);
-			}
+
+			spin_unlock(&fpq->lock);
 		}
 	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
-	spin_unlock(&fpq->lock);
 	spin_unlock(&fsvq->lock);
 
 	/* End requests */
@@ -373,10 +374,8 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 		/* TODO zeroing? */
 
-		spin_lock(&fpq->lock);
 		clear_bit(FR_SENT, &req->flags);
 		list_del_init(&req->list);
-		spin_unlock(&fpq->lock);
 
 		fuse_request_end(fc, req);
 	}
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw

Hi Jun,

Quoted 6 lines · piaojun

Hi liubo,

This change has already committed in rhvgoyal's branch:

https://github.com/rhvgoyal/linux/tree/virtio-fs-dev-5.1

Thanks for the link, the patch here does one more thing beyond Vivek's current code, i.e. the lock protection around req is also removed as it's not necessary.

thanks,
-liubo

Quoted 63 lines

Thanks,
Jun

Quoted 59 lines · Liu Bo

Right now within virtio_fs_requests_done_work(), virtqueue is protected by both fsvq->lock and fpq->lock for processing both virtqueue and fpq->processing_list, however, it is fine for them to be protected separately.

Also since %req has been removed from fpq->processing_list, no one except request_wait_answer() is looking at this %req and request_wait_answer() waits only on FINISH flag, it's OK to remove fpq->lock after %req is dropped from the list.

---
 fs/fuse/virtio_fs.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
fs/fuse/virtio_fs.c +6 -7
index 9b3f2e9..d88bb6c 100644
@@ -347,21 +347,22 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 	/* Collect completed requests off the virtqueue */
 	spin_lock(&fsvq->lock);
-	/* grab fpq->lock as req is on fpq->processing list */
-	spin_lock(&fpq->lock);
 	do {
 		unsigned len;
 
 		virtqueue_disable_cb(vq);
 
 		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
+			/* grab fpq->lock as req is on fpq->processing list */
+			spin_lock(&fpq->lock);
+
 			/* If !connected, req is freed by fuse_abort_conn. */
-			if (fpq->connected) {
+			if (fpq->connected)
 				list_move_tail(&req->list, &reqs);
-			}
+
+			spin_unlock(&fpq->lock);
 		}
 	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
-	spin_unlock(&fpq->lock);
 	spin_unlock(&fsvq->lock);
 
 	/* End requests */
@@ -373,10 +374,8 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 		/* TODO zeroing? */
 
-		spin_lock(&fpq->lock);
 		clear_bit(FR_SENT, &req->flags);
 		list_del_init(&req->list);
-		spin_unlock(&fpq->lock);
 
 		fuse_request_end(fc, req);
 	}
#3 lore raw
Quoted 5 lines · Liu Bo

Hi Jun,

Quoted 2 lines · piaojun

Hi liubo,

Quoted 7 lines
Quoted 4 lines

This change has already committed in rhvgoyal's branch:

https://github.com/rhvgoyal/linux/tree/virtio-fs-dev-5.1

Thanks for the link, the patch here does one more thing beyond Vivek's current code, i.e. the lock protection around req is also removed as it's not necessary.

Agreed, and it could save some CPU time.

Thanks,
Jun

Quoted 69 lines

thanks,
-liubo

Quoted 55 lines

Thanks,
Jun

Quoted 51 lines · Liu Bo

Right now within virtio_fs_requests_done_work(), virtqueue is protected by both fsvq->lock and fpq->lock for processing both virtqueue and fpq->processing_list, however, it is fine for them to be protected separately.

Also since %req has been removed from fpq->processing_list, no one except request_wait_answer() is looking at this %req and request_wait_answer() waits only on FINISH flag, it's OK to remove fpq->lock after %req is dropped from the list.

---
 fs/fuse/virtio_fs.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
fs/fuse/virtio_fs.c +6 -5
index 9b3f2e9..d88bb6c 100644
@@ -347,21 +347,22 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 	/* Collect completed requests off the virtqueue */
 	spin_lock(&fsvq->lock);
-	/* grab fpq->lock as req is on fpq->processing list */
-	spin_lock(&fpq->lock);
 	do {
 		unsigned len;
 
 		virtqueue_disable_cb(vq);
 
 		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
+			/* grab fpq->lock as req is on fpq->processing list */
+			spin_lock(&fpq->lock);
+
 			/* If !connected, req is freed by fuse_abort_conn. */
-			if (fpq->connected) {
+			if (fpq->connected)
 				list_move_tail(&req->list, &reqs);
-			}
+
+			spin_unlock(&fpq->lock);
 		}
 	} while (!virtqueue_enable_cb(vq) && likely(!virtqueue_is_broken(vq)));
-	spin_unlock(&fpq->lock);
 	spin_unlock(&fsvq->lock);
 
 	/* End requests */
@@ -373,10 +374,8 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 
 		/* TODO zeroing? */
 
Quoted 8 lines

fs/fuse/virtio_fs.c static void virtio_fs_requests_done_work(struct work_struct *work) · line 381

Quoted 8 lines
fs/fuse/virtio_fs.c +0 -2
-		spin_lock(&fpq->lock);
 		clear_bit(FR_SENT, &req->flags);
 		list_del_init(&req->list);
-		spin_unlock(&fpq->lock);
 
 		fuse_request_end(fc, req);
 	}

.