btrfs: use a kmem_cache for free space control structures

We are currently allocating the free space control structures for block
groups using the generic slabs, and given that the size of the
btrfs_free_space_ctl structure is 152 bytes (on a release kernel), we end
up using the kmalloc-192 slab and therefore waste quite some memory since
on a 4K page system we can only fit 21 free space control structures per
page. These structures are allocated and delallocated every time we create
and remove block groups.

So use a kmem_cache for free space control structures, this way on a 4K
page system we can fit 26 structures instead of 21.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Filipe Manana
2026-04-15 13:12:13 +01:00
committed by David Sterba
parent a8d1f0b539
commit 66fd877b13

View File

@@ -23,6 +23,7 @@
#include "extent-tree.h"
static struct kmem_cache *block_group_cache;
static struct kmem_cache *free_space_ctl_cache;
int __init btrfs_init_block_group(void)
{
@@ -31,12 +32,22 @@ int __init btrfs_init_block_group(void)
0, 0, NULL);
if (!block_group_cache)
return -ENOMEM;
free_space_ctl_cache = kmem_cache_create("btrfs_free_space_ctl",
sizeof(struct btrfs_free_space_ctl),
0, 0, NULL);
if (!free_space_ctl_cache) {
kmem_cache_destroy(block_group_cache);
return -ENOMEM;
}
return 0;
}
void __cold btrfs_exit_block_group(void)
{
kmem_cache_destroy(block_group_cache);
kmem_cache_destroy(free_space_ctl_cache);
}
#ifdef CONFIG_BTRFS_DEBUG
@@ -197,7 +208,7 @@ void btrfs_put_block_group(struct btrfs_block_group *cache)
btrfs_discard_cancel_work(&cache->fs_info->discard_ctl,
cache);
kfree(cache->free_space_ctl);
kmem_cache_free(free_space_ctl_cache, cache->free_space_ctl);
btrfs_free_chunk_map(cache->physical_map);
kmem_cache_free(block_group_cache, cache);
}
@@ -2392,7 +2403,7 @@ static struct btrfs_block_group *btrfs_create_block_group(
if (!cache)
return NULL;
cache->free_space_ctl = kzalloc_obj(*cache->free_space_ctl, GFP_NOFS);
cache->free_space_ctl = kmem_cache_zalloc(free_space_ctl_cache, GFP_NOFS);
if (!cache->free_space_ctl) {
kmem_cache_free(block_group_cache, cache);
return NULL;