Commit Graph

126 Commits

Author SHA1 Message Date
Nico Pache acfd3aca57 maple_tree: fix mas_skip_node() end slot detection
commit 0fa99fdfe1b38da396d0b2d1496a823bcd0ebea0
Author: Liam R. Howlett <Liam.Howlett@oracle.com>
Date:   Tue Mar 7 13:02:46 2023 -0500

    maple_tree: fix mas_skip_node() end slot detection

    Patch series "Fix mas_skip_node() for mas_empty_area()", v2.

    mas_empty_area() was incorrectly returning an error when there was room.
    The issue was tracked down to mas_skip_node() using the incorrect
    end-of-slot count.  Instead of using the nodes hard limit, the limit of
    data should be used.

    mas_skip_node() was also setting the min and max to that of the child
    node, which was unnecessary.  Within these limits being set, there was
    also a bug that corrupted the maple state's max if the offset was set to
    the maximum node pivot.  The bug was without consequence unless there was
    a sufficient gap in the next child node which would cause an error to be
    returned.

    This patch set fixes these errors by removing the limit setting from
    mas_skip_node() and uses the mas_data_end() for slot limits, and adds
    tests for all failures discovered.

    This patch (of 2):

    mas_skip_node() is used to move the maple state to the node with a higher
    limit.  It does this by walking up the tree and increasing the slot count.
    Since slot count may not be able to be increased, it may need to walk up
    multiple times to find room to walk right to a higher limit node.  The
    limit of slots that was being used was the node limit and not the last
    location of data in the node.  This would cause the maple state to be
    shifted outside actual data and enter an error state, thus returning
    -EBUSY.

    The result of the incorrect error state means that mas_awalk() would
    return an error instead of finding the allocation space.

    The fix is to use mas_data_end() in mas_skip_node() to detect the nodes
    data end point and continue walking the tree up until it is safe to move
    to a node with a higher limit.

    The walk up the tree also sets the maple state limits so remove the buggy
    code from mas_skip_node().  Setting the limits had the unfortunate side
    effect of triggering another bug if the parent node was full and the there
    was no suitable gap in the second last child, but room in the next child.

    mas_skip_node() may also be passed a maple state in an error state from
    mas_anode_descend() when no allocations are available.  Return on such an
    error state immediately.

    Link: https://lkml.kernel.org/r/20230307180247.2220303-1-Liam.Howlett@oracle.com
    Link: https://lkml.kernel.org/r/20230307180247.2220303-2-Liam.Howlett@oracle.com
    Fixes: 54a611b60590 ("Maple Tree: add new data structure")
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Reported-by: Snild Dolkow <snild@sony.com>
      Link: https://lore.kernel.org/linux-mm/cb8dc31a-fef2-1d09-f133-e9f7b9f9e77a@sony.com/
    Tested-by: Snild Dolkow <snild@sony.com>
    Cc: Peng Zhang <zhangpeng.00@bytedance.com>
    Cc: <stable@vger.kernel.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-5595
Signed-off-by: Nico Pache <npache@redhat.com>
2023-09-26 10:23:39 -06:00
Nico Pache 76246a2e26 maple_tree: reduce stack usage with gcc-9 and earlier
commit 44081c77e8a4aac9c5a010ed0d9ccdcf684041e1
Author: Arnd Bergmann <arnd@arndb.de>
Date:   Tue Feb 14 11:30:24 2023 +0100

    maple_tree: reduce stack usage with gcc-9 and earlier

    gcc-10 changed the way inlining works to be less aggressive, but older
    versions run into an oversized stack frame warning whenever
    CONFIG_KASAN_STACK is enabled, as that forces variables from inlined
    callees to be non-overlapping:

    lib/maple_tree.c: In function 'mas_wr_bnode':
    lib/maple_tree.c:4320:1: error: the frame size of 1424 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]

    Change the annotations on mas_store_b_node() and mas_commit_b_node()
    to explicitly forbid inlining in this configuration, which is
    the same behavior that newer versions already have.

    Link: https://lkml.kernel.org/r/20230214103030.1051950-1-arnd@kernel.org
    Signed-off-by: Arnd Bergmann <arnd@arndb.de>
    Reviewed-by: David Hildenbrand <david@redhat.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
    Cc: Alexander Potapenko <glider@google.com>
    Cc: Andrey Konovalov <andreyknvl@gmail.com>
    Cc: Dmitry Vyukov <dvyukov@google.com>
    Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
    Cc: Vernon Yang <vernon2gm@gmail.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 26dc7386c3 maple_tree: fix mas_prev() and mas_find() state handling
commit 17dc622c7b0f94e49bed030726df4db12ecaa6b5
Author: Liam R. Howlett <Liam.Howlett@oracle.com>
Date:   Fri Jan 20 11:26:07 2023 -0500

    maple_tree: fix mas_prev() and mas_find() state handling

    When mas_prev() does not find anything, set the state to MAS_NONE.

    Handle the MAS_NONE in mas_find() like a MAS_START.

    Link: https://lkml.kernel.org/r/20230120162650.984577-7-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Reported-by: <syzbot+502859d610c661e56545@syzkaller.appspotmail.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache e525e59fb2 maple_tree: fix handle of invalidated state in mas_wr_store_setup()
commit 1202700c3f8cc5f7e4646c3cf05ee6f7c8bc6ccf
Author: Liam R. Howlett <Liam.Howlett@oracle.com>
Date:   Fri Jan 20 11:26:06 2023 -0500

    maple_tree: fix handle of invalidated state in mas_wr_store_setup()

    If an invalidated maple state is encountered during write, reset the maple
    state to MAS_START.  This will result in a re-walk of the tree to the
    correct location for the write.

    Link: https://lore.kernel.org/all/20230107020126.1627-1-sj@kernel.org/
    Link: https://lkml.kernel.org/r/20230120162650.984577-6-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Reported-by: SeongJae Park <sj@kernel.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache e741f086cc maple_tree: reduce user error potential
commit 50e81c82ad947045c7ed26ddc9acb17276b653b6
Author: Liam R. Howlett <Liam.Howlett@Oracle.com>
Date:   Fri Jan 20 11:26:04 2023 -0500

    maple_tree: reduce user error potential

    When iterating, a user may operate on the tree and cause the maple state
    to be altered and left in an unintuitive state.  Detect this scenario and
    correct it by setting to the limit and invalidating the state.

    Link: https://lkml.kernel.org/r/20230120162650.984577-4-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 7b544d6c1a maple_tree: fix potential rcu issue
commit 65be6f058b0eba98dc6c6f197ea9f62c9b6a519f
Author: Liam R. Howlett <Liam.Howlett@Oracle.com>
Date:   Fri Jan 20 11:26:03 2023 -0500

    maple_tree: fix potential rcu issue

    Ensure the node isn't dead after reading the node end.

    Link: https://lkml.kernel.org/r/20230120162650.984577-3-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 81d5cb47c5 maple_tree: fix comment of mte_destroy_walk
commit f942b0f0528d1198b94b8211c84d4f28a654c0ff
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Jan 11 21:53:48 2023 +0800

    maple_tree: fix comment of mte_destroy_walk

    The parameter name of maple tree is mt, make the comment be mt instead of
    mn, and the separator between the parameter name and the description to be
    : instead of -.

    Link: https://lkml.kernel.org/r/20230111135348.803181-1-vernon2gm@gmail.com
    Fixes: 54a611b60590 ("Maple Tree: add new data structure")
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
    Cc: Matthew Wilcox <willy@infradead.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 26f9be1361 maple_tree: remove GFP_ZERO from kmem_cache_alloc() and kmem_cache_alloc_bulk()
commit 541e06b772c1aaffb3b6a245ccface36d7107af2
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Thu Jan 5 16:05:34 2023 +0000

    maple_tree: remove GFP_ZERO from kmem_cache_alloc() and kmem_cache_alloc_bulk()

    Preallocations are common in the VMA code to avoid allocating under
    certain locking conditions.  The preallocations must also cover the
    worst-case scenario.  Removing the GFP_ZERO flag from the
    kmem_cache_alloc() (and bulk variant) calls will reduce the amount of time
    spent zeroing memory that may not be used.  Only zero out the necessary
    area to keep track of the allocations in the maple state.  Zero the entire
    node prior to using it in the tree.

    This required internal changes to node counting on allocation, so the test
    code is also updated.

    This restores some micro-benchmark performance: up to +9% in mmtests mmap1
    by my testing +10% to +20% in mmap, mmapaddr, mmapmany tests reported by
    Red Hat

    Link: https://bugzilla.redhat.com/show_bug.cgi?id=2149636
    Link: https://lkml.kernel.org/r/20230105160427.2988454-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam Howlett <Liam.Howlett@oracle.com>
    Reported-by: Jirka Hladky <jhladky@redhat.com>
    Suggested-by: Matthew Wilcox (Oracle) <willy@infradead.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 8aa254f7ff maple_tree: refine mab_calc_split function
commit e11cb683b2ebc6699bc0ca200442f1b80a51553f
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Dec 21 14:00:58 2022 +0800

    maple_tree: refine mab_calc_split function

    Invert the conditional judgment of the mid_split, to focus the return
    statement in the last statement, which is easier to understand and for
    better readability.

    Link: https://lkml.kernel.org/r/20221221060058.609003-8-vernon2gm@gmail.com
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 5b3c6152a1 maple_tree: refine ma_state init from mas_start()
commit 46b345848261009477552d654cb2f65000c30e4d
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Dec 21 14:00:57 2022 +0800

    maple_tree: refine ma_state init from mas_start()

    If mas->node is an MAS_START, there are three cases, and they all assign
    different values to mas->node and mas->offset.  So there is no need to set
    them to a default value before updating.

    Update them directly to make them easier to understand and for better
    readability.

    Link: https://lkml.kernel.org/r/20221221060058.609003-7-vernon2gm@gmail.com
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache 4449b23e5b maple_tree: use macro MA_ROOT_PARENT instead of number
commit 84fd3e1ee395649ac45b7317d44c10b33d0dca79
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Dec 21 14:00:55 2022 +0800

    maple_tree: use macro MA_ROOT_PARENT instead of number

    When you need to compare whether node->parent is parent of the
    root node, using macro MA_ROOT_PARENT is easier to understand
    and for better readability.

    Link: https://lkml.kernel.org/r/20221221060058.609003-5-vernon2gm@gmail.com
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache c2d198f6a5 maple_tree: use mt_node_max() instead of direct operations mt_max[]
commit bd592703b81a95473f6a01fe731beccd0992236e
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Dec 21 14:00:54 2022 +0800

    maple_tree: use mt_node_max() instead of direct operations mt_max[]

    Use mt_node_max() to get the maximum number of slots for a node,
    rather than direct operations mt_max[], makes it better portability.

    Link: https://lkml.kernel.org/r/20221221060058.609003-4-vernon2gm@gmail.com
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:57 -07:00
Nico Pache d58df5e442 maple_tree: remove extra return statement
commit d56c593c8e128c42dc81707c07cbd5af41862214
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Dec 21 14:00:53 2022 +0800

    maple_tree: remove extra return statement

    For functions with a return type of void, it is unnecessary to
    add a reurn statement at the end of the function, so drop it.

    Link: https://lkml.kernel.org/r/20221221060058.609003-3-vernon2gm@gmail.com
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:56 -07:00
Nico Pache 5dfc6fc447 maple_tree: remove extra space and blank line
commit 831978e37e93bd3e36612917a4b193278950daff
Author: Vernon Yang <vernon2gm@gmail.com>
Date:   Wed Dec 21 14:00:52 2022 +0800

    maple_tree: remove extra space and blank line

    Patch series "Clean up and refinement for maple tree", v2.

    This patchset cleans up and refines some maple tree code.  A few small
    changes make the code easier to understand and for better readability.

    This patch (of 7):

    These extra space and blank lines are unnecessary, so drop them.

    Link: https://lkml.kernel.org/r/20221221060058.609003-1-vernon2gm@gmail.com
    Link: https://lkml.kernel.org/r/20221221060058.609003-2-vernon2gm@gmail.com
    Signed-off-by: Vernon Yang <vernon2gm@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:56 -07:00
Nico Pache 454461ae1d maple_tree: should get pivots boundary by type
commit ab6ef70a8b0d314c2160af70b0de984664d675e0
Author: Wei Yang <richard.weiyang@gmail.com>
Date:   Sat Nov 12 23:43:08 2022 +0000

    maple_tree: should get pivots boundary by type

    We should get pivots boundary by type.  Fixes a potential overindexing of
    mt_pivots[].

    Link: https://lkml.kernel.org/r/20221112234308.23823-1-richard.weiyang@gmail.com
    Fixes: 54a611b60590 ("Maple Tree: add new data structure")
    Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:56 -07:00
Nico Pache dc7b3331af maple_tree: fix mas_empty_area_rev() lower bound validation
commit 7327e8111adb315423035fb5233533016dfd3f2e
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Wed Jan 11 20:02:07 2023 +0000

    maple_tree: fix mas_empty_area_rev() lower bound validation

    mas_empty_area_rev() was not correctly validating the start of a gap
    against the lower limit.  This could lead to the range starting lower than
    the requested minimum.

    Fix the issue by better validating a gap once one is found.

    This commit also adds tests to the maple tree test suite for this issue
    and tests the mas_empty_area() function for similar bound checking.

    Link: https://lkml.kernel.org/r/20230111200136.1851322-1-Liam.Howlett@oracle.com
    Link: https://bugzilla.kernel.org/show_bug.cgi?id=216911
    Fixes: 54a611b60590 ("Maple Tree: add new data structure")
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Reported-by: <amanieu@gmail.com>
      Link: https://lore.kernel.org/linux-mm/0b9f5425-08d4-8013-aa4c-e620c3b10bb2@leemhuis.info/
    Tested-by: Holger Hoffsttte <holger@applied-asynchrony.com>
    Cc: <stable@vger.kernel.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:40:56 -07:00
Nico Pache eb6cb47f00 maple_tree: fix mas_spanning_rebalance() on insufficient data
commit 0abb964aae3da746ea2fd4301599a6fa26da58db
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Mon Dec 19 16:20:15 2022 +0000

    maple_tree: fix mas_spanning_rebalance() on insufficient data

    Mike Rapoport contacted me off-list with a regression in running criu.
    Periodic tests fail with an RCU stall during execution.  Although rare, it
    is possible to hit this with other uses so this patch should be backported
    to fix the regression.

    This patchset adds the fix and a test case to the maple tree test
    suite.

    This patch (of 2):

    An insufficient node was causing an out-of-bounds access on the node in
    mas_leaf_max_gap().  The cause was the faulty detection of the new node
    being a root node when overwriting many entries at the end of the tree.

    Fix the detection of a new root and ensure there is sufficient data prior
    to entering the spanning rebalance loop.

    Link: https://lkml.kernel.org/r/20221219161922.2708732-1-Liam.Howlett@oracle.com
    Link: https://lkml.kernel.org/r/20221219161922.2708732-2-Liam.Howlett@oracle.com
    Fixes: 54a611b60590 ("Maple Tree: add new data structure")
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Reported-by: Mike Rapoport <rppt@kernel.org>
    Tested-by: Mike Rapoport <rppt@kernel.org>
    Cc: Andrei Vagin <avagin@gmail.com>
    Cc: Mike Rapoport <rppt@kernel.org>
    Cc: Muhammad Usama Anjum <usama.anjum@collabora.com>
    Cc: <stable@vger.kernel.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache 32c3f90a86 maple_tree: fix mas_find_rev() comment
commit d98c86b9f7a4e1f5a7ead8ba5743952267f9e320
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Tue Oct 25 17:38:14 2022 +0000

    maple_tree: fix mas_find_rev() comment

    mas_find_rev() uses mas_prev_entry(), not mas_next_entry(), correct comment.

    Link: https://lkml.kernel.org/r/20221025173756.2719616-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache a685bf6ef1 maple_tree: mte_set_full() and mte_clear_full() clang-analyzer clean up
commit 6e7ba8b5e2380f941dda8a1025d70c5ce5b38982
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Fri Oct 28 14:45:34 2022 +0000

    maple_tree: mte_set_full() and mte_clear_full() clang-analyzer clean up

    mte_set_full() and mte_clear_full() were incorrectly setting a pointer to
    a value without returning a result.  Fix this by returning the modified
    pointer to be use as necessary.  Also add a third function to return if
    the bit is set or not.

    Link: https://lore.kernel.org/lkml/20221026120029.12555-1-lukas.bulwahn@gmail.com/
    Link: https://lkml.kernel.org/r/20221028144520.2776767-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
    Suggested-by: Dan Carpenter <dan.carpenter@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache 410a81365d maple_tree: don't set a new maximum on the node when not reusing nodes
commit 7dc5ba6254bb242a9f45e43549171a2d84d25e6a
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Mon Nov 7 16:39:02 2022 +0000

    maple_tree: don't set a new maximum on the node when not reusing nodes

    In RCU mode, the node limits were being updated to the last pivot which
    may not be correct and would cause the metadata to be set when it
    shouldn't.  Fix this by not setting a new limit in this case.

    Link: https://lkml.kernel.org/r/20221107163857.867377-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache a87aa05f33 maple_tree: fix depth tracking in maple_state
commit 9bbba5633488ee3e2903647c3484c4390ff39ea7
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Mon Nov 7 16:38:35 2022 +0000

    maple_tree: fix depth tracking in maple_state

    It is possible to confuse the depth tracking in the maple state by
    searching the same node for values.  Fix the depth tracking by moving
    where the depth is incremented closer to where the node changes level.
    Also change the initial depth setting when using the root node.

    Link: https://lkml.kernel.org/r/20221107163814.866612-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache d5392be061 maple_tree: reorganize testing to restore module testing
Conflicts:
       lib/Kconfig.debug: was not expecting TEST_XARRAY config

commit 120b116208a0877227fc82e3f0df81e7a3ed4ab1
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Fri Oct 28 18:04:30 2022 +0000

    maple_tree: reorganize testing to restore module testing

    Along the development cycle, the testing code support for module/in-kernel
    compiles was removed.  Restore this functionality by moving any internal
    API tests to the userspace side, as well as threading tests.  Fix the
    lockdep issues and add a way to reduce memory usage so the tests can
    complete with KASAN + memleak detection.  Make the tests work on 32 bit
    hosts where possible and detect 32 bit hosts in the radix test suite.

    [akpm@linux-foundation.org: fix module export]
    [akpm@linux-foundation.org: fix it some more]
    [liam.howlett@oracle.com: fix compile warnings on 32bit build in check_find()]
      Link: https://lkml.kernel.org/r/20221107203816.1260327-1-Liam.Howlett@oracle.com
    Link: https://lkml.kernel.org/r/20221028180415.3074673-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache 865b611af3 maple_tree: mas_anode_descend() clang-analyzer cleanup
commit 9a887877ef981e5a185a84339603300cf2eb1900
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Wed Oct 26 15:14:31 2022 +0000

    maple_tree: mas_anode_descend() clang-analyzer cleanup

    clang-analyzer reported some Dead Stores in mas_anode_descend().  Upon
    inspection, there were a few clean ups that would make the code cleaner:

    The count variable was set from the mt_slots array and then updated but
    never used again.  Just use the array reference directly.

    Also stop updating the type since it isn't used after the update.

    Stop setting the gaps pointer to NULL at the start since it is always
    set before the loop begins.

    Link: https://lkml.kernel.org/r/20221026151413.4032730-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache 7f683ad072 maple_tree: remove pointer to pointer use in mas_alloc_nodes()
commit c61b3a2b2d9bb36698f8c2f65aa41ba183815264
Author: Liam Howlett <liam.howlett@oracle.com>
Date:   Wed Oct 26 15:13:29 2022 +0000

    maple_tree: remove pointer to pointer use in mas_alloc_nodes()

    There is a more direct and cleaner way of implementing the same functional
    code.  Remove the confusing and unnecessary use of pointers here.

    Link: https://lkml.kernel.org/r/20221026151241.4031117-1-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache cdecaede75 lib: maple_tree: remove unneeded initialization in mtree_range_walk()
commit 1b9c918318476b4441ddd754ee6699b5367bb5ee
Author: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Date:   Wed Oct 26 14:00:29 2022 +0200

    lib: maple_tree: remove unneeded initialization in mtree_range_walk()

    Before the do-while loop in mtree_range_walk(), the variables next, min,
    max need to be initialized.  The variables last, prev_min and prev_max are
    set within the loop body before they are eventually used after exiting the
    loop body.

    As it is a do-while loop, the loop body is executed at least once, so the
    variables last, prev_min and prev_max do not need to be initialized before
    the loop body.

    Remove unneeded initialization of last and prev_min.

    The needless initialization was reported by clang-analyzer as Dead Stores.

    As the compiler already identifies these assignments as unneeded, it
    optimizes the assignments away.  Hence:

    No functional change. No change in object code.

    Link: https://lkml.kernel.org/r/20221026120029.12555-2-lukas.bulwahn@gmail.com
    Signed-off-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
    Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Cc: Matthew Wilcox <willy@infradead.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:55 -07:00
Nico Pache d0b81c5b5a Maple Tree: add new data structure
Conflicts:
       lib/Makefile: slight makefile conflict

commit 54a611b605901c7d5d05b6b8f5d04a6ceb0962aa
Author: Liam R. Howlett <Liam.Howlett@Oracle.com>
Date:   Tue Sep 6 19:48:39 2022 +0000

    Maple Tree: add new data structure

    Patch series "Introducing the Maple Tree"

    The maple tree is an RCU-safe range based B-tree designed to use modern
    processor cache efficiently.  There are a number of places in the kernel
    that a non-overlapping range-based tree would be beneficial, especially
    one with a simple interface.  If you use an rbtree with other data
    structures to improve performance or an interval tree to track
    non-overlapping ranges, then this is for you.

    The tree has a branching factor of 10 for non-leaf nodes and 16 for leaf
    nodes.  With the increased branching factor, it is significantly shorter
    than the rbtree so it has fewer cache misses.  The removal of the linked
    list between subsequent entries also reduces the cache misses and the need
    to pull in the previous and next VMA during many tree alterations.

    The first user that is covered in this patch set is the vm_area_struct,
    where three data structures are replaced by the maple tree: the augmented
    rbtree, the vma cache, and the linked list of VMAs in the mm_struct.  The
    long term goal is to reduce or remove the mmap_lock contention.

    The plan is to get to the point where we use the maple tree in RCU mode.
    Readers will not block for writers.  A single write operation will be
    allowed at a time.  A reader re-walks if stale data is encountered.  VMAs
    would be RCU enabled and this mode would be entered once multiple tasks
    are using the mm_struct.

    Davidlor said

    : Yes I like the maple tree, and at this stage I don't think we can ask for
    : more from this series wrt the MM - albeit there seems to still be some
    : folks reporting breakage.  Fundamentally I see Liam's work to (re)move
    : complexity out of the MM (not to say that the actual maple tree is not
    : complex) by consolidating the three complimentary data structures very
    : much worth it considering performance does not take a hit.  This was very
    : much a turn off with the range locking approach, which worst case scenario
    : incurred in prohibitive overhead.  Also as Liam and Matthew have
    : mentioned, RCU opens up a lot of nice performance opportunities, and in
    : addition academia[1] has shown outstanding scalability of address spaces
    : with the foundation of replacing the locked rbtree with RCU aware trees.

    A similar work has been discovered in the academic press

            https://pdos.csail.mit.edu/papers/rcuvm:asplos12.pdf

    Sheer coincidence.  We designed our tree with the intention of solving the
    hardest problem first.  Upon settling on a b-tree variant and a rough
    outline, we researched ranged based b-trees and RCU b-trees and did find
    that article.  So it was nice to find reassurances that we were on the
    right path, but our design choice of using ranges made that paper unusable
    for us.

    This patch (of 70):

    The maple tree is an RCU-safe range based B-tree designed to use modern
    processor cache efficiently.  There are a number of places in the kernel
    that a non-overlapping range-based tree would be beneficial, especially
    one with a simple interface.  If you use an rbtree with other data
    structures to improve performance or an interval tree to track
    non-overlapping ranges, then this is for you.

    The tree has a branching factor of 10 for non-leaf nodes and 16 for leaf
    nodes.  With the increased branching factor, it is significantly shorter
    than the rbtree so it has fewer cache misses.  The removal of the linked
    list between subsequent entries also reduces the cache misses and the need
    to pull in the previous and next VMA during many tree alterations.

    The first user that is covered in this patch set is the vm_area_struct,
    where three data structures are replaced by the maple tree: the augmented
    rbtree, the vma cache, and the linked list of VMAs in the mm_struct.  The
    long term goal is to reduce or remove the mmap_lock contention.

    The plan is to get to the point where we use the maple tree in RCU mode.
    Readers will not block for writers.  A single write operation will be
    allowed at a time.  A reader re-walks if stale data is encountered.  VMAs
    would be RCU enabled and this mode would be entered once multiple tasks
    are using the mm_struct.

    There is additional BUG_ON() calls added within the tree, most of which
    are in debug code.  These will be replaced with a WARN_ON() call in the
    future.  There is also additional BUG_ON() calls within the code which
    will also be reduced in number at a later date.  These exist to catch
    things such as out-of-range accesses which would crash anyways.

    Link: https://lkml.kernel.org/r/20220906194824.2110408-1-Liam.Howlett@oracle.com
    Link: https://lkml.kernel.org/r/20220906194824.2110408-2-Liam.Howlett@oracle.com
    Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
    Tested-by: David Howells <dhowells@redhat.com>
    Tested-by: Sven Schnelle <svens@linux.ibm.com>
    Tested-by: Yu Zhao <yuzhao@google.com>
    Cc: Vlastimil Babka <vbabka@suse.cz>
    Cc: David Hildenbrand <david@redhat.com>
    Cc: Davidlohr Bueso <dave@stgolabs.net>
    Cc: Catalin Marinas <catalin.marinas@arm.com>
    Cc: SeongJae Park <sj@kernel.org>
    Cc: Will Deacon <will@kernel.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166668
Signed-off-by: Nico Pache <npache@redhat.com>
2023-03-07 01:23:54 -07:00