Linux filesystem development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: NeilBrown <neil@brown.name>, Oleg Nesterov <oleg@redhat.com>,
	 linux-fsdevel@vger.kernel.org
Cc: Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
	 Xin Zhao <jackzxcui1989@163.com>,
	Mateusz Guzik <mjguzik@gmail.com>,
	 Jeff Layton <jlayton@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH RFC v2 02/16] fs: add fput_close_list() and fput_list()
Date: Wed, 02 Sep 2026 17:58:20 +0200	[thread overview]
Message-ID: <20260902-work-coredump-unlock-self-v2-2-1bece368cbb1@kernel.org> (raw)
In-Reply-To: <20260902-work-coredump-unlock-self-v2-0-1bece368cbb1@kernel.org>

Add helpers for paths that close many files in one go. They collect the
last references during the walk and drain them right after

fput_list() runs the final __fput() for everything on the list.
delayed_fput() shares that loop.

We'll use that in the next patches.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/file_table.c | 25 +++++++++++++++++++++----
 fs/internal.h   |  3 +++
 fs/open.c       |  2 +-
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/fs/file_table.c b/fs/file_table.c
index c68b8c0a4097..617912ca4e0a 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -528,11 +528,10 @@ static void __fput(struct file *file)
 static LLIST_HEAD(delayed_fput_list);
 static void delayed_fput(struct work_struct *unused)
 {
-	struct llist_node *node = llist_del_all(&delayed_fput_list);
-	struct file *f, *t;
+	/* Detach the shared list once, then drain it like a private one. */
+	struct llist_head list = { .first = llist_del_all(&delayed_fput_list) };
 
-	llist_for_each_entry_safe(f, t, node, f_llist)
-		__fput(f);
+	fput_list(&list);
 }
 
 static void ____fput(struct callback_head *work)
@@ -629,6 +628,24 @@ void fput_close(struct file *file)
 		__fput_deferred(file);
 }
 
+/* Like fput_close(), but the last reference goes on the caller's @list. */
+void fput_close_list(struct file *file, struct llist_head *list)
+{
+	if (file_ref_put_close(&file->f_ref))
+		__llist_add(&file->f_llist, list);
+}
+
+/* Run the final __fput() for everything on the private @list, right here. */
+void fput_list(struct llist_head *list)
+{
+	struct file *f, *t;
+
+	llist_for_each_entry_safe(f, t, __llist_del_all(list), f_llist) {
+		__fput(f);
+		cond_resched();
+	}
+}
+
 void __init files_init(void)
 {
 	struct kmem_cache_args args = {
diff --git a/fs/internal.h b/fs/internal.h
index c658c8a5ebd5..619eaeb23fd9 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -129,6 +129,8 @@ static inline void put_file_access(struct file *file)
 
 void fput_close_sync(struct file *);
 void fput_close(struct file *);
+void fput_close_list(struct file *file, struct llist_head *list);
+void fput_list(struct llist_head *list);
 
 /*
  * super.c
@@ -198,6 +200,7 @@ extern struct file *do_file_open_root(const struct path *,
 extern struct open_how build_open_how(int flags, umode_t mode);
 extern int build_open_flags(const struct open_how *how, struct open_flags *op);
 struct file *file_close_fd_locked(struct files_struct *files, unsigned fd);
+int filp_flush(struct file *filp, fl_owner_t id);
 
 int do_ftruncate(struct file *file, loff_t length, unsigned int flags);
 int chmod_common(const struct path *path, umode_t mode);
diff --git a/fs/open.c b/fs/open.c
index 6b1c14e684a9..7181665c3af5 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1506,7 +1506,7 @@ SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  * "id" is the POSIX thread ID. We use the
  * files pointer for this..
  */
-static int filp_flush(struct file *filp, fl_owner_t id)
+int filp_flush(struct file *filp, fl_owner_t id)
 {
 	int retval = 0;
 

-- 
2.53.0


  parent reply	other threads:[~2026-09-02 15:58 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-02 15:58 [PATCH RFC v2 00/16] coredump, files: exit files on request Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 01/16] fs: don't open-code file_close_fd() in close_fd() Christian Brauner
2026-09-02 15:58 ` Christian Brauner [this message]
2026-09-02 17:41   ` [PATCH RFC v2 02/16] fs: add fput_close_list() and fput_list() Mateusz Guzik
2026-09-02 17:58     ` Mateusz Guzik
2026-09-04  8:22       ` Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 03/16] fs: rename do_close_on_exec() to close_cloexec_files() Christian Brauner
2026-09-02 17:41   ` Mateusz Guzik
2026-09-02 15:58 ` [PATCH RFC v2 04/16] fs: defer the final fput of close-on-exec files past the exec locks Christian Brauner
2026-09-02 17:50   ` Mateusz Guzik
2026-09-02 15:58 ` [PATCH RFC v2 05/16] fs: add switch_files_struct() Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 06/16] fs: move unshare_fd() to fs/file.c Christian Brauner
2026-09-02 17:51   ` Mateusz Guzik
2026-09-02 15:58 ` [PATCH RFC v2 07/16] fs: remove unshare_files() Christian Brauner
2026-09-02 17:52   ` Mateusz Guzik
2026-09-02 15:58 ` [PATCH RFC v2 08/16] exec: release fdtable after exec locks drop Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 09/16] fs: make close_files() synchronous Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 10/16] fs: make close_range() synchronous Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 11/16] coredump: s/startup/done/g Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 12/16] coredump: factor out coredump_wait_inactive() Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 13/16] fs: add alloc_files_struct() Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 14/16] coredump: add COREDUMP_CLOSE_FILES Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 15/16] tools: sync coredump.h header Christian Brauner
2026-09-02 15:58 ` [PATCH RFC v2 16/16] selftests/coredump: test COREDUMP_CLOSE_FILES 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=20260902-work-coredump-unlock-self-v2-2-1bece368cbb1@kernel.org \
    --to=brauner@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=jack@suse.cz \
    --cc=jackzxcui1989@163.com \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=mjguzik@gmail.com \
    --cc=neil@brown.name \
    --cc=oleg@redhat.com \
    --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