block: prevent reordering of PF_BLOCK_TS clear and plug NULLing
PATCH 1/1 block: prevent reordering of PF_BLOCK_TS clear and plug NULLing
From: Chao Yu <[email protected]>
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.
--- block/blk-core.c | 6 ++++++ 1 file changed, 6 insertions(+)
block/blk-core.c +6 -0
@@ -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 (); }/**
Signature · 1 line
2.49.0