Skip to contentSkip to conversation

f2fs: fix deadlock in serializing io

PATCH In review Xiaosen He via Linux-f2fs-devel 1 participant 9 messages Apr 10 – Apr 22 F2FS file system

lore
Share & tools
lore
#0 lore raw

v3 cover f2fs: fix deadlock in serializing io

Change in v3:
if get mutex by mutex_trylock(), do not call mutex_lock() later.

Change in v2:
call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

Xiaosen He (1):
f2fs: fix deadlock in serializing io

 fs/f2fs/data.c | 6 ++++++
 1 file changed, 6 insertions(+)
Signature · 8 lines
2.34.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH v3 1/1 f2fs: fix deadlock in serializing io

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

    Task name: kworker/u32:7    [affinity: 0xff] pid:    233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
    state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000
    Last_enqueued_ts:     390.169395877 Last_sleep_ts:     389.756639574
    Stack:
    [<ffffffd3a0b1d254>] __switch_to+0x214
    [<ffffffd3a0b1deb8>] __schedule+0xa30
    [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
    [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
    [<ffffffd39f9995b8>] lock_acquire+0x68
    [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
    [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
    [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
    [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
    [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
    [<ffffffd39f93e6c4>] sched_submit_work+0x78
    [<ffffffd3a0b1e4d8>] schedule+0x38
    [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18
    [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8
    [<ffffffd3a0b20084>] mutex_lock_nested+0x2c
    [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64
    [<ffffffd39fbb3958>] do_writepages+0xd4
    [<ffffffd39fd09fd8>] __writeback_single_inode+0x78
    [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8
    [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0
    [<ffffffd39fd09054>] wb_writeback+0x188
    [<ffffffd39fd066d0>] wb_workfn[jt]+0x438
    [<ffffffd39f9198e0>] process_one_work+0x27c
    [<ffffffd39f91baf8>] worker_thread+0x358
    [<ffffffd39f924534>] kthread+0x150
    [<ffffffd39f830e44>] ret_from_fork+0x10

    Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
    state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000
    Last_enqueued_ts:     389.899608637 Last_sleep_ts:     389.899665303
    Stack:
    [<ffffffd3a0b1d254>] __switch_to+0x214
    [<ffffffd3a0b1deb8>] __schedule+0xa30
    [<ffffffd3a0b1e4e8>] schedule+0x48
    [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
    [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
    [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
    [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
    [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
    [<ffffffd3a06de244>] devfreq_set_target+0xb8
    [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
    [<ffffffd3a06de5e8>] devfreq_monitor+0x38
    [<ffffffd39f9198e0>] process_one_work+0x27c
    [<ffffffd39f91baf8>] worker_thread+0x358
    [<ffffffd39f924534>] kthread+0x150
    [<ffffffd39f830e44>] ret_from_fork+0x10

    Task name: emulated;0       [affinity: 0xff] pid:   5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
    state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000
    Last_enqueued_ts:     390.169395877 Last_sleep_ts:     390.178459731
    Stack:
    [<ffffffd3a0b1d254>] __switch_to+0x214
    [<ffffffd3a0b1deb8>] __schedule+0xa30
    [<ffffffd3a0b1e4e8>] schedule+0x48
    [<ffffffd3a0b1e8f4>] io_schedule+0x38
    [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
    [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
    [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
    [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
    [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
    [<ffffffd39ffd3394>] submit_bio_noacct+0x330
    [<ffffffd39ffd3814>] submit_bio+0x1f4
    [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
    [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
    [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
    [<ffffffd39ff04aac>] do_write_page+0x180
    [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
    [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
    [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
    [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
    [<ffffffd39fbb3958>] do_writepages+0xd4
    [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
    [<ffffffd39fbb0370>] generic_fadvise+0x1d8
    [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
    [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
    [<ffffffd39f847e7c>] invoke_syscall+0x58
    [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
    [<ffffffd39f847d18>] do_el0_svc+0x1c
    [<ffffffd3a0b14fac>] el0_svc+0x40
    [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
    [<ffffffd39f8116a0>] ret_to_user[jt]+0x0
---
 fs/f2fs/data.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/f2fs/data.c +6 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
 	}
 
 	if (__should_serialize_io(inode, wbc)) {
+		if (!mutex_trylock(&sbi->writepages))
+			blk_flush_plug(((struct task_struct *)current)->plug, true);
+		else
+			goto set_locked;
+
 		mutex_lock(&sbi->writepages);
+set_locked:
 		locked = true;
 	}
 
Signature · 8 lines
2.34.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw
Quoted 6 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag

Quoted 8 lines

as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

Quoted 15 lines
     Task name: kworker/u32:7    [affinity: 0xff] pid:    233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts:     389.756639574
     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
     [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
     [<ffffffd39f9995b8>] lock_acquire+0x68
     [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
     [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
     [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
     [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
     [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages))

	blk_flush_plug(((struct task_struct *)current)->plug, true); <--- here

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

Quoted 85 lines
     [<ffffffd39f93e6c4>] sched_submit_work+0x78
     [<ffffffd3a0b1e4d8>] schedule+0x38
     [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18
     [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8
     [<ffffffd3a0b20084>] mutex_lock_nested+0x2c
     [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64
     [<ffffffd39fbb3958>] do_writepages+0xd4
     [<ffffffd39fd09fd8>] __writeback_single_inode+0x78
     [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8
     [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0
     [<ffffffd39fd09054>] wb_writeback+0x188
     [<ffffffd39fd066d0>] wb_workfn[jt]+0x438
     [<ffffffd39f9198e0>] process_one_work+0x27c
     [<ffffffd39f91baf8>] worker_thread+0x358
     [<ffffffd39f924534>] kthread+0x150
     [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000
     Last_enqueued_ts:     389.899608637 Last_sleep_ts:     389.899665303
     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e4e8>] schedule+0x48
     [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
     [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
     [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
     [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
     [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
     [<ffffffd3a06de244>] devfreq_set_target+0xb8
     [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
     [<ffffffd3a06de5e8>] devfreq_monitor+0x38
     [<ffffffd39f9198e0>] process_one_work+0x27c
     [<ffffffd39f91baf8>] worker_thread+0x358
     [<ffffffd39f924534>] kthread+0x150
     [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: emulated;0       [affinity: 0xff] pid:   5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts:     390.178459731
     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e4e8>] schedule+0x48
     [<ffffffd3a0b1e8f4>] io_schedule+0x38
     [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
     [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
     [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
     [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
     [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
     [<ffffffd39ffd3394>] submit_bio_noacct+0x330
     [<ffffffd39ffd3814>] submit_bio+0x1f4
     [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
     [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
     [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
     [<ffffffd39ff04aac>] do_write_page+0x180
     [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
     [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
     [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
     [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
     [<ffffffd39fbb3958>] do_writepages+0xd4
     [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
     [<ffffffd39fbb0370>] generic_fadvise+0x1d8
     [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
     [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
     [<ffffffd39f847e7c>] invoke_syscall+0x58
     [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
     [<ffffffd39f847d18>] do_el0_svc+0x1c
     [<ffffffd3a0b14fac>] el0_svc+0x40
     [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
     [<ffffffd39f8116a0>] ret_to_user[jt]+0x0
---
  fs/f2fs/data.c | 6 ++++++
  1 file changed, 6 insertions(+)
fs/f2fs/data.c +0 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
  	}
 
  	if (__should_serialize_io(inode, wbc)) {

fs/f2fs/data.c static int __f2fs_write_data_pages(struct address_space *mapping, · line 3597

Quoted 10 lines
fs/f2fs/data.c +6 -0
+		if (!mutex_trylock(&sbi->writepages))
+			blk_flush_plug(((struct task_struct *)current)->plug, true);
+		else
+			goto set_locked;
+
  		mutex_lock(&sbi->writepages);
+set_locked:
  		locked = true;
  	}
 

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw
Quoted 35 lines · Chao Yu
Quoted 6 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag

Quoted 8 lines

as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

Quoted 4 lines

     Task name: kworker/u32:7    [affinity: 0xff] pid:    233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts: 389.756639574

Quoted 11 lines
     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
     [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
     [<ffffffd39f9995b8>] lock_acquire+0x68
     [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
     [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
     [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
     [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
     [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
Quoted 5 lines

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages))
    blk_flush_plug(((struct task_struct *)current)->plug, true); <--- here

If blk_flush_plug is called here, when preemption happened in RCU critical

seciton, the task would be scheduled out in R state, so that the task can be

scheduled in quickly to end RCU grace period, then it won't block UFS host.

Regards,

Xiaosen

Quoted 84 lines

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

Quoted 77 lines

     [<ffffffd39f93e6c4>] sched_submit_work+0x78 [<ffffffd3a0b1e4d8>] schedule+0x38 [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18 [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8 [<ffffffd3a0b20084>] mutex_lock_nested+0x2c [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64 [<ffffffd39fbb3958>] do_writepages+0xd4 [<ffffffd39fd09fd8>] __writeback_single_inode+0x78 [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8 [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0 [<ffffffd39fd09054>] wb_writeback+0x188 [<ffffffd39fd066d0>] wb_workfn[jt]+0x438 [<ffffffd39f9198e0>] process_one_work+0x27c [<ffffffd39f91baf8>] worker_thread+0x358 [<ffffffd39f924534>] kthread+0x150 [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000
     Last_enqueued_ts:     389.899608637 Last_sleep_ts: 389.899665303

     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e4e8>] schedule+0x48
     [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
     [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
     [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
     [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
     [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
     [<ffffffd3a06de244>] devfreq_set_target+0xb8
     [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
     [<ffffffd3a06de5e8>] devfreq_monitor+0x38
     [<ffffffd39f9198e0>] process_one_work+0x27c
     [<ffffffd39f91baf8>] worker_thread+0x358
     [<ffffffd39f924534>] kthread+0x150
     [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: emulated;0       [affinity: 0xff] pid:   5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts: 390.178459731

     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e4e8>] schedule+0x48
     [<ffffffd3a0b1e8f4>] io_schedule+0x38
     [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
     [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
     [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
     [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
     [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
     [<ffffffd39ffd3394>] submit_bio_noacct+0x330
     [<ffffffd39ffd3814>] submit_bio+0x1f4
     [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
     [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
     [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
     [<ffffffd39ff04aac>] do_write_page+0x180
     [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
     [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
     [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
     [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
     [<ffffffd39fbb3958>] do_writepages+0xd4
     [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
     [<ffffffd39fbb0370>] generic_fadvise+0x1d8
     [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
     [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
     [<ffffffd39f847e7c>] invoke_syscall+0x58
     [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
     [<ffffffd39f847d18>] do_el0_svc+0x1c
     [<ffffffd3a0b14fac>] el0_svc+0x40
     [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
     [<ffffffd39f8116a0>] ret_to_user[jt]+0x0

---
  fs/f2fs/data.c | 6 ++++++

  1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
fs/f2fs/data.c +0 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
       }

fs/f2fs/data.c static int __f2fs_write_data_pages(struct address_space *mapping, · line 3595

Quoted 10 lines
fs/f2fs/data.c +6 -0
         if (__should_serialize_io(inode, wbc)) {
+        if (!mutex_trylock(&sbi->writepages))
+            blk_flush_plug(((struct task_struct *)current)->plug, true);
+        else
+            goto set_locked;
+
           mutex_lock(&sbi->writepages);
+set_locked:
           locked = true;
       }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#4 lore raw
Quoted 18 lines · On 4/13/2026 10:42 PM, Xiaosen via Linux-f2fs-devel wrote:
Quoted 16 lines · Chao Yu
Quoted 14 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

Quoted 3 lines
Quoted 3 lines

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

In writeback thread, the call path would be as follows. blk_start_plug
mutex_lock
writeback file A
mutex_unlock
mutext_lock
writeback file B
mutex_unlock
......
blk_finish_plug

So, most probably when the thread failed to acquire the mutex for writing back a file's data, the block plug list is not empty as the previously handled files' data  have been prepared as write requests and plugged on the list.

Quoted 24 lines
Quoted 16 lines
Quoted 4 lines

     Task name: kworker/u32:7    [affinity: 0xff] pid:    233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts: 389.756639574

Quoted 11 lines
     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
     [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
     [<ffffffd39f9995b8>] lock_acquire+0x68
     [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
     [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
     [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
     [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
     [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
Quoted 5 lines

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages))
    blk_flush_plug(((struct task_struct *)current)->plug, true); <--- here

If blk_flush_plug is called here, when preemption happened in RCU critical

Quoted 14 lines

seciton, the task would be scheduled out in R state, so that the task can be

scheduled in quickly to end RCU grace period, then it won't block UFS host.

Regards,

Xiaosen

Quoted 6 lines

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

kworker/u32:2 (writeback thread)
__mutex_lock_common
  set_current_state(TASK_UNINTERRUPTIBLE);
  schedule_preempt_disabled
    schedule
      sched_submit_work
        blk_flush_plug
          __blk_flush_plug
            blk_mq_flush_plug_list
              blk_mq_dispatch_list
                percpu_ref_put
                  percpu_ref_put_many
                    rcu_read_lock
                      preempt_schedule_notrace

In __mutex_lock_slowpath, the task's state is set to TASK_UNINTERRUPTIBLE, then call blk_flush_plug later, so once preemption happened in RCU critical section, the task would be stuck in RCU critical section waiting for the release of mutex. Regarding my fix, in mutex_trylock, it won't call blk_flush_plug if mutex_trylock failed. Let's flush plug list if mutex_trylock failed, so that when it goes to mutex_lock and failed to acquire the mutex, the later call to blk_flush_plug would be simple as the plug list is empty and there is no chance to go to rcu_read_lock.

Regards,
Xiaosen

Quoted 97 lines
Quoted 78 lines
Quoted 78 lines

     [<ffffffd39f93e6c4>] sched_submit_work+0x78 [<ffffffd3a0b1e4d8>] schedule+0x38 [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18 [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8 [<ffffffd3a0b20084>] mutex_lock_nested+0x2c [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64 [<ffffffd39fbb3958>] do_writepages+0xd4 [<ffffffd39fd09fd8>] __writeback_single_inode+0x78 [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8 [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0 [<ffffffd39fd09054>] wb_writeback+0x188 [<ffffffd39fd066d0>] wb_workfn[jt]+0x438 [<ffffffd39f9198e0>] process_one_work+0x27c [<ffffffd39f91baf8>] worker_thread+0x358 [<ffffffd39f924534>] kthread+0x150 [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000
     Last_enqueued_ts:     389.899608637 Last_sleep_ts: 389.899665303

     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e4e8>] schedule+0x48
     [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
     [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
     [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
     [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
     [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
     [<ffffffd3a06de244>] devfreq_set_target+0xb8
     [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
     [<ffffffd3a06de5e8>] devfreq_monitor+0x38
     [<ffffffd39f9198e0>] process_one_work+0x27c
     [<ffffffd39f91baf8>] worker_thread+0x358
     [<ffffffd39f924534>] kthread+0x150
     [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: emulated;0       [affinity: 0xff] pid:   5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts: 390.178459731

     Stack:
     [<ffffffd3a0b1d254>] __switch_to+0x214
     [<ffffffd3a0b1deb8>] __schedule+0xa30
     [<ffffffd3a0b1e4e8>] schedule+0x48
     [<ffffffd3a0b1e8f4>] io_schedule+0x38
     [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
     [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
     [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
     [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
     [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
     [<ffffffd39ffd3394>] submit_bio_noacct+0x330
     [<ffffffd39ffd3814>] submit_bio+0x1f4
     [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
     [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
     [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
     [<ffffffd39ff04aac>] do_write_page+0x180
     [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
     [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
     [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
     [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
     [<ffffffd39fbb3958>] do_writepages+0xd4
     [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
     [<ffffffd39fbb0370>] generic_fadvise+0x1d8
     [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
     [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
     [<ffffffd39f847e7c>] invoke_syscall+0x58
     [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
     [<ffffffd39f847d18>] do_el0_svc+0x1c
     [<ffffffd3a0b14fac>] el0_svc+0x40
     [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
     [<ffffffd39f8116a0>] ret_to_user[jt]+0x0

---
  fs/f2fs/data.c | 6 ++++++

  1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
fs/f2fs/data.c +0 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
       }

fs/f2fs/data.c static int __f2fs_write_data_pages(struct address_space *mapping, · line 3595

Quoted 10 lines
fs/f2fs/data.c +6 -0
         if (__should_serialize_io(inode, wbc)) {
+        if (!mutex_trylock(&sbi->writepages))
+            blk_flush_plug(((struct task_struct *)current)->plug, true);
+        else
+            goto set_locked;
+
           mutex_lock(&sbi->writepages);
+set_locked:
           locked = true;
       }
Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#5 lore raw
Quoted 90 lines · Xiaosen
Quoted 18 lines · On 4/13/2026 10:42 PM, Xiaosen via Linux-f2fs-devel wrote:
Quoted 16 lines · Chao Yu
Quoted 14 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

Quoted 3 lines
Quoted 3 lines

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

In writeback thread, the call path would be as follows. blk_start_plug
mutex_lock
writeback file A
mutex_unlock
mutext_lock
writeback file B
mutex_unlock
......
blk_finish_plug

So, most probably when the thread failed to acquire the mutex for writing back a file's data, the block plug list is not empty as the previously handled files' data  have been prepared as write requests and plugged on the list.

Quoted 24 lines
Quoted 16 lines
Quoted 4 lines

     Task name: kworker/u32:7    [affinity: 0xff] pid:    233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts: 389.756639574

Quoted 11 lines
      Stack:
      [<ffffffd3a0b1d254>] __switch_to+0x214
      [<ffffffd3a0b1deb8>] __schedule+0xa30
      [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
      [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
      [<ffffffd39f9995b8>] lock_acquire+0x68
      [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
      [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
      [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
      [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
      [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
Quoted 5 lines

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages))
    blk_flush_plug(((struct task_struct *)current)->plug, true); <--- here

If blk_flush_plug is called here, when preemption happened in RCU critical

Quoted 14 lines

seciton, the task would be scheduled out in R state, so that the task can be

scheduled in quickly to end RCU grace period, then it won't block UFS host.

Regards,

Xiaosen

Quoted 6 lines

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

kworker/u32:2 (writeback thread)
__mutex_lock_common
  set_current_state(TASK_UNINTERRUPTIBLE);
  schedule_preempt_disabled
    schedule
      sched_submit_work
        blk_flush_plug
          __blk_flush_plug
            blk_mq_flush_plug_list
              blk_mq_dispatch_list
                percpu_ref_put
                  percpu_ref_put_many
                    rcu_read_lock
                      preempt_schedule_notrace

Quoted 1 line

In __mutex_lock_slowpath, the task's state is set to TASK_UNINTERRUPTIBLE, then

Oh, I see, it's TASK_UNINTERRUPTIBLE state and be preempted, shouldn't we set TASK_INTERRUPTIBLE state before it runs into rcu critical region? Not sure, but I suspect it's a bug from common mutexlock/block/ufs modules rather than f2fs'.

Since it's hard to change common module, or can we consider to use rwsem lock instead of mutex lock in .writepages? I suspect we may suffer performance regression if we flush plug list in advance before mutex_lock.

Can you please give some performance numbers for both mutex and rwsem version?

Thanks,

Quoted 8 lines

call blk_flush_plug later, so once preemption happened in RCU critical section, the task would be stuck in RCU critical section waiting for the release of mutex. Regarding my fix, in mutex_trylock, it won't call blk_flush_plug if mutex_trylock failed. Let's flush plug list if mutex_trylock failed, so that when it goes to mutex_lock and failed to acquire the mutex, the later call to blk_flush_plug would be simple as the plug list is empty and there is no chance to go to rcu_read_lock.

Quoted 103 lines

Regards,
Xiaosen

Quoted 101 lines
Quoted 78 lines
Quoted 78 lines

     [<ffffffd39f93e6c4>] sched_submit_work+0x78 [<ffffffd3a0b1e4d8>] schedule+0x38 [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18 [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8 [<ffffffd3a0b20084>] mutex_lock_nested+0x2c [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64 [<ffffffd39fbb3958>] do_writepages+0xd4 [<ffffffd39fd09fd8>] __writeback_single_inode+0x78 [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8 [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0 [<ffffffd39fd09054>] wb_writeback+0x188 [<ffffffd39fd066d0>] wb_workfn[jt]+0x438 [<ffffffd39f9198e0>] process_one_work+0x27c [<ffffffd39f91baf8>] worker_thread+0x358 [<ffffffd39f924534>] kthread+0x150 [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000
     Last_enqueued_ts:     389.899608637 Last_sleep_ts: 389.899665303

      Stack:
      [<ffffffd3a0b1d254>] __switch_to+0x214
      [<ffffffd3a0b1deb8>] __schedule+0xa30
      [<ffffffd3a0b1e4e8>] schedule+0x48
      [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
      [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
      [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
      [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
      [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
      [<ffffffd3a06de244>] devfreq_set_target+0xb8
      [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
      [<ffffffd3a06de5e8>] devfreq_monitor+0x38
      [<ffffffd39f9198e0>] process_one_work+0x27c
      [<ffffffd39f91baf8>] worker_thread+0x358
      [<ffffffd39f924534>] kthread+0x150
      [<ffffffd39f830e44>] ret_from_fork+0x10

     Task name: emulated;0       [affinity: 0xff] pid:   5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
     state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000
     Last_enqueued_ts:     390.169395877 Last_sleep_ts: 390.178459731

      Stack:
      [<ffffffd3a0b1d254>] __switch_to+0x214
      [<ffffffd3a0b1deb8>] __schedule+0xa30
      [<ffffffd3a0b1e4e8>] schedule+0x48
      [<ffffffd3a0b1e8f4>] io_schedule+0x38
      [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
      [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
      [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
      [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
      [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
      [<ffffffd39ffd3394>] submit_bio_noacct+0x330
      [<ffffffd39ffd3814>] submit_bio+0x1f4
      [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
      [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
      [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
      [<ffffffd39ff04aac>] do_write_page+0x180
      [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
      [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
      [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
      [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
      [<ffffffd39fbb3958>] do_writepages+0xd4
      [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
      [<ffffffd39fbb0370>] generic_fadvise+0x1d8
      [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
      [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
      [<ffffffd39f847e7c>] invoke_syscall+0x58
      [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
      [<ffffffd39f847d18>] do_el0_svc+0x1c
      [<ffffffd3a0b14fac>] el0_svc+0x40
      [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
      [<ffffffd39f8116a0>] ret_to_user[jt]+0x0
---
   fs/f2fs/data.c | 6 ++++++
   1 file changed, 6 insertions(+)
Quoted 17 lines
Quoted 6 lines
fs/f2fs/data.c +0 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
       }

fs/f2fs/data.c static int __f2fs_write_data_pages(struct address_space *mapping, · line 3595

Quoted 10 lines
fs/f2fs/data.c +6 -0
         if (__should_serialize_io(inode, wbc)) {
+        if (!mutex_trylock(&sbi->writepages))
+            blk_flush_plug(((struct task_struct *)current)->plug, true);
+        else
+            goto set_locked;
+
           mutex_lock(&sbi->writepages);
+set_locked:
           locked = true;
       }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#6 lore raw
Quoted 97 lines · Chao Yu
Quoted 90 lines · Xiaosen
Quoted 18 lines · On 4/13/2026 10:42 PM, Xiaosen via Linux-f2fs-devel wrote:
Quoted 16 lines · Chao Yu
Quoted 14 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

Quoted 3 lines
Quoted 3 lines

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

In writeback thread, the call path would be as follows. blk_start_plug
mutex_lock
writeback file A
mutex_unlock
mutext_lock
writeback file B
mutex_unlock
......
blk_finish_plug

So, most probably when the thread failed to acquire the mutex for writing back a file's data, the block plug list is not empty as the previously handled files' data  have been prepared as write requests and plugged on the list.

Quoted 24 lines
Quoted 16 lines
Quoted 4 lines

      Task name: kworker/u32:7    [affinity: 0xff] pid: 233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
      state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000
      Last_enqueued_ts:     390.169395877 Last_sleep_ts: 389.756639574

Quoted 11 lines
      Stack:
      [<ffffffd3a0b1d254>] __switch_to+0x214
      [<ffffffd3a0b1deb8>] __schedule+0xa30
      [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
      [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
      [<ffffffd39f9995b8>] lock_acquire+0x68
      [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
      [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
      [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
      [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
      [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
Quoted 5 lines

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages))
     blk_flush_plug(((struct task_struct *)current)->plug, true); <--- here

If blk_flush_plug is called here, when preemption happened in RCU critical

Quoted 14 lines

seciton, the task would be scheduled out in R state, so that the task can be

scheduled in quickly to end RCU grace period, then it won't block UFS host.

Regards,

Xiaosen

Quoted 6 lines

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

kworker/u32:2 (writeback thread)
__mutex_lock_common
    set_current_state(TASK_UNINTERRUPTIBLE);
    schedule_preempt_disabled
      schedule
        sched_submit_work
          blk_flush_plug
            __blk_flush_plug
              blk_mq_flush_plug_list
                blk_mq_dispatch_list
                  percpu_ref_put
                    percpu_ref_put_many
                      rcu_read_lock
                        preempt_schedule_notrace

Quoted 1 line

In __mutex_lock_slowpath, the task's state is set to TASK_UNINTERRUPTIBLE, then

Oh, I see, it's TASK_UNINTERRUPTIBLE state and be preempted, shouldn't we set TASK_INTERRUPTIBLE state before it runs into rcu critical region? Not sure, but I suspect it's a bug from common mutexlock/block/ufs modules rather than f2fs'.

Quoted 7 lines

Since it's hard to change common module, or can we consider to use rwsem lock instead of mutex lock in .writepages? I suspect we may suffer performance regression if we flush plug list in advance before mutex_lock.

Can you please give some performance numbers for both mutex and rwsem version?

Thanks,

A task would be set to TASK_UNINTERRUPTIBLE then call schedule_preempt_disabled

also if failed to acquire rwsem lock, and the task will encounter similar deadlock.

If the mutex has already been acquired by another task, the writeback thread would flush

plug list anyway. And the plug list is empty when just write back a single file's data.

I don't think it will make big difference to performance.

I can give performance numbers if you insist.

Regards,

Xiaosen

Quoted 110 lines
Quoted 110 lines

call blk_flush_plug later, so once preemption happened in RCU critical section, the task would be stuck in RCU critical section waiting for the release of mutex. Regarding my fix, in mutex_trylock, it won't call blk_flush_plug if mutex_trylock failed. Let's flush plug list if mutex_trylock failed, so that when it goes to mutex_lock and failed to acquire the mutex, the later call to blk_flush_plug would be simple as the plug list is empty and there is no chance to go to rcu_read_lock.

Regards,
Xiaosen

Quoted 99 lines
Quoted 97 lines
Quoted 96 lines

      [<ffffffd39f93e6c4>] sched_submit_work+0x78 [<ffffffd3a0b1e4d8>] schedule+0x38 [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18 [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8 [<ffffffd3a0b20084>] mutex_lock_nested+0x2c [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64 [<ffffffd39fbb3958>] do_writepages+0xd4 [<ffffffd39fd09fd8>] __writeback_single_inode+0x78 [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8 [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0 [<ffffffd39fd09054>] wb_writeback+0x188 [<ffffffd39fd066d0>] wb_workfn[jt]+0x438 [<ffffffd39f9198e0>] process_one_work+0x27c [<ffffffd39f91baf8>] worker_thread+0x358 [<ffffffd39f924534>] kthread+0x150 [<ffffffd39f830e44>] ret_from_fork+0x10

      Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
      state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000
      Last_enqueued_ts:     389.899608637 Last_sleep_ts: 389.899665303

      Stack:
      [<ffffffd3a0b1d254>] __switch_to+0x214
      [<ffffffd3a0b1deb8>] __schedule+0xa30
      [<ffffffd3a0b1e4e8>] schedule+0x48
      [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
      [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
      [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
      [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
      [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
      [<ffffffd3a06de244>] devfreq_set_target+0xb8
      [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
      [<ffffffd3a06de5e8>] devfreq_monitor+0x38
      [<ffffffd39f9198e0>] process_one_work+0x27c
      [<ffffffd39f91baf8>] worker_thread+0x358
      [<ffffffd39f924534>] kthread+0x150
      [<ffffffd39f830e44>] ret_from_fork+0x10

      Task name: emulated;0       [affinity: 0xff] pid: 5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
      state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000
      Last_enqueued_ts:     390.169395877 Last_sleep_ts: 390.178459731

      Stack:
      [<ffffffd3a0b1d254>] __switch_to+0x214
      [<ffffffd3a0b1deb8>] __schedule+0xa30
      [<ffffffd3a0b1e4e8>] schedule+0x48
      [<ffffffd3a0b1e8f4>] io_schedule+0x38
      [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
      [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
      [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
      [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
      [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
      [<ffffffd39ffd3394>] submit_bio_noacct+0x330
      [<ffffffd39ffd3814>] submit_bio+0x1f4
      [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
      [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
      [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
      [<ffffffd39ff04aac>] do_write_page+0x180
      [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
      [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
      [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
      [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
      [<ffffffd39fbb3958>] do_writepages+0xd4
      [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
      [<ffffffd39fbb0370>] generic_fadvise+0x1d8
      [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
      [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
      [<ffffffd39f847e7c>] invoke_syscall+0x58
      [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
      [<ffffffd39f847d18>] do_el0_svc+0x1c
      [<ffffffd3a0b14fac>] el0_svc+0x40
      [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
      [<ffffffd39f8116a0>] ret_to_user[jt]+0x0

---
   fs/f2fs/data.c | 6 ++++++

   1 file changed, 6 insertions(+)
fs/f2fs/data.c +6 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct 
 address_space *mapping,
        }
          if (__should_serialize_io(inode, wbc)) {
+        if (!mutex_trylock(&sbi->writepages))
+            blk_flush_plug(((struct task_struct *)current)->plug, 
 true);
+        else
+            goto set_locked;
+
            mutex_lock(&sbi->writepages);
+set_locked:
            locked = true;
        }
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#7 lore raw
Quoted 121 lines · Xiaosen He
Quoted 119 lines · Chao Yu
Quoted 100 lines · Xiaosen
Quoted 18 lines · On 4/13/2026 10:42 PM, Xiaosen via Linux-f2fs-devel wrote:
Quoted 16 lines · Chao Yu
Quoted 14 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

Quoted 4 lines
Quoted 4 lines

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

In writeback thread, the call path would be as follows. blk_start_plug
mutex_lock
writeback file A
mutex_unlock
mutext_lock
writeback file B
mutex_unlock
......
blk_finish_plug

So, most probably when the thread failed to acquire the mutex for writing back a
file's data, the block plug list is not empty as the previously handled files'
data  have been prepared as write requests and plugged on the list.

Quoted 32 lines
Quoted 18 lines
Quoted 6 lines

      Task name: kworker/u32:7    [affinity: 0xff] pid: 233
tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
      state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000 Last_enqueued_ts:     390.169395877 Last_sleep_ts:
389.756639574

Quoted 11 lines
       Stack:
       [<ffffffd3a0b1d254>] __switch_to+0x214
       [<ffffffd3a0b1deb8>] __schedule+0xa30
       [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
       [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
       [<ffffffd39f9995b8>] lock_acquire+0x68
       [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
       [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
       [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
       [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
       [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
Quoted 7 lines

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause
the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages))
     blk_flush_plug(((struct task_struct *)current)->plug, true);
<--- here

If blk_flush_plug is called here, when preemption happened in RCU critical

seciton, the task would be scheduled out in R state, so that the task can be

Quoted 13 lines

scheduled in quickly to end RCU grace period, then it won't block UFS host.

Regards,

Xiaosen

Quoted 6 lines

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

kworker/u32:2 (writeback thread)
__mutex_lock_common
    set_current_state(TASK_UNINTERRUPTIBLE);
    schedule_preempt_disabled
      schedule
        sched_submit_work
          blk_flush_plug
            __blk_flush_plug
              blk_mq_flush_plug_list
                blk_mq_dispatch_list
                  percpu_ref_put
                    percpu_ref_put_many
                      rcu_read_lock
                        preempt_schedule_notrace

Quoted 2 lines

In __mutex_lock_slowpath, the task's state is set to TASK_UNINTERRUPTIBLE, then

Oh, I see, it's TASK_UNINTERRUPTIBLE state and be preempted, shouldn't we set
TASK_INTERRUPTIBLE state before it runs into rcu critical region? Not sure, but
I suspect it's a bug from common mutexlock/block/ufs modules rather than f2fs'.

Since it's hard to change common module, or can we consider to use rwsem lock
instead of mutex lock in .writepages? I suspect we may suffer performance regression if we flush plug list in advance before mutex_lock.

Can you please give some performance numbers for both mutex and rwsem version?

Quoted 7 lines
Quoted 1 line

Thanks,

A task would be set to TASK_UNINTERRUPTIBLE then call schedule_preempt_disabled

also if failed to acquire rwsem lock, and the task will encounter similar deadlock.

Oh, well, we will suffer the same issue w/ io_rwsem?

- writepages
- f2fs_write_cache_pages
- f2fs_write_single_data_page
- f2fs_do_write_data_page
- f2fs_outplace_write_data
- do_write_page
- f2fs_submit_page_write
- f2fs_down_write_trace(io_rwsem)
: can block on blk_plug_flush -> rcu_read_lock -> preempt
meanwhile blk_mq_quiesce_tagset will block on synchronize_rcu
- __submit_merged_bio

Quoted 10 lines

If the mutex has already been acquired by another task, the writeback thread would flush

plug list anyway. And the plug list is empty when just write back a single file's data.

I don't think it will make big difference to performance.

I can give performance numbers if you insist.

Please figure out some numbers, I guess you can flush plug list whenever trying to grab sbi->writepages lock in the control group, it can help to simulate the extreme case.

Thanks,

Quoted 130 lines

Regards,

Xiaosen

Quoted 125 lines
Quoted 124 lines

call blk_flush_plug later, so once preemption happened in RCU critical section,
the task would be stuck in RCU critical section waiting for the release of mutex.
Regarding my fix, in mutex_trylock, it won't call blk_flush_plug if mutex_trylock
failed. Let's flush plug list if mutex_trylock failed, so that when it goes to
mutex_lock and failed to acquire the mutex, the later call to blk_flush_plug
would be simple as the plug list is empty and there is no chance to go to
rcu_read_lock.

Regards,
Xiaosen

Quoted 107 lines
Quoted 92 lines
Quoted 92 lines

      [<ffffffd39f93e6c4>] sched_submit_work+0x78 [<ffffffd3a0b1e4d8>] schedule+0x38 [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18 [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8 [<ffffffd3a0b20084>] mutex_lock_nested+0x2c [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64 [<ffffffd39fbb3958>] do_writepages+0xd4 [<ffffffd39fd09fd8>] __writeback_single_inode+0x78 [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8 [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0 [<ffffffd39fd09054>] wb_writeback+0x188 [<ffffffd39fd066d0>] wb_workfn[jt]+0x438 [<ffffffd39f9198e0>] process_one_work+0x27c [<ffffffd39f91baf8>] worker_thread+0x358 [<ffffffd39f924534>] kthread+0x150 [<ffffffd39f830e44>] ret_from_fork+0x10

      Task name: kworker/u32:2    [affinity: 0xff] pid:     90
tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
      state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000 Last_enqueued_ts:     389.899608637 Last_sleep_ts:
389.899665303

       Stack:
       [<ffffffd3a0b1d254>] __switch_to+0x214
       [<ffffffd3a0b1deb8>] __schedule+0xa30
       [<ffffffd3a0b1e4e8>] schedule+0x48
       [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
       [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
       [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
       [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
       [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
       [<ffffffd3a06de244>] devfreq_set_target+0xb8
       [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
       [<ffffffd3a06de5e8>] devfreq_monitor+0x38
       [<ffffffd39f9198e0>] process_one_work+0x27c
       [<ffffffd39f91baf8>] worker_thread+0x358
       [<ffffffd39f924534>] kthread+0x150
       [<ffffffd39f830e44>] ret_from_fork+0x10

      Task name: emulated;0       [affinity: 0xff] pid: 5245
tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
      state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000 Last_enqueued_ts:     390.169395877 Last_sleep_ts:
390.178459731

       Stack:
       [<ffffffd3a0b1d254>] __switch_to+0x214
       [<ffffffd3a0b1deb8>] __schedule+0xa30
       [<ffffffd3a0b1e4e8>] schedule+0x48
       [<ffffffd3a0b1e8f4>] io_schedule+0x38
       [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
       [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
       [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
       [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
       [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
       [<ffffffd39ffd3394>] submit_bio_noacct+0x330
       [<ffffffd39ffd3814>] submit_bio+0x1f4
       [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
       [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
       [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
       [<ffffffd39ff04aac>] do_write_page+0x180
       [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
       [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
       [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
       [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
       [<ffffffd39fbb3958>] do_writepages+0xd4
       [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
       [<ffffffd39fbb0370>] generic_fadvise+0x1d8
       [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
       [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
       [<ffffffd39f847e7c>] invoke_syscall+0x58
       [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
       [<ffffffd39f847d18>] do_el0_svc+0x1c
       [<ffffffd3a0b14fac>] el0_svc+0x40
       [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
       [<ffffffd39f8116a0>] ret_to_user[jt]+0x0
---
    fs/f2fs/data.c | 6 ++++++
    1 file changed, 6 insertions(+)
fs/f2fs/data.c +2 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct
 address_space *mapping,
        }
          if (__should_serialize_io(inode, wbc)) {
+        if (!mutex_trylock(&sbi->writepages))
+            blk_flush_plug(((struct task_struct *)current)->plug,
Quoted 9 lines

fs/f2fs/data.c static int __f2fs_write_data_pages(struct address_space *mapping, · line 3596

Quoted 8 lines
fs/f2fs/data.c +4 -0
 true);
+        else
+            goto set_locked;
+
            mutex_lock(&sbi->writepages);
+set_locked:
            locked = true;
        }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#8 lore raw
Quoted 132 lines · Chao Yu
Quoted 121 lines · Xiaosen He
Quoted 119 lines · Chao Yu
Quoted 100 lines · Xiaosen
Quoted 18 lines · On 4/13/2026 10:42 PM, Xiaosen via Linux-f2fs-devel wrote:
Quoted 16 lines · Chao Yu
Quoted 14 lines · Xiaosen He

kworker/u32:7(f2fs writeback thread) was trying to acquire sbi->writepages, the mutex was already acquired by emulated;0. kworker/u32:7 tried to flush plugged IO before sleep, during flushing plug list, kworker/u32:7 got preempted in RCU read-side critical section and got scheduled out waiting to be woken up by the release of mutex. emulated;0 was blocked on blk_mq_get_tag as there was no available tag and expected the previous IO requests to be handled by UFS host to release tags, but UFS host was blocked on synchronize_rcu waiting for ending of RCU grace period. the deadlock was caused by writeback thread was stuck in RCU critical section waiting for mutex. call blk_flush_plug() if mutex_trylock(&sbi->writepages) failed, so that the plug list is empty when blk_flush_plug() is called in sched_submit_work to fix the deadlock.

Quoted 4 lines
Quoted 4 lines

IMO, look more like a common bug? otherwise, sounds like we can not grab mutex lock w/ mutex_lock directly during writeback in .writepages?

In writeback thread, the call path would be as follows. blk_start_plug
mutex_lock
writeback file A
mutex_unlock
mutext_lock
writeback file B
mutex_unlock
......
blk_finish_plug

So, most probably when the thread failed to acquire the mutex for writing back a
file's data, the block plug list is not empty as the previously handled files'
data  have been prepared as write requests and plugged on the list.

Quoted 32 lines
Quoted 18 lines
Quoted 6 lines

       Task name: kworker/u32:7    [affinity: 0xff] pid: 233 tgid:    233 cpu: 6 prio: 98 start: 0xffffff8099390040
       state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc083c48000 Last_enqueued_ts:     390.169395877 Last_sleep_ts:
389.756639574

Quoted 11 lines
       Stack:
       [<ffffffd3a0b1d254>] __switch_to+0x214
       [<ffffffd3a0b1deb8>] __schedule+0xa30
       [<ffffffd3a0b1e754>] preempt_schedule_notrace+0x68
       [<ffffffd39f9ddf38>] rcu_is_watching[jt]+0x5c
       [<ffffffd39f9995b8>] lock_acquire+0x68
       [<ffffffd39ffe870c>] rcu_lock_acquire+0x34
       [<ffffffd39ffe8764>] percpu_ref_put_many+0x20
       [<ffffffd39ffe3a64>] blk_mq_dispatch_list+0x570
       [<ffffffd39ffe3264>] blk_mq_flush_plug_list+0x13c
       [<ffffffd39ffd43a8>] __blk_flush_plug+0x11c
Quoted 7 lines

It tries to flush plug list inside mutex_lock() and then be blocked. Will it cause
the same issue w/ your below implementation:

if (!mutex_trylock(&sbi->writepages)) blk_flush_plug(((struct task_struct *)current)->plug, true); <--- here

If blk_flush_plug is called here, when preemption happened in RCU critical

seciton, the task would be scheduled out in R state, so that the task can be

Quoted 13 lines

scheduled in quickly to end RCU grace period, then it won't block UFS host.

Regards,

Xiaosen

Quoted 6 lines

...
mutex_lock(&sbi->writepages);

Let me know if I'm missing anything.

Thanks,

kworker/u32:2 (writeback thread)
__mutex_lock_common
     set_current_state(TASK_UNINTERRUPTIBLE);
     schedule_preempt_disabled
       schedule
         sched_submit_work
           blk_flush_plug
             __blk_flush_plug
               blk_mq_flush_plug_list
                 blk_mq_dispatch_list
                   percpu_ref_put
                     percpu_ref_put_many
                       rcu_read_lock
                         preempt_schedule_notrace

Quoted 2 lines

In __mutex_lock_slowpath, the task's state is set to TASK_UNINTERRUPTIBLE, then

Oh, I see, it's TASK_UNINTERRUPTIBLE state and be preempted, shouldn't we set
TASK_INTERRUPTIBLE state before it runs into rcu critical region? Not sure, but
I suspect it's a bug from common mutexlock/block/ufs modules rather than f2fs'.

Since it's hard to change common module, or can we consider to use rwsem lock
instead of mutex lock in .writepages? I suspect we may suffer performance regression if we flush plug list in advance before mutex_lock.

Can you please give some performance numbers for both mutex and rwsem version?

Quoted 7 lines
Quoted 1 line

Thanks,

A task would be set to TASK_UNINTERRUPTIBLE then call schedule_preempt_disabled

also if failed to acquire rwsem lock, and the task will encounter similar deadlock.

Oh, well, we will suffer the same issue w/ io_rwsem?

Quoted 11 lines

- writepages
 - f2fs_write_cache_pages
  - f2fs_write_single_data_page
   - f2fs_do_write_data_page
    - f2fs_outplace_write_data
     - do_write_page
      - f2fs_submit_page_write
       - f2fs_down_write_trace(io_rwsem)
         : can block on blk_plug_flush -> rcu_read_lock -> preempt meanwhile blk_mq_quiesce_tagset will block on synchronize_rcu
        - __submit_merged_bio

I noticed this path too, so we are trying to fix the deadlock outside filesystems

now. please ignore this patch.

Quoted 10 lines
Quoted 10 lines

If the mutex has already been acquired by another task, the writeback thread would flush

plug list anyway. And the plug list is empty when just write back a single file's data.

I don't think it will make big difference to performance.

Quoted 139 lines
Quoted 1 line

I can give performance numbers if you insist.

Please figure out some numbers, I guess you can flush plug list whenever trying to grab sbi->writepages lock in the control group, it can help to simulate the extreme case.

Thanks,

Quoted 130 lines

Regards,

Xiaosen

Quoted 125 lines
Quoted 124 lines

call blk_flush_plug later, so once preemption happened in RCU critical section,
the task would be stuck in RCU critical section waiting for the release of mutex.
Regarding my fix, in mutex_trylock, it won't call blk_flush_plug if mutex_trylock
failed. Let's flush plug list if mutex_trylock failed, so that when it goes to
mutex_lock and failed to acquire the mutex, the later call to blk_flush_plug
would be simple as the plug list is empty and there is no chance to go to
rcu_read_lock.

Regards,
Xiaosen

Quoted 107 lines
Quoted 92 lines
Quoted 92 lines

[<ffffffd39f93e6c4>] sched_submit_work+0x78 [<ffffffd3a0b1e4d8>] schedule+0x38 [<ffffffd3a0b1e5c4>] schedule_preempt_disabled+0x18 [<ffffffd3a0b20c7c>] __mutex_lock_common+0xab8 [<ffffffd3a0b20084>] mutex_lock_nested+0x2c [<ffffffd39feea72c>] f2fs_write_data_pages+0xd64 [<ffffffd39fbb3958>] do_writepages+0xd4 [<ffffffd39fd09fd8>] __writeback_single_inode+0x78 [<ffffffd39fd09958>] writeback_sb_inodes+0x2b8 [<ffffffd39fd09cc0>] __writeback_inodes_wb+0xa0 [<ffffffd39fd09054>] wb_writeback+0x188 [<ffffffd39fd066d0>] wb_workfn[jt]+0x438 [<ffffffd39f9198e0>] process_one_work+0x27c [<ffffffd39f91baf8>] worker_thread+0x358 [<ffffffd39f924534>] kthread+0x150 [<ffffffd39f830e44>] ret_from_fork+0x10

       Task name: kworker/u32:2    [affinity: 0xff] pid:     90 tgid:     90 cpu: 1 prio: 120 start: 0xffffff80adae8040
       state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc080a18000 Last_enqueued_ts:     389.899608637 Last_sleep_ts:
389.899665303

       Stack:
       [<ffffffd3a0b1d254>] __switch_to+0x214
       [<ffffffd3a0b1deb8>] __schedule+0xa30
       [<ffffffd3a0b1e4e8>] schedule+0x48
       [<ffffffd39f9dfa64>] synchronize_rcu_expedited+0x928
       [<ffffffd39f9deeb0>] synchronize_rcu[jt]+0x234
       [<ffffffd39ffde5d4>] blk_mq_quiesce_tagset[jt]+0xa8
       [<ffffffd3a061cd90>] ufshcd_devfreq_scale+0x90
       [<ffffffd3a061ca6c>] ufshcd_devfreq_target+0x204
       [<ffffffd3a06de244>] devfreq_set_target+0xb8
       [<ffffffd3a06de14c>] devfreq_update_target[jt]+0xd8
       [<ffffffd3a06de5e8>] devfreq_monitor+0x38
       [<ffffffd39f9198e0>] process_one_work+0x27c
       [<ffffffd39f91baf8>] worker_thread+0x358
       [<ffffffd39f924534>] kthread+0x150
       [<ffffffd39f830e44>] ret_from_fork+0x10

       Task name: emulated;0       [affinity: 0xff] pid: 5245 tgid:   4922 cpu: 6 prio: 120 start: 0xffffff89e2e60040
       state: 0x2[D] exit_state: 0x0 stack base: 0xffffffc0d42f8000 Last_enqueued_ts:     390.169395877 Last_sleep_ts:
390.178459731

       Stack:
       [<ffffffd3a0b1d254>] __switch_to+0x214
       [<ffffffd3a0b1deb8>] __schedule+0xa30
       [<ffffffd3a0b1e4e8>] schedule+0x48
       [<ffffffd3a0b1e8f4>] io_schedule+0x38
       [<ffffffd39ffeb110>] blk_mq_get_tag+0x1a4
       [<ffffffd39ffdedc8>] __blk_mq_alloc_requests+0x358
       [<ffffffd39ffe409c>] blk_mq_submit_bio+0x50c
       [<ffffffd39ffd4cb8>] __submit_bio[jt]+0x164
       [<ffffffd39ffd2e7c>] submit_bio_noacct_nocheck+0x14c
       [<ffffffd39ffd3394>] submit_bio_noacct+0x330
       [<ffffffd39ffd3814>] submit_bio+0x1f4
       [<ffffffd39fee2ba8>] f2fs_submit_write_bio+0x88
       [<ffffffd39fee413c>] __submit_merged_bio[jt]+0xbc
       [<ffffffd39fee3c4c>] f2fs_submit_page_write+0x400
       [<ffffffd39ff04aac>] do_write_page+0x180
       [<ffffffd39ff04ca0>] f2fs_outplace_write_data+0x78
       [<ffffffd39fee88d4>] f2fs_do_write_data_page+0x390
       [<ffffffd39fee8df0>] f2fs_write_single_data_page+0x1e0
       [<ffffffd39feea288>] f2fs_write_data_pages+0x8c0
       [<ffffffd39fbb3958>] do_writepages+0xd4
       [<ffffffd39fba153c>] __filemap_fdatawrite_range+0x94
       [<ffffffd39fbb0370>] generic_fadvise+0x1d8
       [<ffffffd39feabaa0>] f2fs_file_fadvise+0x124
       [<ffffffd39fbb0570>] __arm64_sys_fadvise64_64+0x70
       [<ffffffd39f847e7c>] invoke_syscall+0x58
       [<ffffffd39f847da4>] el0_svc_common[jt]+0xb8
       [<ffffffd39f847d18>] do_el0_svc+0x1c
       [<ffffffd3a0b14fac>] el0_svc+0x40
       [<ffffffd3a0b14ef8>] el0t_64_sync_handler[jt]+0xd0
       [<ffffffd39f8116a0>] ret_to_user[jt]+0x0

---
    fs/f2fs/data.c | 6 ++++++

    1 file changed, 6 insertions(+)
fs/f2fs/data.c +2 -0
index 338df7a2aea6..c8e81f63fe73 100644
@@ -3585,7 +3585,13 @@ static int __f2fs_write_data_pages(struct
 address_space *mapping,
         }
           if (__should_serialize_io(inode, wbc)) {
+        if (!mutex_trylock(&sbi->writepages))
+            blk_flush_plug(((struct task_struct *)current)->plug,
Quoted 9 lines

fs/f2fs/data.c static int __f2fs_write_data_pages(struct · line 3597

Quoted 8 lines
fs/f2fs/data.c +4 -0
 true);
+        else
+            goto set_locked;
+
             mutex_lock(&sbi->writepages);
+set_locked:
             locked = true;
         }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel