Skip to contentSkip to conversation

Btrfs: use a slab for ordered extents allocation

PATCH In review Miao Xie 2 participants 3 messages Aug 29, 2012 – Sep 6, 2012 Btrfs file system

lore
Share & tools
lore

Miao Xie

author
#0 lore raw

PATCH 4/7 Btrfs: use a slab for ordered extents allocation

The ordered extent allocation is in the fast path of the IO, so use a slab to improve the speed of the allocation.

---
 fs/btrfs/ordered-data.c |   23 +++++++++++++++++++++--
 fs/btrfs/ordered-data.h |    2 ++
 fs/btrfs/super.c        |    9 ++++++++-
 3 files changed, 31 insertions(+), 3 deletions(-)
fs/btrfs/ordered-data.c +21 -2
index 2eb79cc..a07ae77 100644
@@ -25,6 +25,8 @@
 #include "btrfs_inode.h"
 #include "extent_io.h"
 
+static struct kmem_cache *ordered_extent_cache;
+
 static u64 entry_end(struct btrfs_ordered_extent *entry)
 {
 	if (entry->file_offset + entry->len < entry->file_offset)
@@ -187,7 +189,7 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
 	struct btrfs_ordered_extent *entry;
 
 	tree = &BTRFS_I(inode)->ordered_tree;
-	entry = kzalloc(sizeof(*entry), GFP_NOFS);
+	entry = kmem_cache_zalloc(ordered_extent_cache, GFP_NOFS);
 	if (!entry)
 		return -ENOMEM;
 
@@ -421,7 +423,7 @@ void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
 			list_del(&sum->list);
 			kfree(sum);
 		}
-		kfree(entry);
+		kmem_cache_free(ordered_extent_cache, entry);
 	}
 }
 
@@ -958,3 +960,20 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
 	}
 	spin_unlock(&root->fs_info->ordered_extent_lock);
 }
+
+int __init ordered_data_init(void)
+{
+	ordered_extent_cache = kmem_cache_create("ordered_extent",
+				sizeof(struct btrfs_ordered_extent), 0,
+				SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
+				NULL);
+	if (!ordered_extent_cache)
+		return -ENOMEM;
+	return 0;
+}
+
+void ordered_data_exit(void)
+{
+	if (ordered_extent_cache)
+		kmem_cache_destroy(ordered_extent_cache);
+}
fs/btrfs/ordered-data.h +2 -0
index c2443a4..d1ddaef 100644
@@ -192,4 +192,6 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
 				 struct inode *inode);
 void btrfs_wait_ordered_extents(struct btrfs_root *root,
 				int nocow_only, int delay_iput);
+int __init ordered_data_init(void);
+void ordered_data_exit(void);
 #endif
fs/btrfs/super.c +8 -1
index 2e06f12..70312e5 100644
@@ -1590,10 +1590,14 @@ static int __init init_btrfs_fs(void)
 	if (err)
 		goto free_extent_io;
 
-	err = btrfs_delayed_inode_init();
+	err = ordered_data_init();
 	if (err)
 		goto free_extent_map;
 
+	err = btrfs_delayed_inode_init();
+	if (err)
+		goto free_ordered_data;
+
 	err = btrfs_interface_init();
 	if (err)
 		goto free_delayed_inode;
@@ -1611,6 +1615,8 @@ unregister_ioctl:
 	btrfs_interface_exit();
 free_delayed_inode:
 	btrfs_delayed_inode_exit();
+free_ordered_data:
+	ordered_data_exit();
 free_extent_map:
 	extent_map_exit();
 free_extent_io:
@@ -1627,6 +1633,7 @@ static void __exit exit_btrfs_fs(void)
 {
 	btrfs_destroy_cachep();
 	btrfs_delayed_inode_exit();
+	ordered_data_exit();
 	extent_map_exit();
 	extent_io_exit();
 	btrfs_interface_exit();
Signature · 1 line
1.7.6.5
#1 lore raw
Quoted 2 lines · Miao Xie

The ordered extent allocation is in the fast path of the IO, so use a slab to improve the speed of the allocation.

Good. Size of the struct is 280, so this will fall into the size-512 bucket, giving 8 objects per page, while own slab will pack 14 objects into a page.

Another benefit I see is to check for leaked objects when the module is removed (and the cache destroy takes place).

Quoted 12 lines
---
 fs/btrfs/ordered-data.c |   23 +++++++++++++++++++++--
 fs/btrfs/ordered-data.h |    2 ++
 fs/btrfs/super.c        |    9 ++++++++-
 3 files changed, 31 insertions(+), 3 deletions(-)
fs/btrfs/ordered-data.c +0 -0
index 2eb79cc..a07ae77 100644

fs/btrfs/ordered-data.c void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans, · line 966

Quoted 7 lines
fs/btrfs/ordered-data.c +4 -0
@@ -958,3 +960,20 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
 	}
 	spin_unlock(&root->fs_info->ordered_extent_lock);
 }
+
+int __init ordered_data_init(void)
+{
+	ordered_extent_cache = kmem_cache_create("ordered_extent",

Please use the 'btrfs_' prefix, ie. 'btrfs_ordered_extent'

Quoted 8 lines
fs/btrfs/ordered-data.c +8 -0
+				sizeof(struct btrfs_ordered_extent), 0,
+				SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
+				NULL);
+	if (!ordered_extent_cache)
+		return -ENOMEM;
+	return 0;
+}
+
author
#2 lore raw
Quoted 18 lines · David Sterba
Quoted 2 lines · Miao Xie

The ordered extent allocation is in the fast path of the IO, so use a slab to improve the speed of the allocation.

Good. Size of the struct is 280, so this will fall into the size-512 bucket, giving 8 objects per page, while own slab will pack 14 objects into a page.

Another benefit I see is to check for leaked objects when the module is removed (and the cache destroy takes place).

Quoted 7 lines
---
 fs/btrfs/ordered-data.c |   23 +++++++++++++++++++++--
 fs/btrfs/ordered-data.h |    2 ++
 fs/btrfs/super.c        |    9 ++++++++-
 3 files changed, 31 insertions(+), 3 deletions(-)
Quoted 14 lines

fs/btrfs/ordered-data.c void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans, · line 966

Quoted 12 lines
fs/btrfs/ordered-data.c +4 -0
index 2eb79cc..a07ae77 100644
@@ -958,3 +960,20 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
 	}
 	spin_unlock(&root->fs_info->ordered_extent_lock);
 }
+
+int __init ordered_data_init(void)
+{
+	ordered_extent_cache = kmem_cache_create("ordered_extent",

Please use the 'btrfs_' prefix, ie. 'btrfs_ordered_extent'

Thanks for your review. I'll update this patch by your advice.

Regards
Miao

Quoted 10 lines
Quoted 9 lines
fs/btrfs/ordered-data.c +8 -0
+				sizeof(struct btrfs_ordered_extent), 0,
+				SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
+				NULL);
+	if (!ordered_extent_cache)
+		return -ENOMEM;
+	return 0;
+}
+