maple_tree: Fix mas_prev() state regression.
Subsystems (4)
v2 cover maple_tree: Fix mas_prev() state regression.
Pedro Falcato retported an mprotect regression [1] which was bisected back to the iterator changes for maple tree. Root cause analysis showed the mas_prev() running off the end of the VMA space (previous from 0) followed by mas_find(), would skip the first value.
This patch set introduces maple state underflow/overflow so the sequence of calls on the maple state will return what the user expects.
Users who encounter this bug may see mprotect(), userfaultfd_register(), and mlock() fail on VMAs mapped with address 0.
[1] https://bugs.archlinux.org/task/79656
Liam R. Howlett (2):
maple_tree: Add mas_is_active() to detect in-tree walks
maple_tree: Add MAS_UNDERFLOW and MAS_OVERFLOW states
include/linux/maple_tree.h | 11 ++ lib/maple_tree.c | 221 +++++++++++++++++++++++++++---------- lib/test_maple_tree.c | 87 ++++++++++++--- 3 files changed, 246 insertions(+), 73 deletions(-)
Signature · 1 line
2.40.1
PATCH v2 1/2 maple_tree: Add mas_is_active() to detect in-tree walks
Instead of constantly checking each possibility of the maple state, create a fast path that will skip over checking unlikely states.
--- include/linux/maple_tree.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/linux/maple_tree.h +9 -0
@@ -511,6 +511,15 @@ static inline bool mas_is_paused(const struct ma_state *mas)return mas->node ==MAS_PAUSE ; } +/* Check if the mas is pointing to a node or not */ +static inline bool mas_is_active (struct ma_state *mas) +{ +if ((unsigned long )mas->node >=MAPLE_RESERVED_RANGE ) +return true; + +return false; +} +/** * mas_reset() - Reset a Maple Tree operation state. * @mas: Maple Tree operation state.
Signature · 1 line
2.40.1
PATCH v2 2/2 maple_tree: Add MAS_UNDERFLOW and MAS_OVERFLOW states
In reply to Liam R. HowlettWhen updating the maple tree iterator to avoid rewalks, an issue was introduced when shifting beyond the limits. This can be seen by trying to go to the previous address of 0, which would set the maple node to MAS_NONE and keep the range as the last entry.
Subsequent calls to mas_find() would then search upwards from mas->last and skip the value at mas->index/mas->last. This showed up as a bug in mprotect which skips the actual VMA at the current range after attempting to go to the previous VMA from 0.
Since MAS_NONE may already be set when searching for a value that isn't contained within a node, changing the handling of MAS_NONE in mas_find() would make the code more complicated and error prone. Furthermore, there was no way to tell which limit was hit, and thus which action to take (next or the entry at the current range).
This solution is to add two states to track what happened with the previous iterator action. This allows for the expected behaviour of the next command to return the correct item (either the item at the range requested, or the next/previous).
Tests are also added and updated accordingly.
--- include/linux/maple_tree.h | 2 + lib/maple_tree.c | 221 +++++++++++++++++++++++++++---------- lib/test_maple_tree.c | 87 ++++++++++++--- 3 files changed, 237 insertions(+), 73 deletions(-)
include/linux/maple_tree.h +2 -0
@@ -428,6 +428,8 @@ struct ma_wr_state {#define MAS_ROOT ((struct maple_enode *)5UL)#define MAS_NONE ((struct maple_enode *)9UL)#define MAS_PAUSE ((struct maple_enode *)17UL) +#define MAS_OVERFLOW ((struct maple_enode *)33UL) +#define MAS_UNDERFLOW ((struct maple_enode *)65UL)#define MA_ERROR (err) \ ((struct maple_enode *)(((unsigned long)err << 2) | 2UL))
lib/maple_tree.c +163 -58
458 lines not on this page · raw message
lib/test_maple_tree.c +72 -15
193 lines not on this page · raw message
Signature · 1 line
2.40.1