Skip to content

Memory leak: arena_extent_dalloc() is not called for extents for which arena_extent_split() failed #2815

Open

Description

@ldorau

There is a memory leak: arena_extent_dalloc() is not called for extents for which arena_extent_split() failed

An example "trace" from gdb (breakpoints were set in all jemalloc hooks):

arena_extent_alloc (extent_hooks=0x7ffff7da6640 <arena_extent_hooks>, new_addr=0x0, size=2097152, alignment=4096, zero=0x7fffffffbf1e, commit=0x7fffffffbf1f, arena_ind=49)
arena_extent_split (extent_hooks=0x7ffff7da6640 <arena_extent_hooks>, addr=0x7ffff67ff000, size=2097152, size_a=4096, size_b=2093056, committed=true, arena_ind=49) - FAILED
arena_extent_alloc (extent_hooks=0x7ffff7da6640 <arena_extent_hooks>, new_addr=0x0, size=4096, alignment=4096, zero=0x7fffffffbe6c, commit=0x7fffffffbf1d, arena_ind=49)
arena_extent_dalloc (extent_hooks=0x7ffff7da6640 <arena_extent_hooks>, addr=0x7ffff7702000, size=4096, committed=true, arena_ind=49)

As you can see above arena_extent_dalloc() was not called for the first allocated extent.

jemalloc commit: 81f35e0

Ref: oneapi-src/unified-memory-framework#902 (comment)

Activity

  1. interwq commented on Mar 14, 2025

    @interwq
    Contributor

    Thanks for reporting and tracking this down. It looks there's custom hooks installed, can you share the path leading to the split hook? Did it reach this function?

    extents_abandon_vm(tsdn, pac, ehooks,

    I wonder if we are missing some error handling after the split failure somewhere or the above case

  2. ldorau commented on Mar 15, 2025

    @ldorau
    Author

    can you share the path leading to the split hook? Did it reach this function?

    The path leading to the split hook:

    Breakpoint 8, arena_extent_split (extent_hooks=0x7ffff7da6640 <arena_extent_hooks>, addr=0x7ffff67ff000, size=2097152, size_a=4096, size_b=2093056, committed=true, arena_ind=49) at /home/ldorau/work/unified-memory-framework/src/pool/pool_jemalloc.c:238
    238         assert(size_a + size_b == size);
    #0  arena_extent_split (extent_hooks=0x7ffff7da6640 <arena_extent_hooks>, addr=0x7ffff67ff000, size=2097152, size_a=4096, size_b=2093056,
        committed=true, arena_ind=49) at /home/ldorau/work/unified-memory-framework/src/pool/pool_jemalloc.c:238
    #1  0x00007ffff7d4e390 in ehooks_split (committed=<optimized out>, size_b=2093056, size_a=4096, size=2097152, addr=<optimized out>,
        ehooks=0x7ffff6a000c0, tsdn=0x7ffff773d768) at include/jemalloc/internal/ehooks.h:104
    #2  extent_split_impl (tsdn=0x7ffff773d768, pac=0x7ffff6a04020, ehooks=ehooks@entry=0x7ffff6a000c0, edata=0x7ffff6a17000,
        size_a=size_a@entry=4096, size_b=size_b@entry=2093056, holding_core_locks=<optimized out>) at src/extent.c:1250
    #3  0x00007ffff7d4e52b in extent_split_interior (tsdn=tsdn@entry=0x7ffff773d768, pac=pac@entry=0x7ffff6a04020,
        ehooks=ehooks@entry=0x7ffff6a000c0, edata=edata@entry=0x7fffffffc190, lead=lead@entry=0x7fffffffc198, trail=trail@entry=0x7fffffffc1a0,
        to_leak=0x7fffffffc1a8, to_salvage=0x7fffffffc1b0, size=4096, alignment=4096, expand_edata=0x0) at src/extent.c:522
    #4  0x00007ffff7d512d7 in extent_grow_retained (commit=0x7fffffffc18d, zero=false, alignment=4096, size=4096, ehooks=0x7ffff6a000c0,
        pac=0x7ffff6a04020, tsdn=0x7ffff773d768) at src/extent.c:710
    #5  extent_alloc_retained (guarded=<optimized out>, commit=0x7fffffffc18d, zero=<optimized out>, alignment=<optimized out>,
        size=140737344952168, expand_edata=0x0, ehooks=<optimized out>, pac=0x7ffff6a04020, tsdn=0x7ffff773d768) at src/extent.c:802
    #6  je_ecache_alloc_grow (tsdn=tsdn@entry=0x7ffff773d768, pac=pac@entry=0x7ffff6a04020, ehooks=<optimized out>,
        ecache=ecache@entry=0x7ffff6a0d838, expand_edata=expand_edata@entry=0x0, size=size@entry=4096, alignment=<optimized out>,
        zero=<optimized out>, guarded=<optimized out>) at src/extent.c:113
    #7  0x00007ffff7d5befe in pac_alloc_real (tsdn=tsdn@entry=0x7ffff773d768, pac=pac@entry=0x7ffff6a04020, ehooks=ehooks@entry=0x7ffff6a000c0,
        size=size@entry=4096, alignment=alignment@entry=4096, zero=zero@entry=false, guarded=false) at src/pac.c:198
    #8  0x00007ffff7d5c040 in pac_alloc_impl (tsdn=0x7ffff773d768, self=0x7ffff6a04020, size=4096, alignment=4096, zero=<optimized out>,
        guarded=<optimized out>, frequent_reuse=true, deferred_work_generated=0x7fffffffc357) at src/pac.c:254
    #9  0x00007ffff7d5afb1 in pai_alloc (deferred_work_generated=0x7fffffffc357, frequent_reuse=true, guarded=false, zero=false,
        alignment=<optimized out>, size=4096, self=0x7ffff6a04020, tsdn=0x7ffff773d768) at include/jemalloc/internal/pai.h:47
    #10 je_pa_alloc (tsdn=tsdn@entry=0x7ffff773d768, shard=shard@entry=0x7ffff6a04008, size=4096, alignment=alignment@entry=4096,
        slab=slab@entry=true, szind=szind@entry=4, zero=false, guarded=false, deferred_work_generated=0x7fffffffc357) at src/pa.c:139
    #11 0x00007ffff7cf761d in arena_slab_alloc (tsdn=tsdn@entry=0x7ffff773d768, arena=arena@entry=0x7ffff6a01040, binind=binind@entry=4,
        binshard=binshard@entry=0, bin_info=bin_info@entry=0x7ffff7fb88c0 <je_bin_infos+160>) at src/arena.c:980
    #12 0x00007ffff7cfbb96 in arena_malloc_small (zero=<optimized out>, binind=4, arena=0x7ffff6a01040, tsdn=0x7ffff773d768) at src/arena.c:1318
    #13 je_arena_malloc_hard (tsdn=tsdn@entry=0x7ffff773d768, arena=arena@entry=0x7ffff6a01040, size=size@entry=64, ind=ind@entry=4,
        zero=zero@entry=false, slab=<optimized out>) at src/arena.c:1367
    #14 0x00007ffff7cf0694 in arena_malloc (slow_path=<optimized out>, tcache=<optimized out>, slab=<optimized out>, zero=<optimized out>,
        ind=<optimized out>, size=<optimized out>, arena=<optimized out>, tsdn=<optimized out>) at include/jemalloc/internal/arena_inlines_b.h:211
    #15 iallocztm_explicit_slab (slow_path=<optimized out>, arena=<optimized out>, is_internal=<optimized out>, tcache=<optimized out>,
        slab=<optimized out>, zero=<optimized out>, ind=<optimized out>, size=<optimized out>, tsdn=<optimized out>)
        at include/jemalloc/internal/jemalloc_internal_inlines_c.h:70
    #16 imalloc_no_sample (slab=true, ind=<optimized out>, usize=64, size=64, tsd=<optimized out>, dopts=<synthetic pointer>,
        sopts=<synthetic pointer>) at src/jemalloc.c:2477
    #17 imalloc_body (tsd=<optimized out>, dopts=<synthetic pointer>, sopts=<synthetic pointer>) at src/jemalloc.c:2650
    #18 imalloc (dopts=<synthetic pointer>, sopts=<synthetic pointer>) at src/jemalloc.c:2764
    #19 je_mallocx (size=64, flags=<optimized out>) at src/jemalloc.c:3424
    #20 0x00007ffff7cdac07 in op_malloc (pool=0x7ffff773b088, size=64) at /home/ldorau/work/unified-memory-framework/src/pool/pool_jemalloc.c:294
    #21 0x00007ffff7cc46b7 in umfPoolMalloc (hPool=0x7ffff772a268, size=64) at /home/ldorau/work/unified-memory-framework/src/memory_pool.c:159
    #22 0x0000555555567561 in umfPoolTest_allocFree_Test::TestBody (this=0x555555665ff0)
        at /home/ldorau/work/unified-memory-framework/test/poolFixtures.hpp:136
    #23 0x00005555555e512b in testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void> (object=0x555555665ff0,
        method=&virtual testing::Test::TestBody(), location=0x55555560344b "the test body")
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:2638
    #24 0x00005555555dd419 in testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void> (object=0x555555665ff0,
        method=&virtual testing::Test::TestBody(), location=0x55555560344b "the test body")
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:2674
    #25 0x00005555555b696a in testing::Test::Run (this=0x555555665ff0)
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:2713
    #26 0x00005555555b73fa in testing::TestInfo::Run (this=0x5555556646d0)
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:2859
    #27 0x00005555555b7d98 in testing::TestSuite::Run (this=0x5555556643a0)
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:3037
    #28 0x00005555555c870c in testing::internal::UnitTestImpl::RunAllTests (this=0x555555660eb0)
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:5967
    #29 0x00005555555e624e in testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool> (
        object=0x555555660eb0,
        method=(bool (testing::internal::UnitTestImpl::*)(testing::internal::UnitTestImpl * const)) 0x5555555c82f2 <testing::internal::UnitTestImpl::RunAllTests()>, location=0x555555603f10 "auxiliary test code (environments or event listeners)")
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:2638
    #30 0x00005555555de6bf in testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool> (
        object=0x555555660eb0,
        method=(bool (testing::internal::UnitTestImpl::*)(testing::internal::UnitTestImpl * const)) 0x5555555c82f2 <testing::internal::UnitTestImpl::RunAllTests()>, location=0x555555603f10 "auxiliary test code (environments or event listeners)")
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:2674
    #31 0x00005555555c6d1a in testing::UnitTest::Run (this=0x55555564e8e0 <testing::UnitTest::GetInstance()::instance>)
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest.cc:5546
    #32 0x00005555555a2c22 in RUN_ALL_TESTS ()
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/include/gtest/gtest.h:2334
    #33 0x00005555555a2b9b in main (argc=1, argv=0x7fffffffcaf8)
        at /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest_main.cc:64
    
  3. ldorau commented on May 13, 2025

    @ldorau
    Author

    ping, any updates on this issue?

  4. interwq commented on May 14, 2025

    @interwq
    Contributor

    @ldorau thanks for following up. The error path is triggered by the extent split failure, which I suspect is not the expected behavior by your custom extent hooks -- can you please check if your split hook is set to split allowed (i.e. returning false)? Usually there is no much point disallowing split / merge of extents on mmap based systems.

  5. ldorau commented on May 14, 2025

    @ldorau
    Author

    @ldorau thanks for following up. The error path is triggered by the extent split failure, which I suspect is not the expected behavior by your custom extent hooks -- can you please check if your split hook is set to split allowed (i.e. returning false)? Usually there is no much point disallowing split / merge of extents on mmap based systems.

    @interwq

    1. The failure of the extent split hook IS the expected behavior in my case. I use an own simple allocator that does not support splitting and merging.
    2. That's exactly what this issue is about: there is a memory leak in jemalloc: arena_extent_dalloc() is not called for extents for which arena_extent_split() fails.
  6. interwq commented on May 14, 2025

    @interwq
    Contributor

    When split / merge isn't allowed, you will need to disable retain. that option always attempts to over-reserve but doesn't help without split. In fact when retain is enabled, dalloc will never be called by default (purge will be called instead to only return the physical memory).

    Try setting "MALLOC_CONF=retain:false" which will do the alloc / dalloc pair you are looking for.

    As for the error handling inside the retain path, it can be improved indeed (see the comments around extents_abandon_vm). But at the same time it's not the intended use case for no split, so avoiding the retain option should be the right solution for you.

  7. ldorau commented on May 14, 2025

    @ldorau
    Author

    Running with MALLOC_CONF="retain:false" nor adding const char *malloc_conf = "retain:false"; to the code does not help, the output is the same like it had been before:

    $ MALLOC_CONF="retain:false" ./test/test_jemalloc_pool --gtest_filter="*.allocFree/1*"
    [PID:20456  TID:20456  INFO  UMF] utils_log_init: Logger enabled (UMF version: 0.11.0-dev4.git12.g10516cf3, level: DEBUG, flush: DEBUG, pid: yes, timestamp: no)
    [PID:20456  TID:20456  DEBUG UMF] umf_ba_create_global: UMF base allocator created
    [PID:20456  TID:20456  DEBUG UMF] umfMemoryTrackerCreate: tracker created, handle=0x7fa8e5928068, alloc_segments_map=0x7fa8e5928070
    [PID:20456  TID:20456  DEBUG UMF] umfInit: UMF tracker created
    [PID:20456  TID:20456  DEBUG UMF] umfInit: UMF IPC cache initialized
    [PID:20456  TID:20456  DEBUG UMF] umfInit: UMF library initialized
    [PID:20456  TID:20456  INFO  UMF] utils_log_init: Logger enabled (UMF version: 0.11.0-dev4.git12.g10516cf3, level: DEBUG, flush: DEBUG, pid: yes, timestamp: no)
    Running main() from /home/ldorau/work/unified-memory-framework/build/_deps/googletest-src/googletest/src/gtest_main.cc
    Note: Google Test filter = *.allocFree/1*
    [==========] Running 1 test from 1 test suite.
    [----------] Global test environment set-up.
    [----------] 1 test from jemallocPoolTest/umfPoolTest
    [ RUN      ] jemallocPoolTest/umfPoolTest.allocFree/1
    [PID:20456  TID:20456  DEBUG UMF] umfTrackingMemoryProviderCreate: upstream=0x7fa8e5928168, tracker=0x7fa8e5928068, pool=0x7fa8e5928268, ipcCache=0x7fa8e5916008, hIpcMappedCache=0x7fa8e5939068
    [PID:20456  TID:20456  INFO  UMF] umfPoolCreateInternal: Memory pool created: 0x7fa8e5928268
    [PID:20456  TID:20456  DEBUG UMF] umfMemoryTrackerAddAtLevel: memory region is added, tracker=0x7fa8e5928068, level=0, pool=0x7fa8e5928268, ptr=0x7fa8e49ff000, size=2097152
    [PID:20456  TID:20456  ERROR UMF] trackingAllocationSplit: upstream provider failed to split the region
    [PID:20456  TID:20456  ERROR UMF] trackingAllocationSplit: failed to split memory region: ptr=0x7fa8e49ff000, totalSize=2097152, firstSize=4096
    [PID:20456  TID:20456  DEBUG UMF] umfMemoryTrackerAddAtLevel: memory region is added, tracker=0x7fa8e5928068, level=0, pool=0x7fa8e5928268, ptr=0x7fa8e5900000, size=4096
    [PID:20456  TID:20456  DEBUG UMF] umfMemoryTrackerRemove: memory region removed: tracker=0x7fa8e5928068, level=0, pool=0x7fa8e5928268, ptr=0x7fa8e5900000, size=4096
    [PID:20456  TID:20456  ERROR UMF] check_if_tracker_is_empty: [0] ptr = 0x7fa8e49ff000, pool = 0x7fa8e5928268, size = 2097152
    [PID:20456  TID:20456  ERROR UMF] check_if_tracker_is_empty: tracking provider of pool 0x7fa8e5928268 is not empty! (1 items left)
    test_jemalloc_pool: /home/ldorau/work/unified-memory-framework/src/provider/provider_tracking.c:827: check_if_tracker_is_empty: Assertion `n_items == 0 && "tracking provider is not empty!"' failed.
    Aborted
    
  8. interwq commented on May 14, 2025

    @interwq
    Contributor

    Did you build with a custom prefix? If so you need to prefix the malloc_conf as well. One quick way to check if it takes effect is, adding a garbage string into it, e.g. "abort_conf:true,abcd:1234" which will abort on init.

    When retain is disabled, the extent_alloc_retained path isn't called so that can't be the source of the issue anymore.

  9. ldorau commented on May 15, 2025

    @ldorau
    Author

    Did you build with a custom prefix? If so you need to prefix the malloc_conf as well.

    Yes, we build jemalloc with a custom prefix, but we cannot disable retain permanently, because it is needed in most cases (splitting is supported in most cases).

    Can retain be disabled in runtime somehow? Unfortunately setting the environment variable MALLOC_CONF="retain:false" does not work in our case (see #2815 (comment) above),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions