Skip to contentSkip to conversation

btrfs: a few fsync related minor improvements and a cleanup

PATCH In review [email protected] 4 participants 9 messages Jul 20, 2021 – Jul 23, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: a few fsync related minor improvements and a cleanup

From: Filipe Manana <[email protected]>

The following patches remove some unnecessary code and bring a couple minor performance improvements in the fsync path. They are independent of each other, but are grouped in the same pathset just because they relate around the same code. The last patch has some performance results in its changelog.

Filipe Manana (4):
btrfs: remove racy and unnecessary inode transaction update when using
no-holes
btrfs: avoid unnecessary log mutex contention when syncing log
btrfs: remove unnecessary list head initialization when syncing log
btrfs: avoid unnecessary lock and leaf splits when updating inode in
the log

 fs/btrfs/inode.c    | 12 ++++------
 fs/btrfs/tree-log.c | 56 ++++++++++++++++++++++++++++++++++++---------
 2 files changed, 50 insertions(+), 18 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/4 btrfs: remove racy and unnecessary inode transaction update when using no-holes

From: Filipe Manana <[email protected]>

When using the NO_HOLES feature and expanding the size of an inode, we update the inode's last_trans, last_sub_trans and last_log_commit fields at maybe_insert_hole() so that a fsync does know that the inode needs to be logged (by making sure that btrfs_inode_in_log() returns false). This happens for expanding truncate operations, buffered writes, direct IO writes and when cloning extents to an offset greater than the inode's i_size.

However the way we do it is racy, because in between setting the inode's last_sub_trans and last_log_commit fields, the log transaction ID that was assigned to last_sub_trans might be committed before we read the root's last_log_commit and assign that value to last_log_commit. If that happens it would make a future call to btrfs_inode_in_log() return true. This is a race that should be extremely unlikely to be hit in practice, and it is the same that was described by commit bc0939fcfab0d7 ("btrfs: fix race between marking inode needs to be logged and log syncing").

The fix would simply be to set last_log_commit to the value we assigned to last_sub_trans minus 1, like it was done in that commit. However updating these two fields plus the last_trans field is pointless here because all the callers of btrfs_cont_expand() (which is the only caller of maybe_insert_hole()) always call btrfs_set_inode_last_trans() or btrfs_update_inode() after calling btrfs_cont_expand(). Calling either btrfs_set_inode_last_trans() or btrfs_update_inode() guarantees that the next fsync will log the inode, as it makes btrfs_inode_in_log() return false.

So just remove the code that explicitly sets the inode's last_trans, last_sub_trans and last_log_commit fields.

---
 fs/btrfs/inode.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
fs/btrfs/inode.c +5 -7
index e6eb20987351..5798c6191908 100644
@@ -4999,15 +4999,13 @@ static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
 	int ret;
 
 	/*
-	 * Still need to make sure the inode looks like it's been updated so
-	 * that any holes get logged if we fsync.
+	 * If NO_HOLES is enabled, we don't need to do anything.
+	 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
+	 * or btrfs_update_inode() will be called, which guarantee that the next
+	 * fsync will know this inode was changed and needs to be logged.
 	 */
-	if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
-		inode->last_trans = fs_info->generation;
-		inode->last_sub_trans = root->log_transid;
-		inode->last_log_commit = root->last_log_commit;
+	if (btrfs_fs_incompat(fs_info, NO_HOLES))
 		return 0;
-	}
 
 	/*
 	 * 1 - for the one we're dropping
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/4 btrfs: avoid unnecessary log mutex contention when syncing log

In reply to [email protected]

From: Filipe Manana <[email protected]>

When syncing the log we acquire the root's log mutex just to update the root's last_log_commit. This is unnecessary because:

1) At this point there can only be one task updating this value, which is the task committing the current log transaction. Any task that enters btrfs_sync_log() has to wait for the previous log transaction to commit and wait for the current log transaction to commit if someone else already started it (in this case it never reaches to the point of updating last_log_commit, as that is done by the comitting task);

2) All readers of the root's last_log_commit don't acquire the root's log mutex. This is to avoid blocking the readers, potentially for too long and because getting a stale value of last_log_commit does not cause any functional problem, in the worst case getting a stale value results in logging an inode unnecessarily. Plus it's actually very rare to get a stale value that results in unnecessarily logging the inode.

So in order to avoid unnecessary contention on the root's log mutex, which is used for several different purposes, like starting/joining a log transaction and starting writeback of a log transaction, stop acquiring the log mutex for updating the root's last_log_commit.

---
 fs/btrfs/tree-log.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/btrfs/tree-log.c +10 -4
index 9fd0348be7f5..90fb5a2fc60b 100644
@@ -3328,10 +3328,16 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 		goto out_wake_log_root;
 	}
 
-	mutex_lock(&root->log_mutex);
-	if (root->last_log_commit < log_transid)
-		root->last_log_commit = log_transid;
-	mutex_unlock(&root->log_mutex);
+	/*
+	 * We know there can only be one task here, since we have not yet set
+	 * root->log_commit[index1] to 0 and any task attempting to sync the
+	 * log must wait for the previous log transaction to commit if it's
+	 * still in progress or wait for the current log transaction commit if
+	 * someone else already started it. We use <= and not < because the
+	 * first log transaction has an ID of 0.
+	 */
+	ASSERT(root->last_log_commit <= log_transid);
+	root->last_log_commit = log_transid;
 
 out_wake_log_root:
 	mutex_lock(&log_root_tree->log_mutex);
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/4 btrfs: remove unnecessary list head initialization when syncing log

In reply to [email protected]

From: Filipe Manana <[email protected]>

One of the last steps of syncing the log is to remove all log contextes from the root's list of contextes, done at btrfs_remove_all_log_ctxs(). There we iterate over all the contextes in the list and delete each one from the list, and after that we call INIT_LIST_HEAD() on the list. That is unnecessary since at that point the list is empty.

So just remove the INIT_LIST_HEAD() call. It's not needed, increases code size (bloat-o-meter reported a delta of -122 for btrfs_sync_log() after this change) and increases two critical sections delimited by log mutexes.

---
 fs/btrfs/tree-log.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -2
index 90fb5a2fc60b..63f48715135c 100644
@@ -3039,8 +3039,6 @@ static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
 		list_del_init(&ctx->list);
 		ctx->log_ret = error;
 	}
-
-	INIT_LIST_HEAD(&root->log_ctxs[index]);
 }
 
 /*
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH 3/4] btrfs: remove unnecessary list head initialization when syncing log

Quoted 9 lines · On 20.07.21 г. 18:03, [email protected] wrote:

From: Filipe Manana <[email protected]>

One of the last steps of syncing the log is to remove all log contextes from the root's list of contextes, done at btrfs_remove_all_log_ctxs(). There we iterate over all the contextes in the list and delete each one from the list, and after that we call INIT_LIST_HEAD() on the list. That is unnecessary since at that point the list is empty.

So just remove the INIT_LIST_HEAD() call. It's not needed, increases code

nit: I assume you mean decreases code size

Quoted 2 lines

size (bloat-o-meter reported a delta of -122 for btrfs_sync_log() after this change) and increases two critical sections delimited by log mutexes.

nit: Here you also mean decreases two critsecs

Quoted 20 lines
---
 fs/btrfs/tree-log.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -2
index 90fb5a2fc60b..63f48715135c 100644
@@ -3039,8 +3039,6 @@ static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
 		list_del_init(&ctx->list);
 		ctx->log_ret = error;
 	}
-
-	INIT_LIST_HEAD(&root->log_ctxs[index]);
 }
 
 /*
#5 lore raw

Re: [PATCH 3/4] btrfs: remove unnecessary list head initialization when syncing log

Quoted 12 lines · Nikolay Borisov
Quoted 8 lines · On 20.07.21 г. 18:03, [email protected] wrote:

From: Filipe Manana <[email protected]>

One of the last steps of syncing the log is to remove all log contextes from the root's list of contextes, done at btrfs_remove_all_log_ctxs(). There we iterate over all the contextes in the list and delete each one from the list, and after that we call INIT_LIST_HEAD() on the list. That is unnecessary since at that point the list is empty.

Quoted 6 lines
Quoted 1 line

So just remove the INIT_LIST_HEAD() call. It's not needed, increases code

nit: I assume you mean decreases code size

Quoted 2 lines

size (bloat-o-meter reported a delta of -122 for btrfs_sync_log() after this change) and increases two critical sections delimited by log mutexes.

nit: Here you also mean decreases two critsecs

No, in both cases I meant "increases".
The goal of the sentence is to list the negative consequences of having the call, that's why the sentence starts with "It's not needed, ".

Thanks.

Quoted 21 lines
Quoted 21 lines
---
 fs/btrfs/tree-log.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -2
index 90fb5a2fc60b..63f48715135c 100644
@@ -3039,8 +3039,6 @@ static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
              list_del_init(&ctx->list);
              ctx->log_ret = error;
      }
-
-     INIT_LIST_HEAD(&root->log_ctxs[index]);
 }
 
 /*
#6 lore raw

PATCH 4/4 btrfs: avoid unnecessary lock and leaf splits when updating inode in the log

In reply to [email protected]

From: Filipe Manana <[email protected]>

During a fast fsync, if we have already fsynced the file before and in the current transaction, we can make the inode item update more efficient and avoid acquiring a write lock on the leaf's parent.

To update the inode item we are always using btrfs_insert_empty_item() to get a path pointing to the inode item, which calls btrfs_search_slot() with an "ins_len" argument of 'sizeof(struct btrfs_inode_item) + sizeof(struct btrfs_item)', and that always results in the search taking a write lock on the level 1 node that is the parent of the leaf that contains the inode item. This adds unnecessary lock contention on log trees when we have multiple fsyncs in parallel against inodes in the same subvolume, which has a very significant impact due to the fact that log trees are short lived and their height very rarely goes beyond level 2.

Also, by using btrfs_insert_empty_item() when we need to update the inode item, we also end up splitting the leaf of the existing inode item when the leaf has an amount of free space smaller than the size of an inode item.

Improve this by using btrfs_seach_slot(), with a 0 "ins_len" argument, when we know the inode item already exists in the log. This avoids these two inefficiencies.

The following script, using fio, was used to perform the tests:

  $ cat fio-test.sh
  #!/bin/bash

DEV=/dev/nvme0n1
MNT=/mnt/nvme0n1
MOUNT_OPTIONS="-o ssd"
MKFS_OPTIONS="-d single -m single"

if [ $# -ne 4 ]; then
echo "Use $0 NUM_JOBS FILE_SIZE FSYNC_FREQ BLOCK_SIZE" exit 1
fi

NUM_JOBS=$1
FILE_SIZE=$2
FSYNC_FREQ=$3
BLOCK_SIZE=$4

cat <<EOF > /tmp/fio-job.ini
[writers]
rw=randwrite
fsync=$FSYNC_FREQ
fallocate=none
group_reporting=1
direct=0
bs=$BLOCK_SIZE
ioengine=sync
size=$FILE_SIZE
directory=$MNT
numjobs=$NUM_JOBS
EOF

echo "performance" | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

echo
echo "Using config:"
echo
cat /tmp/fio-job.ini
echo
echo "mount options: $MOUNT_OPTIONS"
echo

umount $MNT &> /dev/null
mkfs.btrfs -f $MKFS_OPTIONS $DEV
mount $MOUNT_OPTIONS $DEV $MNT

fio /tmp/fio-job.ini
umount $MNT

The tests were done on a physical machine, with 12 cores, 64G of RAM, using a NVMEe device and using a non-debug kernel config (the default one from Debian). The summary line from fio is provided below for each test run.

With 8 jobs, file size 256M, fsync frequency of 4 and a block size of 4K:

Before: WRITE: bw=28.3MiB/s (29.7MB/s), 28.3MiB/s-28.3MiB/s (29.7MB/s-29.7MB/s), io=2048MiB (2147MB), run=72297-72297msec
After:  WRITE: bw=28.7MiB/s (30.1MB/s), 28.7MiB/s-28.7MiB/s (30.1MB/s-30.1MB/s), io=2048MiB (2147MB), run=71411-71411msec

+1.4% throughput, -1.2% runtime

With 16 jobs, file size 256M, fsync frequency of 4 and a block size of 4K:

Before: WRITE: bw=40.0MiB/s (42.0MB/s), 40.0MiB/s-40.0MiB/s (42.0MB/s-42.0MB/s), io=4096MiB (4295MB), run=99980-99980msec
After:  WRITE: bw=40.9MiB/s (42.9MB/s), 40.9MiB/s-40.9MiB/s (42.9MB/s-42.9MB/s), io=4096MiB (4295MB), run=97933-97933msec

+2.2% throughput, -2.1% runtime

The changes are small but it's possible to be better on faster hardware as in the test machine used disk utilization was pretty much 100% during the whole time the tests were running (observed with 'iostat -xz 1').

The tests also included the previous patch with the subject of:
"btrfs: avoid unnecessary log mutex contention when syncing log". So they compared a branch without that patch and without this patch versus a branch with these two patches applied.

---
 fs/btrfs/tree-log.c | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)
fs/btrfs/tree-log.c +35 -5
index 63f48715135c..2fa8815f7b3b 100644
@@ -3972,14 +3972,41 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
 
 static int log_inode_item(struct btrfs_trans_handle *trans,
 			  struct btrfs_root *log, struct btrfs_path *path,
-			  struct btrfs_inode *inode)
+			  struct btrfs_inode *inode, bool inode_item_dropped)
 {
 	struct btrfs_inode_item *inode_item;
 	int ret;
 
-	ret = btrfs_insert_empty_item(trans, log, path,
-				      &inode->location, sizeof(*inode_item));
-	if (ret && ret != -EEXIST)
+	/*
+	 * If we are doing a fast fsync and the inode was logged before in the
+	 * current transaction, then we know the inode was previously logged and
+	 * it exists in the log tree. For performance reasons, in this case use
+	 * btrfs_search_slot() directly with ins_len set to 0 so that we never
+	 * attempt a write lock on the leaf's parent, which adds unnecessary lock
+	 * contention in case there are concurrent fsyncs for other inodes of the
+	 * same subvolume. Using btrfs_insert_empty_item() when the inode item
+	 * already exists can also result in unnecessarily splitting a leaf.
+	 */
+	if (!inode_item_dropped && inode->logged_trans == trans->transid) {
+		ret = btrfs_search_slot(trans, log, &inode->location, path, 0, 1);
+		ASSERT(ret <= 0);
+		if (ret > 0)
+			ret = -ENOENT;
+	} else {
+		/*
+		 * This means it is the first fsync in the current transaction,
+		 * so the inode item is not in the log and we need to insert it.
+		 * We can never get -EEXIST because we are only called for a fast
+		 * fsync and in case an inode eviction happens after the inode was
+		 * logged before in the current transaction, when we load again
+		 * the inode, we set BTRFS_INODE_NEEDS_FULL_SYNC on its runtime
+		 * flags and set ->logged_trans to 0.
+		 */
+		ret = btrfs_insert_empty_item(trans, log, path, &inode->location,
+					      sizeof(*inode_item));
+		ASSERT(ret != -EEXIST);
+	}
+	if (ret)
 		return ret;
 	inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
 				    struct btrfs_inode_item);
@@ -5303,6 +5330,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 	bool need_log_inode_item = true;
 	bool xattrs_logged = false;
 	bool recursive_logging = false;
+	bool inode_item_dropped = true;
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -5437,6 +5465,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 		} else {
 			if (inode_only == LOG_INODE_ALL)
 				fast_search = true;
+			inode_item_dropped = false;
 			goto log_extents;
 		}
 
@@ -5470,7 +5499,8 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 	btrfs_release_path(path);
 	btrfs_release_path(dst_path);
 	if (need_log_inode_item) {
-		err = log_inode_item(trans, log, dst_path, inode);
+		err = log_inode_item(trans, log, dst_path, inode,
+				     inode_item_dropped);
 		if (err)
 			goto out_unlock;
 		/*
Signature · 1 line
2.30.2
Reviewed-by on the v1 series
#7 lore raw
In reply to [email protected]
Quoted 15 lines · On 7/20/21 11:03 AM, [email protected] wrote:

From: Filipe Manana <[email protected]>

The following patches remove some unnecessary code and bring a couple minor performance improvements in the fsync path. They are independent of each other, but are grouped in the same pathset just because they relate around the same code. The last patch has some performance results in its changelog.

Filipe Manana (4):
btrfs: remove racy and unnecessary inode transaction update when using
no-holes
btrfs: avoid unnecessary log mutex contention when syncing log
btrfs: remove unnecessary list head initialization when syncing log
btrfs: avoid unnecessary lock and leaf splits when updating inode in
the log

Quoted 4 lines
  fs/btrfs/inode.c    | 12 ++++------
  fs/btrfs/tree-log.c | 56 ++++++++++++++++++++++++++++++++++++---------
  2 files changed, 50 insertions(+), 18 deletions(-)

You can add

to this series, thanks,

Josef

#8 lore raw
In reply to [email protected]
Quoted 7 lines · On Tue, Jul 20, 2021 at 04:03:39PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

The following patches remove some unnecessary code and bring a couple minor performance improvements in the fsync path. They are independent of each other, but are grouped in the same pathset just because they relate around the same code. The last patch has some performance results in its changelog.

Quoted 7 lines

Filipe Manana (4):
btrfs: remove racy and unnecessary inode transaction update when using
no-holes
btrfs: avoid unnecessary log mutex contention when syncing log
btrfs: remove unnecessary list head initialization when syncing log
btrfs: avoid unnecessary lock and leaf splits when updating inode in
the log

Added to misc-next, thanks.