Skip to content

Commit 51fecdd

Browse files
rhvgoyalMiklos Szeredi
authored andcommitted
virtiofs: Do not end request in submission context
Submission context can hold some locks which end request code tries to hold again and deadlock can occur. For example, fc->bg_lock. If a background request is being submitted, it might hold fc->bg_lock and if we could not submit request (because device went away) and tried to end request, then deadlock happens. During testing, I also got a warning from deadlock detection code. So put requests on a list and end requests from a worker thread. I got following warning from deadlock detector. [ 603.137138] WARNING: possible recursive locking detected [ 603.137142] -------------------------------------------- [ 603.137144] blogbench/2036 is trying to acquire lock: [ 603.137149] 00000000f0f51107 (&(&fc->bg_lock)->rlock){+.+.}, at: fuse_request_end+0xdf/0x1c0 [fuse] [ 603.140701] [ 603.140701] but task is already holding lock: [ 603.140703] 00000000f0f51107 (&(&fc->bg_lock)->rlock){+.+.}, at: fuse_simple_background+0x92/0x1d0 [fuse] [ 603.140713] [ 603.140713] other info that might help us debug this: [ 603.140714] Possible unsafe locking scenario: [ 603.140714] [ 603.140715] CPU0 [ 603.140716] ---- [ 603.140716] lock(&(&fc->bg_lock)->rlock); [ 603.140718] lock(&(&fc->bg_lock)->rlock); [ 603.140719] [ 603.140719] *** DEADLOCK *** Signed-off-by: Vivek Goyal <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]>
1 parent 6c26f71 commit 51fecdd

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

fs/fuse/virtio_fs.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct virtio_fs_vq {
3030
struct virtqueue *vq; /* protected by ->lock */
3131
struct work_struct done_work;
3232
struct list_head queued_reqs;
33+
struct list_head end_reqs; /* End these requests */
3334
struct delayed_work dispatch_work;
3435
struct fuse_dev *fud;
3536
bool connected;
@@ -259,8 +260,27 @@ static void virtio_fs_hiprio_done_work(struct work_struct *work)
259260
spin_unlock(&fsvq->lock);
260261
}
261262

262-
static void virtio_fs_dummy_dispatch_work(struct work_struct *work)
263+
static void virtio_fs_request_dispatch_work(struct work_struct *work)
263264
{
265+
struct fuse_req *req;
266+
struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq,
267+
dispatch_work.work);
268+
struct fuse_conn *fc = fsvq->fud->fc;
269+
270+
pr_debug("virtio-fs: worker %s called.\n", __func__);
271+
while (1) {
272+
spin_lock(&fsvq->lock);
273+
req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req,
274+
list);
275+
if (!req) {
276+
spin_unlock(&fsvq->lock);
277+
return;
278+
}
279+
280+
list_del_init(&req->list);
281+
spin_unlock(&fsvq->lock);
282+
fuse_request_end(fc, req);
283+
}
264284
}
265285

266286
static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
@@ -502,6 +522,7 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,
502522
names[VQ_HIPRIO] = fs->vqs[VQ_HIPRIO].name;
503523
INIT_WORK(&fs->vqs[VQ_HIPRIO].done_work, virtio_fs_hiprio_done_work);
504524
INIT_LIST_HEAD(&fs->vqs[VQ_HIPRIO].queued_reqs);
525+
INIT_LIST_HEAD(&fs->vqs[VQ_HIPRIO].end_reqs);
505526
INIT_DELAYED_WORK(&fs->vqs[VQ_HIPRIO].dispatch_work,
506527
virtio_fs_hiprio_dispatch_work);
507528
spin_lock_init(&fs->vqs[VQ_HIPRIO].lock);
@@ -511,8 +532,9 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,
511532
spin_lock_init(&fs->vqs[i].lock);
512533
INIT_WORK(&fs->vqs[i].done_work, virtio_fs_requests_done_work);
513534
INIT_DELAYED_WORK(&fs->vqs[i].dispatch_work,
514-
virtio_fs_dummy_dispatch_work);
535+
virtio_fs_request_dispatch_work);
515536
INIT_LIST_HEAD(&fs->vqs[i].queued_reqs);
537+
INIT_LIST_HEAD(&fs->vqs[i].end_reqs);
516538
snprintf(fs->vqs[i].name, sizeof(fs->vqs[i].name),
517539
"requests.%u", i - VQ_REQUEST);
518540
callbacks[i] = virtio_fs_vq_done;
@@ -918,6 +940,7 @@ __releases(fiq->lock)
918940
struct fuse_conn *fc;
919941
struct fuse_req *req;
920942
struct fuse_pqueue *fpq;
943+
struct virtio_fs_vq *fsvq;
921944
int ret;
922945

923946
WARN_ON(list_empty(&fiq->pending));
@@ -951,7 +974,8 @@ __releases(fiq->lock)
951974
smp_mb__after_atomic();
952975

953976
retry:
954-
ret = virtio_fs_enqueue_req(&fs->vqs[queue_id], req);
977+
fsvq = &fs->vqs[queue_id];
978+
ret = virtio_fs_enqueue_req(fsvq, req);
955979
if (ret < 0) {
956980
if (ret == -ENOMEM || ret == -ENOSPC) {
957981
/* Virtqueue full. Retry submission */
@@ -965,7 +989,12 @@ __releases(fiq->lock)
965989
clear_bit(FR_SENT, &req->flags);
966990
list_del_init(&req->list);
967991
spin_unlock(&fpq->lock);
968-
fuse_request_end(fc, req);
992+
993+
/* Can't end request in submission context. Use a worker */
994+
spin_lock(&fsvq->lock);
995+
list_add_tail(&req->list, &fsvq->end_reqs);
996+
schedule_delayed_work(&fsvq->dispatch_work, 0);
997+
spin_unlock(&fsvq->lock);
969998
return;
970999
}
9711000
}

0 commit comments

Comments
 (0)