lib: test bitmap vs Maple Tree performance for region allocations
Subsystems (7)
PATCH 1/1 lib: test bitmap vs Maple Tree performance for region allocations
Compare the cost of allocating and freeing variable-sized regions using a bitmap and a Maple Tree. Both implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Run the benchmark at several capacities to show how the two approaches scale. Report allocation and free times separately because bitmap clearing and Maple Tree removal have substantially different costs.
On x86/kvm, the output example is:
Start testing bitmap vs Maple Tree region allocation
type alloc (ns) free (ns) capacity memory (B)
bitmap 100191761 187248 1000000 125000
maple 12256712 12665044 1000000 1555472
bitmap 1094833 22182 100000 12504 maple 1348996 1120168 100000 157456
bitmap 19566 2230 10000 1256
maple 109854 111542 10000 16144
bitmap 918 222 1000 128
maple 8993 7986 1000 1552
Maple Tree memory consumption is a lower-bound estimate because it does not account for internal nodes, partially occupied nodes, slab overhead, and transient allocations.
The benchmark is motivated by the discussion at the link below about choosing the best data structure for the channel ID pool with the capacity of 2048 IDs for the nova GPU driver.
Specifically for 2048 IDs the result is:
bitmap 2261 472 2048 256
maple 21743 24055 2048 3344
---
The numbers are collected on top of bitmap_find_next_zero_area_off() optimization:
https://lore.kernel.org/all/[email protected]/
It's in -next and scheduled for the next merge window.
MAINTAINERS | 2 + lib/Kconfig.debug | 9 +++ lib/Makefile | 1 + lib/region_alloc_benchmark.c | 112 +++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 lib/region_alloc_benchmark.c
MAINTAINERS +2 -0
@@ -4615,6 +4615,7 @@ F: lib/bitmap.c F: lib/cpumask.c F: lib/find_bit.c F: lib/find_bit_benchmark.c +F: lib/region_alloc_benchmark.c F: lib/test_bitmap.c F: lib/tests/cpumask_kunit.c F: tools/include/linux/bitfield.h
@@ -15581,6 +15582,7 @@ F: Documentation/core-api/maple_tree.rst F: include/linux/maple_tree.h F: include/trace/events/maple_tree.h F: lib/maple_tree.c +F: lib/region_alloc_benchmark.c F: lib/test_maple_tree.c F: rust/helpers/maple_tree.c F: rust/kernel/maple_tree.rs
lib/Kconfig.debug +9 -0
@@ -2683,6 +2683,15 @@ config FIND_BIT_BENCHMARK If unsure, say N. +config REGION_ALLOC_BENCHMARK + tristate "Benchmark bitmap and Maple Tree region allocation" + help + This builds a microbenchmark comparing variable-sized region + allocation using bitmaps and Maple Trees. The benchmark runs at + module load time. + + If unsure, say N. + config FIND_BIT_BENCHMARK_RUST tristate "Test find_bit functions in Rust" depends on RUST
lib/Makefile +1 -0
@@ -64,6 +64,7 @@ obj-y += hexdump.o obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o obj-y += kstrtox.o obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o +obj-$(CONFIG_REGION_ALLOC_BENCHMARK) += region_alloc_benchmark.o obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o obj-$(CONFIG_TEST_BPF) += test_bpf.o test_dhry-objs := dhry_1.o dhry_2.o dhry_run.o
lib/region_alloc_benchmark.c new file +112 -0
112 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Large uninitialized static arrays marked with `__initdata` bloat the kernel binary by ~9.1MB.…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Large uninitialized static arrays marked with __initdata bloat the kernel binary by ~9.1MB.
- [Medium] Transient memory allocation failures trigger false positive WARN_ON bug reports.
- [Medium] Initializing a maple tree on the stack with MTREE_INIT causes a lockdep splat. --
--- Patch [1]: [PATCH] lib: test bitmap vs Maple Tree performance for region allocations --- commit b546f14f1527daeb17ae5d45e14a867ddc37369e
Author: Yury Norov <[email protected]>
lib: test bitmap vs Maple Tree performance for region allocations
This commit adds a microbenchmark to compare the performance of allocating and freeing variable-sized regions. It tests a bitmap implementation against a Maple Tree implementation across several capacities when the module loads.
Quoted 14 lines
lib/region_alloc_benchmark.c new file +8
-0@@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Benchmark bitmap and Maple Tree allocation of variable-sized regions. */ + +#include <linux/bitmap.h> +#include <linux/kernel.h> +#include <linux/maple_tree.h> +#include <linux/module.h> +#include <linux/printk.h>
lib/region_alloc_benchmark.c line 18
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+#include <linux/random.h> +#include <linux/xarray.h> + +#define MAP_SIZE (1000000UL) +#define REGION_MAX_SIZE 32 + +static DECLARE_BITMAP(alloc_bitmap, MAP_SIZE) __initdata; +/* One more request guarantees that even an all-ones trace reaches ENOSPC. */ +static u8 region_sizes[MAP_SIZE +1 ]__initdata ; +static unsigned long region_indexes[MAP_SIZE ]__initdata ;
[Severity: Medium]
Does using the __initdata macro on these large uninitialized arrays push them into the PROGBITS .init.data section instead of .bss?
This can bloat the vmlinux image by around 9.1MB at build time, which might cause boot or build failures on memory-constrained embedded architectures with strict size limits.
[ ... ]
lib/region_alloc_benchmark.c line 54
Quoted 3 lines
lib/region_alloc_benchmark.c new file +3
-0+static unsigned long __init benchmark_maple_tree (unsigned long capacity) +{ +struct maple_tree mt =MTREE_INIT (mt,MT_FLAGS_ALLOC_RANGE );
[Severity: Medium]
Is it safe to initialize this maple tree on the stack using MTREE_INIT?
The MTREE_INIT macro uses __SPIN_LOCK_UNLOCKED, which relies on a C99 compound literal for the lockdep lock class key. At block scope, C99 compound literals have automatic storage duration. This places the lockdep key on the stack and triggers a lockdep warning about registering a non-static key.
Should dynamic initialization with mt_init_flags() be used here instead?
Quoted 7 lines
lib/region_alloc_benchmark.c new file +7
-0+unsigned long count, index; +ktime_t alloc_time, free_time; +size_t memory; +int ret; + + alloc_time =ktime_get (); +for (count =0 ; count <ARRAY_SIZE (region_sizes); count++) {
lib/region_alloc_benchmark.c line 71
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+ ret =mtree_alloc_range (&mt, &index,xa_mk_value (count +1 ), + region_sizes[count],0 , capacity -1 , +GFP_KERNEL ); +if (ret) +break ; + + region_indexes[count] = index; + } + alloc_time =ktime_get () - alloc_time; +WARN_ON (ret != -EBUSY );
[Severity: Medium]
Could mtree_alloc_range() legitimately fail with -ENOMEM?
Since it uses GFP_KERNEL to allocate tree nodes internally, transient memory pressure could cause an allocation failure. If that happens, this WARN_ON will trigger a false positive bug report.
[ ... ]
Quoted 6 lines
lib/region_alloc_benchmark.c new file +6
-0+static int __init region_alloc_benchmark (void ) +{ +static const unsigned long capacities[] = {1000000 ,100000 ,10000 ,1000 }; +unsigned long bitmap_count, maple_count; +unsigned long i; +
lib/region_alloc_benchmark.c line 103
Quoted 10 lines
lib/region_alloc_benchmark.c new file +10
-0+for (i =0 ; i <ARRAY_SIZE (region_sizes); i++) + region_sizes[i] =get_random_u32_below (REGION_MAX_SIZE ) +1 ; + +pr_err ("\nStart testing bitmap vs Maple Tree region allocation\n" ); +pr_err ("%-6s %12s %12s %8s %10s\n" ,"type" ,"alloc (ns)" ,"free (ns)" , +"capacity" ,"memory (B)" ); +for (i =0 ; i <ARRAY_SIZE (capacities); i++) { + bitmap_count =benchmark_bitmap (capacities[i]); + maple_count =benchmark_maple_tree (capacities[i]); +WARN_ON (bitmap_count != maple_count);
[Severity: Medium]
If benchmark_maple_tree() experiences an early -ENOMEM failure, will it return a different element count than benchmark_bitmap(), leading to a secondary false positive warning here?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Matthew Wilcox
maintainerQuoted 4 lines · Yury Norov
Compare the cost of allocating and freeing variable-sized regions using a bitmap and a Maple Tree. Both implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
I'd suggest the IDA might be a better comparison than the maple tree. Essentially it's a sparse bitmap which plays some fun tricks to lower the cost.
Eventually, I expect the IDA to be reimplemented on top of the maple tree data structure instead of the XArray.
Quoted 6 lines · Matthew Wilcox
Quoted 4 lines · Yury Norov
Compare the cost of allocating and freeing variable-sized regions using a bitmap and a Maple Tree. Both implementations process the same randomly generated sequence of region sizes, ranging from 1 to 32 entries, until the configured capacity is exhausted.
Quoted 6 lines
I'd suggest the IDA might be a better comparison than the maple tree. Essentially it's a sparse bitmap which plays some fun tricks to lower the cost.
Eventually, I expect the IDA to be reimplemented on top of the maple tree data structure instead of the XArray.
Sure, will send v2