fuse: iqueue locking

Use fiq->waitq.lock for protecting members of struct fuse_iqueue and
FR_PENDING request flag, previously protected by fc->lock.

Following patches will remove fc->lock protection from these members.

Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
Reviewed-by: Ashish Samant <ashish.samant@oracle.com>
diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
index a450940..65ad9b1 100644
--- a/fs/fuse/dev.c
+++ b/fs/fuse/dev.c
@@ -328,7 +328,7 @@
 	req->in.h.len = sizeof(struct fuse_in_header) +
 		len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
 	list_add_tail(&req->list, &fiq->pending);
-	wake_up(&fiq->waitq);
+	wake_up_locked(&fiq->waitq);
 	kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
 }
 
@@ -341,14 +341,16 @@
 	forget->forget_one.nlookup = nlookup;
 
 	spin_lock(&fc->lock);
+	spin_lock(&fiq->waitq.lock);
 	if (fiq->connected) {
 		fiq->forget_list_tail->next = forget;
 		fiq->forget_list_tail = forget;
-		wake_up(&fiq->waitq);
+		wake_up_locked(&fiq->waitq);
 		kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
 	} else {
 		kfree(forget);
 	}
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 }
 
@@ -362,8 +364,10 @@
 		req = list_entry(fc->bg_queue.next, struct fuse_req, list);
 		list_del(&req->list);
 		fc->active_background++;
+		spin_lock(&fiq->waitq.lock);
 		req->in.h.unique = fuse_get_unique(fiq);
 		queue_request(fiq, req);
+		spin_unlock(&fiq->waitq.lock);
 	}
 }
 
@@ -380,10 +384,13 @@
 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
 __releases(fc->lock)
 {
+	struct fuse_iqueue *fiq = &fc->iq;
 	void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
 	req->end = NULL;
 	list_del_init(&req->list);
+	spin_lock(&fiq->waitq.lock);
 	list_del_init(&req->intr_entry);
+	spin_unlock(&fiq->waitq.lock);
 	WARN_ON(test_bit(FR_PENDING, &req->flags));
 	WARN_ON(test_bit(FR_SENT, &req->flags));
 	smp_wmb();
@@ -415,13 +422,16 @@
 
 static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
 {
+	spin_lock(&fiq->waitq.lock);
 	list_add_tail(&req->intr_entry, &fiq->interrupts);
-	wake_up(&fiq->waitq);
+	wake_up_locked(&fiq->waitq);
+	spin_unlock(&fiq->waitq.lock);
 	kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
 }
 
 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
 {
+	struct fuse_iqueue *fiq = &fc->iq;
 	int err;
 
 	if (!fc->no_interrupt) {
@@ -434,7 +444,7 @@
 		spin_lock(&fc->lock);
 		set_bit(FR_INTERRUPTED, &req->flags);
 		if (test_bit(FR_SENT, &req->flags))
-			queue_interrupt(&fc->iq, req);
+			queue_interrupt(fiq, req);
 		spin_unlock(&fc->lock);
 	}
 
@@ -451,14 +461,17 @@
 			return;
 
 		spin_lock(&fc->lock);
+		spin_lock(&fiq->waitq.lock);
 		/* Request is not yet in userspace, bail out */
 		if (test_bit(FR_PENDING, &req->flags)) {
 			list_del(&req->list);
+			spin_unlock(&fiq->waitq.lock);
 			spin_unlock(&fc->lock);
 			__fuse_put_request(req);
 			req->out.h.error = -EINTR;
 			return;
 		}
+		spin_unlock(&fiq->waitq.lock);
 		spin_unlock(&fc->lock);
 	}
 
@@ -475,8 +488,10 @@
 
 	BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
 	spin_lock(&fc->lock);
+	spin_lock(&fiq->waitq.lock);
 	if (!fiq->connected) {
 		spin_unlock(&fc->lock);
+		spin_unlock(&fiq->waitq.lock);
 		req->out.h.error = -ENOTCONN;
 	} else {
 		req->in.h.unique = fuse_get_unique(fiq);
@@ -484,6 +499,7 @@
 		/* acquire extra reference, since request is still needed
 		   after request_end() */
 		__fuse_get_request(req);
+		spin_unlock(&fiq->waitq.lock);
 		spin_unlock(&fc->lock);
 
 		request_wait_answer(fc, req);
@@ -619,10 +635,12 @@
 	__clear_bit(FR_ISREPLY, &req->flags);
 	req->in.h.unique = unique;
 	spin_lock(&fc->lock);
+	spin_lock(&fiq->waitq.lock);
 	if (fiq->connected) {
 		queue_request(fiq, req);
 		err = 0;
 	}
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 
 	return err;
@@ -1064,8 +1082,10 @@
 
 /* Wait until a request is available on the pending list */
 static void request_wait(struct fuse_conn *fc)
+__releases(fc->iq.waitq.lock)
 __releases(fc->lock)
 __acquires(fc->lock)
+__acquires(fc->iq.waitq.lock)
 {
 	struct fuse_iqueue *fiq = &fc->iq;
 	DECLARE_WAITQUEUE(wait, current);
@@ -1076,9 +1096,11 @@
 		if (signal_pending(current))
 			break;
 
+		spin_unlock(&fiq->waitq.lock);
 		spin_unlock(&fc->lock);
 		schedule();
 		spin_lock(&fc->lock);
+		spin_lock(&fiq->waitq.lock);
 	}
 	set_current_state(TASK_RUNNING);
 	remove_wait_queue(&fiq->waitq, &wait);
@@ -1094,15 +1116,17 @@
  */
 static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
 			       size_t nbytes, struct fuse_req *req)
+__releases(fc->iq.waitq.lock)
 __releases(fc->lock)
 {
+	struct fuse_iqueue *fiq = &fc->iq;
 	struct fuse_in_header ih;
 	struct fuse_interrupt_in arg;
 	unsigned reqsize = sizeof(ih) + sizeof(arg);
 	int err;
 
 	list_del_init(&req->intr_entry);
-	req->intr_unique = fuse_get_unique(&fc->iq);
+	req->intr_unique = fuse_get_unique(fiq);
 	memset(&ih, 0, sizeof(ih));
 	memset(&arg, 0, sizeof(arg));
 	ih.len = reqsize;
@@ -1110,6 +1134,7 @@
 	ih.unique = req->intr_unique;
 	arg.unique = req->in.h.unique;
 
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 	if (nbytes < reqsize)
 		return -EINVAL;
@@ -1147,6 +1172,7 @@
 static int fuse_read_single_forget(struct fuse_conn *fc,
 				   struct fuse_copy_state *cs,
 				   size_t nbytes)
+__releases(fc->iq.waitq.lock)
 __releases(fc->lock)
 {
 	int err;
@@ -1162,6 +1188,7 @@
 		.len = sizeof(ih) + sizeof(arg),
 	};
 
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 	kfree(forget);
 	if (nbytes < ih.len)
@@ -1180,6 +1207,7 @@
 
 static int fuse_read_batch_forget(struct fuse_conn *fc,
 				   struct fuse_copy_state *cs, size_t nbytes)
+__releases(fc->iq.waitq.lock)
 __releases(fc->lock)
 {
 	int err;
@@ -1195,12 +1223,14 @@
 	};
 
 	if (nbytes < ih.len) {
+		spin_unlock(&fiq->waitq.lock);
 		spin_unlock(&fc->lock);
 		return -EINVAL;
 	}
 
 	max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
 	head = dequeue_forget(fiq, max_forgets, &count);
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 
 	arg.count = count;
@@ -1230,6 +1260,7 @@
 
 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
 			    size_t nbytes)
+__releases(fc->iq.waitq.lock)
 __releases(fc->lock)
 {
 	struct fuse_iqueue *fiq = &fc->iq;
@@ -1260,6 +1291,7 @@
 
  restart:
 	spin_lock(&fc->lock);
+	spin_lock(&fiq->waitq.lock);
 	err = -EAGAIN;
 	if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
 	    !request_pending(fiq))
@@ -1290,6 +1322,8 @@
 	req = list_entry(fiq->pending.next, struct fuse_req, list);
 	clear_bit(FR_PENDING, &req->flags);
 	list_del_init(&req->list);
+	spin_unlock(&fiq->waitq.lock);
+
 	list_add(&req->list, &fc->io);
 
 	in = &req->in;
@@ -1333,6 +1367,7 @@
 	return reqsize;
 
  err_unlock:
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 	return err;
 }
@@ -2055,10 +2090,12 @@
 	poll_wait(file, &fiq->waitq, wait);
 
 	spin_lock(&fc->lock);
+	spin_lock(&fiq->waitq.lock);
 	if (!fiq->connected)
 		mask = POLLERR;
 	else if (request_pending(fiq))
 		mask |= POLLIN | POLLRDNORM;
+	spin_unlock(&fiq->waitq.lock);
 	spin_unlock(&fc->lock);
 
 	return mask;
@@ -2141,11 +2178,13 @@
 		fc->max_background = UINT_MAX;
 		flush_bg_queue(fc);
 
+		spin_lock(&fiq->waitq.lock);
 		fiq->connected = 0;
 		list_splice_init(&fiq->pending, &to_end2);
 		while (forget_pending(fiq))
 			kfree(dequeue_forget(fiq, 1, NULL));
-		wake_up_all(&fiq->waitq);
+		wake_up_all_locked(&fiq->waitq);
+		spin_unlock(&fiq->waitq.lock);
 		kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
 
 		list_splice_init(&fc->processing, &to_end2);