x86/smpboot: Init apic mapping before usage
[cascardo/linux.git] / fs / pipe.c
index 5c7c8b7..8e0d9f2 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -267,7 +267,6 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
                if (bufs) {
                        int curbuf = pipe->curbuf;
                        struct pipe_buffer *buf = pipe->bufs + curbuf;
-                       const struct pipe_buf_operations *ops = buf->ops;
                        size_t chars = buf->len;
                        size_t written;
                        int error;
@@ -275,7 +274,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
                        if (chars > total_len)
                                chars = total_len;
 
-                       error = ops->confirm(pipe, buf);
+                       error = pipe_buf_confirm(pipe, buf);
                        if (error) {
                                if (!ret)
                                        ret = error;
@@ -299,8 +298,7 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
                        }
 
                        if (!buf->len) {
-                               buf->ops = NULL;
-                               ops->release(pipe, buf);
+                               pipe_buf_release(pipe, buf);
                                curbuf = (curbuf + 1) & (pipe->buffers - 1);
                                pipe->curbuf = curbuf;
                                pipe->nrbufs = --bufs;
@@ -383,11 +381,10 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
                int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
                                                        (pipe->buffers - 1);
                struct pipe_buffer *buf = pipe->bufs + lastbuf;
-               const struct pipe_buf_operations *ops = buf->ops;
                int offset = buf->offset + buf->len;
 
-               if (ops->can_merge && offset + chars <= PAGE_SIZE) {
-                       ret = ops->confirm(pipe, buf);
+               if (buf->ops->can_merge && offset + chars <= PAGE_SIZE) {
+                       ret = pipe_buf_confirm(pipe, buf);
                        if (ret)
                                goto out;
 
@@ -604,54 +601,63 @@ pipe_fasync(int fd, struct file *filp, int on)
        return retval;
 }
 
-static void account_pipe_buffers(struct pipe_inode_info *pipe,
+static unsigned long account_pipe_buffers(struct user_struct *user,
                                  unsigned long old, unsigned long new)
 {
-       atomic_long_add(new - old, &pipe->user->pipe_bufs);
+       return atomic_long_add_return(new - old, &user->pipe_bufs);
 }
 
-static bool too_many_pipe_buffers_soft(struct user_struct *user)
+static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
 {
-       return pipe_user_pages_soft &&
-              atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft;
+       return pipe_user_pages_soft && user_bufs >= pipe_user_pages_soft;
 }
 
-static bool too_many_pipe_buffers_hard(struct user_struct *user)
+static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
 {
-       return pipe_user_pages_hard &&
-              atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard;
+       return pipe_user_pages_hard && user_bufs >= pipe_user_pages_hard;
 }
 
 struct pipe_inode_info *alloc_pipe_info(void)
 {
        struct pipe_inode_info *pipe;
+       unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
+       struct user_struct *user = get_current_user();
+       unsigned long user_bufs;
 
        pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL_ACCOUNT);
-       if (pipe) {
-               unsigned long pipe_bufs = PIPE_DEF_BUFFERS;
-               struct user_struct *user = get_current_user();
-
-               if (!too_many_pipe_buffers_hard(user)) {
-                       if (too_many_pipe_buffers_soft(user))
-                               pipe_bufs = 1;
-                       pipe->bufs = kcalloc(pipe_bufs,
-                                            sizeof(struct pipe_buffer),
-                                            GFP_KERNEL_ACCOUNT);
-               }
+       if (pipe == NULL)
+               goto out_free_uid;
 
-               if (pipe->bufs) {
-                       init_waitqueue_head(&pipe->wait);
-                       pipe->r_counter = pipe->w_counter = 1;
-                       pipe->buffers = pipe_bufs;
-                       pipe->user = user;
-                       account_pipe_buffers(pipe, 0, pipe_bufs);
-                       mutex_init(&pipe->mutex);
-                       return pipe;
-               }
-               free_uid(user);
-               kfree(pipe);
+       if (pipe_bufs * PAGE_SIZE > pipe_max_size && !capable(CAP_SYS_RESOURCE))
+               pipe_bufs = pipe_max_size >> PAGE_SHIFT;
+
+       user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
+
+       if (too_many_pipe_buffers_soft(user_bufs)) {
+               user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
+               pipe_bufs = 1;
+       }
+
+       if (too_many_pipe_buffers_hard(user_bufs))
+               goto out_revert_acct;
+
+       pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
+                            GFP_KERNEL_ACCOUNT);
+
+       if (pipe->bufs) {
+               init_waitqueue_head(&pipe->wait);
+               pipe->r_counter = pipe->w_counter = 1;
+               pipe->buffers = pipe_bufs;
+               pipe->user = user;
+               mutex_init(&pipe->mutex);
+               return pipe;
        }
 
+out_revert_acct:
+       (void) account_pipe_buffers(user, pipe_bufs, 0);
+       kfree(pipe);
+out_free_uid:
+       free_uid(user);
        return NULL;
 }
 
@@ -659,12 +665,12 @@ void free_pipe_info(struct pipe_inode_info *pipe)
 {
        int i;
 
-       account_pipe_buffers(pipe, pipe->buffers, 0);
+       (void) account_pipe_buffers(pipe->user, pipe->buffers, 0);
        free_uid(pipe->user);
        for (i = 0; i < pipe->buffers; i++) {
                struct pipe_buffer *buf = pipe->bufs + i;
                if (buf->ops)
-                       buf->ops->release(pipe, buf);
+                       pipe_buf_release(pipe, buf);
        }
        if (pipe->tmp_page)
                __free_page(pipe->tmp_page);
@@ -1010,13 +1016,55 @@ const struct file_operations pipefifo_fops = {
        .fasync         = pipe_fasync,
 };
 
+/*
+ * Currently we rely on the pipe array holding a power-of-2 number
+ * of pages.
+ */
+static inline unsigned int round_pipe_size(unsigned int size)
+{
+       unsigned long nr_pages;
+
+       nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
+       return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
+}
+
 /*
  * Allocate a new array of pipe buffers and copy the info over. Returns the
  * pipe size if successful, or return -ERROR on error.
  */
-static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
+static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
 {
        struct pipe_buffer *bufs;
+       unsigned int size, nr_pages;
+       unsigned long user_bufs;
+       long ret = 0;
+
+       size = round_pipe_size(arg);
+       nr_pages = size >> PAGE_SHIFT;
+
+       if (!nr_pages)
+               return -EINVAL;
+
+       /*
+        * If trying to increase the pipe capacity, check that an
+        * unprivileged user is not trying to exceed various limits
+        * (soft limit check here, hard limit check just below).
+        * Decreasing the pipe capacity is always permitted, even
+        * if the user is currently over a limit.
+        */
+       if (nr_pages > pipe->buffers &&
+                       size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
+               return -EPERM;
+
+       user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages);
+
+       if (nr_pages > pipe->buffers &&
+                       (too_many_pipe_buffers_hard(user_bufs) ||
+                        too_many_pipe_buffers_soft(user_bufs)) &&
+                       !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
+               ret = -EPERM;
+               goto out_revert_acct;
+       }
 
        /*
         * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
@@ -1024,13 +1072,17 @@ static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
         * again like we would do for growing. If the pipe currently
         * contains more buffers than arg, then return busy.
         */
-       if (nr_pages < pipe->nrbufs)
-               return -EBUSY;
+       if (nr_pages < pipe->nrbufs) {
+               ret = -EBUSY;
+               goto out_revert_acct;
+       }
 
        bufs = kcalloc(nr_pages, sizeof(*bufs),
                       GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
-       if (unlikely(!bufs))
-               return -ENOMEM;
+       if (unlikely(!bufs)) {
+               ret = -ENOMEM;
+               goto out_revert_acct;
+       }
 
        /*
         * The pipe array wraps around, so just start the new one at zero
@@ -1053,24 +1105,15 @@ static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
                        memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
        }
 
-       account_pipe_buffers(pipe, pipe->buffers, nr_pages);
        pipe->curbuf = 0;
        kfree(pipe->bufs);
        pipe->bufs = bufs;
        pipe->buffers = nr_pages;
        return nr_pages * PAGE_SIZE;
-}
-
-/*
- * Currently we rely on the pipe array holding a power-of-2 number
- * of pages.
- */
-static inline unsigned int round_pipe_size(unsigned int size)
-{
-       unsigned long nr_pages;
 
-       nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
-       return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
+out_revert_acct:
+       (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers);
+       return ret;
 }
 
 /*
@@ -1112,28 +1155,9 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
        __pipe_lock(pipe);
 
        switch (cmd) {
-       case F_SETPIPE_SZ: {
-               unsigned int size, nr_pages;
-
-               size = round_pipe_size(arg);
-               nr_pages = size >> PAGE_SHIFT;
-
-               ret = -EINVAL;
-               if (!nr_pages)
-                       goto out;
-
-               if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
-                       ret = -EPERM;
-                       goto out;
-               } else if ((too_many_pipe_buffers_hard(pipe->user) ||
-                           too_many_pipe_buffers_soft(pipe->user)) &&
-                          !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
-                       ret = -EPERM;
-                       goto out;
-               }
-               ret = pipe_set_size(pipe, nr_pages);
+       case F_SETPIPE_SZ:
+               ret = pipe_set_size(pipe, arg);
                break;
-               }
        case F_GETPIPE_SZ:
                ret = pipe->buffers * PAGE_SIZE;
                break;
@@ -1142,7 +1166,6 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
                break;
        }
 
-out:
        __pipe_unlock(pipe);
        return ret;
 }