Skip to contentSkip to conversation

btrfs: qgroup: don't commit transaction when we have already hold a transaction handler

PATCH Superseded Qu Wenruo 2 participants 2 messages Nov 11, 2020 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: qgroup: don't commit transaction when we have already hold a transaction handler

[BUG]
There is a report about v5.9.6 hitting ASSERT() when running fstests:

assertion failed: refcount_read(&trans->use_count) == 1, in fs/btrfs/transaction.c:2022

 ------------[ cut here ]------------
 kernel BUG at fs/btrfs/ctree.h:3230!
 invalid opcode: 0000 [#1] SMP PTI
 RIP: 0010:assertfail.constprop.0+0x18/0x1a [btrfs]
  btrfs_commit_transaction.cold+0x11/0x5d [btrfs]
  try_flush_qgroup+0x67/0x100 [btrfs]
  __btrfs_qgroup_reserve_meta+0x3a/0x60 [btrfs]
  btrfs_delayed_update_inode+0xaa/0x350 [btrfs]
  btrfs_update_inode+0x9d/0x110 [btrfs]
  btrfs_dirty_inode+0x5d/0xd0 [btrfs]
  touch_atime+0xb5/0x100
  iterate_dir+0xf1/0x1b0
  __x64_sys_getdents64+0x78/0x110
  do_syscall_64+0x33/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
 RIP: 0033:0x7fb5afe588db

[CAUSE]
In try_flush_qgroup(), we assume we don't hold a transaction handler at all.
This is true for data reservation and mostly true for metadata. Since data space reservation always happens before we start a transaction, and for most metadata operation we reserve space in start_transaction().

But there is an exception, btrfs_delayed_inode_reserve_metadata(). It holds a transaction handler, while still try to reserve extra metadata space.

When we hit -EDQUOT inside btrfs_delayed_inode_reserve_metadata(), we will join into current transaction, and commit current transaction, while we still have transaction handler out of qgroup code.

[FIX]
Let's check current->journal before we join the transaction.

If current->journal is empty or BTRFS_SEND_TRANS_STUB, it means ourselves are not holding a transaction, thus is able to join and then commit transaction.

If current->journal is a valid transaction handler, we avoid committing transaction, but just end current transaction.

This is less effective than committing current transaction, as it won't free metadata reserved space, but we may still free some data space by the incoming data write.

Fixes c53e9653605d ("btrfs: qgroup: try to flush qgroup space when we get -EDQUOT")
---
 fs/btrfs/qgroup.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
fs/btrfs/qgroup.c +18 -1
index bf4b02a40ecc..e320bb574421 100644
@@ -3478,6 +3478,7 @@ static int try_flush_qgroup(struct btrfs_root *root)
 {
 	struct btrfs_trans_handle *trans;
 	int ret;
+	bool can_commit = true;
 
 	/*
 	 * We don't want to run flush again and again, so if there is a running
@@ -3489,6 +3490,19 @@ static int try_flush_qgroup(struct btrfs_root *root)
 		return 0;
 	}
 
+	/*
+	 * If current process holds a transaction, we shouldn't flush, as we
+	 * assume all space reservation happens before a trans handler is hold.
+	 *
+	 * But there are cases like btrfs_delayed_item_reserve_metadata() where
+	 * we try to reserve space with one trans handler already hold.
+	 * In that case we can't commit transaction. but at most end
+	 * transaction, and hope the started data writes can free some space.
+	 */
+	if (current->journal_info &&
+	    current->journal_info != BTRFS_SEND_TRANS_STUB)
+		can_commit = false;
+
 	ret = btrfs_start_delalloc_snapshot(root);
 	if (ret < 0)
 		goto out;
@@ -3500,7 +3514,10 @@ static int try_flush_qgroup(struct btrfs_root *root)
 		goto out;
 	}
 
-	ret = btrfs_commit_transaction(trans);
+	if (can_commit)
+		ret = btrfs_commit_transaction(trans);
+	else
+		ret = btrfs_end_transaction(trans);
 out:
 	clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
 	wake_up(&root->qgroup_flush_wait);
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Qu Wenruo

[BUG]
There is a report about v5.9.6 hitting ASSERT() when running fstests:

Please mention the test case which triggered the failure.

Quoted 45 lines

assertion failed: refcount_read(&trans->use_count) == 1, in fs/btrfs/transaction.c:2022

 ------------[ cut here ]------------
 kernel BUG at fs/btrfs/ctree.h:3230!
 invalid opcode: 0000 [#1] SMP PTI
 RIP: 0010:assertfail.constprop.0+0x18/0x1a [btrfs]
  btrfs_commit_transaction.cold+0x11/0x5d [btrfs]
  try_flush_qgroup+0x67/0x100 [btrfs]
  __btrfs_qgroup_reserve_meta+0x3a/0x60 [btrfs]
  btrfs_delayed_update_inode+0xaa/0x350 [btrfs]
  btrfs_update_inode+0x9d/0x110 [btrfs]
  btrfs_dirty_inode+0x5d/0xd0 [btrfs]
  touch_atime+0xb5/0x100
  iterate_dir+0xf1/0x1b0
  __x64_sys_getdents64+0x78/0x110
  do_syscall_64+0x33/0x80
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
 RIP: 0033:0x7fb5afe588db

[CAUSE]
In try_flush_qgroup(), we assume we don't hold a transaction handler at all.
This is true for data reservation and mostly true for metadata. Since data space reservation always happens before we start a transaction, and for most metadata operation we reserve space in start_transaction().

But there is an exception, btrfs_delayed_inode_reserve_metadata(). It holds a transaction handler, while still try to reserve extra metadata space.

When we hit -EDQUOT inside btrfs_delayed_inode_reserve_metadata(), we will join into current transaction, and commit current transaction, while we still have transaction handler out of qgroup code.

[FIX]
Let's check current->journal before we join the transaction.

If current->journal is empty or BTRFS_SEND_TRANS_STUB, it means ourselves are not holding a transaction, thus is able to join and then commit transaction.

If current->journal is a valid transaction handler, we avoid committing transaction, but just end current transaction.

Quoted 7 lines

This is less effective than committing current transaction, as it won't free metadata reserved space, but we may still free some data space by the incoming data write.

Fixes c53e9653605d ("btrfs: qgroup: try to flush qgroup space when we get -EDQUOT")

Thanks.

Quoted 51 lines
---
 fs/btrfs/qgroup.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
fs/btrfs/qgroup.c +18 -2
index bf4b02a40ecc..e320bb574421 100644
@@ -3478,6 +3478,7 @@ static int try_flush_qgroup(struct btrfs_root *root)
 {
        struct btrfs_trans_handle *trans;
        int ret;
+       bool can_commit = true;
 
        /*
         * We don't want to run flush again and again, so if there is a running
@@ -3489,6 +3490,19 @@ static int try_flush_qgroup(struct btrfs_root *root)
                return 0;
        }
 
+       /*
+        * If current process holds a transaction, we shouldn't flush, as we
+        * assume all space reservation happens before a trans handler is hold.
+        *
+        * But there are cases like btrfs_delayed_item_reserve_metadata() where
+        * we try to reserve space with one trans handler already hold.
+        * In that case we can't commit transaction. but at most end
+        * transaction, and hope the started data writes can free some space.
+        */
+       if (current->journal_info &&
+           current->journal_info != BTRFS_SEND_TRANS_STUB)
+               can_commit = false;
+
        ret = btrfs_start_delalloc_snapshot(root);
        if (ret < 0)
                goto out;
@@ -3500,7 +3514,10 @@ static int try_flush_qgroup(struct btrfs_root *root)
                goto out;
        }
 
-       ret = btrfs_commit_transaction(trans);
+       if (can_commit)
+               ret = btrfs_commit_transaction(trans);
+       else
+               ret = btrfs_end_transaction(trans);
 out:
        clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
        wake_up(&root->qgroup_flush_wait);
--

2.29.2

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”