Skip to contentSkip to conversation

Debugging macro and kmalloc_obj* cleanups

PATCH In review David Sterba 2 participants 8 messages Apr 14 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Debugging macro and kmalloc_obj* cleanups

Replace some macros with existing or new API. Replacing ASSERT(0) in free space tree updates error handling but it's for "impossible" conditions.

David Sterba (6):
btrfs: move condition to WARN_ON in btrfs_set_delalloc_extent()
btrfs: replace open coded DEBUG_WARN in extent_writepage()
btrfs: handle unexpected free-space-tree key types
btrfs: lift assertions to beginning of insert_delayed_ref()
btrfs: do more kmalloc_obj()/kmalloc_objs() conversions
btrfs: convert kmalloc_array to kmalloc_objs in
btrfs_calc_avail_data_space()

 fs/btrfs/delayed-ref.c     |  5 ++++-
 fs/btrfs/extent_io.c       |  2 +-
 fs/btrfs/free-space-tree.c | 18 +++++++++++++++---
 fs/btrfs/inode.c           |  3 +--
 fs/btrfs/relocation.c      | 10 +++++-----
 fs/btrfs/super.c           |  3 +--
 6 files changed, 27 insertions(+), 14 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/6 btrfs: move condition to WARN_ON in btrfs_set_delalloc_extent()

For a simple if + WARN_ON we should use the condition directly in the macro.

---
 fs/btrfs/inode.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/inode.c +1 -2
index 1e8e9a3d2d3090..247299d0860139 100644
@@ -2656,8 +2656,7 @@ void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *s
 
 	lockdep_assert_held(&inode->io_tree.lock);
 
-	if ((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC))
-		WARN_ON(1);
+	WARN_ON((bits & EXTENT_DEFRAG) && !(bits & EXTENT_DELALLOC));
 	/*
 	 * set_bit and clear bit hooks normally require _irqsave/restore
 	 * but in this case, we are only testing for the DELALLOC
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/6 btrfs: replace open coded DEBUG_WARN in extent_writepage()

In reply to David Sterba
---
 fs/btrfs/extent_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent_io.c +1 -1
index 3411c41993477c..0c15c9783c95da 100644
@@ -1873,7 +1873,7 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
 	 */
 	if (IS_ENABLED(CONFIG_BTRFS_EXPERIMENTAL) &&
 	    unlikely(!folio_test_private(folio))) {
-		WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
+		DEBUG_WARN();
 		btrfs_err_rl(fs_info,
 	"root %lld ino %llu folio %llu is marked dirty without notifying the fs",
 			     btrfs_root_id(inode->root),
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 3/6 btrfs: handle unexpected free-space-tree key types

In reply to David Sterba

Replace the conditional assertions with proper error handling and transaction abort if we find an unexpected key type in the free space tree.

---
 fs/btrfs/free-space-tree.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
fs/btrfs/free-space-tree.c +15 -3
index 9efd1ec90f031f..472b3060e5ac32 100644
@@ -259,7 +259,11 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
 				nr++;
 				path->slots[0]--;
 			} else {
-				ASSERT(0);
+				btrfs_err(fs_info, "unexpected free space tree key type %u",
+					  found_key.type);
+				ret = -EUCLEAN;
+				btrfs_abort_transaction(trans, ret);
+				goto out;
 			}
 		}
 
@@ -405,7 +409,11 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans,
 
 				nr++;
 			} else {
-				ASSERT(0);
+				btrfs_err(fs_info, "unexpected free space tree key type %u",
+					  found_key.type);
+				ret = -EUCLEAN;
+				btrfs_abort_transaction(trans, ret);
+				goto out;
 			}
 		}
 
@@ -1518,7 +1526,11 @@ int btrfs_remove_block_group_free_space(struct btrfs_trans_handle *trans,
 				nr++;
 				path->slots[0]--;
 			} else {
-				ASSERT(0);
+				btrfs_err(trans->fs_info, "unexpected free space tree key type %u",
+					  found_key.type);
+				ret = -EUCLEAN;
+				btrfs_abort_transaction(trans, ret);
+				return ret;
 			}
 		}
 
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 4/6 btrfs: lift assertions to beginning of insert_delayed_ref()

In reply to David Sterba

There are only two possible types of the delayed ref action, this can be verified at the beginning for the whole function and not just one block. Replace the assertion with a debugging warning just in case.

---
 fs/btrfs/delayed-ref.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/delayed-ref.c +4 -1
index 605858c2d9a952..8bc1929237f70a 100644
@@ -615,6 +615,9 @@ static bool insert_delayed_ref(struct btrfs_trans_handle *trans,
 	struct btrfs_delayed_ref_node *exist;
 	int mod;
 
+	ASSERT(ref->action == BTRFS_ADD_DELAYED_REF ||
+	       ref->action == BTRFS_DROP_DELAYED_REF);
+
 	spin_lock(&href->lock);
 	exist = tree_insert(&href->ref_tree, ref);
 	if (!exist) {
@@ -641,7 +644,7 @@ static bool insert_delayed_ref(struct btrfs_trans_handle *trans,
 				ASSERT(!list_empty(&exist->add_list));
 				list_del_init(&exist->add_list);
 			} else {
-				ASSERT(0);
+				DEBUG_WARN();
 			}
 		} else
 			mod = -ref->ref_mod;
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 5/6 btrfs: do more kmalloc_obj()/kmalloc_objs() conversions

In reply to David Sterba

Do a few more (trivial) conversions that started in commit 69050f8d6d075d ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types").

---
 fs/btrfs/relocation.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/relocation.c +5 -5
index 3ebaf5880125fa..9cbae3cf8bdd1e 100644
@@ -590,7 +590,7 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
 	struct btrfs_key root_key;
 	int ret = 0;
 
-	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
+	root_item = kmalloc_obj(*root_item, GFP_NOFS);
 	if (!root_item)
 		return ERR_PTR(-ENOMEM);
 
@@ -2944,7 +2944,7 @@ static int relocate_file_extent_cluster(struct reloc_control *rc)
 	if (!cluster->nr)
 		return 0;
 
-	ra = kzalloc(sizeof(*ra), GFP_NOFS);
+	ra = kzalloc_obj(*ra, GFP_NOFS);
 	if (!ra)
 		return -ENOMEM;
 
@@ -3863,7 +3863,7 @@ static int add_remap_tree_entries(struct btrfs_trans_handle *trans, struct btrfs
 
 	max_items = BTRFS_LEAF_DATA_SIZE(trans->fs_info) / sizeof(struct btrfs_item);
 
-	data_sizes = kzalloc(sizeof(u32) * min_t(u32, num_entries, max_items), GFP_NOFS);
+	data_sizes = kzalloc_objs(u32, min_t(u32, num_entries, max_items), GFP_NOFS);
 	if (!data_sizes)
 		return -ENOMEM;
 
@@ -4454,7 +4454,7 @@ static int create_remap_tree_entries(struct btrfs_trans_handle *trans,
 
 	btrfs_release_path(path);
 
-	space_runs = kmalloc(sizeof(*space_runs) * extent_count, GFP_NOFS);
+	space_runs = kmalloc_objs(*space_runs, extent_count, GFP_NOFS);
 	if (!space_runs) {
 		mutex_unlock(&bg->free_space_lock);
 		return -ENOMEM;
@@ -4543,7 +4543,7 @@ static int create_remap_tree_entries(struct btrfs_trans_handle *trans,
 	mutex_unlock(&bg->free_space_lock);
 
 	max_entries = extent_count + 2;
-	entries = kmalloc(sizeof(*entries) * max_entries, GFP_NOFS);
+	entries = kmalloc_objs(*entries, max_entries, GFP_NOFS);
 	if (!entries) {
 		ret = -ENOMEM;
 		goto out;
Signature · 1 line
2.53.0
author maintainer
#6 lore raw

PATCH 6/6 btrfs: convert kmalloc_array to kmalloc_objs in btrfs_calc_avail_data_space()

In reply to David Sterba

There's one use of kmalloc_array() that can be transformed to kmalloc_objs() in the same way as suggested in commit 69050f8d6d075d ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types"), swap the arguments and drop GFP flags. All the other cases of kmalloc_array() do not use a simple type so this is the only one.

---
 fs/btrfs/super.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/super.c +1 -2
index b26aa9169e8388..f67a268f36a6d6 100644
@@ -1633,8 +1633,7 @@ static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
 		}
 	}
 
-	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
-			       GFP_KERNEL);
+	devices_info = kmalloc_objs(*devices_info, nr_devices);
 	if (!devices_info)
 		return -ENOMEM;
 
Signature · 1 line
2.53.0