From: Christian Brauner <brauner@kernel.org>
To: Jan Kara <jack@suse.cz>
Cc: Shakeel Butt <shakeel.butt@linux.dev>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Jens Axboe <axboe@kernel.dk>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
io-uring@vger.kernel.org, Christoph Hellwig <hch@lst.de>,
linux-block@vger.kernel.org, linux-nvme@lists.infradead.org,
linux-scsi@vger.kernel.org, target-devel@vger.kernel.org,
linux-unionfs@vger.kernel.org, bpf@vger.kernel.org,
linux-nfs@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH 04/12] target: fileio: don't free the aio command under the filesystem
Date: Sun, 15 Nov 2026 10:30:22 +0100 [thread overview]
Message-ID: <20261115-rasch-haltbar-gebrechen-faac5fad7805@brauner> (raw)
In-Reply-To: <20261114-work-fs-frozen_writes-v1-4-f5ae61c898b1@kernel.org>
On Sat, Nov 14, 2026 at 11:47:11AM +0100, Christian Brauner wrote:
> 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.
As sashiko pointed out (cf. [1]) the completion callback still completes
the command. That drops the LUN reference while ->read_iter() or
->write_iter() may still be running. The device and its backing file
can go away underneath the filesystem. So we must complete the command
on the final put as well. I've folded:
5/12 gets simpler on top of this. kiocb_end_write() just moves into
cmd_rw_aio_complete() which is now called exactly once for every I/O.
I've folded both.
[1]: https://lore.kernel.org/r/sashiko-outbox-214401@kernel.org
-- >8 --
Subject: [PATCH] target: fileio: don't free the aio command under the filesystem
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;
}
--
next prev parent reply other threads:[~2026-11-15 9:30 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-14 10:47 [PATCH 00/12] fs: take freeze protection for writes to backing files Christian Brauner
2026-11-14 10:47 ` [PATCH 01/12] fs: add kiocb_start_write_trylock() Christian Brauner
2026-11-16 11:57 ` Jan Kara
2026-11-14 10:47 ` [PATCH 02/12] zloop: take freeze protection for writes Christian Brauner
2026-11-16 6:46 ` Christoph Hellwig
2026-11-14 10:47 ` [PATCH 03/12] nvmet: take freeze protection for writes to files Christian Brauner
2026-11-14 10:47 ` [PATCH 04/12] target: fileio: don't free the aio command under the filesystem Christian Brauner
2026-11-15 9:30 ` Christian Brauner [this message]
2026-11-14 10:47 ` [PATCH 05/12] target: fileio: take freeze protection for aio writes Christian Brauner
2026-11-14 10:47 ` [PATCH 06/12] loop: take freeze protection for discard and write zeroes Christian Brauner
2026-11-16 11:59 ` Jan Kara
2026-11-14 10:47 ` [PATCH 07/12] zloop: take freeze protection when recording write pointers Christian Brauner
2026-11-16 6:48 ` Christoph Hellwig
2026-11-16 9:41 ` Christian Brauner
2026-11-16 11:57 ` Jan Kara
2026-11-20 15:44 ` Christoph Hellwig
2026-11-14 10:47 ` [PATCH 08/12] target: fileio: take freeze protection for unmap Christian Brauner
2026-11-14 10:47 ` [PATCH 09/12] coredump: take freeze protection for the truncate Christian Brauner
2026-11-14 10:47 ` [PATCH 10/12] ovl: take freeze protection when storing the uuid Christian Brauner
2026-11-14 12:19 ` Amir Goldstein
2026-11-14 10:47 ` [PATCH 11/12] bpf: fs/xattr: take freeze protection in the xattr kfuncs Christian Brauner
2026-11-14 10:47 ` [PATCH 12/12] nfsd: take freeze protection for the truncate on open Christian Brauner
2026-11-14 12:31 ` [PATCH 00/12] fs: take freeze protection for writes to backing files Amir Goldstein
2026-11-14 13:04 ` Christian Brauner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261115-rasch-haltbar-gebrechen-faac5fad7805@brauner \
--to=brauner@kernel.org \
--cc=axboe@kernel.dk \
--cc=bpf@vger.kernel.org \
--cc=hch@lst.de \
--cc=io-uring@vger.kernel.org \
--cc=jack@suse.cz \
--cc=linux-block@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=linux-scsi@vger.kernel.org \
--cc=linux-unionfs@vger.kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=stable@vger.kernel.org \
--cc=target-devel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®