linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peng Zhang <zhangpeng.00@bytedance.com>
To: Liam.Howlett@oracle.com, corbet@lwn.net,
	akpm@linux-foundation.org, willy@infradead.org,
	brauner@kernel.org, surenb@google.com,
	michael.christie@oracle.com, mjguzik@gmail.com,
	mathieu.desnoyers@efficios.com, npiggin@gmail.com,
	peterz@infradead.org, oliver.sang@intel.com
Cc: zhangpeng.00@bytedance.com, maple-tree@lists.infradead.org,
	linux-mm@kvack.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: [PATCH v3 0/9] Introduce __mt_dup() to improve the performance of fork()
Date: Mon, 25 Sep 2023 11:56:08 +0800	[thread overview]
Message-ID: <20230925035617.84767-1-zhangpeng.00@bytedance.com> (raw)

Hi all,

This series introduces __mt_dup() to improve the performance of fork(). During
the duplication process of mmap, all VMAs are traversed and inserted one by one
into the new maple tree, causing the maple tree to be rebalanced multiple times.
Balancing the maple tree is a costly operation. To duplicate VMAs more
efficiently, mtree_dup() and __mt_dup() are introduced for the maple tree. They
can efficiently duplicate a maple tree. By applying __mt_dup() to dup_mmap(),
better performance is achieved compared to the original method. After using this
method, the average time complexity decreases from O(n * log(n)) to O(n).

Here are some algorithmic details about {mtree, __mt}_dup(). We perform a DFS
pre-order traversal of all nodes in the source maple tree. During this process,
we fully copy the nodes from the source tree to the new tree. This involves
memory allocation, and when encountering a new node, if it is a non-leaf node,
all its child nodes are allocated at once.
Some previous discussions can be referred to as [1].

There is a "spawn" in byte-unixbench[2], which can be used to test the
performance of fork(). I modified it slightly to make it work with
different number of VMAs.

Below are the test results. By default, there are 21 VMAs. The first row
shows the number of additional VMAs added on top of the default. The last
two rows show the number of fork() calls per ten seconds. The test results
were obtained with CPU binding to avoid scheduler load balancing that
could cause unstable results. There are still some fluctuations in the
test results, but at least they are better than the original performance.

Increment of VMAs: 0      100     200     400     800     1600    3200    6400
next-20230921:     112326 75469   54529   34619   20750   11355   6115    3183
Apply this:        116505 85971   67121   46080   29722   16665   9050    4805
                   +3.72% +13.92% +23.09% +33.11% +43.24% +46.76% +48.00% +50.96%

Thanks to kernel test robot <oliver.sang@intel.com> for reporting the warning
about nested locks.

Thanks to Liam for all the suggestions.

Changes since v2:
 - Some minor modifications to mtree_dup(), __mt_dup() and their test code.
 - Introduce {mtree, mas}_lock_nested() to address lockdep warnings.
 - Update the documentation for maple tree.
 - Introduce undo_dup_mmap() to address the failure of dup_mmap().
 - Performance data was retested based on the latest next-20230921, and there
   were some fluctuations in the results which were expected.

[1] https://lore.kernel.org/lkml/463899aa-6cbd-f08e-0aca-077b0e4e4475@bytedance.com/
[2] https://github.com/kdlucas/byte-unixbench/tree/master

v1: https://lore.kernel.org/lkml/20230726080916.17454-1-zhangpeng.00@bytedance.com/
v2: https://lore.kernel.org/lkml/20230830125654.21257-1-zhangpeng.00@bytedance.com/

Peng Zhang (9):
  maple_tree: Add mt_free_one() and mt_attr() helpers
  maple_tree: Introduce {mtree,mas}_lock_nested()
  maple_tree: Introduce interfaces __mt_dup() and mtree_dup()
  maple_tree: Add test for mtree_dup()
  maple_tree: Update the documentation of maple tree
  maple_tree: Skip other tests when BENCH is enabled
  maple_tree: Update check_forking() and bench_forking()
  maple_tree: Preserve the tree attributes when destroying maple tree
  fork: Use __mt_dup() to duplicate maple tree in dup_mmap()

 Documentation/core-api/maple_tree.rst |   4 +
 include/linux/maple_tree.h            |   7 +
 include/linux/mm.h                    |   1 +
 kernel/fork.c                         |  34 ++-
 lib/maple_tree.c                      | 300 ++++++++++++++++++++-
 lib/test_maple_tree.c                 |  69 +++--
 mm/internal.h                         |   3 +-
 mm/memory.c                           |   7 +-
 mm/mmap.c                             |  52 +++-
 tools/include/linux/spinlock.h        |   1 +
 tools/testing/radix-tree/maple.c      | 363 ++++++++++++++++++++++++++
 11 files changed, 787 insertions(+), 54 deletions(-)

-- 
2.20.1


             reply	other threads:[~2023-09-25  3:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-25  3:56 Peng Zhang [this message]
2023-09-25  3:56 ` [PATCH v3 1/9] maple_tree: Add mt_free_one() and mt_attr() helpers Peng Zhang
2023-09-25  3:56 ` [PATCH v3 2/9] maple_tree: Introduce {mtree,mas}_lock_nested() Peng Zhang
2023-09-25  3:56 ` [PATCH v3 3/9] maple_tree: Introduce interfaces __mt_dup() and mtree_dup() Peng Zhang
2023-10-03 18:45   ` Liam R. Howlett
2023-10-04  9:09     ` Peng Zhang
2023-10-04 14:25       ` Liam R. Howlett
2023-10-05 15:55         ` Peng Zhang
2023-10-05 19:09           ` Liam R. Howlett
2023-09-25  3:56 ` [PATCH v3 4/9] maple_tree: Add test for mtree_dup() Peng Zhang
2023-09-25  3:56 ` [PATCH v3 5/9] maple_tree: Update the documentation of maple tree Peng Zhang
2023-10-03 18:46   ` Liam R. Howlett
2023-10-04  9:09     ` Peng Zhang
2023-09-25  3:56 ` [PATCH v3 6/9] maple_tree: Skip other tests when BENCH is enabled Peng Zhang
2023-09-25  3:56 ` [PATCH v3 7/9] maple_tree: Update check_forking() and bench_forking() Peng Zhang
2023-09-25  3:56 ` [PATCH v3 8/9] maple_tree: Preserve the tree attributes when destroying maple tree Peng Zhang
2023-09-25  3:56 ` [PATCH v3 9/9] fork: Use __mt_dup() to duplicate maple tree in dup_mmap() Peng Zhang
2023-10-03 18:46   ` Liam R. Howlett
2023-10-04  9:10     ` Peng Zhang
2023-10-04 19:53       ` Liam R. Howlett
2023-10-05 15:56         ` Peng Zhang
2023-10-07  1:11           ` Liam R. Howlett
2023-10-07  1:32             ` Liam R. Howlett
2023-10-07  4:25               ` Peng Zhang
2023-10-08  7:54               ` Peng Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230925035617.84767-1-zhangpeng.00@bytedance.com \
    --to=zhangpeng.00@bytedance.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maple-tree@lists.infradead.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=michael.christie@oracle.com \
    --cc=mjguzik@gmail.com \
    --cc=npiggin@gmail.com \
    --cc=oliver.sang@intel.com \
    --cc=peterz@infradead.org \
    --cc=surenb@google.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).