Skip to content

os/bluestore: fix free-run lookup in _try_remove_from_tree() for the three tree allocators - #71656

Merged
Jayaprakash-ibm merged 5 commits into
ceph:mainfrom
Naveenaidu:wip-naveen-fix-try-remove-from-tree
Nov 2, 2026
Merged

Jayaprakash-ibm merged 5 commits into
ceph:mainfrom
Naveenaidu:wip-naveen-fix-try-remove-from-tree

Conversation

@Naveenaidu

@Naveenaidu Naveenaidu commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes: https://tracker.ceph.com/issues/80417

The following are the failures that we see before the fixes happens:

AVLAllocator

No failures seen, because the range_tree.find() implementation has lower_bound() already in it. Ref: boost avl_tree, find() and boost avl_tree, find() implementation

BtreeAllocator

[  RUN  ] BtreeAllocator.try_remove_run_starting_before_query (0 ms)
/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:165: Failure
Expected equality of these values:
  (std::vector<cb_rec_t>{{150 * _64k, 30 * _64k, true}})
    Which is: { {[150,180) found} }
  recs
    Which is: { {[150,180) missing} }

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:166: Failure
Expected equality of these values:
  30u * _64k
    Which is: 1966080
  freed
    Which is: 0

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:167: Failure
Expected equality of these values:
  (std::vector<extent_t>{ {100 * _64k, 50 * _64k}, {180 * _64k, 20 * _64k}, {300 * _64k, 50 * _64k}, })
    Which is: { [100,150), [180,200), [300,350) }
  extents(a)
    Which is: { [100,200), [300,350) }

[  FAILED  ] BtreeAllocator.try_remove_run_starting_before_query (0 ms)
[ RUN      ] BtreeAllocator.try_remove_iterator_stays_valid_across_removal                                           /home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:198: Failure
Expected equality of these values:
  (std::vector<cb_rec_t>{ {0, 10 * _64k, true}, {10 * _64k, 10 * _64k, false}, {20 * _64k, 10 * _64k, true}, {30 * _64k, 10 * _64k, false}, {40 * _64k, 5 * _64k, true}, })
    Which is: { {[0,10) found}, {[10,20) missing}, {[20,30) found}, {[30,40) missing}, {[40,45) found} }
  recs
    Which is: { {[0,10) found}, {[10,40) missing}, {[40,45) found} }

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:205: Failure
Expected equality of these values:
  25u * _64k
    Which is: 1638400
  freed
    Which is: 983040

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:206: Failure
Expected equality of these values:
  (std::vector<extent_t>{ {45 * _64k, 5 * _64k}, })
    Which is: { [45,50) }
  extents(a)
    Which is: { [20,30), [45,50) }

[  FAILED  ] BtreeAllocator.try_remove_iterator_stays_valid_across_removal (0 ms)

Btree2Allocator

[ RUN      ] Btree2Allocator.try_remove_predecessor_not_overlapping
/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:101: Failure
Failed
callback {[20,10) found} is outside query [20,30)

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:270: Failure
Expected equality of these values:
  (std::vector<cb_rec_t>{ {20 * _64k, 10 * _64k, false}, })
    Which is: { {[20,30) missing} }
  recs
    Which is: { {[20,10) found} }

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:273: Failure
Expected equality of these values:
  0u
    Which is: 0
  freed
    Which is: 18446744073708896256

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:274: Failure
Expected equality of these values:
  (std::vector<extent_t>{ {0, 10 * _64k}, {40 * _64k, 10 * _64k}, })
    Which is: { [0,10), [40,50) }
  extents(a)
    Which is: { [0,20), [40,50) }

[  FAILED  ] Btree2Allocator.try_remove_predecessor_not_overlapping (0 ms)
[ RUN      ] Btree2Allocator.try_remove_leftover_split_makes_progress
/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:101: Failure
Failed
callback {[100,50) found} is outside query [50,200)

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:302: Failure
Expected equality of these values:
  (std::vector<cb_rec_t>{ {50 * _64k, 50 * _64k, true}, {100 * _64k, 50 * _64k, false}, {150 * _64k, 10 * _64k, true}, {160 * _64k, 40 * _64k, false}, })
    Which is: { {[50,100) found}, {[100,150) missing}, {[150,160) found}, {[160,200) missing} }
  recs
    Which is: { {[50,100) found}, {[100,50) found} }

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:308: Failure
Expected equality of these values:
  60u * _64k
    Which is: 3932160
  freed
    Which is: 0

/home/naveenaidu/naveen/ceph/src/test/objectstore/allocator_try_remove_from_tree_test.cc:309: Failure
Expected equality of these values:
  (std::vector<extent_t>{ {0, 50 * _64k}, })
    Which is: { [0,50) }
  extents(a)
    Which is: { [0,100), [150,160) }
                                                                                 
[  FAILED  ] Btree2Allocator.try_remove_leftover_split_makes_progress (0 ms)
[----------] 2 tests from Btree2Allocator (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.                                                                                                [  FAILED  ] 2 tests, listed below:
[  FAILED  ] Btree2Allocator.try_remove_predecessor_not_overlapping
[  FAILED  ] Btree2Allocator.try_remove_leftover_split_makes_progress

 2 FAILED TESTS

After the fix commits

naveenaidu@sockeni05:~/naveen/ceph/build$ sudo ninja unittest_alloc_try_remove_from_tree && ./bin/unittest_alloc_try_remove_from_tree
ninja: no work to do.
[==========] Running 7 tests from 3 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from AvlAllocator
[ RUN      ] AvlAllocator.try_remove_spanning_disjoint_runs
[       OK ] AvlAllocator.try_remove_spanning_disjoint_runs (0 ms)
[ RUN      ] AvlAllocator.try_remove_inside_single_run
[       OK ] AvlAllocator.try_remove_inside_single_run (0 ms)
[----------] 2 tests from AvlAllocator (0 ms total)

[----------] 3 tests from BtreeAllocator
[ RUN      ] BtreeAllocator.try_remove_run_starting_before_query
[       OK ] BtreeAllocator.try_remove_run_starting_before_query (0 ms)
[ RUN      ] BtreeAllocator.try_remove_iterator_stays_valid_across_removal
[       OK ] BtreeAllocator.try_remove_iterator_stays_valid_across_removal (0 ms)
[ RUN      ] BtreeAllocator.try_remove_entirely_past_free_runs
[       OK ] BtreeAllocator.try_remove_entirely_past_free_runs (0 ms)
[----------] 3 tests from BtreeAllocator (0 ms total)

[----------] 2 tests from Btree2Allocator
[ RUN      ] Btree2Allocator.try_remove_predecessor_not_overlapping
[       OK ] Btree2Allocator.try_remove_predecessor_not_overlapping (0 ms)
[ RUN      ] Btree2Allocator.try_remove_leftover_split_makes_progress
[       OK ] Btree2Allocator.try_remove_leftover_split_makes_progress (0 ms)
[----------] 2 tests from Btree2Allocator (0 ms total)

[----------] Global test environment tear-down
[==========] 7 tests from 3 test suites ran. (0 ms total)
[  PASSED  ] 7 tests.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@Naveenaidu
Naveenaidu requested a review from a team as a code owner September 9, 2026 15:15
@Naveenaidu Naveenaidu self-assigned this Sep 9, 2026
@Naveenaidu
Naveenaidu requested a review from aclamk September 9, 2026 15:26
@ifed01

ifed01 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Could you please make unit tests to reproduce the issues and check the fixes?

@ifed01
ifed01 self-requested a review September 14, 2026 19:18
@ifed01 ifed01 mentioned this pull request Sep 15, 2026
3 of 14 tasks
Comment thread src/os/bluestore/Btree2Allocator.cc Outdated
cb(start, range_end - start, true);
start = range_end;

// _remove_from_tree() returns an iterator to the range it re-inserted,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we instead modify _remove_from_tree in a way to always return iterator pointing after removed entry. I.e. use the approach similar to btree and std collections. In other words left-over items shouldn't cause return value assignment...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this! I have fixed it. _remove_from_tree returns the iterator that points to the next free run after the range that was removed

@Naveenaidu
Naveenaidu force-pushed the wip-naveen-fix-try-remove-from-tree branch from 4cd130e to 0065bb9 Compare September 16, 2026 05:53
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-fix-try-remove-from-tree branch 5 times, most recently from b52d45b to 69b3812 Compare September 21, 2026 08:51
@Naveenaidu
Naveenaidu requested a review from ifed01 September 21, 2026 09:41
Comment thread src/os/bluestore/BtreeAllocator.cc Outdated
auto length = end - start;
ceph_assert(num_free >= length);
num_free -= length;
return range_tree.lower_bound(end);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 places that use _process_range_removal: _try_remove_from_tree and _remove_from_tree.
Only the first one needs to reacquire iterator, so it would be better to do the action in _try_remove_from_tree.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it thank you!

Naveen Naidu added 5 commits October 12, 2026 17:26
Add AvlAllocator coverage to unittest_alloc.

Unlike the btree allocators, AvlAllocator does not need a fix here.
range_tree is ordered by an overlap comparator (lhs.end <= rhs.start),
so every free run overlapping [start, end) compares equal to the search
key.  boost::intrusive's find() is implemented as lower_bound() followed
by an equality check, so it already returns the leftmost overlapping run
rather than whichever one a tree traversal reaches first, and the
forward walk visits every overlapping run.

The tests just verify that this holds for a query spanning several
disjoint runs (inserted right-to-left so the leftmost run is not the
root) and for a query inside a single run.  Also document why find() is
correct at the call site.

Signed-off-by: Naveen Naidu <[email protected]>
…_from_tree

Make BtreeAllocator::_try_remove_from_tree() protected instead of
private, so a thin test subclass can re-expose it the same way it
already does for AvlAllocator and Btree2Allocator, and extend
unittest_alloc with BtreeAllocator coverage.

Fixes: https://tracker.ceph.com/issues/80417
Signed-off-by: Naveen Naidu <[email protected]>
…ation in _try_remove_from_tree

_try_remove_from_tree() had two latent defects:

- It looked up the first overlapping free run with range_tree.find(start),
  an exact-key lookup that returns end() unless a run begins exactly at
  'start'. A run starting before 'start' and extending into [start, end)
  was missed entirely. We now use lower_bound(start) and step back to the
  preceding run when it actually reaches past 'start'.

  Example:
     range_tree: `[100, 200), [300, 350)`, Query: `[150, 180)`
     The `range_tree.find(150)`  returns `[300, 350)` -> WRONG

- It cached the successor iterator (next_rs) before calling
  _process_range_removal(), which can insert into range_tree and
  invalidate it. The caller now re-acquires the iterator with
  range_tree.lower_bound(end) after _process_range_removal() is done with
  its tree mutations. This is done in _try_remove_from_tree() rather than
  in _process_range_removal(), so that _remove_from_tree(), which doesn't
  need the iterator, doesn't pay for the extra lookup.

  Example:
    range_tree: `[0,10), [20,30), [40,50)`, to remove: `[5, 25)
    After `_process_range_removal`:
      before: [0, 10)   [20, 30)   [40, 50)
      after:  [0, 5)    [25, 30)   [40, 50)

    The issue:
    ```
    auto next_rs = std::next(rs); // points to [20, 30)
    _process_range_removal(rs, 5, 25);
    rs = next_rs; // unsafe, because rs still points to [20,30) when it should point to [25,30)
    ```

Fixes: https://tracker.ceph.com/issues/80417
Signed-off-by: Naveen Naidu <[email protected]>
range_tree maps each free range's start to its end. _try_remove_from_tree()
finds the first range overlapping [start, end) with lower_bound(), then walks
forward trimming ranges through the callback.

1. Wrong step-back. lower_bound(start) may return the range just after the one
   that straddles 'start', so the code steps back one node. It did this
   whenever that node started before 'start', without also checking that it
   ends after 'start'. A non-overlapping predecessor was then used, and
   range_end = min(prev_end, end) could fall below 'start', invoking
   _remove_from_tree() with end < start.
   Fix: step back only if the predecessor ends past 'start'.

2. No forward progress on a left-over split. When a range reaches below
   'start', _remove_from_tree() re-inserts the remainder and returns an
   iterator to it. The loop never skipped it, so it re-processed that segment
   with range_end < start - the same malformed removal as above.
   Fix: in _remove_from_tree() step over the remainder once it has been
   re-inserted, so the returned iterator points to the range following the
   removed one. If the remainder is spilled over instead, the iterator
   already points there and is left as is.

Fixes: https://tracker.ceph.com/issues/80417
Signed-off-by: Naveen Naidu <[email protected]>
@Naveenaidu
Naveenaidu force-pushed the wip-naveen-fix-try-remove-from-tree branch from 69b3812 to ba71356 Compare October 14, 2026 10:13
@Naveenaidu
Naveenaidu requested a review from aclamk October 14, 2026 10:16
@Jayaprakash-ibm

Copy link
Copy Markdown
Contributor

This PR has been added to wip-jaya-bs-testing-10-27-2026.

@Jayaprakash-ibm

Copy link
Copy Markdown
Contributor

@Jayaprakash-ibm
Jayaprakash-ibm merged commit d61c277 into ceph:main Nov 2, 2026
16 checks passed
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

This is an automated message by src/script/redmine-upkeep.py.

I have resolved the following tracker ticket due to the merge of this PR:

No backports are pending for the ticket. If this is incorrect, please update the tracker
ticket and reset to Pending Backport state.

Update Log: https://github.com/ceph/ceph/actions/runs/41407990543

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants