Skip to contentSkip to conversation

fs/file.c: make more use of cloexec bits not being cleared

PATCH New Rasmus Villemoes 1 participant 1 message Oct 24, 2018 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/file.c: make more use of cloexec bits not being cleared

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].

---
 fs/file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/file.c +3 -4
index 7ffd6e9d103d..7bc44e7084e5 100644
@@ -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))
Signature · 1 line
2.19.1.6.gbde171bbf5