BPF List
 help / color / mirror / Atom feed
* [PATCH v2 00/17] fs: take freeze protection for writes to backing files
@ 2026-11-16 11:03 Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 01/17] fs: add kiocb_start_write_trylock() Christian Brauner
                   ` (16 more replies)
  0 siblings, 17 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable, Amir Goldstein

While testing the pseudo filesystem series I noticed that zloop, the
nvme target's file backend and the fileio backend of the scsi target
with fd_async_io=1 call ->write_iter() of their backing files without
taking freeze protection. So their writes go straight through to a
frozen filesystem. ext4 complains loudly:

    WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0

Loop doesn't get it right for discard and write zeroes. Those end up
calling ->fallocate() of the backing file directly. The fileio backend
does the same for UNMAP. And zloop records its write pointers on flush
with vfs_setxattr() which leaves freeze protection to the caller. So all
of them modify a frozen filesystem just the same. Fix them too.

Looking for more of the same turned up a few simple ones. coredump
truncates the core file, overlayfs stores its uuid xattr on mount, nfsd
truncates on OPEN and the BPF xattr kfuncs modify xattrs, all without
freeze protection. Fix those as well. The ones that need more work in
their subsystems I have reported separately.

The fileio backend also frees its aio command in the completion handler
while the filesystem may still be using the kiocb. KASAN catches ext4
doing that for direct reads on v7.4-rc1. ext4 gets its own fix (cf. [1])
but fileio should hold a reference across the submission like loop,
zloop and fs/backing-file.c do.

[1]: https://lore.kernel.org/r/20261113-work-ext4-dio_read_kiocb-v1-1-172a88be803c@kernel.org
[2]: https://lore.kernel.org/r/20261116064654.GA2964@lst.de

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Changes in v2:
- Add kiocb_write_iter() and kiocb_end_write_iter() and use them for
  zloop, the nvme target and fileio as per Christoph's suggestion.
- Add a file_write guard and use it for loop discard and write zeroes
  and fileio unmap.
- Convert loop, vfs_iocb_iter_write() and fs/backing-file.c to the new
  helpers.
- Only complete the fileio target command when the last reference to
  the aio command is dropped.
- Link to v1: https://patch.msgid.link/20261114-work-fs-frozen_writes-v1-0-f5ae61c898b1@kernel.org

---
Christian Brauner (17):
      fs: add kiocb_start_write_trylock()
      fs: add kiocb_write_iter() and kiocb_end_write_iter()
      fs: add file_write guard
      zloop: take freeze protection for writes
      nvmet: take freeze protection for writes to files
      target: fileio: don't free the aio command under the filesystem
      target: fileio: take freeze protection for aio writes
      loop: take freeze protection for discard and write zeroes
      zloop: take freeze protection when recording write pointers
      target: fileio: take freeze protection for unmap
      coredump: take freeze protection for the truncate
      ovl: take freeze protection when storing the uuid
      bpf: fs/xattr: take freeze protection in the xattr kfuncs
      nfsd: take freeze protection for the truncate on open
      loop: use kiocb_write_iter()
      fs: use kiocb_write_iter() in vfs_iocb_iter_write()
      backing-file: use kiocb_end_write_iter()

 drivers/block/loop.c              | 13 ++++---
 drivers/block/zloop.c             | 12 +++++--
 drivers/nvme/target/io-cmd-file.c | 20 +++++++----
 drivers/target/target_core_file.c | 31 +++++++++++-----
 fs/backing-file.c                 |  8 ++---
 fs/bpf_fs_kfuncs.c                |  8 +++++
 fs/coredump.c                     |  6 ++--
 fs/nfsd/nfs4state.c               | 10 +++++-
 fs/overlayfs/util.c               |  8 +++--
 fs/read_write.c                   |  7 ++--
 include/linux/fs.h                | 75 +++++++++++++++++++++++++++++++++++++++
 io_uring/rw.c                     |  9 +----
 12 files changed, 158 insertions(+), 49 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261113-work-fs-frozen_writes-042ca762bfb8


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH v2 01/17] fs: add kiocb_start_write_trylock()
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter() Christian Brauner
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

kiocb_start_write() waits until a frozen filesystem is thawed. A
nonblocking submission can't use it. io_uring open-codes the trylock
variant for its IOCB_NOWAIT writes.

Add kiocb_start_write_trylock() next to kiocb_start_write() and use it
in io_uring. kiocb_write_iter() is going to need it as well.

No functional changes.

Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 include/linux/fs.h | 22 ++++++++++++++++++++++
 io_uring/rw.c      |  9 +--------
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 99caef43ff3c4..eefc2c9d4af19 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2801,6 +2801,28 @@ static inline void kiocb_start_write(struct kiocb *iocb)
 	__sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
 }
 
+/**
+ * kiocb_start_write_trylock - try to get write access to a superblock for
+ *			       async file io
+ * @iocb: the io context we want to submit the write with
+ *
+ * This is kiocb_start_write() for a submission that must not wait until a
+ * frozen filesystem is thawed. Should be matched with a call to
+ * kiocb_end_write() if it succeeds.
+ *
+ * Return: true if write access was acquired, false if it wasn't.
+ */
+static inline bool kiocb_start_write_trylock(struct kiocb *iocb)
+{
+	struct inode *inode = file_inode(iocb->ki_filp);
+
+	if (!sb_start_write_trylock(inode->i_sb))
+		return false;
+	/* Fool lockdep just like kiocb_start_write() does. */
+	__sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
+	return true;
+}
+
 /**
  * kiocb_end_write - drop write access to a superblock after async file io
  * @iocb: the io context we sumbitted the write with
diff --git a/io_uring/rw.c b/io_uring/rw.c
index 3a8559ff4dc66..a0a16fc521455 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -1095,9 +1095,6 @@ int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
 
 static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb)
 {
-	struct inode *inode;
-	bool ret;
-
 	if (!(req->flags & REQ_F_ISREG))
 		return true;
 	if (!(kiocb->ki_flags & IOCB_NOWAIT)) {
@@ -1105,11 +1102,7 @@ static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb)
 		return true;
 	}
 
-	inode = file_inode(kiocb->ki_filp);
-	ret = sb_start_write_trylock(inode->i_sb);
-	if (ret)
-		__sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
-	return ret;
+	return kiocb_start_write_trylock(kiocb);
 }
 
 int io_write(struct io_kiocb *req, unsigned int issue_flags)

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter()
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 01/17] fs: add kiocb_start_write_trylock() Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:30   ` sashiko-bot
  2026-11-16 11:03 ` [PATCH v2 03/17] fs: add file_write guard Christian Brauner
                   ` (14 subsequent siblings)
  16 siblings, 1 reply; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

Calling ->write_iter() with a kiocb of your own means taking freeze
protection, calling ->write_iter() and dropping freeze protection again
unless the write was queued. The completion callback then has to know
whether it completes a write that still holds freeze protection. loop
open-codes this and keys the completion on the request op. zloop, the
nvme target and target fileio need the same. Christoph pointed out that
open-coding this everywhere is fragile (cf. [1]).

Add kiocb_write_iter(). It takes freeze protection, marks the kiocb
with IOCB_WRITE like aio, io_uring and cachefiles do and calls
->write_iter(). If the write wasn't queued freeze protection is dropped
right away. With IOCB_NOWAIT it only tries to get freeze protection and
fails with -EAGAIN instead of waiting for the thaw.

kiocb_end_write_iter() drops freeze protection if the kiocb still holds
it and clears IOCB_WRITE. So a completion callback can call it for any
I/O. That matters for drivers that complete I/O that wasn't queued
through the same callback and for drivers that reuse their kiocbs.

[1]: https://lore.kernel.org/r/20261116064654.GA2964@lst.de

Suggested-by: Christoph Hellwig <hch@lst.de>
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 include/linux/fs.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index eefc2c9d4af19..0b66a09fb686a 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2840,6 +2840,54 @@ static inline void kiocb_end_write(struct kiocb *iocb)
 	sb_end_write(inode->i_sb);
 }
 
+/**
+ * kiocb_end_write_iter - drop write access after kiocb_write_iter()
+ * @iocb: the io context the write was submitted with
+ *
+ * Drop write access if @iocb still holds it and clear IOCB_WRITE. This
+ * does nothing for reads and for writes kiocb_write_iter() already
+ * finished. So the completion callback can call it for any I/O.
+ */
+static inline void kiocb_end_write_iter(struct kiocb *iocb)
+{
+	if (!(iocb->ki_flags & IOCB_WRITE))
+		return;
+	iocb->ki_flags &= ~IOCB_WRITE;
+	kiocb_end_write(iocb);
+}
+
+/**
+ * kiocb_write_iter - submit a write with write access to the superblock
+ * @iocb: the io context to submit the write with
+ * @iter: the data to write
+ *
+ * Get write access, mark @iocb with IOCB_WRITE and call ->write_iter().
+ * A write that wasn't queued drops write access before this returns. A
+ * queued write keeps it until the completion callback calls
+ * kiocb_end_write_iter(). With IOCB_NOWAIT this fails with -EAGAIN
+ * instead of waiting for a frozen filesystem.
+ *
+ * Return: On success the number of bytes written or -EIOCBQUEUED is
+ *         returned. On failure a negative error code is returned.
+ */
+static inline ssize_t kiocb_write_iter(struct kiocb *iocb,
+				       struct iov_iter *iter)
+{
+	ssize_t ret;
+
+	if (iocb->ki_flags & IOCB_NOWAIT) {
+		if (!kiocb_start_write_trylock(iocb))
+			return -EAGAIN;
+	} else {
+		kiocb_start_write(iocb);
+	}
+	iocb->ki_flags |= IOCB_WRITE;
+	ret = iocb->ki_filp->f_op->write_iter(iocb, iter);
+	if (ret != -EIOCBQUEUED)
+		kiocb_end_write_iter(iocb);
+	return ret;
+}
+
 /*
  * This is used for regular files where some users -- especially the
  * currently executed binary in a process, previously handled via

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 03/17] fs: add file_write guard
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 01/17] fs: add kiocb_start_write_trylock() Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter() Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 04/17] zloop: take freeze protection for writes Christian Brauner
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

Add a guard for file_start_write() and file_end_write().

Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 include/linux/fs.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 0b66a09fb686a..c156a326e8c89 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2782,6 +2782,11 @@ static inline void file_end_write(struct file *file)
 	sb_end_write(file_inode(file)->i_sb);
 }
 
+DEFINE_GUARD(file_write,
+	     struct file *,
+	     file_start_write(_T),
+	     file_end_write(_T))
+
 /**
  * kiocb_start_write - get write access to a superblock for async file io
  * @iocb: the io context we want to submit the write with

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 04/17] zloop: take freeze protection for writes
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (2 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 03/17] fs: add file_write guard Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 05/17] nvmet: take freeze protection for writes to files Christian Brauner
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

zloop calls ->write_iter() of the zone files without taking freeze
protection. So a write to a zloop device goes through to a filesystem
that is frozen. With the zone files on ext4:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#3: kworker/u16:4/507
  Workqueue: zloop0 zloop_cmd_workfn
  Call Trace:
   <TASK>
   __ext4_journal_start_sb+0x7a/0x470
   ext4_dirty_inode+0xa1/0x120
   __mark_inode_dirty+0x154/0x1110
   file_update_time_flags+0x3ab/0x4b0
   ext4_buffered_write_iter+0x143/0x630
   ext4_file_write_iter+0x7b9/0x1570
   zloop_do_rw+0x8ac/0xe10
   zloop_cmd_workfn+0xac5/0x1ff0
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

Submit writes with kiocb_write_iter() and drop freeze protection in the
completion callback with kiocb_end_write_iter(). zloop_rw() also calls
the completion callback for I/O that wasn't queued or that failed before
it was submitted. kiocb_end_write_iter() does nothing for these.

Fixes: eb0570c7df23 ("block: new zoned loop block device driver")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/block/zloop.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 65634a39b0bfc..76d54c9b89c0b 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -514,6 +514,7 @@ static void zloop_rw_complete(struct kiocb *iocb, long ret)
 {
 	struct zloop_cmd *cmd = container_of(iocb, struct zloop_cmd, iocb);
 
+	kiocb_end_write_iter(iocb);
 	cmd->ret = ret;
 	zloop_put_cmd(cmd);
 }
@@ -567,7 +568,7 @@ static int zloop_do_rw(struct zloop_cmd *cmd)
 	cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
 
 	if (rw == ITER_SOURCE)
-		return zone->file->f_op->write_iter(&cmd->iocb, &iter);
+		return kiocb_write_iter(&cmd->iocb, &iter);
 	return zone->file->f_op->read_iter(&cmd->iocb, &iter);
 }
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 05/17] nvmet: take freeze protection for writes to files
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (3 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 04/17] zloop: take freeze protection for writes Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 06/17] target: fileio: don't free the aio command under the filesystem Christian Brauner
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

The file backend calls ->write_iter() of the backing file without
taking freeze protection. So a write to a namespace goes through to a
filesystem that is frozen. With the backing file on ext4 and
buffered_io:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#1: kworker/1:1/56
  Workqueue: nvmet-buffered-io-wq nvmet_file_buffered_io_work
  Call Trace:
   <TASK>
   __ext4_journal_start_sb+0x7a/0x470
   ext4_dirty_inode+0xa1/0x120
   __mark_inode_dirty+0x154/0x1110
   file_update_time_flags+0x3ab/0x4b0
   ext4_buffered_write_iter+0x143/0x630
   ext4_file_write_iter+0x7b9/0x1570
   nvmet_file_submit_bvec+0x2e7/0x590
   nvmet_file_execute_io+0x460/0x830
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

The same happens with direct I/O.

Submit writes with kiocb_write_iter() and drop freeze protection in the
completion callback with kiocb_end_write_iter(). nvmet_file_io_done() is
also called for I/O that wasn't queued and for I/O that failed before
the kiocb was even set up. So queued I/O gets its own callback.

Note that buffered I/O is tried with IOCB_NOWAIT first. That attempt
must not wait until the filesystem is thawed. kiocb_write_iter() only
tries to get freeze protection then and fails with -EAGAIN otherwise.
The write is then retried from the workqueue like any other write that
would have blocked.

Fixes: d5eff33ee6f8 ("nvmet: add simple file backed ns support")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/nvme/target/io-cmd-file.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/nvme/target/io-cmd-file.c b/drivers/nvme/target/io-cmd-file.c
index 695963dd932ee..8f97dc597c038 100644
--- a/drivers/nvme/target/io-cmd-file.c
+++ b/drivers/nvme/target/io-cmd-file.c
@@ -77,27 +77,27 @@ static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
 		unsigned long nr_segs, size_t count, int ki_flags)
 {
 	struct kiocb *iocb = &req->f.iocb;
-	ssize_t (*call_iter)(struct kiocb *iocb, struct iov_iter *iter);
+	struct file *file = req->ns->file;
 	struct iov_iter iter;
 	int rw;
 
 	if (req->cmd->rw.opcode == nvme_cmd_write) {
 		if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
 			ki_flags |= IOCB_DSYNC;
-		call_iter = req->ns->file->f_op->write_iter;
 		rw = ITER_SOURCE;
 	} else {
-		call_iter = req->ns->file->f_op->read_iter;
 		rw = ITER_DEST;
 	}
 
 	iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count);
 
 	iocb->ki_pos = pos;
-	iocb->ki_filp = req->ns->file;
-	iocb->ki_flags = ki_flags | iocb->ki_filp->f_iocb_flags;
+	iocb->ki_filp = file;
+	iocb->ki_flags = ki_flags | file->f_iocb_flags;
 
-	return call_iter(iocb, &iter);
+	if (rw == ITER_DEST)
+		return file->f_op->read_iter(iocb, &iter);
+	return kiocb_write_iter(iocb, &iter);
 }
 
 static void nvmet_file_io_done(struct kiocb *iocb, long ret)
@@ -117,6 +117,12 @@ static void nvmet_file_io_done(struct kiocb *iocb, long ret)
 	nvmet_req_complete(req, status);
 }
 
+static void nvmet_file_io_complete(struct kiocb *iocb, long ret)
+{
+	kiocb_end_write_iter(iocb);
+	nvmet_file_io_done(iocb, ret);
+}
+
 static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags)
 {
 	ssize_t nr_bvec = req->sg_cnt;
@@ -175,7 +181,7 @@ static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags)
 	 * for the IOCB_NOWAIT case.
 	 */
 	if (!(ki_flags & IOCB_NOWAIT))
-		req->f.iocb.ki_complete = nvmet_file_io_done;
+		req->f.iocb.ki_complete = nvmet_file_io_complete;
 
 	ret = nvmet_file_submit_bvec(req, pos, bv_cnt, total_len, ki_flags);
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 06/17] target: fileio: don't free the aio command under the filesystem
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (4 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 05/17] nvmet: take freeze protection for writes to files Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 07/17] target: fileio: take freeze protection for aio writes Christian Brauner
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

With fd_async_io=1 the fileio backend allocates a command that holds the
kiocb for every read and write and frees it in the completion callback.
Nothing keeps the command alive until ->read_iter() or ->write_iter()
return. But a filesystem may use the kiocb after it queued the I/O. ext4
does that for direct reads:

  BUG: KASAN: slab-use-after-free in ext4_dio_read_iter+0x5e1/0x690
  Workqueue: target_submission target_queued_submit_work
  Call Trace:
   <TASK>
   dump_stack_lvl+0x5d/0x80
   print_report+0x14b/0x4b0
   kasan_report+0x108/0x130
   ext4_dio_read_iter+0x5e1/0x690
   fd_execute_rw+0xd30/0x1700
   __target_execute_cmd+0x53/0x190
   transport_generic_new_cmd+0x65a/0xdb0
   __target_submit+0x12c/0x380
   target_queued_submit_work+0x73/0x270
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

  Allocated by task 59:
   __kmalloc_noprof+0x223/0x830
   fd_execute_rw+0x766/0x1700
   __target_execute_cmd+0x53/0x190
   transport_generic_new_cmd+0x65a/0xdb0
   __target_submit+0x12c/0x380
   target_queued_submit_work+0x73/0x270

  Freed by task 14:
   kfree+0x267/0x6a0
   blk_update_request+0x3f5/0x1260
   blk_mq_end_request+0x59/0x450
   blk_complete_reqs+0xb0/0x130
   handle_softirqs+0x20c/0x860
   run_ksoftirqd+0x31/0x60

The read completed and freed the command before ext4_dio_read_iter() got
to file_accessed(iocb->ki_filp).

Do what loop, zloop and fs/backing-file.c do and hold a reference to the
command until the I/O is submitted. Only complete the target command
when the last reference is dropped as well. Otherwise the command drops
its LUN reference while the filesystem still uses the kiocb and the
device and with it the backing file can go away underneath it.

Fixes: 769031c92c8b ("scsi: target: target/file: Add support of direct and async I/O")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/target/target_core_file.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/drivers/target/target_core_file.c b/drivers/target/target_core_file.c
index ab9824a4852f1..4b23fadd7bb88 100644
--- a/drivers/target/target_core_file.c
+++ b/drivers/target/target_core_file.c
@@ -15,6 +15,7 @@
 #include <linux/timer.h>
 #include <linux/blkdev.h>
 #include <linux/slab.h>
+#include <linux/refcount.h>
 #include <linux/spinlock.h>
 #include <linux/module.h>
 #include <linux/vmalloc.h>
@@ -243,25 +244,34 @@ static void fd_destroy_device(struct se_device *dev)
 
 struct target_core_file_cmd {
 	unsigned long	len;
+	long		ret;
 	struct se_cmd	*cmd;
+	refcount_t	ref;
 	struct kiocb	iocb;
 	struct bio_vec	bvecs[];
 };
 
-static void cmd_rw_aio_complete(struct kiocb *iocb, long ret)
+static void cmd_rw_aio_put(struct target_core_file_cmd *cmd)
 {
-	struct target_core_file_cmd *cmd;
-
-	cmd = container_of(iocb, struct target_core_file_cmd, iocb);
+	if (!refcount_dec_and_test(&cmd->ref))
+		return;
 
-	if (ret != cmd->len)
+	if (cmd->ret != cmd->len)
 		target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION);
 	else
 		target_complete_cmd(cmd->cmd, SAM_STAT_GOOD);
-
 	kfree(cmd);
 }
 
+static void cmd_rw_aio_complete(struct kiocb *iocb, long ret)
+{
+	struct target_core_file_cmd *cmd;
+
+	cmd = container_of(iocb, struct target_core_file_cmd, iocb);
+	cmd->ret = ret;
+	cmd_rw_aio_put(cmd);
+}
+
 static sense_reason_t
 fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 	      enum dma_data_direction data_direction)
@@ -288,6 +298,8 @@ fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 
 	iov_iter_bvec(&iter, is_write, aio_cmd->bvecs, sgl_nents, len);
 
+	/* The filesystem may still use the kiocb after it queued the I/O. */
+	refcount_set(&aio_cmd->ref, 2);
 	aio_cmd->cmd = cmd;
 	aio_cmd->len = len;
 	aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size;
@@ -305,6 +317,7 @@ fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 
 	if (ret != -EIOCBQUEUED)
 		cmd_rw_aio_complete(&aio_cmd->iocb, ret);
+	cmd_rw_aio_put(aio_cmd);
 
 	return 0;
 }

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 07/17] target: fileio: take freeze protection for aio writes
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (5 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 06/17] target: fileio: don't free the aio command under the filesystem Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 08/17] loop: take freeze protection for discard and write zeroes Christian Brauner
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

With fd_async_io=1 the fileio backend calls ->write_iter() of the
backing file without taking freeze protection. So a write to the LUN
goes through to a filesystem that is frozen. With the backing file on
ext4:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#0: kworker/0:2/40
  Workqueue: target_submission target_queued_submit_work
  Call Trace:
   <TASK>
   __ext4_journal_start_sb+0x7a/0x470
   ext4_dirty_inode+0xa1/0x120
   __mark_inode_dirty+0x154/0x1110
   file_update_time_flags+0x3ab/0x4b0
   ext4_file_write_iter+0xf11/0x1570
   fd_execute_rw+0xb01/0x1700
   __target_execute_cmd+0x53/0x190
   tcm_loop_write_pending+0xe/0x20
   transport_generic_new_cmd+0x612/0xdb0
   __target_submit+0x12c/0x380
   target_queued_submit_work+0x73/0x270
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

The synchronous path doesn't have that problem. vfs_iter_write() takes
freeze protection.

Submit writes with kiocb_write_iter() and drop freeze protection in the
completion callback with kiocb_end_write_iter().

Note that the completion callback couldn't tell from the command whether
it completes a write anyway. COMPARE AND WRITE is a write but reads the
data to compare first. kiocb_write_iter() marks the kiocb with
IOCB_WRITE so the callback doesn't need to know.

Fixes: 769031c92c8b ("scsi: target: target/file: Add support of direct and async I/O")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/target/target_core_file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/target/target_core_file.c b/drivers/target/target_core_file.c
index 4b23fadd7bb88..e8a44ddf6f5c9 100644
--- a/drivers/target/target_core_file.c
+++ b/drivers/target/target_core_file.c
@@ -268,6 +268,7 @@ static void cmd_rw_aio_complete(struct kiocb *iocb, long ret)
 	struct target_core_file_cmd *cmd;
 
 	cmd = container_of(iocb, struct target_core_file_cmd, iocb);
+	kiocb_end_write_iter(iocb);
 	cmd->ret = ret;
 	cmd_rw_aio_put(cmd);
 }
@@ -311,7 +312,7 @@ fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 		aio_cmd->iocb.ki_flags |= IOCB_DSYNC;
 
 	if (is_write)
-		ret = file->f_op->write_iter(&aio_cmd->iocb, &iter);
+		ret = kiocb_write_iter(&aio_cmd->iocb, &iter);
 	else
 		ret = file->f_op->read_iter(&aio_cmd->iocb, &iter);
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 08/17] loop: take freeze protection for discard and write zeroes
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (6 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 07/17] target: fileio: take freeze protection for aio writes Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 09/17] zloop: take freeze protection when recording write pointers Christian Brauner
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

Discard and write zeroes requests end up in lo_fallocate() which calls
->fallocate() of the backing file directly. That skips the freeze
protection vfs_fallocate() would have taken. So they go straight
through to a filesystem that is frozen. With the backing file on ext4:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#3: kworker/u16:1/44
  Workqueue: loop1 loop_rootcg_workfn
  Call Trace:
   <TASK>
   __ext4_journal_start_sb+0x7a/0x470
   ext4_dirty_inode+0xa1/0x120
   __mark_inode_dirty+0x154/0x1110
   file_update_time_flags+0x3ab/0x4b0
   ext4_fallocate+0x286/0x3440
   lo_fallocate+0x13e/0x290
   loop_process_work+0x486/0xe70
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

Take freeze protection around ->fallocate() like vfs_fallocate() does.

Fixes: dfaa2ef68e80 ("loop: add discard support for loop devices")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/block/loop.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a3325ceab484a..d2f29aadabcc1 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -259,7 +259,8 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
 	if (!bdev_max_discard_sectors(lo->lo_device))
 		return -EOPNOTSUPP;
 
-	ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
+	scoped_guard(file_write, file)
+		ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
 	if (unlikely(ret && ret != -EINVAL && ret != -EOPNOTSUPP))
 		return -EIO;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 09/17] zloop: take freeze protection when recording write pointers
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (7 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 08/17] loop: take freeze protection for discard and write zeroes Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 10/17] target: fileio: take freeze protection for unmap Christian Brauner
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

With discard_write_cache a flush records the write pointer of each
active zone in an xattr of its zone file. zloop_record_safe_wps() uses
vfs_setxattr() for that which leaves freeze protection to the caller.
So a flush rewrites the xattrs on a filesystem that is frozen. With the
zone files on ext4:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#0: kworker/u16:2/46
  Workqueue: zloop0 zloop_cmd_workfn
  Call Trace:
   <TASK>
   __ext4_journal_start_sb+0x7a/0x470
   ext4_xattr_set+0x20d/0x380
   __vfs_setxattr+0x10f/0x170
   __vfs_setxattr_noperm+0xf4/0x5e0
   vfs_setxattr+0x14d/0x3d0
   zloop_cmd_workfn+0x384/0x1ff0
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

Take freeze protection around vfs_setxattr() with mnt_want_write_file()
like fsetxattr() does.

Fixes: 829def1e35ca ("zloop: forget write cache on force removal")
Cc: stable@vger.kernel.org
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/block/zloop.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 76d54c9b89c0b..d20a0d2afa855 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -719,8 +719,13 @@ static int zloop_record_safe_wps(struct zloop_device *zlo)
 
 		if (!zloop_zone_is_active(zone))
 			continue;
-		ret = vfs_setxattr(file_mnt_idmap(file), file_dentry(file),
-				"user.zloop.wp", &zone->wp, sizeof(zone->wp), 0);
+		ret = mnt_want_write_file(file);
+		if (!ret) {
+			ret = vfs_setxattr(file_mnt_idmap(file),
+					file_dentry(file), "user.zloop.wp",
+					&zone->wp, sizeof(zone->wp), 0);
+			mnt_drop_write_file(file);
+		}
 		if (ret) {
 			pr_err("%pg: failed to record write pointer (%d)\n",
 				zlo->disk->part0, ret);

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 10/17] target: fileio: take freeze protection for unmap
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (8 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 09/17] zloop: take freeze protection when recording write pointers Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 11/17] coredump: take freeze protection for the truncate Christian Brauner
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

UNMAP and WRITE SAME with UNMAP=1 end up in fd_execute_unmap() which
calls ->fallocate() of the backing file directly. That skips the freeze
protection vfs_fallocate() would have taken. So they go straight
through to a filesystem that is frozen, with and without fd_async_io.
With the backing file on ext4:

  WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#1: kworker/1:1/50
  Workqueue: target_submission target_queued_submit_work
  Call Trace:
   <TASK>
   __ext4_journal_start_sb+0x7a/0x470
   ext4_dirty_inode+0xa1/0x120
   __mark_inode_dirty+0x154/0x1110
   file_update_time_flags+0x3ab/0x4b0
   ext4_fallocate+0x286/0x3440
   fd_execute_unmap+0x1eb/0x6e0
   sbc_execute_write_same_unmap+0x93/0xe0
   __target_execute_cmd+0x53/0x190
   tcm_loop_write_pending+0xe/0x20
   transport_generic_new_cmd+0x612/0xdb0
   __target_submit+0x12c/0x380
   target_queued_submit_work+0x73/0x270
   process_one_work+0x946/0x1820
   worker_thread+0x4f1/0xd60
   kthread+0x364/0x460
   ret_from_fork+0x525/0x770
   ret_from_fork_asm+0x1a/0x30
   </TASK>

Take freeze protection around ->fallocate() like vfs_fallocate() does.

Fixes: 70d3ae5c8075 ("target/file: Add WRITE_SAME w/ UNMAP=1 emulation support")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/target/target_core_file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/target/target_core_file.c b/drivers/target/target_core_file.c
index e8a44ddf6f5c9..5600ef7bbdbb6 100644
--- a/drivers/target/target_core_file.c
+++ b/drivers/target/target_core_file.c
@@ -584,7 +584,8 @@ fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
 		if (!file->f_op->fallocate)
 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
 
-		ret = file->f_op->fallocate(file, mode, pos, len);
+		scoped_guard(file_write, file)
+			ret = file->f_op->fallocate(file, mode, pos, len);
 		if (ret < 0) {
 			pr_warn("FILEIO: fallocate() failed: %d\n", ret);
 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 11/17] coredump: take freeze protection for the truncate
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (9 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 10/17] target: fileio: take freeze protection for unmap Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 12/17] ovl: take freeze protection when storing the uuid Christian Brauner
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

coredump_file() truncates the core file after filp_open() returned.
The open took freeze protection for the create but dropped it again
before returning and coredump_write() only takes it for the dump
itself. So a freeze that lands in between lets the truncate modify a
frozen filesystem.

Take freeze protection around the truncate like do_ftruncate() does.

Fixes: 14da9200140f ("fs: Protect write paths by sb_start_write - sb_end_write")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/coredump.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/coredump.c b/fs/coredump.c
index 82ef9d115999a..cc6a0c7e48ce2 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -1060,8 +1060,10 @@ static bool coredump_file(struct core_name *cn, struct coredump_params *cprm,
 	}
 	if (!(file->f_mode & FMODE_CAN_WRITE))
 		return false;
-	if (do_truncate(idmap, file->f_path.dentry, 0, 0, file))
-		return false;
+	scoped_guard(super_write, inode->i_sb) {
+		if (do_truncate(idmap, file->f_path.dentry, 0, 0, file))
+			return false;
+	}
 
 	cprm->file = no_free_ptr(file);
 	return true;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 12/17] ovl: take freeze protection when storing the uuid
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (10 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 11/17] coredump: take freeze protection for the truncate Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 13/17] bpf: fs/xattr: take freeze protection in the xattr kfuncs Christian Brauner
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable, Amir Goldstein

ovl_init_uuid_xattr() stores the persistent uuid in an xattr on the
upper root. That happens after ovl_make_workdir() dropped its write
access to the upper mount and before ovl_get_indexdir() takes it
again. So a freeze of the upper filesystem that lands in between lets
the mount modify a frozen filesystem.

Take write access to the upper mount around the xattr update.

Fixes: d9544c1b0d9e ("ovl: store persistent uuid/fsid with uuid=on")
Cc: stable@vger.kernel.org
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/overlayfs/util.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/fs/overlayfs/util.c b/fs/overlayfs/util.c
index 521717209b2e4..67aa7b71e9c83 100644
--- a/fs/overlayfs/util.c
+++ b/fs/overlayfs/util.c
@@ -829,8 +829,12 @@ bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
 
 	/* Try to store persistent uuid */
 	set = true;
-	res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, uuid.b,
-			   UUID_SIZE);
+	res = mnt_want_write(ovl_upper_mnt(ofs));
+	if (!res) {
+		res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID,
+				   uuid.b, UUID_SIZE);
+		mnt_drop_write(ovl_upper_mnt(ofs));
+	}
 	if (res)
 		goto fail;
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 13/17] bpf: fs/xattr: take freeze protection in the xattr kfuncs
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (11 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 12/17] ovl: take freeze protection when storing the uuid Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 14/17] nfsd: take freeze protection for the truncate on open Christian Brauner
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

bpf_set_dentry_xattr() and bpf_remove_dentry_xattr() go through
__vfs_setxattr() and __vfs_removexattr() which leave freeze protection
to the caller. BPF LSM programs call them from sleepable hooks such as
file_open where nobody holds it. So a program can modify a frozen
filesystem.

The hook may already hold freeze protection or locks a frozen
filesystem would make us wait under. So don't wait for a thaw. Fail
with -EBUSY if the filesystem is frozen.

The _locked variants are only used from hooks that run inside setattr,
setxattr, removexattr, unlink and rmdir. Their callers hold freeze
protection already.

Fixes: 56467292794b ("bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/bpf_fs_kfuncs.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c
index 35ccd98a4de64..7f61e2efa08ba 100644
--- a/fs/bpf_fs_kfuncs.c
+++ b/fs/bpf_fs_kfuncs.c
@@ -295,9 +295,13 @@ __bpf_kfunc int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__st
 	if (!inode)
 		return -EINVAL;
 
+	/* The hook may hold locks or freeze protection already. */
+	if (!sb_start_write_trylock(inode->i_sb))
+		return -EBUSY;
 	inode_lock(inode);
 	ret = bpf_set_dentry_xattr_locked(dentry, name__str, value_p, flags);
 	inode_unlock(inode);
+	sb_end_write(inode->i_sb);
 	return ret;
 }
 
@@ -323,9 +327,13 @@ __bpf_kfunc int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name_
 	if (!inode)
 		return -EINVAL;
 
+	/* The hook may hold locks or freeze protection already. */
+	if (!sb_start_write_trylock(inode->i_sb))
+		return -EBUSY;
 	inode_lock(inode);
 	ret = bpf_remove_dentry_xattr_locked(dentry, name__str);
 	inode_unlock(inode);
+	sb_end_write(inode->i_sb);
 	return ret;
 }
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 14/17] nfsd: take freeze protection for the truncate on open
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (12 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 13/17] bpf: fs/xattr: take freeze protection in the xattr kfuncs Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 15/17] loop: use kiocb_write_iter() Christian Brauner
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable), stable

An OPEN that truncates an existing file ends up in nfsd4_truncate()
which calls nfsd_setattr() on the already verified file handle. For a
verified file handle nfsd_setattr() leaves fh_want_write() to the
caller but nfsd4_truncate() never takes it, and do_lookup_open() has
dropped its own write access by then. So a freeze of the exported
filesystem that lands in between lets the truncate modify a frozen
filesystem.

Take write access around nfsd_setattr() like nfsd4_setattr() does.

Fixes: 9f67f189939e ("nfsd: notify_change needs elevated write count")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/nfsd/nfs4state.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 1de6c6d757c3e..e561903b9180f 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -6681,11 +6681,19 @@ nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
 	struct nfsd_attrs attrs = {
 		.na_iattr	= &iattr,
 	};
+	__be32 status;
+	int err;
+
 	if (!open->op_truncate)
 		return 0;
 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
 		return nfserr_inval;
-	return nfsd_setattr(rqstp, fh, &attrs, NULL);
+	err = fh_want_write(fh);
+	if (err)
+		return nfserrno(err);
+	status = nfsd_setattr(rqstp, fh, &attrs, NULL);
+	fh_drop_write(fh);
+	return status;
 }
 
 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 15/17] loop: use kiocb_write_iter()
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (13 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 14/17] nfsd: take freeze protection for the truncate on open Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 16/17] fs: use kiocb_write_iter() in vfs_iocb_iter_write() Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 17/17] backing-file: use kiocb_end_write_iter() Christian Brauner
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable)

Use the new helpers instead of open-coding freeze protection for writes
to the backing file.

No functional changes.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 drivers/block/loop.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index d2f29aadabcc1..872d78006b131 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -325,8 +325,7 @@ static void lo_rw_aio_do_completion(struct loop_cmd *cmd)
 		return;
 	kfree(cmd->bvec);
 	cmd->bvec = NULL;
-	if (req_op(rq) == REQ_OP_WRITE)
-		kiocb_end_write(&cmd->iocb);
+	kiocb_end_write_iter(&cmd->iocb);
 	if (likely(!blk_should_fake_timeout(rq->q)))
 		blk_mq_complete_request(rq);
 }
@@ -395,10 +394,9 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 		cmd->iocb.ki_flags = 0;
 	}
 
-	if (rw == ITER_SOURCE) {
-		kiocb_start_write(&cmd->iocb);
-		ret = file->f_op->write_iter(&cmd->iocb, &iter);
-	} else
+	if (rw == ITER_SOURCE)
+		ret = kiocb_write_iter(&cmd->iocb, &iter);
+	else
 		ret = file->f_op->read_iter(&cmd->iocb, &iter);
 
 	lo_rw_aio_do_completion(cmd);

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 16/17] fs: use kiocb_write_iter() in vfs_iocb_iter_write()
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (14 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 15/17] loop: use kiocb_write_iter() Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  2026-11-16 11:03 ` [PATCH v2 17/17] backing-file: use kiocb_end_write_iter() Christian Brauner
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable)

vfs_iocb_iter_write() open-codes what kiocb_write_iter() does. Use it.
The kiocb is marked with IOCB_WRITE now so the caller can drop freeze
protection with kiocb_end_write_iter().

This also means that an IOCB_NOWAIT write fails with -EAGAIN instead of
waiting for a frozen filesystem. No caller passes IOCB_NOWAIT today.
overlayfs and fuse passthrough don't set FMODE_NOWAIT and cachefiles
and nfsd don't ask for it.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/read_write.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index 6767283108991..eca0aa73e5a4d 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -923,7 +923,7 @@ ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
 EXPORT_SYMBOL(vfs_iter_read);
 
 /*
- * Caller is responsible for calling kiocb_end_write() on completion
+ * Caller is responsible for calling kiocb_end_write_iter() on completion
  * if async iocb was queued.
  */
 ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
@@ -946,10 +946,7 @@ ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
 	if (ret < 0)
 		return ret;
 
-	kiocb_start_write(iocb);
-	ret = file->f_op->write_iter(iocb, iter);
-	if (ret != -EIOCBQUEUED)
-		kiocb_end_write(iocb);
+	ret = kiocb_write_iter(iocb, iter);
 	if (ret > 0)
 		fsnotify_modify(file);
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH v2 17/17] backing-file: use kiocb_end_write_iter()
  2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
                   ` (15 preceding siblings ...)
  2026-11-16 11:03 ` [PATCH v2 16/17] fs: use kiocb_write_iter() in vfs_iocb_iter_write() Christian Brauner
@ 2026-11-16 11:03 ` Christian Brauner
  16 siblings, 0 replies; 19+ messages in thread
From: Christian Brauner @ 2026-11-16 11:03 UTC (permalink / raw)
  To: Jan Kara
  Cc: Shakeel Butt, Alexander Viro, Jens Axboe, linux-fsdevel,
	linux-kernel, io-uring, Christoph Hellwig, linux-block,
	linux-nvme, linux-scsi, target-devel, linux-unionfs, bpf,
	linux-nfs, Christian Brauner (Amutable)

vfs_iocb_iter_write() marks the kiocb with IOCB_WRITE now. Drop freeze
protection with kiocb_end_write_iter() in the completion callback and
get rid of aio->is_write.

No functional changes.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/backing-file.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/fs/backing-file.c b/fs/backing-file.c
index 5780f60662d07..e86f5685758eb 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -106,8 +106,6 @@ struct backing_aio {
 	struct kiocb iocb;
 	refcount_t ref;
 	struct kiocb *orig_iocb;
-	/* used for aio completion */
-	bool is_write;
 	void (*end_write)(struct kiocb *iocb, ssize_t);
 	struct work_struct work;
 	long res;
@@ -148,9 +146,8 @@ static void backing_aio_rw_complete(struct kiocb *iocb, long res)
 	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
 	struct kiocb *orig_iocb = aio->orig_iocb;
 
-	/* Pairs with kiocb_start_write() in vfs_iocb_iter_write() */
-	if (aio->is_write)
-		kiocb_end_write(iocb);
+	/* Pairs with kiocb_write_iter() in vfs_iocb_iter_write() */
+	kiocb_end_write_iter(iocb);
 
 	backing_aio_cleanup(aio, res);
 	orig_iocb->ki_complete(orig_iocb, res);
@@ -264,7 +261,6 @@ static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
 		return -ENOMEM;
 
 	aio->orig_iocb = iocb;
-	aio->is_write = true;
 	aio->end_write = end_write;
 	kiocb_clone(&aio->iocb, iocb, get_file(file));
 	aio->iocb.ki_flags = flags;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter()
  2026-11-16 11:03 ` [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter() Christian Brauner
@ 2026-11-16 11:30   ` sashiko-bot
  0 siblings, 0 replies; 19+ messages in thread
From: sashiko-bot @ 2026-11-16 11:30 UTC (permalink / raw)
  To: Christian Brauner
  Cc: linux-nvme, linux-scsi, target-devel, bpf, Chuck Lever,
	Jeff Layton, Anna Schumaker

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] fs: unconditional freeze protection on non-regular files in
  kiocb_write_iter()
--

commit c4bb9dc6398af3c2e2efc447c0b1b29d081b55ab
Author: Christian Brauner <brauner@kernel.org>

fs: add kiocb_write_iter() and kiocb_end_write_iter()

This commit adds kiocb_write_iter() and kiocb_end_write_iter() helpers to
manage superblock freeze protection across ->write_iter() calls.

> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index eefc2c9d4af19..0b66a09fb686a 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -2840,6 +2840,54 @@ static inline void kiocb_end_write(struct kiocb *iocb)
[ ... ]
> +static inline ssize_t kiocb_write_iter(struct kiocb *iocb,
> +				       struct iov_iter *iter)
> +{
> +	ssize_t ret;
> +
> +	if (iocb->ki_flags & IOCB_NOWAIT) {
> +		if (!kiocb_start_write_trylock(iocb))
> +			return -EAGAIN;
> +	} else {
> +		kiocb_start_write(iocb);
> +	}
> +	iocb->ki_flags |= IOCB_WRITE;

[Severity: Medium]
Should kiocb_write_iter() check S_ISREG(file_inode(iocb->ki_filp)->i_mode)
before taking freeze protection and setting IOCB_WRITE, the way
file_start_write(), aio_write() and io_kiocb_start_write() do?

Callers such as loop and target_core_file accept S_ISBLK backing files.
Opening a block device node leaves file_inode(file)->i_sb pointing at the
filesystem hosting the device node rather than the block device.  Without
an S_ISREG() check, writes to a block device node whose hosting filesystem
is frozen will block in sb_start_write() (or fail with -EAGAIN for
IOCB_NOWAIT), and will deadlock if that filesystem is mounted on or
flushing through the virtual block device during freeze_super().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116-work-fs-frozen_writes-v2-0-ecc4a2aac40e@kernel.org?part=2

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2026-11-16 11:31 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 11:03 [PATCH v2 00/17] fs: take freeze protection for writes to backing files Christian Brauner
2026-11-16 11:03 ` [PATCH v2 01/17] fs: add kiocb_start_write_trylock() Christian Brauner
2026-11-16 11:03 ` [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter() Christian Brauner
2026-11-16 11:30   ` sashiko-bot
2026-11-16 11:03 ` [PATCH v2 03/17] fs: add file_write guard Christian Brauner
2026-11-16 11:03 ` [PATCH v2 04/17] zloop: take freeze protection for writes Christian Brauner
2026-11-16 11:03 ` [PATCH v2 05/17] nvmet: take freeze protection for writes to files Christian Brauner
2026-11-16 11:03 ` [PATCH v2 06/17] target: fileio: don't free the aio command under the filesystem Christian Brauner
2026-11-16 11:03 ` [PATCH v2 07/17] target: fileio: take freeze protection for aio writes Christian Brauner
2026-11-16 11:03 ` [PATCH v2 08/17] loop: take freeze protection for discard and write zeroes Christian Brauner
2026-11-16 11:03 ` [PATCH v2 09/17] zloop: take freeze protection when recording write pointers Christian Brauner
2026-11-16 11:03 ` [PATCH v2 10/17] target: fileio: take freeze protection for unmap Christian Brauner
2026-11-16 11:03 ` [PATCH v2 11/17] coredump: take freeze protection for the truncate Christian Brauner
2026-11-16 11:03 ` [PATCH v2 12/17] ovl: take freeze protection when storing the uuid Christian Brauner
2026-11-16 11:03 ` [PATCH v2 13/17] bpf: fs/xattr: take freeze protection in the xattr kfuncs Christian Brauner
2026-11-16 11:03 ` [PATCH v2 14/17] nfsd: take freeze protection for the truncate on open Christian Brauner
2026-11-16 11:03 ` [PATCH v2 15/17] loop: use kiocb_write_iter() Christian Brauner
2026-11-16 11:03 ` [PATCH v2 16/17] fs: use kiocb_write_iter() in vfs_iocb_iter_write() Christian Brauner
2026-11-16 11:03 ` [PATCH v2 17/17] backing-file: use kiocb_end_write_iter() Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox