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 prev parent reply other threads:[~2024-08-12 7:57 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-12 6:42 [PATCHES] fs/file.c stuff Al Viro
2024-08-12 6:44 ` [PATCH 01/11] get rid of ...lookup...fdget_rcu() family Al Viro
2024-08-12 6:44 ` [PATCH 02/11] remove pointless includes of <linux/fdtable.h> Al Viro
2024-08-12 9:25 ` Christian Brauner
2024-08-12 6:44 ` [PATCH 03/11] close_files(): don't bother with xchg() Al Viro
2024-08-12 7:56 ` Mateusz Guzik [this message]
2024-08-14 5:24 ` [PATCH] close_files(): reimplement based on do_close_on_exec() Al Viro
2024-08-14 5:34 ` Mateusz Guzik
2024-08-12 6:44 ` [PATCH 04/11] proc_fd_getattr(): don't bother with S_ISDIR() check Al Viro
2024-08-12 6:44 ` [PATCH 05/11] move close_range(2) into fs/file.c, fold __close_range() into it Al Viro
2024-08-12 6:44 ` [PATCH 06/11] sane_fdtable_size(): don't bother looking at descriptors we are not going to copy Al Viro
2024-08-12 9:30 ` Christian Brauner
2024-08-12 6:44 ` [PATCH 07/11] fs/file.c: remove sanity_check and add likely/unlikely in alloc_fd() Al Viro
2024-08-12 6:44 ` [PATCH 08/11] fs/file.c: conditionally clear full_fds Al Viro
2024-08-12 6:44 ` [PATCH 09/11] fs/file.c: add fast path in find_next_fd() Al Viro
2024-08-12 6:44 ` [PATCH 10/11] alloc_fdtable(): change calling conventions Al Viro
2024-08-12 9:35 ` Christian Brauner
2024-08-12 6:44 ` [PATCH 11/11] dup_fd(): " Al Viro
2024-08-12 9:32 ` Christian Brauner
2024-08-12 9:24 ` [PATCH 01/11] get rid of ...lookup...fdget_rcu() family Christian Brauner
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;
as well as URLs for NNTP newsgroup(s).