linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] fs/file.c: make more use of cloexec bits not being cleared
Date: Wed, 24 Oct 2018 17:01:58 +0100	[thread overview]
Message-ID: <20181024160159.25884-1-linux@rasmusvillemoes.dk> (raw)

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


                 reply	other threads:[~2018-10-24 16:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181024160159.25884-1-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).