Commit b9e9a6cb authored by Wang Shilong's avatar Wang Shilong Committed by Chris Mason

Btrfs: allocate prelim_ref with a slab allocater

struct __prelim_ref is allocated and freed frequently when
walking backref tree, using slab allocater can not only
speed up allocating but also detect memory leaks.
Signed-off-by: default avatarWang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: default avatarMiao Xie <miaox@cn.fujitsu.com>
Reviewed-by: default avatarJan Schmidt <list.btrfs@jan-o-sch.net>
Signed-off-by: default avatarJosef Bacik <jbacik@fusionio.com>
Signed-off-by: default avatarChris Mason <chris.mason@fusionio.com>
parent 742916b8
...@@ -119,6 +119,26 @@ struct __prelim_ref { ...@@ -119,6 +119,26 @@ struct __prelim_ref {
u64 wanted_disk_byte; u64 wanted_disk_byte;
}; };
static struct kmem_cache *btrfs_prelim_ref_cache;
int __init btrfs_prelim_ref_init(void)
{
btrfs_prelim_ref_cache = kmem_cache_create("btrfs_prelim_ref",
sizeof(struct __prelim_ref),
0,
SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
NULL);
if (!btrfs_prelim_ref_cache)
return -ENOMEM;
return 0;
}
void btrfs_prelim_ref_exit(void)
{
if (btrfs_prelim_ref_cache)
kmem_cache_destroy(btrfs_prelim_ref_cache);
}
/* /*
* the rules for all callers of this function are: * the rules for all callers of this function are:
* - obtaining the parent is the goal * - obtaining the parent is the goal
...@@ -165,7 +185,7 @@ static int __add_prelim_ref(struct list_head *head, u64 root_id, ...@@ -165,7 +185,7 @@ static int __add_prelim_ref(struct list_head *head, u64 root_id,
{ {
struct __prelim_ref *ref; struct __prelim_ref *ref;
ref = kmalloc(sizeof(*ref), gfp_mask); ref = kmem_cache_alloc(btrfs_prelim_ref_cache, gfp_mask);
if (!ref) if (!ref)
return -ENOMEM; return -ENOMEM;
...@@ -368,7 +388,8 @@ static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info, ...@@ -368,7 +388,8 @@ static int __resolve_indirect_refs(struct btrfs_fs_info *fs_info,
/* additional parents require new refs being added here */ /* additional parents require new refs being added here */
while ((node = ulist_next(parents, &uiter))) { while ((node = ulist_next(parents, &uiter))) {
new_ref = kmalloc(sizeof(*new_ref), GFP_NOFS); new_ref = kmem_cache_alloc(btrfs_prelim_ref_cache,
GFP_NOFS);
if (!new_ref) { if (!new_ref) {
ret = -ENOMEM; ret = -ENOMEM;
goto out; goto out;
...@@ -492,7 +513,7 @@ static void __merge_refs(struct list_head *head, int mode) ...@@ -492,7 +513,7 @@ static void __merge_refs(struct list_head *head, int mode)
ref1->count += ref2->count; ref1->count += ref2->count;
list_del(&ref2->list); list_del(&ref2->list);
kfree(ref2); kmem_cache_free(btrfs_prelim_ref_cache, ref2);
} }
} }
...@@ -955,7 +976,7 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans, ...@@ -955,7 +976,7 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
} }
} }
list_del(&ref->list); list_del(&ref->list);
kfree(ref); kmem_cache_free(btrfs_prelim_ref_cache, ref);
} }
out: out:
...@@ -963,13 +984,13 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans, ...@@ -963,13 +984,13 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
while (!list_empty(&prefs)) { while (!list_empty(&prefs)) {
ref = list_first_entry(&prefs, struct __prelim_ref, list); ref = list_first_entry(&prefs, struct __prelim_ref, list);
list_del(&ref->list); list_del(&ref->list);
kfree(ref); kmem_cache_free(btrfs_prelim_ref_cache, ref);
} }
while (!list_empty(&prefs_delayed)) { while (!list_empty(&prefs_delayed)) {
ref = list_first_entry(&prefs_delayed, struct __prelim_ref, ref = list_first_entry(&prefs_delayed, struct __prelim_ref,
list); list);
list_del(&ref->list); list_del(&ref->list);
kfree(ref); kmem_cache_free(btrfs_prelim_ref_cache, ref);
} }
return ret; return ret;
......
...@@ -72,4 +72,6 @@ int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, ...@@ -72,4 +72,6 @@ int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
struct btrfs_inode_extref **ret_extref, struct btrfs_inode_extref **ret_extref,
u64 *found_off); u64 *found_off);
int __init btrfs_prelim_ref_init(void);
void btrfs_prelim_ref_exit(void);
#endif #endif
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include "rcu-string.h" #include "rcu-string.h"
#include "dev-replace.h" #include "dev-replace.h"
#include "free-space-cache.h" #include "free-space-cache.h"
#include "backref.h"
#include "tests/btrfs-tests.h" #include "tests/btrfs-tests.h"
#define CREATE_TRACE_POINTS #define CREATE_TRACE_POINTS
...@@ -1810,6 +1811,10 @@ static int __init init_btrfs_fs(void) ...@@ -1810,6 +1811,10 @@ static int __init init_btrfs_fs(void)
if (err) if (err)
goto free_auto_defrag; goto free_auto_defrag;
err = btrfs_prelim_ref_init();
if (err)
goto free_prelim_ref;
err = btrfs_interface_init(); err = btrfs_interface_init();
if (err) if (err)
goto free_delayed_ref; goto free_delayed_ref;
...@@ -1830,6 +1835,8 @@ static int __init init_btrfs_fs(void) ...@@ -1830,6 +1835,8 @@ static int __init init_btrfs_fs(void)
unregister_ioctl: unregister_ioctl:
btrfs_interface_exit(); btrfs_interface_exit();
free_prelim_ref:
btrfs_prelim_ref_exit();
free_delayed_ref: free_delayed_ref:
btrfs_delayed_ref_exit(); btrfs_delayed_ref_exit();
free_auto_defrag: free_auto_defrag:
...@@ -1856,6 +1863,7 @@ static void __exit exit_btrfs_fs(void) ...@@ -1856,6 +1863,7 @@ static void __exit exit_btrfs_fs(void)
btrfs_delayed_ref_exit(); btrfs_delayed_ref_exit();
btrfs_auto_defrag_exit(); btrfs_auto_defrag_exit();
btrfs_delayed_inode_exit(); btrfs_delayed_inode_exit();
btrfs_prelim_ref_exit();
ordered_data_exit(); ordered_data_exit();
extent_map_exit(); extent_map_exit();
extent_io_exit(); extent_io_exit();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment