We can expand somewhat on 529790827054 (vfs: stop clearing close on exec
when closing a fd). First, since we now leave the cloexec bits set,
similar to how we try to avoid dirtying a cache line in the !O_CLOEXEC
case, test whether the bit is set before setting it.

Furthermore, we don't care about the state of close_on_exec in the new
bits when we expand the fdtable, we don't need to clear close_on_exec[i]
in do_close_on_exec, and since we may have quite a few bits set in
close_on_exec[i] without an actual open file, mask the set with
open_fds[i].

Signed-off-by: Rasmus Villemoes <li...@rasmusvillemoes.dk>
---
 fs/file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index 7ffd6e9d103d..7bc44e7084e5 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -56,7 +56,6 @@ static void copy_fd_bitmaps(struct fdtable *nfdt, struct 
fdtable *ofdt,
        memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
        memset((char *)nfdt->open_fds + cpy, 0, set);
        memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
-       memset((char *)nfdt->close_on_exec + cpy, 0, set);
 
        cpy = BITBIT_SIZE(count);
        set = BITBIT_SIZE(nfdt->max_fds) - cpy;
@@ -227,7 +226,8 @@ static int expand_files(struct files_struct *files, 
unsigned int nr)
 
 static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt)
 {
-       __set_bit(fd, fdt->close_on_exec);
+       if (!test_bit(fd, fdt->close_on_exec))
+               __set_bit(fd, fdt->close_on_exec);
 }
 
 static inline void __clear_close_on_exec(unsigned int fd, struct fdtable *fdt)
@@ -653,10 +653,9 @@ void do_close_on_exec(struct files_struct *files)
                fdt = files_fdtable(files);
                if (fd >= fdt->max_fds)
                        break;
-               set = fdt->close_on_exec[i];
+               set = fdt->close_on_exec[i] & fdt->open_fds[i];
                if (!set)
                        continue;
-               fdt->close_on_exec[i] = 0;
                for ( ; set ; fd++, set >>= 1) {
                        struct file *file;
                        if (!(set & 1))
-- 
2.19.1.6.gbde171bbf5

Reply via email to