Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: fdmanana@kernel.org
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 05/15] btrfs: avoid memory allocation at log_new_dir_dentries() for common case
Date: Mon, 22 Aug 2022 11:51:34 +0100	[thread overview]
Message-ID: <bc593419a15094dc0ecbcfa5a8c7355b2fdcf585.1661165149.git.fdmanana@suse.com> (raw)
In-Reply-To: <cover.1661165149.git.fdmanana@suse.com>

From: Filipe Manana <fdmanana@suse.com>

At log_new_dir_dentries() we always start by allocating a list element
for the starting inode and then do a while loop with the condition being
a list emptiness check.

This however is not needed, we can avoid allocating this initial list
element and then just check for the list emptiness at the end of the
loop's body. So just do that to save one memory allocation from the
kmalloc-32 slab.

This allows for not doing any memory allocation when we don't have any
subdirectory to log, which is a very common case.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/tree-log.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index bd50509e9839..94026098bb68 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -5977,6 +5977,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 	struct btrfs_path *path;
 	LIST_HEAD(dir_list);
 	struct btrfs_dir_list *dir_elem;
+	u64 ino = btrfs_ino(start_inode);
 	int ret = 0;
 
 	/*
@@ -5991,28 +5992,13 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 	if (!path)
 		return -ENOMEM;
 
-	dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
-	if (!dir_elem) {
-		btrfs_free_path(path);
-		return -ENOMEM;
-	}
-	dir_elem->ino = btrfs_ino(start_inode);
-	list_add_tail(&dir_elem->list, &dir_list);
-
-	while (!list_empty(&dir_list)) {
+	while (true) {
 		struct extent_buffer *leaf;
 		struct btrfs_key min_key;
-		u64 ino;
 		bool continue_curr_inode = true;
 		int nritems;
 		int i;
 
-		dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
-					    list);
-		ino = dir_elem->ino;
-		list_del(&dir_elem->list);
-		kfree(dir_elem);
-
 		min_key.objectid = ino;
 		min_key.type = BTRFS_DIR_INDEX_KEY;
 		min_key.offset = 0;
@@ -6023,7 +6009,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 			break;
 		} else if (ret > 0) {
 			ret = 0;
-			continue;
+			goto next;
 		}
 
 		leaf = path->nodes[0];
@@ -6086,6 +6072,15 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
 			min_key.offset++;
 			goto again;
 		}
+
+next:
+		if (list_empty(&dir_list))
+			break;
+
+		dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list, list);
+		ino = dir_elem->ino;
+		list_del(&dir_elem->list);
+		kfree(dir_elem);
 	}
 out:
 	btrfs_free_path(path);
-- 
2.35.1


  parent reply	other threads:[~2022-08-22 10:52 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-17 11:22 [PATCH 00/15] btrfs: some updates to delayed items and inode logging fdmanana
2022-08-17 11:22 ` [PATCH 01/15] btrfs: don't drop dir index range items when logging a directory fdmanana
2022-08-17 11:22 ` [PATCH 02/15] btrfs: remove the root argument from log_new_dir_dentries() fdmanana
2022-08-17 11:22 ` [PATCH 03/15] btrfs: update stale comment for log_new_dir_dentries() fdmanana
2022-08-17 11:22 ` [PATCH 04/15] btrfs: free list element sooner at log_new_dir_dentries() fdmanana
2022-08-17 11:22 ` [PATCH 05/15] btrfs: avoid memory allocation at log_new_dir_dentries() for common case fdmanana
2022-08-17 11:22 ` [PATCH 06/15] btrfs: remove root argument from btrfs_delayed_item_reserve_metadata() fdmanana
2022-08-17 11:22 ` [PATCH 07/15] btrfs: store index number instead of key in struct btrfs_delayed_item fdmanana
2022-08-17 11:22 ` [PATCH 08/15] btrfs: remove unused logic when looking up delayed items fdmanana
2022-08-17 11:22 ` [PATCH 09/15] btrfs: shrink the size of struct btrfs_delayed_item fdmanana
2022-08-22 13:43   ` David Sterba
2022-08-22 14:15     ` Filipe Manana
2022-08-22 15:29       ` David Sterba
2022-08-22 16:00         ` Filipe Manana
2022-08-17 11:22 ` [PATCH 10/15] btrfs: search for last logged dir index if it's not cached in the inode fdmanana
2022-08-17 11:22 ` [PATCH 11/15] btrfs: move need_log_inode() to above log_conflicting_inodes() fdmanana
2022-08-17 11:22 ` [PATCH 12/15] btrfs: move log_new_dir_dentries() above btrfs_log_inode() fdmanana
2022-08-17 11:22 ` [PATCH 13/15] btrfs: log conflicting inodes without holding log mutex of the initial inode fdmanana
2022-08-17 11:22 ` [PATCH 14/15] btrfs: skip logging parent dir when conflicting inode is not a dir fdmanana
2022-08-17 11:22 ` [PATCH 15/15] btrfs: use delayed items when logging a directory fdmanana
2022-08-22 10:51 ` [PATCH v2 00/15] btrfs: some updates to delayed items and inode logging fdmanana
2022-08-22 10:51   ` [PATCH v2 01/15] btrfs: don't drop dir index range items when logging a directory fdmanana
2022-08-22 10:51   ` [PATCH v2 02/15] btrfs: remove the root argument from log_new_dir_dentries() fdmanana
2022-08-22 10:51   ` [PATCH v2 03/15] btrfs: update stale comment for log_new_dir_dentries() fdmanana
2022-08-22 10:51   ` [PATCH v2 04/15] btrfs: free list element sooner at log_new_dir_dentries() fdmanana
2022-08-22 10:51   ` fdmanana [this message]
2022-08-22 10:51   ` [PATCH v2 06/15] btrfs: remove root argument from btrfs_delayed_item_reserve_metadata() fdmanana
2022-08-22 10:51   ` [PATCH v2 07/15] btrfs: store index number instead of key in struct btrfs_delayed_item fdmanana
2022-08-22 10:51   ` [PATCH v2 08/15] btrfs: remove unused logic when looking up delayed items fdmanana
2022-08-22 10:51   ` [PATCH v2 09/15] btrfs: shrink the size of struct btrfs_delayed_item fdmanana
2022-08-22 10:51   ` [PATCH v2 10/15] btrfs: search for last logged dir index if it's not cached in the inode fdmanana
2022-08-22 10:51   ` [PATCH v2 11/15] btrfs: move need_log_inode() to above log_conflicting_inodes() fdmanana
2022-08-22 10:51   ` [PATCH v2 12/15] btrfs: move log_new_dir_dentries() above btrfs_log_inode() fdmanana
2022-08-22 10:51   ` [PATCH v2 13/15] btrfs: log conflicting inodes without holding log mutex of the initial inode fdmanana
2022-08-22 10:51   ` [PATCH v2 14/15] btrfs: skip logging parent dir when conflicting inode is not a dir fdmanana
2022-08-22 10:51   ` [PATCH v2 15/15] btrfs: use delayed items when logging a directory fdmanana
2022-08-22 15:45   ` [PATCH v2 00/15] btrfs: some updates to delayed items and inode logging David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bc593419a15094dc0ecbcfa5a8c7355b2fdcf585.1661165149.git.fdmanana@suse.com \
    --to=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox