From: Chao Yu <chao@kernel.org>
To: axboe@kernel.dk
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
Chao Yu <chao@kernel.org>
Subject: [PATCH] block: prevent reordering of PF_BLOCK_TS clear and plug NULLing
Date: Mon, 16 Nov 2026 00:07:23 +0000 [thread overview]
Message-ID: <20261116000723.934826-1-chao@kernel.org> (raw)
From: Chao Yu <chao@kernel.org>
We hit below kernel panic while running fsstress on f2fs on an arm64
Android device:
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000020
Internal error: Oops: 0000000096000046 [#1] PREEMPT SMP
CPU: 1 UID: 0 PID: 9196 Comm: fsstress Tainted: G S W O 6.12.96-android16-6-maybe-dirty-4k #1
pc : __schedule+0xeb8/0x1de4
lr : __schedule+0x7b4/0x1de4
Call trace:
__schedule+0xeb8/0x1de4
preempt_schedule_irq+0x48/0xa8
arm64_preempt_schedule_irq+0x30/0x48
el1_interrupt+0x48/0x8c
el1h_64_irq_handler+0x18/0x24
el1h_64_irq+0x7c/0x80
blk_finish_plug+0x70/0x18c
f2fs_write_data_pages+0x1bc/0x14f4
do_writepages+0x98/0x1b0
filemap_fdatawrite_wbc+0x60/0x9c
filemap_write_and_wait_range+0x7c/0x210
f2fs_flush_buffered_write+0x24/0x50
f2fs_file_write_iter+0x650/0x798
ksys_write+0x2d8/0x500
__arm64_sys_write+0x1c/0x2c
invoke_syscall+0x58/0xe8
do_el0_svc+0x80/0xb0
el0_svc+0x4c/0xd8
el0t_64_sync_handler+0x94/0x10c
el0t_64_sync+0x1bc/0x1c0
Code: b4ffa688 97c33675 17fffd32 f9454e68 (f900111f)
Kernel panic - not syncing: Oops: Fatal exception
The faulting instructions are in blk_plug_invalidate_ts(), which is
inlined into finish_task_switch():
ldr x8, [x19, #0xa98] // x8 = current->plug, which is NULL
str xzr, [x8, #0x20] // current->plug->cur_ktime = 0
blk_plug_invalidate_ts() assumes current->plug is valid as long as
PF_BLOCK_TS is set. However, in blk_finish_plug(), __blk_flush_plug() is
inlined, and the compiler is free to reorder the store that clears
PF_BLOCK_TS after the store that sets current->plug to NULL, since
nothing tells it there is a dependency between them:
str xzr, [x19, #0x20] // plug->cur_ktime = 0
ldr w8, [x20, #0x44] // w8 = current->flags
str xzr, [x20, #0xa98] // current->plug = NULL
and w8, w8, #0xdfffffff // w8 &= ~PF_BLOCK_TS
str w8, [x20, #0x44] // current->flags = w8 <- blk_finish_plug+0x70
The race is as below:
- blk_finish_plug
- __blk_flush_plug
- plug->cur_ktime = 0
- current->plug = NULL
: IRQ arrives at blk_finish_plug+0x70,
: PF_BLOCK_TS is still set
- preempt_schedule_irq
- __schedule
- finish_task_switch
- blk_plug_invalidate_ts
: current->flags & PF_BLOCK_TS
: current->plug->cur_ktime = 0
: NULL pointer dereference
- current->flags &= ~PF_BLOCK_TS
Commit 691b052139c9 ("block: fix race in blk_time_get_ns() returning 0")
already added a barrier() on the side that sets PF_BLOCK_TS, but the
side that clears it in __blk_flush_plug() is still unordered against
current->plug = NULL.
Fix it by adding a barrier() after clearing PF_BLOCK_TS in
__blk_flush_plug(), so that the compiler can not move the store of
current->plug = NULL in blk_finish_plug() before it. Both stores and
the reader in finish_task_switch() run on the same CPU in the context
of current task, so a compiler barrier is enough here.
Fixes: fad156c2af22 ("block: invalidate cached plug timestamp after task switch")
Signed-off-by: Chao Yu <chao@kernel.org>
---
block/blk-core.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/block/blk-core.c b/block/blk-core.c
index 8836c7028568..65379d5a1686 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -1283,6 +1283,12 @@ void __blk_flush_plug(struct blk_plug *plug, bool from_schedule)
plug->cur_ktime = 0;
current->flags &= ~PF_BLOCK_TS;
+ /*
+ * Ensure PF_BLOCK_TS is cleared before blk_finish_plug() sets
+ * current->plug to NULL, otherwise blk_plug_invalidate_ts() can
+ * dereference a NULL current->plug if preempted in between.
+ */
+ barrier();
}
/**
--
2.49.0
reply other threads:[~2026-11-16 0:07 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261116000723.934826-1-chao@kernel.org \
--to=chao@kernel.org \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.