fs: take freeze protection for writes to backing files
Subsystems (14)
Christian Brauner
maintainerv2 cover fs: take freeze protection for writes to backing files
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/[email protected]
[2]: https://lore.kernel.org/r/[email protected]
---
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/[email protected]
---
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
Christian Brauner
maintainerPATCH v2 1/17 fs: add kiocb_start_write_trylock()
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.
--- include/linux/fs.h | 22 ++++++++++++++++++++++ io_uring/rw.c | 9 +-------- 2 files changed, 23 insertions(+), 8 deletions(-)
include/linux/fs.h +22 -0
@@ -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
io_uring/rw.c +1 -8
@@ -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)
Signature · 1 line
2.53.0
Re: [PATCH v2 01/17] fs: add kiocb_start_write_trylock()
Quoted 11 lines · Christian Brauner
kiocb_start_write()waits until a frozen filesystem is thawed. A nonblocking submission can't use it.io_uringopen-codes the trylock variant for itsIOCB_NOWAITwrites.Add
kiocb_start_write_trylock()next tokiocb_start_write()and use it inio_uring.kiocb_write_iter()is going to need it as well.No functional changes.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 69 lines
--- include/linux/fs.h | 22 ++++++++++++++++++++++ io_uring/rw.c | 9 +-------- 2 files changed, 23 insertions(+), 8 deletions(-)include/linux/fs.h +22
-0@@ -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 io_uring/rw.c +1
-8@@ -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)Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH v2 2/17 fs: add kiocb_write_iter() and kiocb_end_write_iter()
In reply to Christian BraunerCalling ->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/[email protected]
--- include/linux/fs.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
include/linux/fs.h +48 -0
@@ -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
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter()
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() --
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 <[email protected]>
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.
Quoted 5 lines
include/linux/fs.h +0
-0@@ -2840,6 +2840,54 @@ static inline void kiocb_end_write(struct kiocb *iocb)
[ ... ]
include/linux/fs.h static inline void kiocb_end_write(struct kiocb *iocb) · line 2884
Quoted 12 lines
include/linux/fs.h +12
-0+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().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Christian Brauner
maintainerRe: [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter()
Quoted 3 lines · On Mon, Nov 16, 2026 at 11:30:59AM +0000, [email protected] wrote:
Should
kiocb_write_iter()check S_ISREG(file_inode(iocb->ki_filp)->i_mode) before taking freeze protection and settingIOCB_WRITE, the wayfile_start_write(),aio_write()andio_kiocb_start_write()do?
Eeew, no. That check alone would bring the bug back for these drivers.
blkdev_write_iter() calls file_update_time() which dirties the device node's inode on the filesystem the node lives on. And write(2) on such a node modifies a frozen filesystem today for the same reason.
For FIFOs we skip the time update on a frozen filesystem instead of blocking (cf. 7e775f46a125f ("fs/pipe.c: skip file_update_time on frozen fs")). If we change file_update_time() to do that for all special files then kiocb_write_iter() can take the S_ISREG() check.
Quoted 4 lines
Without an
S_ISREG()check, writes to a block device node whose hosting filesystem is frozen will block insb_start_write()(or fail with-EAGAINforIOCB_NOWAIT), and will deadlock if that filesystem is mounted on or flushing through the virtual block device duringfreeze_super().
That can't happen. The node is opened when the device is set up so its filesystem can't be mounted on that device.
Re: [PATCH v2 02/17] fs: add kiocb_write_iter() and kiocb_end_write_iter()
In reply to Christian BraunerQuoted 19 lines · Christian Brauner
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 withIOCB_WRITElike aio,io_uringand cachefiles do and calls ->write_iter(). If the write wasn't queued freeze protection is dropped right away. WithIOCB_NOWAITit only tries to get freeze protection and fails with-EAGAINinstead of waiting for the thaw.
kiocb_end_write_iter()drops freeze protection if the kiocb still holds it and clearsIOCB_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.
Quoted 5 lines
[1]: https://lore.kernel.org/r/[email protected]
Suggested-by Christoph Hellwig <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 67 lines
--- include/linux/fs.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)include/linux/fs.h +48
-0@@ -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 Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH v2 3/17 fs: add file_write guard
In reply to Christian BraunerAdd a guard for file_start_write() and file_end_write().
--- include/linux/fs.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/fs.h +5 -0
@@ -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
Signature · 1 line
2.53.0
Re: [PATCH v2 03/17] fs: add file_write guard
Quoted 4 lines · Christian Brauner
Add a guard for
file_start_write()andfile_end_write().Signed-off-by Christian Brauner (Amutable) <[email protected]>
OK. Feel free to add:
Honza
Quoted 24 lines
--- include/linux/fs.h | 5 +++++ 1 file changed, 5 insertions(+)include/linux/fs.h +5
-0@@ -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 Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH v2 4/17 zloop: take freeze protection for writes
In reply to Christian Braunerzloop 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.
--- drivers/block/zloop.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/zloop.c +2 -1
@@ -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); }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 5/17 nvmet: take freeze protection for writes to files
In reply to Christian BraunerThe 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.
--- drivers/nvme/target/io-cmd-file.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/nvme/target/io-cmd-file.c +13 -7
@@ -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);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 6/17 target: fileio: don't free the aio command under the filesystem
In reply to Christian BraunerWith 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.
--- drivers/target/target_core_file.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
drivers/target/target_core_file.c +19 -6
@@ -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 ; }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 7/17 target: fileio: take freeze protection for aio writes
In reply to Christian BraunerWith 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.
--- drivers/target/target_core_file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/target/target_core_file.c +2 -1
@@ -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);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 8/17 loop: take freeze protection for discard and write zeroes
In reply to Christian BraunerDiscard 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.
--- drivers/block/loop.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/loop.c +2 -1
@@ -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 ;
Signature · 1 line
2.53.0
Re: [PATCH v2 08/17] loop: take freeze protection for discard and write zeroes
Quoted 23 lines · Christian Brauner
Discard and write zeroes requests end up in
lo_fallocate()which calls ->fallocate() of the backing file directly. That skips the freeze protectionvfs_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>
Quoted 5 lines
Take freeze protection around ->fallocate() like
vfs_fallocate()does.Fixes dfaa2ef68e80 ("loop: add discard support for loop devices")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 22 lines
--- drivers/block/loop.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/block/loop.c +2
-1@@ -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 ;Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH v2 9/17 zloop: take freeze protection when recording write pointers
In reply to Christian BraunerWith 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.
--- drivers/block/zloop.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/block/zloop.c +7 -2
@@ -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);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 10/17 target: fileio: take freeze protection for unmap
In reply to Christian BraunerUNMAP 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.
--- drivers/target/target_core_file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/target/target_core_file.c +2 -1
@@ -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 ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 11/17 coredump: take freeze protection for the truncate
In reply to Christian Braunercoredump_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.
--- fs/coredump.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/coredump.c +4 -2
@@ -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;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 12/17 ovl: take freeze protection when storing the uuid
In reply to Christian Braunerovl_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.
--- fs/overlayfs/util.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/overlayfs/util.c +6 -2
@@ -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;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 13/17 bpf: fs/xattr: take freeze protection in the xattr kfuncs
In reply to Christian Braunerbpf_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.
--- fs/bpf_fs_kfuncs.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/bpf_fs_kfuncs.c +8 -0
@@ -295,9 +295,13 @@ __bpf_kfunc int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__stif (!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; }
Signature · 1 line
2.53.0
Re: [PATCH v2 13/17] bpf: fs/xattr: take freeze protection in the xattr kfuncs
Quoted 15 lines · Christian Brauner
bpf_set_dentry_xattr()andbpf_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 asfile_openwhere 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
-EBUSYif the filesystem is frozen.The
_lockedvariants are only used from hooks that run inside setattr, setxattr, removexattr, unlink and rmdir. Their callers hold freeze protection already.
Quoted 7 lines
Fixes 56467292794b ("bpf: fs/xattr: Add BPF kfuncs to set and remove xattrs")Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/bpf_fs_kfuncs.c | 8 ++++++++ 1 file changed, 8 insertions(+)
LGTM
Quoted 37 lines
fs/bpf_fs_kfuncs.c +8
-1@@ -295,9 +295,13 @@ __bpf_kfunc int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__stif (!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
Christian Brauner
maintainerPATCH v2 14/17 nfsd: take freeze protection for the truncate on open
In reply to Christian BraunerAn 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.
--- fs/nfsd/nfs4state.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +9 -1
@@ -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,
Signature · 1 line
2.53.0
Re: [PATCH v2 14/17] nfsd: take freeze protection for the truncate on open
Quoted 32 lines · Christian Brauner
An OPEN that truncates an existing file ends up in
nfsd4_truncate()which callsnfsd_setattr()on the already verified file handle. For a verified file handlenfsd_setattr()leavesfh_want_write()to the caller butnfsd4_truncate()never takes it, anddo_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()likenfsd4_setattr()does.Fixes 9f67f189939e ("nfsd: notify_change needs elevated write count")Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/nfsd/nfs4state.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)fs/nfsd/nfs4state.c +3
-0@@ -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;
fs/nfsd/nfs4state.c nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, · line 6699
Quoted 10 lines
fs/nfsd/nfs4state.c +6
-1-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,
Christian Brauner
maintainerPATCH v2 15/17 loop: use kiocb_write_iter()
In reply to Christian BraunerUse the new helpers instead of open-coding freeze protection for writes to the backing file.
No functional changes.
--- drivers/block/loop.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/block/loop.c +4 -6
@@ -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);
Signature · 1 line
2.53.0
Re: [PATCH v2 15/17] loop: use kiocb_write_iter()
Quoted 6 lines · Christian Brauner
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) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 36 lines
--- drivers/block/loop.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)drivers/block/loop.c +4
-6@@ -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);Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH v2 16/17 fs: use kiocb_write_iter() in vfs_iocb_iter_write()
In reply to Christian Braunervfs_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.
--- fs/read_write.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
fs/read_write.c +2 -5
@@ -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);
Signature · 1 line
2.53.0
Re: [PATCH v2 16/17] fs: use kiocb_write_iter() in vfs_iocb_iter_write()
Quoted 10 lines · Christian Brauner
vfs_iocb_iter_write()open-codes whatkiocb_write_iter()does. Use it. The kiocb is marked withIOCB_WRITEnow so the caller can drop freeze protection withkiocb_end_write_iter().This also means that an
IOCB_NOWAITwrite fails with-EAGAINinstead of waiting for a frozen filesystem. No caller passesIOCB_NOWAITtoday. overlayfs and fuse passthrough don't setFMODE_NOWAITand cachefiles and nfsd don't ask for it.Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 33 lines
--- fs/read_write.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)fs/read_write.c +2
-5@@ -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);Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH v2 17/17 backing-file: use kiocb_end_write_iter()
In reply to Christian Braunervfs_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.
--- fs/backing-file.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
fs/backing-file.c +2 -6
@@ -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;
Signature · 1 line
2.53.0