From: Mateusz Guzik <mjguzik@gmail.com>
To: viro@zeniv.linux.org.uk
Cc: brauner@kernel.org, jack@suse.cz, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, Mateusz Guzik <mjguzik@gmail.com>
Subject: [PATCH] close_files(): reimplement based on do_close_on_exec()
Date: Mon, 12 Aug 2024 09:56:58 +0200 [thread overview]
Message-ID: <20240812075659.1399447-1-mjguzik@gmail.com> (raw)
In-Reply-To: <20240812064427.240190-3-viro@zeniv.linux.org.uk>
While here take more advantage of the fact nobody should be messing with
the table anymore and don't clear the fd slot.
Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
---
how about this instead, I think it's a nicer clean up.
It's literally do_close_on_exec except locking and put fd are deleted.
boots & does not blow up, but admittedly I did not bother with ltp or
any serious testing
fs/file.c | 37 +++++++++++++++++++++----------------
1 file changed, 21 insertions(+), 16 deletions(-)
diff --git a/fs/file.c b/fs/file.c
index 74d7ad676579..3ff2e8265156 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -389,33 +389,38 @@ struct files_struct *dup_fd(struct files_struct *oldf, unsigned int max_fds)
return newf;
}
-static struct fdtable *close_files(struct files_struct * files)
+static struct fdtable *close_files(struct files_struct *files)
{
/*
* It is safe to dereference the fd table without RCU or
* ->file_lock because this is the last reference to the
* files structure.
+ *
+ * For the same reason we can skip locking.
*/
struct fdtable *fdt = rcu_dereference_raw(files->fdt);
- unsigned int i, j = 0;
+ unsigned i;
- for (;;) {
+ for (i = 0; ; i++) {
unsigned long set;
- i = j * BITS_PER_LONG;
- if (i >= fdt->max_fds)
+ unsigned fd = i * BITS_PER_LONG;
+ fdt = files_fdtable(files);
+ if (fd >= fdt->max_fds)
break;
- set = fdt->open_fds[j++];
- while (set) {
- if (set & 1) {
- struct file * file = xchg(&fdt->fd[i], NULL);
- if (file) {
- filp_close(file, files);
- cond_resched();
- }
- }
- i++;
- set >>= 1;
+ set = fdt->open_fds[i];
+ if (!set)
+ continue;
+ for ( ; set ; fd++, set >>= 1) {
+ struct file *file;
+ if (!(set & 1))
+ continue;
+ file = fdt->fd[fd];
+ if (!file)
+ continue;
+ filp_close(file, files);
+ cond_resched();
}
+
}
return fdt;
--
2.43.0
next parent reply other threads:[~2024-08-12 7:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240812064427.240190-3-viro@zeniv.linux.org.uk>
2024-08-12 7:56 ` Mateusz Guzik [this message]
2024-08-14 5:24 ` Al Viro
2024-08-14 5:34 ` Mateusz Guzik
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=20240812075659.1399447-1-mjguzik@gmail.com \
--to=mjguzik@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--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
all inboxes | Powered by JetHome®