Skip to contentSkip to conversation

dm thin: superblock may write succeed before other metadata blocks because of wirting metadata in async mode.

DISCUSSION Mike Snitzer 3 participants 7 messages Jun 19, 2018 – Jun 21, 2018 2 messages not in the archive Device mapper

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20180522005336.GA30152@yyp.

#1 lore raw
Quoted 35 lines · Monty Pavel

If dm_bufio_write_dirty_buffers func is called by __commit_transaction func and power loss happens during executing it, coincidencely superblock wrote correctly but some metadata blocks didn't. The reason is we write all metadata in async mode. We can guarantee that we send superblock after other blocks but we cannot guarantee that superblock write completely early than other blocks.
So, We need to commit other metadata blocks before change superblock.

---
 drivers/md/dm-thin-metadata.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/md/dm-thin-metadata.c +8 -0
index 36ef284..897d7d6 100644
@@ -813,6 +813,14 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 	if (r)
 		return r;
 
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r)
+		return r;
+
+	r = superblock_lock(pmd, &sblock);
+	if (r)
+		return r;
+
 	disk_super = dm_block_data(sblock);
 	disk_super->time = cpu_to_le32(pmd->time);
 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);
Signature · 1 line
1.7.1

Have you actually found this patch to be effective? It should be unnecessary. But I must admit that in looking at the related code I couldn't convince myself it was.

But then Joe pointed me to this comment block from dm-transaction-manager.h:

/*
* We use a 2-phase commit here.
*
* i) Make all changes for the transaction *except* for the superblock.
* Then call dm_tm_pre_commit() to flush them to disk.
*
* ii) Lock your superblock. Update. Then call dm_tm_commit() which will
* unlock the superblock and flush it. No other blocks should be updated
* during this period. Care should be taken to never unlock a partially
* updated superblock; perform any operations that could fail *before* you
* take the superblock lock.
*/
int dm_tm_pre_commit(struct dm_transaction_manager *tm);
int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *superblock);

So given __commit_transaction() is using dm_tm_pre_commit() prior to the dm_tm_commit() to flush the superblock -- it would seem that there isn't any conceptual potential for corruption.

If you've found the dm_tm_pre_commit() to be lacking (whereby not all metadata getting flushed to disk before the superblock) then please explain your findings.

Thanks,
Mike

#2 lore raw
Quoted 25 lines · Mike Snitzer
Quoted 22 lines · Monty Pavel

If dm_bufio_write_dirty_buffers func is called by __commit_transaction func and power loss happens during executing it, coincidencely superblock wrote correctly but some metadata blocks didn't. The reason is we write all metadata in async mode. We can guarantee that we send superblock after other blocks but we cannot guarantee that superblock write completely early than other blocks.
So, We need to commit other metadata blocks before change superblock.

---
 drivers/md/dm-thin-metadata.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/md/dm-thin-metadata.c +0 -0
index 36ef284..897d7d6 100644
@@ -813,6 +813,14 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 	if (r)
 		return r;
 
Quoted 11 lines

drivers/md/dm-thin-metadata.c static int __commit_transaction(struct dm_pool_metadata *pmd) · line 826

Quoted 11 lines
drivers/md/dm-thin-metadata.c +8 -0
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r)
+		return r;
+
+	r = superblock_lock(pmd, &sblock);
+	if (r)
+		return r;
+
 	disk_super = dm_block_data(sblock);
 	disk_super->time = cpu_to_le32(pmd->time);
 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);

I don't believe you've tested this; sblock is passed to dm_tm_commit() uninitialised, and you didn't even bother to remove the later (and correct) call to dm_tm_commit(). See dm-transaction-manager.h for an explanation of how the 2-phase commit works.

What is the issue that started you looking in this area?

- Joe

#3 lore raw
Quoted 26 lines · Joe Thornber
Quoted 25 lines · Mike Snitzer
Quoted 22 lines · Monty Pavel

If dm_bufio_write_dirty_buffers func is called by __commit_transaction func and power loss happens during executing it, coincidencely superblock wrote correctly but some metadata blocks didn't. The reason is we write all metadata in async mode. We can guarantee that we send superblock after other blocks but we cannot guarantee that superblock write completely early than other blocks.
So, We need to commit other metadata blocks before change superblock.

---
 drivers/md/dm-thin-metadata.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/md/dm-thin-metadata.c +0 -0
index 36ef284..897d7d6 100644
@@ -813,6 +813,14 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 	if (r)
 		return r;
 
Quoted 15 lines
Quoted 11 lines

drivers/md/dm-thin-metadata.c static int __commit_transaction(struct dm_pool_metadata *pmd) · line 826

Quoted 11 lines
drivers/md/dm-thin-metadata.c +8 -0
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r)
+		return r;
+
+	r = superblock_lock(pmd, &sblock);
+	if (r)
+		return r;
+
 	disk_super = dm_block_data(sblock);
 	disk_super->time = cpu_to_le32(pmd->time);
 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);

I don't believe you've tested this; sblock is passed to dm_tm_commit() uninitialised, and you didn't even bother to remove the later (and correct) call to dm_tm_commit().

I pointed out to Joe that the patch, in isolation, is decieving. It _looks_ like sblock may be uninitialized, etc. But once the patch is applied and you look at the entirety of __commit_transaction() it is clear that you're reusing the existing superblock_lock() to safely accomplish your additional call to dm_tm_commit().

Quoted 1 line

What is the issue that started you looking in this area?

Right, as my previous reply asked: please clarify if you _know_ your patch fixes an actual problem you've experienced. The more details the better.

Thanks,
Mike

A message that is not in the archive (sent off-list or not yet received): 20180620170357.GA5838@yyp.

#5 lore raw
Quoted 63 lines · monty
Quoted 52 lines · Mike Snitzer
Quoted 26 lines · Joe Thornber
Quoted 25 lines · Mike Snitzer
Quoted 22 lines · Monty Pavel

If dm_bufio_write_dirty_buffers func is called by __commit_transaction func and power loss happens during executing it, coincidencely superblock wrote correctly but some metadata blocks didn't. The reason is we write all metadata in async mode. We can guarantee that we send superblock after other blocks but we cannot guarantee that superblock write completely early than other blocks.
So, We need to commit other metadata blocks before change superblock.

---
 drivers/md/dm-thin-metadata.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/md/dm-thin-metadata.c +0 -0
index 36ef284..897d7d6 100644
@@ -813,6 +813,14 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 	if (r)
 		return r;
 
Quoted 15 lines
Quoted 11 lines

drivers/md/dm-thin-metadata.c static int __commit_transaction(struct dm_pool_metadata *pmd) · line 826

Quoted 11 lines
drivers/md/dm-thin-metadata.c +8 -0
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r)
+		return r;
+
+	r = superblock_lock(pmd, &sblock);
+	if (r)
+		return r;
+
 	disk_super = dm_block_data(sblock);
 	disk_super->time = cpu_to_le32(pmd->time);
 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);

I don't believe you've tested this; sblock is passed to dm_tm_commit() uninitialised, and you didn't even bother to remove the later (and correct) call to dm_tm_commit().

I pointed out to Joe that the patch, in isolation, is decieving. It _looks_ like sblock may be uninitialized, etc. But once the patch is applied and you look at the entirety of __commit_transaction() it is clear that you're reusing the existing superblock_lock() to safely accomplish your additional call to dm_tm_commit().

Quoted 9 lines
Quoted 1 line

What is the issue that started you looking in this area?

Right, as my previous reply asked: please clarify if you _know_ your patch fixes an actual problem you've experienced. The more details the better.

Thanks,
Mike

Quoted 9 lines

Hi, Mike and Joe. Thanks for your reply. I read __commit_transaction many times and didn't find any problem of 2-phase commit. I use md-raid1(PCIe nvme and md-raid5) in write-behind mode to store dm-thin metadata.
Test case:
1. I do copy-diff test on thin device and then reboot my machine.
2. Rebuild our device stack and exec "vgchang -ay".
The thin-pool can not be established(details_root become a bitmap node and metadata's bitmap_root become a btree_node).

But are you saying your double commit in __commit_transaction() serves as a workaround for the corruption you're seeing?

Is it just a case where raid5's writebehind mode is _not_ safe for your storage config? By "reboot" do you mean a clean shutdown? Or a forced powerfail scenario?

Mike

#6 lore raw
Quoted 77 lines · Mike Snitzer
Quoted 63 lines · monty
Quoted 52 lines · Mike Snitzer
Quoted 26 lines · Joe Thornber
Quoted 25 lines · Mike Snitzer
Quoted 22 lines · Monty Pavel

If dm_bufio_write_dirty_buffers func is called by __commit_transaction func and power loss happens during executing it, coincidencely superblock wrote correctly but some metadata blocks didn't. The reason is we write all metadata in async mode. We can guarantee that we send superblock after other blocks but we cannot guarantee that superblock write completely early than other blocks.
So, We need to commit other metadata blocks before change superblock.

---
 drivers/md/dm-thin-metadata.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/md/dm-thin-metadata.c +0 -0
index 36ef284..897d7d6 100644
@@ -813,6 +813,14 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 	if (r)
 		return r;
 
Quoted 15 lines
Quoted 11 lines

drivers/md/dm-thin-metadata.c static int __commit_transaction(struct dm_pool_metadata *pmd) · line 826

Quoted 11 lines
drivers/md/dm-thin-metadata.c +8 -0
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r)
+		return r;
+
+	r = superblock_lock(pmd, &sblock);
+	if (r)
+		return r;
+
 	disk_super = dm_block_data(sblock);
 	disk_super->time = cpu_to_le32(pmd->time);
 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);

I don't believe you've tested this; sblock is passed to dm_tm_commit() uninitialised, and you didn't even bother to remove the later (and correct) call to dm_tm_commit().

I pointed out to Joe that the patch, in isolation, is decieving. It _looks_ like sblock may be uninitialized, etc. But once the patch is applied and you look at the entirety of __commit_transaction() it is clear that you're reusing the existing superblock_lock() to safely accomplish your additional call to dm_tm_commit().

Quoted 9 lines
Quoted 1 line

What is the issue that started you looking in this area?

Right, as my previous reply asked: please clarify if you _know_ your patch fixes an actual problem you've experienced. The more details the better.

Thanks,
Mike

Quoted 9 lines

Hi, Mike and Joe. Thanks for your reply. I read __commit_transaction many times and didn't find any problem of 2-phase commit. I use md-raid1(PCIe nvme and md-raid5) in write-behind mode to store dm-thin metadata.
Test case:
1. I do copy-diff test on thin device and then reboot my machine.
2. Rebuild our device stack and exec "vgchang -ay".
The thin-pool can not be established(details_root become a bitmap node and metadata's bitmap_root become a btree_node).

Quoted 9 lines

But are you saying your double commit in __commit_transaction() serves as a workaround for the corruption you're seeing?

Is it just a case where raid5's writebehind mode is _not_ safe for your storage config? By "reboot" do you mean a clean shutdown? Or a forced powerfail scenario?

Mike

Reboot my machine means exec reboot command.
My patch seems unnecessary, because 2-phase commit have ensure that committing superblock after other metadata have written to metadata device.
This problem is hard to recreate, it didn't happen after applying the patch above. I will check my device stack if there is any possible that superblock write complete early than other metadata block.

Monty

#7 lore raw
In reply to a message not in the archive
Quoted 9 lines · monty

Hi, Mike and Joe. Thanks for your reply. I read __commit_transaction many times and didn't find any problem of 2-phase commit. I use md-raid1(PCIe nvme and md-raid5) in write-behind mode to store dm-thin metadata.
Test case:
1. I do copy-diff test on thin device and then reboot my machine.
2. Rebuild our device stack and exec "vgchang -ay".
The thin-pool can not be established(details_root become a bitmap node and metadata's bitmap_root become a btree_node).

As you simplify your setup does the problem go away? eg, turn off write-behind, use just the nvme dev etc.

The only effect of your change is to call flush twice rather than once.

- Joe

#8 lore raw
In reply to Mike Snitzer
Quoted 52 lines · Mike Snitzer
Quoted 26 lines · Joe Thornber
Quoted 25 lines · Mike Snitzer
Quoted 22 lines · Monty Pavel

If dm_bufio_write_dirty_buffers func is called by __commit_transaction func and power loss happens during executing it, coincidencely superblock wrote correctly but some metadata blocks didn't. The reason is we write all metadata in async mode. We can guarantee that we send superblock after other blocks but we cannot guarantee that superblock write completely early than other blocks.
So, We need to commit other metadata blocks before change superblock.

---
 drivers/md/dm-thin-metadata.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/md/dm-thin-metadata.c +0 -0
index 36ef284..897d7d6 100644
@@ -813,6 +813,14 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 	if (r)
 		return r;
 
Quoted 15 lines
Quoted 11 lines

drivers/md/dm-thin-metadata.c static int __commit_transaction(struct dm_pool_metadata *pmd) · line 826

Quoted 11 lines
drivers/md/dm-thin-metadata.c +8 -0
+	r = dm_tm_commit(pmd->tm, sblock);
+	if (r)
+		return r;
+
+	r = superblock_lock(pmd, &sblock);
+	if (r)
+		return r;
+
 	disk_super = dm_block_data(sblock);
 	disk_super->time = cpu_to_le32(pmd->time);
 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);

I don't believe you've tested this; sblock is passed to dm_tm_commit() uninitialised, and you didn't even bother to remove the later (and correct) call to dm_tm_commit().

I pointed out to Joe that the patch, in isolation, is decieving. It _looks_ like sblock may be uninitialized, etc. But once the patch is applied and you look at the entirety of __commit_transaction() it is clear that you're reusing the existing superblock_lock() to safely accomplish your additional call to dm_tm_commit().

Quoted 9 lines
Quoted 1 line

What is the issue that started you looking in this area?

Right, as my previous reply asked: please clarify if you _know_ your patch fixes an actual problem you've experienced. The more details the better.

Thanks,
Mike

Hi, Mike and Joe. Thanks for your reply. I read __commit_transaction many times and didn't find any problem of 2-phase commit. I use md-raid1(PCIe nvme and md-raid5) in write-behind mode to store dm-thin metadata.
Test case:
1. I do copy-diff test on thin device and then reboot my machine.
2. Rebuild our device stack and exec "vgchang -ay".
The thin-pool can not be established(details_root become a bitmap node and metadata's bitmap_root become a btree_node).

Thanks,
Monty