Skip to contentSkip to conversation

f2fs: fix race between write_checkpoint and write_begin

PATCH Superseded Sheng Yong 4 participants 4 messages Nov 12, 2018 – Nov 13, 2018 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sheng Yong

author
#0 lore raw

PATCH 1/1 f2fs: fix race between write_checkpoint and write_begin

The following race could lead to inconsistent SIT bitmap:

Task A Task B
====== ======
f2fs_write_checkpoint
block_operations

    f2fs_lock_all
      down_write(node_change)
      down_write(node_write)
      ... sync ...
      up_write(node_change)
                                f2fs_file_write_iter
                                  set_inode_flag(FI_NO_PREALLOC)
                                  ......
                                  f2fs_write_begin(index=0, has inline data)
                                    prepare_write_begin
                                      __do_map_lock(AIO) => down_read(node_change)
                                      f2fs_convert_inline_page => update SIT
                                      __do_map_lock(AIO) => up_read(node_change)

f2fs_flush_sit_entries <= inconsistent SIT
finish write checkpoint
sudden-power-off

If SPO occurs after checkpoint is finished, SIT bitmap will be set incorrectly. This patch uses node_write to avoid the race condition.

---
 fs/f2fs/data.c | 2 ++
 1 file changed, 2 insertions(+)
fs/f2fs/data.c +2 -0
index 106f116466bf..dc5b251aee86 100644
@@ -2355,7 +2355,9 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi,
 			if (inode->i_nlink)
 				set_inline_node(ipage);
 		} else {
+			down_read(&sbi->node_write);
 			err = f2fs_convert_inline_page(&dn, page);
+			up_read(&sbi->node_write);
 			if (err)
 				goto out;
 			if (dn.data_blkaddr == NULL_ADDR)
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 19 lines · Sheng Yong

The following race could lead to inconsistent SIT bitmap:

Task A Task B
====== ======
f2fs_write_checkpoint
block_operations

    f2fs_lock_all
      down_write(node_change)
      down_write(node_write)
      ... sync ...
      up_write(node_change)
                                f2fs_file_write_iter
                                  set_inode_flag(FI_NO_PREALLOC)
                                  ......
                                  f2fs_write_begin(index=0, has inline data)
                                    prepare_write_begin
                                      __do_map_lock(AIO) => down_read(node_change)
                                      f2fs_convert_inline_page => update SIT
                                      __do_map_lock(AIO) => up_read(node_change)
Quoted 6 lines

f2fs_flush_sit_entries <= inconsistent SIT
finish write checkpoint
sudden-power-off

If SPO occurs after checkpoint is finished, SIT bitmap will be set incorrectly. This patch uses node_write to avoid the race condition.

Good catch!

Quoted 11 lines
---
 fs/f2fs/data.c | 2 ++
 1 file changed, 2 insertions(+)
fs/f2fs/data.c +0 -0
index 106f116466bf..dc5b251aee86 100644

fs/f2fs/data.c static int prepare_write_begin(struct f2fs_sb_info *sbi, · line 2358

Quoted 4 lines
fs/f2fs/data.c +1 -0
@@ -2355,7 +2355,9 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi,
 			if (inode->i_nlink)
 				set_inline_node(ipage);
 		} else {
+			down_read(&sbi->node_write);

This makes lock dependence more complicated, how about calling f2fs_lock_op for inline data conversion case:

        struct extent_info ei = {0,0,0};
+       int flag;
        int err = 0;

- if (f2fs_has_inline_data(inode) ||
- (pos & PAGE_MASK) >= i_size_read(inode)) {
- __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
+ if (f2fs_has_inline_data(inode)) {
+ if (pos + len > MAX_INLINE_DATA(inode))
+ flag = F2FS_GET_BLOCK_PRE_DIO;
+ else if (pos & PAGE_MASK) >= i_size_read(inode))
+ flag = F2FS_GET_BLOCK_PRE_AIO;
+ __do_map_lock(sbi, flag, true);

                        if (err || dn.data_blkaddr == NULL_ADDR) {
                                f2fs_put_dnode(&dn);
-                               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
-                                                               true);
+                               flag = F2FS_GET_BLOCK_PRE_AIO;
+                               __do_map_lock(sbi, flag, true);
                                locked = true;

unlock_out:
if (locked)
- __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
+ __do_map_lock(sbi, flag, false);

Thanks,

Quoted 6 lines
fs/f2fs/data.c +1 -0
 			err = f2fs_convert_inline_page(&dn, page);
+			up_read(&sbi->node_write);
 			if (err)
 				goto out;
 			if (dn.data_blkaddr == NULL_ADDR)
maintainer
#2 lore raw
Quoted 49 lines · Chao Yu
Quoted 19 lines · Sheng Yong

The following race could lead to inconsistent SIT bitmap:

Task A Task B
====== ======
f2fs_write_checkpoint
block_operations

    f2fs_lock_all
      down_write(node_change)
      down_write(node_write)
      ... sync ...
      up_write(node_change)
                                f2fs_file_write_iter
                                  set_inode_flag(FI_NO_PREALLOC)
                                  ......
                                  f2fs_write_begin(index=0, has inline data)
                                    prepare_write_begin
                                      __do_map_lock(AIO) => down_read(node_change)
                                      f2fs_convert_inline_page => update SIT
                                      __do_map_lock(AIO) => up_read(node_change)
Quoted 6 lines

f2fs_flush_sit_entries <= inconsistent SIT
finish write checkpoint
sudden-power-off

If SPO occurs after checkpoint is finished, SIT bitmap will be set incorrectly. This patch uses node_write to avoid the race condition.

Good catch!

Quoted 11 lines
---
 fs/f2fs/data.c | 2 ++
 1 file changed, 2 insertions(+)
fs/f2fs/data.c +0 -0
index 106f116466bf..dc5b251aee86 100644

fs/f2fs/data.c static int prepare_write_begin(struct f2fs_sb_info *sbi, · line 2358

Quoted 4 lines
fs/f2fs/data.c +1 -0
@@ -2355,7 +2355,9 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi,
 			if (inode->i_nlink)
 				set_inline_node(ipage);
 		} else {
+			down_read(&sbi->node_write);

This makes lock dependence more complicated, how about calling f2fs_lock_op for inline data conversion case:

        struct extent_info ei = {0,0,0};

Review

Quoted 9 lines
(quoted diff) +4 -3
+       int flag;
        int err = 0;
 
-       if (f2fs_has_inline_data(inode) ||
-                       (pos & PAGE_MASK) >= i_size_read(inode)) {
-               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
+       if (f2fs_has_inline_data(inode)) {
+               if (pos + len > MAX_INLINE_DATA(inode))
+                       flag = F2FS_GET_BLOCK_PRE_DIO;

This is not DIO case. I think calling f2fs_lock_op() explicitly might be better.

Quoted 25 lines
(quoted diff) +6 -3
+               else if (pos & PAGE_MASK) >= i_size_read(inode))
+                       flag = F2FS_GET_BLOCK_PRE_AIO;
+               __do_map_lock(sbi, flag, true);
 
                        if (err || dn.data_blkaddr == NULL_ADDR) {
                                f2fs_put_dnode(&dn);
-                               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
-                                                               true);
+                               flag = F2FS_GET_BLOCK_PRE_AIO;
+                               __do_map_lock(sbi, flag, true);
                                locked = true;
 
 unlock_out:
        if (locked)
-               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
+               __do_map_lock(sbi, flag, false);

Thanks,

Quoted 6 lines
fs/f2fs/data.c +1 -0
 			err = f2fs_convert_inline_page(&dn, page);
+			up_read(&sbi->node_write);
 			if (err)
 				goto out;
 			if (dn.data_blkaddr == NULL_ADDR)
#3 lore raw
Quoted 53 lines · Jaegeuk Kim
Quoted 52 lines · Chao Yu
Quoted 19 lines · Sheng Yong

The following race could lead to inconsistent SIT bitmap:

Task A Task B
====== ======
f2fs_write_checkpoint
block_operations

    f2fs_lock_all
      down_write(node_change)
      down_write(node_write)
      ... sync ...
      up_write(node_change)
                                f2fs_file_write_iter
                                  set_inode_flag(FI_NO_PREALLOC)
                                  ......
                                  f2fs_write_begin(index=0, has inline data)
                                    prepare_write_begin
                                      __do_map_lock(AIO) => down_read(node_change)
                                      f2fs_convert_inline_page => update SIT
                                      __do_map_lock(AIO) => up_read(node_change)
Quoted 6 lines

f2fs_flush_sit_entries <= inconsistent SIT
finish write checkpoint
sudden-power-off

If SPO occurs after checkpoint is finished, SIT bitmap will be set incorrectly. This patch uses node_write to avoid the race condition.

Good catch!

Quoted 11 lines
---
 fs/f2fs/data.c | 2 ++
 1 file changed, 2 insertions(+)
fs/f2fs/data.c +0 -0
index 106f116466bf..dc5b251aee86 100644

fs/f2fs/data.c static int prepare_write_begin(struct f2fs_sb_info *sbi, · line 2358

Quoted 4 lines
fs/f2fs/data.c +1 -0
@@ -2355,7 +2355,9 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi,
 			if (inode->i_nlink)
 				set_inline_node(ipage);
 		} else {
+			down_read(&sbi->node_write);

This makes lock dependence more complicated, how about calling f2fs_lock_op for inline data conversion case:

        struct extent_info ei = {0,0,0};
+       int flag;
        int err = 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +3 -3
-       if (f2fs_has_inline_data(inode) ||
-                       (pos & PAGE_MASK) >= i_size_read(inode)) {
-               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
+       if (f2fs_has_inline_data(inode)) {
+               if (pos + len > MAX_INLINE_DATA(inode))
+                       flag = F2FS_GET_BLOCK_PRE_DIO;

This is not DIO case. I think calling f2fs_lock_op() explicitly might be better.

Agreed.

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+               else if (pos & PAGE_MASK) >= i_size_read(inode))
+                       flag = F2FS_GET_BLOCK_PRE_AIO;

Sorry, there is a missing case that @flag doesn't be initialized, please note that, Sheng Yong.

Thanks,

Quoted 26 lines
Quoted 15 lines
(quoted diff) +4 -3
+               __do_map_lock(sbi, flag, true);
 
                        if (err || dn.data_blkaddr == NULL_ADDR) {
                                f2fs_put_dnode(&dn);
-                               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
-                                                               true);
+                               flag = F2FS_GET_BLOCK_PRE_AIO;
+                               __do_map_lock(sbi, flag, true);
                                locked = true;
 
 unlock_out:
        if (locked)
-               __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
+               __do_map_lock(sbi, flag, false);
Quoted 8 lines

Thanks,

fs/f2fs/data.c static int prepare_write_begin(struct f2fs_sb_info *sbi, · line 2363

Quoted 6 lines
fs/f2fs/data.c +1 -0
 			err = f2fs_convert_inline_page(&dn, page);
+			up_read(&sbi->node_write);
 			if (err)
 				goto out;
 			if (dn.data_blkaddr == NULL_ADDR)

.