take rlimit check to callers of expand_files()
authorAl Viro <viro@zeniv.linux.org.uk>
Sun, 12 Aug 2012 20:17:59 +0000 (16:17 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 27 Sep 2012 01:08:53 +0000 (21:08 -0400)
... except for one in android, where the check is different
and already done in caller.  No need to recalculate rlimit
many times in alloc_fd() either.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/fcntl.c
fs/file.c

index 887b5ba8c9b56be800d4d3f7a799fc21f1c7892c..08e6af5c1b1f72af71f11f3ce7deb579f968e65a 100644 (file)
@@ -64,6 +64,9 @@ SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
        if (unlikely(oldfd == newfd))
                return -EINVAL;
 
+       if (newfd >= rlimit(RLIMIT_NOFILE))
+               return -EMFILE;
+
        spin_lock(&files->file_lock);
        err = expand_files(files, newfd);
        file = fcheck(oldfd);
index 5b46e9970a7ac35f47347c3f945cbb6c3d516fe7..08922af4a62c37b773faa75779fe2d2579721b83 100644 (file)
--- a/fs/file.c
+++ b/fs/file.c
@@ -251,13 +251,6 @@ int expand_files(struct files_struct *files, int nr)
 
        fdt = files_fdtable(files);
 
-       /*
-        * N.B. For clone tasks sharing a files structure, this test
-        * will limit the total number of files that can be opened.
-        */
-       if (nr >= rlimit(RLIMIT_NOFILE))
-               return -EMFILE;
-
        /* Do we need to expand? */
        if (nr < fdt->max_fds)
                return 0;
@@ -431,6 +424,7 @@ int alloc_fd(unsigned start, unsigned flags)
 {
        struct files_struct *files = current->files;
        unsigned int fd;
+       unsigned end = rlimit(RLIMIT_NOFILE);
        int error;
        struct fdtable *fdt;
 
@@ -444,6 +438,14 @@ repeat:
        if (fd < fdt->max_fds)
                fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);
 
+       /*
+        * N.B. For clone tasks sharing a files structure, this test
+        * will limit the total number of files that can be opened.
+        */
+       error = -EMFILE;
+       if (fd >= end)
+               goto out;
+
        error = expand_files(files, fd);
        if (error < 0)
                goto out;