fs/pipe: use spinlock in pipe_read() only if there is a watch_queue
[linux-2.6-block.git] / fs / pipe.c
index 939def02c18cc88c9a7c311ca7161861b33d7f0a..1362e2ec2211588313c39c5845487dfee8cae156 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -227,6 +227,36 @@ static inline bool pipe_readable(const struct pipe_inode_info *pipe)
        return !pipe_empty(head, tail) || !writers;
 }
 
+static inline unsigned int pipe_update_tail(struct pipe_inode_info *pipe,
+                                           struct pipe_buffer *buf,
+                                           unsigned int tail)
+{
+       pipe_buf_release(pipe, buf);
+
+       /*
+        * If the pipe has a watch_queue, we need additional protection
+        * by the spinlock because notifications get posted with only
+        * this spinlock, no mutex
+        */
+       if (pipe_has_watch_queue(pipe)) {
+               spin_lock_irq(&pipe->rd_wait.lock);
+#ifdef CONFIG_WATCH_QUEUE
+               if (buf->flags & PIPE_BUF_FLAG_LOSS)
+                       pipe->note_loss = true;
+#endif
+               pipe->tail = ++tail;
+               spin_unlock_irq(&pipe->rd_wait.lock);
+               return tail;
+       }
+
+       /*
+        * Without a watch_queue, we can simply increment the tail
+        * without the spinlock - the mutex is enough.
+        */
+       pipe->tail = ++tail;
+       return tail;
+}
+
 static ssize_t
 pipe_read(struct kiocb *iocb, struct iov_iter *to)
 {
@@ -320,17 +350,8 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
                                buf->len = 0;
                        }
 
-                       if (!buf->len) {
-                               pipe_buf_release(pipe, buf);
-                               spin_lock_irq(&pipe->rd_wait.lock);
-#ifdef CONFIG_WATCH_QUEUE
-                               if (buf->flags & PIPE_BUF_FLAG_LOSS)
-                                       pipe->note_loss = true;
-#endif
-                               tail++;
-                               pipe->tail = tail;
-                               spin_unlock_irq(&pipe->rd_wait.lock);
-                       }
+                       if (!buf->len)
+                               tail = pipe_update_tail(pipe, buf, tail);
                        total_len -= chars;
                        if (!total_len)
                                break;  /* common path: read succeeded */