Skip to contentSkip to conversation

f2fs: fix potential deadlock in f2fs_convert_inline_inode

PATCH In review Greg Kroah-Hartman 2 participants 4 messages Feb 24 – Mar 12 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: fix potential deadlock in f2fs_convert_inline_inode

f2fs_convert_inline_inode() holds the page lock of the inline data page and then calls f2fs_lock_op(), which acquires cp_rwsem in read mode. At the same time, f2fs_write_checkpoint() can acquire cp_rwsem in write mode and then will wait for page locks, like during f2fs_write_node_pages() or data flushing, leading to a deadlock.

Fix this by acquiring the lock_op before locking the page. This ensures the correct lock ordering, op before page, and avoids the deadlock.

Cc Jaegeuk Kim <[email protected]>
Cc Chao Yu <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

---

This issue was found by running a tool to compare a past kernel CVE to try to find any potential places in the existing codebase that was missed with the original fix. I do not know if this patch or text really is correct, but the code paths seems sane.

Note that the majority of the changelog text came from an untrusted and experimental LLM model that is known for making crap up. So it might be totally lying here, and if so, I am very sorry for wasting anyone's time and I'll just go back to running this on code that I actually understand and know how to verify myself, but I figured it was worth at least asking you all about it.

fs/f2fs/inline.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/inline.c +6 -4
index 0a1052d5ee62..98bc920a4f35 100644
@@ -232,12 +232,14 @@ int f2fs_convert_inline_inode(struct inode *inode)
 	if (err)
 		return err;
 
-	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
-	if (IS_ERR(folio))
-		return PTR_ERR(folio);
-
 	f2fs_lock_op(sbi, &lc);
 
+	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
+	if (IS_ERR(folio)) {
+		f2fs_unlock_op(sbi, &lc);
+		return PTR_ERR(folio);
+	}
+
 	ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
 	if (IS_ERR(ifolio)) {
 		err = PTR_ERR(ifolio);
Signature · 8 lines
2.53.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 5 lines · Greg Kroah-Hartman

f2fs_convert_inline_inode() holds the page lock of the inline data page and then calls f2fs_lock_op(), which acquires cp_rwsem in read mode. At the same time, f2fs_write_checkpoint() can acquire cp_rwsem in write mode and then will wait for page locks, like during f2fs_write_node_pages() or data flushing, leading to a deadlock.

- f2fs_convert_inline_inode - f2fs_write_checkpoint()
- f2fs_grab_cache_folio page #0
- block_operations
- f2fs_lock_all
- f2fs_lock_op
- f2fs_sync_node_pages
- flush_inline_data
- f2fs_filemap_get_folio page #0

It seems true, although I didn't hit such deadlock.

To Jaegeuk, what do you think?

Quoted 36 lines

Fix this by acquiring the lock_op before locking the page. This ensures the correct lock ordering, op before page, and avoids the deadlock.

Cc Jaegeuk Kim <[email protected]>
Cc Chao Yu <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

---

This issue was found by running a tool to compare a past kernel CVE to try to find any potential places in the existing codebase that was missed with the original fix. I do not know if this patch or text really is correct, but the code paths seems sane.

Note that the majority of the changelog text came from an untrusted and experimental LLM model that is known for making crap up. So it might be totally lying here, and if so, I am very sorry for wasting anyone's time and I'll just go back to running this on code that I actually understand and know how to verify myself, but I figured it was worth at least asking you all about it.

fs/f2fs/inline.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/inline.c +0 -2
index 0a1052d5ee62..98bc920a4f35 100644
@@ -232,12 +232,14 @@ int f2fs_convert_inline_inode(struct inode *inode)
 	if (err)
 		return err;
 
-	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
-	if (IS_ERR(folio))

fs/f2fs/inline.c int f2fs_convert_inline_inode(struct inode *inode) · line 242

Quoted 10 lines
fs/f2fs/inline.c +6 -2
-		return PTR_ERR(folio);
-
 	f2fs_lock_op(sbi, &lc);
 
+	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
+	if (IS_ERR(folio)) {
+		f2fs_unlock_op(sbi, &lc);
+		return PTR_ERR(folio);
+	}
+

It will be better to adjust the unlock order in between cp_rwsem and folio lock?

out:

	f2fs_folio_put(folio, true);
	f2fs_unlock_op(sbi, &lc);

Thanks,

fs/f2fs/inline.c int f2fs_convert_inline_inode(struct inode *inode) · line 245

Quoted 3 lines
fs/f2fs/inline.c +0 -0
 	ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
 	if (IS_ERR(ifolio)) {
 		err = PTR_ERR(ifolio);

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

#2 lore raw
Quoted 7 lines · Chao Yu
Quoted 5 lines · Greg Kroah-Hartman

f2fs_convert_inline_inode() holds the page lock of the inline data page and then calls f2fs_lock_op(), which acquires cp_rwsem in read mode. At the same time, f2fs_write_checkpoint() can acquire cp_rwsem in write mode and then will wait for page locks, like during f2fs_write_node_pages() or data flushing, leading to a deadlock.

Quoted 8 lines

- f2fs_convert_inline_inode - f2fs_write_checkpoint()
- f2fs_grab_cache_folio page #0
- block_operations
- f2fs_lock_all
- f2fs_lock_op
- f2fs_sync_node_pages
- flush_inline_data
- f2fs_filemap_get_folio page #0

flush_inline_data -> f2fs_filemap_get_folio(FGP_LOCK|FGP_NOWAIT) does not wait for the lock, right?

Quoted 54 lines

It seems true, although I didn't hit such deadlock.

To Jaegeuk, what do you think?

Quoted 36 lines

Fix this by acquiring the lock_op before locking the page. This ensures the correct lock ordering, op before page, and avoids the deadlock.

Cc Jaegeuk Kim <[email protected]>
Cc Chao Yu <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

---

This issue was found by running a tool to compare a past kernel CVE to try to find any potential places in the existing codebase that was missed with the original fix. I do not know if this patch or text really is correct, but the code paths seems sane.

Note that the majority of the changelog text came from an untrusted and experimental LLM model that is known for making crap up. So it might be totally lying here, and if so, I am very sorry for wasting anyone's time and I'll just go back to running this on code that I actually understand and know how to verify myself, but I figured it was worth at least asking you all about it.

fs/f2fs/inline.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/inline.c +0 -2
index 0a1052d5ee62..98bc920a4f35 100644
@@ -232,12 +232,14 @@ int f2fs_convert_inline_inode(struct inode *inode)
 	if (err)
 		return err;
 
-	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
-	if (IS_ERR(folio))

fs/f2fs/inline.c int f2fs_convert_inline_inode(struct inode *inode) · line 242

Quoted 10 lines
fs/f2fs/inline.c +6 -2
-		return PTR_ERR(folio);
-
 	f2fs_lock_op(sbi, &lc);
 
+	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
+	if (IS_ERR(folio)) {
+		f2fs_unlock_op(sbi, &lc);
+		return PTR_ERR(folio);
+	}
+

It will be better to adjust the unlock order in between cp_rwsem and folio lock?

Quoted 9 lines

out:

	f2fs_folio_put(folio, true);
	f2fs_unlock_op(sbi, &lc);

Thanks,

Quoted 3 lines
fs/f2fs/inline.c +0 -0
 	ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
 	if (IS_ERR(ifolio)) {
 		err = PTR_ERR(ifolio);

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

#3 lore raw
Quoted 17 lines · Jaegeuk Kim
Quoted 7 lines · Chao Yu
Quoted 5 lines · Greg Kroah-Hartman

f2fs_convert_inline_inode() holds the page lock of the inline data page and then calls f2fs_lock_op(), which acquires cp_rwsem in read mode. At the same time, f2fs_write_checkpoint() can acquire cp_rwsem in write mode and then will wait for page locks, like during f2fs_write_node_pages() or data flushing, leading to a deadlock.

Quoted 8 lines

- f2fs_convert_inline_inode - f2fs_write_checkpoint()
- f2fs_grab_cache_folio page #0
- block_operations
- f2fs_lock_all
- f2fs_lock_op
- f2fs_sync_node_pages
- flush_inline_data
- f2fs_filemap_get_folio page #0

Quoted 2 lines

flush_inline_data -> f2fs_filemap_get_folio(FGP_LOCK|FGP_NOWAIT) does not wait for the lock, right?

Oh, I missed that, that explain why I never hit such deadlock. :)

Thanks,

Quoted 59 lines
Quoted 59 lines

It seems true, although I didn't hit such deadlock.

To Jaegeuk, what do you think?

Quoted 36 lines

Fix this by acquiring the lock_op before locking the page. This ensures the correct lock ordering, op before page, and avoids the deadlock.

Cc Jaegeuk Kim <[email protected]>
Cc Chao Yu <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

---

This issue was found by running a tool to compare a past kernel CVE to try to find any potential places in the existing codebase that was missed with the original fix. I do not know if this patch or text really is correct, but the code paths seems sane.

Note that the majority of the changelog text came from an untrusted and experimental LLM model that is known for making crap up. So it might be totally lying here, and if so, I am very sorry for wasting anyone's time and I'll just go back to running this on code that I actually understand and know how to verify myself, but I figured it was worth at least asking you all about it.

fs/f2fs/inline.c | 10 ++++++----

  1 file changed, 6 insertions(+), 4 deletions(-)
fs/f2fs/inline.c +0 -2
index 0a1052d5ee62..98bc920a4f35 100644
@@ -232,12 +232,14 @@ int f2fs_convert_inline_inode(struct inode *inode)
  	if (err)
  		return err;
 
-	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
-	if (IS_ERR(folio))

fs/f2fs/inline.c int f2fs_convert_inline_inode(struct inode *inode) · line 242

Quoted 10 lines
fs/f2fs/inline.c +6 -2
-		return PTR_ERR(folio);
-
  	f2fs_lock_op(sbi, &lc);
 
+	folio = f2fs_grab_cache_folio(inode->i_mapping, 0, false);
+	if (IS_ERR(folio)) {
+		f2fs_unlock_op(sbi, &lc);
+		return PTR_ERR(folio);
+	}
+

It will be better to adjust the unlock order in between cp_rwsem and folio lock?

out:

	f2fs_folio_put(folio, true);
	f2fs_unlock_op(sbi, &lc);
Quoted 5 lines
Quoted 5 lines

Thanks,

Quoted 3 lines
fs/f2fs/inline.c +0 -0
  	ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
  	if (IS_ERR(ifolio)) {
  		err = PTR_ERR(ifolio);

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