Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1261349

[RFC] vfs: don't bother clearing close_on_exec bit for unused fds

From Rasmus Villemoes <linux@rasmusvillemoes.dk>
Newsgroups linux.kernel
Subject [RFC] vfs: don't bother clearing close_on_exec bit for unused fds
Date 2015-11-03 10:50 +0100
Message-ID <qqGo3-WT-43@gated-at.bofh.it> (permalink)
Organization linux.* mail to news gateway

Show all headers | View raw


In fc90888d07b8 (vfs: conditionally clear close-on-exec flag) a
conditional was added to __clear_close_on_exec to avoid dirtying a
cache line in the common case where the bit is already clear. However,
AFAICT, we don't rely on the close_on_exec bit being clear for unused
fds, except as an optimization in do_close_on_exec(); if I haven't
missed anything, __{set,clear}_close_on_exec is always called when a
new fd is allocated. At the expense of also reading through ->open_fds
in do_close_on_exec(), we can avoid accessing the close_on_exec bitmap
altogether in close(), which I think is a reasonable trade-off.

The conditional added in the commit above still makes sense to avoid
the dirtying on the allocation paths, but I also think it might make
sense in __set_close_on_exec: I suppose any given app handling a
non-trivial amount of fds uses O_CLOEXEC for either almost none or
almost all of them.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
I'm sure I've missed something, hence the RFC. But if not, there's
probably also a few memsets which become redundant. And the
__set_close_on_exec part should probably be its own patch...

 fs/file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/file.c b/fs/file.c
index c6986dce0334..93cfbcd450c3 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -231,7 +231,8 @@ repeat:
 
 static inline void __set_close_on_exec(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(int fd, struct fdtable *fdt)
@@ -644,7 +645,6 @@ int __close_fd(struct files_struct *files, unsigned fd)
 	if (!file)
 		goto out_unlock;
 	rcu_assign_pointer(fdt->fd[fd], NULL);
-	__clear_close_on_exec(fd, fdt);
 	__put_unused_fd(files, fd);
 	spin_unlock(&files->file_lock);
 	return filp_close(file, files);
@@ -667,7 +667,7 @@ 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;
-- 
2.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[RFC] vfs: don't bother clearing close_on_exec bit for unused fds Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-11-03 10:50 +0100
  Re: [RFC] vfs: don't bother clearing close_on_exec bit for unused fds Linus Torvalds <torvalds@linux-foundation.org> - 2015-11-03 23:50 +0100
    Re: [RFC] vfs: don't bother clearing close_on_exec bit for unused fds Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-11-04 00:20 +0100
  Re: [RFC] vfs: don't bother clearing close_on_exec bit for unused  fds Eric Dumazet <eric.dumazet@gmail.com> - 2015-11-04 02:40 +0100
    Re: [RFC] vfs: don't bother clearing close_on_exec bit for unused fds Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2015-11-04 12:00 +0100
      Re: [RFC] vfs: don't bother clearing close_on_exec bit for unused  fds Eric Dumazet <eric.dumazet@gmail.com> - 2015-11-04 13:40 +0100

csiph-web