Skip to contentSkip to conversation

Possible fs corruption when hole punch races with other ops

Ocfs2-devel Jan Kara 4 participants 7 messages Apr 21, 2021 – Apr 22, 2021 OCFS2

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

Signature · 7 lines
Jan Kara <[email protected]>
SUSE Labs, CR

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel
maintainer
#1 lore raw

Hi,

Checked the code flow, it seems the race you worried truly exists. We have to take ip_alloc_sem before calling into ocfs2_get_block().

Thanks,
Joseph

Quoted 14 lines · Jan Kara

Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Quoted 6 lines

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

author
#2 lore raw

Hello!

Quoted 2 lines · Joseph Qi

Checked the code flow, it seems the race you worried truly exists. We have to take ip_alloc_sem before calling into ocfs2_get_block().

OK, that's what I assumed but this won't be easy. Because ocfs2_writepage() is called with a page locked but ocfs2_remove_inode_range() gets called under ip_alloc_sem and locks pages in ocfs2_truncate_cluster_pages() -> truncate_inode_pages_range(). Which creates ABBA deadlock. So you'll probably need to come up with a similar dance like in ocfs2_readpage().

But after fixing this, ip_alloc_sem should provide you with enough protection so that ocfs2 isn't prone to races between hole punch and readahead / fault my work will be mostly irrelevant for OCFS2.

Thanks for confirmation!

Honza

Quoted 21 lines · Jan Kara
Quoted 20 lines

Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

Signature · 7 lines
Jan Kara <[email protected]>
SUSE Labs, CR

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel
#3 lore raw

Hi Honza,

By “hole punching operation”, you meant deallocate some data blocks from a file and keep the file size unchanged, after that operation some “holes" are made in that file, right? I am just curious that do we really have end user programs that do hole punching on files? Or hole punching is only used for development purpose?

thanks,
wengang

On Apr 22, 2021, at 3:44 AM, Jan Kara <[email protected]<mailto:[email protected]>> wrote:

Hello!

On Thu 22-04-21 11:22:07, Joseph Qi wrote:
Checked the code flow, it seems the race you worried truly exists. We have to take ip_alloc_sem before calling into ocfs2_get_block().

OK, that's what I assumed but this won't be easy. Because ocfs2_writepage() is called with a page locked but ocfs2_remove_inode_range() gets called under ip_alloc_sem and locks pages in ocfs2_truncate_cluster_pages() -> truncate_inode_pages_range(). Which creates ABBA deadlock. So you'll probably need to come up with a similar dance like in ocfs2_readpage().

But after fixing this, ip_alloc_sem should provide you with enough protection so that ocfs2 isn't prone to races between hole punch and readahead / fault my work will be mostly irrelevant for OCFS2.

Thanks for confirmation!

Honza

On 4/22/21 12:29 AM, Jan Kara wrote:
Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

--
Jan Kara <[email protected]<mailto:[email protected]>>
SUSE Labs, CR

_______________________________________________
Ocfs2-devel mailing list [email protected]<mailto:[email protected]>
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

#4 lore raw
Quoted 8 lines · Wengang Wang

Hi Honza,

By “hole punching operation”, you meant deallocate some data blocks from a file and keep the file size unchanged, after that operation some “holes" are made in that file, right?
I am just curious that do we really have end user programs that do hole punching on files? Or hole punching is only used for development purpose?

Yes, we do. QEMU does it to image files, various $database products, etc...

--D

Quoted 57 lines

thanks,
wengang

On Apr 22, 2021, at 3:44 AM, Jan Kara <[email protected]<mailto:[email protected]>> wrote:

Hello!

On Thu 22-04-21 11:22:07, Joseph Qi wrote:
Checked the code flow, it seems the race you worried truly exists. We have to take ip_alloc_sem before calling into ocfs2_get_block().

OK, that's what I assumed but this won't be easy. Because ocfs2_writepage() is called with a page locked but ocfs2_remove_inode_range() gets called under ip_alloc_sem and locks pages in ocfs2_truncate_cluster_pages() -> truncate_inode_pages_range(). Which creates ABBA deadlock. So you'll probably need to come up with a similar dance like in ocfs2_readpage().

But after fixing this, ip_alloc_sem should provide you with enough protection so that ocfs2 isn't prone to races between hole punch and readahead / fault my work will be mostly irrelevant for OCFS2.

Thanks for confirmation!

Honza

On 4/22/21 12:29 AM, Jan Kara wrote:
Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

--
Jan Kara <[email protected]<mailto:[email protected]>>
SUSE Labs, CR

_______________________________________________
Ocfs2-devel mailing list [email protected]<mailto:[email protected]>
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

Quoted 4 lines

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

#5 lore raw
Quoted 7 lines · Darrick J. Wong
Quoted 2 lines · Wengang Wang

Hi Honza,

Quoted 8 lines
Quoted 6 lines

By “hole punching operation”, you meant deallocate some data blocks from a file and keep the file size unchanged, after that operation some “holes" are made in that file, right?
I am just curious that do we really have end user programs that do hole punching on files? Or hole punching is only used for development purpose?

Yes, we do. QEMU does it to image files, various $database products,

Hm, to run a hole punching, that means the data blocks to deallocate were useful but now are no longer useful. For QEMU, it does hole punching at guest run time for guest FS trims? Don’t know in what cases DBs make use of hole punching. Well, good to know.

Thanks Darrick.

wengang

Quoted 66 lines

etc...

--D

Quoted 61 lines

thanks,
wengang

On Apr 22, 2021, at 3:44 AM, Jan Kara <[email protected]<mailto:[email protected]>> wrote:

Hello!

On Thu 22-04-21 11:22:07, Joseph Qi wrote:
Checked the code flow, it seems the race you worried truly exists. We have to take ip_alloc_sem before calling into ocfs2_get_block().

OK, that's what I assumed but this won't be easy. Because ocfs2_writepage() is called with a page locked but ocfs2_remove_inode_range() gets called under ip_alloc_sem and locks pages in ocfs2_truncate_cluster_pages() -> truncate_inode_pages_range(). Which creates ABBA deadlock. So you'll probably need to come up with a similar dance like in ocfs2_readpage().

But after fixing this, ip_alloc_sem should provide you with enough protection so that ocfs2 isn't prone to races between hole punch and readahead / fault my work will be mostly irrelevant for OCFS2.

Thanks for confirmation!

Honza

On 4/22/21 12:29 AM, Jan Kara wrote:
Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

--
Jan Kara <[email protected]<mailto:[email protected]>>
SUSE Labs, CR

_______________________________________________
Ocfs2-devel mailing list [email protected]<mailto:[email protected]>
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

#6 lore raw
Quoted 14 lines · Wengang Wang
Quoted 14 lines · Darrick J. Wong
Quoted 8 lines · Wengang Wang

Hi Honza,

By “hole punching operation”, you meant deallocate some data blocks from a file and keep the file size unchanged, after that operation some “holes" are made in that file, right?
I am just curious that do we really have end user programs that do hole punching on files? Or hole punching is only used for development purpose?

Quoted 5 lines
Quoted 1 line

Yes, we do. QEMU does it to image files, various $database products,

Hm, to run a hole punching, that means the data blocks to deallocate were useful but now are no longer useful.
For QEMU, it does hole punching at guest run time for guest FS trims?

Yes. SCSI discard commands are implemented as hole punching if the backing device is a raw file on a filesystem.

--D

Quoted 74 lines

Don’t know in what cases DBs make use of hole punching. Well, good to know.

Thanks Darrick.

wengang

Quoted 66 lines

etc...

--D

Quoted 61 lines

thanks,
wengang

On Apr 22, 2021, at 3:44 AM, Jan Kara <[email protected]<mailto:[email protected]>> wrote:

Hello!

On Thu 22-04-21 11:22:07, Joseph Qi wrote:
Checked the code flow, it seems the race you worried truly exists. We have to take ip_alloc_sem before calling into ocfs2_get_block().

OK, that's what I assumed but this won't be easy. Because ocfs2_writepage() is called with a page locked but ocfs2_remove_inode_range() gets called under ip_alloc_sem and locks pages in ocfs2_truncate_cluster_pages() -> truncate_inode_pages_range(). Which creates ABBA deadlock. So you'll probably need to come up with a similar dance like in ocfs2_readpage().

But after fixing this, ip_alloc_sem should provide you with enough protection so that ocfs2 isn't prone to races between hole punch and readahead / fault my work will be mostly irrelevant for OCFS2.

Thanks for confirmation!

Honza

On 4/22/21 12:29 AM, Jan Kara wrote:
Hello,

I'm unifying protection various filesystems use to protect hole punch operations from racing with other operations (like readahead, page fault, writepage etc.). I was looking into OCFS2 and I think it is prone to a following race which can possibly lead to filesystem corruption. But maybe I miss something so that's why I'm writing here. The scenario I'm concerned about is:

CPU1 CPU2
ocfs2_remove_inode_range() ocfs2_writepage()
... block_write_full_page()
ocfs2_remove_btree_range() ocfs2_extent_map_get_blocks()

Now ocfs2_extent_map_get_blocks() runs without protection of ip_alloc_sem AFAICT and so both these operations can be modifying extent map at the same time? What am I missing?

Honza

--
Jan Kara <[email protected]<mailto:[email protected]>>
SUSE Labs, CR

_______________________________________________
Ocfs2-devel mailing list [email protected]<mailto:[email protected]>
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel