All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH 44/45] btrfs: add a leak check for roots
Date: Tue, 17 Dec 2019 10:36:34 -0500	[thread overview]
Message-ID: <20191217153635.44733-45-josef@toxicpanda.com> (raw)
In-Reply-To: <20191217153635.44733-1-josef@toxicpanda.com>

Now that we're going to start relying on getting ref counting right for
roots, add a list to track allocated roots and print out any roots that
aren't free'd up at free_fs_info time.  Hide this behind
CONFIG_BTRFS_DEBUG because this will just be used for developers to
verify they aren't breaking things.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/ctree.h             |  8 ++++++++
 fs/btrfs/disk-io.c           | 28 ++++++++++++++++++++++++++++
 fs/btrfs/disk-io.h           |  9 ++++++++-
 fs/btrfs/tests/btrfs-tests.c |  1 +
 4 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 8578c65890b2..59d7e55c94d8 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -902,6 +902,10 @@ struct btrfs_fs_info {
 	spinlock_t ref_verify_lock;
 	struct rb_root block_tree;
 #endif
+
+#ifdef CONFIG_BTRFS_DEBUG
+	struct list_head alloced_roots;
+#endif
 };
 
 static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
@@ -1103,6 +1107,10 @@ struct btrfs_root {
 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
 	u64 alloc_bytenr;
 #endif
+
+#ifdef CONFIG_BTRFS_DEBUG
+	struct list_head leak_list;
+#endif
 };
 
 struct btrfs_clone_extent_info {
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 991a6f518c0f..67bd4c91352b 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1204,6 +1204,12 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
 
 	spin_lock_init(&root->root_item_lock);
 	btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
+#ifdef CONFIG_BTRFS_DEBUG
+	INIT_LIST_HEAD(&root->leak_list);
+	spin_lock(&fs_info->fs_roots_radix_lock);
+	list_add_tail(&root->leak_list, &fs_info->alloced_roots);
+	spin_unlock(&fs_info->fs_roots_radix_lock);
+#endif
 }
 
 static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
@@ -1533,6 +1539,24 @@ int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
 	return ret;
 }
 
+void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info)
+{
+#ifdef CONFIG_BTRFS_DEBUG
+	struct btrfs_root *root;
+
+	while (!list_empty(&fs_info->alloced_roots)) {
+		root = list_first_entry(&fs_info->alloced_roots,
+					struct btrfs_root, leak_list);
+		btrfs_err(fs_info, "leaked root %llu-%llu refcount %d",
+			  root->root_key.objectid, root->root_key.offset,
+			  refcount_read(&root->refs));
+		while (refcount_read(&root->refs) > 1)
+			btrfs_put_fs_root(root);
+		btrfs_put_fs_root(root);
+	}
+#endif
+}
+
 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
 {
 	percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
@@ -1553,6 +1577,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
 	btrfs_put_fs_root(fs_info->uuid_root);
 	btrfs_put_fs_root(fs_info->free_space_root);
 	btrfs_put_fs_root(fs_info->fs_root);
+	btrfs_check_leaked_roots(fs_info);
 	kfree(fs_info->super_copy);
 	kfree(fs_info->super_for_commit);
 	kvfree(fs_info);
@@ -2675,6 +2700,9 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 	INIT_LIST_HEAD(&fs_info->space_info);
 	INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
 	INIT_LIST_HEAD(&fs_info->unused_bgs);
+#ifdef CONFIG_BTRFS_DEBUG
+	INIT_LIST_HEAD(&fs_info->alloced_roots);
+#endif
 	extent_map_tree_init(&fs_info->mapping_tree);
 	btrfs_init_block_rsv(&fs_info->global_block_rsv,
 			     BTRFS_BLOCK_RSV_GLOBAL);
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index 5467227f007d..7b79b92fb768 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -39,6 +39,7 @@ static inline u64 btrfs_sb_offset(int mirror)
 struct btrfs_device;
 struct btrfs_fs_devices;
 
+void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info);
 void btrfs_init_fs_info(struct btrfs_fs_info *fs_info);
 int btrfs_verify_level_key(struct extent_buffer *eb, int level,
 			   struct btrfs_key *first_key, u64 parent_transid);
@@ -101,8 +102,14 @@ static inline void btrfs_put_fs_root(struct btrfs_root *root)
 {
 	if (!root)
 		return;
-	if (refcount_dec_and_test(&root->refs))
+	if (refcount_dec_and_test(&root->refs)) {
+#ifdef CONFIG_BTRFS_DEBUG
+		spin_lock(&root->fs_info->fs_roots_radix_lock);
+		list_del_init(&root->leak_list);
+		spin_unlock(&root->fs_info->fs_roots_radix_lock);
+#endif
 		kfree(root);
+	}
 }
 
 void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
diff --git a/fs/btrfs/tests/btrfs-tests.c b/fs/btrfs/tests/btrfs-tests.c
index 2bcef946d92a..8cfcd8ace55c 100644
--- a/fs/btrfs/tests/btrfs-tests.c
+++ b/fs/btrfs/tests/btrfs-tests.c
@@ -164,6 +164,7 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
 	btrfs_free_fs_roots(fs_info);
 	cleanup_srcu_struct(&fs_info->subvol_srcu);
 	kfree(fs_info->super_copy);
+	btrfs_check_leaked_roots(fs_info);
 	kfree(fs_info->fs_devices);
 	kfree(fs_info);
 }
-- 
2.23.0


  parent reply	other threads:[~2019-12-17 15:38 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-17 15:35 [PATCH 0/45][v2] Cleanup how we handle root refs, part 1 Josef Bacik
2019-12-17 15:35 ` [PATCH 01/45] btrfs: push __setup_root into btrfs_alloc_root Josef Bacik
2019-12-17 15:35 ` [PATCH 02/45] btrfs: move fs root init stuff into btrfs_init_fs_root Josef Bacik
2019-12-17 15:35 ` [PATCH 03/45] btrfs: make btrfs_find_orphan_roots use btrfs_get_fs_root Josef Bacik
2019-12-17 15:35 ` [PATCH 04/45] btrfs: export and use btrfs_read_tree_root Josef Bacik
2019-12-17 15:35 ` [PATCH 05/45] btrfs: make relocation use btrfs_read_tree_root() Josef Bacik
2019-12-17 15:35 ` [PATCH 06/45] btrfs: kill btrfs_read_fs_root Josef Bacik
2019-12-17 15:35 ` [PATCH 07/45] btrfs: kill the btrfs_read_fs_root_no_name helper Josef Bacik
2019-12-17 15:35 ` [PATCH 08/45] btrfs: make the fs root init functions static Josef Bacik
2019-12-17 15:35 ` [PATCH 09/45] btrfs: handle NULL roots in btrfs_put/btrfs_grab_fs_root Josef Bacik
2019-12-17 15:36 ` [PATCH 10/45] btrfs: hold a ref on fs roots while they're in the radix tree Josef Bacik
2019-12-17 15:36 ` [PATCH 11/45] btrfs: hold a ref on the root in resolve_indirect_ref Josef Bacik
2019-12-17 15:36 ` [PATCH 12/45] btrfs: hold a root ref in btrfs_get_dentry Josef Bacik
2019-12-17 15:36 ` [PATCH 13/45] btrfs: hold a ref on the root in __btrfs_run_defrag_inode Josef Bacik
2019-12-17 15:36 ` [PATCH 14/45] btrfs: hold a ref for the root in record_one_backref Josef Bacik
2019-12-17 15:36 ` [PATCH 15/45] btrfs: grab a ref on the root in relink_extent_backref Josef Bacik
2019-12-17 15:36 ` [PATCH 16/45] btrfs: hold a ref on the root in fixup_tree_root_location Josef Bacik
2019-12-17 15:36 ` [PATCH 17/45] btrfs: hold a ref on the root in create_subvol Josef Bacik
2019-12-17 15:36 ` [PATCH 18/45] btrfs: hold a ref on the root in search_ioctl Josef Bacik
2019-12-17 15:36 ` [PATCH 19/45] btrfs: hold a ref on the root in btrfs_search_path_in_tree Josef Bacik
2019-12-17 15:36 ` [PATCH 20/45] btrfs: hold a ref on the root in btrfs_search_path_in_tree_user Josef Bacik
2019-12-17 15:36 ` [PATCH 21/45] btrfs: hold a ref on the root in btrfs_ioctl_get_subvol_info Josef Bacik
2019-12-17 15:36 ` [PATCH 22/45] btrfs: hold ref on root in btrfs_ioctl_default_subvol Josef Bacik
2019-12-17 15:36 ` [PATCH 23/45] btrfs: hold a ref on the root in build_backref_tree Josef Bacik
2019-12-17 15:36 ` [PATCH 24/45] btrfs: hold a ref on the root in prepare_to_merge Josef Bacik
2019-12-17 15:36 ` [PATCH 25/45] btrfs: hold a ref on the root in merge_reloc_roots Josef Bacik
2019-12-17 15:36 ` [PATCH 26/45] btrfs: hold a ref on the root in record_reloc_root_in_trans Josef Bacik
2019-12-17 15:36 ` [PATCH 27/45] btrfs: hold a ref on the root in find_data_references Josef Bacik
2019-12-17 15:36 ` [PATCH 28/45] btrfs: hold a ref on the root in create_reloc_inode Josef Bacik
2019-12-17 15:36 ` [PATCH 29/45] btrfs: hold a ref on the root in btrfs_recover_relocation Josef Bacik
2019-12-17 15:36 ` [PATCH 30/45] btrfs: push grab_fs_root into read_fs_root Josef Bacik
2019-12-17 15:36 ` [PATCH 31/45] btrfs: hold a ref for the root in btrfs_find_orphan_roots Josef Bacik
2019-12-17 15:36 ` [PATCH 32/45] btrfs: hold a ref on the root in scrub_print_warning_inode Josef Bacik
2019-12-17 15:36 ` [PATCH 33/45] btrfs: hold a ref on the root in btrfs_ioctl_send Josef Bacik
2019-12-17 15:36 ` [PATCH 34/45] btrfs: hold a ref on the root in get_subvol_name_from_objectid Josef Bacik
2019-12-17 15:36 ` [PATCH 35/45] btrfs: hold a ref on the root in create_pending_snapshot Josef Bacik
2019-12-17 15:36 ` [PATCH 36/45] btrfs: hold a ref on the root in btrfs_recover_log_trees Josef Bacik
2019-12-17 15:36 ` [PATCH 37/45] btrfs: hold a ref on the root in btrfs_check_uuid_tree_entry Josef Bacik
2019-12-17 15:36 ` [PATCH 38/45] btrfs: hold a ref on the root in open_ctree Josef Bacik
2019-12-17 15:36 ` [PATCH 39/45] btrfs: use btrfs_put_fs_root to free roots always Josef Bacik
2019-12-17 15:36 ` [PATCH 40/45] btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root Josef Bacik
2019-12-17 15:36 ` [PATCH 41/45] btrfs: free more things in btrfs_free_fs_info Josef Bacik
2019-12-17 15:36 ` [PATCH 42/45] btrfs: move fs_info init work into it's own helper function Josef Bacik
2019-12-17 15:36 ` [PATCH 43/45] btrfs: make the init of static elements in fs_info separate Josef Bacik
2019-12-17 15:36 ` Josef Bacik [this message]
2019-12-17 15:36 ` [PATCH 45/45] btrfs: rename btrfs_put_fs_root and btrfs_grab_fs_root Josef Bacik

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=20191217153635.44733-45-josef@toxicpanda.com \
    --to=josef@toxicpanda.com \
    --cc=kernel-team@fb.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.