nfs/localio: direct I/O drops freeze protection early and frees the iocb under the filesystem

From: Christian Brauner

Date: Sat Nov 14 2026 - 04:41:01 EST


Hey,

I'm working on some freeze protection bugs across the tree. This one I
don't just want to fix myself. I'm not familiar enough with this at all.
I trust you can fix this.

localio's direct I/O has two problems. Both reproduced on v7.4-rc1 with
an ext4 export mounted over localio from 127.0.0.1.

(1) nfs_local_call_write() calls file_end_write() once it's done looping
over the iters. But the last DIO-aligned iter is submitted with
->ki_complete set and returns -EIOCBQUEUED. So freeze protection is
dropped while that write is still in flight. A freeze completes
right away and the completion of the write modifies the frozen
filesystem.

A direct write into a fallocated file followed by fsfreeze -f on the
export: fsfreeze returns after 0.1s and then

WARNING: fs/ext4/ext4_jbd2.c:90 at ext4_journal_check_start+0x1f6/0x2d0, CPU#1: kworker/1:2/386
Call Trace:
__ext4_journal_start_sb+0x7a/0x470
ext4_convert_unwritten_extents+0x3e6/0x510
ext4_dio_write_end_io+0x38a/0x600
iomap_dio_complete+0x140/0xb20
iomap_dio_complete_work+0x58/0x90
process_one_work+0x946/0x1820
worker_thread+0x4f1/0xd60
kthread+0x364/0x460

A queued write has to keep freeze protection until it completes, the
way aio, io_uring, loop and vfs_iocb_iter_write() do it:
kiocb_start_write() before submitting, kiocb_end_write() in the
completion for a queued write and right away otherwise.

(2) nfs_local_call_read() doesn't hold a reference to the iocb across
->read_iter(). For a queued direct read the completion frees the
iocb from nfsiod while the filesystem may still be in ->read_iter(),
and ext4_dio_read_iter() still touches iocb->ki_filp after
iomap_dio_rw() returned -EIOCBQUEUED:

BUG: KASAN: slab-use-after-free in ext4_dio_read_iter+0x5f7/0x6b0
Read of size 8 at addr ff11000135598400 by task dd/431
Call Trace:
kasan_report+0x108/0x130
ext4_dio_read_iter+0x5f7/0x6b0
nfs_local_call_read+0x1fb/0x690
nfs_local_doio+0x86e/0xc90
nfs_initiate_pgio+0x311/0x4f0
nfs_generic_pg_pgios+0x3e4/0x6f0
nfs_pageio_doio+0x108/0x2a0
nfs_pageio_complete+0x1ba/0x5c0
nfs_direct_read_schedule_iovec+0x3ec/0x600
nfs_file_direct_read+0x3f7/0x700
vfs_read+0x6f8/0xb40
ksys_read+0x116/0x250

Allocated by task 431:
__kmalloc_cache_noprof+0x1db/0x630
nfs_local_doio+0x1e7/0xc90
nfs_initiate_pgio+0x311/0x4f0

Freed by task 44:
kfree+0x267/0x6a0
nfs_local_iocb_release+0x1b6/0x2d0
nfs_local_pgio_release+0x35b/0x4f0
process_one_work+0x946/0x1820

ext4 is getting fixed not to do that (cf. [1]) but localio should hold a
reference across the submission like aio, io_uring, loop and
fs/backing-file.c do.

Both windows are small. I widened them with these test-only delays in
ext4:

--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -94,6 +95,8 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
ret = iomap_dio_read_simple(iocb, to, ext4_iomap_begin);
if (ret == -ENOTBLK)
ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL, 0, NULL, 0);
+ if (ret == -EIOCBQUEUED)
+ msleep(50);
inode_unlock_shared(inode);

file_accessed(iocb->ki_filp);
@@ -450,6 +453,8 @@ static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
loff_t pos = iocb->ki_pos;
struct inode *inode = file_inode(iocb->ki_filp);

+ msleep(3000);
+
if (!error && size && (flags & IOMAP_DIO_UNWRITTEN) &&
(iocb->ki_flags & IOCB_ATOMIC))

Both go back to 3feec68563dd ("nfs/localio: add direct IO enablement
with sync and async IO support").

[1]: https://lore.kernel.org/r/20261113-work-ext4-dio_read_kiocb-v1-1-172a88be803c@xxxxxxxxxx