linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs/file.c: make more use of cloexec bits not being cleared
@ 2018-10-24 16:01 Rasmus Villemoes
  0 siblings, 0 replies; only message in thread
From: Rasmus Villemoes @ 2018-10-24 16:01 UTC (permalink / raw)
  To: Alexander Viro; +Cc: Rasmus Villemoes, linux-fsdevel, linux-kernel

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 <linux@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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-10-24 16:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-24 16:01 [PATCH] fs/file.c: make more use of cloexec bits not being cleared Rasmus Villemoes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).