linux-fsdevel.vger.kernel.org archive mirror
 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 08/16] exec: release fdtable after exec locks drop
Date: Wed, 02 Sep 2026 17:58:26 +0200	[thread overview]
Message-ID: <20260902-work-coredump-unlock-self-v2-8-1bece368cbb1@kernel.org> (raw)
In-Reply-To: <20260902-work-coredump-unlock-self-v2-0-1bece368cbb1@kernel.org>

During exec the task gets a private fdtable and releases the shared
fdtable. This all happens under cred_guard_mutex. If the exec'ing task
has the last reference on the fdtable close_files() closes all files.
Today this happens to be deferred to task work. Following patches will
make that part synchronous so the final put needs to move out of the
cred_guard_mutex.

The shared table now lives until free_bprm() instead of dying in
begin_new_exec(). This doesn't matter as nothing can get access to the
old table anymore and closing of files would've been deferred to task
work anyway.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/exec.c               | 6 ++++--
 include/linux/binfmts.h | 2 ++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 713819244d23..be8601d9c691 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1165,7 +1165,7 @@ int begin_new_exec(struct linux_binprm * bprm)
 	if (retval)
 		goto out;
 	if (files)
-		put_files_struct(switch_files_struct(me, files));
+		bprm->old_files = switch_files_struct(me, files);
 
 	/*
 	 * Must be called _before_ exec_mmap() as bprm->mm is
@@ -1472,7 +1472,9 @@ static void free_bprm(struct linux_binprm *bprm)
 		mutex_unlock(&current->signal->cred_guard_mutex);
 		abort_creds(bprm->cred);
 	}
-	/* The exec locks are out of the way now, see close_cloexec_files(). */
+	/* The exec locks are gone, release what begin_new_exec() closed. */
+	if (bprm->old_files)
+		put_files_struct(bprm->old_files);
 	fput_list(&bprm->cloexec_files);
 	/* exec swapped the mm but failed before setup_new_exec() freed it */
 	if (bprm->old_mm)
diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h
index be935320f1c7..0f0fcf859b4f 100644
--- a/include/linux/binfmts.h
+++ b/include/linux/binfmts.h
@@ -37,6 +37,8 @@ struct linux_binprm {
 #endif
 	struct mm_struct *mm;
 	struct mm_struct *old_mm;	/* replaced address space, freed by setup_new_exec() */
+	/* Descriptor table displaced by begin_new_exec(), put by free_bprm(). */
+	struct files_struct *old_files;
 	/* user_ns published to task->exec_state at execve, narrowed by would_dump(). */
 	struct user_namespace *user_ns;
 	unsigned long p; /* current top of mem */

-- 
2.53.0


  parent reply	other threads:[~2026-09-02 15:59 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 ` [PATCH RFC v2 02/16] fs: add fput_close_list() and fput_list() Christian Brauner
2026-09-02 17:41   ` 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 ` Christian Brauner [this message]
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-8-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;
as well as URLs for NNTP newsgroup(s).