Bug #80417
closedos/bluestore: try_remove_from_tree unable to find the correct overlapping free run in Btree, Btree2 and AVLAllocator
0%
Description
AvlAllocator, BtreeAllocator and Btree2Allocator each implement `try_remove_from_tree(start, size, cb)`, which walks the free runs overlapping `[start, start+size]` , removes the free parts from the range tree and reports every sub-range to a callback as found (was-free) or not found (was allocated). The three allocators seems to some bugs in their implementation.
They are described below:
Btree2Allocator¶
auto rt_p = range_tree.lower_bound(start);
if ((rt_p == range_tree.end() || rt_p->first > start) && rt_p != range_tree.begin()) {
--rt_p;
}
The first issue with Btree2Allocator, is wrong step back. The above is the code snippet for how the range is selected for removal. Let's say we have the following example:
Tree holds one free run: `[0x1000, 0x2000]`, And the Query is `[0x5000, 0x2000)` - Running through the code we see:- `range_tree.lower_bound(0x5000)`, returns back `range_tree.end()` (because lower_back tries to return the run >= 0x5000 and if nothing is there the end is returned)
- `rt_p == range_tree.end()`, condition satisfied - so we go in
- `--rt_p`, rt_p is now `[0x1000, 0x2000)` -> a free run that is in no way associated with the query
The step-back's whole purpose is to catch a run that straddles start. The condition only tests that `lower_bound` didn't land exactly on start; it never checks whether the step backe'd run end reaches past the start. We need to fix this condition.
The second issue, is that we do not progress on a left-over split. after `remove_from_tree()` re-inserts a left-over remainder below start, the loop never skips past it and re-processes it, producing the same `range_end < start` removal. That one additionally requires the query to span more than one free run.
BtreeAllocator¶
There are two issues with BtreeAllocator's `_try_remove_from_tree`:
First issue , It uses `range_tree.find(start)` as it's operation to find an empty run which is an exact-key lookup on a btree_map that returns `range_tree.end()` unless a run begins exactly at start, so a run starting before start and extending into the query is missed entirely.
For example:
Consider range_tree: `[100, 200), [300, 350)`, and the query is `[150, 180)` The `range_tree.find(150)` returns `[300, 350)` -> WRONG
This is because `find()` tries to find an exact run that matches 150, if nothing is found the end is returned. `find()` is not the right operation to use when you can have query that is being straddled by free run. A similar bug was found in `_remove_from_tree` in the Allocator and was removed as part of https://github.com/ceph/ceph/pull/52485/changes#diff-c39481eaa9e770d4173bf717b97878fd09c3780c41014842cd609e2cd5c68936R174
Second issue is, It cached the successor iterator (`next_rs`) before calling `process_range_removal()`, which can insert into range_tree and invalidate it.
Consider range_tree: `[0, 10) [20, 30) [40, 50)`, and we want to remove `[5, 25)`
In the current code, after `_process_range_removal` happens we will have:
before: [0, 10) [20, 30) [40, 50) after: [0, 5) [25, 30) [40, 50)
This is what happens:
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)
AVLAllocator¶
This has the same issue related to using `find()` as we described above for BtreeAllocator. AVLAllocator uses `range_tree.find()` - this tree's compartor is `before_t` which is defined as `lhs.end <= rhs.start`. This is an overlap comparator. For overlap comparators - any overlapping ranges are also compared as equal. Though this helps in finding overlaping runs, but it is totally dependent on the shape of the tree.
The first run that the callback gets - is determined by the rotation history. The forward walk then starts mid-range and silently skips every run to it's left, reporting free space o the callback as not-free.
For example,
range_tree as A=[0,10) B=[20,30) C=[40,50) D=[60,70) E=[80,90) F=[100,110) Query: KEY=[45,85)
We get the following tree:
D=[60,70)
/ \
B=[20,30) F=[100,110)
/ \ /
A=[0,10) C=[40,50) E=[80,90)
With `find()` , D is a valid result because
find() algorithm: Is Query < Node -> Go left, 70 <= 45, NO Is Node < Query -> Go Right, 85 <= 60, NO Neither -> match (equal), return [60,70) If both are no, then they overlap and is a valid find result
The first node we get is `[60,70)` and then we go only towards the right skipping the node `C` which is a valid free space.
NN Updated by Naveen Naidu 3 months ago
- Pull request ID set to 71656
NN Updated by Naveen Naidu 3 months ago
- Status changed from New to Fix Under Review
UB Updated by Upkeep Bot about 1 month ago
- Status changed from Fix Under Review to Resolved
- Merge Commit set to d61c277d1d4e459a0babcde9937ffd8a83fe2a62
- Fixed In set to v22.0.0-284-gd61c277d1d
- Upkeep Timestamp set to 2026-11-02T07:44:07+00:00